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