6af773f538439deded6efce07c9dc6eab0cbd0b1
[mesa.git] / src / mesa / drivers / x11 / 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 #ifdef HAVE_DIX_CONFIG_H
35 #include <dix-config.h>
36 #endif
37
38 #include <stdlib.h>
39
40 #include "xf86glx_util.h"
41 #include <X11/Xmd.h>
42
43 #ifdef ROUNDUP
44 #undef ROUNDUP
45 #endif
46
47 #define ROUNDUP(nbytes, pad) ((((nbytes) + ((pad)-1)) / (pad)) * ((pad)>>3))
48
49 XMesaImage *XMesaCreateImage(int bitsPerPixel, int width, int height, char *data)
50 {
51 XMesaImage *image;
52
53 image = (XMesaImage *)xalloc(sizeof(XMesaImage));
54
55 if (image) {
56 image->width = width;
57 image->height = height;
58 image->data = data;
59 /* Always pad to 32 bits */
60 image->bytes_per_line = ROUNDUP((bitsPerPixel * width), 32);
61 image->bits_per_pixel = bitsPerPixel;
62 }
63
64 return image;
65 }
66
67 void XMesaDestroyImage(XMesaImage *image)
68 {
69 if (image->data)
70 free(image->data);
71 xfree(image);
72 }
73
74 unsigned long XMesaGetPixel(XMesaImage *image, int x, int y)
75 {
76 CARD8 *row = (CARD8 *)(image->data + y*image->bytes_per_line);
77 CARD8 *i8;
78 CARD16 *i16;
79 CARD32 *i32;
80 switch (image->bits_per_pixel) {
81 case 8:
82 i8 = (CARD8 *)row;
83 return i8[x];
84 break;
85 case 15:
86 case 16:
87 i16 = (CARD16 *)row;
88 return i16[x];
89 break;
90 case 24: /* WARNING: architecture specific code */
91 i8 = (CARD8 *)row;
92 return (((CARD32)i8[x*3]) |
93 (((CARD32)i8[x*3+1])<<8) |
94 (((CARD32)i8[x*3+2])<<16));
95 break;
96 case 32:
97 i32 = (CARD32 *)row;
98 return i32[x];
99 break;
100 }
101 return 0;
102 }
103
104 #ifndef XMESA_USE_PUTPIXEL_MACRO
105 void XMesaPutPixel(XMesaImage *image, int x, int y, unsigned long pixel)
106 {
107 CARD8 *row = (CARD8 *)(image->data + y*image->bytes_per_line);
108 CARD8 *i8;
109 CARD16 *i16;
110 CARD32 *i32;
111 switch (image->bits_per_pixel) {
112 case 8:
113 i8 = (CARD8 *)row;
114 i8[x] = (CARD8)pixel;
115 break;
116 case 15:
117 case 16:
118 i16 = (CARD16 *)row;
119 i16[x] = (CARD16)pixel;
120 break;
121 case 24: /* WARNING: architecture specific code */
122 i8 = (CARD8 *)__row;
123 i8[x*3] = (CARD8)(p);
124 i8[x*3+1] = (CARD8)(p>>8);
125 i8[x*3+2] = (CARD8)(p>>16);
126 case 32:
127 i32 = (CARD32 *)row;
128 i32[x] = (CARD32)pixel;
129 break;
130 }
131 }
132 #endif
133
134 void XMesaPutImageHelper(ScreenPtr display,
135 DrawablePtr d, GCPtr gc,
136 XMesaImage *image,
137 int src_x, int src_y,
138 int dest_x, int dest_y,
139 unsigned int width, unsigned int height)
140 {
141 /* NOT_DONE: Verify that the following works for all depths */
142 char *src = (image->data +
143 src_y * image->bytes_per_line +
144 ((src_x * image->bits_per_pixel) >> 3));
145
146 ValidateGC(d, gc);
147 (*gc->ops->PutImage)(d, gc, d->depth, dest_x, dest_y, width, height,
148 0, ZPixmap, src);
149 }