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