glx: split out current context code
[mesa.git] / src / glx / x11 / pixel.c
1 /* $XFree86: xc/lib/GL/glx/pixel.c,v 1.8 2003/09/28 20:15:04 alanh Exp $ */
2 /*
3 ** License Applicability. Except to the extent portions of this file are
4 ** made subject to an alternative license as permitted in the SGI Free
5 ** Software License B, Version 1.1 (the "License"), the contents of this
6 ** file are subject only to the provisions of the License. You may not use
7 ** this file except in compliance with the License. You may obtain a copy
8 ** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
9 ** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
10 **
11 ** http://oss.sgi.com/projects/FreeB
12 **
13 ** Note that, as provided in the License, the Software is distributed on an
14 ** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
15 ** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
16 ** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
17 ** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
18 **
19 ** Original Code. The Original Code is: OpenGL Sample Implementation,
20 ** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
21 ** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
22 ** Copyright in any portions created by third parties is as indicated
23 ** elsewhere herein. All Rights Reserved.
24 **
25 ** Additional Notice Provisions: The application programming interfaces
26 ** established by SGI in conjunction with the Original Code are The
27 ** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
28 ** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
29 ** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
30 ** Window System(R) (Version 1.3), released October 19, 1998. This software
31 ** was created using the OpenGL(R) version 1.2.1 Sample Implementation
32 ** published by SGI, but has not been independently verified as being
33 ** compliant with the OpenGL(R) version 1.2.1 Specification.
34 **
35 */
36
37 #include "packrender.h"
38
39 static const GLubyte MsbToLsbTable[256] = {
40 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
41 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
42 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
43 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
44 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
45 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
46 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
47 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
48 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
49 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
50 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
51 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
52 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
53 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
54 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
55 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
56 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
57 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
58 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
59 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
60 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
61 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
62 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
63 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
64 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
65 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
66 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
67 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
68 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
69 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
70 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
71 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
72 };
73
74 static const GLubyte LowBitsMask[9] = {
75 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff,
76 };
77
78 static const GLubyte HighBitsMask[9] = {
79 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff,
80 };
81
82
83 /*
84 ** Copy bitmap data from clients packed memory applying unpacking modes as the
85 ** data is transfered into the destImage buffer. Return in modes the
86 ** set of pixel modes that are to be done by the server.
87 */
88 static void FillBitmap(__GLXcontext *gc, GLint width, GLint height,
89 GLenum format, const GLvoid *userdata,
90 GLubyte *destImage)
91 {
92 const __GLXattribute * state = gc->client_state_private;
93 GLint rowLength = state->storeUnpack.rowLength;
94 GLint alignment = state->storeUnpack.alignment;
95 GLint skipPixels = state->storeUnpack.skipPixels;
96 GLint skipRows = state->storeUnpack.skipRows;
97 GLint lsbFirst = state->storeUnpack.lsbFirst;
98 GLint elementsLeft, bitOffset, currentByte, nextByte, highBitMask;
99 GLint lowBitMask, i;
100 GLint components, groupsPerRow, rowSize, padding, elementsPerRow;
101 const GLubyte *start, *iter;
102
103 if (rowLength > 0) {
104 groupsPerRow = rowLength;
105 } else {
106 groupsPerRow = width;
107 }
108 components = __glElementsPerGroup(format,GL_BITMAP);
109 rowSize = (groupsPerRow * components + 7) >> 3;
110 padding = (rowSize % alignment);
111 if (padding) {
112 rowSize += alignment - padding;
113 }
114 start = ((const GLubyte*) userdata) + skipRows * rowSize +
115 ((skipPixels * components) >> 3);
116 bitOffset = (skipPixels * components) & 7;
117 highBitMask = LowBitsMask[8-bitOffset];
118 lowBitMask = HighBitsMask[bitOffset];
119 elementsPerRow = width * components;
120 for (i = 0; i < height; i++) {
121 elementsLeft = elementsPerRow;
122 iter = start;
123 while (elementsLeft) {
124 /* First retrieve low bits from current byte */
125 if (lsbFirst) {
126 currentByte = MsbToLsbTable[iter[0]];
127 } else {
128 currentByte = iter[0];
129 }
130 if (bitOffset) {
131 /* Need to read next byte to finish current byte */
132 if (elementsLeft > (8 - bitOffset)) {
133 if (lsbFirst) {
134 nextByte = MsbToLsbTable[iter[1]];
135 } else {
136 nextByte = iter[1];
137 }
138 currentByte =
139 ((currentByte & highBitMask) << bitOffset) |
140 ((nextByte & lowBitMask) >> (8 - bitOffset));
141 } else {
142 currentByte =
143 ((currentByte & highBitMask) << bitOffset);
144 }
145 }
146 if (elementsLeft >= 8) {
147 *destImage = currentByte;
148 elementsLeft -= 8;
149 } else {
150 *destImage = currentByte & HighBitsMask[elementsLeft];
151 elementsLeft = 0;
152 }
153 destImage++;
154 iter++;
155 }
156 start += rowSize;
157 }
158 }
159
160 /*
161 ** Extract array from user's data applying all pixel store modes.
162 ** The internal packed array format used has LSB_FIRST = FALSE and
163 ** ALIGNMENT = 1.
164 */
165 void __glFillImage(__GLXcontext *gc, GLint dim, GLint width, GLint height,
166 GLint depth, GLenum format, GLenum type,
167 const GLvoid *userdata, GLubyte *newimage, GLubyte *modes)
168 {
169 const __GLXattribute * state = gc->client_state_private;
170 GLint rowLength = state->storeUnpack.rowLength;
171 GLint imageHeight = state->storeUnpack.imageHeight;
172 GLint alignment = state->storeUnpack.alignment;
173 GLint skipPixels = state->storeUnpack.skipPixels;
174 GLint skipRows = state->storeUnpack.skipRows;
175 GLint skipImages = state->storeUnpack.skipImages;
176 GLint swapBytes = state->storeUnpack.swapEndian;
177 GLint components, elementSize, rowSize, padding, groupsPerRow, groupSize;
178 GLint elementsPerRow, imageSize, rowsPerImage, h, i, j, k;
179 const GLubyte *start, *iter, *itera, *iterb, *iterc;
180 GLubyte *iter2;
181
182 if (type == GL_BITMAP) {
183 FillBitmap(gc, width, height, format, userdata, newimage);
184 } else {
185 components = __glElementsPerGroup(format,type);
186 if (rowLength > 0) {
187 groupsPerRow = rowLength;
188 } else {
189 groupsPerRow = width;
190 }
191 if (imageHeight > 0) {
192 rowsPerImage = imageHeight;
193 } else {
194 rowsPerImage = height;
195 }
196
197 elementSize = __glBytesPerElement(type);
198 groupSize = elementSize * components;
199 if (elementSize == 1) swapBytes = 0;
200
201 rowSize = groupsPerRow * groupSize;
202 padding = (rowSize % alignment);
203 if (padding) {
204 rowSize += alignment - padding;
205 }
206 imageSize = rowSize * rowsPerImage;
207 start = ((const GLubyte*) userdata) + skipImages * imageSize +
208 skipRows * rowSize + skipPixels * groupSize;
209 iter2 = newimage;
210 elementsPerRow = width * components;
211
212 if (swapBytes) {
213 itera = start;
214 for (h = 0; h < depth; h++) {
215 iterb = itera;
216 for (i = 0; i < height; i++) {
217 iterc = iterb;
218 for (j = 0; j < elementsPerRow; j++) {
219 for (k = 1; k <= elementSize; k++) {
220 iter2[k-1] = iterc[elementSize - k];
221 }
222 iter2 += elementSize;
223 iterc += elementSize;
224 }
225 iterb += rowSize;
226 }
227 itera += imageSize;
228 }
229 } else {
230 itera = start;
231 for (h = 0; h < depth; h++) {
232 if (rowSize == elementsPerRow * elementSize) {
233 /* Ha! This is mondo easy! */
234 __GLX_MEM_COPY(iter2, itera,
235 elementsPerRow * elementSize * height);
236 iter2 += elementsPerRow * elementSize * height;
237 } else {
238 iter = itera;
239 for (i = 0; i < height; i++) {
240 __GLX_MEM_COPY(iter2, iter, elementsPerRow*elementSize);
241 iter2 += elementsPerRow * elementSize;
242 iter += rowSize;
243 }
244 }
245 itera += imageSize;
246 }
247 }
248 }
249
250 /* Setup store modes that describe what we just did */
251 if (modes) {
252 if ( dim < 3 ) {
253 (void) memcpy( modes, __glXDefaultPixelStore + 4, 20 );
254 }
255 else {
256 (void) memcpy( modes, __glXDefaultPixelStore + 0, 36 );
257 }
258 }
259 }
260
261 /*
262 ** Empty a bitmap in LSB_FIRST=GL_FALSE and ALIGNMENT=4 format packing it
263 ** into the clients memory using the pixel store PACK modes.
264 */
265 static void EmptyBitmap(__GLXcontext *gc, GLint width, GLint height,
266 GLenum format, const GLubyte *sourceImage,
267 GLvoid *userdata)
268 {
269 const __GLXattribute * state = gc->client_state_private;
270 GLint rowLength = state->storePack.rowLength;
271 GLint alignment = state->storePack.alignment;
272 GLint skipPixels = state->storePack.skipPixels;
273 GLint skipRows = state->storePack.skipRows;
274 GLint lsbFirst = state->storePack.lsbFirst;
275 GLint components, groupsPerRow, rowSize, padding, elementsPerRow;
276 GLint sourceRowSize, sourcePadding, sourceSkip;
277 GLubyte *start, *iter;
278 GLint elementsLeft, bitOffset, currentByte, highBitMask, lowBitMask;
279 GLint writeMask, i;
280 GLubyte writeByte;
281
282 components = __glElementsPerGroup(format,GL_BITMAP);
283 if (rowLength > 0) {
284 groupsPerRow = rowLength;
285 } else {
286 groupsPerRow = width;
287 }
288
289 rowSize = (groupsPerRow * components + 7) >> 3;
290 padding = (rowSize % alignment);
291 if (padding) {
292 rowSize += alignment - padding;
293 }
294 sourceRowSize = (width * components + 7) >> 3;
295 sourcePadding = (sourceRowSize % 4);
296 if (sourcePadding) {
297 sourceSkip = 4 - sourcePadding;
298 } else {
299 sourceSkip = 0;
300 }
301 start = ((GLubyte*) userdata) + skipRows * rowSize +
302 ((skipPixels * components) >> 3);
303 bitOffset = (skipPixels * components) & 7;
304 highBitMask = LowBitsMask[8-bitOffset];
305 lowBitMask = HighBitsMask[bitOffset];
306 elementsPerRow = width * components;
307 for (i = 0; i < height; i++) {
308 elementsLeft = elementsPerRow;
309 iter = start;
310 writeMask = highBitMask;
311 writeByte = 0;
312 while (elementsLeft) {
313 /* Set up writeMask (to write to current byte) */
314 if (elementsLeft + bitOffset < 8) {
315 /* Need to trim writeMask */
316 writeMask &= HighBitsMask[bitOffset+elementsLeft];
317 }
318
319 if (lsbFirst) {
320 currentByte = MsbToLsbTable[iter[0]];
321 } else {
322 currentByte = iter[0];
323 }
324
325 if (bitOffset) {
326 writeByte |= (sourceImage[0] >> bitOffset);
327 currentByte = (currentByte & ~writeMask) |
328 (writeByte & writeMask);
329 writeByte = (sourceImage[0] << (8 - bitOffset));
330 } else {
331 currentByte = (currentByte & ~writeMask) |
332 (sourceImage[0] & writeMask);
333 }
334
335 if (lsbFirst) {
336 iter[0] = MsbToLsbTable[currentByte];
337 } else {
338 iter[0] = currentByte;
339 }
340
341 if (elementsLeft >= 8) {
342 elementsLeft -= 8;
343 } else {
344 elementsLeft = 0;
345 }
346 sourceImage++;
347 iter++;
348 writeMask = 0xff;
349 }
350 if (writeByte) {
351 /* Some data left over that still needs writing */
352 writeMask &= lowBitMask;
353 if (lsbFirst) {
354 currentByte = MsbToLsbTable[iter[0]];
355 } else {
356 currentByte = iter[0];
357 }
358 currentByte = (currentByte & ~writeMask) | (writeByte & writeMask);
359 if (lsbFirst) {
360 iter[0] = MsbToLsbTable[currentByte];
361 } else {
362 iter[0] = currentByte;
363 }
364 }
365 start += rowSize;
366 sourceImage += sourceSkip;
367 }
368 }
369
370 /*
371 ** Insert array into user's data applying all pixel store modes.
372 ** The packed array format from the server is LSB_FIRST = FALSE,
373 ** SWAP_BYTES = the current pixel storage pack mode, and ALIGNMENT = 4.
374 ** Named __glEmptyImage() because it is the opposite of __glFillImage().
375 */
376 /* ARGSUSED */
377 void __glEmptyImage(__GLXcontext *gc, GLint dim, GLint width, GLint height,
378 GLint depth, GLenum format, GLenum type,
379 const GLubyte *sourceImage, GLvoid *userdata)
380 {
381 const __GLXattribute * state = gc->client_state_private;
382 GLint rowLength = state->storePack.rowLength;
383 GLint imageHeight = state->storePack.imageHeight;
384 GLint alignment = state->storePack.alignment;
385 GLint skipPixels = state->storePack.skipPixels;
386 GLint skipRows = state->storePack.skipRows;
387 GLint skipImages = state->storePack.skipImages;
388 GLint components, elementSize, rowSize, padding, groupsPerRow, groupSize;
389 GLint elementsPerRow, sourceRowSize, sourcePadding, h, i;
390 GLint imageSize, rowsPerImage;
391 GLubyte *start, *iter, *itera;
392
393 if (type == GL_BITMAP) {
394 EmptyBitmap(gc, width, height, format, sourceImage, userdata);
395 } else {
396 components = __glElementsPerGroup(format,type);
397 if (rowLength > 0) {
398 groupsPerRow = rowLength;
399 } else {
400 groupsPerRow = width;
401 }
402 if (imageHeight > 0) {
403 rowsPerImage = imageHeight;
404 } else {
405 rowsPerImage = height;
406 }
407 elementSize = __glBytesPerElement(type);
408 groupSize = elementSize * components;
409 rowSize = groupsPerRow * groupSize;
410 padding = (rowSize % alignment);
411 if (padding) {
412 rowSize += alignment - padding;
413 }
414 sourceRowSize = width * groupSize;
415 sourcePadding = (sourceRowSize % 4);
416 if (sourcePadding) {
417 sourceRowSize += 4 - sourcePadding;
418 }
419 imageSize = sourceRowSize * rowsPerImage;
420 start = ((GLubyte*) userdata) + skipImages * imageSize +
421 skipRows * rowSize + skipPixels * groupSize;
422 elementsPerRow = width * components;
423
424 itera = start;
425 for (h = 0; h < depth; h++) {
426 if ((rowSize == sourceRowSize) && (sourcePadding == 0)) {
427 /* Ha! This is mondo easy! */
428 __GLX_MEM_COPY(itera, sourceImage,
429 elementsPerRow * elementSize * height);
430 sourceImage += elementsPerRow * elementSize * height;
431 } else {
432 iter = itera;
433 for (i = 0; i < height; i++) {
434 __GLX_MEM_COPY(iter, sourceImage,
435 elementsPerRow * elementSize);
436 sourceImage += sourceRowSize;
437 iter += rowSize;
438 }
439 }
440 itera += imageSize;
441 }
442 }
443 }