runtime selectable depth buffer depth
[mesa.git] / src / mesa / main / image.c
1 /* $Id: image.c,v 1.18 2000/03/03 17:47:39 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_BYTE_2_3_3_REV ||
1583 srcType == GL_UNSIGNED_SHORT_5_6_5 ||
1584 srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
1585 srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
1586 srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
1587 srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
1588 srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
1589 srcType == GL_UNSIGNED_INT_8_8_8_8 ||
1590 srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
1591 srcType == GL_UNSIGNED_INT_10_10_10_2 ||
1592 srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
1593
1594 rComp = gComp = bComp = aComp = -1;
1595
1596 switch (srcFormat) {
1597 case GL_RED:
1598 redIndex = 0;
1599 greenIndex = blueIndex = alphaIndex = -1;
1600 stride = 1;
1601 break;
1602 case GL_GREEN:
1603 greenIndex = 0;
1604 redIndex = blueIndex = alphaIndex = -1;
1605 stride = 1;
1606 break;
1607 case GL_BLUE:
1608 blueIndex = 0;
1609 redIndex = greenIndex = alphaIndex = -1;
1610 stride = 1;
1611 break;
1612 case GL_ALPHA:
1613 redIndex = greenIndex = blueIndex = -1;
1614 alphaIndex = 0;
1615 stride = 1;
1616 break;
1617 case GL_LUMINANCE:
1618 redIndex = greenIndex = blueIndex = 0;
1619 alphaIndex = -1;
1620 stride = 1;
1621 break;
1622 case GL_LUMINANCE_ALPHA:
1623 redIndex = greenIndex = blueIndex = 0;
1624 alphaIndex = 1;
1625 stride = 2;
1626 break;
1627 case GL_INTENSITY:
1628 redIndex = 0;
1629 greenIndex = blueIndex = alphaIndex = -1;
1630 stride = 1;
1631 break;
1632 case GL_RGB:
1633 redIndex = 0;
1634 greenIndex = 1;
1635 blueIndex = 2;
1636 alphaIndex = -1;
1637 stride = 3;
1638 break;
1639 case GL_BGR:
1640 redIndex = 2;
1641 greenIndex = 1;
1642 blueIndex = 0;
1643 alphaIndex = -1;
1644 stride = 3;
1645 break;
1646 case GL_RGBA:
1647 redIndex = 0;
1648 greenIndex = 1;
1649 blueIndex = 2;
1650 alphaIndex = 3;
1651 rComp = 0;
1652 gComp = 1;
1653 bComp = 2;
1654 aComp = 3;
1655 stride = 4;
1656 break;
1657 case GL_BGRA:
1658 redIndex = 2;
1659 greenIndex = 1;
1660 blueIndex = 0;
1661 alphaIndex = 3;
1662 rComp = 2;
1663 gComp = 1;
1664 bComp = 0;
1665 aComp = 3;
1666 stride = 4;
1667 break;
1668 case GL_ABGR_EXT:
1669 redIndex = 3;
1670 greenIndex = 2;
1671 blueIndex = 1;
1672 alphaIndex = 0;
1673 rComp = 3;
1674 gComp = 2;
1675 bComp = 1;
1676 aComp = 0;
1677 stride = 4;
1678 break;
1679 default:
1680 gl_problem(NULL, "bad srcFormat in extract float data");
1681 return;
1682 }
1683
1684
1685 #define PROCESS(INDEX, CHANNEL, DEFAULT, TYPE, CONVERSION) \
1686 if ((INDEX) < 0) { \
1687 GLuint i; \
1688 for (i = 0; i < n; i++) { \
1689 rgba[i][CHANNEL] = DEFAULT; \
1690 } \
1691 } \
1692 else if (swapBytes) { \
1693 const TYPE *s = (const TYPE *) src; \
1694 GLuint i; \
1695 for (i = 0; i < n; i++) { \
1696 TYPE value = s[INDEX]; \
1697 if (sizeof(TYPE) == 2) { \
1698 SWAP2BYTE(value); \
1699 } \
1700 else if (sizeof(TYPE) == 4) { \
1701 SWAP4BYTE(value); \
1702 } \
1703 rgba[i][CHANNEL] = (GLfloat) CONVERSION(value); \
1704 s += stride; \
1705 } \
1706 } \
1707 else { \
1708 const TYPE *s = (const TYPE *) src; \
1709 GLuint i; \
1710 for (i = 0; i < n; i++) { \
1711 rgba[i][CHANNEL] = (GLfloat) CONVERSION(s[INDEX]); \
1712 s += stride; \
1713 } \
1714 }
1715
1716 switch (srcType) {
1717 case GL_UNSIGNED_BYTE:
1718 PROCESS(redIndex, RCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
1719 PROCESS(greenIndex, GCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
1720 PROCESS(blueIndex, BCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
1721 PROCESS(alphaIndex, ACOMP, 1.0F, GLubyte, UBYTE_TO_FLOAT);
1722 break;
1723 case GL_BYTE:
1724 PROCESS(redIndex, RCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
1725 PROCESS(greenIndex, GCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
1726 PROCESS(blueIndex, BCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
1727 PROCESS(alphaIndex, ACOMP, 1.0F, GLbyte, BYTE_TO_FLOAT);
1728 break;
1729 case GL_UNSIGNED_SHORT:
1730 PROCESS(redIndex, RCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
1731 PROCESS(greenIndex, GCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
1732 PROCESS(blueIndex, BCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
1733 PROCESS(alphaIndex, ACOMP, 1.0F, GLushort, USHORT_TO_FLOAT);
1734 break;
1735 case GL_SHORT:
1736 PROCESS(redIndex, RCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
1737 PROCESS(greenIndex, GCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
1738 PROCESS(blueIndex, BCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
1739 PROCESS(alphaIndex, ACOMP, 1.0F, GLshort, SHORT_TO_FLOAT);
1740 break;
1741 case GL_UNSIGNED_INT:
1742 PROCESS(redIndex, RCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
1743 PROCESS(greenIndex, GCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
1744 PROCESS(blueIndex, BCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
1745 PROCESS(alphaIndex, ACOMP, 1.0F, GLuint, UINT_TO_FLOAT);
1746 break;
1747 case GL_INT:
1748 PROCESS(redIndex, RCOMP, 0.0F, GLint, INT_TO_FLOAT);
1749 PROCESS(greenIndex, GCOMP, 0.0F, GLint, INT_TO_FLOAT);
1750 PROCESS(blueIndex, BCOMP, 0.0F, GLint, INT_TO_FLOAT);
1751 PROCESS(alphaIndex, ACOMP, 1.0F, GLint, INT_TO_FLOAT);
1752 break;
1753 case GL_FLOAT:
1754 PROCESS(redIndex, RCOMP, 0.0F, GLfloat, (GLfloat));
1755 PROCESS(greenIndex, GCOMP, 0.0F, GLfloat, (GLfloat));
1756 PROCESS(blueIndex, BCOMP, 0.0F, GLfloat, (GLfloat));
1757 PROCESS(alphaIndex, ACOMP, 1.0F, GLfloat, (GLfloat));
1758 break;
1759 case GL_UNSIGNED_BYTE_3_3_2:
1760 {
1761 const GLubyte *ubsrc = (const GLubyte *) src;
1762 GLuint i;
1763 for (i = 0; i < n; i ++) {
1764 GLubyte p = ubsrc[i];
1765 rgba[i][RCOMP] = ((p >> 5) ) * (1.0F / 7.0F);
1766 rgba[i][GCOMP] = ((p >> 2) & 0x7) * (1.0F / 7.0F);
1767 rgba[i][BCOMP] = ((p ) & 0x3) * (1.0F / 3.0F);
1768 rgba[i][ACOMP] = 1.0F;
1769 }
1770 }
1771 break;
1772 case GL_UNSIGNED_BYTE_2_3_3_REV:
1773 {
1774 const GLubyte *ubsrc = (const GLubyte *) src;
1775 GLuint i;
1776 for (i = 0; i < n; i ++) {
1777 GLubyte p = ubsrc[i];
1778 rgba[i][RCOMP] = ((p ) & 0x7) * (1.0F / 7.0F);
1779 rgba[i][GCOMP] = ((p >> 3) & 0x7) * (1.0F / 7.0F);
1780 rgba[i][BCOMP] = ((p >> 6) ) * (1.0F / 3.0F);
1781 rgba[i][ACOMP] = 1.0F;
1782 }
1783 }
1784 break;
1785 case GL_UNSIGNED_SHORT_5_6_5:
1786 if (swapBytes) {
1787 const GLushort *ussrc = (const GLushort *) src;
1788 GLuint i;
1789 for (i = 0; i < n; i ++) {
1790 GLushort p = ussrc[i];
1791 SWAP2BYTE(p);
1792 rgba[i][RCOMP] = ((p >> 11) ) * (1.0F / 31.0F);
1793 rgba[i][GCOMP] = ((p >> 5) & 0x3f) * (1.0F / 63.0F);
1794 rgba[i][BCOMP] = ((p ) & 0x1f) * (1.0F / 31.0F);
1795 rgba[i][ACOMP] = 1.0F;
1796 }
1797 }
1798 else {
1799 const GLushort *ussrc = (const GLushort *) src;
1800 GLuint i;
1801 for (i = 0; i < n; i ++) {
1802 GLushort p = ussrc[i];
1803 rgba[i][RCOMP] = ((p >> 11) ) * (1.0F / 31.0F);
1804 rgba[i][GCOMP] = ((p >> 5) & 0x3f) * (1.0F / 63.0F);
1805 rgba[i][BCOMP] = ((p ) & 0x1f) * (1.0F / 31.0F);
1806 rgba[i][ACOMP] = 1.0F;
1807 }
1808 }
1809 break;
1810 case GL_UNSIGNED_SHORT_5_6_5_REV:
1811 if (swapBytes) {
1812 const GLushort *ussrc = (const GLushort *) src;
1813 GLuint i;
1814 for (i = 0; i < n; i ++) {
1815 GLushort p = ussrc[i];
1816 SWAP2BYTE(p);
1817 rgba[i][RCOMP] = ((p ) & 0x1f) * (1.0F / 31.0F);
1818 rgba[i][GCOMP] = ((p >> 5) & 0x3f) * (1.0F / 63.0F);
1819 rgba[i][BCOMP] = ((p >> 11) ) * (1.0F / 31.0F);
1820 rgba[i][ACOMP] = 1.0F;
1821 }
1822 }
1823 else {
1824 const GLushort *ussrc = (const GLushort *) src;
1825 GLuint i;
1826 for (i = 0; i < n; i ++) {
1827 GLushort p = ussrc[i];
1828 rgba[i][RCOMP] = ((p ) & 0x1f) * (1.0F / 31.0F);
1829 rgba[i][GCOMP] = ((p >> 5) & 0x3f) * (1.0F / 63.0F);
1830 rgba[i][BCOMP] = ((p >> 11) ) * (1.0F / 31.0F);
1831 rgba[i][ACOMP] = 1.0F;
1832 }
1833 }
1834 break;
1835 case GL_UNSIGNED_SHORT_4_4_4_4:
1836 if (swapBytes) {
1837 const GLushort *ussrc = (const GLushort *) src;
1838 GLuint i;
1839 for (i = 0; i < n; i ++) {
1840 GLushort p = ussrc[i];
1841 SWAP2BYTE(p);
1842 rgba[i][rComp] = ((p >> 12) ) * (1.0F / 15.0F);
1843 rgba[i][gComp] = ((p >> 8) & 0xf) * (1.0F / 15.0F);
1844 rgba[i][bComp] = ((p >> 4) & 0xf) * (1.0F / 15.0F);
1845 rgba[i][aComp] = ((p ) & 0xf) * (1.0F / 15.0F);
1846 }
1847 }
1848 else {
1849 const GLushort *ussrc = (const GLushort *) src;
1850 GLuint i;
1851 for (i = 0; i < n; i ++) {
1852 GLushort p = ussrc[i];
1853 rgba[i][rComp] = ((p >> 12) ) * (1.0F / 15.0F);
1854 rgba[i][gComp] = ((p >> 8) & 0xf) * (1.0F / 15.0F);
1855 rgba[i][bComp] = ((p >> 4) & 0xf) * (1.0F / 15.0F);
1856 rgba[i][aComp] = ((p ) & 0xf) * (1.0F / 15.0F);
1857 }
1858 }
1859 break;
1860 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1861 if (swapBytes) {
1862 const GLushort *ussrc = (const GLushort *) src;
1863 GLuint i;
1864 for (i = 0; i < n; i ++) {
1865 GLushort p = ussrc[i];
1866 SWAP2BYTE(p);
1867 rgba[i][rComp] = ((p ) & 0xf) * (1.0F / 15.0F);
1868 rgba[i][gComp] = ((p >> 4) & 0xf) * (1.0F / 15.0F);
1869 rgba[i][bComp] = ((p >> 8) & 0xf) * (1.0F / 15.0F);
1870 rgba[i][aComp] = ((p >> 12) ) * (1.0F / 15.0F);
1871 }
1872 }
1873 else {
1874 const GLushort *ussrc = (const GLushort *) src;
1875 GLuint i;
1876 for (i = 0; i < n; i ++) {
1877 GLushort p = ussrc[i];
1878 rgba[i][rComp] = ((p ) & 0xf) * (1.0F / 15.0F);
1879 rgba[i][gComp] = ((p >> 4) & 0xf) * (1.0F / 15.0F);
1880 rgba[i][bComp] = ((p >> 8) & 0xf) * (1.0F / 15.0F);
1881 rgba[i][aComp] = ((p >> 12) ) * (1.0F / 15.0F);
1882 }
1883 }
1884 break;
1885 case GL_UNSIGNED_SHORT_5_5_5_1:
1886 if (swapBytes) {
1887 const GLushort *ussrc = (const GLushort *) src;
1888 GLuint i;
1889 for (i = 0; i < n; i ++) {
1890 GLushort p = ussrc[i];
1891 SWAP2BYTE(p);
1892 rgba[i][rComp] = ((p >> 11) ) * (1.0F / 31.0F);
1893 rgba[i][gComp] = ((p >> 6) & 0x1f) * (1.0F / 31.0F);
1894 rgba[i][bComp] = ((p >> 1) & 0x1f) * (1.0F / 31.0F);
1895 rgba[i][aComp] = ((p ) & 0x1) * (1.0F / 1.0F);
1896 }
1897 }
1898 else {
1899 const GLushort *ussrc = (const GLushort *) src;
1900 GLuint i;
1901 for (i = 0; i < n; i ++) {
1902 GLushort p = ussrc[i];
1903 rgba[i][rComp] = ((p >> 11) ) * (1.0F / 31.0F);
1904 rgba[i][gComp] = ((p >> 6) & 0x1f) * (1.0F / 31.0F);
1905 rgba[i][bComp] = ((p >> 1) & 0x1f) * (1.0F / 31.0F);
1906 rgba[i][aComp] = ((p ) & 0x1) * (1.0F / 1.0F);
1907 }
1908 }
1909 break;
1910 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
1911 if (swapBytes) {
1912 const GLushort *ussrc = (const GLushort *) src;
1913 GLuint i;
1914 for (i = 0; i < n; i ++) {
1915 GLushort p = ussrc[i];
1916 SWAP2BYTE(p);
1917 rgba[i][rComp] = ((p ) & 0x1f) * (1.0F / 31.0F);
1918 rgba[i][gComp] = ((p >> 5) & 0x1f) * (1.0F / 31.0F);
1919 rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
1920 rgba[i][aComp] = ((p >> 15) ) * (1.0F / 1.0F);
1921 }
1922 }
1923 else {
1924 const GLushort *ussrc = (const GLushort *) src;
1925 GLuint i;
1926 for (i = 0; i < n; i ++) {
1927 GLushort p = ussrc[i];
1928 rgba[i][rComp] = ((p ) & 0x1f) * (1.0F / 31.0F);
1929 rgba[i][gComp] = ((p >> 5) & 0x1f) * (1.0F / 31.0F);
1930 rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
1931 rgba[i][aComp] = ((p >> 15) ) * (1.0F / 1.0F);
1932 }
1933 }
1934 break;
1935 case GL_UNSIGNED_INT_8_8_8_8:
1936 if (swapBytes) {
1937 const GLuint *uisrc = (const GLuint *) src;
1938 GLuint i;
1939 for (i = 0; i < n; i ++) {
1940 GLuint p = uisrc[i];
1941 rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p ) & 0xff);
1942 rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 8) & 0xff);
1943 rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
1944 rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24) );
1945 }
1946 }
1947 else {
1948 const GLuint *uisrc = (const GLuint *) src;
1949 GLuint i;
1950 for (i = 0; i < n; i ++) {
1951 GLuint p = uisrc[i];
1952 rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24) );
1953 rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
1954 rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 8) & 0xff);
1955 rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p ) & 0xff);
1956 }
1957 }
1958 break;
1959 case GL_UNSIGNED_INT_8_8_8_8_REV:
1960 if (swapBytes) {
1961 const GLuint *uisrc = (const GLuint *) src;
1962 GLuint i;
1963 for (i = 0; i < n; i ++) {
1964 GLuint p = uisrc[i];
1965 rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24) );
1966 rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
1967 rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 8) & 0xff);
1968 rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p ) & 0xff);
1969 }
1970 }
1971 else {
1972 const GLuint *uisrc = (const GLuint *) src;
1973 GLuint i;
1974 for (i = 0; i < n; i ++) {
1975 GLuint p = uisrc[i];
1976 rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p ) & 0xff);
1977 rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 8) & 0xff);
1978 rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
1979 rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24) );
1980 }
1981 }
1982 break;
1983 case GL_UNSIGNED_INT_10_10_10_2:
1984 if (swapBytes) {
1985 const GLuint *uisrc = (const GLuint *) src;
1986 GLuint i;
1987 for (i = 0; i < n; i ++) {
1988 GLuint p = uisrc[i];
1989 SWAP4BYTE(p);
1990 rgba[i][rComp] = ((p >> 22) ) * (1.0F / 1023.0F);
1991 rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
1992 rgba[i][bComp] = ((p >> 2) & 0x3ff) * (1.0F / 1023.0F);
1993 rgba[i][aComp] = ((p ) & 0x3 ) * (1.0F / 3.0F);
1994 }
1995 }
1996 else {
1997 const GLuint *uisrc = (const GLuint *) src;
1998 GLuint i;
1999 for (i = 0; i < n; i ++) {
2000 GLuint p = uisrc[i];
2001 rgba[i][rComp] = ((p >> 22) ) * (1.0F / 1023.0F);
2002 rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
2003 rgba[i][bComp] = ((p >> 2) & 0x3ff) * (1.0F / 1023.0F);
2004 rgba[i][aComp] = ((p ) & 0x3 ) * (1.0F / 3.0F);
2005 }
2006 }
2007 break;
2008 case GL_UNSIGNED_INT_2_10_10_10_REV:
2009 if (swapBytes) {
2010 const GLuint *uisrc = (const GLuint *) src;
2011 GLuint i;
2012 for (i = 0; i < n; i ++) {
2013 GLuint p = uisrc[i];
2014 SWAP4BYTE(p);
2015 rgba[i][rComp] = ((p ) & 0x3ff) * (1.0F / 1023.0F);
2016 rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
2017 rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
2018 rgba[i][aComp] = ((p >> 30) ) * (1.0F / 3.0F);
2019 }
2020 }
2021 else {
2022 const GLuint *uisrc = (const GLuint *) src;
2023 GLuint i;
2024 for (i = 0; i < n; i ++) {
2025 GLuint p = uisrc[i];
2026 rgba[i][rComp] = ((p ) & 0x3ff) * (1.0F / 1023.0F);
2027 rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
2028 rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
2029 rgba[i][aComp] = ((p >> 30) ) * (1.0F / 3.0F);
2030 }
2031 }
2032 break;
2033 default:
2034 gl_problem(NULL, "bad srcType in extract float data");
2035 break;
2036 }
2037 }
2038
2039
2040
2041 /*
2042 * Unpack a row of color image data from a client buffer according to
2043 * the pixel unpacking parameters. Apply any enabled pixel transfer
2044 * ops (PixelMap, scale/bias) if the applyTransferOps flag is enabled.
2045 * Return GLubyte values in the specified dest image format.
2046 * This is (or will be) used by glDrawPixels and glTexImage?D().
2047 * Input: ctx - the context
2048 * n - number of pixels in the span
2049 * dstFormat - format of destination color array
2050 * dest - the destination color array
2051 * srcFormat - source image format
2052 * srcType - source image datatype
2053 * source - source image pointer
2054 * unpacking - pixel unpacking parameters
2055 * applyTransferOps - apply scale/bias/lookup-table ops?
2056 *
2057 * XXX perhaps expand this to process whole images someday.
2058 */
2059 void
2060 _mesa_unpack_ubyte_color_span( const GLcontext *ctx,
2061 GLuint n, GLenum dstFormat, GLubyte dest[],
2062 GLenum srcFormat, GLenum srcType,
2063 const GLvoid *source,
2064 const struct gl_pixelstore_attrib *unpacking,
2065 GLboolean applyTransferOps )
2066 {
2067 ASSERT(dstFormat == GL_ALPHA ||
2068 dstFormat == GL_LUMINANCE ||
2069 dstFormat == GL_LUMINANCE_ALPHA ||
2070 dstFormat == GL_INTENSITY ||
2071 dstFormat == GL_RGB ||
2072 dstFormat == GL_RGBA ||
2073 dstFormat == GL_COLOR_INDEX);
2074
2075 ASSERT(srcFormat == GL_RED ||
2076 srcFormat == GL_GREEN ||
2077 srcFormat == GL_BLUE ||
2078 srcFormat == GL_ALPHA ||
2079 srcFormat == GL_LUMINANCE ||
2080 srcFormat == GL_LUMINANCE_ALPHA ||
2081 srcFormat == GL_INTENSITY ||
2082 srcFormat == GL_RGB ||
2083 srcFormat == GL_BGR ||
2084 srcFormat == GL_RGBA ||
2085 srcFormat == GL_BGRA ||
2086 srcFormat == GL_ABGR_EXT ||
2087 srcFormat == GL_COLOR_INDEX);
2088
2089 ASSERT(srcType == GL_BITMAP ||
2090 srcType == GL_UNSIGNED_BYTE ||
2091 srcType == GL_BYTE ||
2092 srcType == GL_UNSIGNED_SHORT ||
2093 srcType == GL_SHORT ||
2094 srcType == GL_UNSIGNED_INT ||
2095 srcType == GL_INT ||
2096 srcType == GL_FLOAT ||
2097 srcType == GL_UNSIGNED_BYTE_3_3_2 ||
2098 srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
2099 srcType == GL_UNSIGNED_SHORT_5_6_5 ||
2100 srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
2101 srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
2102 srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
2103 srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
2104 srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
2105 srcType == GL_UNSIGNED_INT_8_8_8_8 ||
2106 srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
2107 srcType == GL_UNSIGNED_INT_10_10_10_2 ||
2108 srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
2109
2110 /* this is intended for RGBA mode */
2111 assert(ctx->Visual->RGBAflag);
2112
2113 applyTransferOps &= (ctx->Pixel.ScaleOrBiasRGBA ||
2114 ctx->Pixel.MapColorFlag ||
2115 ctx->Pixel.MapColorFlag);
2116
2117 /* Try simple cases first */
2118 if (!applyTransferOps && srcType == GL_UNSIGNED_BYTE) {
2119 if (dstFormat == GL_RGBA) {
2120 if (srcFormat == GL_RGBA) {
2121 MEMCPY( dest, source, n * 4 * sizeof(GLubyte) );
2122 return;
2123 }
2124 else if (srcFormat == GL_RGB) {
2125 GLuint i;
2126 const GLubyte *src = (const GLubyte *) source;
2127 GLubyte *dst = dest;
2128 for (i = 0; i < n; i++) {
2129 dst[0] = src[0];
2130 dst[1] = src[1];
2131 dst[2] = src[2];
2132 dst[3] = 255;
2133 src += 3;
2134 dst += 4;
2135 }
2136 return;
2137 }
2138 }
2139 else if (dstFormat == GL_RGB) {
2140 if (srcFormat == GL_RGB) {
2141 MEMCPY( dest, source, n * 3 * sizeof(GLubyte) );
2142 return;
2143 }
2144 else if (srcFormat == GL_RGBA) {
2145 GLuint i;
2146 const GLubyte *src = (const GLubyte *) source;
2147 GLubyte *dst = dest;
2148 for (i = 0; i < n; i++) {
2149 dst[0] = src[0];
2150 dst[1] = src[1];
2151 dst[2] = src[2];
2152 src += 4;
2153 dst += 3;
2154 }
2155 return;
2156 }
2157 }
2158 else if (dstFormat == srcFormat) {
2159 GLint comps = gl_components_in_format(srcFormat);
2160 assert(comps > 0);
2161 MEMCPY( dest, source, n * comps * sizeof(GLubyte) );
2162 return;
2163 }
2164 }
2165
2166
2167 {
2168 /* general solution */
2169 GLfloat rgba[MAX_WIDTH][4];
2170 GLint dstComponents;
2171 GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
2172 GLint dstLuminanceIndex, dstIntensityIndex;
2173
2174 dstComponents = gl_components_in_format( dstFormat );
2175 /* source & dest image formats should have been error checked by now */
2176 assert(dstComponents > 0);
2177
2178 /*
2179 * Extract image data and convert to RGBA floats
2180 */
2181 assert(n <= MAX_WIDTH);
2182 if (srcFormat == GL_COLOR_INDEX) {
2183 GLuint indexes[MAX_WIDTH];
2184 extract_uint_indexes(n, indexes, srcFormat, srcType, source,
2185 unpacking);
2186
2187 /* shift and offset indexes */
2188 gl_shift_and_offset_ci(ctx, n, indexes);
2189
2190 if (dstFormat == GL_COLOR_INDEX) {
2191 if (applyTransferOps) {
2192 if (ctx->Pixel.MapColorFlag) {
2193 /* Apply lookup table */
2194 gl_map_ci(ctx, n, indexes);
2195 }
2196
2197 if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset) {
2198
2199 }
2200 }
2201
2202 /* convert to GLubyte and return */
2203 {
2204 GLuint i;
2205 for (i = 0; i < n; i++) {
2206 dest[i] = (GLubyte) (indexes[i] & 0xff);
2207 }
2208 }
2209 }
2210 else {
2211 /* Convert indexes to RGBA */
2212 gl_map_ci_to_rgba_float(ctx, n, indexes, rgba);
2213 }
2214 }
2215 else {
2216 extract_float_rgba(n, rgba, srcFormat, srcType, source,
2217 unpacking->SwapBytes);
2218
2219 if (applyTransferOps) {
2220 /* scale and bias colors */
2221 gl_scale_and_bias_rgba_float(ctx, n, rgba);
2222
2223 /* color table lookup */
2224 if (ctx->Pixel.MapColorFlag) {
2225 gl_map_rgba_float(ctx, n, rgba);
2226 }
2227 }
2228 }
2229
2230
2231 /*
2232 * XXX This is where more color table lookups, convolution,
2233 * histograms, minmax, color matrix, etc would take place if
2234 * implemented.
2235 * See figure 3.7 in the OpenGL 1.2 specification for more info.
2236 */
2237
2238
2239 /* clamp to [0,1] */
2240 {
2241 GLuint i;
2242 for (i = 0; i < n; i++) {
2243 rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F);
2244 rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F);
2245 rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F);
2246 rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F);
2247 }
2248 }
2249
2250 /* Now determine which color channels we need to produce.
2251 * And determine the dest index (offset) within each color tuple.
2252 */
2253 switch (dstFormat) {
2254 case GL_ALPHA:
2255 dstAlphaIndex = 0;
2256 dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
2257 dstLuminanceIndex = dstIntensityIndex = -1;
2258 break;
2259 case GL_LUMINANCE:
2260 dstLuminanceIndex = 0;
2261 dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
2262 dstIntensityIndex = -1;
2263 break;
2264 case GL_LUMINANCE_ALPHA:
2265 dstLuminanceIndex = 0;
2266 dstAlphaIndex = 1;
2267 dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
2268 dstIntensityIndex = -1;
2269 break;
2270 case GL_INTENSITY:
2271 dstIntensityIndex = 0;
2272 dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
2273 dstLuminanceIndex = -1;
2274 break;
2275 case GL_RGB:
2276 dstRedIndex = 0;
2277 dstGreenIndex = 1;
2278 dstBlueIndex = 2;
2279 dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
2280 break;
2281 case GL_RGBA:
2282 dstRedIndex = 0;
2283 dstGreenIndex = 1;
2284 dstBlueIndex = 2;
2285 dstAlphaIndex = 3;
2286 dstLuminanceIndex = dstIntensityIndex = -1;
2287 break;
2288 default:
2289 gl_problem(ctx, "bad dstFormat in _mesa_unpack_ubyte_span()");
2290 return;
2291 }
2292
2293
2294 /* Now return the GLubyte data in the requested dstFormat */
2295
2296 if (dstRedIndex >= 0) {
2297 GLubyte *dst = dest;
2298 GLuint i;
2299 for (i = 0; i < n; i++) {
2300 dst[dstRedIndex] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
2301 dst += dstComponents;
2302 }
2303 }
2304
2305 if (dstGreenIndex >= 0) {
2306 GLubyte *dst = dest;
2307 GLuint i;
2308 for (i = 0; i < n; i++) {
2309 dst[dstGreenIndex] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
2310 dst += dstComponents;
2311 }
2312 }
2313
2314 if (dstBlueIndex >= 0) {
2315 GLubyte *dst = dest;
2316 GLuint i;
2317 for (i = 0; i < n; i++) {
2318 dst[dstBlueIndex] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
2319 dst += dstComponents;
2320 }
2321 }
2322
2323 if (dstAlphaIndex >= 0) {
2324 GLubyte *dst = dest;
2325 GLuint i;
2326 for (i = 0; i < n; i++) {
2327 dst[dstAlphaIndex] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
2328 dst += dstComponents;
2329 }
2330 }
2331
2332 if (dstIntensityIndex >= 0) {
2333 GLubyte *dst = dest;
2334 GLuint i;
2335 assert(dstIntensityIndex == 0);
2336 assert(dstComponents == 1);
2337 for (i = 0; i < n; i++) {
2338 /* Intensity comes from red channel */
2339 dst[i] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
2340 }
2341 }
2342
2343 if (dstLuminanceIndex >= 0) {
2344 GLubyte *dst = dest;
2345 GLuint i;
2346 assert(dstLuminanceIndex == 0);
2347 for (i = 0; i < n; i++) {
2348 /* Luminance comes from red channel */
2349 dst[0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
2350 dst += dstComponents;
2351 }
2352 }
2353 }
2354 }
2355
2356
2357
2358 /*
2359 * Unpack a row of color index data from a client buffer according to
2360 * the pixel unpacking parameters. Apply pixel transfer ops if enabled
2361 * and applyTransferOps is true.
2362 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
2363 *
2364 * Args: ctx - the context
2365 * n - number of pixels
2366 * dstType - destination datatype
2367 * dest - destination array
2368 * srcType - source pixel type
2369 * source - source data pointer
2370 * unpacking - pixel unpacking parameters
2371 * applyTransferOps - apply offset/bias/lookup ops?
2372 */
2373 void
2374 _mesa_unpack_index_span( const GLcontext *ctx, GLuint n,
2375 GLenum dstType, GLvoid *dest,
2376 GLenum srcType, const GLvoid *source,
2377 const struct gl_pixelstore_attrib *unpacking,
2378 GLboolean applyTransferOps )
2379 {
2380 ASSERT(srcType == GL_BITMAP ||
2381 srcType == GL_UNSIGNED_BYTE ||
2382 srcType == GL_BYTE ||
2383 srcType == GL_UNSIGNED_SHORT ||
2384 srcType == GL_SHORT ||
2385 srcType == GL_UNSIGNED_INT ||
2386 srcType == GL_INT ||
2387 srcType == GL_FLOAT);
2388
2389 ASSERT(dstType == GL_UNSIGNED_BYTE ||
2390 dstType == GL_UNSIGNED_SHORT ||
2391 dstType == GL_UNSIGNED_INT);
2392
2393 applyTransferOps &= (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset || ctx->Pixel.MapColorFlag);
2394
2395 /*
2396 * Try simple cases first
2397 */
2398 if (!applyTransferOps && srcType == GL_UNSIGNED_BYTE
2399 && dstType == GL_UNSIGNED_BYTE) {
2400 MEMCPY(dest, source, n * sizeof(GLubyte));
2401 }
2402 else if (!applyTransferOps && srcType == GL_UNSIGNED_INT
2403 && dstType == GL_UNSIGNED_INT && !unpacking->SwapBytes) {
2404 MEMCPY(dest, source, n * sizeof(GLuint));
2405 }
2406 else {
2407 /*
2408 * general solution
2409 */
2410 GLuint indexes[MAX_WIDTH];
2411 assert(n <= MAX_WIDTH);
2412
2413 extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
2414 unpacking);
2415
2416 if (applyTransferOps) {
2417 if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset) {
2418 /* shift and offset indexes */
2419 gl_shift_and_offset_ci(ctx, n, indexes);
2420 }
2421
2422 if (ctx->Pixel.MapColorFlag) {
2423 /* Apply lookup table */
2424 gl_map_ci(ctx, n, indexes);
2425 }
2426 }
2427
2428 /* convert to dest type */
2429 switch (dstType) {
2430 case GL_UNSIGNED_BYTE:
2431 {
2432 GLubyte *dst = (GLubyte *) dest;
2433 GLuint i;
2434 for (i = 0; i < n; i++) {
2435 dst[i] = (GLubyte) (indexes[i] & 0xff);
2436 }
2437 }
2438 break;
2439 case GL_UNSIGNED_SHORT:
2440 {
2441 GLuint *dst = (GLuint *) dest;
2442 GLuint i;
2443 for (i = 0; i < n; i++) {
2444 dst[i] = (GLushort) (indexes[i] & 0xffff);
2445 }
2446 }
2447 break;
2448 case GL_UNSIGNED_INT:
2449 MEMCPY(dest, indexes, n * sizeof(GLuint));
2450 break;
2451 default:
2452 gl_problem(ctx, "bad dstType in _mesa_unpack_index_span");
2453 }
2454 }
2455 }
2456
2457
2458 /*
2459 * Unpack a row of stencil data from a client buffer according to
2460 * the pixel unpacking parameters. Apply pixel transfer ops if enabled
2461 * and applyTransferOps is true.
2462 * This is (or will be) used by glDrawPixels
2463 *
2464 * Args: ctx - the context
2465 * n - number of pixels
2466 * dstType - destination datatype
2467 * dest - destination array
2468 * srcType - source pixel type
2469 * source - source data pointer
2470 * unpacking - pixel unpacking parameters
2471 * applyTransferOps - apply offset/bias/lookup ops?
2472 */
2473 void
2474 _mesa_unpack_stencil_span( const GLcontext *ctx, GLuint n,
2475 GLenum dstType, GLvoid *dest,
2476 GLenum srcType, const GLvoid *source,
2477 const struct gl_pixelstore_attrib *unpacking,
2478 GLboolean applyTransferOps )
2479 {
2480 ASSERT(srcType == GL_BITMAP ||
2481 srcType == GL_UNSIGNED_BYTE ||
2482 srcType == GL_BYTE ||
2483 srcType == GL_UNSIGNED_SHORT ||
2484 srcType == GL_SHORT ||
2485 srcType == GL_UNSIGNED_INT ||
2486 srcType == GL_INT ||
2487 srcType == GL_FLOAT);
2488
2489 ASSERT(dstType == GL_UNSIGNED_BYTE ||
2490 dstType == GL_UNSIGNED_SHORT ||
2491 dstType == GL_UNSIGNED_INT);
2492
2493 applyTransferOps &= (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset || ctx->Pixel.MapColorFlag);
2494
2495 /*
2496 * Try simple cases first
2497 */
2498 if (!applyTransferOps && srcType == GL_UNSIGNED_BYTE
2499 && dstType == GL_UNSIGNED_BYTE) {
2500 MEMCPY(dest, source, n * sizeof(GLubyte));
2501 }
2502 else if (!applyTransferOps && srcType == GL_UNSIGNED_INT
2503 && dstType == GL_UNSIGNED_INT && !unpacking->SwapBytes) {
2504 MEMCPY(dest, source, n * sizeof(GLuint));
2505 }
2506 else {
2507 /*
2508 * general solution
2509 */
2510 GLuint indexes[MAX_WIDTH];
2511 assert(n <= MAX_WIDTH);
2512
2513 extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
2514 unpacking);
2515
2516 if (applyTransferOps) {
2517 if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset) {
2518 /* shift and offset indexes */
2519 gl_shift_and_offset_ci(ctx, n, indexes);
2520 }
2521
2522 if (ctx->Pixel.MapStencilFlag) {
2523 /* Apply stencil lookup table */
2524 GLuint mask = ctx->Pixel.MapStoSsize - 1;
2525 GLuint i;
2526 for (i=0;i<n;i++) {
2527 indexes[i] = ctx->Pixel.MapStoS[ indexes[i] & mask ];
2528 }
2529 }
2530 }
2531
2532 /* convert to dest type */
2533 switch (dstType) {
2534 case GL_UNSIGNED_BYTE:
2535 {
2536 GLubyte *dst = (GLubyte *) dest;
2537 GLuint i;
2538 for (i = 0; i < n; i++) {
2539 dst[i] = (GLubyte) (indexes[i] & 0xff);
2540 }
2541 }
2542 break;
2543 case GL_UNSIGNED_SHORT:
2544 {
2545 GLuint *dst = (GLuint *) dest;
2546 GLuint i;
2547 for (i = 0; i < n; i++) {
2548 dst[i] = (GLushort) (indexes[i] & 0xffff);
2549 }
2550 }
2551 break;
2552 case GL_UNSIGNED_INT:
2553 MEMCPY(dest, indexes, n * sizeof(GLuint));
2554 break;
2555 default:
2556 gl_problem(ctx, "bad dstType in _mesa_unpack_stencil_span");
2557 }
2558 }
2559 }
2560
2561
2562
2563 void
2564 _mesa_unpack_depth_span( const GLcontext *ctx, GLuint n, GLdepth *dest,
2565 GLenum srcType, const GLvoid *source,
2566 const struct gl_pixelstore_attrib *unpacking,
2567 GLboolean applyTransferOps )
2568 {
2569 GLfloat *depth = MALLOC(n * sizeof(GLfloat));
2570 if (!depth)
2571 return;
2572
2573 switch (srcType) {
2574 case GL_BYTE:
2575 {
2576 GLuint i;
2577 const GLubyte *src = (const GLubyte *) source;
2578 for (i = 0; i < n; i++) {
2579 depth[i] = BYTE_TO_FLOAT(src[i]);
2580 }
2581 }
2582 break;
2583 case GL_UNSIGNED_BYTE:
2584 {
2585 GLuint i;
2586 const GLubyte *src = (const GLubyte *) source;
2587 for (i = 0; i < n; i++) {
2588 depth[i] = UBYTE_TO_FLOAT(src[i]);
2589 }
2590 }
2591 break;
2592 case GL_SHORT:
2593 {
2594 GLuint i;
2595 const GLshort *src = (const GLshort *) source;
2596 for (i = 0; i < n; i++) {
2597 depth[i] = SHORT_TO_FLOAT(src[i]);
2598 }
2599 }
2600 break;
2601 case GL_UNSIGNED_SHORT:
2602 {
2603 GLuint i;
2604 const GLushort *src = (const GLushort *) source;
2605 for (i = 0; i < n; i++) {
2606 depth[i] = USHORT_TO_FLOAT(src[i]);
2607 }
2608 }
2609 break;
2610 case GL_INT:
2611 {
2612 GLuint i;
2613 const GLint *src = (const GLint *) source;
2614 for (i = 0; i < n; i++) {
2615 depth[i] = INT_TO_FLOAT(src[i]);
2616 }
2617 }
2618 break;
2619 case GL_UNSIGNED_INT:
2620 {
2621 GLuint i;
2622 const GLuint *src = (const GLuint *) source;
2623 for (i = 0; i < n; i++) {
2624 depth[i] = UINT_TO_FLOAT(src[i]);
2625 }
2626 }
2627 break;
2628 case GL_FLOAT:
2629 MEMCPY(depth, source, n * sizeof(GLfloat));
2630 break;
2631 default:
2632 gl_problem(NULL, "bad type in _mesa_unpack_depth_span()");
2633 return;
2634 }
2635
2636
2637 /* apply depth scale and bias */
2638 if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
2639 GLuint i;
2640 for (i = 0; i < n; i++) {
2641 depth[i] = depth[i] * ctx->Pixel.DepthScale + ctx->Pixel.DepthBias;
2642 }
2643 }
2644
2645 /* clamp depth values to [0,1] and convert from floats to integers */
2646 {
2647 const GLfloat zs = ctx->Visual->DepthMaxF;
2648 GLuint i;
2649 for (i = 0; i < n; i++) {
2650 dest[i] = (GLdepth) (CLAMP(depth[i], 0.0F, 1.0F) * zs);
2651 }
2652 }
2653
2654 FREE(depth);
2655 }
2656
2657
2658
2659 /*
2660 * Unpack image data. Apply byteswapping, byte flipping (bitmap).
2661 * Return all image data in a contiguous block.
2662 */
2663 void *
2664 _mesa_unpack_image( GLsizei width, GLsizei height, GLsizei depth,
2665 GLenum format, GLenum type, const GLvoid *pixels,
2666 const struct gl_pixelstore_attrib *unpack )
2667 {
2668 GLint bytesPerRow, compsPerRow;
2669 GLboolean flipBytes, swap2, swap4;
2670
2671 if (!pixels)
2672 return NULL; /* not necessarily an error */
2673
2674 if (width <= 0 || height <= 0 || depth <= 0)
2675 return NULL; /* generate error later */
2676
2677 if (format == GL_BITMAP) {
2678 bytesPerRow = (width + 7) >> 3;
2679 flipBytes = !unpack->LsbFirst;
2680 swap2 = swap4 = GL_FALSE;
2681 compsPerRow = 0;
2682 }
2683 else {
2684 const GLint bytesPerPixel = gl_bytes_per_pixel(format, type);
2685 const GLint components = gl_components_in_format(format);
2686 GLint bytesPerComp;
2687 if (bytesPerPixel <= 0 || components <= 0)
2688 return NULL; /* bad format or type. generate error later */
2689 bytesPerRow = bytesPerPixel * width;
2690 bytesPerComp = bytesPerPixel / components;
2691 flipBytes = GL_FALSE;
2692 swap2 = (bytesPerComp == 2) && unpack->SwapBytes;
2693 swap4 = (bytesPerComp == 4) && unpack->SwapBytes;
2694 compsPerRow = components * width;
2695 assert(compsPerRow >= width);
2696 }
2697
2698 {
2699 GLubyte *destBuffer = MALLOC(bytesPerRow * height * depth);
2700 GLubyte *dst;
2701 GLint img, row;
2702 if (!destBuffer)
2703 return NULL; /* generate GL_OUT_OF_MEMORY later */
2704
2705 dst = destBuffer;
2706 for (img = 0; img < depth; img++) {
2707 for (row = 0; row < height; row++) {
2708 const GLvoid *src = gl_pixel_addr_in_image(unpack, pixels,
2709 width, height, format, type, img, row, 0);
2710 MEMCPY(dst, src, bytesPerRow);
2711 /* byte flipping/swapping */
2712 if (flipBytes) {
2713 gl_flip_bytes((GLubyte *) dst, bytesPerRow);
2714 }
2715 else if (swap2) {
2716 gl_swap2((GLushort*) dst, compsPerRow);
2717 }
2718 else if (swap4) {
2719 gl_swap4((GLuint*) dst, compsPerRow);
2720 }
2721 dst += bytesPerRow;
2722 }
2723 }
2724 return destBuffer;
2725 }
2726 }
2727
2728
2729 /*
2730 * Unpack bitmap data. Resulting data will be in most-significant-bit-first
2731 * order with row alignment = 1 byte.
2732 */
2733 GLvoid *
2734 _mesa_unpack_bitmap( GLint width, GLint height, const GLubyte *pixels,
2735 const struct gl_pixelstore_attrib *packing )
2736 {
2737 GLint bytes, row, width_in_bytes;
2738 GLubyte *buffer, *dst;
2739
2740 if (!pixels)
2741 return NULL;
2742
2743 /* Alloc dest storage */
2744 bytes = ((width + 7) / 8 * height);
2745 buffer = (GLubyte *) MALLOC( bytes );
2746 if (!buffer)
2747 return NULL;
2748
2749
2750 width_in_bytes = CEILING( width, 8 );
2751 dst = buffer;
2752 for (row = 0; row < height; row++) {
2753 GLubyte *src = gl_pixel_addr_in_image( packing, pixels, width, height,
2754 GL_COLOR_INDEX, GL_BITMAP,
2755 0, row, 0 );
2756 if (!src) {
2757 FREE(buffer);
2758 return NULL;
2759 }
2760
2761 if (packing->SkipPixels == 0) {
2762 MEMCPY( dst, src, width_in_bytes );
2763 if (packing->LsbFirst) {
2764 gl_flip_bytes( dst, width_in_bytes );
2765 }
2766 }
2767 else {
2768 /* handling SkipPixels is a bit tricky (no pun intended!) */
2769 GLint i;
2770 if (packing->LsbFirst) {
2771 GLubyte srcMask = 1 << (packing->SkipPixels & 0x7);
2772 GLubyte dstMask = 128;
2773 GLubyte *s = src;
2774 GLubyte *d = dst;
2775 *d = 0;
2776 for (i = 0; i < width; i++) {
2777 if (*s & srcMask) {
2778 *d |= dstMask;
2779 }
2780 if (srcMask == 128) {
2781 srcMask = 1;
2782 s++;
2783 }
2784 else {
2785 srcMask = srcMask << 1;
2786 }
2787 if (dstMask == 1) {
2788 dstMask = 128;
2789 d++;
2790 *d = 0;
2791 }
2792 else {
2793 dstMask = dstMask >> 1;
2794 }
2795 }
2796 }
2797 else {
2798 GLubyte srcMask = 128 >> (packing->SkipPixels & 0x7);
2799 GLubyte dstMask = 128;
2800 GLubyte *s = src;
2801 GLubyte *d = dst;
2802 *d = 0;
2803 for (i = 0; i < width; i++) {
2804 if (*s & srcMask) {
2805 *d |= dstMask;
2806 }
2807 if (srcMask == 1) {
2808 srcMask = 128;
2809 s++;
2810 }
2811 else {
2812 srcMask = srcMask >> 1;
2813 }
2814 if (dstMask == 1) {
2815 dstMask = 128;
2816 d++;
2817 *d = 0;
2818 }
2819 else {
2820 dstMask = dstMask >> 1;
2821 }
2822 }
2823 }
2824 }
2825 dst += width_in_bytes;
2826 }
2827
2828 return buffer;
2829 }