1 /* $XFree86: xc/lib/GL/glx/pixel.c,v 1.8 2003/09/28 20:15:04 alanh Exp $ */
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:
11 ** http://oss.sgi.com/projects/FreeB
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.
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.
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.
37 #include "packrender.h"
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,
74 static const GLubyte LowBitsMask
[9] = {
75 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff,
78 static const GLubyte HighBitsMask
[9] = {
79 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff,
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.
88 static void FillBitmap(__GLXcontext
*gc
, GLint width
, GLint height
,
89 GLenum format
, const GLvoid
*userdata
,
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
;
100 GLint components
, groupsPerRow
, rowSize
, padding
, elementsPerRow
;
101 const GLubyte
*start
, *iter
;
104 groupsPerRow
= rowLength
;
106 groupsPerRow
= width
;
108 components
= __glElementsPerGroup(format
,GL_BITMAP
);
109 rowSize
= (groupsPerRow
* components
+ 7) >> 3;
110 padding
= (rowSize
% alignment
);
112 rowSize
+= alignment
- padding
;
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
;
123 while (elementsLeft
) {
124 /* First retrieve low bits from current byte */
126 currentByte
= MsbToLsbTable
[iter
[0]];
128 currentByte
= iter
[0];
131 /* Need to read next byte to finish current byte */
132 if (elementsLeft
> (8 - bitOffset
)) {
134 nextByte
= MsbToLsbTable
[iter
[1]];
139 ((currentByte
& highBitMask
) << bitOffset
) |
140 ((nextByte
& lowBitMask
) >> (8 - bitOffset
));
143 ((currentByte
& highBitMask
) << bitOffset
);
146 if (elementsLeft
>= 8) {
147 *destImage
= currentByte
;
150 *destImage
= currentByte
& HighBitsMask
[elementsLeft
];
161 ** Extract array from user's data applying all pixel store modes.
162 ** The internal packed array format used has LSB_FIRST = FALSE and
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
)
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
;
182 if (type
== GL_BITMAP
) {
183 FillBitmap(gc
, width
, height
, format
, userdata
, newimage
);
185 components
= __glElementsPerGroup(format
,type
);
187 groupsPerRow
= rowLength
;
189 groupsPerRow
= width
;
191 if (imageHeight
> 0) {
192 rowsPerImage
= imageHeight
;
194 rowsPerImage
= height
;
197 elementSize
= __glBytesPerElement(type
);
198 groupSize
= elementSize
* components
;
199 if (elementSize
== 1) swapBytes
= 0;
201 rowSize
= groupsPerRow
* groupSize
;
202 padding
= (rowSize
% alignment
);
204 rowSize
+= alignment
- padding
;
206 imageSize
= rowSize
* rowsPerImage
;
207 start
= ((const GLubyte
*) userdata
) + skipImages
* imageSize
+
208 skipRows
* rowSize
+ skipPixels
* groupSize
;
210 elementsPerRow
= width
* components
;
214 for (h
= 0; h
< depth
; h
++) {
216 for (i
= 0; i
< height
; i
++) {
218 for (j
= 0; j
< elementsPerRow
; j
++) {
219 for (k
= 1; k
<= elementSize
; k
++) {
220 iter2
[k
-1] = iterc
[elementSize
- k
];
222 iter2
+= elementSize
;
223 iterc
+= elementSize
;
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
;
239 for (i
= 0; i
< height
; i
++) {
240 __GLX_MEM_COPY(iter2
, iter
, elementsPerRow
*elementSize
);
241 iter2
+= elementsPerRow
* elementSize
;
250 /* Setup store modes that describe what we just did */
253 (void) memcpy( modes
, __glXDefaultPixelStore
+ 4, 20 );
256 (void) memcpy( modes
, __glXDefaultPixelStore
+ 0, 36 );
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.
265 static void EmptyBitmap(__GLXcontext
*gc
, GLint width
, GLint height
,
266 GLenum format
, const GLubyte
*sourceImage
,
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
;
282 components
= __glElementsPerGroup(format
,GL_BITMAP
);
284 groupsPerRow
= rowLength
;
286 groupsPerRow
= width
;
289 rowSize
= (groupsPerRow
* components
+ 7) >> 3;
290 padding
= (rowSize
% alignment
);
292 rowSize
+= alignment
- padding
;
294 sourceRowSize
= (width
* components
+ 7) >> 3;
295 sourcePadding
= (sourceRowSize
% 4);
297 sourceSkip
= 4 - sourcePadding
;
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
;
310 writeMask
= highBitMask
;
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
];
320 currentByte
= MsbToLsbTable
[iter
[0]];
322 currentByte
= iter
[0];
326 writeByte
|= (sourceImage
[0] >> bitOffset
);
327 currentByte
= (currentByte
& ~writeMask
) |
328 (writeByte
& writeMask
);
329 writeByte
= (sourceImage
[0] << (8 - bitOffset
));
331 currentByte
= (currentByte
& ~writeMask
) |
332 (sourceImage
[0] & writeMask
);
336 iter
[0] = MsbToLsbTable
[currentByte
];
338 iter
[0] = currentByte
;
341 if (elementsLeft
>= 8) {
351 /* Some data left over that still needs writing */
352 writeMask
&= lowBitMask
;
354 currentByte
= MsbToLsbTable
[iter
[0]];
356 currentByte
= iter
[0];
358 currentByte
= (currentByte
& ~writeMask
) | (writeByte
& writeMask
);
360 iter
[0] = MsbToLsbTable
[currentByte
];
362 iter
[0] = currentByte
;
366 sourceImage
+= sourceSkip
;
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().
377 void __glEmptyImage(__GLXcontext
*gc
, GLint dim
, GLint width
, GLint height
,
378 GLint depth
, GLenum format
, GLenum type
,
379 const GLubyte
*sourceImage
, GLvoid
*userdata
)
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
;
393 if (type
== GL_BITMAP
) {
394 EmptyBitmap(gc
, width
, height
, format
, sourceImage
, userdata
);
396 components
= __glElementsPerGroup(format
,type
);
398 groupsPerRow
= rowLength
;
400 groupsPerRow
= width
;
402 if (imageHeight
> 0) {
403 rowsPerImage
= imageHeight
;
405 rowsPerImage
= height
;
407 elementSize
= __glBytesPerElement(type
);
408 groupSize
= elementSize
* components
;
409 rowSize
= groupsPerRow
* groupSize
;
410 padding
= (rowSize
% alignment
);
412 rowSize
+= alignment
- padding
;
414 sourceRowSize
= width
* groupSize
;
415 sourcePadding
= (sourceRowSize
% 4);
417 sourceRowSize
+= 4 - sourcePadding
;
419 imageSize
= sourceRowSize
* rowsPerImage
;
420 start
= ((GLubyte
*) userdata
) + skipImages
* imageSize
+
421 skipRows
* rowSize
+ skipPixels
* groupSize
;
422 elementsPerRow
= width
* components
;
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
;
433 for (i
= 0; i
< height
; i
++) {
434 __GLX_MEM_COPY(iter
, sourceImage
,
435 elementsPerRow
* elementSize
);
436 sourceImage
+= sourceRowSize
;