fixed stupid error in GL_RGA case in gl_pack_rgba_span()
[mesa.git] / src / mesa / main / image.c
1 /* $Id: image.c,v 1.10 1999/10/26 09:21:04 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 return 1;
214 case GL_LUMINANCE_ALPHA:
215 return 2;
216 case GL_RGB:
217 return 3;
218 case GL_RGBA:
219 return 4;
220 case GL_BGR:
221 return 3;
222 case GL_BGRA:
223 return 4;
224 case GL_ABGR_EXT:
225 return 4;
226 default:
227 return -1;
228 }
229 }
230
231
232 /*
233 * Return bytes per pixel for given format and type
234 * Return -1 if bad format or type.
235 */
236 GLint gl_bytes_per_pixel( GLenum format, GLenum type )
237 {
238 GLint comps = gl_components_in_format( format );
239 if (comps < 0)
240 return -1;
241
242 switch (type) {
243 case GL_BITMAP:
244 return 0; /* special case */
245 case GL_BYTE:
246 case GL_UNSIGNED_BYTE:
247 return comps * sizeof(GLubyte);
248 case GL_SHORT:
249 case GL_UNSIGNED_SHORT:
250 return comps * sizeof(GLshort);
251 case GL_INT:
252 case GL_UNSIGNED_INT:
253 return comps * sizeof(GLint);
254 case GL_FLOAT:
255 return comps * sizeof(GLfloat);
256 case GL_UNSIGNED_BYTE_3_3_2:
257 case GL_UNSIGNED_BYTE_2_3_3_REV:
258 if (format == GL_RGB || format == GL_BGR)
259 return sizeof(GLubyte);
260 else
261 return -1; /* error */
262 case GL_UNSIGNED_SHORT_5_6_5:
263 case GL_UNSIGNED_SHORT_5_6_5_REV:
264 if (format == GL_RGB || format == GL_BGR)
265 return sizeof(GLshort);
266 else
267 return -1; /* error */
268 case GL_UNSIGNED_SHORT_4_4_4_4:
269 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
270 case GL_UNSIGNED_SHORT_5_5_5_1:
271 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
272 if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT)
273 return sizeof(GLushort);
274 else
275 return -1;
276 case GL_UNSIGNED_INT_8_8_8_8:
277 case GL_UNSIGNED_INT_8_8_8_8_REV:
278 case GL_UNSIGNED_INT_10_10_10_2:
279 case GL_UNSIGNED_INT_2_10_10_10_REV:
280 if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT)
281 return sizeof(GLuint);
282 else
283 return -1;
284 default:
285 return -1;
286 }
287 }
288
289
290 /*
291 * Test if the given pixel format and type are legal.
292 * Return GL_TRUE for legal, GL_FALSE for illegal.
293 */
294 GLboolean gl_is_legal_format_and_type( GLenum format, GLenum type )
295 {
296 switch (format) {
297 case GL_COLOR_INDEX:
298 case GL_STENCIL_INDEX:
299 switch (type) {
300 case GL_BITMAP:
301 case GL_BYTE:
302 case GL_UNSIGNED_BYTE:
303 case GL_SHORT:
304 case GL_UNSIGNED_SHORT:
305 case GL_INT:
306 case GL_UNSIGNED_INT:
307 case GL_FLOAT:
308 return GL_TRUE;
309 default:
310 return GL_FALSE;
311 }
312 case GL_RED:
313 case GL_GREEN:
314 case GL_BLUE:
315 case GL_ALPHA:
316 case GL_LUMINANCE:
317 case GL_LUMINANCE_ALPHA:
318 case GL_DEPTH_COMPONENT:
319 case GL_BGR:
320 switch (type) {
321 case GL_BYTE:
322 case GL_UNSIGNED_BYTE:
323 case GL_SHORT:
324 case GL_UNSIGNED_SHORT:
325 case GL_INT:
326 case GL_UNSIGNED_INT:
327 case GL_FLOAT:
328 return GL_TRUE;
329 default:
330 return GL_FALSE;
331 }
332 case GL_RGB:
333 switch (type) {
334 case GL_BYTE:
335 case GL_UNSIGNED_BYTE:
336 case GL_SHORT:
337 case GL_UNSIGNED_SHORT:
338 case GL_INT:
339 case GL_UNSIGNED_INT:
340 case GL_FLOAT:
341 case GL_UNSIGNED_BYTE_3_3_2:
342 case GL_UNSIGNED_BYTE_2_3_3_REV:
343 case GL_UNSIGNED_SHORT_5_6_5:
344 case GL_UNSIGNED_SHORT_5_6_5_REV:
345 return GL_TRUE;
346 default:
347 return GL_FALSE;
348 }
349 case GL_RGBA:
350 case GL_BGRA:
351 case GL_ABGR_EXT:
352 switch (type) {
353 case GL_BYTE:
354 case GL_UNSIGNED_BYTE:
355 case GL_SHORT:
356 case GL_UNSIGNED_SHORT:
357 case GL_INT:
358 case GL_UNSIGNED_INT:
359 case GL_FLOAT:
360 case GL_UNSIGNED_SHORT_4_4_4_4:
361 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
362 case GL_UNSIGNED_SHORT_5_5_5_1:
363 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
364 case GL_UNSIGNED_INT_8_8_8_8:
365 case GL_UNSIGNED_INT_8_8_8_8_REV:
366 case GL_UNSIGNED_INT_10_10_10_2:
367 case GL_UNSIGNED_INT_2_10_10_10_REV:
368 return GL_TRUE;
369 default:
370 return GL_FALSE;
371 }
372 default:
373 ; /* fall-through */
374 }
375 return GL_FALSE;
376 }
377
378
379
380 /*
381 * Return the address of a pixel in an image (actually a volume).
382 * Pixel unpacking/packing parameters are observed according to 'packing'.
383 * Input: image - start of image data
384 * width, height - size of image
385 * format - image format
386 * type - pixel component type
387 * packing - the pixelstore attributes
388 * img - which image in the volume (0 for 1D or 2D images)
389 * row, column - location of pixel in the image
390 * Return: address of pixel at (image,row,column) in image or NULL if error.
391 */
392 GLvoid *gl_pixel_addr_in_image( const struct gl_pixelstore_attrib *packing,
393 const GLvoid *image, GLsizei width,
394 GLsizei height, GLenum format, GLenum type,
395 GLint img, GLint row, GLint column )
396 {
397 GLint alignment; /* 1, 2 or 4 */
398 GLint pixels_per_row;
399 GLint rows_per_image;
400 GLint skiprows;
401 GLint skippixels;
402 GLint skipimages; /* for 3-D volume images */
403 GLubyte *pixel_addr;
404
405 alignment = packing->Alignment;
406 if (packing->RowLength > 0) {
407 pixels_per_row = packing->RowLength;
408 }
409 else {
410 pixels_per_row = width;
411 }
412 if (packing->ImageHeight > 0) {
413 rows_per_image = packing->ImageHeight;
414 }
415 else {
416 rows_per_image = height;
417 }
418 skiprows = packing->SkipRows;
419 skippixels = packing->SkipPixels;
420 skipimages = packing->SkipImages;
421
422 if (type==GL_BITMAP) {
423 /* BITMAP data */
424 GLint comp_per_pixel; /* components per pixel */
425 GLint bytes_per_comp; /* bytes per component */
426 GLint bytes_per_row;
427 GLint bytes_per_image;
428
429 /* Compute bytes per component */
430 bytes_per_comp = gl_sizeof_packed_type( type );
431 if (bytes_per_comp<0) {
432 return NULL;
433 }
434
435 /* Compute number of components per pixel */
436 comp_per_pixel = gl_components_in_format( format );
437 if (comp_per_pixel<0 && type != GL_BITMAP) {
438 return NULL;
439 }
440
441 bytes_per_row = alignment
442 * CEILING( comp_per_pixel*pixels_per_row, 8*alignment );
443
444 bytes_per_image = bytes_per_row * rows_per_image;
445
446 pixel_addr = (GLubyte *) image
447 + (skipimages + img) * bytes_per_image
448 + (skiprows + row) * bytes_per_row
449 + (skippixels + column) / 8;
450 }
451 else {
452 /* Non-BITMAP data */
453 GLint bytes_per_pixel, bytes_per_row, remainder, bytes_per_image;
454
455 bytes_per_pixel = gl_bytes_per_pixel( format, type );
456
457 /* The pixel type and format should have been error checked earlier */
458 assert(bytes_per_pixel > 0);
459
460 bytes_per_row = pixels_per_row * bytes_per_pixel;
461 remainder = bytes_per_row % alignment;
462 if (remainder > 0)
463 bytes_per_row += (alignment - remainder);
464
465 ASSERT(bytes_per_row % alignment == 0);
466
467 bytes_per_image = bytes_per_row * rows_per_image;
468
469 /* compute final pixel address */
470 pixel_addr = (GLubyte *) image
471 + (skipimages + img) * bytes_per_image
472 + (skiprows + row) * bytes_per_row
473 + (skippixels + column) * bytes_per_pixel;
474 }
475
476 return (GLvoid *) pixel_addr;
477 }
478
479
480
481 /*
482 * Allocate a new gl_image. All fields are initialized to zero.
483 */
484 static struct gl_image *alloc_image( void )
485 {
486 return CALLOC_STRUCT(gl_image);
487 }
488
489
490
491 /*
492 * Allocate a new gl_image with the error flag set.
493 */
494 static struct gl_image *alloc_error_image( GLint width, GLint height,
495 GLint depth, GLenum format,
496 GLenum type )
497 {
498 struct gl_image *image = alloc_image();
499 if (image) {
500 image->Width = width;
501 image->Height = height;
502 image->Depth = depth;
503 image->Format = format;
504 image->Type = type;
505 image->ErrorFlag = GL_TRUE;
506 }
507 return image;
508 }
509
510
511
512 /*
513 * Free a gl_image.
514 */
515 void gl_free_image( struct gl_image *image )
516 {
517 if (image->Data) {
518 FREE(image->Data);
519 }
520 FREE(image);
521 }
522
523
524
525 /*
526 * Do error checking on an image. If there's an error, register it and
527 * return GL_TRUE, else return GL_FALSE.
528 */
529 GLboolean gl_image_error_test( GLcontext *ctx, const struct gl_image *image,
530 const char *msg )
531 {
532 if (!image) {
533 gl_error( ctx, GL_OUT_OF_MEMORY, msg );
534 return GL_TRUE;
535 }
536 if (image->Width <= 0 || image->Height <= 0 || image->Depth <= 0) {
537 gl_error( ctx, GL_INVALID_VALUE, msg );
538 return GL_TRUE;
539 }
540 else if (!gl_is_legal_format_and_type(image->Format, image->Type)) {
541 return GL_TRUE;
542 }
543 else {
544 return GL_FALSE;
545 }
546 }
547
548
549
550 /*
551 * Unpack a depth-buffer image storing values as GLshort, GLuint, or GLfloats.
552 * Input: type - datatype of src depth image
553 * Return pointer to a new gl_image structure.
554 *
555 * Notes: if the source image type is GLushort then the gl_image will
556 * also store GLushorts. If the src image type is GLuint then the gl_image
557 * will also store GLuints. For all other src image types the gl_image
558 * will store GLfloats. The integer cases can later be optimized.
559 */
560 static struct gl_image *
561 unpack_depth_image( GLcontext *ctx, GLenum type, GLint width, GLint height,
562 const GLvoid *pixels,
563 const struct gl_pixelstore_attrib *packing)
564
565 {
566 struct gl_image *image;
567 GLfloat *fDst;
568 GLushort *sDst;
569 GLuint *iDst;
570 GLint i, j;
571 GLboolean errorType;
572
573 errorType = type != GL_BYTE &&
574 type != GL_UNSIGNED_BYTE &&
575 type != GL_SHORT &&
576 type != GL_UNSIGNED_SHORT &&
577 type != GL_INT &&
578 type != GL_UNSIGNED_INT &&
579 type != GL_FLOAT;
580
581 image = alloc_image();
582 if (image) {
583 image->Width = width;
584 image->Height = height;
585 image->Depth = 1;
586 image->Components = 1;
587 image->Format = GL_DEPTH_COMPONENT;
588 if (errorType) {
589 image->Type = type;
590 image->Data = NULL;
591 }
592 if (type==GL_UNSIGNED_SHORT) {
593 image->Type = GL_UNSIGNED_SHORT;
594 image->Data = MALLOC( width * height * sizeof(GLushort));
595 }
596 else if (type==GL_UNSIGNED_INT) {
597 image->Type = GL_UNSIGNED_INT;
598 image->Data = MALLOC( width * height * sizeof(GLuint));
599 }
600 else {
601 image->Type = GL_FLOAT;
602 image->Data = MALLOC( width * height * sizeof(GLfloat));
603 }
604 image->RefCount = 0;
605 if (!image->Data)
606 return image;
607 }
608 else {
609 return NULL;
610 }
611
612 if (errorType)
613 return image;
614
615 fDst = (GLfloat *) image->Data;
616 sDst = (GLushort *) image->Data;
617 iDst = (GLuint *) image->Data;
618
619 for (i=0;i<height;i++) {
620 GLvoid *src = gl_pixel_addr_in_image( packing, pixels,
621 width, height,
622 GL_DEPTH_COMPONENT, type,
623 0, i, 0 );
624 if (!src) {
625 return image;
626 }
627
628 switch (type) {
629 case GL_BYTE:
630 assert(image->Type == GL_FLOAT);
631 for (j=0; j<width; j++) {
632 *fDst++ = BYTE_TO_FLOAT(((GLbyte*)src)[j]);
633 }
634 break;
635 case GL_UNSIGNED_BYTE:
636 assert(image->Type == GL_FLOAT);
637 for (j=0; j<width; j++) {
638 *fDst++ = UBYTE_TO_FLOAT(((GLubyte*)src)[j]);
639 }
640 break;
641 case GL_UNSIGNED_SHORT:
642 assert(image->Type == GL_UNSIGNED_SHORT);
643 MEMCPY( sDst, src, width * sizeof(GLushort) );
644 if (packing->SwapBytes) {
645 gl_swap2( sDst, width );
646 }
647 sDst += width;
648 break;
649 case GL_SHORT:
650 assert(image->Type == GL_FLOAT);
651 if (packing->SwapBytes) {
652 for (j=0;j<width;j++) {
653 GLshort value = ((GLshort*)src)[j];
654 value = ((value >> 8) & 0xff) | ((value&0xff) << 8);
655 *fDst++ = SHORT_TO_FLOAT(value);
656 }
657 }
658 else {
659 for (j=0;j<width;j++) {
660 *fDst++ = SHORT_TO_FLOAT(((GLshort*)src)[j]);
661 }
662 }
663 break;
664 case GL_INT:
665 assert(image->Type == GL_FLOAT);
666 if (packing->SwapBytes) {
667 for (j=0;j<width;j++) {
668 GLint value = ((GLint*)src)[j];
669 value = ((value >> 24) & 0x000000ff) |
670 ((value >> 8) & 0x0000ff00) |
671 ((value << 8) & 0x00ff0000) |
672 ((value << 24) & 0xff000000);
673 *fDst++ = INT_TO_FLOAT(value);
674 }
675 }
676 else {
677 for (j=0;j<width;j++) {
678 *fDst++ = INT_TO_FLOAT(((GLint*)src)[j]);
679 }
680 }
681 iDst += width;
682 break;
683 case GL_UNSIGNED_INT:
684 assert(image->Type == GL_UNSIGNED_INT);
685 MEMCPY( iDst, src, width * sizeof(GLuint) );
686 if (packing->SwapBytes) {
687 gl_swap4( iDst, width );
688 }
689 iDst += width;
690 break;
691 case GL_FLOAT:
692 assert(image->Type == GL_FLOAT);
693 MEMCPY( fDst, src, width * sizeof(GLfloat) );
694 if (packing->SwapBytes) {
695 gl_swap4( (GLuint*) fDst, width );
696 }
697 fDst += width;
698 break;
699 default:
700 gl_problem(ctx, "unpack_depth_image type" );
701 return image;
702 }
703 }
704
705 return image;
706 }
707
708
709
710 /*
711 * Unpack a stencil image. Store as GLubytes in a gl_image structure.
712 * Return: pointer to new gl_image structure.
713 */
714 static struct gl_image *
715 unpack_stencil_image( GLcontext *ctx, GLenum type, GLint width, GLint height,
716 const GLvoid *pixels,
717 const struct gl_pixelstore_attrib *packing )
718 {
719 struct gl_image *image;
720 GLubyte *dst;
721 GLint i, j;
722 GLboolean errorType;
723
724 assert(sizeof(GLstencil) == sizeof(GLubyte));
725
726 errorType = type != GL_BYTE &&
727 type != GL_UNSIGNED_BYTE &&
728 type != GL_SHORT &&
729 type != GL_UNSIGNED_SHORT &&
730 type != GL_INT &&
731 type != GL_UNSIGNED_INT &&
732 type != GL_FLOAT &&
733 type != GL_BITMAP;
734
735 image = alloc_image();
736 if (image) {
737 image->Width = width;
738 image->Height = height;
739 image->Depth = 1;
740 image->Components = 1;
741 image->Format = GL_STENCIL_INDEX;
742 if (errorType) {
743 image->Type = type;
744 image->Data = NULL;
745 }
746 else {
747 image->Type = GL_UNSIGNED_BYTE;
748 image->Data = MALLOC( width * height * sizeof(GLubyte));
749 }
750 image->RefCount = 0;
751 if (!image->Data)
752 return image;
753 }
754 else {
755 return NULL;
756 }
757
758 if (errorType)
759 return image; /* error will be generated later */
760
761 dst = (GLubyte *) image->Data;
762
763 for (i=0;i<height;i++) {
764 GLvoid *src = gl_pixel_addr_in_image( packing, pixels,
765 width, height,
766 GL_STENCIL_INDEX, type,
767 0, i, 0 );
768 if (!src) {
769 return image;
770 }
771
772 switch (type) {
773 case GL_UNSIGNED_BYTE:
774 case GL_BYTE:
775 MEMCPY( dst, src, width * sizeof(GLubyte) );
776 dst += width * sizeof(GLubyte);
777 break;
778 case GL_UNSIGNED_SHORT:
779 case GL_SHORT:
780 if (packing->SwapBytes) {
781 /* grab upper byte */
782 for (j=0; j < width; j++) {
783 *dst++ = (((GLushort*)src)[j] & 0xff00) >> 8;
784 }
785 }
786 else {
787 for (j=0; j < width; j++) {
788 *dst++ = (((GLushort*)src)[j]) & 0xff;
789 }
790 }
791 break;
792 case GL_INT:
793 if (packing->SwapBytes) {
794 /* grab upper byte */
795 for (j=0; j < width; j++) {
796 *dst++ = (((GLuint*)src)[j] & 0xff000000) >> 8;
797 }
798 }
799 else {
800 for (j=0; j < width; j++) {
801 *dst++ = (((GLuint*)src)[j]) & 0xff;
802 }
803 }
804 break;
805 case GL_UNSIGNED_INT:
806 if (packing->SwapBytes) {
807 /* grab upper byte */
808 for (j=0; j < width; j++) {
809 *dst++ = (((GLuint*)src)[j] & 0xff000000) >> 8;
810 }
811 }
812 else {
813 for (j=0; j < width; j++) {
814 *dst++ = (((GLuint*)src)[j]) & 0xff;
815 }
816 }
817 break;
818 case GL_FLOAT:
819 if (packing->SwapBytes) {
820 for (j=0; j < width; j++) {
821 GLfloat fvalue;
822 GLint value = ((GLuint*)src)[j];
823 value = ((value & 0xff000000) >> 24)
824 | ((value & 0x00ff0000) >> 8)
825 | ((value & 0x0000ff00) << 8)
826 | ((value & 0x000000ff) << 24);
827 fvalue = *((GLfloat*) &value);
828 *dst++ = ((GLint) fvalue) & 0xff;
829 }
830 }
831 else {
832 for (j=0; j < width; j++) {
833 GLfloat fvalue = ((GLfloat *)src)[j];
834 *dst++ = ((GLint) fvalue) & 0xff;
835 }
836 }
837 break;
838 default:
839 gl_problem(ctx, "unpack_stencil_image type" );
840 return image;
841 }
842 }
843
844 return image;
845 }
846
847
848
849 /*
850 * Unpack a bitmap, return a new gl_image struct.
851 */
852 static struct gl_image *
853 unpack_bitmap( GLenum format, GLint width, GLint height,
854 const GLvoid *pixels,
855 const struct gl_pixelstore_attrib *packing )
856 {
857 struct gl_image *image;
858 GLint bytes, i, width_in_bytes;
859 GLubyte *buffer, *dst;
860
861 assert(format == GL_COLOR_INDEX || format == GL_STENCIL_INDEX);
862
863 /* Alloc dest storage */
864 bytes = ((width+7)/8 * height);
865 if (bytes>0 && pixels!=NULL) {
866 buffer = (GLubyte *) MALLOC( bytes );
867 if (!buffer) {
868 return NULL;
869 }
870 /* Copy/unpack pixel data to buffer */
871 width_in_bytes = CEILING( width, 8 );
872 dst = buffer;
873 for (i=0; i<height; i++) {
874 GLvoid *src = gl_pixel_addr_in_image( packing, pixels,
875 width, height,
876 GL_COLOR_INDEX, GL_BITMAP,
877 0, i, 0 );
878 if (!src) {
879 FREE(buffer);
880 return NULL;
881 }
882 MEMCPY( dst, src, width_in_bytes );
883 dst += width_in_bytes;
884 }
885 /* Bit flipping */
886 if (packing->LsbFirst) {
887 gl_flip_bytes( buffer, bytes );
888 }
889 }
890 else {
891 /* a 'null' bitmap */
892 buffer = NULL;
893 }
894
895 image = alloc_image();
896 if (image) {
897 image->Width = width;
898 image->Height = height;
899 image->Depth = 1;
900 image->Components = 0;
901 image->Format = format;
902 image->Type = GL_BITMAP;
903 image->Data = buffer;
904 image->RefCount = 0;
905 }
906 else {
907 FREE( buffer );
908 return NULL;
909 }
910
911 return image;
912 }
913
914
915
916 /*
917 * Unpack a 32x32 pixel polygon stipple from user memory using the
918 * current pixel unpack settings.
919 */
920 void gl_unpack_polygon_stipple( const GLcontext *ctx,
921 const GLubyte *pattern, GLuint dest[32] )
922 {
923 GLint i;
924 for (i = 0; i < 32; i++) {
925 GLubyte *src = (GLubyte *) gl_pixel_addr_in_image( &ctx->Unpack, pattern,
926 32, 32, GL_COLOR_INDEX, GL_BITMAP, 0, i, 0 );
927 dest[i] = (src[0] << 24)
928 | (src[1] << 16)
929 | (src[2] << 8)
930 | (src[3] );
931 }
932
933 /* Bit flipping within each byte */
934 if (ctx->Unpack.LsbFirst) {
935 gl_flip_bytes( (GLubyte *) dest, 32 * 4 );
936 }
937 }
938
939
940
941 /*
942 * Pack polygon stipple into user memory given current pixel packing
943 * settings.
944 */
945 void gl_pack_polygon_stipple( const GLcontext *ctx,
946 const GLuint pattern[32],
947 GLubyte *dest )
948 {
949 GLint i;
950 for (i = 0; i < 32; i++) {
951 GLubyte *dst = (GLubyte *) gl_pixel_addr_in_image( &ctx->Pack, dest,
952 32, 32, GL_COLOR_INDEX, GL_BITMAP, 0, i, 0 );
953 dst[0] = (pattern[i] >> 24) & 0xff;
954 dst[1] = (pattern[i] >> 16) & 0xff;
955 dst[2] = (pattern[i] >> 8) & 0xff;
956 dst[3] = (pattern[i] ) & 0xff;
957
958 /* Bit flipping within each byte */
959 if (ctx->Pack.LsbFirst) {
960 gl_flip_bytes( (GLubyte *) dst, 4 );
961 }
962 }
963 }
964
965
966
967 /*
968 * Unpack an RGBA or CI image and store it as unsigned bytes
969 */
970 static struct gl_image *
971 unpack_ubyte_image( GLint width, GLint height,
972 GLint depth, GLenum format, const GLvoid *pixels,
973 const struct gl_pixelstore_attrib *packing )
974 {
975 struct gl_image *image;
976 GLint width_in_bytes;
977 GLint components;
978 GLubyte *buffer, *dst;
979 GLint i, d;
980
981 components = gl_components_in_format( format );
982
983 width_in_bytes = width * components * sizeof(GLubyte);
984 buffer = (GLubyte *) MALLOC( height * width_in_bytes * depth );
985 if (!buffer) {
986 return NULL;
987 }
988
989 /* Copy/unpack pixel data to buffer */
990 dst = buffer;
991 for (d=0; d<depth; d++ ) {
992 for (i=0;i<height;i++) {
993 GLubyte *src = (GLubyte *) gl_pixel_addr_in_image( packing,
994 pixels, width, height, format, GL_UNSIGNED_BYTE,
995 d, i, 0 );
996 if (!src) {
997 FREE(buffer);
998 return NULL;
999 }
1000 MEMCPY( dst, src, width_in_bytes );
1001 dst += width_in_bytes;
1002 }
1003 }
1004
1005 if (format == GL_BGR) {
1006 /* swap order of every ubyte triplet from BGR to RGB */
1007 for (i=0; i<width*height; i++) {
1008 GLubyte b = buffer[i*3+0];
1009 GLubyte r = buffer[i*3+2];
1010 buffer[i*3+0] = r;
1011 buffer[i*3+2] = b;
1012 }
1013 }
1014 else if (format == GL_BGRA) {
1015 /* swap order of every ubyte quadruplet from BGRA to RGBA */
1016 for (i=0; i<width*height; i++) {
1017 GLubyte b = buffer[i*4+0];
1018 GLubyte r = buffer[i*4+2];
1019 buffer[i*4+0] = r;
1020 buffer[i*4+2] = b;
1021 }
1022 }
1023 else if (format == GL_ABGR_EXT) {
1024 /* swap order of every ubyte quadruplet from ABGR to RGBA */
1025 for (i=0; i<width*height; i++) {
1026 GLubyte a = buffer[i*4+0];
1027 GLubyte b = buffer[i*4+1];
1028 GLubyte g = buffer[i*4+2];
1029 GLubyte r = buffer[i*4+3];
1030 buffer[i*4+0] = r;
1031 buffer[i*4+1] = g;
1032 buffer[i*4+2] = b;
1033 buffer[i*4+3] = a;
1034 }
1035 }
1036
1037
1038 image = alloc_image();
1039 if (image) {
1040 image->Width = width;
1041 image->Height = height;
1042 image->Depth = depth;
1043 image->Components = components;
1044 if (format == GL_BGR)
1045 image->Format = GL_RGB;
1046 else if (format == GL_BGRA)
1047 image->Format = GL_RGBA;
1048 else if (format == GL_ABGR_EXT)
1049 image->Format = GL_RGBA;
1050 else
1051 image->Format = format;
1052 image->Type = GL_UNSIGNED_BYTE;
1053 image->Data = buffer;
1054 image->RefCount = 0;
1055 }
1056 else {
1057 FREE( buffer );
1058 }
1059
1060 return image;
1061 }
1062
1063
1064
1065 /*
1066 * Unpack a color image storing image as GLfloats
1067 */
1068 static struct gl_image *
1069 unpack_float_image( GLcontext *ctx, GLint width, GLint height, GLint depth,
1070 GLenum format, GLenum type, const GLvoid *pixels,
1071 const struct gl_pixelstore_attrib *packing )
1072 {
1073 struct gl_image *image;
1074 GLfloat *dst;
1075 GLint elems_per_row;
1076 GLint components;
1077 GLint i, j, d;
1078 GLboolean normalize;
1079
1080 assert(type != GL_BITMAP);
1081
1082 components = gl_components_in_format( format );
1083 assert(components > 0); /* should have been caught earlier */
1084
1085 if (!gl_is_legal_format_and_type( format, type )) {
1086 /* bad pixel type for format, make dummy image */
1087 image = alloc_image();
1088 if (image) {
1089 image->Width = width;
1090 image->Height = height;
1091 image->Depth = depth;
1092 image->Components = components;
1093 image->Format = format;
1094 image->Type = type;
1095 image->Data = NULL;
1096 image->RefCount = 0;
1097 }
1098 return image;
1099 }
1100
1101 elems_per_row = width * components;
1102
1103 image = alloc_image();
1104 if (image) {
1105 image->Width = width;
1106 image->Height = height;
1107 image->Depth = depth;
1108 image->Components = components;
1109 if (format == GL_BGR)
1110 image->Format = GL_RGB;
1111 else if (format == GL_BGRA)
1112 image->Format = GL_RGBA;
1113 else if (format == GL_ABGR_EXT)
1114 image->Format = GL_RGBA;
1115 else
1116 image->Format = format;
1117 image->Type = GL_FLOAT;
1118 image->Data = MALLOC( elems_per_row * height * depth * sizeof(GLfloat));
1119 image->RefCount = 0;
1120 if (!image->Data)
1121 return image;
1122 }
1123 else {
1124 return NULL;
1125 }
1126
1127 normalize = (format != GL_COLOR_INDEX) && (format != GL_STENCIL_INDEX);
1128
1129 dst = (GLfloat *) image->Data;
1130
1131 for (d=0; d<depth; d++) {
1132 for (i=0;i<height;i++) {
1133 GLvoid *src = gl_pixel_addr_in_image( packing, pixels,
1134 width, height,
1135 format, type,
1136 d, i, 0 );
1137 if (!src) {
1138 return image;
1139 }
1140
1141 switch (type) {
1142 case GL_UNSIGNED_BYTE:
1143 {
1144 GLubyte *ubsrc = (GLubyte *) src;
1145 if (normalize) {
1146 for (j=0;j<elems_per_row;j++) {
1147 *dst++ = UBYTE_TO_FLOAT(ubsrc[j]);
1148 }
1149 }
1150 else {
1151 for (j=0;j<elems_per_row;j++) {
1152 *dst++ = (GLfloat) ubsrc[j];
1153 }
1154 }
1155 }
1156 break;
1157 case GL_BYTE:
1158 if (normalize) {
1159 for (j=0;j<elems_per_row;j++) {
1160 *dst++ = BYTE_TO_FLOAT(((GLbyte*)src)[j]);
1161 }
1162 }
1163 else {
1164 for (j=0;j<elems_per_row;j++) {
1165 *dst++ = (GLfloat) ((GLbyte*)src)[j];
1166 }
1167 }
1168 break;
1169 case GL_UNSIGNED_SHORT:
1170 if (packing->SwapBytes) {
1171 for (j=0;j<elems_per_row;j++) {
1172 GLushort value = ((GLushort*)src)[j];
1173 value = ((value >> 8) & 0xff) | ((value&0xff) << 8);
1174 if (normalize) {
1175 *dst++ = USHORT_TO_FLOAT(value);
1176 }
1177 else {
1178 *dst++ = (GLfloat) value;
1179 }
1180 }
1181 }
1182 else {
1183 if (normalize) {
1184 for (j=0;j<elems_per_row;j++) {
1185 *dst++ = USHORT_TO_FLOAT(((GLushort*)src)[j]);
1186 }
1187 }
1188 else {
1189 for (j=0;j<elems_per_row;j++) {
1190 *dst++ = (GLfloat) ((GLushort*)src)[j];
1191 }
1192 }
1193 }
1194 break;
1195 case GL_SHORT:
1196 if (packing->SwapBytes) {
1197 for (j=0;j<elems_per_row;j++) {
1198 GLshort value = ((GLshort*)src)[j];
1199 value = ((value >> 8) & 0xff) | ((value&0xff) << 8);
1200 if (normalize) {
1201 *dst++ = SHORT_TO_FLOAT(value);
1202 }
1203 else {
1204 *dst++ = (GLfloat) value;
1205 }
1206 }
1207 }
1208 else {
1209 if (normalize) {
1210 for (j=0;j<elems_per_row;j++) {
1211 *dst++ = SHORT_TO_FLOAT(((GLshort*)src)[j]);
1212 }
1213 }
1214 else {
1215 for (j=0;j<elems_per_row;j++) {
1216 *dst++ = (GLfloat) ((GLshort*)src)[j];
1217 }
1218 }
1219 }
1220 break;
1221 case GL_UNSIGNED_INT:
1222 if (packing->SwapBytes) {
1223 GLuint value;
1224 for (j=0;j<elems_per_row;j++) {
1225 value = ((GLuint*)src)[j];
1226 value = ((value & 0xff000000) >> 24)
1227 | ((value & 0x00ff0000) >> 8)
1228 | ((value & 0x0000ff00) << 8)
1229 | ((value & 0x000000ff) << 24);
1230 if (normalize) {
1231 *dst++ = UINT_TO_FLOAT(value);
1232 }
1233 else {
1234 *dst++ = (GLfloat) value;
1235 }
1236 }
1237 }
1238 else {
1239 if (normalize) {
1240 for (j=0;j<elems_per_row;j++) {
1241 *dst++ = UINT_TO_FLOAT(((GLuint*)src)[j]);
1242 }
1243 }
1244 else {
1245 for (j=0;j<elems_per_row;j++) {
1246 *dst++ = (GLfloat) ((GLuint*)src)[j];
1247 }
1248 }
1249 }
1250 break;
1251 case GL_INT:
1252 if (packing->SwapBytes) {
1253 GLint value;
1254 for (j=0;j<elems_per_row;j++) {
1255 value = ((GLint*)src)[j];
1256 value = ((value & 0xff000000) >> 24)
1257 | ((value & 0x00ff0000) >> 8)
1258 | ((value & 0x0000ff00) << 8)
1259 | ((value & 0x000000ff) << 24);
1260 if (normalize) {
1261 *dst++ = INT_TO_FLOAT(value);
1262 }
1263 else {
1264 *dst++ = (GLfloat) value;
1265 }
1266 }
1267 }
1268 else {
1269 if (normalize) {
1270 for (j=0;j<elems_per_row;j++) {
1271 *dst++ = INT_TO_FLOAT(((GLint*)src)[j]);
1272 }
1273 }
1274 else {
1275 for (j=0;j<elems_per_row;j++) {
1276 *dst++ = (GLfloat) ((GLint*)src)[j];
1277 }
1278 }
1279 }
1280 break;
1281 case GL_FLOAT:
1282 if (packing->SwapBytes) {
1283 GLint value;
1284 for (j=0;j<elems_per_row;j++) {
1285 value = ((GLuint*)src)[j];
1286 value = ((value & 0xff000000) >> 24)
1287 | ((value & 0x00ff0000) >> 8)
1288 | ((value & 0x0000ff00) << 8)
1289 | ((value & 0x000000ff) << 24);
1290 *dst++ = *((GLfloat*) &value);
1291 }
1292 }
1293 else {
1294 MEMCPY( dst, src, elems_per_row*sizeof(GLfloat) );
1295 dst += elems_per_row;
1296 }
1297 break;
1298 case GL_UNSIGNED_BYTE_3_3_2:
1299 {
1300 GLubyte *ubsrc = (GLubyte *) src;
1301 for (j=0;j<width;j++) {
1302 GLubyte p = ubsrc[j];
1303 *dst++ = ((p >> 5) ) * (1.0F / 7.0F); /* red */
1304 *dst++ = ((p >> 2) & 0x7) * (1.0F / 7.0F); /* green */
1305 *dst++ = ((p ) & 0x3) * (1.0F / 3.0F); /* blue */
1306 }
1307 }
1308 break;
1309 case GL_UNSIGNED_BYTE_2_3_3_REV:
1310 {
1311 GLubyte *ubsrc = (GLubyte *) src;
1312 for (j=0;j<width;j++) {
1313 GLubyte p = ubsrc[j];
1314 *dst++ = ((p ) & 0x7) * (1.0F / 7.0F); /* red */
1315 *dst++ = ((p >> 3) & 0x7) * (1.0F / 7.0F); /* green */
1316 *dst++ = ((p >> 6) ) * (1.0F / 3.0F); /* blue */
1317 }
1318 }
1319 break;
1320 case GL_UNSIGNED_SHORT_5_6_5:
1321 {
1322 GLushort *ussrc = (GLushort *) src;
1323 for (j=0;j<width;j++) {
1324 GLushort p = ussrc[j];
1325 *dst++ = ((p >> 11) ) * (1.0F / 31.0F); /* red */
1326 *dst++ = ((p >> 5) & 0x3f) * (1.0F / 63.0F); /* green */
1327 *dst++ = ((p ) & 0x1f) * (1.0F / 31.0F); /* blue */
1328 }
1329 }
1330 break;
1331 case GL_UNSIGNED_SHORT_5_6_5_REV:
1332 {
1333 GLushort *ussrc = (GLushort *) src;
1334 for (j=0;j<width;j++) {
1335 GLushort p = ussrc[j];
1336 *dst++ = ((p ) & 0x1f) * (1.0F / 31.0F); /* red */
1337 *dst++ = ((p >> 5) & 0x3f) * (1.0F / 63.0F); /* green */
1338 *dst++ = ((p >> 11) ) * (1.0F / 31.0F); /* blue */
1339 }
1340 }
1341 break;
1342 case GL_UNSIGNED_SHORT_4_4_4_4:
1343 {
1344 GLushort *ussrc = (GLushort *) src;
1345 for (j=0;j<width;j++) {
1346 GLushort p = ussrc[j];
1347 *dst++ = ((p >> 12) ) * (1.0F / 15.0F); /* red */
1348 *dst++ = ((p >> 8) & 0xf) * (1.0F / 15.0F); /* green */
1349 *dst++ = ((p >> 4) & 0xf) * (1.0F / 15.0F); /* blue */
1350 *dst++ = ((p ) & 0xf) * (1.0F / 15.0F); /* alpha */
1351 }
1352 }
1353 break;
1354 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1355 {
1356 GLushort *ussrc = (GLushort *) src;
1357 for (j=0;j<width;j++) {
1358 GLushort p = ussrc[j];
1359 *dst++ = ((p ) & 0xf) * (1.0F / 15.0F); /* red */
1360 *dst++ = ((p >> 4) & 0xf) * (1.0F / 15.0F); /* green */
1361 *dst++ = ((p >> 8) & 0xf) * (1.0F / 15.0F); /* blue */
1362 *dst++ = ((p >> 12) ) * (1.0F / 15.0F); /* alpha */
1363 }
1364 }
1365 break;
1366 case GL_UNSIGNED_SHORT_5_5_5_1:
1367 {
1368 GLushort *ussrc = (GLushort *) src;
1369 for (j=0;j<width;j++) {
1370 GLushort p = ussrc[j];
1371 *dst++ = ((p >> 11) ) * (1.0F / 31.0F); /* red */
1372 *dst++ = ((p >> 6) & 0x1f) * (1.0F / 31.0F); /* green */
1373 *dst++ = ((p >> 1) & 0x1f) * (1.0F / 31.0F); /* blue */
1374 *dst++ = ((p ) & 0x1) * (1.0F / 1.0F); /* alpha */
1375 }
1376 }
1377 break;
1378 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
1379 {
1380 GLushort *ussrc = (GLushort *) src;
1381 for (j=0;j<width;j++) {
1382 GLushort p = ussrc[j];
1383 *dst++ = ((p ) & 0x1f) * (1.0F / 31.0F); /* red */
1384 *dst++ = ((p >> 5) & 0x1f) * (1.0F / 31.0F); /* green */
1385 *dst++ = ((p >> 10) & 0x1f) * (1.0F / 31.0F); /* blue */
1386 *dst++ = ((p >> 15) ) * (1.0F / 1.0F); /* alpha */
1387 }
1388 }
1389 break;
1390 case GL_UNSIGNED_INT_8_8_8_8:
1391 {
1392 GLuint *uisrc = (GLuint *) src;
1393 for (j=0;j<width;j++) {
1394 GLuint p = uisrc[j];
1395 *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24) );
1396 *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
1397 *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 8) & 0xff);
1398 *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p ) & 0xff);
1399 }
1400 }
1401 break;
1402 case GL_UNSIGNED_INT_8_8_8_8_REV:
1403 {
1404 GLuint *uisrc = (GLuint *) src;
1405 for (j=0;j<width;j++) {
1406 GLuint p = uisrc[j];
1407 *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p ) & 0xff);
1408 *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 8) & 0xff);
1409 *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
1410 *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24) );
1411 }
1412 }
1413 break;
1414 case GL_UNSIGNED_INT_10_10_10_2:
1415 {
1416 GLuint *uisrc = (GLuint *) src;
1417 for (j=0;j<width;j++) {
1418 GLuint p = uisrc[j];
1419 *dst++ = ((p >> 22) ) * (1.0F / 1023.0F); /* r */
1420 *dst++ = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F); /* g */
1421 *dst++ = ((p >> 2) & 0x3ff) * (1.0F / 1023.0F); /* b */
1422 *dst++ = ((p ) & 0x3 ) * (1.0F / 3.0F); /* a */
1423 }
1424 }
1425 break;
1426 case GL_UNSIGNED_INT_2_10_10_10_REV:
1427 {
1428 GLuint *uisrc = (GLuint *) src;
1429 for (j=0;j<width;j++) {
1430 GLuint p = uisrc[j];
1431 *dst++ = ((p ) & 0x3ff) * (1.0F / 1023.0F); /* r*/
1432 *dst++ = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F); /* g */
1433 *dst++ = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F); /* b */
1434 *dst++ = ((p >> 30) ) * (1.0F / 3.0F); /* a */
1435 }
1436 }
1437 break;
1438 default:
1439 gl_problem(ctx, "unpack_float_image type" );
1440 return image;
1441 }
1442 }
1443 }
1444
1445 if (format == GL_BGR) {
1446 /* swap order of every float triplet from BGR to RGBA */
1447 GLfloat *buffer = (GLfloat *) image->Data;
1448 for (i=0; i<width*height*depth; i++) {
1449 GLfloat b = buffer[i*3+0];
1450 GLfloat r = buffer[i*3+2];
1451 buffer[i*3+0] = r;
1452 buffer[i*3+2] = b;
1453 }
1454 }
1455 else if (format == GL_BGRA) {
1456 /* swap order of every float quadruplet from BGRA to RGBA */
1457 GLfloat *buffer = (GLfloat *) image->Data;
1458 for (i=0; i<width*height*depth; i++) {
1459 GLfloat b = buffer[i*4+0];
1460 GLfloat r = buffer[i*4+2];
1461 buffer[i*4+0] = r;
1462 buffer[i*4+2] = b;
1463 }
1464 }
1465 else if (format == GL_ABGR_EXT) {
1466 /* swap order of every float quadruplet from ABGR to RGBA */
1467 GLfloat *buffer = (GLfloat *) image->Data;
1468 for (i=0; i<width*height*depth; i++) {
1469 GLfloat a = buffer[i*4+0];
1470 GLfloat b = buffer[i*4+1];
1471 GLfloat g = buffer[i*4+2];
1472 GLfloat r = buffer[i*4+3];
1473 buffer[i*4+0] = r;
1474 buffer[i*4+1] = g;
1475 buffer[i*4+2] = b;
1476 buffer[i*4+3] = a;
1477 }
1478 }
1479
1480 return image;
1481 }
1482
1483
1484
1485 /*
1486 * Unpack a bitmap image, using current glPixelStore parameters,
1487 * making a new gl_image.
1488 */
1489 struct gl_image *gl_unpack_bitmap( GLcontext *ctx,
1490 GLsizei width, GLsizei height,
1491 const GLubyte *bitmap,
1492 const struct gl_pixelstore_attrib *packing )
1493 {
1494 return gl_unpack_image( ctx, width, height,
1495 GL_COLOR_INDEX, GL_BITMAP, bitmap, packing );
1496 }
1497
1498
1499
1500 /*
1501 * Unpack a 2-D image from user's buffer. Return pointer to new
1502 * gl_image struct.
1503 *
1504 * Input: width, height - size in pixels
1505 * format - format of incoming pixel data
1506 * type - datatype of incoming pixel data
1507 * pixels - pointer to unpacked image in user buffer
1508 */
1509 struct gl_image *gl_unpack_image( GLcontext *ctx,
1510 GLint width, GLint height,
1511 GLenum format, GLenum type,
1512 const GLvoid *pixels,
1513 const struct gl_pixelstore_attrib *packing )
1514 {
1515 return gl_unpack_image3D( ctx, width, height, 1,
1516 format, type, pixels, packing );
1517 }
1518
1519
1520
1521 /*
1522 * Unpack a 1, 2 or 3-D image from user-supplied address, returning a
1523 * pointer to a new gl_image struct.
1524 * This function is always called by a higher-level unpack function such
1525 * as gl_unpack_texsubimage() or gl_unpack_bitmap().
1526 *
1527 * Input: width, height, depth - size in pixels
1528 * format - format of incoming pixel data
1529 * type - datatype of incoming pixel data
1530 * pixels - pointer to unpacked image.
1531 */
1532 struct gl_image *gl_unpack_image3D( GLcontext *ctx,
1533 GLint width, GLint height, GLint depth,
1534 GLenum format, GLenum type,
1535 const GLvoid *pixels,
1536 const struct gl_pixelstore_attrib *packing)
1537 {
1538 if (width <= 0 || height <= 0 || depth <= 0) {
1539 return alloc_error_image(width, height, depth, format, type);
1540 }
1541
1542 if (type==GL_BITMAP) {
1543 if (format != GL_COLOR_INDEX && format != GL_STENCIL_INDEX) {
1544 return alloc_error_image(width, height, depth, format, type);
1545 }
1546 else {
1547 return unpack_bitmap( format, width, height, pixels, packing );
1548 }
1549 }
1550 else if (format==GL_DEPTH_COMPONENT) {
1551 /* TODO: pack as GLdepth values (GLushort or GLuint) */
1552 return unpack_depth_image( ctx, type, width, height, pixels, packing );
1553 }
1554 else if (format==GL_STENCIL_INDEX) {
1555 /* TODO: pack as GLstencil (GLubyte or GLushort) */
1556 return unpack_stencil_image( ctx, type, width, height, pixels, packing );
1557 }
1558 else if (type==GL_UNSIGNED_BYTE) {
1559 /* upack, convert to GLubytes */
1560 return unpack_ubyte_image( width, height, depth, format, pixels, packing );
1561 }
1562 else {
1563 /* upack, convert to floats */
1564 return unpack_float_image( ctx, width, height, depth,
1565 format, type, pixels, packing );
1566 }
1567
1568 /* never get here */
1569 /*return NULL;*/
1570 }
1571
1572
1573 /*
1574 * Apply pixel-transfer operations (scale, bias, mapping) to a single row
1575 * of a gl_image. Put resulting color components into result array.
1576 */
1577 void gl_scale_bias_map_image_data( const GLcontext *ctx,
1578 const struct gl_image *image,
1579 GLint row, GLubyte result[] )
1580 {
1581 GLint start, i;
1582
1583 assert(ctx);
1584 assert(image);
1585 assert(result);
1586 assert(row >= 0);
1587
1588 start = row * image->Width * image->Components;
1589
1590 for (i=0; i < image->Width; i++) {
1591 GLint pos = start+i;
1592 GLfloat red, green, blue, alpha;
1593 if (image->Type == GL_UNSIGNED_BYTE) {
1594 const GLubyte *data = (GLubyte *) image->Data;
1595 switch (image->Format) {
1596 case GL_RED:
1597 red = data[pos] * (1.0F/255.0F);
1598 green = 0;
1599 blue = 0;
1600 alpha = 0;
1601 break;
1602 case GL_RGB:
1603 red = data[pos*3+0] * (1.0F/255.0F);
1604 green = data[pos*3+1] * (1.0F/255.0F);
1605 blue = data[pos*3+2] * (1.0F/255.0F);
1606 alpha = 0;
1607 break;
1608 default:
1609 gl_problem(ctx, "bad image format in gl_scale...image_data");
1610 return;
1611 }
1612 }
1613 else if (image->Type == GL_FLOAT) {
1614 const GLubyte *data = (GLubyte *) image->Data;
1615 switch (image->Format) {
1616 case GL_RED:
1617 red = data[pos];
1618 green = 0;
1619 blue = 0;
1620 alpha = 0;
1621 break;
1622 case GL_RGB:
1623 red = data[pos*3+0];
1624 green = data[pos*3+1];
1625 blue = data[pos*3+2];
1626 alpha = 0;
1627 break;
1628 default:
1629 gl_problem(ctx, "bad image format in gl_scale...image_data");
1630 return;
1631 }
1632 }
1633 else {
1634 gl_problem(ctx, "Bad image type in gl_scale_...image_data");
1635 return;
1636 }
1637
1638 assert(red >= 0.0 && red <= 1.0);
1639 assert(green >= 0.0 && green <= 1.0);
1640 assert(blue >= 0.0 && blue <= 1.0);
1641 assert(alpha >= 0.0 && alpha <= 1.0);
1642
1643 /*
1644 if (scale or bias) {
1645
1646
1647 }
1648 if (mapping) {
1649
1650 }
1651 */
1652
1653 result[i*4+0] = (GLubyte) (red * 255.0);
1654 result[i*4+1] = (GLubyte) (green * 255.0);
1655 result[i*4+2] = (GLubyte) (blue * 255.0);
1656 result[i*4+3] = (GLubyte) (alpha * 255.0);
1657 }
1658 }
1659
1660
1661
1662 /*
1663 * Pack the given RGBA span into client memory at 'dest' address
1664 * in the given pixel format and type.
1665 * Optionally apply the enabled pixel transfer ops.
1666 * Pack into memory using the given packing params struct.
1667 * This is used by glReadPixels and glGetTexImage?D()
1668 * Input: ctx - the context
1669 * n - number of pixels in the span
1670 * rgba - the pixels
1671 * format - dest packing format
1672 * type - dest packing datatype
1673 * destination - destination packing address
1674 * packing - pixel packing parameters
1675 * applyTransferOps - apply scale/bias/lookup-table ops?
1676 */
1677 void gl_pack_rgba_span( const GLcontext *ctx,
1678 GLuint n, CONST GLubyte rgba[][4],
1679 GLenum format, GLenum type, GLvoid *destination,
1680 const struct gl_pixelstore_attrib *packing,
1681 GLboolean applyTransferOps )
1682 {
1683 /* Test for optimized case first */
1684 if (!ctx->Pixel.ScaleOrBiasRGBA && !ctx->Pixel.MapColorFlag &&
1685 format == GL_RGBA && type == GL_UNSIGNED_BYTE) {
1686 /* common simple case */
1687 MEMCPY( destination, rgba, n * 4 * sizeof(GLubyte) );
1688 }
1689 else if (!ctx->Pixel.ScaleOrBiasRGBA && !ctx->Pixel.MapColorFlag &&
1690 format == GL_RGB && type == GL_UNSIGNED_BYTE) {
1691 /* common simple case */
1692 GLint i;
1693 GLubyte *dest = (GLubyte *) destination;
1694 for (i = 0; i < n; i++) {
1695 dest[0] = rgba[i][RCOMP];
1696 dest[1] = rgba[i][GCOMP];
1697 dest[2] = rgba[i][BCOMP];
1698 dest += 3;
1699 }
1700 }
1701 else {
1702 GLfloat red[MAX_WIDTH], green[MAX_WIDTH], blue[MAX_WIDTH];
1703 GLfloat alpha[MAX_WIDTH], luminance[MAX_WIDTH];
1704 const GLfloat rscale = 1.0F / 255.0F;
1705 const GLfloat gscale = 1.0F / 255.0F;
1706 const GLfloat bscale = 1.0F / 255.0F;
1707 const GLfloat ascale = 1.0F / 255.0F;
1708 const GLint comps = gl_components_in_format(format);
1709 GLuint i;
1710
1711 assert(n <= MAX_WIDTH);
1712
1713 /* convert color components to floating point */
1714 for (i=0;i<n;i++) {
1715 red[i] = rgba[i][RCOMP] * rscale;
1716 green[i] = rgba[i][GCOMP] * gscale;
1717 blue[i] = rgba[i][BCOMP] * bscale;
1718 alpha[i] = rgba[i][ACOMP] * ascale;
1719 }
1720
1721 /*
1722 * Apply scale, bias and lookup-tables if enabled.
1723 */
1724 if (applyTransferOps) {
1725 if (ctx->Pixel.ScaleOrBiasRGBA) {
1726 gl_scale_and_bias_color( ctx, n, red, green, blue, alpha );
1727 }
1728 if (ctx->Pixel.MapColorFlag) {
1729 gl_map_color( ctx, n, red, green, blue, alpha );
1730 }
1731 }
1732
1733 if (format==GL_LUMINANCE || format==GL_LUMINANCE_ALPHA) {
1734 for (i=0;i<n;i++) {
1735 GLfloat sum = red[i] + green[i] + blue[i];
1736 luminance[i] = CLAMP( sum, 0.0F, 1.0F );
1737 }
1738 }
1739
1740 /*
1741 * Pack/store the pixels. Ugh! Lots of cases!!!
1742 */
1743 switch (type) {
1744 case GL_UNSIGNED_BYTE:
1745 {
1746 GLubyte *dst = (GLubyte *) destination;
1747 switch (format) {
1748 case GL_RED:
1749 for (i=0;i<n;i++)
1750 dst[i] = FLOAT_TO_UBYTE(red[i]);
1751 break;
1752 case GL_GREEN:
1753 for (i=0;i<n;i++)
1754 dst[i] = FLOAT_TO_UBYTE(green[i]);
1755 break;
1756 case GL_BLUE:
1757 for (i=0;i<n;i++)
1758 dst[i] = FLOAT_TO_UBYTE(blue[i]);
1759 break;
1760 case GL_ALPHA:
1761 for (i=0;i<n;i++)
1762 dst[i] = FLOAT_TO_UBYTE(alpha[i]);
1763 break;
1764 case GL_LUMINANCE:
1765 for (i=0;i<n;i++)
1766 dst[i] = FLOAT_TO_UBYTE(luminance[i]);
1767 break;
1768 case GL_LUMINANCE_ALPHA:
1769 for (i=0;i<n;i++) {
1770 dst[i*2+0] = FLOAT_TO_UBYTE(luminance[i]);
1771 dst[i*2+1] = FLOAT_TO_UBYTE(alpha[i]);
1772 }
1773 break;
1774 case GL_RGB:
1775 for (i=0;i<n;i++) {
1776 dst[i*3+0] = FLOAT_TO_UBYTE(red[i]);
1777 dst[i*3+1] = FLOAT_TO_UBYTE(green[i]);
1778 dst[i*3+2] = FLOAT_TO_UBYTE(blue[i]);
1779 }
1780 break;
1781 case GL_RGBA:
1782 for (i=0;i<n;i++) {
1783 dst[i*4+0] = FLOAT_TO_UBYTE(red[i]);
1784 dst[i*4+1] = FLOAT_TO_UBYTE(green[i]);
1785 dst[i*4+2] = FLOAT_TO_UBYTE(blue[i]);
1786 dst[i*4+3] = FLOAT_TO_UBYTE(alpha[i]);
1787 }
1788 break;
1789 case GL_BGR:
1790 for (i=0;i<n;i++) {
1791 dst[i*3+0] = FLOAT_TO_UBYTE(blue[i]);
1792 dst[i*3+1] = FLOAT_TO_UBYTE(green[i]);
1793 dst[i*3+2] = FLOAT_TO_UBYTE(red[i]);
1794 }
1795 break;
1796 case GL_BGRA:
1797 for (i=0;i<n;i++) {
1798 dst[i*4+0] = FLOAT_TO_UBYTE(blue[i]);
1799 dst[i*4+1] = FLOAT_TO_UBYTE(green[i]);
1800 dst[i*4+2] = FLOAT_TO_UBYTE(red[i]);
1801 dst[i*4+3] = FLOAT_TO_UBYTE(alpha[i]);
1802 }
1803 break;
1804 case GL_ABGR_EXT:
1805 for (i=0;i<n;i++) {
1806 dst[i*4+0] = FLOAT_TO_UBYTE(alpha[i]);
1807 dst[i*4+1] = FLOAT_TO_UBYTE(blue[i]);
1808 dst[i*4+2] = FLOAT_TO_UBYTE(green[i]);
1809 dst[i*4+3] = FLOAT_TO_UBYTE(red[i]);
1810 }
1811 break;
1812 default:
1813 gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
1814 }
1815 }
1816 break;
1817 case GL_BYTE:
1818 {
1819 GLbyte *dst = (GLbyte *) destination;
1820 switch (format) {
1821 case GL_RED:
1822 for (i=0;i<n;i++)
1823 dst[i] = FLOAT_TO_BYTE(red[i]);
1824 break;
1825 case GL_GREEN:
1826 for (i=0;i<n;i++)
1827 dst[i] = FLOAT_TO_BYTE(green[i]);
1828 break;
1829 case GL_BLUE:
1830 for (i=0;i<n;i++)
1831 dst[i] = FLOAT_TO_BYTE(blue[i]);
1832 break;
1833 case GL_ALPHA:
1834 for (i=0;i<n;i++)
1835 dst[i] = FLOAT_TO_BYTE(alpha[i]);
1836 break;
1837 case GL_LUMINANCE:
1838 for (i=0;i<n;i++)
1839 dst[i] = FLOAT_TO_BYTE(luminance[i]);
1840 break;
1841 case GL_LUMINANCE_ALPHA:
1842 for (i=0;i<n;i++) {
1843 dst[i*2+0] = FLOAT_TO_BYTE(luminance[i]);
1844 dst[i*2+1] = FLOAT_TO_BYTE(alpha[i]);
1845 }
1846 break;
1847 case GL_RGB:
1848 for (i=0;i<n;i++) {
1849 dst[i*3+0] = FLOAT_TO_BYTE(red[i]);
1850 dst[i*3+1] = FLOAT_TO_BYTE(green[i]);
1851 dst[i*3+2] = FLOAT_TO_BYTE(blue[i]);
1852 }
1853 break;
1854 case GL_RGBA:
1855 for (i=0;i<n;i++) {
1856 dst[i*4+0] = FLOAT_TO_BYTE(red[i]);
1857 dst[i*4+1] = FLOAT_TO_BYTE(green[i]);
1858 dst[i*4+2] = FLOAT_TO_BYTE(blue[i]);
1859 dst[i*4+3] = FLOAT_TO_BYTE(alpha[i]);
1860 }
1861 break;
1862 case GL_BGR:
1863 for (i=0;i<n;i++) {
1864 dst[i*3+0] = FLOAT_TO_BYTE(blue[i]);
1865 dst[i*3+1] = FLOAT_TO_BYTE(green[i]);
1866 dst[i*3+2] = FLOAT_TO_BYTE(red[i]);
1867 }
1868 break;
1869 case GL_BGRA:
1870 for (i=0;i<n;i++) {
1871 dst[i*4+0] = FLOAT_TO_BYTE(blue[i]);
1872 dst[i*4+1] = FLOAT_TO_BYTE(green[i]);
1873 dst[i*4+2] = FLOAT_TO_BYTE(red[i]);
1874 dst[i*4+3] = FLOAT_TO_BYTE(alpha[i]);
1875 }
1876 case GL_ABGR_EXT:
1877 for (i=0;i<n;i++) {
1878 dst[i*4+0] = FLOAT_TO_BYTE(alpha[i]);
1879 dst[i*4+1] = FLOAT_TO_BYTE(blue[i]);
1880 dst[i*4+2] = FLOAT_TO_BYTE(green[i]);
1881 dst[i*4+3] = FLOAT_TO_BYTE(red[i]);
1882 }
1883 break;
1884 default:
1885 gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
1886 }
1887 }
1888 break;
1889 case GL_UNSIGNED_SHORT:
1890 {
1891 GLushort *dst = (GLushort *) destination;
1892 switch (format) {
1893 case GL_RED:
1894 for (i=0;i<n;i++)
1895 dst[i] = FLOAT_TO_USHORT(red[i]);
1896 break;
1897 case GL_GREEN:
1898 for (i=0;i<n;i++)
1899 dst[i] = FLOAT_TO_USHORT(green[i]);
1900 break;
1901 case GL_BLUE:
1902 for (i=0;i<n;i++)
1903 dst[i] = FLOAT_TO_USHORT(blue[i]);
1904 break;
1905 case GL_ALPHA:
1906 for (i=0;i<n;i++)
1907 dst[i] = FLOAT_TO_USHORT(alpha[i]);
1908 break;
1909 case GL_LUMINANCE:
1910 for (i=0;i<n;i++)
1911 dst[i] = FLOAT_TO_USHORT(luminance[i]);
1912 break;
1913 case GL_LUMINANCE_ALPHA:
1914 for (i=0;i<n;i++) {
1915 dst[i*2+0] = FLOAT_TO_USHORT(luminance[i]);
1916 dst[i*2+1] = FLOAT_TO_USHORT(alpha[i]);
1917 }
1918 break;
1919 case GL_RGB:
1920 for (i=0;i<n;i++) {
1921 dst[i*3+0] = FLOAT_TO_USHORT(red[i]);
1922 dst[i*3+1] = FLOAT_TO_USHORT(green[i]);
1923 dst[i*3+2] = FLOAT_TO_USHORT(blue[i]);
1924 }
1925 break;
1926 case GL_RGBA:
1927 for (i=0;i<n;i++) {
1928 dst[i*4+0] = FLOAT_TO_USHORT(red[i]);
1929 dst[i*4+1] = FLOAT_TO_USHORT(green[i]);
1930 dst[i*4+2] = FLOAT_TO_USHORT(blue[i]);
1931 dst[i*4+3] = FLOAT_TO_USHORT(alpha[i]);
1932 }
1933 break;
1934 case GL_BGR:
1935 for (i=0;i<n;i++) {
1936 dst[i*3+0] = FLOAT_TO_USHORT(blue[i]);
1937 dst[i*3+1] = FLOAT_TO_USHORT(green[i]);
1938 dst[i*3+2] = FLOAT_TO_USHORT(red[i]);
1939 }
1940 break;
1941 case GL_BGRA:
1942 for (i=0;i<n;i++) {
1943 dst[i*4+0] = FLOAT_TO_USHORT(blue[i]);
1944 dst[i*4+1] = FLOAT_TO_USHORT(green[i]);
1945 dst[i*4+2] = FLOAT_TO_USHORT(red[i]);
1946 dst[i*4+3] = FLOAT_TO_USHORT(alpha[i]);
1947 }
1948 break;
1949 case GL_ABGR_EXT:
1950 for (i=0;i<n;i++) {
1951 dst[i*4+0] = FLOAT_TO_USHORT(alpha[i]);
1952 dst[i*4+1] = FLOAT_TO_USHORT(blue[i]);
1953 dst[i*4+2] = FLOAT_TO_USHORT(green[i]);
1954 dst[i*4+3] = FLOAT_TO_USHORT(red[i]);
1955 }
1956 break;
1957 default:
1958 gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
1959 }
1960 if (packing->SwapBytes) {
1961 gl_swap2( (GLushort *) dst, n * comps);
1962 }
1963 }
1964 break;
1965 case GL_SHORT:
1966 {
1967 GLshort *dst = (GLshort *) destination;
1968 switch (format) {
1969 case GL_RED:
1970 for (i=0;i<n;i++)
1971 dst[i] = FLOAT_TO_SHORT(red[i]);
1972 break;
1973 case GL_GREEN:
1974 for (i=0;i<n;i++)
1975 dst[i] = FLOAT_TO_SHORT(green[i]);
1976 break;
1977 case GL_BLUE:
1978 for (i=0;i<n;i++)
1979 dst[i] = FLOAT_TO_SHORT(blue[i]);
1980 break;
1981 case GL_ALPHA:
1982 for (i=0;i<n;i++)
1983 dst[i] = FLOAT_TO_SHORT(alpha[i]);
1984 break;
1985 case GL_LUMINANCE:
1986 for (i=0;i<n;i++)
1987 dst[i] = FLOAT_TO_SHORT(luminance[i]);
1988 break;
1989 case GL_LUMINANCE_ALPHA:
1990 for (i=0;i<n;i++) {
1991 dst[i*2+0] = FLOAT_TO_SHORT(luminance[i]);
1992 dst[i*2+1] = FLOAT_TO_SHORT(alpha[i]);
1993 }
1994 break;
1995 case GL_RGB:
1996 for (i=0;i<n;i++) {
1997 dst[i*3+0] = FLOAT_TO_SHORT(red[i]);
1998 dst[i*3+1] = FLOAT_TO_SHORT(green[i]);
1999 dst[i*3+2] = FLOAT_TO_SHORT(blue[i]);
2000 }
2001 break;
2002 case GL_RGBA:
2003 for (i=0;i<n;i++) {
2004 dst[i*4+0] = FLOAT_TO_SHORT(red[i]);
2005 dst[i*4+1] = FLOAT_TO_SHORT(green[i]);
2006 dst[i*4+2] = FLOAT_TO_SHORT(blue[i]);
2007 dst[i*4+3] = FLOAT_TO_SHORT(alpha[i]);
2008 }
2009 break;
2010 case GL_BGR:
2011 for (i=0;i<n;i++) {
2012 dst[i*3+0] = FLOAT_TO_SHORT(blue[i]);
2013 dst[i*3+1] = FLOAT_TO_SHORT(green[i]);
2014 dst[i*3+2] = FLOAT_TO_SHORT(red[i]);
2015 }
2016 break;
2017 case GL_BGRA:
2018 for (i=0;i<n;i++) {
2019 dst[i*4+0] = FLOAT_TO_SHORT(blue[i]);
2020 dst[i*4+1] = FLOAT_TO_SHORT(green[i]);
2021 dst[i*4+2] = FLOAT_TO_SHORT(red[i]);
2022 dst[i*4+3] = FLOAT_TO_SHORT(alpha[i]);
2023 }
2024 case GL_ABGR_EXT:
2025 for (i=0;i<n;i++) {
2026 dst[i*4+0] = FLOAT_TO_SHORT(alpha[i]);
2027 dst[i*4+1] = FLOAT_TO_SHORT(blue[i]);
2028 dst[i*4+2] = FLOAT_TO_SHORT(green[i]);
2029 dst[i*4+3] = FLOAT_TO_SHORT(red[i]);
2030 }
2031 break;
2032 default:
2033 gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
2034 }
2035 if (packing->SwapBytes) {
2036 gl_swap2( (GLushort *) dst, n * comps );
2037 }
2038 }
2039 break;
2040 case GL_UNSIGNED_INT:
2041 {
2042 GLuint *dst = (GLuint *) destination;
2043 switch (format) {
2044 case GL_RED:
2045 for (i=0;i<n;i++)
2046 dst[i] = FLOAT_TO_UINT(red[i]);
2047 break;
2048 case GL_GREEN:
2049 for (i=0;i<n;i++)
2050 dst[i] = FLOAT_TO_UINT(green[i]);
2051 break;
2052 case GL_BLUE:
2053 for (i=0;i<n;i++)
2054 dst[i] = FLOAT_TO_UINT(blue[i]);
2055 break;
2056 case GL_ALPHA:
2057 for (i=0;i<n;i++)
2058 dst[i] = FLOAT_TO_UINT(alpha[i]);
2059 break;
2060 case GL_LUMINANCE:
2061 for (i=0;i<n;i++)
2062 dst[i] = FLOAT_TO_UINT(luminance[i]);
2063 break;
2064 case GL_LUMINANCE_ALPHA:
2065 for (i=0;i<n;i++) {
2066 dst[i*2+0] = FLOAT_TO_UINT(luminance[i]);
2067 dst[i*2+1] = FLOAT_TO_UINT(alpha[i]);
2068 }
2069 break;
2070 case GL_RGB:
2071 for (i=0;i<n;i++) {
2072 dst[i*3+0] = FLOAT_TO_UINT(red[i]);
2073 dst[i*3+1] = FLOAT_TO_UINT(green[i]);
2074 dst[i*3+2] = FLOAT_TO_UINT(blue[i]);
2075 }
2076 break;
2077 case GL_RGBA:
2078 for (i=0;i<n;i++) {
2079 dst[i*4+0] = FLOAT_TO_UINT(red[i]);
2080 dst[i*4+1] = FLOAT_TO_UINT(green[i]);
2081 dst[i*4+2] = FLOAT_TO_UINT(blue[i]);
2082 dst[i*4+3] = FLOAT_TO_UINT(alpha[i]);
2083 }
2084 break;
2085 case GL_BGR:
2086 for (i=0;i<n;i++) {
2087 dst[i*3+0] = FLOAT_TO_UINT(blue[i]);
2088 dst[i*3+1] = FLOAT_TO_UINT(green[i]);
2089 dst[i*3+2] = FLOAT_TO_UINT(red[i]);
2090 }
2091 break;
2092 case GL_BGRA:
2093 for (i=0;i<n;i++) {
2094 dst[i*4+0] = FLOAT_TO_UINT(blue[i]);
2095 dst[i*4+1] = FLOAT_TO_UINT(green[i]);
2096 dst[i*4+2] = FLOAT_TO_UINT(red[i]);
2097 dst[i*4+3] = FLOAT_TO_UINT(alpha[i]);
2098 }
2099 break;
2100 case GL_ABGR_EXT:
2101 for (i=0;i<n;i++) {
2102 dst[i*4+0] = FLOAT_TO_UINT(alpha[i]);
2103 dst[i*4+1] = FLOAT_TO_UINT(blue[i]);
2104 dst[i*4+2] = FLOAT_TO_UINT(green[i]);
2105 dst[i*4+3] = FLOAT_TO_UINT(red[i]);
2106 }
2107 break;
2108 default:
2109 gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
2110 }
2111 if (packing->SwapBytes) {
2112 gl_swap4( (GLuint *) dst, n * comps );
2113 }
2114 }
2115 break;
2116 case GL_INT:
2117 {
2118 GLint *dst = (GLint *) destination;
2119 switch (format) {
2120 case GL_RED:
2121 for (i=0;i<n;i++)
2122 dst[i] = FLOAT_TO_INT(red[i]);
2123 break;
2124 case GL_GREEN:
2125 for (i=0;i<n;i++)
2126 dst[i] = FLOAT_TO_INT(green[i]);
2127 break;
2128 case GL_BLUE:
2129 for (i=0;i<n;i++)
2130 dst[i] = FLOAT_TO_INT(blue[i]);
2131 break;
2132 case GL_ALPHA:
2133 for (i=0;i<n;i++)
2134 dst[i] = FLOAT_TO_INT(alpha[i]);
2135 break;
2136 case GL_LUMINANCE:
2137 for (i=0;i<n;i++)
2138 dst[i] = FLOAT_TO_INT(luminance[i]);
2139 break;
2140 case GL_LUMINANCE_ALPHA:
2141 for (i=0;i<n;i++) {
2142 dst[i*2+0] = FLOAT_TO_INT(luminance[i]);
2143 dst[i*2+1] = FLOAT_TO_INT(alpha[i]);
2144 }
2145 break;
2146 case GL_RGB:
2147 for (i=0;i<n;i++) {
2148 dst[i*3+0] = FLOAT_TO_INT(red[i]);
2149 dst[i*3+1] = FLOAT_TO_INT(green[i]);
2150 dst[i*3+2] = FLOAT_TO_INT(blue[i]);
2151 }
2152 break;
2153 case GL_RGBA:
2154 for (i=0;i<n;i++) {
2155 dst[i*4+0] = FLOAT_TO_INT(red[i]);
2156 dst[i*4+1] = FLOAT_TO_INT(green[i]);
2157 dst[i*4+2] = FLOAT_TO_INT(blue[i]);
2158 dst[i*4+3] = FLOAT_TO_INT(alpha[i]);
2159 }
2160 break;
2161 case GL_BGR:
2162 for (i=0;i<n;i++) {
2163 dst[i*3+0] = FLOAT_TO_INT(blue[i]);
2164 dst[i*3+1] = FLOAT_TO_INT(green[i]);
2165 dst[i*3+2] = FLOAT_TO_INT(red[i]);
2166 }
2167 break;
2168 case GL_BGRA:
2169 for (i=0;i<n;i++) {
2170 dst[i*4+0] = FLOAT_TO_INT(blue[i]);
2171 dst[i*4+1] = FLOAT_TO_INT(green[i]);
2172 dst[i*4+2] = FLOAT_TO_INT(red[i]);
2173 dst[i*4+3] = FLOAT_TO_INT(alpha[i]);
2174 }
2175 break;
2176 case GL_ABGR_EXT:
2177 for (i=0;i<n;i++) {
2178 dst[i*4+0] = FLOAT_TO_INT(alpha[i]);
2179 dst[i*4+1] = FLOAT_TO_INT(blue[i]);
2180 dst[i*4+2] = FLOAT_TO_INT(green[i]);
2181 dst[i*4+3] = FLOAT_TO_INT(red[i]);
2182 }
2183 break;
2184 default:
2185 gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
2186 }
2187 if (packing->SwapBytes) {
2188 gl_swap4( (GLuint *) dst, n * comps );
2189 }
2190 }
2191 break;
2192 case GL_FLOAT:
2193 {
2194 GLfloat *dst = (GLfloat *) destination;
2195 switch (format) {
2196 case GL_RED:
2197 for (i=0;i<n;i++)
2198 dst[i] = red[i];
2199 break;
2200 case GL_GREEN:
2201 for (i=0;i<n;i++)
2202 dst[i] = green[i];
2203 break;
2204 case GL_BLUE:
2205 for (i=0;i<n;i++)
2206 dst[i] = blue[i];
2207 break;
2208 case GL_ALPHA:
2209 for (i=0;i<n;i++)
2210 dst[i] = alpha[i];
2211 break;
2212 case GL_LUMINANCE:
2213 for (i=0;i<n;i++)
2214 dst[i] = luminance[i];
2215 break;
2216 case GL_LUMINANCE_ALPHA:
2217 for (i=0;i<n;i++) {
2218 dst[i*2+0] = luminance[i];
2219 dst[i*2+1] = alpha[i];
2220 }
2221 break;
2222 case GL_RGB:
2223 for (i=0;i<n;i++) {
2224 dst[i*3+0] = red[i];
2225 dst[i*3+1] = green[i];
2226 dst[i*3+2] = blue[i];
2227 }
2228 break;
2229 case GL_RGBA:
2230 for (i=0;i<n;i++) {
2231 dst[i*4+0] = red[i];
2232 dst[i*4+1] = green[i];
2233 dst[i*4+2] = blue[i];
2234 dst[i*4+3] = alpha[i];
2235 }
2236 break;
2237 case GL_BGR:
2238 for (i=0;i<n;i++) {
2239 dst[i*3+0] = blue[i];
2240 dst[i*3+1] = green[i];
2241 dst[i*3+2] = red[i];
2242 }
2243 break;
2244 case GL_BGRA:
2245 for (i=0;i<n;i++) {
2246 dst[i*4+0] = blue[i];
2247 dst[i*4+1] = green[i];
2248 dst[i*4+2] = red[i];
2249 dst[i*4+3] = alpha[i];
2250 }
2251 break;
2252 case GL_ABGR_EXT:
2253 for (i=0;i<n;i++) {
2254 dst[i*4+0] = alpha[i];
2255 dst[i*4+1] = blue[i];
2256 dst[i*4+2] = green[i];
2257 dst[i*4+3] = red[i];
2258 }
2259 break;
2260 default:
2261 gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
2262 }
2263 if (packing->SwapBytes) {
2264 gl_swap4( (GLuint *) dst, n * comps );
2265 }
2266 }
2267 break;
2268 case GL_UNSIGNED_BYTE_3_3_2:
2269 if (format == GL_RGB) {
2270 GLubyte *dst = (GLubyte *) destination;
2271 for (i=0;i<n;i++) {
2272 dst[i] = (((GLint) (red[i] * 7.0F)) << 5)
2273 | (((GLint) (green[i] * 7.0F)) << 2)
2274 | (((GLint) (blue[i] * 3.0F)) );
2275 }
2276 }
2277 break;
2278 case GL_UNSIGNED_BYTE_2_3_3_REV:
2279 if (format == GL_RGB) {
2280 GLubyte *dst = (GLubyte *) destination;
2281 for (i=0;i<n;i++) {
2282 dst[i] = (((GLint) (red[i] * 7.0F)) )
2283 | (((GLint) (green[i] * 7.0F)) << 3)
2284 | (((GLint) (blue[i] * 3.0F)) << 5);
2285 }
2286 }
2287 break;
2288 case GL_UNSIGNED_SHORT_5_6_5:
2289 if (format == GL_RGB) {
2290 GLushort *dst = (GLushort *) destination;
2291 for (i=0;i<n;i++) {
2292 dst[i] = (((GLint) (red[i] * 31.0F)) << 11)
2293 | (((GLint) (green[i] * 63.0F)) << 5)
2294 | (((GLint) (blue[i] * 31.0F)) );
2295 }
2296 }
2297 break;
2298 case GL_UNSIGNED_SHORT_5_6_5_REV:
2299 if (format == GL_RGB) {
2300 GLushort *dst = (GLushort *) destination;
2301 for (i=0;i<n;i++) {
2302 dst[i] = (((GLint) (red[i] * 31.0F)) )
2303 | (((GLint) (green[i] * 63.0F)) << 5)
2304 | (((GLint) (blue[i] * 31.0F)) << 11);
2305 }
2306 }
2307 break;
2308 case GL_UNSIGNED_SHORT_4_4_4_4:
2309 if (format == GL_RGB) {
2310 GLushort *dst = (GLushort *) destination;
2311 for (i=0;i<n;i++) {
2312 dst[i] = (((GLint) (red[i] * 15.0F)) << 12)
2313 | (((GLint) (green[i] * 15.0F)) << 8)
2314 | (((GLint) (blue[i] * 15.0F)) << 4)
2315 | (((GLint) (alpha[i] * 15.0F)) );
2316 }
2317 }
2318 break;
2319 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
2320 if (format == GL_RGB) {
2321 GLushort *dst = (GLushort *) destination;
2322 for (i=0;i<n;i++) {
2323 dst[i] = (((GLint) (red[i] * 15.0F)) )
2324 | (((GLint) (green[i] * 15.0F)) << 4)
2325 | (((GLint) (blue[i] * 15.0F)) << 8)
2326 | (((GLint) (alpha[i] * 15.0F)) << 12);
2327 }
2328 }
2329 break;
2330 case GL_UNSIGNED_SHORT_5_5_5_1:
2331 if (format == GL_RGB) {
2332 GLushort *dst = (GLushort *) destination;
2333 for (i=0;i<n;i++) {
2334 dst[i] = (((GLint) (red[i] * 31.0F)) << 11)
2335 | (((GLint) (green[i] * 31.0F)) << 6)
2336 | (((GLint) (blue[i] * 31.0F)) << 1)
2337 | (((GLint) (alpha[i] * 1.0F)) );
2338 }
2339 }
2340 break;
2341 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
2342 if (format == GL_RGB) {
2343 GLushort *dst = (GLushort *) destination;
2344 for (i=0;i<n;i++) {
2345 dst[i] = (((GLint) (red[i] * 31.0F)) )
2346 | (((GLint) (green[i] * 31.0F)) << 5)
2347 | (((GLint) (blue[i] * 31.0F)) << 10)
2348 | (((GLint) (alpha[i] * 1.0F)) << 15);
2349 }
2350 }
2351 break;
2352 case GL_UNSIGNED_INT_8_8_8_8:
2353 if (format == GL_RGBA) {
2354 GLuint *dst = (GLuint *) destination;
2355 for (i=0;i<n;i++) {
2356 dst[i] = (((GLuint) (red[i] * 255.0F)) << 24)
2357 | (((GLuint) (green[i] * 255.0F)) << 16)
2358 | (((GLuint) (blue[i] * 255.0F)) << 8)
2359 | (((GLuint) (alpha[i] * 255.0F)) );
2360 }
2361 }
2362 else if (format == GL_BGRA) {
2363 GLuint *dst = (GLuint *) destination;
2364 for (i=0;i<n;i++) {
2365 dst[i] = (((GLuint) (blue[i] * 255.0F)) << 24)
2366 | (((GLuint) (green[i] * 255.0F)) << 16)
2367 | (((GLuint) (red[i] * 255.0F)) << 8)
2368 | (((GLuint) (alpha[i] * 255.0F)) );
2369 }
2370 }
2371 else if (format == GL_ABGR_EXT) {
2372 GLuint *dst = (GLuint *) destination;
2373 for (i=0;i<n;i++) {
2374 dst[i] = (((GLuint) (alpha[i] * 255.0F)) << 24)
2375 | (((GLuint) (blue[i] * 255.0F)) << 16)
2376 | (((GLuint) (green[i] * 255.0F)) << 8)
2377 | (((GLuint) (red[i] * 255.0F)) );
2378 }
2379 }
2380 break;
2381 case GL_UNSIGNED_INT_8_8_8_8_REV:
2382 if (format == GL_RGBA) {
2383 GLuint *dst = (GLuint *) destination;
2384 for (i=0;i<n;i++) {
2385 dst[i] = (((GLuint) (red[i] * 255.0F)) )
2386 | (((GLuint) (green[i] * 255.0F)) << 8)
2387 | (((GLuint) (blue[i] * 255.0F)) << 16)
2388 | (((GLuint) (alpha[i] * 255.0F)) << 24);
2389 }
2390 }
2391 else if (format == GL_BGRA) {
2392 GLuint *dst = (GLuint *) destination;
2393 for (i=0;i<n;i++) {
2394 dst[i] = (((GLuint) (blue[i] * 255.0F)) )
2395 | (((GLuint) (green[i] * 255.0F)) << 8)
2396 | (((GLuint) (red[i] * 255.0F)) << 16)
2397 | (((GLuint) (alpha[i] * 255.0F)) << 24);
2398 }
2399 }
2400 else if (format == GL_ABGR_EXT) {
2401 GLuint *dst = (GLuint *) destination;
2402 for (i=0;i<n;i++) {
2403 dst[i] = (((GLuint) (alpha[i] * 255.0F)) )
2404 | (((GLuint) (blue[i] * 255.0F)) << 8)
2405 | (((GLuint) (green[i] * 255.0F)) << 16)
2406 | (((GLuint) (red[i] * 255.0F)) << 24);
2407 }
2408 }
2409 break;
2410 case GL_UNSIGNED_INT_10_10_10_2:
2411 if (format == GL_RGBA) {
2412 GLuint *dst = (GLuint *) destination;
2413 for (i=0;i<n;i++) {
2414 dst[i] = (((GLuint) (red[i] * 1023.0F)) << 22)
2415 | (((GLuint) (green[i] * 1023.0F)) << 12)
2416 | (((GLuint) (blue[i] * 1023.0F)) << 2)
2417 | (((GLuint) (alpha[i] * 3.0F)) );
2418 }
2419 }
2420 else if (format == GL_BGRA) {
2421 GLuint *dst = (GLuint *) destination;
2422 for (i=0;i<n;i++) {
2423 dst[i] = (((GLuint) (blue[i] * 1023.0F)) << 22)
2424 | (((GLuint) (green[i] * 1023.0F)) << 12)
2425 | (((GLuint) (red[i] * 1023.0F)) << 2)
2426 | (((GLuint) (alpha[i] * 3.0F)) );
2427 }
2428 }
2429 else if (format == GL_ABGR_EXT) {
2430 GLuint *dst = (GLuint *) destination;
2431 for (i=0;i<n;i++) {
2432 dst[i] = (((GLuint) (alpha[i] * 1023.0F)) << 22)
2433 | (((GLuint) (blue[i] * 1023.0F)) << 12)
2434 | (((GLuint) (green[i] * 1023.0F)) << 2)
2435 | (((GLuint) (red[i] * 3.0F)) );
2436 }
2437 }
2438 break;
2439 case GL_UNSIGNED_INT_2_10_10_10_REV:
2440 if (format == GL_RGBA) {
2441 GLuint *dst = (GLuint *) destination;
2442 for (i=0;i<n;i++) {
2443 dst[i] = (((GLuint) (red[i] * 1023.0F)) )
2444 | (((GLuint) (green[i] * 1023.0F)) << 10)
2445 | (((GLuint) (blue[i] * 1023.0F)) << 20)
2446 | (((GLuint) (alpha[i] * 3.0F)) << 30);
2447 }
2448 }
2449 else if (format == GL_BGRA) {
2450 GLuint *dst = (GLuint *) destination;
2451 for (i=0;i<n;i++) {
2452 dst[i] = (((GLuint) (blue[i] * 1023.0F)) )
2453 | (((GLuint) (green[i] * 1023.0F)) << 10)
2454 | (((GLuint) (red[i] * 1023.0F)) << 20)
2455 | (((GLuint) (alpha[i] * 3.0F)) << 30);
2456 }
2457 }
2458 else if (format == GL_ABGR_EXT) {
2459 GLuint *dst = (GLuint *) destination;
2460 for (i=0;i<n;i++) {
2461 dst[i] = (((GLuint) (alpha[i] * 1023.0F)) )
2462 | (((GLuint) (blue[i] * 1023.0F)) << 10)
2463 | (((GLuint) (green[i] * 1023.0F)) << 20)
2464 | (((GLuint) (red[i] * 3.0F)) << 30);
2465 }
2466 }
2467 break;
2468 default:
2469 gl_problem( ctx, "bad type in gl_pack_rgba_span" );
2470 }
2471 }
2472 }