Merge commit 'origin/gallium-0.1' into gallium-0.2
[mesa.git] / src / gallium / winsys / xlib / xm_image.c
1 /**************************************************************************
2
3 Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
4 All Rights Reserved.
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sub license, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice (including the
15 next paragraph) shall be included in all copies or substantial portions
16 of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
22 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Kevin E. Martin <kevin@precisioninsight.com>
31 * Brian Paul <brian@precisioninsight.com>
32 */
33
34 #include <stdlib.h>
35 #include <X11/Xmd.h>
36
37 #include "glxheader.h"
38 #include "xmesaP.h"
39
40 #ifdef XFree86Server
41
42 #ifdef ROUNDUP
43 #undef ROUNDUP
44 #endif
45
46 #define ROUNDUP(nbytes, pad) ((((nbytes) + ((pad)-1)) / (pad)) * ((pad)>>3))
47
48 XMesaImage *XMesaCreateImage(int bitsPerPixel, int width, int height, char *data)
49 {
50 XMesaImage *image;
51
52 image = (XMesaImage *)xalloc(sizeof(XMesaImage));
53
54 if (image) {
55 image->width = width;
56 image->height = height;
57 image->data = data;
58 /* Always pad to 32 bits */
59 image->bytes_per_line = ROUNDUP((bitsPerPixel * width), 32);
60 image->bits_per_pixel = bitsPerPixel;
61 }
62
63 return image;
64 }
65
66 void XMesaDestroyImage(XMesaImage *image)
67 {
68 if (image->data)
69 free(image->data);
70 xfree(image);
71 }
72
73 unsigned long XMesaGetPixel(XMesaImage *image, int x, int y)
74 {
75 CARD8 *row = (CARD8 *)(image->data + y*image->bytes_per_line);
76 CARD8 *i8;
77 CARD16 *i16;
78 CARD32 *i32;
79 switch (image->bits_per_pixel) {
80 case 8:
81 i8 = (CARD8 *)row;
82 return i8[x];
83 break;
84 case 15:
85 case 16:
86 i16 = (CARD16 *)row;
87 return i16[x];
88 break;
89 case 24: /* WARNING: architecture specific code */
90 i8 = (CARD8 *)row;
91 return (((CARD32)i8[x*3]) |
92 (((CARD32)i8[x*3+1])<<8) |
93 (((CARD32)i8[x*3+2])<<16));
94 break;
95 case 32:
96 i32 = (CARD32 *)row;
97 return i32[x];
98 break;
99 }
100 return 0;
101 }
102
103 #ifndef XMESA_USE_PUTPIXEL_MACRO
104 void XMesaPutPixel(XMesaImage *image, int x, int y, unsigned long pixel)
105 {
106 CARD8 *row = (CARD8 *)(image->data + y*image->bytes_per_line);
107 CARD8 *i8;
108 CARD16 *i16;
109 CARD32 *i32;
110 switch (image->bits_per_pixel) {
111 case 8:
112 i8 = (CARD8 *)row;
113 i8[x] = (CARD8)pixel;
114 break;
115 case 15:
116 case 16:
117 i16 = (CARD16 *)row;
118 i16[x] = (CARD16)pixel;
119 break;
120 case 24: /* WARNING: architecture specific code */
121 i8 = (CARD8 *)__row;
122 i8[x*3] = (CARD8)(p);
123 i8[x*3+1] = (CARD8)(p>>8);
124 i8[x*3+2] = (CARD8)(p>>16);
125 case 32:
126 i32 = (CARD32 *)row;
127 i32[x] = (CARD32)pixel;
128 break;
129 }
130 }
131 #endif
132
133 #endif /* XFree86Server */