added GL_SGI_color_matrix extension
[mesa.git] / src / mesa / main / image.c
1 /* $Id: image.c,v 1.25 2000/04/08 18:57:45 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.3
6 *
7 * Copyright (C) 1999-2000 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 #ifdef PC_HEADER
29 #include "all.h"
30 #else
31 #include "glheader.h"
32 #include "context.h"
33 #include "image.h"
34 #include "macros.h"
35 #include "mem.h"
36 #include "mmath.h"
37 #include "pixel.h"
38 #include "types.h"
39 #endif
40
41
42
43 /*
44 * These are the image packing parameters for Mesa's internal images.
45 * That is, _mesa_unpack_image() returns image data in this format.
46 * When we execute image commands (glDrawPixels, glTexImage, etc)
47 * from within display lists we have to be sure to set the current
48 * unpacking params to these values!
49 */
50 const struct gl_pixelstore_attrib _mesa_native_packing = {
51 1, /* Alignment */
52 0, /* RowLength */
53 0, /* SkipPixels */
54 0, /* SkipRows */
55 0, /* ImageHeight */
56 0, /* SkipImages */
57 GL_FALSE, /* SwapBytes */
58 GL_FALSE /* LsbFirst */
59 };
60
61
62
63 /*
64 * Flip the 8 bits in each byte of the given array.
65 */
66 static void
67 flip_bytes( GLubyte *p, GLuint n )
68 {
69 register GLuint i, a, b;
70
71 for (i=0;i<n;i++) {
72 b = (GLuint) p[i];
73 a = ((b & 0x01) << 7) |
74 ((b & 0x02) << 5) |
75 ((b & 0x04) << 3) |
76 ((b & 0x08) << 1) |
77 ((b & 0x10) >> 1) |
78 ((b & 0x20) >> 3) |
79 ((b & 0x40) >> 5) |
80 ((b & 0x80) >> 7);
81 p[i] = (GLubyte) a;
82 }
83 }
84
85
86 /*
87 * Flip the order of the 2 bytes in each word in the given array.
88 */
89 void
90 _mesa_swap2( GLushort *p, GLuint n )
91 {
92 register GLuint i;
93
94 for (i=0;i<n;i++) {
95 p[i] = (p[i] >> 8) | ((p[i] << 8) & 0xff00);
96 }
97 }
98
99
100
101 /*
102 * Flip the order of the 4 bytes in each word in the given array.
103 */
104 void
105 _mesa_swap4( GLuint *p, GLuint n )
106 {
107 register GLuint i, a, b;
108
109 for (i=0;i<n;i++) {
110 b = p[i];
111 a = (b >> 24)
112 | ((b >> 8) & 0xff00)
113 | ((b << 8) & 0xff0000)
114 | ((b << 24) & 0xff000000);
115 p[i] = a;
116 }
117 }
118
119
120
121
122 /*
123 * Return the size, in bytes, of the given GL datatype.
124 * Return 0 if GL_BITMAP.
125 * Return -1 if invalid type enum.
126 */
127 GLint _mesa_sizeof_type( GLenum type )
128 {
129 switch (type) {
130 case GL_BITMAP:
131 return 0;
132 case GL_UNSIGNED_BYTE:
133 return sizeof(GLubyte);
134 case GL_BYTE:
135 return sizeof(GLbyte);
136 case GL_UNSIGNED_SHORT:
137 return sizeof(GLushort);
138 case GL_SHORT:
139 return sizeof(GLshort);
140 case GL_UNSIGNED_INT:
141 return sizeof(GLuint);
142 case GL_INT:
143 return sizeof(GLint);
144 case GL_FLOAT:
145 return sizeof(GLfloat);
146 default:
147 return -1;
148 }
149 }
150
151
152 /*
153 * Same as _mesa_sizeof_packed_type() but we also accept the
154 * packed pixel format datatypes.
155 */
156 GLint _mesa_sizeof_packed_type( GLenum type )
157 {
158 switch (type) {
159 case GL_BITMAP:
160 return 0;
161 case GL_UNSIGNED_BYTE:
162 return sizeof(GLubyte);
163 case GL_BYTE:
164 return sizeof(GLbyte);
165 case GL_UNSIGNED_SHORT:
166 return sizeof(GLushort);
167 case GL_SHORT:
168 return sizeof(GLshort);
169 case GL_UNSIGNED_INT:
170 return sizeof(GLuint);
171 case GL_INT:
172 return sizeof(GLint);
173 case GL_FLOAT:
174 return sizeof(GLfloat);
175 case GL_UNSIGNED_BYTE_3_3_2:
176 return sizeof(GLubyte);
177 case GL_UNSIGNED_BYTE_2_3_3_REV:
178 return sizeof(GLubyte);
179 case GL_UNSIGNED_SHORT_5_6_5:
180 return sizeof(GLshort);
181 case GL_UNSIGNED_SHORT_5_6_5_REV:
182 return sizeof(GLshort);
183 case GL_UNSIGNED_SHORT_4_4_4_4:
184 return sizeof(GLshort);
185 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
186 return sizeof(GLshort);
187 case GL_UNSIGNED_SHORT_5_5_5_1:
188 return sizeof(GLshort);
189 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
190 return sizeof(GLshort);
191 case GL_UNSIGNED_INT_8_8_8_8:
192 return sizeof(GLuint);
193 case GL_UNSIGNED_INT_8_8_8_8_REV:
194 return sizeof(GLuint);
195 case GL_UNSIGNED_INT_10_10_10_2:
196 return sizeof(GLuint);
197 case GL_UNSIGNED_INT_2_10_10_10_REV:
198 return sizeof(GLuint);
199 default:
200 return -1;
201 }
202 }
203
204
205
206 /*
207 * Return the number of components in a GL enum pixel type.
208 * Return -1 if bad format.
209 */
210 GLint _mesa_components_in_format( GLenum format )
211 {
212 switch (format) {
213 case GL_COLOR_INDEX:
214 case GL_COLOR_INDEX1_EXT:
215 case GL_COLOR_INDEX2_EXT:
216 case GL_COLOR_INDEX4_EXT:
217 case GL_COLOR_INDEX8_EXT:
218 case GL_COLOR_INDEX12_EXT:
219 case GL_COLOR_INDEX16_EXT:
220 case GL_STENCIL_INDEX:
221 case GL_DEPTH_COMPONENT:
222 case GL_RED:
223 case GL_GREEN:
224 case GL_BLUE:
225 case GL_ALPHA:
226 case GL_LUMINANCE:
227 case GL_INTENSITY:
228 return 1;
229 case GL_LUMINANCE_ALPHA:
230 return 2;
231 case GL_RGB:
232 return 3;
233 case GL_RGBA:
234 return 4;
235 case GL_BGR:
236 return 3;
237 case GL_BGRA:
238 return 4;
239 case GL_ABGR_EXT:
240 return 4;
241 default:
242 return -1;
243 }
244 }
245
246
247 /*
248 * Return bytes per pixel for given format and type
249 * Return -1 if bad format or type.
250 */
251 GLint _mesa_bytes_per_pixel( GLenum format, GLenum type )
252 {
253 GLint comps = _mesa_components_in_format( format );
254 if (comps < 0)
255 return -1;
256
257 switch (type) {
258 case GL_BITMAP:
259 return 0; /* special case */
260 case GL_BYTE:
261 case GL_UNSIGNED_BYTE:
262 return comps * sizeof(GLubyte);
263 case GL_SHORT:
264 case GL_UNSIGNED_SHORT:
265 return comps * sizeof(GLshort);
266 case GL_INT:
267 case GL_UNSIGNED_INT:
268 return comps * sizeof(GLint);
269 case GL_FLOAT:
270 return comps * sizeof(GLfloat);
271 case GL_UNSIGNED_BYTE_3_3_2:
272 case GL_UNSIGNED_BYTE_2_3_3_REV:
273 if (format == GL_RGB || format == GL_BGR)
274 return sizeof(GLubyte);
275 else
276 return -1; /* error */
277 case GL_UNSIGNED_SHORT_5_6_5:
278 case GL_UNSIGNED_SHORT_5_6_5_REV:
279 if (format == GL_RGB || format == GL_BGR)
280 return sizeof(GLshort);
281 else
282 return -1; /* error */
283 case GL_UNSIGNED_SHORT_4_4_4_4:
284 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
285 case GL_UNSIGNED_SHORT_5_5_5_1:
286 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
287 if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT)
288 return sizeof(GLushort);
289 else
290 return -1;
291 case GL_UNSIGNED_INT_8_8_8_8:
292 case GL_UNSIGNED_INT_8_8_8_8_REV:
293 case GL_UNSIGNED_INT_10_10_10_2:
294 case GL_UNSIGNED_INT_2_10_10_10_REV:
295 if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT)
296 return sizeof(GLuint);
297 else
298 return -1;
299 default:
300 return -1;
301 }
302 }
303
304
305 /*
306 * Test if the given pixel format and type are legal.
307 * Return GL_TRUE for legal, GL_FALSE for illegal.
308 */
309 GLboolean
310 _mesa_is_legal_format_and_type( GLenum format, GLenum type )
311 {
312 switch (format) {
313 case GL_COLOR_INDEX:
314 case GL_STENCIL_INDEX:
315 switch (type) {
316 case GL_BITMAP:
317 case GL_BYTE:
318 case GL_UNSIGNED_BYTE:
319 case GL_SHORT:
320 case GL_UNSIGNED_SHORT:
321 case GL_INT:
322 case GL_UNSIGNED_INT:
323 case GL_FLOAT:
324 return GL_TRUE;
325 default:
326 return GL_FALSE;
327 }
328 case GL_RED:
329 case GL_GREEN:
330 case GL_BLUE:
331 case GL_ALPHA:
332 case GL_LUMINANCE:
333 case GL_LUMINANCE_ALPHA:
334 case GL_DEPTH_COMPONENT:
335 case GL_BGR:
336 switch (type) {
337 case GL_BYTE:
338 case GL_UNSIGNED_BYTE:
339 case GL_SHORT:
340 case GL_UNSIGNED_SHORT:
341 case GL_INT:
342 case GL_UNSIGNED_INT:
343 case GL_FLOAT:
344 return GL_TRUE;
345 default:
346 return GL_FALSE;
347 }
348 case GL_RGB:
349 switch (type) {
350 case GL_BYTE:
351 case GL_UNSIGNED_BYTE:
352 case GL_SHORT:
353 case GL_UNSIGNED_SHORT:
354 case GL_INT:
355 case GL_UNSIGNED_INT:
356 case GL_FLOAT:
357 case GL_UNSIGNED_BYTE_3_3_2:
358 case GL_UNSIGNED_BYTE_2_3_3_REV:
359 case GL_UNSIGNED_SHORT_5_6_5:
360 case GL_UNSIGNED_SHORT_5_6_5_REV:
361 return GL_TRUE;
362 default:
363 return GL_FALSE;
364 }
365 case GL_RGBA:
366 case GL_BGRA:
367 case GL_ABGR_EXT:
368 switch (type) {
369 case GL_BYTE:
370 case GL_UNSIGNED_BYTE:
371 case GL_SHORT:
372 case GL_UNSIGNED_SHORT:
373 case GL_INT:
374 case GL_UNSIGNED_INT:
375 case GL_FLOAT:
376 case GL_UNSIGNED_SHORT_4_4_4_4:
377 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
378 case GL_UNSIGNED_SHORT_5_5_5_1:
379 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
380 case GL_UNSIGNED_INT_8_8_8_8:
381 case GL_UNSIGNED_INT_8_8_8_8_REV:
382 case GL_UNSIGNED_INT_10_10_10_2:
383 case GL_UNSIGNED_INT_2_10_10_10_REV:
384 return GL_TRUE;
385 default:
386 return GL_FALSE;
387 }
388 default:
389 ; /* fall-through */
390 }
391 return GL_FALSE;
392 }
393
394
395
396 /*
397 * Return the address of a pixel in an image (actually a volume).
398 * Pixel unpacking/packing parameters are observed according to 'packing'.
399 * Input: image - start of image data
400 * width, height - size of image
401 * format - image format
402 * type - pixel component type
403 * packing - the pixelstore attributes
404 * img - which image in the volume (0 for 1D or 2D images)
405 * row, column - location of pixel in the image
406 * Return: address of pixel at (image,row,column) in image or NULL if error.
407 */
408 GLvoid *
409 _mesa_image_address( const struct gl_pixelstore_attrib *packing,
410 const GLvoid *image, GLsizei width,
411 GLsizei height, GLenum format, GLenum type,
412 GLint img, GLint row, GLint column )
413 {
414 GLint alignment; /* 1, 2 or 4 */
415 GLint pixels_per_row;
416 GLint rows_per_image;
417 GLint skiprows;
418 GLint skippixels;
419 GLint skipimages; /* for 3-D volume images */
420 GLubyte *pixel_addr;
421
422 alignment = packing->Alignment;
423 if (packing->RowLength > 0) {
424 pixels_per_row = packing->RowLength;
425 }
426 else {
427 pixels_per_row = width;
428 }
429 if (packing->ImageHeight > 0) {
430 rows_per_image = packing->ImageHeight;
431 }
432 else {
433 rows_per_image = height;
434 }
435 skiprows = packing->SkipRows;
436 skippixels = packing->SkipPixels;
437 skipimages = packing->SkipImages;
438
439 if (type==GL_BITMAP) {
440 /* BITMAP data */
441 GLint comp_per_pixel; /* components per pixel */
442 GLint bytes_per_comp; /* bytes per component */
443 GLint bytes_per_row;
444 GLint bytes_per_image;
445
446 /* Compute bytes per component */
447 bytes_per_comp = _mesa_sizeof_packed_type( type );
448 if (bytes_per_comp<0) {
449 return NULL;
450 }
451
452 /* Compute number of components per pixel */
453 comp_per_pixel = _mesa_components_in_format( format );
454 if (comp_per_pixel<0 && type != GL_BITMAP) {
455 return NULL;
456 }
457
458 bytes_per_row = alignment
459 * CEILING( comp_per_pixel*pixels_per_row, 8*alignment );
460
461 bytes_per_image = bytes_per_row * rows_per_image;
462
463 pixel_addr = (GLubyte *) image
464 + (skipimages + img) * bytes_per_image
465 + (skiprows + row) * bytes_per_row
466 + (skippixels + column) / 8;
467 }
468 else {
469 /* Non-BITMAP data */
470 GLint bytes_per_pixel, bytes_per_row, remainder, bytes_per_image;
471
472 bytes_per_pixel = _mesa_bytes_per_pixel( format, type );
473
474 /* The pixel type and format should have been error checked earlier */
475 assert(bytes_per_pixel > 0);
476
477 bytes_per_row = pixels_per_row * bytes_per_pixel;
478 remainder = bytes_per_row % alignment;
479 if (remainder > 0)
480 bytes_per_row += (alignment - remainder);
481
482 ASSERT(bytes_per_row % alignment == 0);
483
484 bytes_per_image = bytes_per_row * rows_per_image;
485
486 /* compute final pixel address */
487 pixel_addr = (GLubyte *) image
488 + (skipimages + img) * bytes_per_image
489 + (skiprows + row) * bytes_per_row
490 + (skippixels + column) * bytes_per_pixel;
491 }
492
493 return (GLvoid *) pixel_addr;
494 }
495
496
497
498 /*
499 * Compute the stride between image rows (in bytes) for the given
500 * pixel packing parameters and image width, format and type.
501 */
502 GLint
503 _mesa_image_row_stride( const struct gl_pixelstore_attrib *packing,
504 GLint width, GLenum format, GLenum type )
505 {
506 ASSERT(packing);
507 if (type == GL_BITMAP) {
508 /* BITMAP data */
509 if (packing->RowLength == 0) {
510 GLint bytes = (width + 7) / 8;
511 return bytes;
512 }
513 else {
514 GLint bytes = (packing->RowLength + 7) / 8;
515 return bytes;
516 }
517 }
518 else {
519 /* Non-BITMAP data */
520 const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
521 if (bytesPerPixel <= 0)
522 return -1; /* error */
523 if (packing->RowLength == 0) {
524 GLint bytes = bytesPerPixel * width;
525 return bytes;
526 }
527 else {
528 GLint bytes = bytesPerPixel * packing->RowLength;
529 return bytes;
530 }
531 }
532 }
533
534
535
536 /*
537 * Unpack a 32x32 pixel polygon stipple from user memory using the
538 * current pixel unpack settings.
539 */
540 void
541 _mesa_unpack_polygon_stipple( const GLubyte *pattern, GLuint dest[32],
542 const struct gl_pixelstore_attrib *unpacking )
543 {
544 GLubyte *ptrn = (GLubyte *) _mesa_unpack_bitmap( 32, 32, pattern, unpacking );
545 if (ptrn) {
546 /* Convert pattern from GLubytes to GLuints and handle big/little
547 * endian differences
548 */
549 GLubyte *p = ptrn;
550 GLint i;
551 for (i = 0; i < 32; i++) {
552 dest[i] = (p[0] << 24)
553 | (p[1] << 16)
554 | (p[2] << 8)
555 | (p[3] );
556 p += 4;
557 }
558 FREE(ptrn);
559 }
560 }
561
562
563
564 /*
565 * Pack polygon stipple into user memory given current pixel packing
566 * settings.
567 */
568 void
569 _mesa_pack_polygon_stipple( const GLuint pattern[32], GLubyte *dest,
570 const struct gl_pixelstore_attrib *packing )
571 {
572 /* Convert pattern from GLuints to GLubytes to handle big/little
573 * endian differences.
574 */
575 GLubyte ptrn[32*4];
576 GLint i;
577 for (i = 0; i < 32; i++) {
578 ptrn[i * 4 + 0] = (GLubyte) ((pattern[i] >> 24) & 0xff);
579 ptrn[i * 4 + 1] = (GLubyte) ((pattern[i] >> 16) & 0xff);
580 ptrn[i * 4 + 2] = (GLubyte) ((pattern[i] >> 8 ) & 0xff);
581 ptrn[i * 4 + 3] = (GLubyte) ((pattern[i] ) & 0xff);
582 }
583
584 _mesa_pack_bitmap(32, 32, ptrn, dest, packing);
585 }
586
587
588
589 /*
590 * Pack the given RGBA span into client memory at 'dest' address
591 * in the given pixel format and type.
592 * Optionally apply the enabled pixel transfer ops.
593 * Pack into memory using the given packing params struct.
594 * This is used by glReadPixels and glGetTexImage?D()
595 * Input: ctx - the context
596 * n - number of pixels in the span
597 * rgba - the pixels
598 * format - dest packing format
599 * type - dest packing datatype
600 * destination - destination packing address
601 * packing - pixel packing parameters
602 * applyTransferOps - apply scale/bias/lookup-table ops?
603 */
604 void
605 _mesa_pack_rgba_span( const GLcontext *ctx,
606 GLuint n, CONST GLubyte rgba[][4],
607 GLenum format, GLenum type, GLvoid *destination,
608 const struct gl_pixelstore_attrib *packing,
609 GLboolean applyTransferOps )
610 {
611 applyTransferOps &= (ctx->Pixel.ScaleOrBiasRGBA || ctx->Pixel.MapColorFlag);
612
613 /* Test for optimized case first */
614 if (!applyTransferOps && format == GL_RGBA && type == GL_UNSIGNED_BYTE) {
615 /* common simple case */
616 MEMCPY( destination, rgba, n * 4 * sizeof(GLubyte) );
617 }
618 else if (!applyTransferOps && format == GL_RGB && type == GL_UNSIGNED_BYTE) {
619 /* common simple case */
620 GLint i;
621 GLubyte *dest = (GLubyte *) destination;
622 for (i = 0; i < n; i++) {
623 dest[0] = rgba[i][RCOMP];
624 dest[1] = rgba[i][GCOMP];
625 dest[2] = rgba[i][BCOMP];
626 dest += 3;
627 }
628 }
629 else {
630 /* general solution */
631 GLfloat red[MAX_WIDTH], green[MAX_WIDTH], blue[MAX_WIDTH];
632 GLfloat alpha[MAX_WIDTH], luminance[MAX_WIDTH];
633 const GLfloat rscale = 1.0F / 255.0F;
634 const GLfloat gscale = 1.0F / 255.0F;
635 const GLfloat bscale = 1.0F / 255.0F;
636 const GLfloat ascale = 1.0F / 255.0F;
637 const GLint comps = _mesa_components_in_format(format);
638 GLuint i;
639
640 assert(n <= MAX_WIDTH);
641
642 /* convert color components to floating point */
643 for (i=0;i<n;i++) {
644 red[i] = rgba[i][RCOMP] * rscale;
645 green[i] = rgba[i][GCOMP] * gscale;
646 blue[i] = rgba[i][BCOMP] * bscale;
647 alpha[i] = rgba[i][ACOMP] * ascale;
648 }
649
650 /*
651 * Apply scale, bias and lookup-tables if enabled.
652 */
653 if (applyTransferOps) {
654 if (ctx->Pixel.ScaleOrBiasRGBA) {
655 gl_scale_and_bias_color( ctx, n, red, green, blue, alpha );
656 }
657 if (ctx->Pixel.MapColorFlag) {
658 gl_map_color( ctx, n, red, green, blue, alpha );
659 }
660 }
661
662 if (format==GL_LUMINANCE || format==GL_LUMINANCE_ALPHA) {
663 for (i=0;i<n;i++) {
664 GLfloat sum = red[i] + green[i] + blue[i];
665 luminance[i] = CLAMP( sum, 0.0F, 1.0F );
666 }
667 }
668
669 /*
670 * Pack/store the pixels. Ugh! Lots of cases!!!
671 */
672 switch (type) {
673 case GL_UNSIGNED_BYTE:
674 {
675 GLubyte *dst = (GLubyte *) destination;
676 switch (format) {
677 case GL_RED:
678 for (i=0;i<n;i++)
679 dst[i] = FLOAT_TO_UBYTE(red[i]);
680 break;
681 case GL_GREEN:
682 for (i=0;i<n;i++)
683 dst[i] = FLOAT_TO_UBYTE(green[i]);
684 break;
685 case GL_BLUE:
686 for (i=0;i<n;i++)
687 dst[i] = FLOAT_TO_UBYTE(blue[i]);
688 break;
689 case GL_ALPHA:
690 for (i=0;i<n;i++)
691 dst[i] = FLOAT_TO_UBYTE(alpha[i]);
692 break;
693 case GL_LUMINANCE:
694 for (i=0;i<n;i++)
695 dst[i] = FLOAT_TO_UBYTE(luminance[i]);
696 break;
697 case GL_LUMINANCE_ALPHA:
698 for (i=0;i<n;i++) {
699 dst[i*2+0] = FLOAT_TO_UBYTE(luminance[i]);
700 dst[i*2+1] = FLOAT_TO_UBYTE(alpha[i]);
701 }
702 break;
703 case GL_RGB:
704 for (i=0;i<n;i++) {
705 dst[i*3+0] = FLOAT_TO_UBYTE(red[i]);
706 dst[i*3+1] = FLOAT_TO_UBYTE(green[i]);
707 dst[i*3+2] = FLOAT_TO_UBYTE(blue[i]);
708 }
709 break;
710 case GL_RGBA:
711 for (i=0;i<n;i++) {
712 dst[i*4+0] = FLOAT_TO_UBYTE(red[i]);
713 dst[i*4+1] = FLOAT_TO_UBYTE(green[i]);
714 dst[i*4+2] = FLOAT_TO_UBYTE(blue[i]);
715 dst[i*4+3] = FLOAT_TO_UBYTE(alpha[i]);
716 }
717 break;
718 case GL_BGR:
719 for (i=0;i<n;i++) {
720 dst[i*3+0] = FLOAT_TO_UBYTE(blue[i]);
721 dst[i*3+1] = FLOAT_TO_UBYTE(green[i]);
722 dst[i*3+2] = FLOAT_TO_UBYTE(red[i]);
723 }
724 break;
725 case GL_BGRA:
726 for (i=0;i<n;i++) {
727 dst[i*4+0] = FLOAT_TO_UBYTE(blue[i]);
728 dst[i*4+1] = FLOAT_TO_UBYTE(green[i]);
729 dst[i*4+2] = FLOAT_TO_UBYTE(red[i]);
730 dst[i*4+3] = FLOAT_TO_UBYTE(alpha[i]);
731 }
732 break;
733 case GL_ABGR_EXT:
734 for (i=0;i<n;i++) {
735 dst[i*4+0] = FLOAT_TO_UBYTE(alpha[i]);
736 dst[i*4+1] = FLOAT_TO_UBYTE(blue[i]);
737 dst[i*4+2] = FLOAT_TO_UBYTE(green[i]);
738 dst[i*4+3] = FLOAT_TO_UBYTE(red[i]);
739 }
740 break;
741 default:
742 gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
743 }
744 }
745 break;
746 case GL_BYTE:
747 {
748 GLbyte *dst = (GLbyte *) destination;
749 switch (format) {
750 case GL_RED:
751 for (i=0;i<n;i++)
752 dst[i] = FLOAT_TO_BYTE(red[i]);
753 break;
754 case GL_GREEN:
755 for (i=0;i<n;i++)
756 dst[i] = FLOAT_TO_BYTE(green[i]);
757 break;
758 case GL_BLUE:
759 for (i=0;i<n;i++)
760 dst[i] = FLOAT_TO_BYTE(blue[i]);
761 break;
762 case GL_ALPHA:
763 for (i=0;i<n;i++)
764 dst[i] = FLOAT_TO_BYTE(alpha[i]);
765 break;
766 case GL_LUMINANCE:
767 for (i=0;i<n;i++)
768 dst[i] = FLOAT_TO_BYTE(luminance[i]);
769 break;
770 case GL_LUMINANCE_ALPHA:
771 for (i=0;i<n;i++) {
772 dst[i*2+0] = FLOAT_TO_BYTE(luminance[i]);
773 dst[i*2+1] = FLOAT_TO_BYTE(alpha[i]);
774 }
775 break;
776 case GL_RGB:
777 for (i=0;i<n;i++) {
778 dst[i*3+0] = FLOAT_TO_BYTE(red[i]);
779 dst[i*3+1] = FLOAT_TO_BYTE(green[i]);
780 dst[i*3+2] = FLOAT_TO_BYTE(blue[i]);
781 }
782 break;
783 case GL_RGBA:
784 for (i=0;i<n;i++) {
785 dst[i*4+0] = FLOAT_TO_BYTE(red[i]);
786 dst[i*4+1] = FLOAT_TO_BYTE(green[i]);
787 dst[i*4+2] = FLOAT_TO_BYTE(blue[i]);
788 dst[i*4+3] = FLOAT_TO_BYTE(alpha[i]);
789 }
790 break;
791 case GL_BGR:
792 for (i=0;i<n;i++) {
793 dst[i*3+0] = FLOAT_TO_BYTE(blue[i]);
794 dst[i*3+1] = FLOAT_TO_BYTE(green[i]);
795 dst[i*3+2] = FLOAT_TO_BYTE(red[i]);
796 }
797 break;
798 case GL_BGRA:
799 for (i=0;i<n;i++) {
800 dst[i*4+0] = FLOAT_TO_BYTE(blue[i]);
801 dst[i*4+1] = FLOAT_TO_BYTE(green[i]);
802 dst[i*4+2] = FLOAT_TO_BYTE(red[i]);
803 dst[i*4+3] = FLOAT_TO_BYTE(alpha[i]);
804 }
805 case GL_ABGR_EXT:
806 for (i=0;i<n;i++) {
807 dst[i*4+0] = FLOAT_TO_BYTE(alpha[i]);
808 dst[i*4+1] = FLOAT_TO_BYTE(blue[i]);
809 dst[i*4+2] = FLOAT_TO_BYTE(green[i]);
810 dst[i*4+3] = FLOAT_TO_BYTE(red[i]);
811 }
812 break;
813 default:
814 gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
815 }
816 }
817 break;
818 case GL_UNSIGNED_SHORT:
819 {
820 GLushort *dst = (GLushort *) destination;
821 switch (format) {
822 case GL_RED:
823 for (i=0;i<n;i++)
824 dst[i] = FLOAT_TO_USHORT(red[i]);
825 break;
826 case GL_GREEN:
827 for (i=0;i<n;i++)
828 dst[i] = FLOAT_TO_USHORT(green[i]);
829 break;
830 case GL_BLUE:
831 for (i=0;i<n;i++)
832 dst[i] = FLOAT_TO_USHORT(blue[i]);
833 break;
834 case GL_ALPHA:
835 for (i=0;i<n;i++)
836 dst[i] = FLOAT_TO_USHORT(alpha[i]);
837 break;
838 case GL_LUMINANCE:
839 for (i=0;i<n;i++)
840 dst[i] = FLOAT_TO_USHORT(luminance[i]);
841 break;
842 case GL_LUMINANCE_ALPHA:
843 for (i=0;i<n;i++) {
844 dst[i*2+0] = FLOAT_TO_USHORT(luminance[i]);
845 dst[i*2+1] = FLOAT_TO_USHORT(alpha[i]);
846 }
847 break;
848 case GL_RGB:
849 for (i=0;i<n;i++) {
850 dst[i*3+0] = FLOAT_TO_USHORT(red[i]);
851 dst[i*3+1] = FLOAT_TO_USHORT(green[i]);
852 dst[i*3+2] = FLOAT_TO_USHORT(blue[i]);
853 }
854 break;
855 case GL_RGBA:
856 for (i=0;i<n;i++) {
857 dst[i*4+0] = FLOAT_TO_USHORT(red[i]);
858 dst[i*4+1] = FLOAT_TO_USHORT(green[i]);
859 dst[i*4+2] = FLOAT_TO_USHORT(blue[i]);
860 dst[i*4+3] = FLOAT_TO_USHORT(alpha[i]);
861 }
862 break;
863 case GL_BGR:
864 for (i=0;i<n;i++) {
865 dst[i*3+0] = FLOAT_TO_USHORT(blue[i]);
866 dst[i*3+1] = FLOAT_TO_USHORT(green[i]);
867 dst[i*3+2] = FLOAT_TO_USHORT(red[i]);
868 }
869 break;
870 case GL_BGRA:
871 for (i=0;i<n;i++) {
872 dst[i*4+0] = FLOAT_TO_USHORT(blue[i]);
873 dst[i*4+1] = FLOAT_TO_USHORT(green[i]);
874 dst[i*4+2] = FLOAT_TO_USHORT(red[i]);
875 dst[i*4+3] = FLOAT_TO_USHORT(alpha[i]);
876 }
877 break;
878 case GL_ABGR_EXT:
879 for (i=0;i<n;i++) {
880 dst[i*4+0] = FLOAT_TO_USHORT(alpha[i]);
881 dst[i*4+1] = FLOAT_TO_USHORT(blue[i]);
882 dst[i*4+2] = FLOAT_TO_USHORT(green[i]);
883 dst[i*4+3] = FLOAT_TO_USHORT(red[i]);
884 }
885 break;
886 default:
887 gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
888 }
889 if (packing->SwapBytes) {
890 _mesa_swap2( (GLushort *) dst, n * comps);
891 }
892 }
893 break;
894 case GL_SHORT:
895 {
896 GLshort *dst = (GLshort *) destination;
897 switch (format) {
898 case GL_RED:
899 for (i=0;i<n;i++)
900 dst[i] = FLOAT_TO_SHORT(red[i]);
901 break;
902 case GL_GREEN:
903 for (i=0;i<n;i++)
904 dst[i] = FLOAT_TO_SHORT(green[i]);
905 break;
906 case GL_BLUE:
907 for (i=0;i<n;i++)
908 dst[i] = FLOAT_TO_SHORT(blue[i]);
909 break;
910 case GL_ALPHA:
911 for (i=0;i<n;i++)
912 dst[i] = FLOAT_TO_SHORT(alpha[i]);
913 break;
914 case GL_LUMINANCE:
915 for (i=0;i<n;i++)
916 dst[i] = FLOAT_TO_SHORT(luminance[i]);
917 break;
918 case GL_LUMINANCE_ALPHA:
919 for (i=0;i<n;i++) {
920 dst[i*2+0] = FLOAT_TO_SHORT(luminance[i]);
921 dst[i*2+1] = FLOAT_TO_SHORT(alpha[i]);
922 }
923 break;
924 case GL_RGB:
925 for (i=0;i<n;i++) {
926 dst[i*3+0] = FLOAT_TO_SHORT(red[i]);
927 dst[i*3+1] = FLOAT_TO_SHORT(green[i]);
928 dst[i*3+2] = FLOAT_TO_SHORT(blue[i]);
929 }
930 break;
931 case GL_RGBA:
932 for (i=0;i<n;i++) {
933 dst[i*4+0] = FLOAT_TO_SHORT(red[i]);
934 dst[i*4+1] = FLOAT_TO_SHORT(green[i]);
935 dst[i*4+2] = FLOAT_TO_SHORT(blue[i]);
936 dst[i*4+3] = FLOAT_TO_SHORT(alpha[i]);
937 }
938 break;
939 case GL_BGR:
940 for (i=0;i<n;i++) {
941 dst[i*3+0] = FLOAT_TO_SHORT(blue[i]);
942 dst[i*3+1] = FLOAT_TO_SHORT(green[i]);
943 dst[i*3+2] = FLOAT_TO_SHORT(red[i]);
944 }
945 break;
946 case GL_BGRA:
947 for (i=0;i<n;i++) {
948 dst[i*4+0] = FLOAT_TO_SHORT(blue[i]);
949 dst[i*4+1] = FLOAT_TO_SHORT(green[i]);
950 dst[i*4+2] = FLOAT_TO_SHORT(red[i]);
951 dst[i*4+3] = FLOAT_TO_SHORT(alpha[i]);
952 }
953 case GL_ABGR_EXT:
954 for (i=0;i<n;i++) {
955 dst[i*4+0] = FLOAT_TO_SHORT(alpha[i]);
956 dst[i*4+1] = FLOAT_TO_SHORT(blue[i]);
957 dst[i*4+2] = FLOAT_TO_SHORT(green[i]);
958 dst[i*4+3] = FLOAT_TO_SHORT(red[i]);
959 }
960 break;
961 default:
962 gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
963 }
964 if (packing->SwapBytes) {
965 _mesa_swap2( (GLushort *) dst, n * comps );
966 }
967 }
968 break;
969 case GL_UNSIGNED_INT:
970 {
971 GLuint *dst = (GLuint *) destination;
972 switch (format) {
973 case GL_RED:
974 for (i=0;i<n;i++)
975 dst[i] = FLOAT_TO_UINT(red[i]);
976 break;
977 case GL_GREEN:
978 for (i=0;i<n;i++)
979 dst[i] = FLOAT_TO_UINT(green[i]);
980 break;
981 case GL_BLUE:
982 for (i=0;i<n;i++)
983 dst[i] = FLOAT_TO_UINT(blue[i]);
984 break;
985 case GL_ALPHA:
986 for (i=0;i<n;i++)
987 dst[i] = FLOAT_TO_UINT(alpha[i]);
988 break;
989 case GL_LUMINANCE:
990 for (i=0;i<n;i++)
991 dst[i] = FLOAT_TO_UINT(luminance[i]);
992 break;
993 case GL_LUMINANCE_ALPHA:
994 for (i=0;i<n;i++) {
995 dst[i*2+0] = FLOAT_TO_UINT(luminance[i]);
996 dst[i*2+1] = FLOAT_TO_UINT(alpha[i]);
997 }
998 break;
999 case GL_RGB:
1000 for (i=0;i<n;i++) {
1001 dst[i*3+0] = FLOAT_TO_UINT(red[i]);
1002 dst[i*3+1] = FLOAT_TO_UINT(green[i]);
1003 dst[i*3+2] = FLOAT_TO_UINT(blue[i]);
1004 }
1005 break;
1006 case GL_RGBA:
1007 for (i=0;i<n;i++) {
1008 dst[i*4+0] = FLOAT_TO_UINT(red[i]);
1009 dst[i*4+1] = FLOAT_TO_UINT(green[i]);
1010 dst[i*4+2] = FLOAT_TO_UINT(blue[i]);
1011 dst[i*4+3] = FLOAT_TO_UINT(alpha[i]);
1012 }
1013 break;
1014 case GL_BGR:
1015 for (i=0;i<n;i++) {
1016 dst[i*3+0] = FLOAT_TO_UINT(blue[i]);
1017 dst[i*3+1] = FLOAT_TO_UINT(green[i]);
1018 dst[i*3+2] = FLOAT_TO_UINT(red[i]);
1019 }
1020 break;
1021 case GL_BGRA:
1022 for (i=0;i<n;i++) {
1023 dst[i*4+0] = FLOAT_TO_UINT(blue[i]);
1024 dst[i*4+1] = FLOAT_TO_UINT(green[i]);
1025 dst[i*4+2] = FLOAT_TO_UINT(red[i]);
1026 dst[i*4+3] = FLOAT_TO_UINT(alpha[i]);
1027 }
1028 break;
1029 case GL_ABGR_EXT:
1030 for (i=0;i<n;i++) {
1031 dst[i*4+0] = FLOAT_TO_UINT(alpha[i]);
1032 dst[i*4+1] = FLOAT_TO_UINT(blue[i]);
1033 dst[i*4+2] = FLOAT_TO_UINT(green[i]);
1034 dst[i*4+3] = FLOAT_TO_UINT(red[i]);
1035 }
1036 break;
1037 default:
1038 gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1039 }
1040 if (packing->SwapBytes) {
1041 _mesa_swap4( (GLuint *) dst, n * comps );
1042 }
1043 }
1044 break;
1045 case GL_INT:
1046 {
1047 GLint *dst = (GLint *) destination;
1048 switch (format) {
1049 case GL_RED:
1050 for (i=0;i<n;i++)
1051 dst[i] = FLOAT_TO_INT(red[i]);
1052 break;
1053 case GL_GREEN:
1054 for (i=0;i<n;i++)
1055 dst[i] = FLOAT_TO_INT(green[i]);
1056 break;
1057 case GL_BLUE:
1058 for (i=0;i<n;i++)
1059 dst[i] = FLOAT_TO_INT(blue[i]);
1060 break;
1061 case GL_ALPHA:
1062 for (i=0;i<n;i++)
1063 dst[i] = FLOAT_TO_INT(alpha[i]);
1064 break;
1065 case GL_LUMINANCE:
1066 for (i=0;i<n;i++)
1067 dst[i] = FLOAT_TO_INT(luminance[i]);
1068 break;
1069 case GL_LUMINANCE_ALPHA:
1070 for (i=0;i<n;i++) {
1071 dst[i*2+0] = FLOAT_TO_INT(luminance[i]);
1072 dst[i*2+1] = FLOAT_TO_INT(alpha[i]);
1073 }
1074 break;
1075 case GL_RGB:
1076 for (i=0;i<n;i++) {
1077 dst[i*3+0] = FLOAT_TO_INT(red[i]);
1078 dst[i*3+1] = FLOAT_TO_INT(green[i]);
1079 dst[i*3+2] = FLOAT_TO_INT(blue[i]);
1080 }
1081 break;
1082 case GL_RGBA:
1083 for (i=0;i<n;i++) {
1084 dst[i*4+0] = FLOAT_TO_INT(red[i]);
1085 dst[i*4+1] = FLOAT_TO_INT(green[i]);
1086 dst[i*4+2] = FLOAT_TO_INT(blue[i]);
1087 dst[i*4+3] = FLOAT_TO_INT(alpha[i]);
1088 }
1089 break;
1090 case GL_BGR:
1091 for (i=0;i<n;i++) {
1092 dst[i*3+0] = FLOAT_TO_INT(blue[i]);
1093 dst[i*3+1] = FLOAT_TO_INT(green[i]);
1094 dst[i*3+2] = FLOAT_TO_INT(red[i]);
1095 }
1096 break;
1097 case GL_BGRA:
1098 for (i=0;i<n;i++) {
1099 dst[i*4+0] = FLOAT_TO_INT(blue[i]);
1100 dst[i*4+1] = FLOAT_TO_INT(green[i]);
1101 dst[i*4+2] = FLOAT_TO_INT(red[i]);
1102 dst[i*4+3] = FLOAT_TO_INT(alpha[i]);
1103 }
1104 break;
1105 case GL_ABGR_EXT:
1106 for (i=0;i<n;i++) {
1107 dst[i*4+0] = FLOAT_TO_INT(alpha[i]);
1108 dst[i*4+1] = FLOAT_TO_INT(blue[i]);
1109 dst[i*4+2] = FLOAT_TO_INT(green[i]);
1110 dst[i*4+3] = FLOAT_TO_INT(red[i]);
1111 }
1112 break;
1113 default:
1114 gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1115 }
1116 if (packing->SwapBytes) {
1117 _mesa_swap4( (GLuint *) dst, n * comps );
1118 }
1119 }
1120 break;
1121 case GL_FLOAT:
1122 {
1123 GLfloat *dst = (GLfloat *) destination;
1124 switch (format) {
1125 case GL_RED:
1126 for (i=0;i<n;i++)
1127 dst[i] = red[i];
1128 break;
1129 case GL_GREEN:
1130 for (i=0;i<n;i++)
1131 dst[i] = green[i];
1132 break;
1133 case GL_BLUE:
1134 for (i=0;i<n;i++)
1135 dst[i] = blue[i];
1136 break;
1137 case GL_ALPHA:
1138 for (i=0;i<n;i++)
1139 dst[i] = alpha[i];
1140 break;
1141 case GL_LUMINANCE:
1142 for (i=0;i<n;i++)
1143 dst[i] = luminance[i];
1144 break;
1145 case GL_LUMINANCE_ALPHA:
1146 for (i=0;i<n;i++) {
1147 dst[i*2+0] = luminance[i];
1148 dst[i*2+1] = alpha[i];
1149 }
1150 break;
1151 case GL_RGB:
1152 for (i=0;i<n;i++) {
1153 dst[i*3+0] = red[i];
1154 dst[i*3+1] = green[i];
1155 dst[i*3+2] = blue[i];
1156 }
1157 break;
1158 case GL_RGBA:
1159 for (i=0;i<n;i++) {
1160 dst[i*4+0] = red[i];
1161 dst[i*4+1] = green[i];
1162 dst[i*4+2] = blue[i];
1163 dst[i*4+3] = alpha[i];
1164 }
1165 break;
1166 case GL_BGR:
1167 for (i=0;i<n;i++) {
1168 dst[i*3+0] = blue[i];
1169 dst[i*3+1] = green[i];
1170 dst[i*3+2] = red[i];
1171 }
1172 break;
1173 case GL_BGRA:
1174 for (i=0;i<n;i++) {
1175 dst[i*4+0] = blue[i];
1176 dst[i*4+1] = green[i];
1177 dst[i*4+2] = red[i];
1178 dst[i*4+3] = alpha[i];
1179 }
1180 break;
1181 case GL_ABGR_EXT:
1182 for (i=0;i<n;i++) {
1183 dst[i*4+0] = alpha[i];
1184 dst[i*4+1] = blue[i];
1185 dst[i*4+2] = green[i];
1186 dst[i*4+3] = red[i];
1187 }
1188 break;
1189 default:
1190 gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1191 }
1192 if (packing->SwapBytes) {
1193 _mesa_swap4( (GLuint *) dst, n * comps );
1194 }
1195 }
1196 break;
1197 case GL_UNSIGNED_BYTE_3_3_2:
1198 if (format == GL_RGB) {
1199 GLubyte *dst = (GLubyte *) destination;
1200 for (i=0;i<n;i++) {
1201 dst[i] = (((GLint) (red[i] * 7.0F)) << 5)
1202 | (((GLint) (green[i] * 7.0F)) << 2)
1203 | (((GLint) (blue[i] * 3.0F)) );
1204 }
1205 }
1206 break;
1207 case GL_UNSIGNED_BYTE_2_3_3_REV:
1208 if (format == GL_RGB) {
1209 GLubyte *dst = (GLubyte *) destination;
1210 for (i=0;i<n;i++) {
1211 dst[i] = (((GLint) (red[i] * 7.0F)) )
1212 | (((GLint) (green[i] * 7.0F)) << 3)
1213 | (((GLint) (blue[i] * 3.0F)) << 5);
1214 }
1215 }
1216 break;
1217 case GL_UNSIGNED_SHORT_5_6_5:
1218 if (format == GL_RGB) {
1219 GLushort *dst = (GLushort *) destination;
1220 for (i=0;i<n;i++) {
1221 dst[i] = (((GLint) (red[i] * 31.0F)) << 11)
1222 | (((GLint) (green[i] * 63.0F)) << 5)
1223 | (((GLint) (blue[i] * 31.0F)) );
1224 }
1225 }
1226 break;
1227 case GL_UNSIGNED_SHORT_5_6_5_REV:
1228 if (format == GL_RGB) {
1229 GLushort *dst = (GLushort *) destination;
1230 for (i=0;i<n;i++) {
1231 dst[i] = (((GLint) (red[i] * 31.0F)) )
1232 | (((GLint) (green[i] * 63.0F)) << 5)
1233 | (((GLint) (blue[i] * 31.0F)) << 11);
1234 }
1235 }
1236 break;
1237 case GL_UNSIGNED_SHORT_4_4_4_4:
1238 if (format == GL_RGB) {
1239 GLushort *dst = (GLushort *) destination;
1240 for (i=0;i<n;i++) {
1241 dst[i] = (((GLint) (red[i] * 15.0F)) << 12)
1242 | (((GLint) (green[i] * 15.0F)) << 8)
1243 | (((GLint) (blue[i] * 15.0F)) << 4)
1244 | (((GLint) (alpha[i] * 15.0F)) );
1245 }
1246 }
1247 break;
1248 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1249 if (format == GL_RGB) {
1250 GLushort *dst = (GLushort *) destination;
1251 for (i=0;i<n;i++) {
1252 dst[i] = (((GLint) (red[i] * 15.0F)) )
1253 | (((GLint) (green[i] * 15.0F)) << 4)
1254 | (((GLint) (blue[i] * 15.0F)) << 8)
1255 | (((GLint) (alpha[i] * 15.0F)) << 12);
1256 }
1257 }
1258 break;
1259 case GL_UNSIGNED_SHORT_5_5_5_1:
1260 if (format == GL_RGB) {
1261 GLushort *dst = (GLushort *) destination;
1262 for (i=0;i<n;i++) {
1263 dst[i] = (((GLint) (red[i] * 31.0F)) << 11)
1264 | (((GLint) (green[i] * 31.0F)) << 6)
1265 | (((GLint) (blue[i] * 31.0F)) << 1)
1266 | (((GLint) (alpha[i] * 1.0F)) );
1267 }
1268 }
1269 break;
1270 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
1271 if (format == GL_RGB) {
1272 GLushort *dst = (GLushort *) destination;
1273 for (i=0;i<n;i++) {
1274 dst[i] = (((GLint) (red[i] * 31.0F)) )
1275 | (((GLint) (green[i] * 31.0F)) << 5)
1276 | (((GLint) (blue[i] * 31.0F)) << 10)
1277 | (((GLint) (alpha[i] * 1.0F)) << 15);
1278 }
1279 }
1280 break;
1281 case GL_UNSIGNED_INT_8_8_8_8:
1282 if (format == GL_RGBA) {
1283 GLuint *dst = (GLuint *) destination;
1284 for (i=0;i<n;i++) {
1285 dst[i] = (((GLuint) (red[i] * 255.0F)) << 24)
1286 | (((GLuint) (green[i] * 255.0F)) << 16)
1287 | (((GLuint) (blue[i] * 255.0F)) << 8)
1288 | (((GLuint) (alpha[i] * 255.0F)) );
1289 }
1290 }
1291 else if (format == GL_BGRA) {
1292 GLuint *dst = (GLuint *) destination;
1293 for (i=0;i<n;i++) {
1294 dst[i] = (((GLuint) (blue[i] * 255.0F)) << 24)
1295 | (((GLuint) (green[i] * 255.0F)) << 16)
1296 | (((GLuint) (red[i] * 255.0F)) << 8)
1297 | (((GLuint) (alpha[i] * 255.0F)) );
1298 }
1299 }
1300 else if (format == GL_ABGR_EXT) {
1301 GLuint *dst = (GLuint *) destination;
1302 for (i=0;i<n;i++) {
1303 dst[i] = (((GLuint) (alpha[i] * 255.0F)) << 24)
1304 | (((GLuint) (blue[i] * 255.0F)) << 16)
1305 | (((GLuint) (green[i] * 255.0F)) << 8)
1306 | (((GLuint) (red[i] * 255.0F)) );
1307 }
1308 }
1309 break;
1310 case GL_UNSIGNED_INT_8_8_8_8_REV:
1311 if (format == GL_RGBA) {
1312 GLuint *dst = (GLuint *) destination;
1313 for (i=0;i<n;i++) {
1314 dst[i] = (((GLuint) (red[i] * 255.0F)) )
1315 | (((GLuint) (green[i] * 255.0F)) << 8)
1316 | (((GLuint) (blue[i] * 255.0F)) << 16)
1317 | (((GLuint) (alpha[i] * 255.0F)) << 24);
1318 }
1319 }
1320 else if (format == GL_BGRA) {
1321 GLuint *dst = (GLuint *) destination;
1322 for (i=0;i<n;i++) {
1323 dst[i] = (((GLuint) (blue[i] * 255.0F)) )
1324 | (((GLuint) (green[i] * 255.0F)) << 8)
1325 | (((GLuint) (red[i] * 255.0F)) << 16)
1326 | (((GLuint) (alpha[i] * 255.0F)) << 24);
1327 }
1328 }
1329 else if (format == GL_ABGR_EXT) {
1330 GLuint *dst = (GLuint *) destination;
1331 for (i=0;i<n;i++) {
1332 dst[i] = (((GLuint) (alpha[i] * 255.0F)) )
1333 | (((GLuint) (blue[i] * 255.0F)) << 8)
1334 | (((GLuint) (green[i] * 255.0F)) << 16)
1335 | (((GLuint) (red[i] * 255.0F)) << 24);
1336 }
1337 }
1338 break;
1339 case GL_UNSIGNED_INT_10_10_10_2:
1340 if (format == GL_RGBA) {
1341 GLuint *dst = (GLuint *) destination;
1342 for (i=0;i<n;i++) {
1343 dst[i] = (((GLuint) (red[i] * 1023.0F)) << 22)
1344 | (((GLuint) (green[i] * 1023.0F)) << 12)
1345 | (((GLuint) (blue[i] * 1023.0F)) << 2)
1346 | (((GLuint) (alpha[i] * 3.0F)) );
1347 }
1348 }
1349 else if (format == GL_BGRA) {
1350 GLuint *dst = (GLuint *) destination;
1351 for (i=0;i<n;i++) {
1352 dst[i] = (((GLuint) (blue[i] * 1023.0F)) << 22)
1353 | (((GLuint) (green[i] * 1023.0F)) << 12)
1354 | (((GLuint) (red[i] * 1023.0F)) << 2)
1355 | (((GLuint) (alpha[i] * 3.0F)) );
1356 }
1357 }
1358 else if (format == GL_ABGR_EXT) {
1359 GLuint *dst = (GLuint *) destination;
1360 for (i=0;i<n;i++) {
1361 dst[i] = (((GLuint) (alpha[i] * 1023.0F)) << 22)
1362 | (((GLuint) (blue[i] * 1023.0F)) << 12)
1363 | (((GLuint) (green[i] * 1023.0F)) << 2)
1364 | (((GLuint) (red[i] * 3.0F)) );
1365 }
1366 }
1367 break;
1368 case GL_UNSIGNED_INT_2_10_10_10_REV:
1369 if (format == GL_RGBA) {
1370 GLuint *dst = (GLuint *) destination;
1371 for (i=0;i<n;i++) {
1372 dst[i] = (((GLuint) (red[i] * 1023.0F)) )
1373 | (((GLuint) (green[i] * 1023.0F)) << 10)
1374 | (((GLuint) (blue[i] * 1023.0F)) << 20)
1375 | (((GLuint) (alpha[i] * 3.0F)) << 30);
1376 }
1377 }
1378 else if (format == GL_BGRA) {
1379 GLuint *dst = (GLuint *) destination;
1380 for (i=0;i<n;i++) {
1381 dst[i] = (((GLuint) (blue[i] * 1023.0F)) )
1382 | (((GLuint) (green[i] * 1023.0F)) << 10)
1383 | (((GLuint) (red[i] * 1023.0F)) << 20)
1384 | (((GLuint) (alpha[i] * 3.0F)) << 30);
1385 }
1386 }
1387 else if (format == GL_ABGR_EXT) {
1388 GLuint *dst = (GLuint *) destination;
1389 for (i=0;i<n;i++) {
1390 dst[i] = (((GLuint) (alpha[i] * 1023.0F)) )
1391 | (((GLuint) (blue[i] * 1023.0F)) << 10)
1392 | (((GLuint) (green[i] * 1023.0F)) << 20)
1393 | (((GLuint) (red[i] * 3.0F)) << 30);
1394 }
1395 }
1396 break;
1397 default:
1398 gl_problem( ctx, "bad type in _mesa_pack_rgba_span" );
1399 }
1400 }
1401 }
1402
1403
1404 #define SWAP2BYTE(VALUE) \
1405 { \
1406 GLubyte *bytes = (GLubyte *) &(VALUE); \
1407 GLubyte tmp = bytes[0]; \
1408 bytes[0] = bytes[1]; \
1409 bytes[1] = tmp; \
1410 }
1411
1412 #define SWAP4BYTE(VALUE) \
1413 { \
1414 GLubyte *bytes = (GLubyte *) &(VALUE); \
1415 GLubyte tmp = bytes[0]; \
1416 bytes[0] = bytes[3]; \
1417 bytes[3] = tmp; \
1418 tmp = bytes[1]; \
1419 bytes[1] = bytes[2]; \
1420 bytes[2] = tmp; \
1421 }
1422
1423
1424 static void
1425 extract_uint_indexes(GLuint n, GLuint indexes[],
1426 GLenum srcFormat, GLenum srcType, const GLvoid *src,
1427 const struct gl_pixelstore_attrib *unpack )
1428 {
1429 assert(srcFormat == GL_COLOR_INDEX);
1430
1431 ASSERT(srcType == GL_BITMAP ||
1432 srcType == GL_UNSIGNED_BYTE ||
1433 srcType == GL_BYTE ||
1434 srcType == GL_UNSIGNED_SHORT ||
1435 srcType == GL_SHORT ||
1436 srcType == GL_UNSIGNED_INT ||
1437 srcType == GL_INT ||
1438 srcType == GL_FLOAT);
1439
1440 switch (srcType) {
1441 case GL_BITMAP:
1442 {
1443 GLubyte *ubsrc = (GLubyte *) src;
1444 if (unpack->LsbFirst) {
1445 GLubyte mask = 1 << (unpack->SkipPixels & 0x7);
1446 GLuint i;
1447 for (i = 0; i < n; i++) {
1448 indexes[i] = (*ubsrc & mask) ? 1 : 0;
1449 if (mask == 128) {
1450 mask = 1;
1451 ubsrc++;
1452 }
1453 else {
1454 mask = mask << 1;
1455 }
1456 }
1457 }
1458 else {
1459 GLubyte mask = 128 >> (unpack->SkipPixels & 0x7);
1460 GLuint i;
1461 for (i = 0; i < n; i++) {
1462 indexes[i] = (*ubsrc & mask) ? 1 : 0;
1463 if (mask == 1) {
1464 mask = 128;
1465 ubsrc++;
1466 }
1467 else {
1468 mask = mask >> 1;
1469 }
1470 }
1471 }
1472 }
1473 break;
1474 case GL_UNSIGNED_BYTE:
1475 {
1476 GLuint i;
1477 const GLubyte *s = (const GLubyte *) src;
1478 for (i = 0; i < n; i++)
1479 indexes[i] = s[i];
1480 }
1481 break;
1482 case GL_BYTE:
1483 {
1484 GLuint i;
1485 const GLbyte *s = (const GLbyte *) src;
1486 for (i = 0; i < n; i++)
1487 indexes[i] = s[i];
1488 }
1489 break;
1490 case GL_UNSIGNED_SHORT:
1491 {
1492 GLuint i;
1493 const GLushort *s = (const GLushort *) src;
1494 if (unpack->SwapBytes) {
1495 for (i = 0; i < n; i++) {
1496 GLushort value = s[i];
1497 SWAP2BYTE(value);
1498 indexes[i] = value;
1499 }
1500 }
1501 else {
1502 for (i = 0; i < n; i++)
1503 indexes[i] = s[i];
1504 }
1505 }
1506 break;
1507 case GL_SHORT:
1508 {
1509 GLuint i;
1510 const GLshort *s = (const GLshort *) src;
1511 if (unpack->SwapBytes) {
1512 for (i = 0; i < n; i++) {
1513 GLshort value = s[i];
1514 SWAP2BYTE(value);
1515 indexes[i] = value;
1516 }
1517 }
1518 else {
1519 for (i = 0; i < n; i++)
1520 indexes[i] = s[i];
1521 }
1522 }
1523 break;
1524 case GL_UNSIGNED_INT:
1525 {
1526 GLuint i;
1527 const GLuint *s = (const GLuint *) src;
1528 if (unpack->SwapBytes) {
1529 for (i = 0; i < n; i++) {
1530 GLuint value = s[i];
1531 SWAP4BYTE(value);
1532 indexes[i] = value;
1533 }
1534 }
1535 else {
1536 for (i = 0; i < n; i++)
1537 indexes[i] = s[i];
1538 }
1539 }
1540 break;
1541 case GL_INT:
1542 {
1543 GLuint i;
1544 const GLint *s = (const GLint *) src;
1545 if (unpack->SwapBytes) {
1546 for (i = 0; i < n; i++) {
1547 GLint value = s[i];
1548 SWAP4BYTE(value);
1549 indexes[i] = value;
1550 }
1551 }
1552 else {
1553 for (i = 0; i < n; i++)
1554 indexes[i] = s[i];
1555 }
1556 }
1557 break;
1558 case GL_FLOAT:
1559 {
1560 GLuint i;
1561 const GLfloat *s = (const GLfloat *) src;
1562 if (unpack->SwapBytes) {
1563 for (i = 0; i < n; i++) {
1564 GLfloat value = s[i];
1565 SWAP4BYTE(value);
1566 indexes[i] = (GLuint) value;
1567 }
1568 }
1569 else {
1570 for (i = 0; i < n; i++)
1571 indexes[i] = (GLuint) s[i];
1572 }
1573 }
1574 break;
1575 default:
1576 gl_problem(NULL, "bad srcType in extract_uint_indexes");
1577 return;
1578 }
1579 }
1580
1581
1582
1583 /*
1584 * This function extracts floating point RGBA values from arbitrary
1585 * image data. srcFormat and srcType are the format and type parameters
1586 * passed to glDrawPixels, glTexImage[123]D, glTexSubImage[123]D, etc.
1587 *
1588 * Refering to section 3.6.4 of the OpenGL 1.2 spec, this function
1589 * implements the "Conversion to floating point", "Conversion to RGB",
1590 * and "Final Expansion to RGBA" operations.
1591 *
1592 * Args: n - number of pixels
1593 * rgba - output colors
1594 * srcFormat - format of incoming data
1595 * srcType - datatype of incoming data
1596 * src - source data pointer
1597 * swapBytes - perform byteswapping of incoming data?
1598 */
1599 static void
1600 extract_float_rgba(GLuint n, GLfloat rgba[][4],
1601 GLenum srcFormat, GLenum srcType, const GLvoid *src,
1602 GLboolean swapBytes)
1603 {
1604 GLint redIndex, greenIndex, blueIndex, alphaIndex;
1605 GLint stride;
1606 GLint rComp, bComp, gComp, aComp;
1607
1608 ASSERT(srcFormat == GL_RED ||
1609 srcFormat == GL_GREEN ||
1610 srcFormat == GL_BLUE ||
1611 srcFormat == GL_ALPHA ||
1612 srcFormat == GL_LUMINANCE ||
1613 srcFormat == GL_LUMINANCE_ALPHA ||
1614 srcFormat == GL_INTENSITY ||
1615 srcFormat == GL_RGB ||
1616 srcFormat == GL_BGR ||
1617 srcFormat == GL_RGBA ||
1618 srcFormat == GL_BGRA ||
1619 srcFormat == GL_ABGR_EXT);
1620
1621 ASSERT(srcType == GL_UNSIGNED_BYTE ||
1622 srcType == GL_BYTE ||
1623 srcType == GL_UNSIGNED_SHORT ||
1624 srcType == GL_SHORT ||
1625 srcType == GL_UNSIGNED_INT ||
1626 srcType == GL_INT ||
1627 srcType == GL_FLOAT ||
1628 srcType == GL_UNSIGNED_BYTE_3_3_2 ||
1629 srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
1630 srcType == GL_UNSIGNED_SHORT_5_6_5 ||
1631 srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
1632 srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
1633 srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
1634 srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
1635 srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
1636 srcType == GL_UNSIGNED_INT_8_8_8_8 ||
1637 srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
1638 srcType == GL_UNSIGNED_INT_10_10_10_2 ||
1639 srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
1640
1641 rComp = gComp = bComp = aComp = -1;
1642
1643 switch (srcFormat) {
1644 case GL_RED:
1645 redIndex = 0;
1646 greenIndex = blueIndex = alphaIndex = -1;
1647 stride = 1;
1648 break;
1649 case GL_GREEN:
1650 greenIndex = 0;
1651 redIndex = blueIndex = alphaIndex = -1;
1652 stride = 1;
1653 break;
1654 case GL_BLUE:
1655 blueIndex = 0;
1656 redIndex = greenIndex = alphaIndex = -1;
1657 stride = 1;
1658 break;
1659 case GL_ALPHA:
1660 redIndex = greenIndex = blueIndex = -1;
1661 alphaIndex = 0;
1662 stride = 1;
1663 break;
1664 case GL_LUMINANCE:
1665 redIndex = greenIndex = blueIndex = 0;
1666 alphaIndex = -1;
1667 stride = 1;
1668 break;
1669 case GL_LUMINANCE_ALPHA:
1670 redIndex = greenIndex = blueIndex = 0;
1671 alphaIndex = 1;
1672 stride = 2;
1673 break;
1674 case GL_INTENSITY:
1675 redIndex = 0;
1676 greenIndex = blueIndex = alphaIndex = -1;
1677 stride = 1;
1678 break;
1679 case GL_RGB:
1680 redIndex = 0;
1681 greenIndex = 1;
1682 blueIndex = 2;
1683 alphaIndex = -1;
1684 stride = 3;
1685 break;
1686 case GL_BGR:
1687 redIndex = 2;
1688 greenIndex = 1;
1689 blueIndex = 0;
1690 alphaIndex = -1;
1691 stride = 3;
1692 break;
1693 case GL_RGBA:
1694 redIndex = 0;
1695 greenIndex = 1;
1696 blueIndex = 2;
1697 alphaIndex = 3;
1698 rComp = 0;
1699 gComp = 1;
1700 bComp = 2;
1701 aComp = 3;
1702 stride = 4;
1703 break;
1704 case GL_BGRA:
1705 redIndex = 2;
1706 greenIndex = 1;
1707 blueIndex = 0;
1708 alphaIndex = 3;
1709 rComp = 2;
1710 gComp = 1;
1711 bComp = 0;
1712 aComp = 3;
1713 stride = 4;
1714 break;
1715 case GL_ABGR_EXT:
1716 redIndex = 3;
1717 greenIndex = 2;
1718 blueIndex = 1;
1719 alphaIndex = 0;
1720 rComp = 3;
1721 gComp = 2;
1722 bComp = 1;
1723 aComp = 0;
1724 stride = 4;
1725 break;
1726 default:
1727 gl_problem(NULL, "bad srcFormat in extract float data");
1728 return;
1729 }
1730
1731
1732 #define PROCESS(INDEX, CHANNEL, DEFAULT, TYPE, CONVERSION) \
1733 if ((INDEX) < 0) { \
1734 GLuint i; \
1735 for (i = 0; i < n; i++) { \
1736 rgba[i][CHANNEL] = DEFAULT; \
1737 } \
1738 } \
1739 else if (swapBytes) { \
1740 const TYPE *s = (const TYPE *) src; \
1741 GLuint i; \
1742 for (i = 0; i < n; i++) { \
1743 TYPE value = s[INDEX]; \
1744 if (sizeof(TYPE) == 2) { \
1745 SWAP2BYTE(value); \
1746 } \
1747 else if (sizeof(TYPE) == 4) { \
1748 SWAP4BYTE(value); \
1749 } \
1750 rgba[i][CHANNEL] = (GLfloat) CONVERSION(value); \
1751 s += stride; \
1752 } \
1753 } \
1754 else { \
1755 const TYPE *s = (const TYPE *) src; \
1756 GLuint i; \
1757 for (i = 0; i < n; i++) { \
1758 rgba[i][CHANNEL] = (GLfloat) CONVERSION(s[INDEX]); \
1759 s += stride; \
1760 } \
1761 }
1762
1763 switch (srcType) {
1764 case GL_UNSIGNED_BYTE:
1765 PROCESS(redIndex, RCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
1766 PROCESS(greenIndex, GCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
1767 PROCESS(blueIndex, BCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
1768 PROCESS(alphaIndex, ACOMP, 1.0F, GLubyte, UBYTE_TO_FLOAT);
1769 break;
1770 case GL_BYTE:
1771 PROCESS(redIndex, RCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
1772 PROCESS(greenIndex, GCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
1773 PROCESS(blueIndex, BCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
1774 PROCESS(alphaIndex, ACOMP, 1.0F, GLbyte, BYTE_TO_FLOAT);
1775 break;
1776 case GL_UNSIGNED_SHORT:
1777 PROCESS(redIndex, RCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
1778 PROCESS(greenIndex, GCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
1779 PROCESS(blueIndex, BCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
1780 PROCESS(alphaIndex, ACOMP, 1.0F, GLushort, USHORT_TO_FLOAT);
1781 break;
1782 case GL_SHORT:
1783 PROCESS(redIndex, RCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
1784 PROCESS(greenIndex, GCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
1785 PROCESS(blueIndex, BCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
1786 PROCESS(alphaIndex, ACOMP, 1.0F, GLshort, SHORT_TO_FLOAT);
1787 break;
1788 case GL_UNSIGNED_INT:
1789 PROCESS(redIndex, RCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
1790 PROCESS(greenIndex, GCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
1791 PROCESS(blueIndex, BCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
1792 PROCESS(alphaIndex, ACOMP, 1.0F, GLuint, UINT_TO_FLOAT);
1793 break;
1794 case GL_INT:
1795 PROCESS(redIndex, RCOMP, 0.0F, GLint, INT_TO_FLOAT);
1796 PROCESS(greenIndex, GCOMP, 0.0F, GLint, INT_TO_FLOAT);
1797 PROCESS(blueIndex, BCOMP, 0.0F, GLint, INT_TO_FLOAT);
1798 PROCESS(alphaIndex, ACOMP, 1.0F, GLint, INT_TO_FLOAT);
1799 break;
1800 case GL_FLOAT:
1801 PROCESS(redIndex, RCOMP, 0.0F, GLfloat, (GLfloat));
1802 PROCESS(greenIndex, GCOMP, 0.0F, GLfloat, (GLfloat));
1803 PROCESS(blueIndex, BCOMP, 0.0F, GLfloat, (GLfloat));
1804 PROCESS(alphaIndex, ACOMP, 1.0F, GLfloat, (GLfloat));
1805 break;
1806 case GL_UNSIGNED_BYTE_3_3_2:
1807 {
1808 const GLubyte *ubsrc = (const GLubyte *) src;
1809 GLuint i;
1810 for (i = 0; i < n; i ++) {
1811 GLubyte p = ubsrc[i];
1812 rgba[i][RCOMP] = ((p >> 5) ) * (1.0F / 7.0F);
1813 rgba[i][GCOMP] = ((p >> 2) & 0x7) * (1.0F / 7.0F);
1814 rgba[i][BCOMP] = ((p ) & 0x3) * (1.0F / 3.0F);
1815 rgba[i][ACOMP] = 1.0F;
1816 }
1817 }
1818 break;
1819 case GL_UNSIGNED_BYTE_2_3_3_REV:
1820 {
1821 const GLubyte *ubsrc = (const GLubyte *) src;
1822 GLuint i;
1823 for (i = 0; i < n; i ++) {
1824 GLubyte p = ubsrc[i];
1825 rgba[i][RCOMP] = ((p ) & 0x7) * (1.0F / 7.0F);
1826 rgba[i][GCOMP] = ((p >> 3) & 0x7) * (1.0F / 7.0F);
1827 rgba[i][BCOMP] = ((p >> 6) ) * (1.0F / 3.0F);
1828 rgba[i][ACOMP] = 1.0F;
1829 }
1830 }
1831 break;
1832 case GL_UNSIGNED_SHORT_5_6_5:
1833 if (swapBytes) {
1834 const GLushort *ussrc = (const GLushort *) src;
1835 GLuint i;
1836 for (i = 0; i < n; i ++) {
1837 GLushort p = ussrc[i];
1838 SWAP2BYTE(p);
1839 rgba[i][RCOMP] = ((p >> 11) ) * (1.0F / 31.0F);
1840 rgba[i][GCOMP] = ((p >> 5) & 0x3f) * (1.0F / 63.0F);
1841 rgba[i][BCOMP] = ((p ) & 0x1f) * (1.0F / 31.0F);
1842 rgba[i][ACOMP] = 1.0F;
1843 }
1844 }
1845 else {
1846 const GLushort *ussrc = (const GLushort *) src;
1847 GLuint i;
1848 for (i = 0; i < n; i ++) {
1849 GLushort p = ussrc[i];
1850 rgba[i][RCOMP] = ((p >> 11) ) * (1.0F / 31.0F);
1851 rgba[i][GCOMP] = ((p >> 5) & 0x3f) * (1.0F / 63.0F);
1852 rgba[i][BCOMP] = ((p ) & 0x1f) * (1.0F / 31.0F);
1853 rgba[i][ACOMP] = 1.0F;
1854 }
1855 }
1856 break;
1857 case GL_UNSIGNED_SHORT_5_6_5_REV:
1858 if (swapBytes) {
1859 const GLushort *ussrc = (const GLushort *) src;
1860 GLuint i;
1861 for (i = 0; i < n; i ++) {
1862 GLushort p = ussrc[i];
1863 SWAP2BYTE(p);
1864 rgba[i][RCOMP] = ((p ) & 0x1f) * (1.0F / 31.0F);
1865 rgba[i][GCOMP] = ((p >> 5) & 0x3f) * (1.0F / 63.0F);
1866 rgba[i][BCOMP] = ((p >> 11) ) * (1.0F / 31.0F);
1867 rgba[i][ACOMP] = 1.0F;
1868 }
1869 }
1870 else {
1871 const GLushort *ussrc = (const GLushort *) src;
1872 GLuint i;
1873 for (i = 0; i < n; i ++) {
1874 GLushort p = ussrc[i];
1875 rgba[i][RCOMP] = ((p ) & 0x1f) * (1.0F / 31.0F);
1876 rgba[i][GCOMP] = ((p >> 5) & 0x3f) * (1.0F / 63.0F);
1877 rgba[i][BCOMP] = ((p >> 11) ) * (1.0F / 31.0F);
1878 rgba[i][ACOMP] = 1.0F;
1879 }
1880 }
1881 break;
1882 case GL_UNSIGNED_SHORT_4_4_4_4:
1883 if (swapBytes) {
1884 const GLushort *ussrc = (const GLushort *) src;
1885 GLuint i;
1886 for (i = 0; i < n; i ++) {
1887 GLushort p = ussrc[i];
1888 SWAP2BYTE(p);
1889 rgba[i][rComp] = ((p >> 12) ) * (1.0F / 15.0F);
1890 rgba[i][gComp] = ((p >> 8) & 0xf) * (1.0F / 15.0F);
1891 rgba[i][bComp] = ((p >> 4) & 0xf) * (1.0F / 15.0F);
1892 rgba[i][aComp] = ((p ) & 0xf) * (1.0F / 15.0F);
1893 }
1894 }
1895 else {
1896 const GLushort *ussrc = (const GLushort *) src;
1897 GLuint i;
1898 for (i = 0; i < n; i ++) {
1899 GLushort p = ussrc[i];
1900 rgba[i][rComp] = ((p >> 12) ) * (1.0F / 15.0F);
1901 rgba[i][gComp] = ((p >> 8) & 0xf) * (1.0F / 15.0F);
1902 rgba[i][bComp] = ((p >> 4) & 0xf) * (1.0F / 15.0F);
1903 rgba[i][aComp] = ((p ) & 0xf) * (1.0F / 15.0F);
1904 }
1905 }
1906 break;
1907 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1908 if (swapBytes) {
1909 const GLushort *ussrc = (const GLushort *) src;
1910 GLuint i;
1911 for (i = 0; i < n; i ++) {
1912 GLushort p = ussrc[i];
1913 SWAP2BYTE(p);
1914 rgba[i][rComp] = ((p ) & 0xf) * (1.0F / 15.0F);
1915 rgba[i][gComp] = ((p >> 4) & 0xf) * (1.0F / 15.0F);
1916 rgba[i][bComp] = ((p >> 8) & 0xf) * (1.0F / 15.0F);
1917 rgba[i][aComp] = ((p >> 12) ) * (1.0F / 15.0F);
1918 }
1919 }
1920 else {
1921 const GLushort *ussrc = (const GLushort *) src;
1922 GLuint i;
1923 for (i = 0; i < n; i ++) {
1924 GLushort p = ussrc[i];
1925 rgba[i][rComp] = ((p ) & 0xf) * (1.0F / 15.0F);
1926 rgba[i][gComp] = ((p >> 4) & 0xf) * (1.0F / 15.0F);
1927 rgba[i][bComp] = ((p >> 8) & 0xf) * (1.0F / 15.0F);
1928 rgba[i][aComp] = ((p >> 12) ) * (1.0F / 15.0F);
1929 }
1930 }
1931 break;
1932 case GL_UNSIGNED_SHORT_5_5_5_1:
1933 if (swapBytes) {
1934 const GLushort *ussrc = (const GLushort *) src;
1935 GLuint i;
1936 for (i = 0; i < n; i ++) {
1937 GLushort p = ussrc[i];
1938 SWAP2BYTE(p);
1939 rgba[i][rComp] = ((p >> 11) ) * (1.0F / 31.0F);
1940 rgba[i][gComp] = ((p >> 6) & 0x1f) * (1.0F / 31.0F);
1941 rgba[i][bComp] = ((p >> 1) & 0x1f) * (1.0F / 31.0F);
1942 rgba[i][aComp] = ((p ) & 0x1) * (1.0F / 1.0F);
1943 }
1944 }
1945 else {
1946 const GLushort *ussrc = (const GLushort *) src;
1947 GLuint i;
1948 for (i = 0; i < n; i ++) {
1949 GLushort p = ussrc[i];
1950 rgba[i][rComp] = ((p >> 11) ) * (1.0F / 31.0F);
1951 rgba[i][gComp] = ((p >> 6) & 0x1f) * (1.0F / 31.0F);
1952 rgba[i][bComp] = ((p >> 1) & 0x1f) * (1.0F / 31.0F);
1953 rgba[i][aComp] = ((p ) & 0x1) * (1.0F / 1.0F);
1954 }
1955 }
1956 break;
1957 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
1958 if (swapBytes) {
1959 const GLushort *ussrc = (const GLushort *) src;
1960 GLuint i;
1961 for (i = 0; i < n; i ++) {
1962 GLushort p = ussrc[i];
1963 SWAP2BYTE(p);
1964 rgba[i][rComp] = ((p ) & 0x1f) * (1.0F / 31.0F);
1965 rgba[i][gComp] = ((p >> 5) & 0x1f) * (1.0F / 31.0F);
1966 rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
1967 rgba[i][aComp] = ((p >> 15) ) * (1.0F / 1.0F);
1968 }
1969 }
1970 else {
1971 const GLushort *ussrc = (const GLushort *) src;
1972 GLuint i;
1973 for (i = 0; i < n; i ++) {
1974 GLushort p = ussrc[i];
1975 rgba[i][rComp] = ((p ) & 0x1f) * (1.0F / 31.0F);
1976 rgba[i][gComp] = ((p >> 5) & 0x1f) * (1.0F / 31.0F);
1977 rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
1978 rgba[i][aComp] = ((p >> 15) ) * (1.0F / 1.0F);
1979 }
1980 }
1981 break;
1982 case GL_UNSIGNED_INT_8_8_8_8:
1983 if (swapBytes) {
1984 const GLuint *uisrc = (const GLuint *) src;
1985 GLuint i;
1986 for (i = 0; i < n; i ++) {
1987 GLuint p = uisrc[i];
1988 rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p ) & 0xff);
1989 rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 8) & 0xff);
1990 rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
1991 rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24) );
1992 }
1993 }
1994 else {
1995 const GLuint *uisrc = (const GLuint *) src;
1996 GLuint i;
1997 for (i = 0; i < n; i ++) {
1998 GLuint p = uisrc[i];
1999 rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24) );
2000 rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
2001 rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 8) & 0xff);
2002 rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p ) & 0xff);
2003 }
2004 }
2005 break;
2006 case GL_UNSIGNED_INT_8_8_8_8_REV:
2007 if (swapBytes) {
2008 const GLuint *uisrc = (const GLuint *) src;
2009 GLuint i;
2010 for (i = 0; i < n; i ++) {
2011 GLuint p = uisrc[i];
2012 rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24) );
2013 rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
2014 rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 8) & 0xff);
2015 rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p ) & 0xff);
2016 }
2017 }
2018 else {
2019 const GLuint *uisrc = (const GLuint *) src;
2020 GLuint i;
2021 for (i = 0; i < n; i ++) {
2022 GLuint p = uisrc[i];
2023 rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p ) & 0xff);
2024 rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 8) & 0xff);
2025 rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
2026 rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24) );
2027 }
2028 }
2029 break;
2030 case GL_UNSIGNED_INT_10_10_10_2:
2031 if (swapBytes) {
2032 const GLuint *uisrc = (const GLuint *) src;
2033 GLuint i;
2034 for (i = 0; i < n; i ++) {
2035 GLuint p = uisrc[i];
2036 SWAP4BYTE(p);
2037 rgba[i][rComp] = ((p >> 22) ) * (1.0F / 1023.0F);
2038 rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
2039 rgba[i][bComp] = ((p >> 2) & 0x3ff) * (1.0F / 1023.0F);
2040 rgba[i][aComp] = ((p ) & 0x3 ) * (1.0F / 3.0F);
2041 }
2042 }
2043 else {
2044 const GLuint *uisrc = (const GLuint *) src;
2045 GLuint i;
2046 for (i = 0; i < n; i ++) {
2047 GLuint p = uisrc[i];
2048 rgba[i][rComp] = ((p >> 22) ) * (1.0F / 1023.0F);
2049 rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
2050 rgba[i][bComp] = ((p >> 2) & 0x3ff) * (1.0F / 1023.0F);
2051 rgba[i][aComp] = ((p ) & 0x3 ) * (1.0F / 3.0F);
2052 }
2053 }
2054 break;
2055 case GL_UNSIGNED_INT_2_10_10_10_REV:
2056 if (swapBytes) {
2057 const GLuint *uisrc = (const GLuint *) src;
2058 GLuint i;
2059 for (i = 0; i < n; i ++) {
2060 GLuint p = uisrc[i];
2061 SWAP4BYTE(p);
2062 rgba[i][rComp] = ((p ) & 0x3ff) * (1.0F / 1023.0F);
2063 rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
2064 rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
2065 rgba[i][aComp] = ((p >> 30) ) * (1.0F / 3.0F);
2066 }
2067 }
2068 else {
2069 const GLuint *uisrc = (const GLuint *) src;
2070 GLuint i;
2071 for (i = 0; i < n; i ++) {
2072 GLuint p = uisrc[i];
2073 rgba[i][rComp] = ((p ) & 0x3ff) * (1.0F / 1023.0F);
2074 rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
2075 rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
2076 rgba[i][aComp] = ((p >> 30) ) * (1.0F / 3.0F);
2077 }
2078 }
2079 break;
2080 default:
2081 gl_problem(NULL, "bad srcType in extract float data");
2082 break;
2083 }
2084 }
2085
2086
2087
2088 /*
2089 * Unpack a row of color image data from a client buffer according to
2090 * the pixel unpacking parameters. Apply any enabled pixel transfer
2091 * ops (PixelMap, scale/bias) if the applyTransferOps flag is enabled.
2092 * Return GLubyte values in the specified dest image format.
2093 * This is (or will be) used by glDrawPixels and glTexImage?D().
2094 * Input: ctx - the context
2095 * n - number of pixels in the span
2096 * dstFormat - format of destination color array
2097 * dest - the destination color array
2098 * srcFormat - source image format
2099 * srcType - source image datatype
2100 * source - source image pointer
2101 * unpacking - pixel unpacking parameters
2102 * applyTransferOps - apply scale/bias/lookup-table ops?
2103 *
2104 * XXX perhaps expand this to process whole images someday.
2105 */
2106 void
2107 _mesa_unpack_ubyte_color_span( const GLcontext *ctx,
2108 GLuint n, GLenum dstFormat, GLubyte dest[],
2109 GLenum srcFormat, GLenum srcType,
2110 const GLvoid *source,
2111 const struct gl_pixelstore_attrib *unpacking,
2112 GLboolean applyTransferOps )
2113 {
2114 ASSERT(dstFormat == GL_ALPHA ||
2115 dstFormat == GL_LUMINANCE ||
2116 dstFormat == GL_LUMINANCE_ALPHA ||
2117 dstFormat == GL_INTENSITY ||
2118 dstFormat == GL_RGB ||
2119 dstFormat == GL_RGBA ||
2120 dstFormat == GL_COLOR_INDEX);
2121
2122 ASSERT(srcFormat == GL_RED ||
2123 srcFormat == GL_GREEN ||
2124 srcFormat == GL_BLUE ||
2125 srcFormat == GL_ALPHA ||
2126 srcFormat == GL_LUMINANCE ||
2127 srcFormat == GL_LUMINANCE_ALPHA ||
2128 srcFormat == GL_INTENSITY ||
2129 srcFormat == GL_RGB ||
2130 srcFormat == GL_BGR ||
2131 srcFormat == GL_RGBA ||
2132 srcFormat == GL_BGRA ||
2133 srcFormat == GL_ABGR_EXT ||
2134 srcFormat == GL_COLOR_INDEX);
2135
2136 ASSERT(srcType == GL_BITMAP ||
2137 srcType == GL_UNSIGNED_BYTE ||
2138 srcType == GL_BYTE ||
2139 srcType == GL_UNSIGNED_SHORT ||
2140 srcType == GL_SHORT ||
2141 srcType == GL_UNSIGNED_INT ||
2142 srcType == GL_INT ||
2143 srcType == GL_FLOAT ||
2144 srcType == GL_UNSIGNED_BYTE_3_3_2 ||
2145 srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
2146 srcType == GL_UNSIGNED_SHORT_5_6_5 ||
2147 srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
2148 srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
2149 srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
2150 srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
2151 srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
2152 srcType == GL_UNSIGNED_INT_8_8_8_8 ||
2153 srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
2154 srcType == GL_UNSIGNED_INT_10_10_10_2 ||
2155 srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
2156
2157 /* this is intended for RGBA mode only */
2158 assert(ctx->Visual->RGBAflag);
2159
2160 applyTransferOps &= (ctx->Pixel.ScaleOrBiasRGBA ||
2161 ctx->Pixel.MapColorFlag ||
2162 ctx->ColorMatrix.type != MATRIX_IDENTITY ||
2163 ctx->Pixel.ScaleOrBiasRGBApcm);
2164
2165 /* Try simple cases first */
2166 if (!applyTransferOps && srcType == GL_UNSIGNED_BYTE) {
2167 if (dstFormat == GL_RGBA) {
2168 if (srcFormat == GL_RGBA) {
2169 MEMCPY( dest, source, n * 4 * sizeof(GLubyte) );
2170 return;
2171 }
2172 else if (srcFormat == GL_RGB) {
2173 GLuint i;
2174 const GLubyte *src = (const GLubyte *) source;
2175 GLubyte *dst = dest;
2176 for (i = 0; i < n; i++) {
2177 dst[0] = src[0];
2178 dst[1] = src[1];
2179 dst[2] = src[2];
2180 dst[3] = 255;
2181 src += 3;
2182 dst += 4;
2183 }
2184 return;
2185 }
2186 }
2187 else if (dstFormat == GL_RGB) {
2188 if (srcFormat == GL_RGB) {
2189 MEMCPY( dest, source, n * 3 * sizeof(GLubyte) );
2190 return;
2191 }
2192 else if (srcFormat == GL_RGBA) {
2193 GLuint i;
2194 const GLubyte *src = (const GLubyte *) source;
2195 GLubyte *dst = dest;
2196 for (i = 0; i < n; i++) {
2197 dst[0] = src[0];
2198 dst[1] = src[1];
2199 dst[2] = src[2];
2200 src += 4;
2201 dst += 3;
2202 }
2203 return;
2204 }
2205 }
2206 else if (dstFormat == srcFormat) {
2207 GLint comps = _mesa_components_in_format(srcFormat);
2208 assert(comps > 0);
2209 MEMCPY( dest, source, n * comps * sizeof(GLubyte) );
2210 return;
2211 }
2212 }
2213
2214
2215 {
2216 /* general solution */
2217 GLfloat rgba[MAX_WIDTH][4];
2218 GLint dstComponents;
2219 GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
2220 GLint dstLuminanceIndex, dstIntensityIndex;
2221
2222 dstComponents = _mesa_components_in_format( dstFormat );
2223 /* source & dest image formats should have been error checked by now */
2224 assert(dstComponents > 0);
2225
2226 /*
2227 * Extract image data and convert to RGBA floats
2228 */
2229 assert(n <= MAX_WIDTH);
2230 if (srcFormat == GL_COLOR_INDEX) {
2231 GLuint indexes[MAX_WIDTH];
2232 extract_uint_indexes(n, indexes, srcFormat, srcType, source,
2233 unpacking);
2234
2235 /* shift and offset indexes */
2236 gl_shift_and_offset_ci(ctx, n, indexes);
2237
2238 if (dstFormat == GL_COLOR_INDEX) {
2239 if (applyTransferOps) {
2240 if (ctx->Pixel.MapColorFlag) {
2241 /* Apply lookup table */
2242 gl_map_ci(ctx, n, indexes);
2243 }
2244
2245 if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset) {
2246
2247 }
2248 }
2249
2250 /* convert to GLubyte and return */
2251 {
2252 GLuint i;
2253 for (i = 0; i < n; i++) {
2254 dest[i] = (GLubyte) (indexes[i] & 0xff);
2255 }
2256 }
2257 }
2258 else {
2259 /* Convert indexes to RGBA */
2260 gl_map_ci_to_rgba_float(ctx, n, indexes, rgba);
2261 }
2262 }
2263 else {
2264 extract_float_rgba(n, rgba, srcFormat, srcType, source,
2265 unpacking->SwapBytes);
2266
2267 if (applyTransferOps) {
2268 /* scale and bias colors */
2269 _mesa_scale_and_bias_rgba_float(ctx, n, rgba);
2270
2271 /* color table lookup */
2272 if (ctx->Pixel.MapColorFlag) {
2273 _mesa_map_rgba_float(ctx, n, rgba);
2274 }
2275
2276 if (ctx->ColorMatrix.type != MATRIX_IDENTITY ||
2277 ctx->Pixel.ScaleOrBiasRGBApcm) {
2278 _mesa_transform_rgba(ctx, n, rgba);
2279 }
2280 }
2281 }
2282
2283
2284 /*
2285 * XXX This is where more color table lookups, convolution,
2286 * histograms, minmax, color matrix, etc would take place if
2287 * implemented.
2288 * See figure 3.7 in the OpenGL 1.2 specification for more info.
2289 */
2290
2291
2292 /* clamp to [0,1] */
2293 {
2294 GLuint i;
2295 for (i = 0; i < n; i++) {
2296 rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F);
2297 rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F);
2298 rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F);
2299 rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F);
2300 }
2301 }
2302
2303 /* Now determine which color channels we need to produce.
2304 * And determine the dest index (offset) within each color tuple.
2305 */
2306 switch (dstFormat) {
2307 case GL_ALPHA:
2308 dstAlphaIndex = 0;
2309 dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
2310 dstLuminanceIndex = dstIntensityIndex = -1;
2311 break;
2312 case GL_LUMINANCE:
2313 dstLuminanceIndex = 0;
2314 dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
2315 dstIntensityIndex = -1;
2316 break;
2317 case GL_LUMINANCE_ALPHA:
2318 dstLuminanceIndex = 0;
2319 dstAlphaIndex = 1;
2320 dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
2321 dstIntensityIndex = -1;
2322 break;
2323 case GL_INTENSITY:
2324 dstIntensityIndex = 0;
2325 dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
2326 dstLuminanceIndex = -1;
2327 break;
2328 case GL_RGB:
2329 dstRedIndex = 0;
2330 dstGreenIndex = 1;
2331 dstBlueIndex = 2;
2332 dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
2333 break;
2334 case GL_RGBA:
2335 dstRedIndex = 0;
2336 dstGreenIndex = 1;
2337 dstBlueIndex = 2;
2338 dstAlphaIndex = 3;
2339 dstLuminanceIndex = dstIntensityIndex = -1;
2340 break;
2341 default:
2342 gl_problem(ctx, "bad dstFormat in _mesa_unpack_ubyte_span()");
2343 return;
2344 }
2345
2346
2347 /* Now return the GLubyte data in the requested dstFormat */
2348
2349 if (dstRedIndex >= 0) {
2350 GLubyte *dst = dest;
2351 GLuint i;
2352 for (i = 0; i < n; i++) {
2353 dst[dstRedIndex] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
2354 dst += dstComponents;
2355 }
2356 }
2357
2358 if (dstGreenIndex >= 0) {
2359 GLubyte *dst = dest;
2360 GLuint i;
2361 for (i = 0; i < n; i++) {
2362 dst[dstGreenIndex] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
2363 dst += dstComponents;
2364 }
2365 }
2366
2367 if (dstBlueIndex >= 0) {
2368 GLubyte *dst = dest;
2369 GLuint i;
2370 for (i = 0; i < n; i++) {
2371 dst[dstBlueIndex] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
2372 dst += dstComponents;
2373 }
2374 }
2375
2376 if (dstAlphaIndex >= 0) {
2377 GLubyte *dst = dest;
2378 GLuint i;
2379 for (i = 0; i < n; i++) {
2380 dst[dstAlphaIndex] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
2381 dst += dstComponents;
2382 }
2383 }
2384
2385 if (dstIntensityIndex >= 0) {
2386 GLubyte *dst = dest;
2387 GLuint i;
2388 assert(dstIntensityIndex == 0);
2389 assert(dstComponents == 1);
2390 for (i = 0; i < n; i++) {
2391 /* Intensity comes from red channel */
2392 dst[i] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
2393 }
2394 }
2395
2396 if (dstLuminanceIndex >= 0) {
2397 GLubyte *dst = dest;
2398 GLuint i;
2399 assert(dstLuminanceIndex == 0);
2400 for (i = 0; i < n; i++) {
2401 /* Luminance comes from red channel */
2402 dst[0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
2403 dst += dstComponents;
2404 }
2405 }
2406 }
2407 }
2408
2409
2410
2411 /*
2412 * Unpack a row of color index data from a client buffer according to
2413 * the pixel unpacking parameters. Apply pixel transfer ops if enabled
2414 * and applyTransferOps is true.
2415 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
2416 *
2417 * Args: ctx - the context
2418 * n - number of pixels
2419 * dstType - destination datatype
2420 * dest - destination array
2421 * srcType - source pixel type
2422 * source - source data pointer
2423 * unpacking - pixel unpacking parameters
2424 * applyTransferOps - apply offset/bias/lookup ops?
2425 */
2426 void
2427 _mesa_unpack_index_span( const GLcontext *ctx, GLuint n,
2428 GLenum dstType, GLvoid *dest,
2429 GLenum srcType, const GLvoid *source,
2430 const struct gl_pixelstore_attrib *unpacking,
2431 GLboolean applyTransferOps )
2432 {
2433 ASSERT(srcType == GL_BITMAP ||
2434 srcType == GL_UNSIGNED_BYTE ||
2435 srcType == GL_BYTE ||
2436 srcType == GL_UNSIGNED_SHORT ||
2437 srcType == GL_SHORT ||
2438 srcType == GL_UNSIGNED_INT ||
2439 srcType == GL_INT ||
2440 srcType == GL_FLOAT);
2441
2442 ASSERT(dstType == GL_UNSIGNED_BYTE ||
2443 dstType == GL_UNSIGNED_SHORT ||
2444 dstType == GL_UNSIGNED_INT);
2445
2446 applyTransferOps &= (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset || ctx->Pixel.MapColorFlag);
2447
2448 /*
2449 * Try simple cases first
2450 */
2451 if (!applyTransferOps && srcType == GL_UNSIGNED_BYTE
2452 && dstType == GL_UNSIGNED_BYTE) {
2453 MEMCPY(dest, source, n * sizeof(GLubyte));
2454 }
2455 else if (!applyTransferOps && srcType == GL_UNSIGNED_INT
2456 && dstType == GL_UNSIGNED_INT && !unpacking->SwapBytes) {
2457 MEMCPY(dest, source, n * sizeof(GLuint));
2458 }
2459 else {
2460 /*
2461 * general solution
2462 */
2463 GLuint indexes[MAX_WIDTH];
2464 assert(n <= MAX_WIDTH);
2465
2466 extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
2467 unpacking);
2468
2469 if (applyTransferOps) {
2470 if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset) {
2471 /* shift and offset indexes */
2472 gl_shift_and_offset_ci(ctx, n, indexes);
2473 }
2474
2475 if (ctx->Pixel.MapColorFlag) {
2476 /* Apply lookup table */
2477 gl_map_ci(ctx, n, indexes);
2478 }
2479 }
2480
2481 /* convert to dest type */
2482 switch (dstType) {
2483 case GL_UNSIGNED_BYTE:
2484 {
2485 GLubyte *dst = (GLubyte *) dest;
2486 GLuint i;
2487 for (i = 0; i < n; i++) {
2488 dst[i] = (GLubyte) (indexes[i] & 0xff);
2489 }
2490 }
2491 break;
2492 case GL_UNSIGNED_SHORT:
2493 {
2494 GLuint *dst = (GLuint *) dest;
2495 GLuint i;
2496 for (i = 0; i < n; i++) {
2497 dst[i] = (GLushort) (indexes[i] & 0xffff);
2498 }
2499 }
2500 break;
2501 case GL_UNSIGNED_INT:
2502 MEMCPY(dest, indexes, n * sizeof(GLuint));
2503 break;
2504 default:
2505 gl_problem(ctx, "bad dstType in _mesa_unpack_index_span");
2506 }
2507 }
2508 }
2509
2510
2511 /*
2512 * Unpack a row of stencil data from a client buffer according to
2513 * the pixel unpacking parameters. Apply pixel transfer ops if enabled
2514 * and applyTransferOps is true.
2515 * This is (or will be) used by glDrawPixels
2516 *
2517 * Args: ctx - the context
2518 * n - number of pixels
2519 * dstType - destination datatype
2520 * dest - destination array
2521 * srcType - source pixel type
2522 * source - source data pointer
2523 * unpacking - pixel unpacking parameters
2524 * applyTransferOps - apply offset/bias/lookup ops?
2525 */
2526 void
2527 _mesa_unpack_stencil_span( const GLcontext *ctx, GLuint n,
2528 GLenum dstType, GLvoid *dest,
2529 GLenum srcType, const GLvoid *source,
2530 const struct gl_pixelstore_attrib *unpacking,
2531 GLboolean applyTransferOps )
2532 {
2533 ASSERT(srcType == GL_BITMAP ||
2534 srcType == GL_UNSIGNED_BYTE ||
2535 srcType == GL_BYTE ||
2536 srcType == GL_UNSIGNED_SHORT ||
2537 srcType == GL_SHORT ||
2538 srcType == GL_UNSIGNED_INT ||
2539 srcType == GL_INT ||
2540 srcType == GL_FLOAT);
2541
2542 ASSERT(dstType == GL_UNSIGNED_BYTE ||
2543 dstType == GL_UNSIGNED_SHORT ||
2544 dstType == GL_UNSIGNED_INT);
2545
2546 applyTransferOps &= (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset || ctx->Pixel.MapColorFlag);
2547
2548 /*
2549 * Try simple cases first
2550 */
2551 if (!applyTransferOps && srcType == GL_UNSIGNED_BYTE
2552 && dstType == GL_UNSIGNED_BYTE) {
2553 MEMCPY(dest, source, n * sizeof(GLubyte));
2554 }
2555 else if (!applyTransferOps && srcType == GL_UNSIGNED_INT
2556 && dstType == GL_UNSIGNED_INT && !unpacking->SwapBytes) {
2557 MEMCPY(dest, source, n * sizeof(GLuint));
2558 }
2559 else {
2560 /*
2561 * general solution
2562 */
2563 GLuint indexes[MAX_WIDTH];
2564 assert(n <= MAX_WIDTH);
2565
2566 extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
2567 unpacking);
2568
2569 if (applyTransferOps) {
2570 if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset) {
2571 /* shift and offset indexes */
2572 gl_shift_and_offset_ci(ctx, n, indexes);
2573 }
2574
2575 if (ctx->Pixel.MapStencilFlag) {
2576 /* Apply stencil lookup table */
2577 GLuint mask = ctx->Pixel.MapStoSsize - 1;
2578 GLuint i;
2579 for (i=0;i<n;i++) {
2580 indexes[i] = ctx->Pixel.MapStoS[ indexes[i] & mask ];
2581 }
2582 }
2583 }
2584
2585 /* convert to dest type */
2586 switch (dstType) {
2587 case GL_UNSIGNED_BYTE:
2588 {
2589 GLubyte *dst = (GLubyte *) dest;
2590 GLuint i;
2591 for (i = 0; i < n; i++) {
2592 dst[i] = (GLubyte) (indexes[i] & 0xff);
2593 }
2594 }
2595 break;
2596 case GL_UNSIGNED_SHORT:
2597 {
2598 GLuint *dst = (GLuint *) dest;
2599 GLuint i;
2600 for (i = 0; i < n; i++) {
2601 dst[i] = (GLushort) (indexes[i] & 0xffff);
2602 }
2603 }
2604 break;
2605 case GL_UNSIGNED_INT:
2606 MEMCPY(dest, indexes, n * sizeof(GLuint));
2607 break;
2608 default:
2609 gl_problem(ctx, "bad dstType in _mesa_unpack_stencil_span");
2610 }
2611 }
2612 }
2613
2614
2615
2616 void
2617 _mesa_unpack_depth_span( const GLcontext *ctx, GLuint n, GLdepth *dest,
2618 GLenum srcType, const GLvoid *source,
2619 const struct gl_pixelstore_attrib *unpacking,
2620 GLboolean applyTransferOps )
2621 {
2622 GLfloat *depth = MALLOC(n * sizeof(GLfloat));
2623 if (!depth)
2624 return;
2625
2626 switch (srcType) {
2627 case GL_BYTE:
2628 {
2629 GLuint i;
2630 const GLubyte *src = (const GLubyte *) source;
2631 for (i = 0; i < n; i++) {
2632 depth[i] = BYTE_TO_FLOAT(src[i]);
2633 }
2634 }
2635 break;
2636 case GL_UNSIGNED_BYTE:
2637 {
2638 GLuint i;
2639 const GLubyte *src = (const GLubyte *) source;
2640 for (i = 0; i < n; i++) {
2641 depth[i] = UBYTE_TO_FLOAT(src[i]);
2642 }
2643 }
2644 break;
2645 case GL_SHORT:
2646 {
2647 GLuint i;
2648 const GLshort *src = (const GLshort *) source;
2649 for (i = 0; i < n; i++) {
2650 depth[i] = SHORT_TO_FLOAT(src[i]);
2651 }
2652 }
2653 break;
2654 case GL_UNSIGNED_SHORT:
2655 {
2656 GLuint i;
2657 const GLushort *src = (const GLushort *) source;
2658 for (i = 0; i < n; i++) {
2659 depth[i] = USHORT_TO_FLOAT(src[i]);
2660 }
2661 }
2662 break;
2663 case GL_INT:
2664 {
2665 GLuint i;
2666 const GLint *src = (const GLint *) source;
2667 for (i = 0; i < n; i++) {
2668 depth[i] = INT_TO_FLOAT(src[i]);
2669 }
2670 }
2671 break;
2672 case GL_UNSIGNED_INT:
2673 {
2674 GLuint i;
2675 const GLuint *src = (const GLuint *) source;
2676 for (i = 0; i < n; i++) {
2677 depth[i] = UINT_TO_FLOAT(src[i]);
2678 }
2679 }
2680 break;
2681 case GL_FLOAT:
2682 MEMCPY(depth, source, n * sizeof(GLfloat));
2683 break;
2684 default:
2685 gl_problem(NULL, "bad type in _mesa_unpack_depth_span()");
2686 return;
2687 }
2688
2689
2690 /* apply depth scale and bias */
2691 if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
2692 GLuint i;
2693 for (i = 0; i < n; i++) {
2694 depth[i] = depth[i] * ctx->Pixel.DepthScale + ctx->Pixel.DepthBias;
2695 }
2696 }
2697
2698 /* clamp depth values to [0,1] and convert from floats to integers */
2699 {
2700 const GLfloat zs = ctx->Visual->DepthMaxF;
2701 GLuint i;
2702 for (i = 0; i < n; i++) {
2703 dest[i] = (GLdepth) (CLAMP(depth[i], 0.0F, 1.0F) * zs);
2704 }
2705 }
2706
2707 FREE(depth);
2708 }
2709
2710
2711
2712 /*
2713 * Unpack image data. Apply byteswapping, byte flipping (bitmap).
2714 * Return all image data in a contiguous block.
2715 */
2716 void *
2717 _mesa_unpack_image( GLsizei width, GLsizei height, GLsizei depth,
2718 GLenum format, GLenum type, const GLvoid *pixels,
2719 const struct gl_pixelstore_attrib *unpack )
2720 {
2721 GLint bytesPerRow, compsPerRow;
2722 GLboolean flipBytes, swap2, swap4;
2723
2724 if (!pixels)
2725 return NULL; /* not necessarily an error */
2726
2727 if (width <= 0 || height <= 0 || depth <= 0)
2728 return NULL; /* generate error later */
2729
2730 if (format == GL_BITMAP) {
2731 bytesPerRow = (width + 7) >> 3;
2732 flipBytes = !unpack->LsbFirst;
2733 swap2 = swap4 = GL_FALSE;
2734 compsPerRow = 0;
2735 }
2736 else {
2737 const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
2738 const GLint components = _mesa_components_in_format(format);
2739 GLint bytesPerComp;
2740 if (bytesPerPixel <= 0 || components <= 0)
2741 return NULL; /* bad format or type. generate error later */
2742 bytesPerRow = bytesPerPixel * width;
2743 bytesPerComp = bytesPerPixel / components;
2744 flipBytes = GL_FALSE;
2745 swap2 = (bytesPerComp == 2) && unpack->SwapBytes;
2746 swap4 = (bytesPerComp == 4) && unpack->SwapBytes;
2747 compsPerRow = components * width;
2748 assert(compsPerRow >= width);
2749 }
2750
2751 {
2752 GLubyte *destBuffer = MALLOC(bytesPerRow * height * depth);
2753 GLubyte *dst;
2754 GLint img, row;
2755 if (!destBuffer)
2756 return NULL; /* generate GL_OUT_OF_MEMORY later */
2757
2758 dst = destBuffer;
2759 for (img = 0; img < depth; img++) {
2760 for (row = 0; row < height; row++) {
2761 const GLvoid *src = _mesa_image_address(unpack, pixels,
2762 width, height, format, type, img, row, 0);
2763 MEMCPY(dst, src, bytesPerRow);
2764 /* byte flipping/swapping */
2765 if (flipBytes) {
2766 flip_bytes((GLubyte *) dst, bytesPerRow);
2767 }
2768 else if (swap2) {
2769 _mesa_swap2((GLushort*) dst, compsPerRow);
2770 }
2771 else if (swap4) {
2772 _mesa_swap4((GLuint*) dst, compsPerRow);
2773 }
2774 dst += bytesPerRow;
2775 }
2776 }
2777 return destBuffer;
2778 }
2779 }
2780
2781
2782 /*
2783 * Unpack bitmap data. Resulting data will be in most-significant-bit-first
2784 * order with row alignment = 1 byte.
2785 */
2786 GLvoid *
2787 _mesa_unpack_bitmap( GLint width, GLint height, const GLubyte *pixels,
2788 const struct gl_pixelstore_attrib *packing )
2789 {
2790 GLint bytes, row, width_in_bytes;
2791 GLubyte *buffer, *dst;
2792
2793 if (!pixels)
2794 return NULL;
2795
2796 /* Alloc dest storage */
2797 bytes = ((width + 7) / 8 * height);
2798 buffer = (GLubyte *) MALLOC( bytes );
2799 if (!buffer)
2800 return NULL;
2801
2802
2803 width_in_bytes = CEILING( width, 8 );
2804 dst = buffer;
2805 for (row = 0; row < height; row++) {
2806 GLubyte *src = _mesa_image_address( packing, pixels, width, height,
2807 GL_COLOR_INDEX, GL_BITMAP,
2808 0, row, 0 );
2809 if (!src) {
2810 FREE(buffer);
2811 return NULL;
2812 }
2813
2814 if (packing->SkipPixels == 0) {
2815 MEMCPY( dst, src, width_in_bytes );
2816 if (packing->LsbFirst) {
2817 flip_bytes( dst, width_in_bytes );
2818 }
2819 }
2820 else {
2821 /* handling SkipPixels is a bit tricky (no pun intended!) */
2822 GLint i;
2823 if (packing->LsbFirst) {
2824 GLubyte srcMask = 1 << (packing->SkipPixels & 0x7);
2825 GLubyte dstMask = 128;
2826 GLubyte *s = src;
2827 GLubyte *d = dst;
2828 *d = 0;
2829 for (i = 0; i < width; i++) {
2830 if (*s & srcMask) {
2831 *d |= dstMask;
2832 }
2833 if (srcMask == 128) {
2834 srcMask = 1;
2835 s++;
2836 }
2837 else {
2838 srcMask = srcMask << 1;
2839 }
2840 if (dstMask == 1) {
2841 dstMask = 128;
2842 d++;
2843 *d = 0;
2844 }
2845 else {
2846 dstMask = dstMask >> 1;
2847 }
2848 }
2849 }
2850 else {
2851 GLubyte srcMask = 128 >> (packing->SkipPixels & 0x7);
2852 GLubyte dstMask = 128;
2853 GLubyte *s = src;
2854 GLubyte *d = dst;
2855 *d = 0;
2856 for (i = 0; i < width; i++) {
2857 if (*s & srcMask) {
2858 *d |= dstMask;
2859 }
2860 if (srcMask == 1) {
2861 srcMask = 128;
2862 s++;
2863 }
2864 else {
2865 srcMask = srcMask >> 1;
2866 }
2867 if (dstMask == 1) {
2868 dstMask = 128;
2869 d++;
2870 *d = 0;
2871 }
2872 else {
2873 dstMask = dstMask >> 1;
2874 }
2875 }
2876 }
2877 }
2878 dst += width_in_bytes;
2879 }
2880
2881 return buffer;
2882 }
2883
2884
2885 /*
2886 * Pack bitmap data.
2887 */
2888 void
2889 _mesa_pack_bitmap( GLint width, GLint height, const GLubyte *source,
2890 GLubyte *dest, const struct gl_pixelstore_attrib *packing )
2891 {
2892 GLint row, width_in_bytes;
2893 const GLubyte *src;
2894
2895 if (!source)
2896 return;
2897
2898 width_in_bytes = CEILING( width, 8 );
2899 src = source;
2900 for (row = 0; row < height; row++) {
2901 GLubyte *dst = _mesa_image_address( packing, dest, width, height,
2902 GL_COLOR_INDEX, GL_BITMAP,
2903 0, row, 0 );
2904 if (!dst)
2905 return;
2906
2907 if (packing->SkipPixels == 0) {
2908 MEMCPY( dst, src, width_in_bytes );
2909 if (packing->LsbFirst) {
2910 flip_bytes( dst, width_in_bytes );
2911 }
2912 }
2913 else {
2914 /* handling SkipPixels is a bit tricky (no pun intended!) */
2915 GLint i;
2916 if (packing->LsbFirst) {
2917 GLubyte srcMask = 1 << (packing->SkipPixels & 0x7);
2918 GLubyte dstMask = 128;
2919 const GLubyte *s = src;
2920 GLubyte *d = dst;
2921 *d = 0;
2922 for (i = 0; i < width; i++) {
2923 if (*s & srcMask) {
2924 *d |= dstMask;
2925 }
2926 if (srcMask == 128) {
2927 srcMask = 1;
2928 s++;
2929 }
2930 else {
2931 srcMask = srcMask << 1;
2932 }
2933 if (dstMask == 1) {
2934 dstMask = 128;
2935 d++;
2936 *d = 0;
2937 }
2938 else {
2939 dstMask = dstMask >> 1;
2940 }
2941 }
2942 }
2943 else {
2944 GLubyte srcMask = 128 >> (packing->SkipPixels & 0x7);
2945 GLubyte dstMask = 128;
2946 const GLubyte *s = src;
2947 GLubyte *d = dst;
2948 *d = 0;
2949 for (i = 0; i < width; i++) {
2950 if (*s & srcMask) {
2951 *d |= dstMask;
2952 }
2953 if (srcMask == 1) {
2954 srcMask = 128;
2955 s++;
2956 }
2957 else {
2958 srcMask = srcMask >> 1;
2959 }
2960 if (dstMask == 1) {
2961 dstMask = 128;
2962 d++;
2963 *d = 0;
2964 }
2965 else {
2966 dstMask = dstMask >> 1;
2967 }
2968 }
2969 }
2970 }
2971 src += width_in_bytes;
2972 }
2973 }