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