clean-up of header includes (Daryll)
[mesa.git] / src / mesa / main / image.c
1 /* $Id: image.c,v 1.13 1999/11/08 07:36:44 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.1
6 *
7 * Copyright (C) 1999 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28
29 #ifdef PC_HEADER
30 #include "all.h"
31 #else
32 #ifndef XFree86Server
33 #include <assert.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #else
37 #include "GL/xf86glx.h"
38 #endif
39 #include "context.h"
40 #include "image.h"
41 #include "macros.h"
42 #include "mmath.h"
43 #include "pixel.h"
44 #include "types.h"
45 #endif
46
47
48
49 /*
50 * Flip the 8 bits in each byte of the given array.
51 */
52 void gl_flip_bytes( GLubyte *p, GLuint n )
53 {
54 register GLuint i, a, b;
55
56 for (i=0;i<n;i++) {
57 b = (GLuint) p[i];
58 a = ((b & 0x01) << 7) |
59 ((b & 0x02) << 5) |
60 ((b & 0x04) << 3) |
61 ((b & 0x08) << 1) |
62 ((b & 0x10) >> 1) |
63 ((b & 0x20) >> 3) |
64 ((b & 0x40) >> 5) |
65 ((b & 0x80) >> 7);
66 p[i] = (GLubyte) a;
67 }
68 }
69
70
71 /*
72 * Flip the order of the 2 bytes in each word in the given array.
73 */
74 void gl_swap2( GLushort *p, GLuint n )
75 {
76 register GLuint i;
77
78 for (i=0;i<n;i++) {
79 p[i] = (p[i] >> 8) | ((p[i] << 8) & 0xff00);
80 }
81 }
82
83
84
85 /*
86 * Flip the order of the 4 bytes in each word in the given array.
87 */
88 void gl_swap4( GLuint *p, GLuint n )
89 {
90 register GLuint i, a, b;
91
92 for (i=0;i<n;i++) {
93 b = p[i];
94 a = (b >> 24)
95 | ((b >> 8) & 0xff00)
96 | ((b << 8) & 0xff0000)
97 | ((b << 24) & 0xff000000);
98 p[i] = a;
99 }
100 }
101
102
103
104
105 /*
106 * Return the size, in bytes, of the given GL datatype.
107 * Return 0 if GL_BITMAP.
108 * Return -1 if invalid type enum.
109 */
110 GLint gl_sizeof_type( GLenum type )
111 {
112 switch (type) {
113 case GL_BITMAP:
114 return 0;
115 case GL_UNSIGNED_BYTE:
116 return sizeof(GLubyte);
117 case GL_BYTE:
118 return sizeof(GLbyte);
119 case GL_UNSIGNED_SHORT:
120 return sizeof(GLushort);
121 case GL_SHORT:
122 return sizeof(GLshort);
123 case GL_UNSIGNED_INT:
124 return sizeof(GLuint);
125 case GL_INT:
126 return sizeof(GLint);
127 case GL_FLOAT:
128 return sizeof(GLfloat);
129 default:
130 return -1;
131 }
132 }
133
134
135 /*
136 * Same as gl_sizeof_packed_type() but we also accept the
137 * packed pixel format datatypes.
138 */
139 GLint gl_sizeof_packed_type( GLenum type )
140 {
141 switch (type) {
142 case GL_BITMAP:
143 return 0;
144 case GL_UNSIGNED_BYTE:
145 return sizeof(GLubyte);
146 case GL_BYTE:
147 return sizeof(GLbyte);
148 case GL_UNSIGNED_SHORT:
149 return sizeof(GLushort);
150 case GL_SHORT:
151 return sizeof(GLshort);
152 case GL_UNSIGNED_INT:
153 return sizeof(GLuint);
154 case GL_INT:
155 return sizeof(GLint);
156 case GL_FLOAT:
157 return sizeof(GLfloat);
158 case GL_UNSIGNED_BYTE_3_3_2:
159 return sizeof(GLubyte);
160 case GL_UNSIGNED_BYTE_2_3_3_REV:
161 return sizeof(GLubyte);
162 case GL_UNSIGNED_SHORT_5_6_5:
163 return sizeof(GLshort);
164 case GL_UNSIGNED_SHORT_5_6_5_REV:
165 return sizeof(GLshort);
166 case GL_UNSIGNED_SHORT_4_4_4_4:
167 return sizeof(GLshort);
168 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
169 return sizeof(GLshort);
170 case GL_UNSIGNED_SHORT_5_5_5_1:
171 return sizeof(GLshort);
172 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
173 return sizeof(GLshort);
174 case GL_UNSIGNED_INT_8_8_8_8:
175 return sizeof(GLuint);
176 case GL_UNSIGNED_INT_8_8_8_8_REV:
177 return sizeof(GLuint);
178 case GL_UNSIGNED_INT_10_10_10_2:
179 return sizeof(GLuint);
180 case GL_UNSIGNED_INT_2_10_10_10_REV:
181 return sizeof(GLuint);
182 default:
183 return -1;
184 }
185 }
186
187
188
189 /*
190 * Return the number of components in a GL enum pixel type.
191 * Return -1 if bad format.
192 */
193 GLint gl_components_in_format( GLenum format )
194 {
195 switch (format) {
196 case GL_COLOR_INDEX:
197 case GL_COLOR_INDEX1_EXT:
198 case GL_COLOR_INDEX2_EXT:
199 case GL_COLOR_INDEX4_EXT:
200 case GL_COLOR_INDEX8_EXT:
201 case GL_COLOR_INDEX12_EXT:
202 case GL_COLOR_INDEX16_EXT:
203 case GL_STENCIL_INDEX:
204 case GL_DEPTH_COMPONENT:
205 case GL_RED:
206 case GL_GREEN:
207 case GL_BLUE:
208 case GL_ALPHA:
209 case GL_LUMINANCE:
210 case GL_INTENSITY:
211 return 1;
212 case GL_LUMINANCE_ALPHA:
213 return 2;
214 case GL_RGB:
215 return 3;
216 case GL_RGBA:
217 return 4;
218 case GL_BGR:
219 return 3;
220 case GL_BGRA:
221 return 4;
222 case GL_ABGR_EXT:
223 return 4;
224 default:
225 return -1;
226 }
227 }
228
229
230 /*
231 * Return bytes per pixel for given format and type
232 * Return -1 if bad format or type.
233 */
234 GLint gl_bytes_per_pixel( GLenum format, GLenum type )
235 {
236 GLint comps = gl_components_in_format( format );
237 if (comps < 0)
238 return -1;
239
240 switch (type) {
241 case GL_BITMAP:
242 return 0; /* special case */
243 case GL_BYTE:
244 case GL_UNSIGNED_BYTE:
245 return comps * sizeof(GLubyte);
246 case GL_SHORT:
247 case GL_UNSIGNED_SHORT:
248 return comps * sizeof(GLshort);
249 case GL_INT:
250 case GL_UNSIGNED_INT:
251 return comps * sizeof(GLint);
252 case GL_FLOAT:
253 return comps * sizeof(GLfloat);
254 case GL_UNSIGNED_BYTE_3_3_2:
255 case GL_UNSIGNED_BYTE_2_3_3_REV:
256 if (format == GL_RGB || format == GL_BGR)
257 return sizeof(GLubyte);
258 else
259 return -1; /* error */
260 case GL_UNSIGNED_SHORT_5_6_5:
261 case GL_UNSIGNED_SHORT_5_6_5_REV:
262 if (format == GL_RGB || format == GL_BGR)
263 return sizeof(GLshort);
264 else
265 return -1; /* error */
266 case GL_UNSIGNED_SHORT_4_4_4_4:
267 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
268 case GL_UNSIGNED_SHORT_5_5_5_1:
269 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
270 if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT)
271 return sizeof(GLushort);
272 else
273 return -1;
274 case GL_UNSIGNED_INT_8_8_8_8:
275 case GL_UNSIGNED_INT_8_8_8_8_REV:
276 case GL_UNSIGNED_INT_10_10_10_2:
277 case GL_UNSIGNED_INT_2_10_10_10_REV:
278 if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT)
279 return sizeof(GLuint);
280 else
281 return -1;
282 default:
283 return -1;
284 }
285 }
286
287
288 /*
289 * Test if the given pixel format and type are legal.
290 * Return GL_TRUE for legal, GL_FALSE for illegal.
291 */
292 GLboolean gl_is_legal_format_and_type( GLenum format, GLenum type )
293 {
294 switch (format) {
295 case GL_COLOR_INDEX:
296 case GL_STENCIL_INDEX:
297 switch (type) {
298 case GL_BITMAP:
299 case GL_BYTE:
300 case GL_UNSIGNED_BYTE:
301 case GL_SHORT:
302 case GL_UNSIGNED_SHORT:
303 case GL_INT:
304 case GL_UNSIGNED_INT:
305 case GL_FLOAT:
306 return GL_TRUE;
307 default:
308 return GL_FALSE;
309 }
310 case GL_RED:
311 case GL_GREEN:
312 case GL_BLUE:
313 case GL_ALPHA:
314 case GL_LUMINANCE:
315 case GL_LUMINANCE_ALPHA:
316 case GL_DEPTH_COMPONENT:
317 case GL_BGR:
318 switch (type) {
319 case GL_BYTE:
320 case GL_UNSIGNED_BYTE:
321 case GL_SHORT:
322 case GL_UNSIGNED_SHORT:
323 case GL_INT:
324 case GL_UNSIGNED_INT:
325 case GL_FLOAT:
326 return GL_TRUE;
327 default:
328 return GL_FALSE;
329 }
330 case GL_RGB:
331 switch (type) {
332 case GL_BYTE:
333 case GL_UNSIGNED_BYTE:
334 case GL_SHORT:
335 case GL_UNSIGNED_SHORT:
336 case GL_INT:
337 case GL_UNSIGNED_INT:
338 case GL_FLOAT:
339 case GL_UNSIGNED_BYTE_3_3_2:
340 case GL_UNSIGNED_BYTE_2_3_3_REV:
341 case GL_UNSIGNED_SHORT_5_6_5:
342 case GL_UNSIGNED_SHORT_5_6_5_REV:
343 return GL_TRUE;
344 default:
345 return GL_FALSE;
346 }
347 case GL_RGBA:
348 case GL_BGRA:
349 case GL_ABGR_EXT:
350 switch (type) {
351 case GL_BYTE:
352 case GL_UNSIGNED_BYTE:
353 case GL_SHORT:
354 case GL_UNSIGNED_SHORT:
355 case GL_INT:
356 case GL_UNSIGNED_INT:
357 case GL_FLOAT:
358 case GL_UNSIGNED_SHORT_4_4_4_4:
359 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
360 case GL_UNSIGNED_SHORT_5_5_5_1:
361 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
362 case GL_UNSIGNED_INT_8_8_8_8:
363 case GL_UNSIGNED_INT_8_8_8_8_REV:
364 case GL_UNSIGNED_INT_10_10_10_2:
365 case GL_UNSIGNED_INT_2_10_10_10_REV:
366 return GL_TRUE;
367 default:
368 return GL_FALSE;
369 }
370 default:
371 ; /* fall-through */
372 }
373 return GL_FALSE;
374 }
375
376
377
378 /*
379 * Return the address of a pixel in an image (actually a volume).
380 * Pixel unpacking/packing parameters are observed according to 'packing'.
381 * Input: image - start of image data
382 * width, height - size of image
383 * format - image format
384 * type - pixel component type
385 * packing - the pixelstore attributes
386 * img - which image in the volume (0 for 1D or 2D images)
387 * row, column - location of pixel in the image
388 * Return: address of pixel at (image,row,column) in image or NULL if error.
389 */
390 GLvoid *gl_pixel_addr_in_image( const struct gl_pixelstore_attrib *packing,
391 const GLvoid *image, GLsizei width,
392 GLsizei height, GLenum format, GLenum type,
393 GLint img, GLint row, GLint column )
394 {
395 GLint alignment; /* 1, 2 or 4 */
396 GLint pixels_per_row;
397 GLint rows_per_image;
398 GLint skiprows;
399 GLint skippixels;
400 GLint skipimages; /* for 3-D volume images */
401 GLubyte *pixel_addr;
402
403 alignment = packing->Alignment;
404 if (packing->RowLength > 0) {
405 pixels_per_row = packing->RowLength;
406 }
407 else {
408 pixels_per_row = width;
409 }
410 if (packing->ImageHeight > 0) {
411 rows_per_image = packing->ImageHeight;
412 }
413 else {
414 rows_per_image = height;
415 }
416 skiprows = packing->SkipRows;
417 skippixels = packing->SkipPixels;
418 skipimages = packing->SkipImages;
419
420 if (type==GL_BITMAP) {
421 /* BITMAP data */
422 GLint comp_per_pixel; /* components per pixel */
423 GLint bytes_per_comp; /* bytes per component */
424 GLint bytes_per_row;
425 GLint bytes_per_image;
426
427 /* Compute bytes per component */
428 bytes_per_comp = gl_sizeof_packed_type( type );
429 if (bytes_per_comp<0) {
430 return NULL;
431 }
432
433 /* Compute number of components per pixel */
434 comp_per_pixel = gl_components_in_format( format );
435 if (comp_per_pixel<0 && type != GL_BITMAP) {
436 return NULL;
437 }
438
439 bytes_per_row = alignment
440 * CEILING( comp_per_pixel*pixels_per_row, 8*alignment );
441
442 bytes_per_image = bytes_per_row * rows_per_image;
443
444 pixel_addr = (GLubyte *) image
445 + (skipimages + img) * bytes_per_image
446 + (skiprows + row) * bytes_per_row
447 + (skippixels + column) / 8;
448 }
449 else {
450 /* Non-BITMAP data */
451 GLint bytes_per_pixel, bytes_per_row, remainder, bytes_per_image;
452
453 bytes_per_pixel = gl_bytes_per_pixel( format, type );
454
455 /* The pixel type and format should have been error checked earlier */
456 assert(bytes_per_pixel > 0);
457
458 bytes_per_row = pixels_per_row * bytes_per_pixel;
459 remainder = bytes_per_row % alignment;
460 if (remainder > 0)
461 bytes_per_row += (alignment - remainder);
462
463 ASSERT(bytes_per_row % alignment == 0);
464
465 bytes_per_image = bytes_per_row * rows_per_image;
466
467 /* compute final pixel address */
468 pixel_addr = (GLubyte *) image
469 + (skipimages + img) * bytes_per_image
470 + (skiprows + row) * bytes_per_row
471 + (skippixels + column) * bytes_per_pixel;
472 }
473
474 return (GLvoid *) pixel_addr;
475 }
476
477
478
479 /*
480 * Allocate a new gl_image. All fields are initialized to zero.
481 */
482 static struct gl_image *alloc_image( void )
483 {
484 return CALLOC_STRUCT(gl_image);
485 }
486
487
488
489 /*
490 * Allocate a new gl_image with the error flag set.
491 */
492 static struct gl_image *alloc_error_image( GLint width, GLint height,
493 GLint depth, GLenum format,
494 GLenum type )
495 {
496 struct gl_image *image = alloc_image();
497 if (image) {
498 image->Width = width;
499 image->Height = height;
500 image->Depth = depth;
501 image->Format = format;
502 image->Type = type;
503 image->ErrorFlag = GL_TRUE;
504 }
505 return image;
506 }
507
508
509
510 /*
511 * Free a gl_image.
512 */
513 void gl_free_image( struct gl_image *image )
514 {
515 if (image->Data) {
516 FREE(image->Data);
517 }
518 FREE(image);
519 }
520
521
522
523 /*
524 * Do error checking on an image. If there's an error, register it and
525 * return GL_TRUE, else return GL_FALSE.
526 */
527 GLboolean gl_image_error_test( GLcontext *ctx, const struct gl_image *image,
528 const char *msg )
529 {
530 if (!image) {
531 gl_error( ctx, GL_OUT_OF_MEMORY, msg );
532 return GL_TRUE;
533 }
534 if (image->Width <= 0 || image->Height <= 0 || image->Depth <= 0) {
535 gl_error( ctx, GL_INVALID_VALUE, msg );
536 return GL_TRUE;
537 }
538 else if (!gl_is_legal_format_and_type(image->Format, image->Type)) {
539 return GL_TRUE;
540 }
541 else {
542 return GL_FALSE;
543 }
544 }
545
546
547
548 /*
549 * Unpack a depth-buffer image storing values as GLshort, GLuint, or GLfloats.
550 * Input: type - datatype of src depth image
551 * Return pointer to a new gl_image structure.
552 *
553 * Notes: if the source image type is GLushort then the gl_image will
554 * also store GLushorts. If the src image type is GLuint then the gl_image
555 * will also store GLuints. For all other src image types the gl_image
556 * will store GLfloats. The integer cases can later be optimized.
557 */
558 static struct gl_image *
559 unpack_depth_image( GLcontext *ctx, GLenum type, GLint width, GLint height,
560 const GLvoid *pixels,
561 const struct gl_pixelstore_attrib *packing)
562
563 {
564 struct gl_image *image;
565 GLfloat *fDst;
566 GLushort *sDst;
567 GLuint *iDst;
568 GLint i, j;
569 GLboolean errorType;
570
571 errorType = type != GL_BYTE &&
572 type != GL_UNSIGNED_BYTE &&
573 type != GL_SHORT &&
574 type != GL_UNSIGNED_SHORT &&
575 type != GL_INT &&
576 type != GL_UNSIGNED_INT &&
577 type != GL_FLOAT;
578
579 image = alloc_image();
580 if (image) {
581 image->Width = width;
582 image->Height = height;
583 image->Depth = 1;
584 image->Components = 1;
585 image->Format = GL_DEPTH_COMPONENT;
586 if (errorType) {
587 image->Type = type;
588 image->Data = NULL;
589 }
590 if (type==GL_UNSIGNED_SHORT) {
591 image->Type = GL_UNSIGNED_SHORT;
592 image->Data = MALLOC( width * height * sizeof(GLushort));
593 }
594 else if (type==GL_UNSIGNED_INT) {
595 image->Type = GL_UNSIGNED_INT;
596 image->Data = MALLOC( width * height * sizeof(GLuint));
597 }
598 else {
599 image->Type = GL_FLOAT;
600 image->Data = MALLOC( width * height * sizeof(GLfloat));
601 }
602 image->RefCount = 0;
603 if (!image->Data)
604 return image;
605 }
606 else {
607 return NULL;
608 }
609
610 if (errorType)
611 return image;
612
613 fDst = (GLfloat *) image->Data;
614 sDst = (GLushort *) image->Data;
615 iDst = (GLuint *) image->Data;
616
617 for (i=0;i<height;i++) {
618 GLvoid *src = gl_pixel_addr_in_image( packing, pixels,
619 width, height,
620 GL_DEPTH_COMPONENT, type,
621 0, i, 0 );
622 if (!src) {
623 return image;
624 }
625
626 switch (type) {
627 case GL_BYTE:
628 assert(image->Type == GL_FLOAT);
629 for (j=0; j<width; j++) {
630 *fDst++ = BYTE_TO_FLOAT(((GLbyte*)src)[j]);
631 }
632 break;
633 case GL_UNSIGNED_BYTE:
634 assert(image->Type == GL_FLOAT);
635 for (j=0; j<width; j++) {
636 *fDst++ = UBYTE_TO_FLOAT(((GLubyte*)src)[j]);
637 }
638 break;
639 case GL_UNSIGNED_SHORT:
640 assert(image->Type == GL_UNSIGNED_SHORT);
641 MEMCPY( sDst, src, width * sizeof(GLushort) );
642 if (packing->SwapBytes) {
643 gl_swap2( sDst, width );
644 }
645 sDst += width;
646 break;
647 case GL_SHORT:
648 assert(image->Type == GL_FLOAT);
649 if (packing->SwapBytes) {
650 for (j=0;j<width;j++) {
651 GLshort value = ((GLshort*)src)[j];
652 value = ((value >> 8) & 0xff) | ((value&0xff) << 8);
653 *fDst++ = SHORT_TO_FLOAT(value);
654 }
655 }
656 else {
657 for (j=0;j<width;j++) {
658 *fDst++ = SHORT_TO_FLOAT(((GLshort*)src)[j]);
659 }
660 }
661 break;
662 case GL_INT:
663 assert(image->Type == GL_FLOAT);
664 if (packing->SwapBytes) {
665 for (j=0;j<width;j++) {
666 GLint value = ((GLint*)src)[j];
667 value = ((value >> 24) & 0x000000ff) |
668 ((value >> 8) & 0x0000ff00) |
669 ((value << 8) & 0x00ff0000) |
670 ((value << 24) & 0xff000000);
671 *fDst++ = INT_TO_FLOAT(value);
672 }
673 }
674 else {
675 for (j=0;j<width;j++) {
676 *fDst++ = INT_TO_FLOAT(((GLint*)src)[j]);
677 }
678 }
679 iDst += width;
680 break;
681 case GL_UNSIGNED_INT:
682 assert(image->Type == GL_UNSIGNED_INT);
683 MEMCPY( iDst, src, width * sizeof(GLuint) );
684 if (packing->SwapBytes) {
685 gl_swap4( iDst, width );
686 }
687 iDst += width;
688 break;
689 case GL_FLOAT:
690 assert(image->Type == GL_FLOAT);
691 MEMCPY( fDst, src, width * sizeof(GLfloat) );
692 if (packing->SwapBytes) {
693 gl_swap4( (GLuint*) fDst, width );
694 }
695 fDst += width;
696 break;
697 default:
698 gl_problem(ctx, "unpack_depth_image type" );
699 return image;
700 }
701 }
702
703 return image;
704 }
705
706
707
708 /*
709 * Unpack a stencil image. Store as GLubytes in a gl_image structure.
710 * Return: pointer to new gl_image structure.
711 */
712 static struct gl_image *
713 unpack_stencil_image( GLcontext *ctx, GLenum type, GLint width, GLint height,
714 const GLvoid *pixels,
715 const struct gl_pixelstore_attrib *packing )
716 {
717 struct gl_image *image;
718 GLubyte *dst;
719 GLint i, j;
720 GLboolean errorType;
721
722 assert(sizeof(GLstencil) == sizeof(GLubyte));
723
724 errorType = type != GL_BYTE &&
725 type != GL_UNSIGNED_BYTE &&
726 type != GL_SHORT &&
727 type != GL_UNSIGNED_SHORT &&
728 type != GL_INT &&
729 type != GL_UNSIGNED_INT &&
730 type != GL_FLOAT &&
731 type != GL_BITMAP;
732
733 image = alloc_image();
734 if (image) {
735 image->Width = width;
736 image->Height = height;
737 image->Depth = 1;
738 image->Components = 1;
739 image->Format = GL_STENCIL_INDEX;
740 if (errorType) {
741 image->Type = type;
742 image->Data = NULL;
743 }
744 else {
745 image->Type = GL_UNSIGNED_BYTE;
746 image->Data = MALLOC( width * height * sizeof(GLubyte));
747 }
748 image->RefCount = 0;
749 if (!image->Data)
750 return image;
751 }
752 else {
753 return NULL;
754 }
755
756 if (errorType)
757 return image; /* error will be generated later */
758
759 dst = (GLubyte *) image->Data;
760
761 for (i=0;i<height;i++) {
762 GLvoid *src = gl_pixel_addr_in_image( packing, pixels,
763 width, height,
764 GL_STENCIL_INDEX, type,
765 0, i, 0 );
766 if (!src) {
767 return image;
768 }
769
770 switch (type) {
771 case GL_UNSIGNED_BYTE:
772 case GL_BYTE:
773 MEMCPY( dst, src, width * sizeof(GLubyte) );
774 dst += width * sizeof(GLubyte);
775 break;
776 case GL_UNSIGNED_SHORT:
777 case GL_SHORT:
778 if (packing->SwapBytes) {
779 /* grab upper byte */
780 for (j=0; j < width; j++) {
781 *dst++ = (((GLushort*)src)[j] & 0xff00) >> 8;
782 }
783 }
784 else {
785 for (j=0; j < width; j++) {
786 *dst++ = (((GLushort*)src)[j]) & 0xff;
787 }
788 }
789 break;
790 case GL_INT:
791 if (packing->SwapBytes) {
792 /* grab upper byte */
793 for (j=0; j < width; j++) {
794 *dst++ = (((GLuint*)src)[j] & 0xff000000) >> 8;
795 }
796 }
797 else {
798 for (j=0; j < width; j++) {
799 *dst++ = (((GLuint*)src)[j]) & 0xff;
800 }
801 }
802 break;
803 case GL_UNSIGNED_INT:
804 if (packing->SwapBytes) {
805 /* grab upper byte */
806 for (j=0; j < width; j++) {
807 *dst++ = (((GLuint*)src)[j] & 0xff000000) >> 8;
808 }
809 }
810 else {
811 for (j=0; j < width; j++) {
812 *dst++ = (((GLuint*)src)[j]) & 0xff;
813 }
814 }
815 break;
816 case GL_FLOAT:
817 if (packing->SwapBytes) {
818 for (j=0; j < width; j++) {
819 GLfloat fvalue;
820 GLint value = ((GLuint*)src)[j];
821 value = ((value & 0xff000000) >> 24)
822 | ((value & 0x00ff0000) >> 8)
823 | ((value & 0x0000ff00) << 8)
824 | ((value & 0x000000ff) << 24);
825 fvalue = *((GLfloat*) &value);
826 *dst++ = ((GLint) fvalue) & 0xff;
827 }
828 }
829 else {
830 for (j=0; j < width; j++) {
831 GLfloat fvalue = ((GLfloat *)src)[j];
832 *dst++ = ((GLint) fvalue) & 0xff;
833 }
834 }
835 break;
836 default:
837 gl_problem(ctx, "unpack_stencil_image type" );
838 return image;
839 }
840 }
841
842 return image;
843 }
844
845
846
847 /*
848 * Unpack a bitmap, return a new gl_image struct.
849 */
850 static struct gl_image *
851 unpack_bitmap( GLenum format, GLint width, GLint height,
852 const GLvoid *pixels,
853 const struct gl_pixelstore_attrib *packing )
854 {
855 struct gl_image *image;
856 GLint bytes, i, width_in_bytes;
857 GLubyte *buffer, *dst;
858
859 assert(format == GL_COLOR_INDEX || format == GL_STENCIL_INDEX);
860
861 /* Alloc dest storage */
862 bytes = ((width+7)/8 * height);
863 if (bytes>0 && pixels!=NULL) {
864 buffer = (GLubyte *) MALLOC( bytes );
865 if (!buffer) {
866 return NULL;
867 }
868 /* Copy/unpack pixel data to buffer */
869 width_in_bytes = CEILING( width, 8 );
870 dst = buffer;
871 for (i=0; i<height; i++) {
872 GLvoid *src = gl_pixel_addr_in_image( packing, pixels,
873 width, height,
874 GL_COLOR_INDEX, GL_BITMAP,
875 0, i, 0 );
876 if (!src) {
877 FREE(buffer);
878 return NULL;
879 }
880 MEMCPY( dst, src, width_in_bytes );
881 dst += width_in_bytes;
882 }
883 /* Bit flipping */
884 if (packing->LsbFirst) {
885 gl_flip_bytes( buffer, bytes );
886 }
887 }
888 else {
889 /* a 'null' bitmap */
890 buffer = NULL;
891 }
892
893 image = alloc_image();
894 if (image) {
895 image->Width = width;
896 image->Height = height;
897 image->Depth = 1;
898 image->Components = 0;
899 image->Format = format;
900 image->Type = GL_BITMAP;
901 image->Data = buffer;
902 image->RefCount = 0;
903 }
904 else {
905 FREE( buffer );
906 return NULL;
907 }
908
909 return image;
910 }
911
912
913
914 /*
915 * Unpack a 32x32 pixel polygon stipple from user memory using the
916 * current pixel unpack settings.
917 */
918 void gl_unpack_polygon_stipple( const GLcontext *ctx,
919 const GLubyte *pattern, GLuint dest[32] )
920 {
921 GLint i;
922 for (i = 0; i < 32; i++) {
923 GLubyte *src = (GLubyte *) gl_pixel_addr_in_image( &ctx->Unpack, pattern,
924 32, 32, GL_COLOR_INDEX, GL_BITMAP, 0, i, 0 );
925 dest[i] = (src[0] << 24)
926 | (src[1] << 16)
927 | (src[2] << 8)
928 | (src[3] );
929 }
930
931 /* Bit flipping within each byte */
932 if (ctx->Unpack.LsbFirst) {
933 gl_flip_bytes( (GLubyte *) dest, 32 * 4 );
934 }
935 }
936
937
938
939 /*
940 * Pack polygon stipple into user memory given current pixel packing
941 * settings.
942 */
943 void gl_pack_polygon_stipple( const GLcontext *ctx,
944 const GLuint pattern[32],
945 GLubyte *dest )
946 {
947 GLint i;
948 for (i = 0; i < 32; i++) {
949 GLubyte *dst = (GLubyte *) gl_pixel_addr_in_image( &ctx->Pack, dest,
950 32, 32, GL_COLOR_INDEX, GL_BITMAP, 0, i, 0 );
951 dst[0] = (pattern[i] >> 24) & 0xff;
952 dst[1] = (pattern[i] >> 16) & 0xff;
953 dst[2] = (pattern[i] >> 8) & 0xff;
954 dst[3] = (pattern[i] ) & 0xff;
955
956 /* Bit flipping within each byte */
957 if (ctx->Pack.LsbFirst) {
958 gl_flip_bytes( (GLubyte *) dst, 4 );
959 }
960 }
961 }
962
963
964
965 /*
966 * Unpack an RGBA or CI image and store it as unsigned bytes
967 */
968 static struct gl_image *
969 unpack_ubyte_image( GLint width, GLint height,
970 GLint depth, GLenum format, const GLvoid *pixels,
971 const struct gl_pixelstore_attrib *packing )
972 {
973 struct gl_image *image;
974 GLint width_in_bytes;
975 GLint components;
976 GLubyte *buffer, *dst;
977 GLint i, d;
978
979 components = gl_components_in_format( format );
980
981 width_in_bytes = width * components * sizeof(GLubyte);
982 buffer = (GLubyte *) MALLOC( height * width_in_bytes * depth );
983 if (!buffer) {
984 return NULL;
985 }
986
987 /* Copy/unpack pixel data to buffer */
988 dst = buffer;
989 for (d=0; d<depth; d++ ) {
990 for (i=0;i<height;i++) {
991 GLubyte *src = (GLubyte *) gl_pixel_addr_in_image( packing,
992 pixels, width, height, format, GL_UNSIGNED_BYTE,
993 d, i, 0 );
994 if (!src) {
995 FREE(buffer);
996 return NULL;
997 }
998 MEMCPY( dst, src, width_in_bytes );
999 dst += width_in_bytes;
1000 }
1001 }
1002
1003 if (format == GL_BGR) {
1004 /* swap order of every ubyte triplet from BGR to RGB */
1005 for (i=0; i<width*height; i++) {
1006 GLubyte b = buffer[i*3+0];
1007 GLubyte r = buffer[i*3+2];
1008 buffer[i*3+0] = r;
1009 buffer[i*3+2] = b;
1010 }
1011 }
1012 else if (format == GL_BGRA) {
1013 /* swap order of every ubyte quadruplet from BGRA to RGBA */
1014 for (i=0; i<width*height; i++) {
1015 GLubyte b = buffer[i*4+0];
1016 GLubyte r = buffer[i*4+2];
1017 buffer[i*4+0] = r;
1018 buffer[i*4+2] = b;
1019 }
1020 }
1021 else if (format == GL_ABGR_EXT) {
1022 /* swap order of every ubyte quadruplet from ABGR to RGBA */
1023 for (i=0; i<width*height; i++) {
1024 GLubyte a = buffer[i*4+0];
1025 GLubyte b = buffer[i*4+1];
1026 GLubyte g = buffer[i*4+2];
1027 GLubyte r = buffer[i*4+3];
1028 buffer[i*4+0] = r;
1029 buffer[i*4+1] = g;
1030 buffer[i*4+2] = b;
1031 buffer[i*4+3] = a;
1032 }
1033 }
1034
1035
1036 image = alloc_image();
1037 if (image) {
1038 image->Width = width;
1039 image->Height = height;
1040 image->Depth = depth;
1041 image->Components = components;
1042 if (format == GL_BGR)
1043 image->Format = GL_RGB;
1044 else if (format == GL_BGRA)
1045 image->Format = GL_RGBA;
1046 else if (format == GL_ABGR_EXT)
1047 image->Format = GL_RGBA;
1048 else
1049 image->Format = format;
1050 image->Type = GL_UNSIGNED_BYTE;
1051 image->Data = buffer;
1052 image->RefCount = 0;
1053 }
1054 else {
1055 FREE( buffer );
1056 }
1057
1058 return image;
1059 }
1060
1061
1062
1063 /*
1064 * Unpack a color image storing image as GLfloats
1065 */
1066 static struct gl_image *
1067 unpack_float_image( GLcontext *ctx, GLint width, GLint height, GLint depth,
1068 GLenum format, GLenum type, const GLvoid *pixels,
1069 const struct gl_pixelstore_attrib *packing )
1070 {
1071 struct gl_image *image;
1072 GLfloat *dst;
1073 GLint elems_per_row;
1074 GLint components;
1075 GLint i, j, d;
1076 GLboolean normalize;
1077
1078 assert(type != GL_BITMAP);
1079
1080 components = gl_components_in_format( format );
1081 assert(components > 0); /* should have been caught earlier */
1082
1083 if (!gl_is_legal_format_and_type( format, type )) {
1084 /* bad pixel type for format, make dummy image */
1085 image = alloc_image();
1086 if (image) {
1087 image->Width = width;
1088 image->Height = height;
1089 image->Depth = depth;
1090 image->Components = components;
1091 image->Format = format;
1092 image->Type = type;
1093 image->Data = NULL;
1094 image->RefCount = 0;
1095 }
1096 return image;
1097 }
1098
1099 elems_per_row = width * components;
1100
1101 image = alloc_image();
1102 if (image) {
1103 image->Width = width;
1104 image->Height = height;
1105 image->Depth = depth;
1106 image->Components = components;
1107 if (format == GL_BGR)
1108 image->Format = GL_RGB;
1109 else if (format == GL_BGRA)
1110 image->Format = GL_RGBA;
1111 else if (format == GL_ABGR_EXT)
1112 image->Format = GL_RGBA;
1113 else
1114 image->Format = format;
1115 image->Type = GL_FLOAT;
1116 image->Data = MALLOC( elems_per_row * height * depth * sizeof(GLfloat));
1117 image->RefCount = 0;
1118 if (!image->Data)
1119 return image;
1120 }
1121 else {
1122 return NULL;
1123 }
1124
1125 normalize = (format != GL_COLOR_INDEX) && (format != GL_STENCIL_INDEX);
1126
1127 dst = (GLfloat *) image->Data;
1128
1129 for (d=0; d<depth; d++) {
1130 for (i=0;i<height;i++) {
1131 GLvoid *src = gl_pixel_addr_in_image( packing, pixels,
1132 width, height,
1133 format, type,
1134 d, i, 0 );
1135 if (!src) {
1136 return image;
1137 }
1138
1139 switch (type) {
1140 case GL_UNSIGNED_BYTE:
1141 {
1142 GLubyte *ubsrc = (GLubyte *) src;
1143 if (normalize) {
1144 for (j=0;j<elems_per_row;j++) {
1145 *dst++ = UBYTE_TO_FLOAT(ubsrc[j]);
1146 }
1147 }
1148 else {
1149 for (j=0;j<elems_per_row;j++) {
1150 *dst++ = (GLfloat) ubsrc[j];
1151 }
1152 }
1153 }
1154 break;
1155 case GL_BYTE:
1156 if (normalize) {
1157 for (j=0;j<elems_per_row;j++) {
1158 *dst++ = BYTE_TO_FLOAT(((GLbyte*)src)[j]);
1159 }
1160 }
1161 else {
1162 for (j=0;j<elems_per_row;j++) {
1163 *dst++ = (GLfloat) ((GLbyte*)src)[j];
1164 }
1165 }
1166 break;
1167 case GL_UNSIGNED_SHORT:
1168 if (packing->SwapBytes) {
1169 for (j=0;j<elems_per_row;j++) {
1170 GLushort value = ((GLushort*)src)[j];
1171 value = ((value >> 8) & 0xff) | ((value&0xff) << 8);
1172 if (normalize) {
1173 *dst++ = USHORT_TO_FLOAT(value);
1174 }
1175 else {
1176 *dst++ = (GLfloat) value;
1177 }
1178 }
1179 }
1180 else {
1181 if (normalize) {
1182 for (j=0;j<elems_per_row;j++) {
1183 *dst++ = USHORT_TO_FLOAT(((GLushort*)src)[j]);
1184 }
1185 }
1186 else {
1187 for (j=0;j<elems_per_row;j++) {
1188 *dst++ = (GLfloat) ((GLushort*)src)[j];
1189 }
1190 }
1191 }
1192 break;
1193 case GL_SHORT:
1194 if (packing->SwapBytes) {
1195 for (j=0;j<elems_per_row;j++) {
1196 GLshort value = ((GLshort*)src)[j];
1197 value = ((value >> 8) & 0xff) | ((value&0xff) << 8);
1198 if (normalize) {
1199 *dst++ = SHORT_TO_FLOAT(value);
1200 }
1201 else {
1202 *dst++ = (GLfloat) value;
1203 }
1204 }
1205 }
1206 else {
1207 if (normalize) {
1208 for (j=0;j<elems_per_row;j++) {
1209 *dst++ = SHORT_TO_FLOAT(((GLshort*)src)[j]);
1210 }
1211 }
1212 else {
1213 for (j=0;j<elems_per_row;j++) {
1214 *dst++ = (GLfloat) ((GLshort*)src)[j];
1215 }
1216 }
1217 }
1218 break;
1219 case GL_UNSIGNED_INT:
1220 if (packing->SwapBytes) {
1221 GLuint value;
1222 for (j=0;j<elems_per_row;j++) {
1223 value = ((GLuint*)src)[j];
1224 value = ((value & 0xff000000) >> 24)
1225 | ((value & 0x00ff0000) >> 8)
1226 | ((value & 0x0000ff00) << 8)
1227 | ((value & 0x000000ff) << 24);
1228 if (normalize) {
1229 *dst++ = UINT_TO_FLOAT(value);
1230 }
1231 else {
1232 *dst++ = (GLfloat) value;
1233 }
1234 }
1235 }
1236 else {
1237 if (normalize) {
1238 for (j=0;j<elems_per_row;j++) {
1239 *dst++ = UINT_TO_FLOAT(((GLuint*)src)[j]);
1240 }
1241 }
1242 else {
1243 for (j=0;j<elems_per_row;j++) {
1244 *dst++ = (GLfloat) ((GLuint*)src)[j];
1245 }
1246 }
1247 }
1248 break;
1249 case GL_INT:
1250 if (packing->SwapBytes) {
1251 GLint value;
1252 for (j=0;j<elems_per_row;j++) {
1253 value = ((GLint*)src)[j];
1254 value = ((value & 0xff000000) >> 24)
1255 | ((value & 0x00ff0000) >> 8)
1256 | ((value & 0x0000ff00) << 8)
1257 | ((value & 0x000000ff) << 24);
1258 if (normalize) {
1259 *dst++ = INT_TO_FLOAT(value);
1260 }
1261 else {
1262 *dst++ = (GLfloat) value;
1263 }
1264 }
1265 }
1266 else {
1267 if (normalize) {
1268 for (j=0;j<elems_per_row;j++) {
1269 *dst++ = INT_TO_FLOAT(((GLint*)src)[j]);
1270 }
1271 }
1272 else {
1273 for (j=0;j<elems_per_row;j++) {
1274 *dst++ = (GLfloat) ((GLint*)src)[j];
1275 }
1276 }
1277 }
1278 break;
1279 case GL_FLOAT:
1280 if (packing->SwapBytes) {
1281 GLint value;
1282 for (j=0;j<elems_per_row;j++) {
1283 value = ((GLuint*)src)[j];
1284 value = ((value & 0xff000000) >> 24)
1285 | ((value & 0x00ff0000) >> 8)
1286 | ((value & 0x0000ff00) << 8)
1287 | ((value & 0x000000ff) << 24);
1288 *dst++ = *((GLfloat*) &value);
1289 }
1290 }
1291 else {
1292 MEMCPY( dst, src, elems_per_row*sizeof(GLfloat) );
1293 dst += elems_per_row;
1294 }
1295 break;
1296 case GL_UNSIGNED_BYTE_3_3_2:
1297 {
1298 GLubyte *ubsrc = (GLubyte *) src;
1299 for (j=0;j<width;j++) {
1300 GLubyte p = ubsrc[j];
1301 *dst++ = ((p >> 5) ) * (1.0F / 7.0F); /* red */
1302 *dst++ = ((p >> 2) & 0x7) * (1.0F / 7.0F); /* green */
1303 *dst++ = ((p ) & 0x3) * (1.0F / 3.0F); /* blue */
1304 }
1305 }
1306 break;
1307 case GL_UNSIGNED_BYTE_2_3_3_REV:
1308 {
1309 GLubyte *ubsrc = (GLubyte *) src;
1310 for (j=0;j<width;j++) {
1311 GLubyte p = ubsrc[j];
1312 *dst++ = ((p ) & 0x7) * (1.0F / 7.0F); /* red */
1313 *dst++ = ((p >> 3) & 0x7) * (1.0F / 7.0F); /* green */
1314 *dst++ = ((p >> 6) ) * (1.0F / 3.0F); /* blue */
1315 }
1316 }
1317 break;
1318 case GL_UNSIGNED_SHORT_5_6_5:
1319 {
1320 GLushort *ussrc = (GLushort *) src;
1321 for (j=0;j<width;j++) {
1322 GLushort p = ussrc[j];
1323 *dst++ = ((p >> 11) ) * (1.0F / 31.0F); /* red */
1324 *dst++ = ((p >> 5) & 0x3f) * (1.0F / 63.0F); /* green */
1325 *dst++ = ((p ) & 0x1f) * (1.0F / 31.0F); /* blue */
1326 }
1327 }
1328 break;
1329 case GL_UNSIGNED_SHORT_5_6_5_REV:
1330 {
1331 GLushort *ussrc = (GLushort *) src;
1332 for (j=0;j<width;j++) {
1333 GLushort p = ussrc[j];
1334 *dst++ = ((p ) & 0x1f) * (1.0F / 31.0F); /* red */
1335 *dst++ = ((p >> 5) & 0x3f) * (1.0F / 63.0F); /* green */
1336 *dst++ = ((p >> 11) ) * (1.0F / 31.0F); /* blue */
1337 }
1338 }
1339 break;
1340 case GL_UNSIGNED_SHORT_4_4_4_4:
1341 {
1342 GLushort *ussrc = (GLushort *) src;
1343 for (j=0;j<width;j++) {
1344 GLushort p = ussrc[j];
1345 *dst++ = ((p >> 12) ) * (1.0F / 15.0F); /* red */
1346 *dst++ = ((p >> 8) & 0xf) * (1.0F / 15.0F); /* green */
1347 *dst++ = ((p >> 4) & 0xf) * (1.0F / 15.0F); /* blue */
1348 *dst++ = ((p ) & 0xf) * (1.0F / 15.0F); /* alpha */
1349 }
1350 }
1351 break;
1352 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1353 {
1354 GLushort *ussrc = (GLushort *) src;
1355 for (j=0;j<width;j++) {
1356 GLushort p = ussrc[j];
1357 *dst++ = ((p ) & 0xf) * (1.0F / 15.0F); /* red */
1358 *dst++ = ((p >> 4) & 0xf) * (1.0F / 15.0F); /* green */
1359 *dst++ = ((p >> 8) & 0xf) * (1.0F / 15.0F); /* blue */
1360 *dst++ = ((p >> 12) ) * (1.0F / 15.0F); /* alpha */
1361 }
1362 }
1363 break;
1364 case GL_UNSIGNED_SHORT_5_5_5_1:
1365 {
1366 GLushort *ussrc = (GLushort *) src;
1367 for (j=0;j<width;j++) {
1368 GLushort p = ussrc[j];
1369 *dst++ = ((p >> 11) ) * (1.0F / 31.0F); /* red */
1370 *dst++ = ((p >> 6) & 0x1f) * (1.0F / 31.0F); /* green */
1371 *dst++ = ((p >> 1) & 0x1f) * (1.0F / 31.0F); /* blue */
1372 *dst++ = ((p ) & 0x1) * (1.0F / 1.0F); /* alpha */
1373 }
1374 }
1375 break;
1376 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
1377 {
1378 GLushort *ussrc = (GLushort *) src;
1379 for (j=0;j<width;j++) {
1380 GLushort p = ussrc[j];
1381 *dst++ = ((p ) & 0x1f) * (1.0F / 31.0F); /* red */
1382 *dst++ = ((p >> 5) & 0x1f) * (1.0F / 31.0F); /* green */
1383 *dst++ = ((p >> 10) & 0x1f) * (1.0F / 31.0F); /* blue */
1384 *dst++ = ((p >> 15) ) * (1.0F / 1.0F); /* alpha */
1385 }
1386 }
1387 break;
1388 case GL_UNSIGNED_INT_8_8_8_8:
1389 {
1390 GLuint *uisrc = (GLuint *) src;
1391 for (j=0;j<width;j++) {
1392 GLuint p = uisrc[j];
1393 *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24) );
1394 *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
1395 *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 8) & 0xff);
1396 *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p ) & 0xff);
1397 }
1398 }
1399 break;
1400 case GL_UNSIGNED_INT_8_8_8_8_REV:
1401 {
1402 GLuint *uisrc = (GLuint *) src;
1403 for (j=0;j<width;j++) {
1404 GLuint p = uisrc[j];
1405 *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p ) & 0xff);
1406 *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 8) & 0xff);
1407 *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
1408 *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24) );
1409 }
1410 }
1411 break;
1412 case GL_UNSIGNED_INT_10_10_10_2:
1413 {
1414 GLuint *uisrc = (GLuint *) src;
1415 for (j=0;j<width;j++) {
1416 GLuint p = uisrc[j];
1417 *dst++ = ((p >> 22) ) * (1.0F / 1023.0F); /* r */
1418 *dst++ = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F); /* g */
1419 *dst++ = ((p >> 2) & 0x3ff) * (1.0F / 1023.0F); /* b */
1420 *dst++ = ((p ) & 0x3 ) * (1.0F / 3.0F); /* a */
1421 }
1422 }
1423 break;
1424 case GL_UNSIGNED_INT_2_10_10_10_REV:
1425 {
1426 GLuint *uisrc = (GLuint *) src;
1427 for (j=0;j<width;j++) {
1428 GLuint p = uisrc[j];
1429 *dst++ = ((p ) & 0x3ff) * (1.0F / 1023.0F); /* r*/
1430 *dst++ = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F); /* g */
1431 *dst++ = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F); /* b */
1432 *dst++ = ((p >> 30) ) * (1.0F / 3.0F); /* a */
1433 }
1434 }
1435 break;
1436 default:
1437 gl_problem(ctx, "unpack_float_image type" );
1438 return image;
1439 }
1440 }
1441 }
1442
1443 if (format == GL_BGR) {
1444 /* swap order of every float triplet from BGR to RGBA */
1445 GLfloat *buffer = (GLfloat *) image->Data;
1446 for (i=0; i<width*height*depth; i++) {
1447 GLfloat b = buffer[i*3+0];
1448 GLfloat r = buffer[i*3+2];
1449 buffer[i*3+0] = r;
1450 buffer[i*3+2] = b;
1451 }
1452 }
1453 else if (format == GL_BGRA) {
1454 /* swap order of every float quadruplet from BGRA to RGBA */
1455 GLfloat *buffer = (GLfloat *) image->Data;
1456 for (i=0; i<width*height*depth; i++) {
1457 GLfloat b = buffer[i*4+0];
1458 GLfloat r = buffer[i*4+2];
1459 buffer[i*4+0] = r;
1460 buffer[i*4+2] = b;
1461 }
1462 }
1463 else if (format == GL_ABGR_EXT) {
1464 /* swap order of every float quadruplet from ABGR to RGBA */
1465 GLfloat *buffer = (GLfloat *) image->Data;
1466 for (i=0; i<width*height*depth; i++) {
1467 GLfloat a = buffer[i*4+0];
1468 GLfloat b = buffer[i*4+1];
1469 GLfloat g = buffer[i*4+2];
1470 GLfloat r = buffer[i*4+3];
1471 buffer[i*4+0] = r;
1472 buffer[i*4+1] = g;
1473 buffer[i*4+2] = b;
1474 buffer[i*4+3] = a;
1475 }
1476 }
1477
1478 return image;
1479 }
1480
1481
1482
1483 /*
1484 * Unpack a bitmap image, using current glPixelStore parameters,
1485 * making a new gl_image.
1486 */
1487 struct gl_image *gl_unpack_bitmap( GLcontext *ctx,
1488 GLsizei width, GLsizei height,
1489 const GLubyte *bitmap,
1490 const struct gl_pixelstore_attrib *packing )
1491 {
1492 return gl_unpack_image( ctx, width, height,
1493 GL_COLOR_INDEX, GL_BITMAP, bitmap, packing );
1494 }
1495
1496
1497
1498 /*
1499 * Unpack a 2-D image from user's buffer. Return pointer to new
1500 * gl_image struct.
1501 *
1502 * Input: width, height - size in pixels
1503 * format - format of incoming pixel data
1504 * type - datatype of incoming pixel data
1505 * pixels - pointer to unpacked image in user buffer
1506 */
1507 struct gl_image *gl_unpack_image( GLcontext *ctx,
1508 GLint width, GLint height,
1509 GLenum format, GLenum type,
1510 const GLvoid *pixels,
1511 const struct gl_pixelstore_attrib *packing )
1512 {
1513 return gl_unpack_image3D( ctx, width, height, 1,
1514 format, type, pixels, packing );
1515 }
1516
1517
1518
1519 /*
1520 * Unpack a 1, 2 or 3-D image from user-supplied address, returning a
1521 * pointer to a new gl_image struct.
1522 * This function is always called by a higher-level unpack function such
1523 * as gl_unpack_texsubimage() or gl_unpack_bitmap().
1524 *
1525 * Input: width, height, depth - size in pixels
1526 * format - format of incoming pixel data
1527 * type - datatype of incoming pixel data
1528 * pixels - pointer to unpacked image.
1529 */
1530 struct gl_image *gl_unpack_image3D( GLcontext *ctx,
1531 GLint width, GLint height, GLint depth,
1532 GLenum format, GLenum type,
1533 const GLvoid *pixels,
1534 const struct gl_pixelstore_attrib *packing)
1535 {
1536 if (width <= 0 || height <= 0 || depth <= 0) {
1537 return alloc_error_image(width, height, depth, format, type);
1538 }
1539
1540 if (type==GL_BITMAP) {
1541 if (format != GL_COLOR_INDEX && format != GL_STENCIL_INDEX) {
1542 return alloc_error_image(width, height, depth, format, type);
1543 }
1544 else {
1545 return unpack_bitmap( format, width, height, pixels, packing );
1546 }
1547 }
1548 else if (format==GL_DEPTH_COMPONENT) {
1549 /* TODO: pack as GLdepth values (GLushort or GLuint) */
1550 return unpack_depth_image( ctx, type, width, height, pixels, packing );
1551 }
1552 else if (format==GL_STENCIL_INDEX) {
1553 /* TODO: pack as GLstencil (GLubyte or GLushort) */
1554 return unpack_stencil_image( ctx, type, width, height, pixels, packing );
1555 }
1556 else if (type==GL_UNSIGNED_BYTE) {
1557 /* upack, convert to GLubytes */
1558 return unpack_ubyte_image( width, height, depth, format, pixels, packing );
1559 }
1560 else {
1561 /* upack, convert to floats */
1562 return unpack_float_image( ctx, width, height, depth,
1563 format, type, pixels, packing );
1564 }
1565
1566 /* never get here */
1567 /*return NULL;*/
1568 }
1569
1570
1571 /*
1572 * Apply pixel-transfer operations (scale, bias, mapping) to a single row
1573 * of a gl_image. Put resulting color components into result array.
1574 */
1575 void gl_scale_bias_map_image_data( const GLcontext *ctx,
1576 const struct gl_image *image,
1577 GLint row, GLubyte result[] )
1578 {
1579 GLint start, i;
1580
1581 assert(ctx);
1582 assert(image);
1583 assert(result);
1584 assert(row >= 0);
1585
1586 start = row * image->Width * image->Components;
1587
1588 for (i=0; i < image->Width; i++) {
1589 GLint pos = start+i;
1590 GLfloat red, green, blue, alpha;
1591 if (image->Type == GL_UNSIGNED_BYTE) {
1592 const GLubyte *data = (GLubyte *) image->Data;
1593 switch (image->Format) {
1594 case GL_RED:
1595 red = data[pos] * (1.0F/255.0F);
1596 green = 0;
1597 blue = 0;
1598 alpha = 0;
1599 break;
1600 case GL_RGB:
1601 red = data[pos*3+0] * (1.0F/255.0F);
1602 green = data[pos*3+1] * (1.0F/255.0F);
1603 blue = data[pos*3+2] * (1.0F/255.0F);
1604 alpha = 0;
1605 break;
1606 default:
1607 gl_problem(ctx, "bad image format in gl_scale...image_data");
1608 return;
1609 }
1610 }
1611 else if (image->Type == GL_FLOAT) {
1612 const GLubyte *data = (GLubyte *) image->Data;
1613 switch (image->Format) {
1614 case GL_RED:
1615 red = data[pos];
1616 green = 0;
1617 blue = 0;
1618 alpha = 0;
1619 break;
1620 case GL_RGB:
1621 red = data[pos*3+0];
1622 green = data[pos*3+1];
1623 blue = data[pos*3+2];
1624 alpha = 0;
1625 break;
1626 default:
1627 gl_problem(ctx, "bad image format in gl_scale...image_data");
1628 return;
1629 }
1630 }
1631 else {
1632 gl_problem(ctx, "Bad image type in gl_scale_...image_data");
1633 return;
1634 }
1635
1636 assert(red >= 0.0 && red <= 1.0);
1637 assert(green >= 0.0 && green <= 1.0);
1638 assert(blue >= 0.0 && blue <= 1.0);
1639 assert(alpha >= 0.0 && alpha <= 1.0);
1640
1641 /*
1642 if (scale or bias) {
1643
1644
1645 }
1646 if (mapping) {
1647
1648 }
1649 */
1650
1651 result[i*4+0] = (GLubyte) (red * 255.0);
1652 result[i*4+1] = (GLubyte) (green * 255.0);
1653 result[i*4+2] = (GLubyte) (blue * 255.0);
1654 result[i*4+3] = (GLubyte) (alpha * 255.0);
1655 }
1656 }
1657
1658
1659
1660 /*
1661 * Pack the given RGBA span into client memory at 'dest' address
1662 * in the given pixel format and type.
1663 * Optionally apply the enabled pixel transfer ops.
1664 * Pack into memory using the given packing params struct.
1665 * This is used by glReadPixels and glGetTexImage?D()
1666 * Input: ctx - the context
1667 * n - number of pixels in the span
1668 * rgba - the pixels
1669 * format - dest packing format
1670 * type - dest packing datatype
1671 * destination - destination packing address
1672 * packing - pixel packing parameters
1673 * applyTransferOps - apply scale/bias/lookup-table ops?
1674 */
1675 void gl_pack_rgba_span( const GLcontext *ctx,
1676 GLuint n, CONST GLubyte rgba[][4],
1677 GLenum format, GLenum type, GLvoid *destination,
1678 const struct gl_pixelstore_attrib *packing,
1679 GLboolean applyTransferOps )
1680 {
1681 /* Test for optimized case first */
1682 if (!ctx->Pixel.ScaleOrBiasRGBA && !ctx->Pixel.MapColorFlag &&
1683 format == GL_RGBA && type == GL_UNSIGNED_BYTE) {
1684 /* common simple case */
1685 MEMCPY( destination, rgba, n * 4 * sizeof(GLubyte) );
1686 }
1687 else if (!ctx->Pixel.ScaleOrBiasRGBA && !ctx->Pixel.MapColorFlag &&
1688 format == GL_RGB && type == GL_UNSIGNED_BYTE) {
1689 /* common simple case */
1690 GLint i;
1691 GLubyte *dest = (GLubyte *) destination;
1692 for (i = 0; i < n; i++) {
1693 dest[0] = rgba[i][RCOMP];
1694 dest[1] = rgba[i][GCOMP];
1695 dest[2] = rgba[i][BCOMP];
1696 dest += 3;
1697 }
1698 }
1699 else {
1700 GLfloat red[MAX_WIDTH], green[MAX_WIDTH], blue[MAX_WIDTH];
1701 GLfloat alpha[MAX_WIDTH], luminance[MAX_WIDTH];
1702 const GLfloat rscale = 1.0F / 255.0F;
1703 const GLfloat gscale = 1.0F / 255.0F;
1704 const GLfloat bscale = 1.0F / 255.0F;
1705 const GLfloat ascale = 1.0F / 255.0F;
1706 const GLint comps = gl_components_in_format(format);
1707 GLuint i;
1708
1709 assert(n <= MAX_WIDTH);
1710
1711 /* convert color components to floating point */
1712 for (i=0;i<n;i++) {
1713 red[i] = rgba[i][RCOMP] * rscale;
1714 green[i] = rgba[i][GCOMP] * gscale;
1715 blue[i] = rgba[i][BCOMP] * bscale;
1716 alpha[i] = rgba[i][ACOMP] * ascale;
1717 }
1718
1719 /*
1720 * Apply scale, bias and lookup-tables if enabled.
1721 */
1722 if (applyTransferOps) {
1723 if (ctx->Pixel.ScaleOrBiasRGBA) {
1724 gl_scale_and_bias_color( ctx, n, red, green, blue, alpha );
1725 }
1726 if (ctx->Pixel.MapColorFlag) {
1727 gl_map_color( ctx, n, red, green, blue, alpha );
1728 }
1729 }
1730
1731 if (format==GL_LUMINANCE || format==GL_LUMINANCE_ALPHA) {
1732 for (i=0;i<n;i++) {
1733 GLfloat sum = red[i] + green[i] + blue[i];
1734 luminance[i] = CLAMP( sum, 0.0F, 1.0F );
1735 }
1736 }
1737
1738 /*
1739 * Pack/store the pixels. Ugh! Lots of cases!!!
1740 */
1741 switch (type) {
1742 case GL_UNSIGNED_BYTE:
1743 {
1744 GLubyte *dst = (GLubyte *) destination;
1745 switch (format) {
1746 case GL_RED:
1747 for (i=0;i<n;i++)
1748 dst[i] = FLOAT_TO_UBYTE(red[i]);
1749 break;
1750 case GL_GREEN:
1751 for (i=0;i<n;i++)
1752 dst[i] = FLOAT_TO_UBYTE(green[i]);
1753 break;
1754 case GL_BLUE:
1755 for (i=0;i<n;i++)
1756 dst[i] = FLOAT_TO_UBYTE(blue[i]);
1757 break;
1758 case GL_ALPHA:
1759 for (i=0;i<n;i++)
1760 dst[i] = FLOAT_TO_UBYTE(alpha[i]);
1761 break;
1762 case GL_LUMINANCE:
1763 for (i=0;i<n;i++)
1764 dst[i] = FLOAT_TO_UBYTE(luminance[i]);
1765 break;
1766 case GL_LUMINANCE_ALPHA:
1767 for (i=0;i<n;i++) {
1768 dst[i*2+0] = FLOAT_TO_UBYTE(luminance[i]);
1769 dst[i*2+1] = FLOAT_TO_UBYTE(alpha[i]);
1770 }
1771 break;
1772 case GL_RGB:
1773 for (i=0;i<n;i++) {
1774 dst[i*3+0] = FLOAT_TO_UBYTE(red[i]);
1775 dst[i*3+1] = FLOAT_TO_UBYTE(green[i]);
1776 dst[i*3+2] = FLOAT_TO_UBYTE(blue[i]);
1777 }
1778 break;
1779 case GL_RGBA:
1780 for (i=0;i<n;i++) {
1781 dst[i*4+0] = FLOAT_TO_UBYTE(red[i]);
1782 dst[i*4+1] = FLOAT_TO_UBYTE(green[i]);
1783 dst[i*4+2] = FLOAT_TO_UBYTE(blue[i]);
1784 dst[i*4+3] = FLOAT_TO_UBYTE(alpha[i]);
1785 }
1786 break;
1787 case GL_BGR:
1788 for (i=0;i<n;i++) {
1789 dst[i*3+0] = FLOAT_TO_UBYTE(blue[i]);
1790 dst[i*3+1] = FLOAT_TO_UBYTE(green[i]);
1791 dst[i*3+2] = FLOAT_TO_UBYTE(red[i]);
1792 }
1793 break;
1794 case GL_BGRA:
1795 for (i=0;i<n;i++) {
1796 dst[i*4+0] = FLOAT_TO_UBYTE(blue[i]);
1797 dst[i*4+1] = FLOAT_TO_UBYTE(green[i]);
1798 dst[i*4+2] = FLOAT_TO_UBYTE(red[i]);
1799 dst[i*4+3] = FLOAT_TO_UBYTE(alpha[i]);
1800 }
1801 break;
1802 case GL_ABGR_EXT:
1803 for (i=0;i<n;i++) {
1804 dst[i*4+0] = FLOAT_TO_UBYTE(alpha[i]);
1805 dst[i*4+1] = FLOAT_TO_UBYTE(blue[i]);
1806 dst[i*4+2] = FLOAT_TO_UBYTE(green[i]);
1807 dst[i*4+3] = FLOAT_TO_UBYTE(red[i]);
1808 }
1809 break;
1810 default:
1811 gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
1812 }
1813 }
1814 break;
1815 case GL_BYTE:
1816 {
1817 GLbyte *dst = (GLbyte *) destination;
1818 switch (format) {
1819 case GL_RED:
1820 for (i=0;i<n;i++)
1821 dst[i] = FLOAT_TO_BYTE(red[i]);
1822 break;
1823 case GL_GREEN:
1824 for (i=0;i<n;i++)
1825 dst[i] = FLOAT_TO_BYTE(green[i]);
1826 break;
1827 case GL_BLUE:
1828 for (i=0;i<n;i++)
1829 dst[i] = FLOAT_TO_BYTE(blue[i]);
1830 break;
1831 case GL_ALPHA:
1832 for (i=0;i<n;i++)
1833 dst[i] = FLOAT_TO_BYTE(alpha[i]);
1834 break;
1835 case GL_LUMINANCE:
1836 for (i=0;i<n;i++)
1837 dst[i] = FLOAT_TO_BYTE(luminance[i]);
1838 break;
1839 case GL_LUMINANCE_ALPHA:
1840 for (i=0;i<n;i++) {
1841 dst[i*2+0] = FLOAT_TO_BYTE(luminance[i]);
1842 dst[i*2+1] = FLOAT_TO_BYTE(alpha[i]);
1843 }
1844 break;
1845 case GL_RGB:
1846 for (i=0;i<n;i++) {
1847 dst[i*3+0] = FLOAT_TO_BYTE(red[i]);
1848 dst[i*3+1] = FLOAT_TO_BYTE(green[i]);
1849 dst[i*3+2] = FLOAT_TO_BYTE(blue[i]);
1850 }
1851 break;
1852 case GL_RGBA:
1853 for (i=0;i<n;i++) {
1854 dst[i*4+0] = FLOAT_TO_BYTE(red[i]);
1855 dst[i*4+1] = FLOAT_TO_BYTE(green[i]);
1856 dst[i*4+2] = FLOAT_TO_BYTE(blue[i]);
1857 dst[i*4+3] = FLOAT_TO_BYTE(alpha[i]);
1858 }
1859 break;
1860 case GL_BGR:
1861 for (i=0;i<n;i++) {
1862 dst[i*3+0] = FLOAT_TO_BYTE(blue[i]);
1863 dst[i*3+1] = FLOAT_TO_BYTE(green[i]);
1864 dst[i*3+2] = FLOAT_TO_BYTE(red[i]);
1865 }
1866 break;
1867 case GL_BGRA:
1868 for (i=0;i<n;i++) {
1869 dst[i*4+0] = FLOAT_TO_BYTE(blue[i]);
1870 dst[i*4+1] = FLOAT_TO_BYTE(green[i]);
1871 dst[i*4+2] = FLOAT_TO_BYTE(red[i]);
1872 dst[i*4+3] = FLOAT_TO_BYTE(alpha[i]);
1873 }
1874 case GL_ABGR_EXT:
1875 for (i=0;i<n;i++) {
1876 dst[i*4+0] = FLOAT_TO_BYTE(alpha[i]);
1877 dst[i*4+1] = FLOAT_TO_BYTE(blue[i]);
1878 dst[i*4+2] = FLOAT_TO_BYTE(green[i]);
1879 dst[i*4+3] = FLOAT_TO_BYTE(red[i]);
1880 }
1881 break;
1882 default:
1883 gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
1884 }
1885 }
1886 break;
1887 case GL_UNSIGNED_SHORT:
1888 {
1889 GLushort *dst = (GLushort *) destination;
1890 switch (format) {
1891 case GL_RED:
1892 for (i=0;i<n;i++)
1893 dst[i] = FLOAT_TO_USHORT(red[i]);
1894 break;
1895 case GL_GREEN:
1896 for (i=0;i<n;i++)
1897 dst[i] = FLOAT_TO_USHORT(green[i]);
1898 break;
1899 case GL_BLUE:
1900 for (i=0;i<n;i++)
1901 dst[i] = FLOAT_TO_USHORT(blue[i]);
1902 break;
1903 case GL_ALPHA:
1904 for (i=0;i<n;i++)
1905 dst[i] = FLOAT_TO_USHORT(alpha[i]);
1906 break;
1907 case GL_LUMINANCE:
1908 for (i=0;i<n;i++)
1909 dst[i] = FLOAT_TO_USHORT(luminance[i]);
1910 break;
1911 case GL_LUMINANCE_ALPHA:
1912 for (i=0;i<n;i++) {
1913 dst[i*2+0] = FLOAT_TO_USHORT(luminance[i]);
1914 dst[i*2+1] = FLOAT_TO_USHORT(alpha[i]);
1915 }
1916 break;
1917 case GL_RGB:
1918 for (i=0;i<n;i++) {
1919 dst[i*3+0] = FLOAT_TO_USHORT(red[i]);
1920 dst[i*3+1] = FLOAT_TO_USHORT(green[i]);
1921 dst[i*3+2] = FLOAT_TO_USHORT(blue[i]);
1922 }
1923 break;
1924 case GL_RGBA:
1925 for (i=0;i<n;i++) {
1926 dst[i*4+0] = FLOAT_TO_USHORT(red[i]);
1927 dst[i*4+1] = FLOAT_TO_USHORT(green[i]);
1928 dst[i*4+2] = FLOAT_TO_USHORT(blue[i]);
1929 dst[i*4+3] = FLOAT_TO_USHORT(alpha[i]);
1930 }
1931 break;
1932 case GL_BGR:
1933 for (i=0;i<n;i++) {
1934 dst[i*3+0] = FLOAT_TO_USHORT(blue[i]);
1935 dst[i*3+1] = FLOAT_TO_USHORT(green[i]);
1936 dst[i*3+2] = FLOAT_TO_USHORT(red[i]);
1937 }
1938 break;
1939 case GL_BGRA:
1940 for (i=0;i<n;i++) {
1941 dst[i*4+0] = FLOAT_TO_USHORT(blue[i]);
1942 dst[i*4+1] = FLOAT_TO_USHORT(green[i]);
1943 dst[i*4+2] = FLOAT_TO_USHORT(red[i]);
1944 dst[i*4+3] = FLOAT_TO_USHORT(alpha[i]);
1945 }
1946 break;
1947 case GL_ABGR_EXT:
1948 for (i=0;i<n;i++) {
1949 dst[i*4+0] = FLOAT_TO_USHORT(alpha[i]);
1950 dst[i*4+1] = FLOAT_TO_USHORT(blue[i]);
1951 dst[i*4+2] = FLOAT_TO_USHORT(green[i]);
1952 dst[i*4+3] = FLOAT_TO_USHORT(red[i]);
1953 }
1954 break;
1955 default:
1956 gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
1957 }
1958 if (packing->SwapBytes) {
1959 gl_swap2( (GLushort *) dst, n * comps);
1960 }
1961 }
1962 break;
1963 case GL_SHORT:
1964 {
1965 GLshort *dst = (GLshort *) destination;
1966 switch (format) {
1967 case GL_RED:
1968 for (i=0;i<n;i++)
1969 dst[i] = FLOAT_TO_SHORT(red[i]);
1970 break;
1971 case GL_GREEN:
1972 for (i=0;i<n;i++)
1973 dst[i] = FLOAT_TO_SHORT(green[i]);
1974 break;
1975 case GL_BLUE:
1976 for (i=0;i<n;i++)
1977 dst[i] = FLOAT_TO_SHORT(blue[i]);
1978 break;
1979 case GL_ALPHA:
1980 for (i=0;i<n;i++)
1981 dst[i] = FLOAT_TO_SHORT(alpha[i]);
1982 break;
1983 case GL_LUMINANCE:
1984 for (i=0;i<n;i++)
1985 dst[i] = FLOAT_TO_SHORT(luminance[i]);
1986 break;
1987 case GL_LUMINANCE_ALPHA:
1988 for (i=0;i<n;i++) {
1989 dst[i*2+0] = FLOAT_TO_SHORT(luminance[i]);
1990 dst[i*2+1] = FLOAT_TO_SHORT(alpha[i]);
1991 }
1992 break;
1993 case GL_RGB:
1994 for (i=0;i<n;i++) {
1995 dst[i*3+0] = FLOAT_TO_SHORT(red[i]);
1996 dst[i*3+1] = FLOAT_TO_SHORT(green[i]);
1997 dst[i*3+2] = FLOAT_TO_SHORT(blue[i]);
1998 }
1999 break;
2000 case GL_RGBA:
2001 for (i=0;i<n;i++) {
2002 dst[i*4+0] = FLOAT_TO_SHORT(red[i]);
2003 dst[i*4+1] = FLOAT_TO_SHORT(green[i]);
2004 dst[i*4+2] = FLOAT_TO_SHORT(blue[i]);
2005 dst[i*4+3] = FLOAT_TO_SHORT(alpha[i]);
2006 }
2007 break;
2008 case GL_BGR:
2009 for (i=0;i<n;i++) {
2010 dst[i*3+0] = FLOAT_TO_SHORT(blue[i]);
2011 dst[i*3+1] = FLOAT_TO_SHORT(green[i]);
2012 dst[i*3+2] = FLOAT_TO_SHORT(red[i]);
2013 }
2014 break;
2015 case GL_BGRA:
2016 for (i=0;i<n;i++) {
2017 dst[i*4+0] = FLOAT_TO_SHORT(blue[i]);
2018 dst[i*4+1] = FLOAT_TO_SHORT(green[i]);
2019 dst[i*4+2] = FLOAT_TO_SHORT(red[i]);
2020 dst[i*4+3] = FLOAT_TO_SHORT(alpha[i]);
2021 }
2022 case GL_ABGR_EXT:
2023 for (i=0;i<n;i++) {
2024 dst[i*4+0] = FLOAT_TO_SHORT(alpha[i]);
2025 dst[i*4+1] = FLOAT_TO_SHORT(blue[i]);
2026 dst[i*4+2] = FLOAT_TO_SHORT(green[i]);
2027 dst[i*4+3] = FLOAT_TO_SHORT(red[i]);
2028 }
2029 break;
2030 default:
2031 gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
2032 }
2033 if (packing->SwapBytes) {
2034 gl_swap2( (GLushort *) dst, n * comps );
2035 }
2036 }
2037 break;
2038 case GL_UNSIGNED_INT:
2039 {
2040 GLuint *dst = (GLuint *) destination;
2041 switch (format) {
2042 case GL_RED:
2043 for (i=0;i<n;i++)
2044 dst[i] = FLOAT_TO_UINT(red[i]);
2045 break;
2046 case GL_GREEN:
2047 for (i=0;i<n;i++)
2048 dst[i] = FLOAT_TO_UINT(green[i]);
2049 break;
2050 case GL_BLUE:
2051 for (i=0;i<n;i++)
2052 dst[i] = FLOAT_TO_UINT(blue[i]);
2053 break;
2054 case GL_ALPHA:
2055 for (i=0;i<n;i++)
2056 dst[i] = FLOAT_TO_UINT(alpha[i]);
2057 break;
2058 case GL_LUMINANCE:
2059 for (i=0;i<n;i++)
2060 dst[i] = FLOAT_TO_UINT(luminance[i]);
2061 break;
2062 case GL_LUMINANCE_ALPHA:
2063 for (i=0;i<n;i++) {
2064 dst[i*2+0] = FLOAT_TO_UINT(luminance[i]);
2065 dst[i*2+1] = FLOAT_TO_UINT(alpha[i]);
2066 }
2067 break;
2068 case GL_RGB:
2069 for (i=0;i<n;i++) {
2070 dst[i*3+0] = FLOAT_TO_UINT(red[i]);
2071 dst[i*3+1] = FLOAT_TO_UINT(green[i]);
2072 dst[i*3+2] = FLOAT_TO_UINT(blue[i]);
2073 }
2074 break;
2075 case GL_RGBA:
2076 for (i=0;i<n;i++) {
2077 dst[i*4+0] = FLOAT_TO_UINT(red[i]);
2078 dst[i*4+1] = FLOAT_TO_UINT(green[i]);
2079 dst[i*4+2] = FLOAT_TO_UINT(blue[i]);
2080 dst[i*4+3] = FLOAT_TO_UINT(alpha[i]);
2081 }
2082 break;
2083 case GL_BGR:
2084 for (i=0;i<n;i++) {
2085 dst[i*3+0] = FLOAT_TO_UINT(blue[i]);
2086 dst[i*3+1] = FLOAT_TO_UINT(green[i]);
2087 dst[i*3+2] = FLOAT_TO_UINT(red[i]);
2088 }
2089 break;
2090 case GL_BGRA:
2091 for (i=0;i<n;i++) {
2092 dst[i*4+0] = FLOAT_TO_UINT(blue[i]);
2093 dst[i*4+1] = FLOAT_TO_UINT(green[i]);
2094 dst[i*4+2] = FLOAT_TO_UINT(red[i]);
2095 dst[i*4+3] = FLOAT_TO_UINT(alpha[i]);
2096 }
2097 break;
2098 case GL_ABGR_EXT:
2099 for (i=0;i<n;i++) {
2100 dst[i*4+0] = FLOAT_TO_UINT(alpha[i]);
2101 dst[i*4+1] = FLOAT_TO_UINT(blue[i]);
2102 dst[i*4+2] = FLOAT_TO_UINT(green[i]);
2103 dst[i*4+3] = FLOAT_TO_UINT(red[i]);
2104 }
2105 break;
2106 default:
2107 gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
2108 }
2109 if (packing->SwapBytes) {
2110 gl_swap4( (GLuint *) dst, n * comps );
2111 }
2112 }
2113 break;
2114 case GL_INT:
2115 {
2116 GLint *dst = (GLint *) destination;
2117 switch (format) {
2118 case GL_RED:
2119 for (i=0;i<n;i++)
2120 dst[i] = FLOAT_TO_INT(red[i]);
2121 break;
2122 case GL_GREEN:
2123 for (i=0;i<n;i++)
2124 dst[i] = FLOAT_TO_INT(green[i]);
2125 break;
2126 case GL_BLUE:
2127 for (i=0;i<n;i++)
2128 dst[i] = FLOAT_TO_INT(blue[i]);
2129 break;
2130 case GL_ALPHA:
2131 for (i=0;i<n;i++)
2132 dst[i] = FLOAT_TO_INT(alpha[i]);
2133 break;
2134 case GL_LUMINANCE:
2135 for (i=0;i<n;i++)
2136 dst[i] = FLOAT_TO_INT(luminance[i]);
2137 break;
2138 case GL_LUMINANCE_ALPHA:
2139 for (i=0;i<n;i++) {
2140 dst[i*2+0] = FLOAT_TO_INT(luminance[i]);
2141 dst[i*2+1] = FLOAT_TO_INT(alpha[i]);
2142 }
2143 break;
2144 case GL_RGB:
2145 for (i=0;i<n;i++) {
2146 dst[i*3+0] = FLOAT_TO_INT(red[i]);
2147 dst[i*3+1] = FLOAT_TO_INT(green[i]);
2148 dst[i*3+2] = FLOAT_TO_INT(blue[i]);
2149 }
2150 break;
2151 case GL_RGBA:
2152 for (i=0;i<n;i++) {
2153 dst[i*4+0] = FLOAT_TO_INT(red[i]);
2154 dst[i*4+1] = FLOAT_TO_INT(green[i]);
2155 dst[i*4+2] = FLOAT_TO_INT(blue[i]);
2156 dst[i*4+3] = FLOAT_TO_INT(alpha[i]);
2157 }
2158 break;
2159 case GL_BGR:
2160 for (i=0;i<n;i++) {
2161 dst[i*3+0] = FLOAT_TO_INT(blue[i]);
2162 dst[i*3+1] = FLOAT_TO_INT(green[i]);
2163 dst[i*3+2] = FLOAT_TO_INT(red[i]);
2164 }
2165 break;
2166 case GL_BGRA:
2167 for (i=0;i<n;i++) {
2168 dst[i*4+0] = FLOAT_TO_INT(blue[i]);
2169 dst[i*4+1] = FLOAT_TO_INT(green[i]);
2170 dst[i*4+2] = FLOAT_TO_INT(red[i]);
2171 dst[i*4+3] = FLOAT_TO_INT(alpha[i]);
2172 }
2173 break;
2174 case GL_ABGR_EXT:
2175 for (i=0;i<n;i++) {
2176 dst[i*4+0] = FLOAT_TO_INT(alpha[i]);
2177 dst[i*4+1] = FLOAT_TO_INT(blue[i]);
2178 dst[i*4+2] = FLOAT_TO_INT(green[i]);
2179 dst[i*4+3] = FLOAT_TO_INT(red[i]);
2180 }
2181 break;
2182 default:
2183 gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
2184 }
2185 if (packing->SwapBytes) {
2186 gl_swap4( (GLuint *) dst, n * comps );
2187 }
2188 }
2189 break;
2190 case GL_FLOAT:
2191 {
2192 GLfloat *dst = (GLfloat *) destination;
2193 switch (format) {
2194 case GL_RED:
2195 for (i=0;i<n;i++)
2196 dst[i] = red[i];
2197 break;
2198 case GL_GREEN:
2199 for (i=0;i<n;i++)
2200 dst[i] = green[i];
2201 break;
2202 case GL_BLUE:
2203 for (i=0;i<n;i++)
2204 dst[i] = blue[i];
2205 break;
2206 case GL_ALPHA:
2207 for (i=0;i<n;i++)
2208 dst[i] = alpha[i];
2209 break;
2210 case GL_LUMINANCE:
2211 for (i=0;i<n;i++)
2212 dst[i] = luminance[i];
2213 break;
2214 case GL_LUMINANCE_ALPHA:
2215 for (i=0;i<n;i++) {
2216 dst[i*2+0] = luminance[i];
2217 dst[i*2+1] = alpha[i];
2218 }
2219 break;
2220 case GL_RGB:
2221 for (i=0;i<n;i++) {
2222 dst[i*3+0] = red[i];
2223 dst[i*3+1] = green[i];
2224 dst[i*3+2] = blue[i];
2225 }
2226 break;
2227 case GL_RGBA:
2228 for (i=0;i<n;i++) {
2229 dst[i*4+0] = red[i];
2230 dst[i*4+1] = green[i];
2231 dst[i*4+2] = blue[i];
2232 dst[i*4+3] = alpha[i];
2233 }
2234 break;
2235 case GL_BGR:
2236 for (i=0;i<n;i++) {
2237 dst[i*3+0] = blue[i];
2238 dst[i*3+1] = green[i];
2239 dst[i*3+2] = red[i];
2240 }
2241 break;
2242 case GL_BGRA:
2243 for (i=0;i<n;i++) {
2244 dst[i*4+0] = blue[i];
2245 dst[i*4+1] = green[i];
2246 dst[i*4+2] = red[i];
2247 dst[i*4+3] = alpha[i];
2248 }
2249 break;
2250 case GL_ABGR_EXT:
2251 for (i=0;i<n;i++) {
2252 dst[i*4+0] = alpha[i];
2253 dst[i*4+1] = blue[i];
2254 dst[i*4+2] = green[i];
2255 dst[i*4+3] = red[i];
2256 }
2257 break;
2258 default:
2259 gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
2260 }
2261 if (packing->SwapBytes) {
2262 gl_swap4( (GLuint *) dst, n * comps );
2263 }
2264 }
2265 break;
2266 case GL_UNSIGNED_BYTE_3_3_2:
2267 if (format == GL_RGB) {
2268 GLubyte *dst = (GLubyte *) destination;
2269 for (i=0;i<n;i++) {
2270 dst[i] = (((GLint) (red[i] * 7.0F)) << 5)
2271 | (((GLint) (green[i] * 7.0F)) << 2)
2272 | (((GLint) (blue[i] * 3.0F)) );
2273 }
2274 }
2275 break;
2276 case GL_UNSIGNED_BYTE_2_3_3_REV:
2277 if (format == GL_RGB) {
2278 GLubyte *dst = (GLubyte *) destination;
2279 for (i=0;i<n;i++) {
2280 dst[i] = (((GLint) (red[i] * 7.0F)) )
2281 | (((GLint) (green[i] * 7.0F)) << 3)
2282 | (((GLint) (blue[i] * 3.0F)) << 5);
2283 }
2284 }
2285 break;
2286 case GL_UNSIGNED_SHORT_5_6_5:
2287 if (format == GL_RGB) {
2288 GLushort *dst = (GLushort *) destination;
2289 for (i=0;i<n;i++) {
2290 dst[i] = (((GLint) (red[i] * 31.0F)) << 11)
2291 | (((GLint) (green[i] * 63.0F)) << 5)
2292 | (((GLint) (blue[i] * 31.0F)) );
2293 }
2294 }
2295 break;
2296 case GL_UNSIGNED_SHORT_5_6_5_REV:
2297 if (format == GL_RGB) {
2298 GLushort *dst = (GLushort *) destination;
2299 for (i=0;i<n;i++) {
2300 dst[i] = (((GLint) (red[i] * 31.0F)) )
2301 | (((GLint) (green[i] * 63.0F)) << 5)
2302 | (((GLint) (blue[i] * 31.0F)) << 11);
2303 }
2304 }
2305 break;
2306 case GL_UNSIGNED_SHORT_4_4_4_4:
2307 if (format == GL_RGB) {
2308 GLushort *dst = (GLushort *) destination;
2309 for (i=0;i<n;i++) {
2310 dst[i] = (((GLint) (red[i] * 15.0F)) << 12)
2311 | (((GLint) (green[i] * 15.0F)) << 8)
2312 | (((GLint) (blue[i] * 15.0F)) << 4)
2313 | (((GLint) (alpha[i] * 15.0F)) );
2314 }
2315 }
2316 break;
2317 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
2318 if (format == GL_RGB) {
2319 GLushort *dst = (GLushort *) destination;
2320 for (i=0;i<n;i++) {
2321 dst[i] = (((GLint) (red[i] * 15.0F)) )
2322 | (((GLint) (green[i] * 15.0F)) << 4)
2323 | (((GLint) (blue[i] * 15.0F)) << 8)
2324 | (((GLint) (alpha[i] * 15.0F)) << 12);
2325 }
2326 }
2327 break;
2328 case GL_UNSIGNED_SHORT_5_5_5_1:
2329 if (format == GL_RGB) {
2330 GLushort *dst = (GLushort *) destination;
2331 for (i=0;i<n;i++) {
2332 dst[i] = (((GLint) (red[i] * 31.0F)) << 11)
2333 | (((GLint) (green[i] * 31.0F)) << 6)
2334 | (((GLint) (blue[i] * 31.0F)) << 1)
2335 | (((GLint) (alpha[i] * 1.0F)) );
2336 }
2337 }
2338 break;
2339 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
2340 if (format == GL_RGB) {
2341 GLushort *dst = (GLushort *) destination;
2342 for (i=0;i<n;i++) {
2343 dst[i] = (((GLint) (red[i] * 31.0F)) )
2344 | (((GLint) (green[i] * 31.0F)) << 5)
2345 | (((GLint) (blue[i] * 31.0F)) << 10)
2346 | (((GLint) (alpha[i] * 1.0F)) << 15);
2347 }
2348 }
2349 break;
2350 case GL_UNSIGNED_INT_8_8_8_8:
2351 if (format == GL_RGBA) {
2352 GLuint *dst = (GLuint *) destination;
2353 for (i=0;i<n;i++) {
2354 dst[i] = (((GLuint) (red[i] * 255.0F)) << 24)
2355 | (((GLuint) (green[i] * 255.0F)) << 16)
2356 | (((GLuint) (blue[i] * 255.0F)) << 8)
2357 | (((GLuint) (alpha[i] * 255.0F)) );
2358 }
2359 }
2360 else if (format == GL_BGRA) {
2361 GLuint *dst = (GLuint *) destination;
2362 for (i=0;i<n;i++) {
2363 dst[i] = (((GLuint) (blue[i] * 255.0F)) << 24)
2364 | (((GLuint) (green[i] * 255.0F)) << 16)
2365 | (((GLuint) (red[i] * 255.0F)) << 8)
2366 | (((GLuint) (alpha[i] * 255.0F)) );
2367 }
2368 }
2369 else if (format == GL_ABGR_EXT) {
2370 GLuint *dst = (GLuint *) destination;
2371 for (i=0;i<n;i++) {
2372 dst[i] = (((GLuint) (alpha[i] * 255.0F)) << 24)
2373 | (((GLuint) (blue[i] * 255.0F)) << 16)
2374 | (((GLuint) (green[i] * 255.0F)) << 8)
2375 | (((GLuint) (red[i] * 255.0F)) );
2376 }
2377 }
2378 break;
2379 case GL_UNSIGNED_INT_8_8_8_8_REV:
2380 if (format == GL_RGBA) {
2381 GLuint *dst = (GLuint *) destination;
2382 for (i=0;i<n;i++) {
2383 dst[i] = (((GLuint) (red[i] * 255.0F)) )
2384 | (((GLuint) (green[i] * 255.0F)) << 8)
2385 | (((GLuint) (blue[i] * 255.0F)) << 16)
2386 | (((GLuint) (alpha[i] * 255.0F)) << 24);
2387 }
2388 }
2389 else if (format == GL_BGRA) {
2390 GLuint *dst = (GLuint *) destination;
2391 for (i=0;i<n;i++) {
2392 dst[i] = (((GLuint) (blue[i] * 255.0F)) )
2393 | (((GLuint) (green[i] * 255.0F)) << 8)
2394 | (((GLuint) (red[i] * 255.0F)) << 16)
2395 | (((GLuint) (alpha[i] * 255.0F)) << 24);
2396 }
2397 }
2398 else if (format == GL_ABGR_EXT) {
2399 GLuint *dst = (GLuint *) destination;
2400 for (i=0;i<n;i++) {
2401 dst[i] = (((GLuint) (alpha[i] * 255.0F)) )
2402 | (((GLuint) (blue[i] * 255.0F)) << 8)
2403 | (((GLuint) (green[i] * 255.0F)) << 16)
2404 | (((GLuint) (red[i] * 255.0F)) << 24);
2405 }
2406 }
2407 break;
2408 case GL_UNSIGNED_INT_10_10_10_2:
2409 if (format == GL_RGBA) {
2410 GLuint *dst = (GLuint *) destination;
2411 for (i=0;i<n;i++) {
2412 dst[i] = (((GLuint) (red[i] * 1023.0F)) << 22)
2413 | (((GLuint) (green[i] * 1023.0F)) << 12)
2414 | (((GLuint) (blue[i] * 1023.0F)) << 2)
2415 | (((GLuint) (alpha[i] * 3.0F)) );
2416 }
2417 }
2418 else if (format == GL_BGRA) {
2419 GLuint *dst = (GLuint *) destination;
2420 for (i=0;i<n;i++) {
2421 dst[i] = (((GLuint) (blue[i] * 1023.0F)) << 22)
2422 | (((GLuint) (green[i] * 1023.0F)) << 12)
2423 | (((GLuint) (red[i] * 1023.0F)) << 2)
2424 | (((GLuint) (alpha[i] * 3.0F)) );
2425 }
2426 }
2427 else if (format == GL_ABGR_EXT) {
2428 GLuint *dst = (GLuint *) destination;
2429 for (i=0;i<n;i++) {
2430 dst[i] = (((GLuint) (alpha[i] * 1023.0F)) << 22)
2431 | (((GLuint) (blue[i] * 1023.0F)) << 12)
2432 | (((GLuint) (green[i] * 1023.0F)) << 2)
2433 | (((GLuint) (red[i] * 3.0F)) );
2434 }
2435 }
2436 break;
2437 case GL_UNSIGNED_INT_2_10_10_10_REV:
2438 if (format == GL_RGBA) {
2439 GLuint *dst = (GLuint *) destination;
2440 for (i=0;i<n;i++) {
2441 dst[i] = (((GLuint) (red[i] * 1023.0F)) )
2442 | (((GLuint) (green[i] * 1023.0F)) << 10)
2443 | (((GLuint) (blue[i] * 1023.0F)) << 20)
2444 | (((GLuint) (alpha[i] * 3.0F)) << 30);
2445 }
2446 }
2447 else if (format == GL_BGRA) {
2448 GLuint *dst = (GLuint *) destination;
2449 for (i=0;i<n;i++) {
2450 dst[i] = (((GLuint) (blue[i] * 1023.0F)) )
2451 | (((GLuint) (green[i] * 1023.0F)) << 10)
2452 | (((GLuint) (red[i] * 1023.0F)) << 20)
2453 | (((GLuint) (alpha[i] * 3.0F)) << 30);
2454 }
2455 }
2456 else if (format == GL_ABGR_EXT) {
2457 GLuint *dst = (GLuint *) destination;
2458 for (i=0;i<n;i++) {
2459 dst[i] = (((GLuint) (alpha[i] * 1023.0F)) )
2460 | (((GLuint) (blue[i] * 1023.0F)) << 10)
2461 | (((GLuint) (green[i] * 1023.0F)) << 20)
2462 | (((GLuint) (red[i] * 3.0F)) << 30);
2463 }
2464 }
2465 break;
2466 default:
2467 gl_problem( ctx, "bad type in gl_pack_rgba_span" );
2468 }
2469 }
2470 }
2471
2472
2473
2474 /*
2475 * New (3.3) functions
2476 */
2477
2478 #define SWAP2BYTE(VALUE) \
2479 { \
2480 GLubyte *bytes = (GLubyte *) &(VALUE); \
2481 GLubyte tmp = bytes[0]; \
2482 bytes[0] = bytes[1]; \
2483 bytes[1] = tmp; \
2484 }
2485
2486 #define SWAP4BYTE(VALUE) \
2487 { \
2488 GLubyte *bytes = (GLubyte *) &(VALUE); \
2489 GLubyte tmp = bytes[0]; \
2490 bytes[0] = bytes[3]; \
2491 bytes[3] = tmp; \
2492 tmp = bytes[1]; \
2493 bytes[1] = bytes[2]; \
2494 bytes[2] = tmp; \
2495 }
2496
2497
2498 static void
2499 extract_uint_indexes(GLuint n, GLuint indexes[],
2500 GLenum srcFormat, GLenum srcType, const GLvoid *src,
2501 const struct gl_pixelstore_attrib *unpack )
2502 {
2503 assert(srcFormat == GL_COLOR_INDEX);
2504
2505 ASSERT(srcType == GL_BITMAP ||
2506 srcType == GL_UNSIGNED_BYTE ||
2507 srcType == GL_BYTE ||
2508 srcType == GL_UNSIGNED_SHORT ||
2509 srcType == GL_SHORT ||
2510 srcType == GL_UNSIGNED_INT ||
2511 srcType == GL_INT ||
2512 srcType == GL_FLOAT);
2513
2514 switch (srcType) {
2515 case GL_BITMAP:
2516 {
2517 GLubyte *ubsrc = (GLubyte *) src;
2518 if (unpack->LsbFirst) {
2519 GLubyte mask = 1 << (unpack->SkipPixels & 0x7);
2520 GLuint i;
2521 for (i = 0; i < n; i++) {
2522 indexes[i] = (*ubsrc & mask) ? 1 : 0;
2523 if (mask == 128) {
2524 mask = 1;
2525 ubsrc++;
2526 }
2527 else {
2528 mask = mask << 1;
2529 }
2530 }
2531 }
2532 else {
2533 GLubyte mask = 128 >> (unpack->SkipPixels & 0x7);
2534 GLuint i;
2535 for (i = 0; i < n; i++) {
2536 indexes[i] = (*ubsrc & mask) ? 1 : 0;
2537 if (mask == 1) {
2538 mask = 128;
2539 ubsrc++;
2540 }
2541 else {
2542 mask = mask >> 1;
2543 }
2544 }
2545 }
2546 }
2547 break;
2548 case GL_UNSIGNED_BYTE:
2549 {
2550 GLuint i;
2551 const GLubyte *s = (const GLubyte *) src;
2552 for (i = 0; i < n; i++)
2553 indexes[i] = s[i];
2554 }
2555 break;
2556 case GL_BYTE:
2557 {
2558 GLuint i;
2559 const GLbyte *s = (const GLbyte *) src;
2560 for (i = 0; i < n; i++)
2561 indexes[i] = s[i];
2562 }
2563 break;
2564 case GL_UNSIGNED_SHORT:
2565 {
2566 GLuint i;
2567 const GLushort *s = (const GLushort *) src;
2568 if (unpack->SwapBytes) {
2569 for (i = 0; i < n; i++) {
2570 GLushort value = s[i];
2571 SWAP2BYTE(value);
2572 indexes[i] = value;
2573 }
2574 }
2575 else {
2576 for (i = 0; i < n; i++)
2577 indexes[i] = s[i];
2578 }
2579 }
2580 break;
2581 case GL_SHORT:
2582 {
2583 GLuint i;
2584 const GLshort *s = (const GLshort *) src;
2585 if (unpack->SwapBytes) {
2586 for (i = 0; i < n; i++) {
2587 GLshort value = s[i];
2588 SWAP2BYTE(value);
2589 indexes[i] = value;
2590 }
2591 }
2592 else {
2593 for (i = 0; i < n; i++)
2594 indexes[i] = s[i];
2595 }
2596 }
2597 break;
2598 case GL_UNSIGNED_INT:
2599 {
2600 GLuint i;
2601 const GLuint *s = (const GLuint *) src;
2602 if (unpack->SwapBytes) {
2603 for (i = 0; i < n; i++) {
2604 GLuint value = s[i];
2605 SWAP4BYTE(value);
2606 indexes[i] = value;
2607 }
2608 }
2609 else {
2610 for (i = 0; i < n; i++)
2611 indexes[i] = s[i];
2612 }
2613 }
2614 break;
2615 case GL_INT:
2616 {
2617 GLuint i;
2618 const GLint *s = (const GLint *) src;
2619 if (unpack->SwapBytes) {
2620 for (i = 0; i < n; i++) {
2621 GLint value = s[i];
2622 SWAP4BYTE(value);
2623 indexes[i] = value;
2624 }
2625 }
2626 else {
2627 for (i = 0; i < n; i++)
2628 indexes[i] = s[i];
2629 }
2630 }
2631 break;
2632 case GL_FLOAT:
2633 {
2634 GLuint i;
2635 const GLfloat *s = (const GLfloat *) src;
2636 if (unpack->SwapBytes) {
2637 for (i = 0; i < n; i++) {
2638 GLfloat value = s[i];
2639 SWAP4BYTE(value);
2640 indexes[i] = value;
2641 }
2642 }
2643 else {
2644 for (i = 0; i < n; i++)
2645 indexes[i] = s[i];
2646 }
2647 }
2648 break;
2649 default:
2650 gl_problem(NULL, "bad srcType in extract_uint_indexes");
2651 return;
2652 }
2653 }
2654
2655
2656
2657 /*
2658 * This function extracts floating point RGBA values from arbitrary
2659 * image data. srcFormat and srcType are the format and type parameters
2660 * passed to glDrawPixels, glTexImage[123]D, glTexSubImage[123]D, etc.
2661 *
2662 * Refering to section 3.6.4 of the OpenGL 1.2 spec, this function
2663 * implements the "Conversion to floating point", "Conversion to RGB",
2664 * and "Final Expansion to RGBA" operations.
2665 *
2666 * Args: n - number of pixels
2667 * rgba - output colors
2668 * srcFormat - format of incoming data
2669 * srcType - datatype of incoming data
2670 * src - source data pointer
2671 * swapBytes - perform byteswapping of incoming data?
2672 */
2673 static void
2674 extract_float_rgba(GLuint n, GLfloat rgba[][4],
2675 GLenum srcFormat, GLenum srcType, const GLvoid *src,
2676 GLboolean swapBytes)
2677 {
2678 GLint redIndex, greenIndex, blueIndex, alphaIndex;
2679 GLint stride;
2680 GLint rComp, bComp, gComp, aComp;
2681
2682 if (0)
2683 {
2684 int i;
2685 for (i = 0; i<n;i++) {
2686 rgba[i][0] = rgba[i][1] = rgba[i][2] = rgba[i][3] = 0;
2687 }
2688 return;
2689 }
2690
2691
2692 ASSERT(srcFormat == GL_RED ||
2693 srcFormat == GL_GREEN ||
2694 srcFormat == GL_BLUE ||
2695 srcFormat == GL_ALPHA ||
2696 srcFormat == GL_LUMINANCE ||
2697 srcFormat == GL_LUMINANCE_ALPHA ||
2698 srcFormat == GL_INTENSITY ||
2699 srcFormat == GL_RGB ||
2700 srcFormat == GL_BGR ||
2701 srcFormat == GL_RGBA ||
2702 srcFormat == GL_BGRA ||
2703 srcFormat == GL_ABGR_EXT);
2704
2705 ASSERT(srcType == GL_UNSIGNED_BYTE ||
2706 srcType == GL_BYTE ||
2707 srcType == GL_UNSIGNED_SHORT ||
2708 srcType == GL_SHORT ||
2709 srcType == GL_UNSIGNED_INT ||
2710 srcType == GL_INT ||
2711 srcType == GL_FLOAT ||
2712 srcType == GL_UNSIGNED_BYTE_3_3_2 ||
2713 srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
2714 srcType == GL_UNSIGNED_SHORT_5_6_5 ||
2715 srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
2716 srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
2717 srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
2718 srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
2719 srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
2720 srcType == GL_UNSIGNED_INT_8_8_8_8 ||
2721 srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
2722 srcType == GL_UNSIGNED_INT_10_10_10_2 ||
2723 srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
2724
2725 switch (srcFormat) {
2726 case GL_RED:
2727 redIndex = 0;
2728 greenIndex = blueIndex = alphaIndex = -1;
2729 stride = 1;
2730 break;
2731 case GL_GREEN:
2732 greenIndex = 0;
2733 redIndex = blueIndex = alphaIndex = -1;
2734 stride = 1;
2735 break;
2736 case GL_BLUE:
2737 blueIndex = 0;
2738 redIndex = greenIndex = alphaIndex = -1;
2739 stride = 1;
2740 break;
2741 case GL_ALPHA:
2742 redIndex = greenIndex = blueIndex = -1;
2743 alphaIndex = 0;
2744 stride = 1;
2745 break;
2746 case GL_LUMINANCE:
2747 redIndex = greenIndex = blueIndex = 0;
2748 alphaIndex = -1;
2749 stride = 1;
2750 break;
2751 case GL_LUMINANCE_ALPHA:
2752 redIndex = greenIndex = blueIndex = 0;
2753 alphaIndex = 1;
2754 stride = 2;
2755 break;
2756 case GL_INTENSITY:
2757 redIndex = 0;
2758 greenIndex = blueIndex = alphaIndex = -1;
2759 stride = 1;
2760 break;
2761 case GL_RGB:
2762 redIndex = 0;
2763 greenIndex = 1;
2764 blueIndex = 2;
2765 alphaIndex = -1;
2766 stride = 3;
2767 break;
2768 case GL_BGR:
2769 redIndex = 2;
2770 greenIndex = 1;
2771 blueIndex = 0;
2772 alphaIndex = -1;
2773 stride = 3;
2774 break;
2775 case GL_RGBA:
2776 redIndex = 0;
2777 greenIndex = 1;
2778 blueIndex = 2;
2779 alphaIndex = 3;
2780 rComp = 0;
2781 gComp = 1;
2782 bComp = 2;
2783 aComp = 3;
2784 stride = 4;
2785 break;
2786 case GL_BGRA:
2787 redIndex = 2;
2788 greenIndex = 1;
2789 blueIndex = 0;
2790 alphaIndex = 3;
2791 rComp = 2;
2792 gComp = 1;
2793 bComp = 0;
2794 aComp = 3;
2795 stride = 4;
2796 break;
2797 case GL_ABGR_EXT:
2798 redIndex = 3;
2799 greenIndex = 2;
2800 blueIndex = 1;
2801 alphaIndex = 0;
2802 rComp = 3;
2803 gComp = 2;
2804 bComp = 1;
2805 aComp = 0;
2806 stride = 4;
2807 break;
2808 default:
2809 gl_problem(NULL, "bad srcFormat in extract float data");
2810 return;
2811 }
2812
2813 assert(redIndex >= -1 && redIndex <= 4);
2814 assert(greenIndex >= -1 && greenIndex <= 4);
2815 assert(blueIndex >= -1 && blueIndex <= 4);
2816 assert(alphaIndex >= -1 && alphaIndex <= 4);
2817
2818 #define PROCESS(INDEX, CHANNEL, DEFAULT, TYPE, CONVERSION) \
2819 if ((INDEX) < 0) { \
2820 GLuint i; \
2821 for (i = 0; i < n; i++) { \
2822 rgba[i][CHANNEL] = DEFAULT; \
2823 } \
2824 } \
2825 else if (swapBytes) { \
2826 const TYPE *s = (const TYPE *) src; \
2827 GLuint i; \
2828 for (i = 0; i < n; i++) { \
2829 TYPE value = s[INDEX]; \
2830 if (sizeof(TYPE) == 2) { \
2831 SWAP2BYTE(value); \
2832 } \
2833 else if (sizeof(TYPE) == 4) { \
2834 SWAP4BYTE(value); \
2835 } \
2836 rgba[i][CHANNEL] = (GLfloat) CONVERSION(value); \
2837 s += stride; \
2838 } \
2839 } \
2840 else { \
2841 const TYPE *s = (const TYPE *) src; \
2842 GLuint i; \
2843 for (i = 0; i < n; i++) { \
2844 rgba[i][CHANNEL] = (GLfloat) CONVERSION(s[INDEX]); \
2845 s += stride; \
2846 } \
2847 }
2848
2849 switch (srcType) {
2850 case GL_UNSIGNED_BYTE:
2851 PROCESS(redIndex, RCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
2852 PROCESS(greenIndex, GCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
2853 PROCESS(blueIndex, BCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
2854 PROCESS(alphaIndex, ACOMP, 1.0F, GLubyte, UBYTE_TO_FLOAT);
2855 break;
2856 case GL_BYTE:
2857 PROCESS(redIndex, RCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
2858 PROCESS(greenIndex, GCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
2859 PROCESS(blueIndex, BCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
2860 PROCESS(alphaIndex, ACOMP, 1.0F, GLbyte, BYTE_TO_FLOAT);
2861 break;
2862 case GL_UNSIGNED_SHORT:
2863 PROCESS(redIndex, RCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
2864 PROCESS(greenIndex, GCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
2865 PROCESS(blueIndex, BCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
2866 PROCESS(alphaIndex, ACOMP, 1.0F, GLushort, USHORT_TO_FLOAT);
2867 break;
2868 case GL_SHORT:
2869 PROCESS(redIndex, RCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
2870 PROCESS(greenIndex, GCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
2871 PROCESS(blueIndex, BCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
2872 PROCESS(alphaIndex, ACOMP, 1.0F, GLshort, SHORT_TO_FLOAT);
2873 break;
2874 case GL_UNSIGNED_INT:
2875 PROCESS(redIndex, RCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
2876 PROCESS(greenIndex, GCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
2877 PROCESS(blueIndex, BCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
2878 PROCESS(alphaIndex, ACOMP, 1.0F, GLuint, UINT_TO_FLOAT);
2879 break;
2880 case GL_INT:
2881 PROCESS(redIndex, RCOMP, 0.0F, GLint, INT_TO_FLOAT);
2882 PROCESS(greenIndex, GCOMP, 0.0F, GLint, INT_TO_FLOAT);
2883 PROCESS(blueIndex, BCOMP, 0.0F, GLint, INT_TO_FLOAT);
2884 PROCESS(alphaIndex, ACOMP, 1.0F, GLint, INT_TO_FLOAT);
2885 break;
2886 case GL_FLOAT:
2887 PROCESS(redIndex, RCOMP, 0.0F, GLfloat, (GLfloat));
2888 PROCESS(greenIndex, GCOMP, 0.0F, GLfloat, (GLfloat));
2889 PROCESS(blueIndex, BCOMP, 0.0F, GLfloat, (GLfloat));
2890 PROCESS(alphaIndex, ACOMP, 1.0F, GLfloat, (GLfloat));
2891 break;
2892 case GL_UNSIGNED_BYTE_3_3_2:
2893 {
2894 const GLubyte *ubsrc = (const GLubyte *) src;
2895 GLuint i;
2896 for (i = 0; i < n; i ++) {
2897 GLubyte p = ubsrc[i];
2898 rgba[i][RCOMP] = ((p >> 5) ) * (1.0F / 7.0F);
2899 rgba[i][GCOMP] = ((p >> 2) & 0x7) * (1.0F / 7.0F);
2900 rgba[i][BCOMP] = ((p ) & 0x3) * (1.0F / 3.0F);
2901 rgba[i][ACOMP] = 1.0F;
2902 }
2903 }
2904 break;
2905 case GL_UNSIGNED_BYTE_2_3_3_REV:
2906 {
2907 const GLubyte *ubsrc = (const GLubyte *) src;
2908 GLuint i;
2909 for (i = 0; i < n; i ++) {
2910 GLubyte p = ubsrc[i];
2911 rgba[i][RCOMP] = ((p ) & 0x7) * (1.0F / 7.0F);
2912 rgba[i][GCOMP] = ((p >> 3) & 0x7) * (1.0F / 7.0F);
2913 rgba[i][BCOMP] = ((p >> 6) ) * (1.0F / 3.0F);
2914 rgba[i][ACOMP] = 1.0F;
2915 }
2916 }
2917 break;
2918 case GL_UNSIGNED_SHORT_5_6_5:
2919 if (swapBytes) {
2920 const GLushort *ussrc = (const GLushort *) src;
2921 GLuint i;
2922 for (i = 0; i < n; i ++) {
2923 GLushort p = ussrc[i];
2924 SWAP2BYTE(p);
2925 rgba[i][RCOMP] = ((p >> 11) ) * (1.0F / 31.0F);
2926 rgba[i][GCOMP] = ((p >> 5) & 0x3f) * (1.0F / 63.0F);
2927 rgba[i][BCOMP] = ((p ) & 0x1f) * (1.0F / 31.0F);
2928 rgba[i][ACOMP] = 1.0F;
2929 }
2930 }
2931 else {
2932 const GLushort *ussrc = (const GLushort *) src;
2933 GLuint i;
2934 for (i = 0; i < n; i ++) {
2935 GLushort p = ussrc[i];
2936 rgba[i][RCOMP] = ((p >> 11) ) * (1.0F / 31.0F);
2937 rgba[i][GCOMP] = ((p >> 5) & 0x3f) * (1.0F / 63.0F);
2938 rgba[i][BCOMP] = ((p ) & 0x1f) * (1.0F / 31.0F);
2939 rgba[i][ACOMP] = 1.0F;
2940 }
2941 }
2942 break;
2943 case GL_UNSIGNED_SHORT_5_6_5_REV:
2944 if (swapBytes) {
2945 const GLushort *ussrc = (const GLushort *) src;
2946 GLuint i;
2947 for (i = 0; i < n; i ++) {
2948 GLushort p = ussrc[i];
2949 SWAP2BYTE(p);
2950 rgba[i][RCOMP] = ((p ) & 0x1f) * (1.0F / 31.0F);
2951 rgba[i][GCOMP] = ((p >> 5) & 0x3f) * (1.0F / 63.0F);
2952 rgba[i][BCOMP] = ((p >> 11) ) * (1.0F / 31.0F);
2953 rgba[i][ACOMP] = 1.0F;
2954 }
2955 }
2956 else {
2957 const GLushort *ussrc = (const GLushort *) src;
2958 GLuint i;
2959 for (i = 0; i < n; i ++) {
2960 GLushort p = ussrc[i];
2961 rgba[i][RCOMP] = ((p ) & 0x1f) * (1.0F / 31.0F);
2962 rgba[i][GCOMP] = ((p >> 5) & 0x3f) * (1.0F / 63.0F);
2963 rgba[i][BCOMP] = ((p >> 11) ) * (1.0F / 31.0F);
2964 rgba[i][ACOMP] = 1.0F;
2965 }
2966 }
2967 break;
2968 case GL_UNSIGNED_SHORT_4_4_4_4:
2969 if (swapBytes) {
2970 const GLushort *ussrc = (const GLushort *) src;
2971 GLuint i;
2972 for (i = 0; i < n; i ++) {
2973 GLushort p = ussrc[i];
2974 SWAP2BYTE(p);
2975 rgba[i][rComp] = ((p >> 12) ) * (1.0F / 15.0F);
2976 rgba[i][gComp] = ((p >> 8) & 0xf) * (1.0F / 15.0F);
2977 rgba[i][bComp] = ((p >> 4) & 0xf) * (1.0F / 15.0F);
2978 rgba[i][aComp] = ((p ) & 0xf) * (1.0F / 15.0F);
2979 }
2980 }
2981 else {
2982 const GLushort *ussrc = (const GLushort *) src;
2983 GLuint i;
2984 for (i = 0; i < n; i ++) {
2985 GLushort p = ussrc[i];
2986 rgba[i][rComp] = ((p >> 12) ) * (1.0F / 15.0F);
2987 rgba[i][gComp] = ((p >> 8) & 0xf) * (1.0F / 15.0F);
2988 rgba[i][bComp] = ((p >> 4) & 0xf) * (1.0F / 15.0F);
2989 rgba[i][aComp] = ((p ) & 0xf) * (1.0F / 15.0F);
2990 }
2991 }
2992 break;
2993 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
2994 if (swapBytes) {
2995 const GLushort *ussrc = (const GLushort *) src;
2996 GLuint i;
2997 for (i = 0; i < n; i ++) {
2998 GLushort p = ussrc[i];
2999 SWAP2BYTE(p);
3000 rgba[i][rComp] = ((p ) & 0xf) * (1.0F / 15.0F);
3001 rgba[i][gComp] = ((p >> 4) & 0xf) * (1.0F / 15.0F);
3002 rgba[i][bComp] = ((p >> 8) & 0xf) * (1.0F / 15.0F);
3003 rgba[i][aComp] = ((p >> 12) ) * (1.0F / 15.0F);
3004 }
3005 }
3006 else {
3007 const GLushort *ussrc = (const GLushort *) src;
3008 GLuint i;
3009 for (i = 0; i < n; i ++) {
3010 GLushort p = ussrc[i];
3011 rgba[i][rComp] = ((p ) & 0xf) * (1.0F / 15.0F);
3012 rgba[i][gComp] = ((p >> 4) & 0xf) * (1.0F / 15.0F);
3013 rgba[i][bComp] = ((p >> 8) & 0xf) * (1.0F / 15.0F);
3014 rgba[i][aComp] = ((p >> 12) ) * (1.0F / 15.0F);
3015 }
3016 }
3017 break;
3018 case GL_UNSIGNED_SHORT_5_5_5_1:
3019 if (swapBytes) {
3020 const GLushort *ussrc = (const GLushort *) src;
3021 GLuint i;
3022 for (i = 0; i < n; i ++) {
3023 GLushort p = ussrc[i];
3024 SWAP2BYTE(p);
3025 rgba[i][rComp] = ((p >> 11) ) * (1.0F / 31.0F);
3026 rgba[i][gComp] = ((p >> 6) & 0x1f) * (1.0F / 31.0F);
3027 rgba[i][bComp] = ((p >> 1) & 0x1f) * (1.0F / 31.0F);
3028 rgba[i][aComp] = ((p ) & 0x1) * (1.0F / 1.0F);
3029 }
3030 }
3031 else {
3032 const GLushort *ussrc = (const GLushort *) src;
3033 GLuint i;
3034 for (i = 0; i < n; i ++) {
3035 GLushort p = ussrc[i];
3036 rgba[i][rComp] = ((p >> 11) ) * (1.0F / 31.0F);
3037 rgba[i][gComp] = ((p >> 6) & 0x1f) * (1.0F / 31.0F);
3038 rgba[i][bComp] = ((p >> 1) & 0x1f) * (1.0F / 31.0F);
3039 rgba[i][aComp] = ((p ) & 0x1) * (1.0F / 1.0F);
3040 }
3041 }
3042 break;
3043 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
3044 if (swapBytes) {
3045 const GLushort *ussrc = (const GLushort *) src;
3046 GLuint i;
3047 for (i = 0; i < n; i ++) {
3048 GLushort p = ussrc[i];
3049 SWAP2BYTE(p);
3050 rgba[i][rComp] = ((p ) & 0x1f) * (1.0F / 31.0F);
3051 rgba[i][gComp] = ((p >> 5) & 0x1f) * (1.0F / 31.0F);
3052 rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
3053 rgba[i][aComp] = ((p >> 15) ) * (1.0F / 1.0F);
3054 }
3055 }
3056 else {
3057 const GLushort *ussrc = (const GLushort *) src;
3058 GLuint i;
3059 for (i = 0; i < n; i ++) {
3060 GLushort p = ussrc[i];
3061 rgba[i][rComp] = ((p ) & 0x1f) * (1.0F / 31.0F);
3062 rgba[i][gComp] = ((p >> 5) & 0x1f) * (1.0F / 31.0F);
3063 rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
3064 rgba[i][aComp] = ((p >> 15) ) * (1.0F / 1.0F);
3065 }
3066 }
3067 break;
3068 case GL_UNSIGNED_INT_8_8_8_8:
3069 if (swapBytes) {
3070 const GLuint *uisrc = (const GLuint *) src;
3071 GLuint i;
3072 for (i = 0; i < n; i ++) {
3073 GLuint p = uisrc[i];
3074 rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p ) & 0xff);
3075 rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 8) & 0xff);
3076 rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
3077 rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24) );
3078 }
3079 }
3080 else {
3081 const GLuint *uisrc = (const GLuint *) src;
3082 GLuint i;
3083 for (i = 0; i < n; i ++) {
3084 GLuint p = uisrc[i];
3085 rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24) );
3086 rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
3087 rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 8) & 0xff);
3088 rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p ) & 0xff);
3089 }
3090 }
3091 break;
3092 case GL_UNSIGNED_INT_8_8_8_8_REV:
3093 if (swapBytes) {
3094 const GLuint *uisrc = (const GLuint *) src;
3095 GLuint i;
3096 for (i = 0; i < n; i ++) {
3097 GLuint p = uisrc[i];
3098 rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24) );
3099 rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
3100 rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 8) & 0xff);
3101 rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p ) & 0xff);
3102 }
3103 }
3104 else {
3105 const GLuint *uisrc = (const GLuint *) src;
3106 GLuint i;
3107 for (i = 0; i < n; i ++) {
3108 GLuint p = uisrc[i];
3109 rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p ) & 0xff);
3110 rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 8) & 0xff);
3111 rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
3112 rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24) );
3113 }
3114 }
3115 break;
3116 case GL_UNSIGNED_INT_10_10_10_2:
3117 if (swapBytes) {
3118 const GLuint *uisrc = (const GLuint *) src;
3119 GLuint i;
3120 for (i = 0; i < n; i ++) {
3121 GLuint p = uisrc[i];
3122 SWAP4BYTE(p);
3123 rgba[i][rComp] = ((p ) & 0x3 ) * (1.0F / 3.0F);
3124 rgba[i][gComp] = ((p >> 2) & 0x3ff) * (1.0F / 1023.0F);
3125 rgba[i][bComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
3126 rgba[i][aComp] = ((p >> 22) ) * (1.0F / 1023.0F);
3127 }
3128 }
3129 else {
3130 const GLuint *uisrc = (const GLuint *) src;
3131 GLuint i;
3132 for (i = 0; i < n; i ++) {
3133 GLuint p = uisrc[i];
3134 rgba[i][rComp] = ((p ) & 0x3 ) * (1.0F / 3.0F);
3135 rgba[i][gComp] = ((p >> 2) & 0x3ff) * (1.0F / 1023.0F);
3136 rgba[i][bComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
3137 rgba[i][aComp] = ((p >> 22) ) * (1.0F / 1023.0F);
3138 }
3139 }
3140 break;
3141 case GL_UNSIGNED_INT_2_10_10_10_REV:
3142 if (swapBytes) {
3143 const GLuint *uisrc = (const GLuint *) src;
3144 GLuint i;
3145 for (i = 0; i < n; i ++) {
3146 GLuint p = uisrc[i];
3147 SWAP4BYTE(p);
3148 rgba[i][rComp] = ((p ) & 0x3ff) * (1.0F / 1023.0F);
3149 rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
3150 rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
3151 rgba[i][aComp] = ((p >> 30) ) * (1.0F / 3.0F);
3152 }
3153 }
3154 else {
3155 const GLuint *uisrc = (const GLuint *) src;
3156 GLuint i;
3157 for (i = 0; i < n; i ++) {
3158 GLuint p = uisrc[i];
3159 rgba[i][rComp] = ((p ) & 0x3ff) * (1.0F / 1023.0F);
3160 rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
3161 rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
3162 rgba[i][aComp] = ((p >> 30) ) * (1.0F / 3.0F);
3163 }
3164 }
3165 break;
3166 default:
3167 gl_problem(NULL, "bad srcType in extract float data");
3168 break;
3169 }
3170 }
3171
3172
3173
3174 /*
3175 * Unpack a row of color image data from a client buffer according to
3176 * the pixel unpacking parameters. Apply any enabled pixel transfer
3177 * ops (PixelMap, scale/bias) if the applyTransferOps flag is enabled.
3178 * Return GLubyte values in the specified dest image format.
3179 * This is (or will be) used by glDrawPixels and glTexImage?D().
3180 * Input: ctx - the context
3181 * n - number of pixels in the span
3182 * dstFormat - format of destination color array
3183 * dest - the destination color array
3184 * srcFormat - source image format
3185 * srcType - source image datatype
3186 * source - source image pointer
3187 * unpacking - pixel unpacking parameters
3188 * applyTransferOps - apply scale/bias/lookup-table ops?
3189 *
3190 * XXX perhaps expand this to process whole images someday.
3191 */
3192 void
3193 _mesa_unpack_ubyte_color_span( const GLcontext *ctx,
3194 GLuint n, GLenum dstFormat, GLubyte dest[],
3195 GLenum srcFormat, GLenum srcType,
3196 const GLvoid *source,
3197 const struct gl_pixelstore_attrib *unpacking,
3198 GLboolean applyTransferOps )
3199 {
3200 ASSERT(dstFormat == GL_ALPHA ||
3201 dstFormat == GL_LUMINANCE ||
3202 dstFormat == GL_LUMINANCE_ALPHA ||
3203 dstFormat == GL_INTENSITY ||
3204 dstFormat == GL_RGB ||
3205 dstFormat == GL_RGBA ||
3206 dstFormat == GL_COLOR_INDEX);
3207
3208 ASSERT(srcFormat == GL_RED ||
3209 srcFormat == GL_GREEN ||
3210 srcFormat == GL_BLUE ||
3211 srcFormat == GL_ALPHA ||
3212 srcFormat == GL_LUMINANCE ||
3213 srcFormat == GL_LUMINANCE_ALPHA ||
3214 srcFormat == GL_INTENSITY ||
3215 srcFormat == GL_RGB ||
3216 srcFormat == GL_BGR ||
3217 srcFormat == GL_RGBA ||
3218 srcFormat == GL_BGRA ||
3219 srcFormat == GL_ABGR_EXT ||
3220 srcFormat == GL_COLOR_INDEX);
3221
3222 ASSERT(srcType == GL_BITMAP ||
3223 srcType == GL_UNSIGNED_BYTE ||
3224 srcType == GL_BYTE ||
3225 srcType == GL_UNSIGNED_SHORT ||
3226 srcType == GL_SHORT ||
3227 srcType == GL_UNSIGNED_INT ||
3228 srcType == GL_INT ||
3229 srcType == GL_FLOAT ||
3230 srcType == GL_UNSIGNED_BYTE_3_3_2 ||
3231 srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
3232 srcType == GL_UNSIGNED_SHORT_5_6_5 ||
3233 srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
3234 srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
3235 srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
3236 srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
3237 srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
3238 srcType == GL_UNSIGNED_INT_8_8_8_8 ||
3239 srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
3240 srcType == GL_UNSIGNED_INT_10_10_10_2 ||
3241 srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
3242
3243 /* this is intended for RGBA mode */
3244 ASSERT(ctx->Visual->RGBAflag);
3245
3246 applyTransferOps &= (ctx->Pixel.ScaleOrBiasRGBA ||
3247 ctx->Pixel.MapColorFlag);
3248
3249 /* Try simple cases first */
3250 if (!applyTransferOps && srcType == GL_UNSIGNED_BYTE) {
3251 if (dstFormat == GL_RGBA) {
3252 if (srcFormat == GL_RGBA) {
3253 MEMCPY( dest, source, n * 4 * sizeof(GLubyte) );
3254 return;
3255 }
3256 else if (srcFormat == GL_RGB) {
3257 GLuint i;
3258 const GLubyte *src = (const GLubyte *) source;
3259 GLubyte *dst = dest;
3260 for (i = 0; i < n; i++) {
3261 dst[0] = src[0];
3262 dst[1] = src[1];
3263 dst[2] = src[2];
3264 dst[3] = 255;
3265 src += 3;
3266 dst += 4;
3267 }
3268 return;
3269 }
3270 }
3271 else if (dstFormat == GL_RGB) {
3272 if (srcFormat == GL_RGB) {
3273 MEMCPY( dest, source, n * 3 * sizeof(GLubyte) );
3274 return;
3275 }
3276 else if (srcFormat == GL_RGBA) {
3277 GLuint i;
3278 const GLubyte *src = (const GLubyte *) source;
3279 GLubyte *dst = dest;
3280 for (i = 0; i < n; i++) {
3281 dst[0] = src[0];
3282 dst[1] = src[1];
3283 dst[2] = src[2];
3284 src += 4;
3285 dst += 3;
3286 }
3287 return;
3288 }
3289 }
3290 else if (dstFormat == srcFormat) {
3291 GLint comps = gl_components_in_format(srcFormat);
3292 assert(comps > 0);
3293 MEMCPY( dest, source, n * comps * sizeof(GLubyte) );
3294 return;
3295 }
3296 }
3297
3298
3299 {
3300 /* general solution */
3301 GLfloat rgba[MAX_WIDTH][4];
3302 GLint dstComponents;
3303 GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
3304 GLint dstLuminanceIndex, dstIntensityIndex;
3305
3306 dstComponents = gl_components_in_format( dstFormat );
3307 /* source & dest image formats should have been error checked by now */
3308 assert(dstComponents > 0);
3309
3310 /*
3311 * Extract image data and convert to RGBA floats
3312 */
3313 assert(n <= MAX_WIDTH);
3314 if (srcFormat == GL_COLOR_INDEX) {
3315 GLuint indexes[MAX_WIDTH];
3316 extract_uint_indexes(n, indexes, srcFormat, srcType, source,
3317 unpacking);
3318
3319 /* shift and offset indexes */
3320 gl_shift_and_offset_ci(ctx, n, indexes);
3321
3322 if (dstFormat == GL_COLOR_INDEX) {
3323 if (applyTransferOps) {
3324 if (ctx->Pixel.MapColorFlag) {
3325 /* Apply lookup table */
3326 gl_map_ci(ctx, n, indexes);
3327 }
3328
3329 if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset) {
3330
3331 }
3332 }
3333
3334 /* convert to GLubyte and return */
3335 {
3336 GLuint i;
3337 for (i = 0; i < n; i++) {
3338 dest[i] = (GLubyte) (indexes[i] & 0xff);
3339 }
3340 }
3341 }
3342 else {
3343 /* Convert indexes to RGBA */
3344 gl_map_ci_to_rgba_float(ctx, n, indexes, rgba);
3345 }
3346 }
3347 else {
3348 extract_float_rgba(n, rgba, srcFormat, srcType, source,
3349 unpacking->SwapBytes);
3350
3351 if (applyTransferOps) {
3352 /* scale and bias colors */
3353 gl_scale_and_bias_rgba_float(ctx, n, rgba);
3354
3355 /* color table lookup */
3356 if (ctx->Pixel.MapColorFlag) {
3357 gl_map_rgba_float(ctx, n, rgba);
3358 }
3359 }
3360 }
3361
3362
3363 /*
3364 * XXX This is where more color table lookups, convolution,
3365 * histograms, minmax, color matrix, etc would take place if
3366 * implemented.
3367 * See figure 3.7 in the OpenGL 1.2 specification for more info.
3368 */
3369
3370
3371 /* clamp to [0,1] */
3372 {
3373 GLuint i;
3374 for (i = 0; i < n; i++) {
3375 rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F);
3376 rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F);
3377 rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F);
3378 rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F);
3379 }
3380 }
3381
3382 /* Now determine which color channels we need to produce.
3383 * And determine the dest index (offset) within each color tuple.
3384 */
3385 switch (dstFormat) {
3386 case GL_ALPHA:
3387 dstAlphaIndex = 0;
3388 dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
3389 dstLuminanceIndex = dstIntensityIndex = -1;
3390 break;
3391 case GL_LUMINANCE:
3392 dstLuminanceIndex = 0;
3393 dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
3394 dstIntensityIndex = -1;
3395 break;
3396 case GL_LUMINANCE_ALPHA:
3397 dstLuminanceIndex = 0;
3398 dstAlphaIndex = 1;
3399 dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
3400 dstIntensityIndex = -1;
3401 break;
3402 case GL_INTENSITY:
3403 dstIntensityIndex = 0;
3404 dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
3405 dstLuminanceIndex = -1;
3406 break;
3407 case GL_RGB:
3408 dstRedIndex = 0;
3409 dstGreenIndex = 1;
3410 dstBlueIndex = 2;
3411 dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
3412 break;
3413 case GL_RGBA:
3414 dstRedIndex = 0;
3415 dstGreenIndex = 1;
3416 dstBlueIndex = 2;
3417 dstAlphaIndex = 3;
3418 dstLuminanceIndex = dstIntensityIndex = -1;
3419 break;
3420 case GL_COLOR_INDEX:
3421 assert(0);
3422 break;
3423 default:
3424 gl_problem(ctx, "bad dstFormat in _mesa_unpack_ubyte_span()");
3425 }
3426
3427
3428 /* Now return the GLubyte data in the requested dstFormat */
3429 if (dstRedIndex >= 0) {
3430 GLubyte *dst = dest;
3431 GLuint i;
3432 for (i = 0; i < n; i++) {
3433 dst[dstRedIndex] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
3434 dst += dstComponents;
3435 }
3436 }
3437
3438 if (dstGreenIndex >= 0) {
3439 GLubyte *dst = dest;
3440 GLuint i;
3441 for (i = 0; i < n; i++) {
3442 dst[dstGreenIndex] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
3443 dst += dstComponents;
3444 }
3445 }
3446
3447 if (dstBlueIndex >= 0) {
3448 GLubyte *dst = dest;
3449 GLuint i;
3450 for (i = 0; i < n; i++) {
3451 dst[dstBlueIndex] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
3452 dst += dstComponents;
3453 }
3454 }
3455
3456 if (dstAlphaIndex >= 0) {
3457 GLubyte *dst = dest;
3458 GLuint i;
3459 for (i = 0; i < n; i++) {
3460 dst[dstAlphaIndex] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
3461 dst += dstComponents;
3462 }
3463 }
3464
3465 if (dstIntensityIndex >= 0) {
3466 GLubyte *dst = dest;
3467 GLuint i;
3468 assert(dstIntensityIndex == 0);
3469 assert(dstComponents == 1);
3470 for (i = 0; i < n; i++) {
3471 /* Intensity comes from red channel */
3472 dst[i] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
3473 }
3474 }
3475
3476 if (dstLuminanceIndex >= 0) {
3477 GLubyte *dst = dest;
3478 GLuint i;
3479 assert(dstLuminanceIndex == 0);
3480 for (i = 0; i < n; i++) {
3481 /* Luminance comes from red channel */
3482 dst[0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
3483 dst += dstComponents;
3484 }
3485 }
3486 }
3487 }
3488
3489
3490
3491 /*
3492 * Unpack a row of color index data from a client buffer according to
3493 * the pixel unpacking parameters. Apply pixel transfer ops if enabled
3494 * and applyTransferOps is true.
3495 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
3496 *
3497 * Args: ctx - the context
3498 * n - number of pixels
3499 * dstType - destination datatype
3500 * dest - destination array
3501 * srcType - source pixel type
3502 * source - source data pointer
3503 * unpacking - pixel unpacking parameters
3504 * applyTransferOps - apply offset/bias/lookup ops?
3505 */
3506 void
3507 _mesa_unpack_index_span( const GLcontext *ctx, GLuint n,
3508 GLenum dstType, GLvoid *dest,
3509 GLenum srcType, const GLvoid *source,
3510 const struct gl_pixelstore_attrib *unpacking,
3511 GLboolean applyTransferOps )
3512 {
3513 ASSERT(srcType == GL_BITMAP ||
3514 srcType == GL_UNSIGNED_BYTE ||
3515 srcType == GL_BYTE ||
3516 srcType == GL_UNSIGNED_SHORT ||
3517 srcType == GL_SHORT ||
3518 srcType == GL_UNSIGNED_INT ||
3519 srcType == GL_INT ||
3520 srcType == GL_FLOAT);
3521
3522 ASSERT(dstType == GL_UNSIGNED_BYTE ||
3523 dstType == GL_UNSIGNED_SHORT ||
3524 dstType == GL_UNSIGNED_INT);
3525
3526 applyTransferOps &= (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset || ctx->Pixel.MapColorFlag);
3527
3528 /*
3529 * Try simple cases first
3530 */
3531 if (!applyTransferOps && srcType == GL_UNSIGNED_BYTE
3532 && dstType == GL_UNSIGNED_BYTE) {
3533 MEMCPY(dest, source, n * sizeof(GLubyte));
3534 }
3535 else if (!applyTransferOps && srcType == GL_UNSIGNED_INT
3536 && dstType == GL_UNSIGNED_INT && !unpacking->SwapBytes) {
3537 MEMCPY(dest, source, n * sizeof(GLuint));
3538 }
3539 else {
3540 /*
3541 * general solution
3542 */
3543 GLuint indexes[MAX_WIDTH];
3544 assert(n <= MAX_WIDTH);
3545
3546 extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
3547 unpacking);
3548
3549 if (applyTransferOps) {
3550 if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset) {
3551 /* shift and offset indexes */
3552 gl_shift_and_offset_ci(ctx, n, indexes);
3553 }
3554
3555 if (ctx->Pixel.MapColorFlag) {
3556 /* Apply lookup table */
3557 gl_map_ci(ctx, n, indexes);
3558 }
3559 }
3560
3561 /* convert to dest type */
3562 switch (dstType) {
3563 case GL_UNSIGNED_BYTE:
3564 {
3565 GLubyte *dst = (GLubyte *) dest;
3566 GLuint i;
3567 for (i = 0; i < n; i++) {
3568 dst[i] = (GLubyte) (indexes[i] & 0xff);
3569 }
3570 }
3571 break;
3572 case GL_UNSIGNED_SHORT:
3573 {
3574 GLuint *dst = (GLuint *) dest;
3575 GLuint i;
3576 for (i = 0; i < n; i++) {
3577 dst[i] = (GLushort) (indexes[i] & 0xffff);
3578 }
3579 }
3580 break;
3581 case GL_UNSIGNED_INT:
3582 MEMCPY(dest, indexes, n * sizeof(GLuint));
3583 break;
3584 default:
3585 gl_problem(ctx, "bad dstType in _mesa_unpack_index_span");
3586 }
3587 }
3588 }
3589
3590
3591 /*
3592 * Unpack image data. Apply byteswapping, byte flipping (bitmap).
3593 * Return all image data in a contiguous block.
3594 */
3595 void *
3596 _mesa_unpack_image( GLsizei width, GLsizei height, GLsizei depth,
3597 GLenum format, GLenum type, const GLvoid *pixels,
3598 const struct gl_pixelstore_attrib *unpack )
3599 {
3600 GLint bytesPerRow, compsPerRow;
3601 GLboolean flipBytes, swap2, swap4;
3602
3603 if (!pixels)
3604 return NULL; /* not necessarily an error */
3605
3606 if (width <= 0 || height <= 0 || depth <= 0)
3607 return NULL; /* generate error later */
3608
3609 if (format == GL_BITMAP) {
3610 bytesPerRow = (width + 7) >> 3;
3611 flipBytes = !unpack->LsbFirst;
3612 swap2 = swap4 = GL_FALSE;
3613 compsPerRow = 0;
3614 }
3615 else {
3616 const GLint bytesPerPixel = gl_bytes_per_pixel(format, type);
3617 const GLint components = gl_components_in_format(format);
3618 GLint bytesPerComp;
3619 if (bytesPerPixel <= 0 || components <= 0)
3620 return NULL; /* bad format or type. generate error later */
3621 bytesPerRow = bytesPerPixel * width;
3622 bytesPerComp = bytesPerPixel / components;
3623 flipBytes = GL_FALSE;
3624 swap2 = (bytesPerComp == 2) && unpack->SwapBytes;
3625 swap4 = (bytesPerComp == 4) && unpack->SwapBytes;
3626 compsPerRow = components * width;
3627 assert(compsPerRow >= width);
3628 }
3629
3630 {
3631 GLubyte *destBuffer = MALLOC(bytesPerRow * height * depth);
3632 GLubyte *dst;
3633 GLint img, row;
3634 if (!destBuffer)
3635 return NULL; /* generate GL_OUT_OF_MEMORY later */
3636
3637 dst = destBuffer;
3638 for (img = 0; img < depth; img++) {
3639 for (row = 0; row < height; row++) {
3640 const GLvoid *src = gl_pixel_addr_in_image(unpack, pixels,
3641 width, height, format, type, img, row, 0);
3642 MEMCPY(dst, src, bytesPerRow);
3643 /* byte flipping/swapping */
3644 if (flipBytes) {
3645 gl_flip_bytes((GLubyte *) dst, bytesPerRow);
3646 }
3647 else if (swap2) {
3648 gl_swap2((GLushort*) dst, compsPerRow);
3649 }
3650 else if (swap4) {
3651 gl_swap4((GLuint*) dst, compsPerRow);
3652 }
3653 dst += bytesPerRow;
3654 }
3655 }
3656 return destBuffer;
3657 }
3658 }