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