added optimized GL_RGB, GL_UNSIGNED_BYTE case to gl_pack_rgba_span)
[mesa.git] / src / mesa / main / image.c
1 /* $Id: image.c,v 1.8 1999/10/22 10:59:15 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[i+0] = rgba[i][RCOMP];
1696 dest[i+1] = rgba[i][GCOMP];
1697 dest[i+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 GLfloat rscale = 1.0F / 255.0F;
1705 GLfloat gscale = 1.0F / 255.0F;
1706 GLfloat bscale = 1.0F / 255.0F;
1707 GLfloat ascale = 1.0F / 255.0F;
1708 GLuint i;
1709
1710 assert( n < MAX_WIDTH );
1711
1712 /* convert color components to floating point */
1713 for (i=0;i<n;i++) {
1714 red[i] = rgba[i][RCOMP] * rscale;
1715 green[i] = rgba[i][GCOMP] * gscale;
1716 blue[i] = rgba[i][BCOMP] * bscale;
1717 alpha[i] = rgba[i][ACOMP] * ascale;
1718 }
1719
1720 /*
1721 * Apply scale, bias and lookup-tables if enabled.
1722 */
1723 if (applyTransferOps) {
1724 if (ctx->Pixel.ScaleOrBiasRGBA) {
1725 gl_scale_and_bias_color( ctx, n, red, green, blue, alpha );
1726 }
1727 if (ctx->Pixel.MapColorFlag) {
1728 gl_map_color( ctx, n, red, green, blue, alpha );
1729 }
1730 }
1731
1732 if (format==GL_LUMINANCE || format==GL_LUMINANCE_ALPHA) {
1733 for (i=0;i<n;i++) {
1734 GLfloat sum = red[i] + green[i] + blue[i];
1735 luminance[i] = CLAMP( sum, 0.0F, 1.0F );
1736 }
1737 }
1738
1739 /*
1740 * Pack/store the pixels. Ugh! Lots of cases!!!
1741 */
1742 switch (type) {
1743 case GL_UNSIGNED_BYTE:
1744 {
1745 GLubyte *dst = (GLubyte *) destination;
1746 switch (format) {
1747 case GL_RED:
1748 for (i=0;i<n;i++)
1749 dst[i] = FLOAT_TO_UBYTE(red[i]);
1750 break;
1751 case GL_GREEN:
1752 for (i=0;i<n;i++)
1753 dst[i] = FLOAT_TO_UBYTE(green[i]);
1754 break;
1755 case GL_BLUE:
1756 for (i=0;i<n;i++)
1757 dst[i] = FLOAT_TO_UBYTE(blue[i]);
1758 break;
1759 case GL_ALPHA:
1760 for (i=0;i<n;i++)
1761 dst[i] = FLOAT_TO_UBYTE(alpha[i]);
1762 break;
1763 case GL_LUMINANCE:
1764 for (i=0;i<n;i++)
1765 dst[i] = FLOAT_TO_UBYTE(luminance[i]);
1766 break;
1767 case GL_LUMINANCE_ALPHA:
1768 for (i=0;i<n;i++) {
1769 dst[i*2+0] = FLOAT_TO_UBYTE(luminance[i]);
1770 dst[i*2+1] = FLOAT_TO_UBYTE(alpha[i]);
1771 }
1772 break;
1773 case GL_RGB:
1774 for (i=0;i<n;i++) {
1775 dst[i*3+0] = FLOAT_TO_UBYTE(red[i]);
1776 dst[i*3+1] = FLOAT_TO_UBYTE(green[i]);
1777 dst[i*3+2] = FLOAT_TO_UBYTE(blue[i]);
1778 }
1779 break;
1780 case GL_RGBA:
1781 for (i=0;i<n;i++) {
1782 dst[i*4+0] = FLOAT_TO_UBYTE(red[i]);
1783 dst[i*4+1] = FLOAT_TO_UBYTE(green[i]);
1784 dst[i*4+2] = FLOAT_TO_UBYTE(blue[i]);
1785 dst[i*4+3] = FLOAT_TO_UBYTE(alpha[i]);
1786 }
1787 break;
1788 case GL_BGR:
1789 for (i=0;i<n;i++) {
1790 dst[i*3+0] = FLOAT_TO_UBYTE(blue[i]);
1791 dst[i*3+1] = FLOAT_TO_UBYTE(green[i]);
1792 dst[i*3+2] = FLOAT_TO_UBYTE(red[i]);
1793 }
1794 break;
1795 case GL_BGRA:
1796 for (i=0;i<n;i++) {
1797 dst[i*4+0] = FLOAT_TO_UBYTE(blue[i]);
1798 dst[i*4+1] = FLOAT_TO_UBYTE(green[i]);
1799 dst[i*4+2] = FLOAT_TO_UBYTE(red[i]);
1800 dst[i*4+3] = FLOAT_TO_UBYTE(alpha[i]);
1801 }
1802 break;
1803 case GL_ABGR_EXT:
1804 for (i=0;i<n;i++) {
1805 dst[i*4+0] = FLOAT_TO_UBYTE(alpha[i]);
1806 dst[i*4+1] = FLOAT_TO_UBYTE(blue[i]);
1807 dst[i*4+2] = FLOAT_TO_UBYTE(green[i]);
1808 dst[i*4+3] = FLOAT_TO_UBYTE(red[i]);
1809 }
1810 break;
1811 default:
1812 gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
1813 }
1814 }
1815 break;
1816 case GL_BYTE:
1817 {
1818 GLbyte *dst = (GLbyte *) destination;
1819 switch (format) {
1820 case GL_RED:
1821 for (i=0;i<n;i++)
1822 dst[i] = FLOAT_TO_BYTE(red[i]);
1823 break;
1824 case GL_GREEN:
1825 for (i=0;i<n;i++)
1826 dst[i] = FLOAT_TO_BYTE(green[i]);
1827 break;
1828 case GL_BLUE:
1829 for (i=0;i<n;i++)
1830 dst[i] = FLOAT_TO_BYTE(blue[i]);
1831 break;
1832 case GL_ALPHA:
1833 for (i=0;i<n;i++)
1834 dst[i] = FLOAT_TO_BYTE(alpha[i]);
1835 break;
1836 case GL_LUMINANCE:
1837 for (i=0;i<n;i++)
1838 dst[i] = FLOAT_TO_BYTE(luminance[i]);
1839 break;
1840 case GL_LUMINANCE_ALPHA:
1841 for (i=0;i<n;i++) {
1842 dst[i*2+0] = FLOAT_TO_BYTE(luminance[i]);
1843 dst[i*2+1] = FLOAT_TO_BYTE(alpha[i]);
1844 }
1845 break;
1846 case GL_RGB:
1847 for (i=0;i<n;i++) {
1848 dst[i*3+0] = FLOAT_TO_BYTE(red[i]);
1849 dst[i*3+1] = FLOAT_TO_BYTE(green[i]);
1850 dst[i*3+2] = FLOAT_TO_BYTE(blue[i]);
1851 }
1852 break;
1853 case GL_RGBA:
1854 for (i=0;i<n;i++) {
1855 dst[i*4+0] = FLOAT_TO_BYTE(red[i]);
1856 dst[i*4+1] = FLOAT_TO_BYTE(green[i]);
1857 dst[i*4+2] = FLOAT_TO_BYTE(blue[i]);
1858 dst[i*4+3] = FLOAT_TO_BYTE(alpha[i]);
1859 }
1860 break;
1861 case GL_BGR:
1862 for (i=0;i<n;i++) {
1863 dst[i*3+0] = FLOAT_TO_BYTE(blue[i]);
1864 dst[i*3+1] = FLOAT_TO_BYTE(green[i]);
1865 dst[i*3+2] = FLOAT_TO_BYTE(red[i]);
1866 }
1867 break;
1868 case GL_BGRA:
1869 for (i=0;i<n;i++) {
1870 dst[i*4+0] = FLOAT_TO_BYTE(blue[i]);
1871 dst[i*4+1] = FLOAT_TO_BYTE(green[i]);
1872 dst[i*4+2] = FLOAT_TO_BYTE(red[i]);
1873 dst[i*4+3] = FLOAT_TO_BYTE(alpha[i]);
1874 }
1875 case GL_ABGR_EXT:
1876 for (i=0;i<n;i++) {
1877 dst[i*4+0] = FLOAT_TO_BYTE(alpha[i]);
1878 dst[i*4+1] = FLOAT_TO_BYTE(blue[i]);
1879 dst[i*4+2] = FLOAT_TO_BYTE(green[i]);
1880 dst[i*4+3] = FLOAT_TO_BYTE(red[i]);
1881 }
1882 break;
1883 default:
1884 gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
1885 }
1886 }
1887 break;
1888 case GL_UNSIGNED_SHORT:
1889 {
1890 GLushort *dst = (GLushort *) destination;
1891 switch (format) {
1892 case GL_RED:
1893 for (i=0;i<n;i++)
1894 dst[i] = FLOAT_TO_USHORT(red[i]);
1895 break;
1896 case GL_GREEN:
1897 for (i=0;i<n;i++)
1898 dst[i] = FLOAT_TO_USHORT(green[i]);
1899 break;
1900 case GL_BLUE:
1901 for (i=0;i<n;i++)
1902 dst[i] = FLOAT_TO_USHORT(blue[i]);
1903 break;
1904 case GL_ALPHA:
1905 for (i=0;i<n;i++)
1906 dst[i] = FLOAT_TO_USHORT(alpha[i]);
1907 break;
1908 case GL_LUMINANCE:
1909 for (i=0;i<n;i++)
1910 dst[i] = FLOAT_TO_USHORT(luminance[i]);
1911 break;
1912 case GL_LUMINANCE_ALPHA:
1913 for (i=0;i<n;i++) {
1914 dst[i*2+0] = FLOAT_TO_USHORT(luminance[i]);
1915 dst[i*2+1] = FLOAT_TO_USHORT(alpha[i]);
1916 }
1917 break;
1918 case GL_RGB:
1919 for (i=0;i<n;i++) {
1920 dst[i*3+0] = FLOAT_TO_USHORT(red[i]);
1921 dst[i*3+1] = FLOAT_TO_USHORT(green[i]);
1922 dst[i*3+2] = FLOAT_TO_USHORT(blue[i]);
1923 }
1924 break;
1925 case GL_RGBA:
1926 for (i=0;i<n;i++) {
1927 dst[i*4+0] = FLOAT_TO_USHORT(red[i]);
1928 dst[i*4+1] = FLOAT_TO_USHORT(green[i]);
1929 dst[i*4+2] = FLOAT_TO_USHORT(blue[i]);
1930 dst[i*4+3] = FLOAT_TO_USHORT(alpha[i]);
1931 }
1932 break;
1933 case GL_BGR:
1934 for (i=0;i<n;i++) {
1935 dst[i*3+0] = FLOAT_TO_USHORT(blue[i]);
1936 dst[i*3+1] = FLOAT_TO_USHORT(green[i]);
1937 dst[i*3+2] = FLOAT_TO_USHORT(red[i]);
1938 }
1939 break;
1940 case GL_BGRA:
1941 for (i=0;i<n;i++) {
1942 dst[i*4+0] = FLOAT_TO_USHORT(blue[i]);
1943 dst[i*4+1] = FLOAT_TO_USHORT(green[i]);
1944 dst[i*4+2] = FLOAT_TO_USHORT(red[i]);
1945 dst[i*4+3] = FLOAT_TO_USHORT(alpha[i]);
1946 }
1947 break;
1948 case GL_ABGR_EXT:
1949 for (i=0;i<n;i++) {
1950 dst[i*4+0] = FLOAT_TO_USHORT(alpha[i]);
1951 dst[i*4+1] = FLOAT_TO_USHORT(blue[i]);
1952 dst[i*4+2] = FLOAT_TO_USHORT(green[i]);
1953 dst[i*4+3] = FLOAT_TO_USHORT(red[i]);
1954 }
1955 break;
1956 default:
1957 gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
1958 }
1959 if (packing->SwapBytes) {
1960 gl_swap2( (GLushort *) dst, n );
1961 }
1962 }
1963 break;
1964 case GL_SHORT:
1965 {
1966 GLshort *dst = (GLshort *) destination;
1967 switch (format) {
1968 case GL_RED:
1969 for (i=0;i<n;i++)
1970 dst[i] = FLOAT_TO_SHORT(red[i]);
1971 break;
1972 case GL_GREEN:
1973 for (i=0;i<n;i++)
1974 dst[i] = FLOAT_TO_SHORT(green[i]);
1975 break;
1976 case GL_BLUE:
1977 for (i=0;i<n;i++)
1978 dst[i] = FLOAT_TO_SHORT(blue[i]);
1979 break;
1980 case GL_ALPHA:
1981 for (i=0;i<n;i++)
1982 dst[i] = FLOAT_TO_SHORT(alpha[i]);
1983 break;
1984 case GL_LUMINANCE:
1985 for (i=0;i<n;i++)
1986 dst[i] = FLOAT_TO_SHORT(luminance[i]);
1987 break;
1988 case GL_LUMINANCE_ALPHA:
1989 for (i=0;i<n;i++) {
1990 dst[i*2+0] = FLOAT_TO_SHORT(luminance[i]);
1991 dst[i*2+1] = FLOAT_TO_SHORT(alpha[i]);
1992 }
1993 break;
1994 case GL_RGB:
1995 for (i=0;i<n;i++) {
1996 dst[i*3+0] = FLOAT_TO_SHORT(red[i]);
1997 dst[i*3+1] = FLOAT_TO_SHORT(green[i]);
1998 dst[i*3+2] = FLOAT_TO_SHORT(blue[i]);
1999 }
2000 break;
2001 case GL_RGBA:
2002 for (i=0;i<n;i++) {
2003 dst[i*4+0] = FLOAT_TO_SHORT(red[i]);
2004 dst[i*4+1] = FLOAT_TO_SHORT(green[i]);
2005 dst[i*4+2] = FLOAT_TO_SHORT(blue[i]);
2006 dst[i*4+3] = FLOAT_TO_SHORT(alpha[i]);
2007 }
2008 break;
2009 case GL_BGR:
2010 for (i=0;i<n;i++) {
2011 dst[i*3+0] = FLOAT_TO_SHORT(blue[i]);
2012 dst[i*3+1] = FLOAT_TO_SHORT(green[i]);
2013 dst[i*3+2] = FLOAT_TO_SHORT(red[i]);
2014 }
2015 break;
2016 case GL_BGRA:
2017 for (i=0;i<n;i++) {
2018 dst[i*4+0] = FLOAT_TO_SHORT(blue[i]);
2019 dst[i*4+1] = FLOAT_TO_SHORT(green[i]);
2020 dst[i*4+2] = FLOAT_TO_SHORT(red[i]);
2021 dst[i*4+3] = FLOAT_TO_SHORT(alpha[i]);
2022 }
2023 case GL_ABGR_EXT:
2024 for (i=0;i<n;i++) {
2025 dst[i*4+0] = FLOAT_TO_SHORT(alpha[i]);
2026 dst[i*4+1] = FLOAT_TO_SHORT(blue[i]);
2027 dst[i*4+2] = FLOAT_TO_SHORT(green[i]);
2028 dst[i*4+3] = FLOAT_TO_SHORT(red[i]);
2029 }
2030 break;
2031 default:
2032 gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
2033 }
2034 if (packing->SwapBytes) {
2035 gl_swap2( (GLushort *) dst, n );
2036 }
2037 }
2038 break;
2039 case GL_UNSIGNED_INT:
2040 {
2041 GLuint *dst = (GLuint *) destination;
2042 switch (format) {
2043 case GL_RED:
2044 for (i=0;i<n;i++)
2045 dst[i] = FLOAT_TO_UINT(red[i]);
2046 break;
2047 case GL_GREEN:
2048 for (i=0;i<n;i++)
2049 dst[i] = FLOAT_TO_UINT(green[i]);
2050 break;
2051 case GL_BLUE:
2052 for (i=0;i<n;i++)
2053 dst[i] = FLOAT_TO_UINT(blue[i]);
2054 break;
2055 case GL_ALPHA:
2056 for (i=0;i<n;i++)
2057 dst[i] = FLOAT_TO_UINT(alpha[i]);
2058 break;
2059 case GL_LUMINANCE:
2060 for (i=0;i<n;i++)
2061 dst[i] = FLOAT_TO_UINT(luminance[i]);
2062 break;
2063 case GL_LUMINANCE_ALPHA:
2064 for (i=0;i<n;i++) {
2065 dst[i*2+0] = FLOAT_TO_UINT(luminance[i]);
2066 dst[i*2+1] = FLOAT_TO_UINT(alpha[i]);
2067 }
2068 break;
2069 case GL_RGB:
2070 for (i=0;i<n;i++) {
2071 dst[i*3+0] = FLOAT_TO_UINT(red[i]);
2072 dst[i*3+1] = FLOAT_TO_UINT(green[i]);
2073 dst[i*3+2] = FLOAT_TO_UINT(blue[i]);
2074 }
2075 break;
2076 case GL_RGBA:
2077 for (i=0;i<n;i++) {
2078 dst[i*4+0] = FLOAT_TO_UINT(red[i]);
2079 dst[i*4+1] = FLOAT_TO_UINT(green[i]);
2080 dst[i*4+2] = FLOAT_TO_UINT(blue[i]);
2081 dst[i*4+3] = FLOAT_TO_UINT(alpha[i]);
2082 }
2083 break;
2084 case GL_BGR:
2085 for (i=0;i<n;i++) {
2086 dst[i*3+0] = FLOAT_TO_UINT(blue[i]);
2087 dst[i*3+1] = FLOAT_TO_UINT(green[i]);
2088 dst[i*3+2] = FLOAT_TO_UINT(red[i]);
2089 }
2090 break;
2091 case GL_BGRA:
2092 for (i=0;i<n;i++) {
2093 dst[i*4+0] = FLOAT_TO_UINT(blue[i]);
2094 dst[i*4+1] = FLOAT_TO_UINT(green[i]);
2095 dst[i*4+2] = FLOAT_TO_UINT(red[i]);
2096 dst[i*4+3] = FLOAT_TO_UINT(alpha[i]);
2097 }
2098 break;
2099 case GL_ABGR_EXT:
2100 for (i=0;i<n;i++) {
2101 dst[i*4+0] = FLOAT_TO_UINT(alpha[i]);
2102 dst[i*4+1] = FLOAT_TO_UINT(blue[i]);
2103 dst[i*4+2] = FLOAT_TO_UINT(green[i]);
2104 dst[i*4+3] = FLOAT_TO_UINT(red[i]);
2105 }
2106 break;
2107 default:
2108 gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
2109 }
2110 if (packing->SwapBytes) {
2111 gl_swap4( (GLuint *) dst, n );
2112 }
2113 }
2114 break;
2115 case GL_INT:
2116 {
2117 GLint *dst = (GLint *) destination;
2118 switch (format) {
2119 case GL_RED:
2120 for (i=0;i<n;i++)
2121 dst[i] = FLOAT_TO_INT(red[i]);
2122 break;
2123 case GL_GREEN:
2124 for (i=0;i<n;i++)
2125 dst[i] = FLOAT_TO_INT(green[i]);
2126 break;
2127 case GL_BLUE:
2128 for (i=0;i<n;i++)
2129 dst[i] = FLOAT_TO_INT(blue[i]);
2130 break;
2131 case GL_ALPHA:
2132 for (i=0;i<n;i++)
2133 dst[i] = FLOAT_TO_INT(alpha[i]);
2134 break;
2135 case GL_LUMINANCE:
2136 for (i=0;i<n;i++)
2137 dst[i] = FLOAT_TO_INT(luminance[i]);
2138 break;
2139 case GL_LUMINANCE_ALPHA:
2140 for (i=0;i<n;i++) {
2141 dst[i*2+0] = FLOAT_TO_INT(luminance[i]);
2142 dst[i*2+1] = FLOAT_TO_INT(alpha[i]);
2143 }
2144 break;
2145 case GL_RGB:
2146 for (i=0;i<n;i++) {
2147 dst[i*3+0] = FLOAT_TO_INT(red[i]);
2148 dst[i*3+1] = FLOAT_TO_INT(green[i]);
2149 dst[i*3+2] = FLOAT_TO_INT(blue[i]);
2150 }
2151 break;
2152 case GL_RGBA:
2153 for (i=0;i<n;i++) {
2154 dst[i*4+0] = FLOAT_TO_INT(red[i]);
2155 dst[i*4+1] = FLOAT_TO_INT(green[i]);
2156 dst[i*4+2] = FLOAT_TO_INT(blue[i]);
2157 dst[i*4+3] = FLOAT_TO_INT(alpha[i]);
2158 }
2159 break;
2160 case GL_BGR:
2161 for (i=0;i<n;i++) {
2162 dst[i*3+0] = FLOAT_TO_INT(blue[i]);
2163 dst[i*3+1] = FLOAT_TO_INT(green[i]);
2164 dst[i*3+2] = FLOAT_TO_INT(red[i]);
2165 }
2166 break;
2167 case GL_BGRA:
2168 for (i=0;i<n;i++) {
2169 dst[i*4+0] = FLOAT_TO_INT(blue[i]);
2170 dst[i*4+1] = FLOAT_TO_INT(green[i]);
2171 dst[i*4+2] = FLOAT_TO_INT(red[i]);
2172 dst[i*4+3] = FLOAT_TO_INT(alpha[i]);
2173 }
2174 break;
2175 case GL_ABGR_EXT:
2176 for (i=0;i<n;i++) {
2177 dst[i*4+0] = FLOAT_TO_INT(alpha[i]);
2178 dst[i*4+1] = FLOAT_TO_INT(blue[i]);
2179 dst[i*4+2] = FLOAT_TO_INT(green[i]);
2180 dst[i*4+3] = FLOAT_TO_INT(red[i]);
2181 }
2182 break;
2183 default:
2184 gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
2185 }
2186 if (packing->SwapBytes) {
2187 gl_swap4( (GLuint *) dst, n );
2188 }
2189 }
2190 break;
2191 case GL_FLOAT:
2192 {
2193 GLfloat *dst = (GLfloat *) destination;
2194 switch (format) {
2195 case GL_RED:
2196 for (i=0;i<n;i++)
2197 dst[i] = red[i];
2198 break;
2199 case GL_GREEN:
2200 for (i=0;i<n;i++)
2201 dst[i] = green[i];
2202 break;
2203 case GL_BLUE:
2204 for (i=0;i<n;i++)
2205 dst[i] = blue[i];
2206 break;
2207 case GL_ALPHA:
2208 for (i=0;i<n;i++)
2209 dst[i] = alpha[i];
2210 break;
2211 case GL_LUMINANCE:
2212 for (i=0;i<n;i++)
2213 dst[i] = luminance[i];
2214 break;
2215 case GL_LUMINANCE_ALPHA:
2216 for (i=0;i<n;i++) {
2217 dst[i*2+0] = luminance[i];
2218 dst[i*2+1] = alpha[i];
2219 }
2220 break;
2221 case GL_RGB:
2222 for (i=0;i<n;i++) {
2223 dst[i*3+0] = red[i];
2224 dst[i*3+1] = green[i];
2225 dst[i*3+2] = blue[i];
2226 }
2227 break;
2228 case GL_RGBA:
2229 for (i=0;i<n;i++) {
2230 dst[i*4+0] = red[i];
2231 dst[i*4+1] = green[i];
2232 dst[i*4+2] = blue[i];
2233 dst[i*4+3] = alpha[i];
2234 }
2235 break;
2236 case GL_BGR:
2237 for (i=0;i<n;i++) {
2238 dst[i*3+0] = blue[i];
2239 dst[i*3+1] = green[i];
2240 dst[i*3+2] = red[i];
2241 }
2242 break;
2243 case GL_BGRA:
2244 for (i=0;i<n;i++) {
2245 dst[i*4+0] = blue[i];
2246 dst[i*4+1] = green[i];
2247 dst[i*4+2] = red[i];
2248 dst[i*4+3] = alpha[i];
2249 }
2250 break;
2251 case GL_ABGR_EXT:
2252 for (i=0;i<n;i++) {
2253 dst[i*4+0] = alpha[i];
2254 dst[i*4+1] = blue[i];
2255 dst[i*4+2] = green[i];
2256 dst[i*4+3] = red[i];
2257 }
2258 break;
2259 default:
2260 gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
2261 }
2262 if (packing->SwapBytes) {
2263 gl_swap4( (GLuint *) dst, n );
2264 }
2265 }
2266 break;
2267 case GL_UNSIGNED_BYTE_3_3_2:
2268 if (format == GL_RGB) {
2269 GLubyte *dst = (GLubyte *) destination;
2270 for (i=0;i<n;i++) {
2271 dst[i] = (((GLint) (red[i] * 7.0F)) << 5)
2272 | (((GLint) (green[i] * 7.0F)) << 2)
2273 | (((GLint) (blue[i] * 3.0F)) );
2274 }
2275 }
2276 break;
2277 case GL_UNSIGNED_BYTE_2_3_3_REV:
2278 if (format == GL_RGB) {
2279 GLubyte *dst = (GLubyte *) destination;
2280 for (i=0;i<n;i++) {
2281 dst[i] = (((GLint) (red[i] * 7.0F)) )
2282 | (((GLint) (green[i] * 7.0F)) << 3)
2283 | (((GLint) (blue[i] * 3.0F)) << 5);
2284 }
2285 }
2286 break;
2287 case GL_UNSIGNED_SHORT_5_6_5:
2288 if (format == GL_RGB) {
2289 GLushort *dst = (GLushort *) destination;
2290 for (i=0;i<n;i++) {
2291 dst[i] = (((GLint) (red[i] * 31.0F)) << 11)
2292 | (((GLint) (green[i] * 63.0F)) << 5)
2293 | (((GLint) (blue[i] * 31.0F)) );
2294 }
2295 }
2296 break;
2297 case GL_UNSIGNED_SHORT_5_6_5_REV:
2298 if (format == GL_RGB) {
2299 GLushort *dst = (GLushort *) destination;
2300 for (i=0;i<n;i++) {
2301 dst[i] = (((GLint) (red[i] * 31.0F)) )
2302 | (((GLint) (green[i] * 63.0F)) << 5)
2303 | (((GLint) (blue[i] * 31.0F)) << 11);
2304 }
2305 }
2306 break;
2307 case GL_UNSIGNED_SHORT_4_4_4_4:
2308 if (format == GL_RGB) {
2309 GLushort *dst = (GLushort *) destination;
2310 for (i=0;i<n;i++) {
2311 dst[i] = (((GLint) (red[i] * 15.0F)) << 12)
2312 | (((GLint) (green[i] * 15.0F)) << 8)
2313 | (((GLint) (blue[i] * 15.0F)) << 4)
2314 | (((GLint) (alpha[i] * 15.0F)) );
2315 }
2316 }
2317 break;
2318 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
2319 if (format == GL_RGB) {
2320 GLushort *dst = (GLushort *) destination;
2321 for (i=0;i<n;i++) {
2322 dst[i] = (((GLint) (red[i] * 15.0F)) )
2323 | (((GLint) (green[i] * 15.0F)) << 4)
2324 | (((GLint) (blue[i] * 15.0F)) << 8)
2325 | (((GLint) (alpha[i] * 15.0F)) << 12);
2326 }
2327 }
2328 break;
2329 case GL_UNSIGNED_SHORT_5_5_5_1:
2330 if (format == GL_RGB) {
2331 GLushort *dst = (GLushort *) destination;
2332 for (i=0;i<n;i++) {
2333 dst[i] = (((GLint) (red[i] * 31.0F)) << 11)
2334 | (((GLint) (green[i] * 31.0F)) << 6)
2335 | (((GLint) (blue[i] * 31.0F)) << 1)
2336 | (((GLint) (alpha[i] * 1.0F)) );
2337 }
2338 }
2339 break;
2340 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
2341 if (format == GL_RGB) {
2342 GLushort *dst = (GLushort *) destination;
2343 for (i=0;i<n;i++) {
2344 dst[i] = (((GLint) (red[i] * 31.0F)) )
2345 | (((GLint) (green[i] * 31.0F)) << 5)
2346 | (((GLint) (blue[i] * 31.0F)) << 10)
2347 | (((GLint) (alpha[i] * 1.0F)) << 15);
2348 }
2349 }
2350 break;
2351 case GL_UNSIGNED_INT_8_8_8_8:
2352 if (format == GL_RGBA) {
2353 GLuint *dst = (GLuint *) destination;
2354 for (i=0;i<n;i++) {
2355 dst[i] = (((GLuint) (red[i] * 255.0F)) << 24)
2356 | (((GLuint) (green[i] * 255.0F)) << 16)
2357 | (((GLuint) (blue[i] * 255.0F)) << 8)
2358 | (((GLuint) (alpha[i] * 255.0F)) );
2359 }
2360 }
2361 else if (format == GL_BGRA) {
2362 GLuint *dst = (GLuint *) destination;
2363 for (i=0;i<n;i++) {
2364 dst[i] = (((GLuint) (blue[i] * 255.0F)) << 24)
2365 | (((GLuint) (green[i] * 255.0F)) << 16)
2366 | (((GLuint) (red[i] * 255.0F)) << 8)
2367 | (((GLuint) (alpha[i] * 255.0F)) );
2368 }
2369 }
2370 else if (format == GL_ABGR_EXT) {
2371 GLuint *dst = (GLuint *) destination;
2372 for (i=0;i<n;i++) {
2373 dst[i] = (((GLuint) (alpha[i] * 255.0F)) << 24)
2374 | (((GLuint) (blue[i] * 255.0F)) << 16)
2375 | (((GLuint) (green[i] * 255.0F)) << 8)
2376 | (((GLuint) (red[i] * 255.0F)) );
2377 }
2378 }
2379 break;
2380 case GL_UNSIGNED_INT_8_8_8_8_REV:
2381 if (format == GL_RGBA) {
2382 GLuint *dst = (GLuint *) destination;
2383 for (i=0;i<n;i++) {
2384 dst[i] = (((GLuint) (red[i] * 255.0F)) )
2385 | (((GLuint) (green[i] * 255.0F)) << 8)
2386 | (((GLuint) (blue[i] * 255.0F)) << 16)
2387 | (((GLuint) (alpha[i] * 255.0F)) << 24);
2388 }
2389 }
2390 else if (format == GL_BGRA) {
2391 GLuint *dst = (GLuint *) destination;
2392 for (i=0;i<n;i++) {
2393 dst[i] = (((GLuint) (blue[i] * 255.0F)) )
2394 | (((GLuint) (green[i] * 255.0F)) << 8)
2395 | (((GLuint) (red[i] * 255.0F)) << 16)
2396 | (((GLuint) (alpha[i] * 255.0F)) << 24);
2397 }
2398 }
2399 else if (format == GL_ABGR_EXT) {
2400 GLuint *dst = (GLuint *) destination;
2401 for (i=0;i<n;i++) {
2402 dst[i] = (((GLuint) (alpha[i] * 255.0F)) )
2403 | (((GLuint) (blue[i] * 255.0F)) << 8)
2404 | (((GLuint) (green[i] * 255.0F)) << 16)
2405 | (((GLuint) (red[i] * 255.0F)) << 24);
2406 }
2407 }
2408 break;
2409 case GL_UNSIGNED_INT_10_10_10_2:
2410 if (format == GL_RGBA) {
2411 GLuint *dst = (GLuint *) destination;
2412 for (i=0;i<n;i++) {
2413 dst[i] = (((GLuint) (red[i] * 1023.0F)) << 22)
2414 | (((GLuint) (green[i] * 1023.0F)) << 12)
2415 | (((GLuint) (blue[i] * 1023.0F)) << 2)
2416 | (((GLuint) (alpha[i] * 3.0F)) );
2417 }
2418 }
2419 else if (format == GL_BGRA) {
2420 GLuint *dst = (GLuint *) destination;
2421 for (i=0;i<n;i++) {
2422 dst[i] = (((GLuint) (blue[i] * 1023.0F)) << 22)
2423 | (((GLuint) (green[i] * 1023.0F)) << 12)
2424 | (((GLuint) (red[i] * 1023.0F)) << 2)
2425 | (((GLuint) (alpha[i] * 3.0F)) );
2426 }
2427 }
2428 else if (format == GL_ABGR_EXT) {
2429 GLuint *dst = (GLuint *) destination;
2430 for (i=0;i<n;i++) {
2431 dst[i] = (((GLuint) (alpha[i] * 1023.0F)) << 22)
2432 | (((GLuint) (blue[i] * 1023.0F)) << 12)
2433 | (((GLuint) (green[i] * 1023.0F)) << 2)
2434 | (((GLuint) (red[i] * 3.0F)) );
2435 }
2436 }
2437 break;
2438 case GL_UNSIGNED_INT_2_10_10_10_REV:
2439 if (format == GL_RGBA) {
2440 GLuint *dst = (GLuint *) destination;
2441 for (i=0;i<n;i++) {
2442 dst[i] = (((GLuint) (red[i] * 1023.0F)) )
2443 | (((GLuint) (green[i] * 1023.0F)) << 10)
2444 | (((GLuint) (blue[i] * 1023.0F)) << 20)
2445 | (((GLuint) (alpha[i] * 3.0F)) << 30);
2446 }
2447 }
2448 else if (format == GL_BGRA) {
2449 GLuint *dst = (GLuint *) destination;
2450 for (i=0;i<n;i++) {
2451 dst[i] = (((GLuint) (blue[i] * 1023.0F)) )
2452 | (((GLuint) (green[i] * 1023.0F)) << 10)
2453 | (((GLuint) (red[i] * 1023.0F)) << 20)
2454 | (((GLuint) (alpha[i] * 3.0F)) << 30);
2455 }
2456 }
2457 else if (format == GL_ABGR_EXT) {
2458 GLuint *dst = (GLuint *) destination;
2459 for (i=0;i<n;i++) {
2460 dst[i] = (((GLuint) (alpha[i] * 1023.0F)) )
2461 | (((GLuint) (blue[i] * 1023.0F)) << 10)
2462 | (((GLuint) (green[i] * 1023.0F)) << 20)
2463 | (((GLuint) (red[i] * 3.0F)) << 30);
2464 }
2465 }
2466 break;
2467 default:
2468 gl_problem( ctx, "bad type in gl_pack_rgba_span" );
2469 }
2470 }
2471 }