use BCOPY macro on FreeBSD
[mesa.git] / src / mesa / main / image.c
1 /* $Id: image.c,v 1.24 2000/03/29 15:56:53 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 */
2158 assert(ctx->Visual->RGBAflag);
2159
2160 applyTransferOps &= (ctx->Pixel.ScaleOrBiasRGBA ||
2161 ctx->Pixel.MapColorFlag ||
2162 ctx->Pixel.MapColorFlag);
2163
2164 /* Try simple cases first */
2165 if (!applyTransferOps && srcType == GL_UNSIGNED_BYTE) {
2166 if (dstFormat == GL_RGBA) {
2167 if (srcFormat == GL_RGBA) {
2168 MEMCPY( dest, source, n * 4 * sizeof(GLubyte) );
2169 return;
2170 }
2171 else if (srcFormat == GL_RGB) {
2172 GLuint i;
2173 const GLubyte *src = (const GLubyte *) source;
2174 GLubyte *dst = dest;
2175 for (i = 0; i < n; i++) {
2176 dst[0] = src[0];
2177 dst[1] = src[1];
2178 dst[2] = src[2];
2179 dst[3] = 255;
2180 src += 3;
2181 dst += 4;
2182 }
2183 return;
2184 }
2185 }
2186 else if (dstFormat == GL_RGB) {
2187 if (srcFormat == GL_RGB) {
2188 MEMCPY( dest, source, n * 3 * sizeof(GLubyte) );
2189 return;
2190 }
2191 else if (srcFormat == GL_RGBA) {
2192 GLuint i;
2193 const GLubyte *src = (const GLubyte *) source;
2194 GLubyte *dst = dest;
2195 for (i = 0; i < n; i++) {
2196 dst[0] = src[0];
2197 dst[1] = src[1];
2198 dst[2] = src[2];
2199 src += 4;
2200 dst += 3;
2201 }
2202 return;
2203 }
2204 }
2205 else if (dstFormat == srcFormat) {
2206 GLint comps = _mesa_components_in_format(srcFormat);
2207 assert(comps > 0);
2208 MEMCPY( dest, source, n * comps * sizeof(GLubyte) );
2209 return;
2210 }
2211 }
2212
2213
2214 {
2215 /* general solution */
2216 GLfloat rgba[MAX_WIDTH][4];
2217 GLint dstComponents;
2218 GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
2219 GLint dstLuminanceIndex, dstIntensityIndex;
2220
2221 dstComponents = _mesa_components_in_format( dstFormat );
2222 /* source & dest image formats should have been error checked by now */
2223 assert(dstComponents > 0);
2224
2225 /*
2226 * Extract image data and convert to RGBA floats
2227 */
2228 assert(n <= MAX_WIDTH);
2229 if (srcFormat == GL_COLOR_INDEX) {
2230 GLuint indexes[MAX_WIDTH];
2231 extract_uint_indexes(n, indexes, srcFormat, srcType, source,
2232 unpacking);
2233
2234 /* shift and offset indexes */
2235 gl_shift_and_offset_ci(ctx, n, indexes);
2236
2237 if (dstFormat == GL_COLOR_INDEX) {
2238 if (applyTransferOps) {
2239 if (ctx->Pixel.MapColorFlag) {
2240 /* Apply lookup table */
2241 gl_map_ci(ctx, n, indexes);
2242 }
2243
2244 if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset) {
2245
2246 }
2247 }
2248
2249 /* convert to GLubyte and return */
2250 {
2251 GLuint i;
2252 for (i = 0; i < n; i++) {
2253 dest[i] = (GLubyte) (indexes[i] & 0xff);
2254 }
2255 }
2256 }
2257 else {
2258 /* Convert indexes to RGBA */
2259 gl_map_ci_to_rgba_float(ctx, n, indexes, rgba);
2260 }
2261 }
2262 else {
2263 extract_float_rgba(n, rgba, srcFormat, srcType, source,
2264 unpacking->SwapBytes);
2265
2266 if (applyTransferOps) {
2267 /* scale and bias colors */
2268 gl_scale_and_bias_rgba_float(ctx, n, rgba);
2269
2270 /* color table lookup */
2271 if (ctx->Pixel.MapColorFlag) {
2272 gl_map_rgba_float(ctx, n, rgba);
2273 }
2274 }
2275 }
2276
2277
2278 /*
2279 * XXX This is where more color table lookups, convolution,
2280 * histograms, minmax, color matrix, etc would take place if
2281 * implemented.
2282 * See figure 3.7 in the OpenGL 1.2 specification for more info.
2283 */
2284
2285
2286 /* clamp to [0,1] */
2287 {
2288 GLuint i;
2289 for (i = 0; i < n; i++) {
2290 rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F);
2291 rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F);
2292 rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F);
2293 rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F);
2294 }
2295 }
2296
2297 /* Now determine which color channels we need to produce.
2298 * And determine the dest index (offset) within each color tuple.
2299 */
2300 switch (dstFormat) {
2301 case GL_ALPHA:
2302 dstAlphaIndex = 0;
2303 dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
2304 dstLuminanceIndex = dstIntensityIndex = -1;
2305 break;
2306 case GL_LUMINANCE:
2307 dstLuminanceIndex = 0;
2308 dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
2309 dstIntensityIndex = -1;
2310 break;
2311 case GL_LUMINANCE_ALPHA:
2312 dstLuminanceIndex = 0;
2313 dstAlphaIndex = 1;
2314 dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
2315 dstIntensityIndex = -1;
2316 break;
2317 case GL_INTENSITY:
2318 dstIntensityIndex = 0;
2319 dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
2320 dstLuminanceIndex = -1;
2321 break;
2322 case GL_RGB:
2323 dstRedIndex = 0;
2324 dstGreenIndex = 1;
2325 dstBlueIndex = 2;
2326 dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
2327 break;
2328 case GL_RGBA:
2329 dstRedIndex = 0;
2330 dstGreenIndex = 1;
2331 dstBlueIndex = 2;
2332 dstAlphaIndex = 3;
2333 dstLuminanceIndex = dstIntensityIndex = -1;
2334 break;
2335 default:
2336 gl_problem(ctx, "bad dstFormat in _mesa_unpack_ubyte_span()");
2337 return;
2338 }
2339
2340
2341 /* Now return the GLubyte data in the requested dstFormat */
2342
2343 if (dstRedIndex >= 0) {
2344 GLubyte *dst = dest;
2345 GLuint i;
2346 for (i = 0; i < n; i++) {
2347 dst[dstRedIndex] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
2348 dst += dstComponents;
2349 }
2350 }
2351
2352 if (dstGreenIndex >= 0) {
2353 GLubyte *dst = dest;
2354 GLuint i;
2355 for (i = 0; i < n; i++) {
2356 dst[dstGreenIndex] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
2357 dst += dstComponents;
2358 }
2359 }
2360
2361 if (dstBlueIndex >= 0) {
2362 GLubyte *dst = dest;
2363 GLuint i;
2364 for (i = 0; i < n; i++) {
2365 dst[dstBlueIndex] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
2366 dst += dstComponents;
2367 }
2368 }
2369
2370 if (dstAlphaIndex >= 0) {
2371 GLubyte *dst = dest;
2372 GLuint i;
2373 for (i = 0; i < n; i++) {
2374 dst[dstAlphaIndex] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
2375 dst += dstComponents;
2376 }
2377 }
2378
2379 if (dstIntensityIndex >= 0) {
2380 GLubyte *dst = dest;
2381 GLuint i;
2382 assert(dstIntensityIndex == 0);
2383 assert(dstComponents == 1);
2384 for (i = 0; i < n; i++) {
2385 /* Intensity comes from red channel */
2386 dst[i] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
2387 }
2388 }
2389
2390 if (dstLuminanceIndex >= 0) {
2391 GLubyte *dst = dest;
2392 GLuint i;
2393 assert(dstLuminanceIndex == 0);
2394 for (i = 0; i < n; i++) {
2395 /* Luminance comes from red channel */
2396 dst[0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
2397 dst += dstComponents;
2398 }
2399 }
2400 }
2401 }
2402
2403
2404
2405 /*
2406 * Unpack a row of color index data from a client buffer according to
2407 * the pixel unpacking parameters. Apply pixel transfer ops if enabled
2408 * and applyTransferOps is true.
2409 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
2410 *
2411 * Args: ctx - the context
2412 * n - number of pixels
2413 * dstType - destination datatype
2414 * dest - destination array
2415 * srcType - source pixel type
2416 * source - source data pointer
2417 * unpacking - pixel unpacking parameters
2418 * applyTransferOps - apply offset/bias/lookup ops?
2419 */
2420 void
2421 _mesa_unpack_index_span( const GLcontext *ctx, GLuint n,
2422 GLenum dstType, GLvoid *dest,
2423 GLenum srcType, const GLvoid *source,
2424 const struct gl_pixelstore_attrib *unpacking,
2425 GLboolean applyTransferOps )
2426 {
2427 ASSERT(srcType == GL_BITMAP ||
2428 srcType == GL_UNSIGNED_BYTE ||
2429 srcType == GL_BYTE ||
2430 srcType == GL_UNSIGNED_SHORT ||
2431 srcType == GL_SHORT ||
2432 srcType == GL_UNSIGNED_INT ||
2433 srcType == GL_INT ||
2434 srcType == GL_FLOAT);
2435
2436 ASSERT(dstType == GL_UNSIGNED_BYTE ||
2437 dstType == GL_UNSIGNED_SHORT ||
2438 dstType == GL_UNSIGNED_INT);
2439
2440 applyTransferOps &= (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset || ctx->Pixel.MapColorFlag);
2441
2442 /*
2443 * Try simple cases first
2444 */
2445 if (!applyTransferOps && srcType == GL_UNSIGNED_BYTE
2446 && dstType == GL_UNSIGNED_BYTE) {
2447 MEMCPY(dest, source, n * sizeof(GLubyte));
2448 }
2449 else if (!applyTransferOps && srcType == GL_UNSIGNED_INT
2450 && dstType == GL_UNSIGNED_INT && !unpacking->SwapBytes) {
2451 MEMCPY(dest, source, n * sizeof(GLuint));
2452 }
2453 else {
2454 /*
2455 * general solution
2456 */
2457 GLuint indexes[MAX_WIDTH];
2458 assert(n <= MAX_WIDTH);
2459
2460 extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
2461 unpacking);
2462
2463 if (applyTransferOps) {
2464 if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset) {
2465 /* shift and offset indexes */
2466 gl_shift_and_offset_ci(ctx, n, indexes);
2467 }
2468
2469 if (ctx->Pixel.MapColorFlag) {
2470 /* Apply lookup table */
2471 gl_map_ci(ctx, n, indexes);
2472 }
2473 }
2474
2475 /* convert to dest type */
2476 switch (dstType) {
2477 case GL_UNSIGNED_BYTE:
2478 {
2479 GLubyte *dst = (GLubyte *) dest;
2480 GLuint i;
2481 for (i = 0; i < n; i++) {
2482 dst[i] = (GLubyte) (indexes[i] & 0xff);
2483 }
2484 }
2485 break;
2486 case GL_UNSIGNED_SHORT:
2487 {
2488 GLuint *dst = (GLuint *) dest;
2489 GLuint i;
2490 for (i = 0; i < n; i++) {
2491 dst[i] = (GLushort) (indexes[i] & 0xffff);
2492 }
2493 }
2494 break;
2495 case GL_UNSIGNED_INT:
2496 MEMCPY(dest, indexes, n * sizeof(GLuint));
2497 break;
2498 default:
2499 gl_problem(ctx, "bad dstType in _mesa_unpack_index_span");
2500 }
2501 }
2502 }
2503
2504
2505 /*
2506 * Unpack a row of stencil data from a client buffer according to
2507 * the pixel unpacking parameters. Apply pixel transfer ops if enabled
2508 * and applyTransferOps is true.
2509 * This is (or will be) used by glDrawPixels
2510 *
2511 * Args: ctx - the context
2512 * n - number of pixels
2513 * dstType - destination datatype
2514 * dest - destination array
2515 * srcType - source pixel type
2516 * source - source data pointer
2517 * unpacking - pixel unpacking parameters
2518 * applyTransferOps - apply offset/bias/lookup ops?
2519 */
2520 void
2521 _mesa_unpack_stencil_span( const GLcontext *ctx, GLuint n,
2522 GLenum dstType, GLvoid *dest,
2523 GLenum srcType, const GLvoid *source,
2524 const struct gl_pixelstore_attrib *unpacking,
2525 GLboolean applyTransferOps )
2526 {
2527 ASSERT(srcType == GL_BITMAP ||
2528 srcType == GL_UNSIGNED_BYTE ||
2529 srcType == GL_BYTE ||
2530 srcType == GL_UNSIGNED_SHORT ||
2531 srcType == GL_SHORT ||
2532 srcType == GL_UNSIGNED_INT ||
2533 srcType == GL_INT ||
2534 srcType == GL_FLOAT);
2535
2536 ASSERT(dstType == GL_UNSIGNED_BYTE ||
2537 dstType == GL_UNSIGNED_SHORT ||
2538 dstType == GL_UNSIGNED_INT);
2539
2540 applyTransferOps &= (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset || ctx->Pixel.MapColorFlag);
2541
2542 /*
2543 * Try simple cases first
2544 */
2545 if (!applyTransferOps && srcType == GL_UNSIGNED_BYTE
2546 && dstType == GL_UNSIGNED_BYTE) {
2547 MEMCPY(dest, source, n * sizeof(GLubyte));
2548 }
2549 else if (!applyTransferOps && srcType == GL_UNSIGNED_INT
2550 && dstType == GL_UNSIGNED_INT && !unpacking->SwapBytes) {
2551 MEMCPY(dest, source, n * sizeof(GLuint));
2552 }
2553 else {
2554 /*
2555 * general solution
2556 */
2557 GLuint indexes[MAX_WIDTH];
2558 assert(n <= MAX_WIDTH);
2559
2560 extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
2561 unpacking);
2562
2563 if (applyTransferOps) {
2564 if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset) {
2565 /* shift and offset indexes */
2566 gl_shift_and_offset_ci(ctx, n, indexes);
2567 }
2568
2569 if (ctx->Pixel.MapStencilFlag) {
2570 /* Apply stencil lookup table */
2571 GLuint mask = ctx->Pixel.MapStoSsize - 1;
2572 GLuint i;
2573 for (i=0;i<n;i++) {
2574 indexes[i] = ctx->Pixel.MapStoS[ indexes[i] & mask ];
2575 }
2576 }
2577 }
2578
2579 /* convert to dest type */
2580 switch (dstType) {
2581 case GL_UNSIGNED_BYTE:
2582 {
2583 GLubyte *dst = (GLubyte *) dest;
2584 GLuint i;
2585 for (i = 0; i < n; i++) {
2586 dst[i] = (GLubyte) (indexes[i] & 0xff);
2587 }
2588 }
2589 break;
2590 case GL_UNSIGNED_SHORT:
2591 {
2592 GLuint *dst = (GLuint *) dest;
2593 GLuint i;
2594 for (i = 0; i < n; i++) {
2595 dst[i] = (GLushort) (indexes[i] & 0xffff);
2596 }
2597 }
2598 break;
2599 case GL_UNSIGNED_INT:
2600 MEMCPY(dest, indexes, n * sizeof(GLuint));
2601 break;
2602 default:
2603 gl_problem(ctx, "bad dstType in _mesa_unpack_stencil_span");
2604 }
2605 }
2606 }
2607
2608
2609
2610 void
2611 _mesa_unpack_depth_span( const GLcontext *ctx, GLuint n, GLdepth *dest,
2612 GLenum srcType, const GLvoid *source,
2613 const struct gl_pixelstore_attrib *unpacking,
2614 GLboolean applyTransferOps )
2615 {
2616 GLfloat *depth = MALLOC(n * sizeof(GLfloat));
2617 if (!depth)
2618 return;
2619
2620 switch (srcType) {
2621 case GL_BYTE:
2622 {
2623 GLuint i;
2624 const GLubyte *src = (const GLubyte *) source;
2625 for (i = 0; i < n; i++) {
2626 depth[i] = BYTE_TO_FLOAT(src[i]);
2627 }
2628 }
2629 break;
2630 case GL_UNSIGNED_BYTE:
2631 {
2632 GLuint i;
2633 const GLubyte *src = (const GLubyte *) source;
2634 for (i = 0; i < n; i++) {
2635 depth[i] = UBYTE_TO_FLOAT(src[i]);
2636 }
2637 }
2638 break;
2639 case GL_SHORT:
2640 {
2641 GLuint i;
2642 const GLshort *src = (const GLshort *) source;
2643 for (i = 0; i < n; i++) {
2644 depth[i] = SHORT_TO_FLOAT(src[i]);
2645 }
2646 }
2647 break;
2648 case GL_UNSIGNED_SHORT:
2649 {
2650 GLuint i;
2651 const GLushort *src = (const GLushort *) source;
2652 for (i = 0; i < n; i++) {
2653 depth[i] = USHORT_TO_FLOAT(src[i]);
2654 }
2655 }
2656 break;
2657 case GL_INT:
2658 {
2659 GLuint i;
2660 const GLint *src = (const GLint *) source;
2661 for (i = 0; i < n; i++) {
2662 depth[i] = INT_TO_FLOAT(src[i]);
2663 }
2664 }
2665 break;
2666 case GL_UNSIGNED_INT:
2667 {
2668 GLuint i;
2669 const GLuint *src = (const GLuint *) source;
2670 for (i = 0; i < n; i++) {
2671 depth[i] = UINT_TO_FLOAT(src[i]);
2672 }
2673 }
2674 break;
2675 case GL_FLOAT:
2676 MEMCPY(depth, source, n * sizeof(GLfloat));
2677 break;
2678 default:
2679 gl_problem(NULL, "bad type in _mesa_unpack_depth_span()");
2680 return;
2681 }
2682
2683
2684 /* apply depth scale and bias */
2685 if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
2686 GLuint i;
2687 for (i = 0; i < n; i++) {
2688 depth[i] = depth[i] * ctx->Pixel.DepthScale + ctx->Pixel.DepthBias;
2689 }
2690 }
2691
2692 /* clamp depth values to [0,1] and convert from floats to integers */
2693 {
2694 const GLfloat zs = ctx->Visual->DepthMaxF;
2695 GLuint i;
2696 for (i = 0; i < n; i++) {
2697 dest[i] = (GLdepth) (CLAMP(depth[i], 0.0F, 1.0F) * zs);
2698 }
2699 }
2700
2701 FREE(depth);
2702 }
2703
2704
2705
2706 /*
2707 * Unpack image data. Apply byteswapping, byte flipping (bitmap).
2708 * Return all image data in a contiguous block.
2709 */
2710 void *
2711 _mesa_unpack_image( GLsizei width, GLsizei height, GLsizei depth,
2712 GLenum format, GLenum type, const GLvoid *pixels,
2713 const struct gl_pixelstore_attrib *unpack )
2714 {
2715 GLint bytesPerRow, compsPerRow;
2716 GLboolean flipBytes, swap2, swap4;
2717
2718 if (!pixels)
2719 return NULL; /* not necessarily an error */
2720
2721 if (width <= 0 || height <= 0 || depth <= 0)
2722 return NULL; /* generate error later */
2723
2724 if (format == GL_BITMAP) {
2725 bytesPerRow = (width + 7) >> 3;
2726 flipBytes = !unpack->LsbFirst;
2727 swap2 = swap4 = GL_FALSE;
2728 compsPerRow = 0;
2729 }
2730 else {
2731 const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
2732 const GLint components = _mesa_components_in_format(format);
2733 GLint bytesPerComp;
2734 if (bytesPerPixel <= 0 || components <= 0)
2735 return NULL; /* bad format or type. generate error later */
2736 bytesPerRow = bytesPerPixel * width;
2737 bytesPerComp = bytesPerPixel / components;
2738 flipBytes = GL_FALSE;
2739 swap2 = (bytesPerComp == 2) && unpack->SwapBytes;
2740 swap4 = (bytesPerComp == 4) && unpack->SwapBytes;
2741 compsPerRow = components * width;
2742 assert(compsPerRow >= width);
2743 }
2744
2745 {
2746 GLubyte *destBuffer = MALLOC(bytesPerRow * height * depth);
2747 GLubyte *dst;
2748 GLint img, row;
2749 if (!destBuffer)
2750 return NULL; /* generate GL_OUT_OF_MEMORY later */
2751
2752 dst = destBuffer;
2753 for (img = 0; img < depth; img++) {
2754 for (row = 0; row < height; row++) {
2755 const GLvoid *src = _mesa_image_address(unpack, pixels,
2756 width, height, format, type, img, row, 0);
2757 MEMCPY(dst, src, bytesPerRow);
2758 /* byte flipping/swapping */
2759 if (flipBytes) {
2760 flip_bytes((GLubyte *) dst, bytesPerRow);
2761 }
2762 else if (swap2) {
2763 _mesa_swap2((GLushort*) dst, compsPerRow);
2764 }
2765 else if (swap4) {
2766 _mesa_swap4((GLuint*) dst, compsPerRow);
2767 }
2768 dst += bytesPerRow;
2769 }
2770 }
2771 return destBuffer;
2772 }
2773 }
2774
2775
2776 /*
2777 * Unpack bitmap data. Resulting data will be in most-significant-bit-first
2778 * order with row alignment = 1 byte.
2779 */
2780 GLvoid *
2781 _mesa_unpack_bitmap( GLint width, GLint height, const GLubyte *pixels,
2782 const struct gl_pixelstore_attrib *packing )
2783 {
2784 GLint bytes, row, width_in_bytes;
2785 GLubyte *buffer, *dst;
2786
2787 if (!pixels)
2788 return NULL;
2789
2790 /* Alloc dest storage */
2791 bytes = ((width + 7) / 8 * height);
2792 buffer = (GLubyte *) MALLOC( bytes );
2793 if (!buffer)
2794 return NULL;
2795
2796
2797 width_in_bytes = CEILING( width, 8 );
2798 dst = buffer;
2799 for (row = 0; row < height; row++) {
2800 GLubyte *src = _mesa_image_address( packing, pixels, width, height,
2801 GL_COLOR_INDEX, GL_BITMAP,
2802 0, row, 0 );
2803 if (!src) {
2804 FREE(buffer);
2805 return NULL;
2806 }
2807
2808 if (packing->SkipPixels == 0) {
2809 MEMCPY( dst, src, width_in_bytes );
2810 if (packing->LsbFirst) {
2811 flip_bytes( dst, width_in_bytes );
2812 }
2813 }
2814 else {
2815 /* handling SkipPixels is a bit tricky (no pun intended!) */
2816 GLint i;
2817 if (packing->LsbFirst) {
2818 GLubyte srcMask = 1 << (packing->SkipPixels & 0x7);
2819 GLubyte dstMask = 128;
2820 GLubyte *s = src;
2821 GLubyte *d = dst;
2822 *d = 0;
2823 for (i = 0; i < width; i++) {
2824 if (*s & srcMask) {
2825 *d |= dstMask;
2826 }
2827 if (srcMask == 128) {
2828 srcMask = 1;
2829 s++;
2830 }
2831 else {
2832 srcMask = srcMask << 1;
2833 }
2834 if (dstMask == 1) {
2835 dstMask = 128;
2836 d++;
2837 *d = 0;
2838 }
2839 else {
2840 dstMask = dstMask >> 1;
2841 }
2842 }
2843 }
2844 else {
2845 GLubyte srcMask = 128 >> (packing->SkipPixels & 0x7);
2846 GLubyte dstMask = 128;
2847 GLubyte *s = src;
2848 GLubyte *d = dst;
2849 *d = 0;
2850 for (i = 0; i < width; i++) {
2851 if (*s & srcMask) {
2852 *d |= dstMask;
2853 }
2854 if (srcMask == 1) {
2855 srcMask = 128;
2856 s++;
2857 }
2858 else {
2859 srcMask = srcMask >> 1;
2860 }
2861 if (dstMask == 1) {
2862 dstMask = 128;
2863 d++;
2864 *d = 0;
2865 }
2866 else {
2867 dstMask = dstMask >> 1;
2868 }
2869 }
2870 }
2871 }
2872 dst += width_in_bytes;
2873 }
2874
2875 return buffer;
2876 }
2877
2878
2879 /*
2880 * Pack bitmap data.
2881 */
2882 void
2883 _mesa_pack_bitmap( GLint width, GLint height, const GLubyte *source,
2884 GLubyte *dest, const struct gl_pixelstore_attrib *packing )
2885 {
2886 GLint row, width_in_bytes;
2887 const GLubyte *src;
2888
2889 if (!source)
2890 return;
2891
2892 width_in_bytes = CEILING( width, 8 );
2893 src = source;
2894 for (row = 0; row < height; row++) {
2895 GLubyte *dst = _mesa_image_address( packing, dest, width, height,
2896 GL_COLOR_INDEX, GL_BITMAP,
2897 0, row, 0 );
2898 if (!dst)
2899 return;
2900
2901 if (packing->SkipPixels == 0) {
2902 MEMCPY( dst, src, width_in_bytes );
2903 if (packing->LsbFirst) {
2904 flip_bytes( dst, width_in_bytes );
2905 }
2906 }
2907 else {
2908 /* handling SkipPixels is a bit tricky (no pun intended!) */
2909 GLint i;
2910 if (packing->LsbFirst) {
2911 GLubyte srcMask = 1 << (packing->SkipPixels & 0x7);
2912 GLubyte dstMask = 128;
2913 const GLubyte *s = src;
2914 GLubyte *d = dst;
2915 *d = 0;
2916 for (i = 0; i < width; i++) {
2917 if (*s & srcMask) {
2918 *d |= dstMask;
2919 }
2920 if (srcMask == 128) {
2921 srcMask = 1;
2922 s++;
2923 }
2924 else {
2925 srcMask = srcMask << 1;
2926 }
2927 if (dstMask == 1) {
2928 dstMask = 128;
2929 d++;
2930 *d = 0;
2931 }
2932 else {
2933 dstMask = dstMask >> 1;
2934 }
2935 }
2936 }
2937 else {
2938 GLubyte srcMask = 128 >> (packing->SkipPixels & 0x7);
2939 GLubyte dstMask = 128;
2940 const GLubyte *s = src;
2941 GLubyte *d = dst;
2942 *d = 0;
2943 for (i = 0; i < width; i++) {
2944 if (*s & srcMask) {
2945 *d |= dstMask;
2946 }
2947 if (srcMask == 1) {
2948 srcMask = 128;
2949 s++;
2950 }
2951 else {
2952 srcMask = srcMask >> 1;
2953 }
2954 if (dstMask == 1) {
2955 dstMask = 128;
2956 d++;
2957 *d = 0;
2958 }
2959 else {
2960 dstMask = dstMask >> 1;
2961 }
2962 }
2963 }
2964 }
2965 src += width_in_bytes;
2966 }
2967 }