Merge remote branch 'origin/master' into radeon-rewrite
[mesa.git] / src / mesa / main / image.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /**
27 * \file image.c
28 * Image handling.
29 */
30
31
32 #include "glheader.h"
33 #include "colormac.h"
34 #include "context.h"
35 #include "image.h"
36 #include "imports.h"
37 #include "macros.h"
38 #include "pixel.h"
39
40
41 /**
42 * NOTE:
43 * Normally, BYTE_TO_FLOAT(0) returns 0.00392 That causes problems when
44 * we later convert the float to a packed integer value (such as for
45 * GL_RGB5_A1) because we'll wind up with a non-zero value.
46 *
47 * We redefine the macros here so zero is handled correctly.
48 */
49 #undef BYTE_TO_FLOAT
50 #define BYTE_TO_FLOAT(B) ((B) == 0 ? 0.0F : ((2.0F * (B) + 1.0F) * (1.0F/255.0F)))
51
52 #undef SHORT_TO_FLOAT
53 #define SHORT_TO_FLOAT(S) ((S) == 0 ? 0.0F : ((2.0F * (S) + 1.0F) * (1.0F/65535.0F)))
54
55
56
57 /** Compute ceiling of integer quotient of A divided by B. */
58 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
59
60
61 /**
62 * \return GL_TRUE if type is packed pixel type, GL_FALSE otherwise.
63 */
64 GLboolean
65 _mesa_type_is_packed(GLenum type)
66 {
67 switch (type) {
68 case GL_UNSIGNED_BYTE_3_3_2:
69 case GL_UNSIGNED_BYTE_2_3_3_REV:
70 case GL_UNSIGNED_SHORT_5_6_5:
71 case GL_UNSIGNED_SHORT_5_6_5_REV:
72 case GL_UNSIGNED_SHORT_4_4_4_4:
73 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
74 case GL_UNSIGNED_SHORT_5_5_5_1:
75 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
76 case GL_UNSIGNED_INT_8_8_8_8:
77 case GL_UNSIGNED_INT_8_8_8_8_REV:
78 case GL_UNSIGNED_INT_10_10_10_2:
79 case GL_UNSIGNED_INT_2_10_10_10_REV:
80 case GL_UNSIGNED_SHORT_8_8_MESA:
81 case GL_UNSIGNED_SHORT_8_8_REV_MESA:
82 case GL_UNSIGNED_INT_24_8_EXT:
83 return GL_TRUE;
84 }
85
86 return GL_FALSE;
87 }
88
89 /**
90 * Flip the 8 bits in each byte of the given array.
91 *
92 * \param p array.
93 * \param n number of bytes.
94 *
95 * \todo try this trick to flip bytes someday:
96 * \code
97 * v = ((v & 0x55555555) << 1) | ((v >> 1) & 0x55555555);
98 * v = ((v & 0x33333333) << 2) | ((v >> 2) & 0x33333333);
99 * v = ((v & 0x0f0f0f0f) << 4) | ((v >> 4) & 0x0f0f0f0f);
100 * \endcode
101 */
102 static void
103 flip_bytes( GLubyte *p, GLuint n )
104 {
105 GLuint i, a, b;
106 for (i = 0; i < n; i++) {
107 b = (GLuint) p[i]; /* words are often faster than bytes */
108 a = ((b & 0x01) << 7) |
109 ((b & 0x02) << 5) |
110 ((b & 0x04) << 3) |
111 ((b & 0x08) << 1) |
112 ((b & 0x10) >> 1) |
113 ((b & 0x20) >> 3) |
114 ((b & 0x40) >> 5) |
115 ((b & 0x80) >> 7);
116 p[i] = (GLubyte) a;
117 }
118 }
119
120
121 /**
122 * Flip the order of the 2 bytes in each word in the given array.
123 *
124 * \param p array.
125 * \param n number of words.
126 */
127 void
128 _mesa_swap2( GLushort *p, GLuint n )
129 {
130 GLuint i;
131 for (i = 0; i < n; i++) {
132 p[i] = (p[i] >> 8) | ((p[i] << 8) & 0xff00);
133 }
134 }
135
136
137
138 /*
139 * Flip the order of the 4 bytes in each word in the given array.
140 */
141 void
142 _mesa_swap4( GLuint *p, GLuint n )
143 {
144 GLuint i, a, b;
145 for (i = 0; i < n; i++) {
146 b = p[i];
147 a = (b >> 24)
148 | ((b >> 8) & 0xff00)
149 | ((b << 8) & 0xff0000)
150 | ((b << 24) & 0xff000000);
151 p[i] = a;
152 }
153 }
154
155
156 /**
157 * Get the size of a GL data type.
158 *
159 * \param type GL data type.
160 *
161 * \return the size, in bytes, of the given data type, 0 if a GL_BITMAP, or -1
162 * if an invalid type enum.
163 */
164 GLint
165 _mesa_sizeof_type( GLenum type )
166 {
167 switch (type) {
168 case GL_BITMAP:
169 return 0;
170 case GL_UNSIGNED_BYTE:
171 return sizeof(GLubyte);
172 case GL_BYTE:
173 return sizeof(GLbyte);
174 case GL_UNSIGNED_SHORT:
175 return sizeof(GLushort);
176 case GL_SHORT:
177 return sizeof(GLshort);
178 case GL_UNSIGNED_INT:
179 return sizeof(GLuint);
180 case GL_INT:
181 return sizeof(GLint);
182 case GL_FLOAT:
183 return sizeof(GLfloat);
184 case GL_HALF_FLOAT_ARB:
185 return sizeof(GLhalfARB);
186 default:
187 return -1;
188 }
189 }
190
191
192 /**
193 * Same as _mesa_sizeof_type() but also accepting the packed pixel
194 * format data types.
195 */
196 GLint
197 _mesa_sizeof_packed_type( GLenum type )
198 {
199 switch (type) {
200 case GL_BITMAP:
201 return 0;
202 case GL_UNSIGNED_BYTE:
203 return sizeof(GLubyte);
204 case GL_BYTE:
205 return sizeof(GLbyte);
206 case GL_UNSIGNED_SHORT:
207 return sizeof(GLushort);
208 case GL_SHORT:
209 return sizeof(GLshort);
210 case GL_UNSIGNED_INT:
211 return sizeof(GLuint);
212 case GL_INT:
213 return sizeof(GLint);
214 case GL_HALF_FLOAT_ARB:
215 return sizeof(GLhalfARB);
216 case GL_FLOAT:
217 return sizeof(GLfloat);
218 case GL_UNSIGNED_BYTE_3_3_2:
219 return sizeof(GLubyte);
220 case GL_UNSIGNED_BYTE_2_3_3_REV:
221 return sizeof(GLubyte);
222 case GL_UNSIGNED_SHORT_5_6_5:
223 return sizeof(GLushort);
224 case GL_UNSIGNED_SHORT_5_6_5_REV:
225 return sizeof(GLushort);
226 case GL_UNSIGNED_SHORT_4_4_4_4:
227 return sizeof(GLushort);
228 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
229 return sizeof(GLushort);
230 case GL_UNSIGNED_SHORT_5_5_5_1:
231 return sizeof(GLushort);
232 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
233 return sizeof(GLushort);
234 case GL_UNSIGNED_INT_8_8_8_8:
235 return sizeof(GLuint);
236 case GL_UNSIGNED_INT_8_8_8_8_REV:
237 return sizeof(GLuint);
238 case GL_UNSIGNED_INT_10_10_10_2:
239 return sizeof(GLuint);
240 case GL_UNSIGNED_INT_2_10_10_10_REV:
241 return sizeof(GLuint);
242 case GL_UNSIGNED_SHORT_8_8_MESA:
243 case GL_UNSIGNED_SHORT_8_8_REV_MESA:
244 return sizeof(GLushort);
245 case GL_UNSIGNED_INT_24_8_EXT:
246 return sizeof(GLuint);
247 default:
248 return -1;
249 }
250 }
251
252
253 /**
254 * Get the number of components in a pixel format.
255 *
256 * \param format pixel format.
257 *
258 * \return the number of components in the given format, or -1 if a bad format.
259 */
260 GLint
261 _mesa_components_in_format( GLenum format )
262 {
263 switch (format) {
264 case GL_COLOR_INDEX:
265 case GL_COLOR_INDEX1_EXT:
266 case GL_COLOR_INDEX2_EXT:
267 case GL_COLOR_INDEX4_EXT:
268 case GL_COLOR_INDEX8_EXT:
269 case GL_COLOR_INDEX12_EXT:
270 case GL_COLOR_INDEX16_EXT:
271 case GL_STENCIL_INDEX:
272 case GL_DEPTH_COMPONENT:
273 case GL_RED:
274 case GL_GREEN:
275 case GL_BLUE:
276 case GL_ALPHA:
277 case GL_LUMINANCE:
278 case GL_INTENSITY:
279 return 1;
280 case GL_LUMINANCE_ALPHA:
281 return 2;
282 case GL_RGB:
283 return 3;
284 case GL_RGBA:
285 return 4;
286 case GL_BGR:
287 return 3;
288 case GL_BGRA:
289 return 4;
290 case GL_ABGR_EXT:
291 return 4;
292 case GL_YCBCR_MESA:
293 return 2;
294 case GL_DEPTH_STENCIL_EXT:
295 return 2;
296 case GL_DUDV_ATI:
297 case GL_DU8DV8_ATI:
298 return 2;
299 default:
300 return -1;
301 }
302 }
303
304
305 /**
306 * Get the bytes per pixel of pixel format type pair.
307 *
308 * \param format pixel format.
309 * \param type pixel type.
310 *
311 * \return bytes per pixel, or -1 if a bad format or type was given.
312 */
313 GLint
314 _mesa_bytes_per_pixel( GLenum format, GLenum type )
315 {
316 GLint comps = _mesa_components_in_format( format );
317 if (comps < 0)
318 return -1;
319
320 switch (type) {
321 case GL_BITMAP:
322 return 0; /* special case */
323 case GL_BYTE:
324 case GL_UNSIGNED_BYTE:
325 return comps * sizeof(GLubyte);
326 case GL_SHORT:
327 case GL_UNSIGNED_SHORT:
328 return comps * sizeof(GLshort);
329 case GL_INT:
330 case GL_UNSIGNED_INT:
331 return comps * sizeof(GLint);
332 case GL_FLOAT:
333 return comps * sizeof(GLfloat);
334 case GL_HALF_FLOAT_ARB:
335 return comps * sizeof(GLhalfARB);
336 case GL_UNSIGNED_BYTE_3_3_2:
337 case GL_UNSIGNED_BYTE_2_3_3_REV:
338 if (format == GL_RGB || format == GL_BGR)
339 return sizeof(GLubyte);
340 else
341 return -1; /* error */
342 case GL_UNSIGNED_SHORT_5_6_5:
343 case GL_UNSIGNED_SHORT_5_6_5_REV:
344 if (format == GL_RGB || format == GL_BGR)
345 return sizeof(GLushort);
346 else
347 return -1; /* error */
348 case GL_UNSIGNED_SHORT_4_4_4_4:
349 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
350 case GL_UNSIGNED_SHORT_5_5_5_1:
351 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
352 if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT)
353 return sizeof(GLushort);
354 else
355 return -1;
356 case GL_UNSIGNED_INT_8_8_8_8:
357 case GL_UNSIGNED_INT_8_8_8_8_REV:
358 case GL_UNSIGNED_INT_10_10_10_2:
359 case GL_UNSIGNED_INT_2_10_10_10_REV:
360 if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT)
361 return sizeof(GLuint);
362 else
363 return -1;
364 case GL_UNSIGNED_SHORT_8_8_MESA:
365 case GL_UNSIGNED_SHORT_8_8_REV_MESA:
366 if (format == GL_YCBCR_MESA)
367 return sizeof(GLushort);
368 else
369 return -1;
370 case GL_UNSIGNED_INT_24_8_EXT:
371 if (format == GL_DEPTH_STENCIL_EXT)
372 return sizeof(GLuint);
373 else
374 return -1;
375 default:
376 return -1;
377 }
378 }
379
380
381 /**
382 * Test for a legal pixel format and type.
383 *
384 * \param format pixel format.
385 * \param type pixel type.
386 *
387 * \return GL_TRUE if the given pixel format and type are legal, or GL_FALSE
388 * otherwise.
389 */
390 GLboolean
391 _mesa_is_legal_format_and_type( GLcontext *ctx, GLenum format, GLenum type )
392 {
393 switch (format) {
394 case GL_COLOR_INDEX:
395 case GL_STENCIL_INDEX:
396 switch (type) {
397 case GL_BITMAP:
398 case GL_BYTE:
399 case GL_UNSIGNED_BYTE:
400 case GL_SHORT:
401 case GL_UNSIGNED_SHORT:
402 case GL_INT:
403 case GL_UNSIGNED_INT:
404 case GL_FLOAT:
405 return GL_TRUE;
406 case GL_HALF_FLOAT_ARB:
407 return ctx->Extensions.ARB_half_float_pixel;
408 default:
409 return GL_FALSE;
410 }
411 case GL_RED:
412 case GL_GREEN:
413 case GL_BLUE:
414 case GL_ALPHA:
415 #if 0 /* not legal! see table 3.6 of the 1.5 spec */
416 case GL_INTENSITY:
417 #endif
418 case GL_LUMINANCE:
419 case GL_LUMINANCE_ALPHA:
420 case GL_DEPTH_COMPONENT:
421 switch (type) {
422 case GL_BYTE:
423 case GL_UNSIGNED_BYTE:
424 case GL_SHORT:
425 case GL_UNSIGNED_SHORT:
426 case GL_INT:
427 case GL_UNSIGNED_INT:
428 case GL_FLOAT:
429 return GL_TRUE;
430 case GL_HALF_FLOAT_ARB:
431 return ctx->Extensions.ARB_half_float_pixel;
432 default:
433 return GL_FALSE;
434 }
435 case GL_RGB:
436 switch (type) {
437 case GL_BYTE:
438 case GL_UNSIGNED_BYTE:
439 case GL_SHORT:
440 case GL_UNSIGNED_SHORT:
441 case GL_INT:
442 case GL_UNSIGNED_INT:
443 case GL_FLOAT:
444 case GL_UNSIGNED_BYTE_3_3_2:
445 case GL_UNSIGNED_BYTE_2_3_3_REV:
446 case GL_UNSIGNED_SHORT_5_6_5:
447 case GL_UNSIGNED_SHORT_5_6_5_REV:
448 return GL_TRUE;
449 case GL_HALF_FLOAT_ARB:
450 return ctx->Extensions.ARB_half_float_pixel;
451 default:
452 return GL_FALSE;
453 }
454 case GL_BGR:
455 switch (type) {
456 /* NOTE: no packed types are supported with BGR. That's
457 * intentional, according to the GL spec.
458 */
459 case GL_BYTE:
460 case GL_UNSIGNED_BYTE:
461 case GL_SHORT:
462 case GL_UNSIGNED_SHORT:
463 case GL_INT:
464 case GL_UNSIGNED_INT:
465 case GL_FLOAT:
466 return GL_TRUE;
467 case GL_HALF_FLOAT_ARB:
468 return ctx->Extensions.ARB_half_float_pixel;
469 default:
470 return GL_FALSE;
471 }
472 case GL_RGBA:
473 case GL_BGRA:
474 case GL_ABGR_EXT:
475 switch (type) {
476 case GL_BYTE:
477 case GL_UNSIGNED_BYTE:
478 case GL_SHORT:
479 case GL_UNSIGNED_SHORT:
480 case GL_INT:
481 case GL_UNSIGNED_INT:
482 case GL_FLOAT:
483 case GL_UNSIGNED_SHORT_4_4_4_4:
484 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
485 case GL_UNSIGNED_SHORT_5_5_5_1:
486 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
487 case GL_UNSIGNED_INT_8_8_8_8:
488 case GL_UNSIGNED_INT_8_8_8_8_REV:
489 case GL_UNSIGNED_INT_10_10_10_2:
490 case GL_UNSIGNED_INT_2_10_10_10_REV:
491 return GL_TRUE;
492 case GL_HALF_FLOAT_ARB:
493 return ctx->Extensions.ARB_half_float_pixel;
494 default:
495 return GL_FALSE;
496 }
497 case GL_YCBCR_MESA:
498 if (type == GL_UNSIGNED_SHORT_8_8_MESA ||
499 type == GL_UNSIGNED_SHORT_8_8_REV_MESA)
500 return GL_TRUE;
501 else
502 return GL_FALSE;
503 case GL_DEPTH_STENCIL_EXT:
504 if (ctx->Extensions.EXT_packed_depth_stencil
505 && type == GL_UNSIGNED_INT_24_8_EXT)
506 return GL_TRUE;
507 else
508 return GL_FALSE;
509 case GL_DUDV_ATI:
510 case GL_DU8DV8_ATI:
511 switch (type) {
512 case GL_BYTE:
513 case GL_UNSIGNED_BYTE:
514 case GL_SHORT:
515 case GL_UNSIGNED_SHORT:
516 case GL_INT:
517 case GL_UNSIGNED_INT:
518 case GL_FLOAT:
519 return GL_TRUE;
520 default:
521 return GL_FALSE;
522 }
523 default:
524 ; /* fall-through */
525 }
526 return GL_FALSE;
527 }
528
529
530 /**
531 * Return the address of a specific pixel in an image (1D, 2D or 3D).
532 *
533 * Pixel unpacking/packing parameters are observed according to \p packing.
534 *
535 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
536 * \param image starting address of image data
537 * \param width the image width
538 * \param height theimage height
539 * \param format the pixel format
540 * \param type the pixel data type
541 * \param packing the pixelstore attributes
542 * \param img which image in the volume (0 for 1D or 2D images)
543 * \param row row of pixel in the image (0 for 1D images)
544 * \param column column of pixel in the image
545 *
546 * \return address of pixel on success, or NULL on error.
547 *
548 * \sa gl_pixelstore_attrib.
549 */
550 GLvoid *
551 _mesa_image_address( GLuint dimensions,
552 const struct gl_pixelstore_attrib *packing,
553 const GLvoid *image,
554 GLsizei width, GLsizei height,
555 GLenum format, GLenum type,
556 GLint img, GLint row, GLint column )
557 {
558 GLint alignment; /* 1, 2 or 4 */
559 GLint pixels_per_row;
560 GLint rows_per_image;
561 GLint skiprows;
562 GLint skippixels;
563 GLint skipimages; /* for 3-D volume images */
564 GLubyte *pixel_addr;
565
566 ASSERT(dimensions >= 1 && dimensions <= 3);
567
568 alignment = packing->Alignment;
569 if (packing->RowLength > 0) {
570 pixels_per_row = packing->RowLength;
571 }
572 else {
573 pixels_per_row = width;
574 }
575 if (packing->ImageHeight > 0) {
576 rows_per_image = packing->ImageHeight;
577 }
578 else {
579 rows_per_image = height;
580 }
581
582 skippixels = packing->SkipPixels;
583 /* Note: SKIP_ROWS _is_ used for 1D images */
584 skiprows = packing->SkipRows;
585 /* Note: SKIP_IMAGES is only used for 3D images */
586 skipimages = (dimensions == 3) ? packing->SkipImages : 0;
587
588 if (type == GL_BITMAP) {
589 /* BITMAP data */
590 GLint comp_per_pixel; /* components per pixel */
591 GLint bytes_per_comp; /* bytes per component */
592 GLint bytes_per_row;
593 GLint bytes_per_image;
594
595 /* Compute bytes per component */
596 bytes_per_comp = _mesa_sizeof_packed_type( type );
597 if (bytes_per_comp < 0) {
598 return NULL;
599 }
600
601 /* Compute number of components per pixel */
602 comp_per_pixel = _mesa_components_in_format( format );
603 if (comp_per_pixel < 0) {
604 return NULL;
605 }
606
607 bytes_per_row = alignment
608 * CEILING( comp_per_pixel*pixels_per_row, 8*alignment );
609
610 bytes_per_image = bytes_per_row * rows_per_image;
611
612 pixel_addr = (GLubyte *) image
613 + (skipimages + img) * bytes_per_image
614 + (skiprows + row) * bytes_per_row
615 + (skippixels + column) / 8;
616 }
617 else {
618 /* Non-BITMAP data */
619 GLint bytes_per_pixel, bytes_per_row, remainder, bytes_per_image;
620 GLint topOfImage;
621
622 bytes_per_pixel = _mesa_bytes_per_pixel( format, type );
623
624 /* The pixel type and format should have been error checked earlier */
625 assert(bytes_per_pixel > 0);
626
627 bytes_per_row = pixels_per_row * bytes_per_pixel;
628 remainder = bytes_per_row % alignment;
629 if (remainder > 0)
630 bytes_per_row += (alignment - remainder);
631
632 ASSERT(bytes_per_row % alignment == 0);
633
634 bytes_per_image = bytes_per_row * rows_per_image;
635
636 if (packing->Invert) {
637 /* set pixel_addr to the last row */
638 topOfImage = bytes_per_row * (height - 1);
639 bytes_per_row = -bytes_per_row;
640 }
641 else {
642 topOfImage = 0;
643 }
644
645 /* compute final pixel address */
646 pixel_addr = (GLubyte *) image
647 + (skipimages + img) * bytes_per_image
648 + topOfImage
649 + (skiprows + row) * bytes_per_row
650 + (skippixels + column) * bytes_per_pixel;
651 }
652
653 return (GLvoid *) pixel_addr;
654 }
655
656
657 GLvoid *
658 _mesa_image_address1d( const struct gl_pixelstore_attrib *packing,
659 const GLvoid *image,
660 GLsizei width,
661 GLenum format, GLenum type,
662 GLint column )
663 {
664 return _mesa_image_address(1, packing, image, width, 1,
665 format, type, 0, 0, column);
666 }
667
668
669 GLvoid *
670 _mesa_image_address2d( const struct gl_pixelstore_attrib *packing,
671 const GLvoid *image,
672 GLsizei width, GLsizei height,
673 GLenum format, GLenum type,
674 GLint row, GLint column )
675 {
676 return _mesa_image_address(2, packing, image, width, height,
677 format, type, 0, row, column);
678 }
679
680
681 GLvoid *
682 _mesa_image_address3d( const struct gl_pixelstore_attrib *packing,
683 const GLvoid *image,
684 GLsizei width, GLsizei height,
685 GLenum format, GLenum type,
686 GLint img, GLint row, GLint column )
687 {
688 return _mesa_image_address(3, packing, image, width, height,
689 format, type, img, row, column);
690 }
691
692
693
694 /**
695 * Compute the stride (in bytes) between image rows.
696 *
697 * \param packing the pixelstore attributes
698 * \param width image width.
699 * \param format pixel format.
700 * \param type pixel data type.
701 *
702 * \return the stride in bytes for the given parameters, or -1 if error
703 */
704 GLint
705 _mesa_image_row_stride( const struct gl_pixelstore_attrib *packing,
706 GLint width, GLenum format, GLenum type )
707 {
708 GLint bytesPerRow, remainder;
709
710 ASSERT(packing);
711
712 if (type == GL_BITMAP) {
713 if (packing->RowLength == 0) {
714 bytesPerRow = (width + 7) / 8;
715 }
716 else {
717 bytesPerRow = (packing->RowLength + 7) / 8;
718 }
719 }
720 else {
721 /* Non-BITMAP data */
722 const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
723 if (bytesPerPixel <= 0)
724 return -1; /* error */
725 if (packing->RowLength == 0) {
726 bytesPerRow = bytesPerPixel * width;
727 }
728 else {
729 bytesPerRow = bytesPerPixel * packing->RowLength;
730 }
731 }
732
733 remainder = bytesPerRow % packing->Alignment;
734 if (remainder > 0) {
735 bytesPerRow += (packing->Alignment - remainder);
736 }
737
738 if (packing->Invert) {
739 /* negate the bytes per row (negative row stride) */
740 bytesPerRow = -bytesPerRow;
741 }
742
743 return bytesPerRow;
744 }
745
746
747 #if _HAVE_FULL_GL
748
749 /*
750 * Compute the stride between images in a 3D texture (in bytes) for the given
751 * pixel packing parameters and image width, format and type.
752 */
753 GLint
754 _mesa_image_image_stride( const struct gl_pixelstore_attrib *packing,
755 GLint width, GLint height,
756 GLenum format, GLenum type )
757 {
758 ASSERT(packing);
759 ASSERT(type != GL_BITMAP);
760
761 {
762 const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
763 GLint bytesPerRow, bytesPerImage, remainder;
764
765 if (bytesPerPixel <= 0)
766 return -1; /* error */
767 if (packing->RowLength == 0) {
768 bytesPerRow = bytesPerPixel * width;
769 }
770 else {
771 bytesPerRow = bytesPerPixel * packing->RowLength;
772 }
773 remainder = bytesPerRow % packing->Alignment;
774 if (remainder > 0)
775 bytesPerRow += (packing->Alignment - remainder);
776
777 if (packing->ImageHeight == 0)
778 bytesPerImage = bytesPerRow * height;
779 else
780 bytesPerImage = bytesPerRow * packing->ImageHeight;
781
782 return bytesPerImage;
783 }
784 }
785
786
787 /*
788 * Unpack a 32x32 pixel polygon stipple from user memory using the
789 * current pixel unpack settings.
790 */
791 void
792 _mesa_unpack_polygon_stipple( const GLubyte *pattern, GLuint dest[32],
793 const struct gl_pixelstore_attrib *unpacking )
794 {
795 GLubyte *ptrn = (GLubyte *) _mesa_unpack_bitmap(32, 32, pattern, unpacking);
796 if (ptrn) {
797 /* Convert pattern from GLubytes to GLuints and handle big/little
798 * endian differences
799 */
800 GLubyte *p = ptrn;
801 GLint i;
802 for (i = 0; i < 32; i++) {
803 dest[i] = (p[0] << 24)
804 | (p[1] << 16)
805 | (p[2] << 8)
806 | (p[3] );
807 p += 4;
808 }
809 _mesa_free(ptrn);
810 }
811 }
812
813
814 /*
815 * Pack polygon stipple into user memory given current pixel packing
816 * settings.
817 */
818 void
819 _mesa_pack_polygon_stipple( const GLuint pattern[32], GLubyte *dest,
820 const struct gl_pixelstore_attrib *packing )
821 {
822 /* Convert pattern from GLuints to GLubytes to handle big/little
823 * endian differences.
824 */
825 GLubyte ptrn[32*4];
826 GLint i;
827 for (i = 0; i < 32; i++) {
828 ptrn[i * 4 + 0] = (GLubyte) ((pattern[i] >> 24) & 0xff);
829 ptrn[i * 4 + 1] = (GLubyte) ((pattern[i] >> 16) & 0xff);
830 ptrn[i * 4 + 2] = (GLubyte) ((pattern[i] >> 8 ) & 0xff);
831 ptrn[i * 4 + 3] = (GLubyte) ((pattern[i] ) & 0xff);
832 }
833
834 _mesa_pack_bitmap(32, 32, ptrn, dest, packing);
835 }
836
837
838 /*
839 * Unpack bitmap data. Resulting data will be in most-significant-bit-first
840 * order with row alignment = 1 byte.
841 */
842 GLvoid *
843 _mesa_unpack_bitmap( GLint width, GLint height, const GLubyte *pixels,
844 const struct gl_pixelstore_attrib *packing )
845 {
846 GLint bytes, row, width_in_bytes;
847 GLubyte *buffer, *dst;
848
849 if (!pixels)
850 return NULL;
851
852 /* Alloc dest storage */
853 bytes = ((width + 7) / 8 * height);
854 buffer = (GLubyte *) _mesa_malloc( bytes );
855 if (!buffer)
856 return NULL;
857
858 width_in_bytes = CEILING( width, 8 );
859 dst = buffer;
860 for (row = 0; row < height; row++) {
861 const GLubyte *src = (const GLubyte *)
862 _mesa_image_address2d(packing, pixels, width, height,
863 GL_COLOR_INDEX, GL_BITMAP, row, 0);
864 if (!src) {
865 _mesa_free(buffer);
866 return NULL;
867 }
868
869 if ((packing->SkipPixels & 7) == 0) {
870 _mesa_memcpy( dst, src, width_in_bytes );
871 if (packing->LsbFirst) {
872 flip_bytes( dst, width_in_bytes );
873 }
874 }
875 else {
876 /* handling SkipPixels is a bit tricky (no pun intended!) */
877 GLint i;
878 if (packing->LsbFirst) {
879 GLubyte srcMask = 1 << (packing->SkipPixels & 0x7);
880 GLubyte dstMask = 128;
881 const GLubyte *s = src;
882 GLubyte *d = dst;
883 *d = 0;
884 for (i = 0; i < width; i++) {
885 if (*s & srcMask) {
886 *d |= dstMask;
887 }
888 if (srcMask == 128) {
889 srcMask = 1;
890 s++;
891 }
892 else {
893 srcMask = srcMask << 1;
894 }
895 if (dstMask == 1) {
896 dstMask = 128;
897 d++;
898 *d = 0;
899 }
900 else {
901 dstMask = dstMask >> 1;
902 }
903 }
904 }
905 else {
906 GLubyte srcMask = 128 >> (packing->SkipPixels & 0x7);
907 GLubyte dstMask = 128;
908 const GLubyte *s = src;
909 GLubyte *d = dst;
910 *d = 0;
911 for (i = 0; i < width; i++) {
912 if (*s & srcMask) {
913 *d |= dstMask;
914 }
915 if (srcMask == 1) {
916 srcMask = 128;
917 s++;
918 }
919 else {
920 srcMask = srcMask >> 1;
921 }
922 if (dstMask == 1) {
923 dstMask = 128;
924 d++;
925 *d = 0;
926 }
927 else {
928 dstMask = dstMask >> 1;
929 }
930 }
931 }
932 }
933 dst += width_in_bytes;
934 }
935
936 return buffer;
937 }
938
939
940 /*
941 * Pack bitmap data.
942 */
943 void
944 _mesa_pack_bitmap( GLint width, GLint height, const GLubyte *source,
945 GLubyte *dest, const struct gl_pixelstore_attrib *packing )
946 {
947 GLint row, width_in_bytes;
948 const GLubyte *src;
949
950 if (!source)
951 return;
952
953 width_in_bytes = CEILING( width, 8 );
954 src = source;
955 for (row = 0; row < height; row++) {
956 GLubyte *dst = (GLubyte *) _mesa_image_address2d(packing, dest,
957 width, height, GL_COLOR_INDEX, GL_BITMAP, row, 0);
958 if (!dst)
959 return;
960
961 if ((packing->SkipPixels & 7) == 0) {
962 _mesa_memcpy( dst, src, width_in_bytes );
963 if (packing->LsbFirst) {
964 flip_bytes( dst, width_in_bytes );
965 }
966 }
967 else {
968 /* handling SkipPixels is a bit tricky (no pun intended!) */
969 GLint i;
970 if (packing->LsbFirst) {
971 GLubyte srcMask = 128;
972 GLubyte dstMask = 1 << (packing->SkipPixels & 0x7);
973 const GLubyte *s = src;
974 GLubyte *d = dst;
975 *d = 0;
976 for (i = 0; i < width; i++) {
977 if (*s & srcMask) {
978 *d |= dstMask;
979 }
980 if (srcMask == 1) {
981 srcMask = 128;
982 s++;
983 }
984 else {
985 srcMask = srcMask >> 1;
986 }
987 if (dstMask == 128) {
988 dstMask = 1;
989 d++;
990 *d = 0;
991 }
992 else {
993 dstMask = dstMask << 1;
994 }
995 }
996 }
997 else {
998 GLubyte srcMask = 128;
999 GLubyte dstMask = 128 >> (packing->SkipPixels & 0x7);
1000 const GLubyte *s = src;
1001 GLubyte *d = dst;
1002 *d = 0;
1003 for (i = 0; i < width; i++) {
1004 if (*s & srcMask) {
1005 *d |= dstMask;
1006 }
1007 if (srcMask == 1) {
1008 srcMask = 128;
1009 s++;
1010 }
1011 else {
1012 srcMask = srcMask >> 1;
1013 }
1014 if (dstMask == 1) {
1015 dstMask = 128;
1016 d++;
1017 *d = 0;
1018 }
1019 else {
1020 dstMask = dstMask >> 1;
1021 }
1022 }
1023 }
1024 }
1025 src += width_in_bytes;
1026 }
1027 }
1028
1029
1030 /**********************************************************************/
1031 /***** Pixel processing functions ******/
1032 /**********************************************************************/
1033
1034 /*
1035 * Apply scale and bias factors to an array of RGBA pixels.
1036 */
1037 void
1038 _mesa_scale_and_bias_rgba(GLuint n, GLfloat rgba[][4],
1039 GLfloat rScale, GLfloat gScale,
1040 GLfloat bScale, GLfloat aScale,
1041 GLfloat rBias, GLfloat gBias,
1042 GLfloat bBias, GLfloat aBias)
1043 {
1044 if (rScale != 1.0 || rBias != 0.0) {
1045 GLuint i;
1046 for (i = 0; i < n; i++) {
1047 rgba[i][RCOMP] = rgba[i][RCOMP] * rScale + rBias;
1048 }
1049 }
1050 if (gScale != 1.0 || gBias != 0.0) {
1051 GLuint i;
1052 for (i = 0; i < n; i++) {
1053 rgba[i][GCOMP] = rgba[i][GCOMP] * gScale + gBias;
1054 }
1055 }
1056 if (bScale != 1.0 || bBias != 0.0) {
1057 GLuint i;
1058 for (i = 0; i < n; i++) {
1059 rgba[i][BCOMP] = rgba[i][BCOMP] * bScale + bBias;
1060 }
1061 }
1062 if (aScale != 1.0 || aBias != 0.0) {
1063 GLuint i;
1064 for (i = 0; i < n; i++) {
1065 rgba[i][ACOMP] = rgba[i][ACOMP] * aScale + aBias;
1066 }
1067 }
1068 }
1069
1070
1071 /*
1072 * Apply pixel mapping to an array of floating point RGBA pixels.
1073 */
1074 void
1075 _mesa_map_rgba( const GLcontext *ctx, GLuint n, GLfloat rgba[][4] )
1076 {
1077 const GLfloat rscale = (GLfloat) (ctx->PixelMaps.RtoR.Size - 1);
1078 const GLfloat gscale = (GLfloat) (ctx->PixelMaps.GtoG.Size - 1);
1079 const GLfloat bscale = (GLfloat) (ctx->PixelMaps.BtoB.Size - 1);
1080 const GLfloat ascale = (GLfloat) (ctx->PixelMaps.AtoA.Size - 1);
1081 const GLfloat *rMap = ctx->PixelMaps.RtoR.Map;
1082 const GLfloat *gMap = ctx->PixelMaps.GtoG.Map;
1083 const GLfloat *bMap = ctx->PixelMaps.BtoB.Map;
1084 const GLfloat *aMap = ctx->PixelMaps.AtoA.Map;
1085 GLuint i;
1086 for (i=0;i<n;i++) {
1087 GLfloat r = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F);
1088 GLfloat g = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F);
1089 GLfloat b = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F);
1090 GLfloat a = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F);
1091 rgba[i][RCOMP] = rMap[IROUND(r * rscale)];
1092 rgba[i][GCOMP] = gMap[IROUND(g * gscale)];
1093 rgba[i][BCOMP] = bMap[IROUND(b * bscale)];
1094 rgba[i][ACOMP] = aMap[IROUND(a * ascale)];
1095 }
1096 }
1097
1098
1099 /*
1100 * Apply the color matrix and post color matrix scaling and biasing.
1101 */
1102 void
1103 _mesa_transform_rgba(const GLcontext *ctx, GLuint n, GLfloat rgba[][4])
1104 {
1105 const GLfloat rs = ctx->Pixel.PostColorMatrixScale[0];
1106 const GLfloat rb = ctx->Pixel.PostColorMatrixBias[0];
1107 const GLfloat gs = ctx->Pixel.PostColorMatrixScale[1];
1108 const GLfloat gb = ctx->Pixel.PostColorMatrixBias[1];
1109 const GLfloat bs = ctx->Pixel.PostColorMatrixScale[2];
1110 const GLfloat bb = ctx->Pixel.PostColorMatrixBias[2];
1111 const GLfloat as = ctx->Pixel.PostColorMatrixScale[3];
1112 const GLfloat ab = ctx->Pixel.PostColorMatrixBias[3];
1113 const GLfloat *m = ctx->ColorMatrixStack.Top->m;
1114 GLuint i;
1115 for (i = 0; i < n; i++) {
1116 const GLfloat r = rgba[i][RCOMP];
1117 const GLfloat g = rgba[i][GCOMP];
1118 const GLfloat b = rgba[i][BCOMP];
1119 const GLfloat a = rgba[i][ACOMP];
1120 rgba[i][RCOMP] = (m[0] * r + m[4] * g + m[ 8] * b + m[12] * a) * rs + rb;
1121 rgba[i][GCOMP] = (m[1] * r + m[5] * g + m[ 9] * b + m[13] * a) * gs + gb;
1122 rgba[i][BCOMP] = (m[2] * r + m[6] * g + m[10] * b + m[14] * a) * bs + bb;
1123 rgba[i][ACOMP] = (m[3] * r + m[7] * g + m[11] * b + m[15] * a) * as + ab;
1124 }
1125 }
1126
1127
1128 /**
1129 * Apply a color table lookup to an array of floating point RGBA colors.
1130 */
1131 void
1132 _mesa_lookup_rgba_float(const struct gl_color_table *table,
1133 GLuint n, GLfloat rgba[][4])
1134 {
1135 const GLint max = table->Size - 1;
1136 const GLfloat scale = (GLfloat) max;
1137 const GLfloat *lut = table->TableF;
1138 GLuint i;
1139
1140 if (!table->TableF || table->Size == 0)
1141 return;
1142
1143 switch (table->_BaseFormat) {
1144 case GL_INTENSITY:
1145 /* replace RGBA with I */
1146 for (i = 0; i < n; i++) {
1147 GLint j = IROUND(rgba[i][RCOMP] * scale);
1148 GLfloat c = lut[CLAMP(j, 0, max)];
1149 rgba[i][RCOMP] =
1150 rgba[i][GCOMP] =
1151 rgba[i][BCOMP] =
1152 rgba[i][ACOMP] = c;
1153 }
1154 break;
1155 case GL_LUMINANCE:
1156 /* replace RGB with L */
1157 for (i = 0; i < n; i++) {
1158 GLint j = IROUND(rgba[i][RCOMP] * scale);
1159 GLfloat c = lut[CLAMP(j, 0, max)];
1160 rgba[i][RCOMP] =
1161 rgba[i][GCOMP] =
1162 rgba[i][BCOMP] = c;
1163 }
1164 break;
1165 case GL_ALPHA:
1166 /* replace A with A */
1167 for (i = 0; i < n; i++) {
1168 GLint j = IROUND(rgba[i][ACOMP] * scale);
1169 rgba[i][ACOMP] = lut[CLAMP(j, 0, max)];
1170 }
1171 break;
1172 case GL_LUMINANCE_ALPHA:
1173 /* replace RGBA with LLLA */
1174 for (i = 0; i < n; i++) {
1175 GLint jL = IROUND(rgba[i][RCOMP] * scale);
1176 GLint jA = IROUND(rgba[i][ACOMP] * scale);
1177 GLfloat luminance, alpha;
1178 jL = CLAMP(jL, 0, max);
1179 jA = CLAMP(jA, 0, max);
1180 luminance = lut[jL * 2 + 0];
1181 alpha = lut[jA * 2 + 1];
1182 rgba[i][RCOMP] =
1183 rgba[i][GCOMP] =
1184 rgba[i][BCOMP] = luminance;
1185 rgba[i][ACOMP] = alpha;;
1186 }
1187 break;
1188 case GL_RGB:
1189 /* replace RGB with RGB */
1190 for (i = 0; i < n; i++) {
1191 GLint jR = IROUND(rgba[i][RCOMP] * scale);
1192 GLint jG = IROUND(rgba[i][GCOMP] * scale);
1193 GLint jB = IROUND(rgba[i][BCOMP] * scale);
1194 jR = CLAMP(jR, 0, max);
1195 jG = CLAMP(jG, 0, max);
1196 jB = CLAMP(jB, 0, max);
1197 rgba[i][RCOMP] = lut[jR * 3 + 0];
1198 rgba[i][GCOMP] = lut[jG * 3 + 1];
1199 rgba[i][BCOMP] = lut[jB * 3 + 2];
1200 }
1201 break;
1202 case GL_RGBA:
1203 /* replace RGBA with RGBA */
1204 for (i = 0; i < n; i++) {
1205 GLint jR = IROUND(rgba[i][RCOMP] * scale);
1206 GLint jG = IROUND(rgba[i][GCOMP] * scale);
1207 GLint jB = IROUND(rgba[i][BCOMP] * scale);
1208 GLint jA = IROUND(rgba[i][ACOMP] * scale);
1209 jR = CLAMP(jR, 0, max);
1210 jG = CLAMP(jG, 0, max);
1211 jB = CLAMP(jB, 0, max);
1212 jA = CLAMP(jA, 0, max);
1213 rgba[i][RCOMP] = lut[jR * 4 + 0];
1214 rgba[i][GCOMP] = lut[jG * 4 + 1];
1215 rgba[i][BCOMP] = lut[jB * 4 + 2];
1216 rgba[i][ACOMP] = lut[jA * 4 + 3];
1217 }
1218 break;
1219 default:
1220 _mesa_problem(NULL, "Bad format in _mesa_lookup_rgba_float");
1221 return;
1222 }
1223 }
1224
1225
1226
1227 /**
1228 * Apply a color table lookup to an array of ubyte/RGBA colors.
1229 */
1230 void
1231 _mesa_lookup_rgba_ubyte(const struct gl_color_table *table,
1232 GLuint n, GLubyte rgba[][4])
1233 {
1234 const GLubyte *lut = table->TableUB;
1235 const GLfloat scale = (GLfloat) (table->Size - 1) / (GLfloat)255.0;
1236 GLuint i;
1237
1238 if (!table->TableUB || table->Size == 0)
1239 return;
1240
1241 switch (table->_BaseFormat) {
1242 case GL_INTENSITY:
1243 /* replace RGBA with I */
1244 if (table->Size == 256) {
1245 for (i = 0; i < n; i++) {
1246 const GLubyte c = lut[rgba[i][RCOMP]];
1247 rgba[i][RCOMP] =
1248 rgba[i][GCOMP] =
1249 rgba[i][BCOMP] =
1250 rgba[i][ACOMP] = c;
1251 }
1252 }
1253 else {
1254 for (i = 0; i < n; i++) {
1255 GLint j = IROUND((GLfloat) rgba[i][RCOMP] * scale);
1256 rgba[i][RCOMP] =
1257 rgba[i][GCOMP] =
1258 rgba[i][BCOMP] =
1259 rgba[i][ACOMP] = lut[j];
1260 }
1261 }
1262 break;
1263 case GL_LUMINANCE:
1264 /* replace RGB with L */
1265 if (table->Size == 256) {
1266 for (i = 0; i < n; i++) {
1267 const GLubyte c = lut[rgba[i][RCOMP]];
1268 rgba[i][RCOMP] =
1269 rgba[i][GCOMP] =
1270 rgba[i][BCOMP] = c;
1271 }
1272 }
1273 else {
1274 for (i = 0; i < n; i++) {
1275 GLint j = IROUND((GLfloat) rgba[i][RCOMP] * scale);
1276 rgba[i][RCOMP] =
1277 rgba[i][GCOMP] =
1278 rgba[i][BCOMP] = lut[j];
1279 }
1280 }
1281 break;
1282 case GL_ALPHA:
1283 /* replace A with A */
1284 if (table->Size == 256) {
1285 for (i = 0; i < n; i++) {
1286 rgba[i][ACOMP] = lut[rgba[i][ACOMP]];
1287 }
1288 }
1289 else {
1290 for (i = 0; i < n; i++) {
1291 GLint j = IROUND((GLfloat) rgba[i][ACOMP] * scale);
1292 rgba[i][ACOMP] = lut[j];
1293 }
1294 }
1295 break;
1296 case GL_LUMINANCE_ALPHA:
1297 /* replace RGBA with LLLA */
1298 if (table->Size == 256) {
1299 for (i = 0; i < n; i++) {
1300 GLubyte l = lut[rgba[i][RCOMP] * 2 + 0];
1301 GLubyte a = lut[rgba[i][ACOMP] * 2 + 1];;
1302 rgba[i][RCOMP] =
1303 rgba[i][GCOMP] =
1304 rgba[i][BCOMP] = l;
1305 rgba[i][ACOMP] = a;
1306 }
1307 }
1308 else {
1309 for (i = 0; i < n; i++) {
1310 GLint jL = IROUND((GLfloat) rgba[i][RCOMP] * scale);
1311 GLint jA = IROUND((GLfloat) rgba[i][ACOMP] * scale);
1312 GLubyte luminance = lut[jL * 2 + 0];
1313 GLubyte alpha = lut[jA * 2 + 1];
1314 rgba[i][RCOMP] =
1315 rgba[i][GCOMP] =
1316 rgba[i][BCOMP] = luminance;
1317 rgba[i][ACOMP] = alpha;
1318 }
1319 }
1320 break;
1321 case GL_RGB:
1322 if (table->Size == 256) {
1323 for (i = 0; i < n; i++) {
1324 rgba[i][RCOMP] = lut[rgba[i][RCOMP] * 3 + 0];
1325 rgba[i][GCOMP] = lut[rgba[i][GCOMP] * 3 + 1];
1326 rgba[i][BCOMP] = lut[rgba[i][BCOMP] * 3 + 2];
1327 }
1328 }
1329 else {
1330 for (i = 0; i < n; i++) {
1331 GLint jR = IROUND((GLfloat) rgba[i][RCOMP] * scale);
1332 GLint jG = IROUND((GLfloat) rgba[i][GCOMP] * scale);
1333 GLint jB = IROUND((GLfloat) rgba[i][BCOMP] * scale);
1334 rgba[i][RCOMP] = lut[jR * 3 + 0];
1335 rgba[i][GCOMP] = lut[jG * 3 + 1];
1336 rgba[i][BCOMP] = lut[jB * 3 + 2];
1337 }
1338 }
1339 break;
1340 case GL_RGBA:
1341 if (table->Size == 256) {
1342 for (i = 0; i < n; i++) {
1343 rgba[i][RCOMP] = lut[rgba[i][RCOMP] * 4 + 0];
1344 rgba[i][GCOMP] = lut[rgba[i][GCOMP] * 4 + 1];
1345 rgba[i][BCOMP] = lut[rgba[i][BCOMP] * 4 + 2];
1346 rgba[i][ACOMP] = lut[rgba[i][ACOMP] * 4 + 3];
1347 }
1348 }
1349 else {
1350 for (i = 0; i < n; i++) {
1351 GLint jR = IROUND((GLfloat) rgba[i][RCOMP] * scale);
1352 GLint jG = IROUND((GLfloat) rgba[i][GCOMP] * scale);
1353 GLint jB = IROUND((GLfloat) rgba[i][BCOMP] * scale);
1354 GLint jA = IROUND((GLfloat) rgba[i][ACOMP] * scale);
1355 CLAMPED_FLOAT_TO_CHAN(rgba[i][RCOMP], lut[jR * 4 + 0]);
1356 CLAMPED_FLOAT_TO_CHAN(rgba[i][GCOMP], lut[jG * 4 + 1]);
1357 CLAMPED_FLOAT_TO_CHAN(rgba[i][BCOMP], lut[jB * 4 + 2]);
1358 CLAMPED_FLOAT_TO_CHAN(rgba[i][ACOMP], lut[jA * 4 + 3]);
1359 }
1360 }
1361 break;
1362 default:
1363 _mesa_problem(NULL, "Bad format in _mesa_lookup_rgba_chan");
1364 return;
1365 }
1366 }
1367
1368
1369
1370 /*
1371 * Map color indexes to float rgba values.
1372 */
1373 void
1374 _mesa_map_ci_to_rgba( const GLcontext *ctx, GLuint n,
1375 const GLuint index[], GLfloat rgba[][4] )
1376 {
1377 GLuint rmask = ctx->PixelMaps.ItoR.Size - 1;
1378 GLuint gmask = ctx->PixelMaps.ItoG.Size - 1;
1379 GLuint bmask = ctx->PixelMaps.ItoB.Size - 1;
1380 GLuint amask = ctx->PixelMaps.ItoA.Size - 1;
1381 const GLfloat *rMap = ctx->PixelMaps.ItoR.Map;
1382 const GLfloat *gMap = ctx->PixelMaps.ItoG.Map;
1383 const GLfloat *bMap = ctx->PixelMaps.ItoB.Map;
1384 const GLfloat *aMap = ctx->PixelMaps.ItoA.Map;
1385 GLuint i;
1386 for (i=0;i<n;i++) {
1387 rgba[i][RCOMP] = rMap[index[i] & rmask];
1388 rgba[i][GCOMP] = gMap[index[i] & gmask];
1389 rgba[i][BCOMP] = bMap[index[i] & bmask];
1390 rgba[i][ACOMP] = aMap[index[i] & amask];
1391 }
1392 }
1393
1394
1395 /**
1396 * Map ubyte color indexes to ubyte/RGBA values.
1397 */
1398 void
1399 _mesa_map_ci8_to_rgba8(const GLcontext *ctx, GLuint n, const GLubyte index[],
1400 GLubyte rgba[][4])
1401 {
1402 GLuint rmask = ctx->PixelMaps.ItoR.Size - 1;
1403 GLuint gmask = ctx->PixelMaps.ItoG.Size - 1;
1404 GLuint bmask = ctx->PixelMaps.ItoB.Size - 1;
1405 GLuint amask = ctx->PixelMaps.ItoA.Size - 1;
1406 const GLubyte *rMap = ctx->PixelMaps.ItoR.Map8;
1407 const GLubyte *gMap = ctx->PixelMaps.ItoG.Map8;
1408 const GLubyte *bMap = ctx->PixelMaps.ItoB.Map8;
1409 const GLubyte *aMap = ctx->PixelMaps.ItoA.Map8;
1410 GLuint i;
1411 for (i=0;i<n;i++) {
1412 rgba[i][RCOMP] = rMap[index[i] & rmask];
1413 rgba[i][GCOMP] = gMap[index[i] & gmask];
1414 rgba[i][BCOMP] = bMap[index[i] & bmask];
1415 rgba[i][ACOMP] = aMap[index[i] & amask];
1416 }
1417 }
1418
1419
1420 void
1421 _mesa_scale_and_bias_depth(const GLcontext *ctx, GLuint n,
1422 GLfloat depthValues[])
1423 {
1424 const GLfloat scale = ctx->Pixel.DepthScale;
1425 const GLfloat bias = ctx->Pixel.DepthBias;
1426 GLuint i;
1427 for (i = 0; i < n; i++) {
1428 GLfloat d = depthValues[i] * scale + bias;
1429 depthValues[i] = CLAMP(d, 0.0F, 1.0F);
1430 }
1431 }
1432
1433
1434 void
1435 _mesa_scale_and_bias_depth_uint(const GLcontext *ctx, GLuint n,
1436 GLuint depthValues[])
1437 {
1438 const GLdouble max = (double) 0xffffffff;
1439 const GLdouble scale = ctx->Pixel.DepthScale;
1440 const GLdouble bias = ctx->Pixel.DepthBias * max;
1441 GLuint i;
1442 for (i = 0; i < n; i++) {
1443 GLdouble d = (GLdouble) depthValues[i] * scale + bias;
1444 d = CLAMP(d, 0.0, max);
1445 depthValues[i] = (GLuint) d;
1446 }
1447 }
1448
1449
1450
1451 /*
1452 * Update the min/max values from an array of fragment colors.
1453 */
1454 static void
1455 update_minmax(GLcontext *ctx, GLuint n, const GLfloat rgba[][4])
1456 {
1457 GLuint i;
1458 for (i = 0; i < n; i++) {
1459 /* update mins */
1460 if (rgba[i][RCOMP] < ctx->MinMax.Min[RCOMP])
1461 ctx->MinMax.Min[RCOMP] = rgba[i][RCOMP];
1462 if (rgba[i][GCOMP] < ctx->MinMax.Min[GCOMP])
1463 ctx->MinMax.Min[GCOMP] = rgba[i][GCOMP];
1464 if (rgba[i][BCOMP] < ctx->MinMax.Min[BCOMP])
1465 ctx->MinMax.Min[BCOMP] = rgba[i][BCOMP];
1466 if (rgba[i][ACOMP] < ctx->MinMax.Min[ACOMP])
1467 ctx->MinMax.Min[ACOMP] = rgba[i][ACOMP];
1468
1469 /* update maxs */
1470 if (rgba[i][RCOMP] > ctx->MinMax.Max[RCOMP])
1471 ctx->MinMax.Max[RCOMP] = rgba[i][RCOMP];
1472 if (rgba[i][GCOMP] > ctx->MinMax.Max[GCOMP])
1473 ctx->MinMax.Max[GCOMP] = rgba[i][GCOMP];
1474 if (rgba[i][BCOMP] > ctx->MinMax.Max[BCOMP])
1475 ctx->MinMax.Max[BCOMP] = rgba[i][BCOMP];
1476 if (rgba[i][ACOMP] > ctx->MinMax.Max[ACOMP])
1477 ctx->MinMax.Max[ACOMP] = rgba[i][ACOMP];
1478 }
1479 }
1480
1481
1482 /*
1483 * Update the histogram values from an array of fragment colors.
1484 */
1485 static void
1486 update_histogram(GLcontext *ctx, GLuint n, const GLfloat rgba[][4])
1487 {
1488 const GLint max = ctx->Histogram.Width - 1;
1489 GLfloat w = (GLfloat) max;
1490 GLuint i;
1491
1492 if (ctx->Histogram.Width == 0)
1493 return;
1494
1495 for (i = 0; i < n; i++) {
1496 GLint ri = IROUND(rgba[i][RCOMP] * w);
1497 GLint gi = IROUND(rgba[i][GCOMP] * w);
1498 GLint bi = IROUND(rgba[i][BCOMP] * w);
1499 GLint ai = IROUND(rgba[i][ACOMP] * w);
1500 ri = CLAMP(ri, 0, max);
1501 gi = CLAMP(gi, 0, max);
1502 bi = CLAMP(bi, 0, max);
1503 ai = CLAMP(ai, 0, max);
1504 ctx->Histogram.Count[ri][RCOMP]++;
1505 ctx->Histogram.Count[gi][GCOMP]++;
1506 ctx->Histogram.Count[bi][BCOMP]++;
1507 ctx->Histogram.Count[ai][ACOMP]++;
1508 }
1509 }
1510
1511
1512 /**
1513 * Apply various pixel transfer operations to an array of RGBA pixels
1514 * as indicated by the transferOps bitmask
1515 */
1516 void
1517 _mesa_apply_rgba_transfer_ops(GLcontext *ctx, GLbitfield transferOps,
1518 GLuint n, GLfloat rgba[][4])
1519 {
1520 /* scale & bias */
1521 if (transferOps & IMAGE_SCALE_BIAS_BIT) {
1522 _mesa_scale_and_bias_rgba(n, rgba,
1523 ctx->Pixel.RedScale, ctx->Pixel.GreenScale,
1524 ctx->Pixel.BlueScale, ctx->Pixel.AlphaScale,
1525 ctx->Pixel.RedBias, ctx->Pixel.GreenBias,
1526 ctx->Pixel.BlueBias, ctx->Pixel.AlphaBias);
1527 }
1528 /* color map lookup */
1529 if (transferOps & IMAGE_MAP_COLOR_BIT) {
1530 _mesa_map_rgba( ctx, n, rgba );
1531 }
1532 /* GL_COLOR_TABLE lookup */
1533 if (transferOps & IMAGE_COLOR_TABLE_BIT) {
1534 _mesa_lookup_rgba_float(&ctx->ColorTable[COLORTABLE_PRECONVOLUTION], n, rgba);
1535 }
1536 /* convolution */
1537 if (transferOps & IMAGE_CONVOLUTION_BIT) {
1538 /* this has to be done in the calling code */
1539 _mesa_problem(ctx, "IMAGE_CONVOLUTION_BIT set in _mesa_apply_transfer_ops");
1540 }
1541 /* GL_POST_CONVOLUTION_RED/GREEN/BLUE/ALPHA_SCALE/BIAS */
1542 if (transferOps & IMAGE_POST_CONVOLUTION_SCALE_BIAS) {
1543 _mesa_scale_and_bias_rgba(n, rgba,
1544 ctx->Pixel.PostConvolutionScale[RCOMP],
1545 ctx->Pixel.PostConvolutionScale[GCOMP],
1546 ctx->Pixel.PostConvolutionScale[BCOMP],
1547 ctx->Pixel.PostConvolutionScale[ACOMP],
1548 ctx->Pixel.PostConvolutionBias[RCOMP],
1549 ctx->Pixel.PostConvolutionBias[GCOMP],
1550 ctx->Pixel.PostConvolutionBias[BCOMP],
1551 ctx->Pixel.PostConvolutionBias[ACOMP]);
1552 }
1553 /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
1554 if (transferOps & IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT) {
1555 _mesa_lookup_rgba_float(&ctx->ColorTable[COLORTABLE_POSTCONVOLUTION], n, rgba);
1556 }
1557 /* color matrix transform */
1558 if (transferOps & IMAGE_COLOR_MATRIX_BIT) {
1559 _mesa_transform_rgba(ctx, n, rgba);
1560 }
1561 /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
1562 if (transferOps & IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT) {
1563 _mesa_lookup_rgba_float(&ctx->ColorTable[COLORTABLE_POSTCOLORMATRIX], n, rgba);
1564 }
1565 /* update histogram count */
1566 if (transferOps & IMAGE_HISTOGRAM_BIT) {
1567 update_histogram(ctx, n, (CONST GLfloat (*)[4]) rgba);
1568 }
1569 /* update min/max values */
1570 if (transferOps & IMAGE_MIN_MAX_BIT) {
1571 update_minmax(ctx, n, (CONST GLfloat (*)[4]) rgba);
1572 }
1573 /* clamping to [0,1] */
1574 if (transferOps & IMAGE_CLAMP_BIT) {
1575 GLuint i;
1576 for (i = 0; i < n; i++) {
1577 rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F);
1578 rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F);
1579 rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F);
1580 rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F);
1581 }
1582 }
1583 }
1584
1585
1586 /*
1587 * Apply color index shift and offset to an array of pixels.
1588 */
1589 static void
1590 shift_and_offset_ci( const GLcontext *ctx, GLuint n, GLuint indexes[] )
1591 {
1592 GLint shift = ctx->Pixel.IndexShift;
1593 GLint offset = ctx->Pixel.IndexOffset;
1594 GLuint i;
1595 if (shift > 0) {
1596 for (i=0;i<n;i++) {
1597 indexes[i] = (indexes[i] << shift) + offset;
1598 }
1599 }
1600 else if (shift < 0) {
1601 shift = -shift;
1602 for (i=0;i<n;i++) {
1603 indexes[i] = (indexes[i] >> shift) + offset;
1604 }
1605 }
1606 else {
1607 for (i=0;i<n;i++) {
1608 indexes[i] = indexes[i] + offset;
1609 }
1610 }
1611 }
1612
1613
1614
1615 /**
1616 * Apply color index shift, offset and table lookup to an array
1617 * of color indexes;
1618 */
1619 void
1620 _mesa_apply_ci_transfer_ops(const GLcontext *ctx, GLbitfield transferOps,
1621 GLuint n, GLuint indexes[])
1622 {
1623 if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
1624 shift_and_offset_ci(ctx, n, indexes);
1625 }
1626 if (transferOps & IMAGE_MAP_COLOR_BIT) {
1627 const GLuint mask = ctx->PixelMaps.ItoI.Size - 1;
1628 GLuint i;
1629 for (i = 0; i < n; i++) {
1630 const GLuint j = indexes[i] & mask;
1631 indexes[i] = IROUND(ctx->PixelMaps.ItoI.Map[j]);
1632 }
1633 }
1634 }
1635
1636
1637 /**
1638 * Apply stencil index shift, offset and table lookup to an array
1639 * of stencil values.
1640 */
1641 void
1642 _mesa_apply_stencil_transfer_ops(const GLcontext *ctx, GLuint n,
1643 GLstencil stencil[])
1644 {
1645 if (ctx->Pixel.IndexShift != 0 || ctx->Pixel.IndexOffset != 0) {
1646 const GLint offset = ctx->Pixel.IndexOffset;
1647 GLint shift = ctx->Pixel.IndexShift;
1648 GLuint i;
1649 if (shift > 0) {
1650 for (i = 0; i < n; i++) {
1651 stencil[i] = (stencil[i] << shift) + offset;
1652 }
1653 }
1654 else if (shift < 0) {
1655 shift = -shift;
1656 for (i = 0; i < n; i++) {
1657 stencil[i] = (stencil[i] >> shift) + offset;
1658 }
1659 }
1660 else {
1661 for (i = 0; i < n; i++) {
1662 stencil[i] = stencil[i] + offset;
1663 }
1664 }
1665 }
1666 if (ctx->Pixel.MapStencilFlag) {
1667 GLuint mask = ctx->PixelMaps.StoS.Size - 1;
1668 GLuint i;
1669 for (i = 0; i < n; i++) {
1670 stencil[i] = (GLstencil)ctx->PixelMaps.StoS.Map[ stencil[i] & mask ];
1671 }
1672 }
1673 }
1674
1675
1676 /**
1677 * Used to pack an array [][4] of RGBA float colors as specified
1678 * by the dstFormat, dstType and dstPacking. Used by glReadPixels,
1679 * glGetConvolutionFilter(), etc.
1680 * Note: the rgba values will be modified by this function when any pixel
1681 * transfer ops are enabled.
1682 */
1683 void
1684 _mesa_pack_rgba_span_float(GLcontext *ctx, GLuint n, GLfloat rgba[][4],
1685 GLenum dstFormat, GLenum dstType,
1686 GLvoid *dstAddr,
1687 const struct gl_pixelstore_attrib *dstPacking,
1688 GLbitfield transferOps)
1689 {
1690 GLfloat luminance[MAX_WIDTH];
1691 const GLint comps = _mesa_components_in_format(dstFormat);
1692 GLuint i;
1693
1694 /* XXX
1695 * This test should probably go away. Have the caller set/clear the
1696 * IMAGE_CLAMP_BIT as needed.
1697 */
1698 if (dstType != GL_FLOAT || ctx->Color.ClampReadColor == GL_TRUE) {
1699 /* need to clamp to [0, 1] */
1700 transferOps |= IMAGE_CLAMP_BIT;
1701 }
1702
1703 if (transferOps) {
1704 _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
1705 if ((transferOps & IMAGE_MIN_MAX_BIT) && ctx->MinMax.Sink) {
1706 return;
1707 }
1708 }
1709
1710 if (dstFormat == GL_LUMINANCE || dstFormat == GL_LUMINANCE_ALPHA) {
1711 /* compute luminance values */
1712 if (transferOps & IMAGE_CLAMP_BIT) {
1713 for (i = 0; i < n; i++) {
1714 GLfloat sum = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
1715 luminance[i] = CLAMP(sum, 0.0F, 1.0F);
1716 }
1717 }
1718 else {
1719 for (i = 0; i < n; i++) {
1720 luminance[i] = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
1721 }
1722 }
1723 }
1724
1725 /*
1726 * Pack/store the pixels. Ugh! Lots of cases!!!
1727 */
1728 switch (dstType) {
1729 case GL_UNSIGNED_BYTE:
1730 {
1731 GLubyte *dst = (GLubyte *) dstAddr;
1732 switch (dstFormat) {
1733 case GL_RED:
1734 for (i=0;i<n;i++)
1735 dst[i] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1736 break;
1737 case GL_GREEN:
1738 for (i=0;i<n;i++)
1739 dst[i] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1740 break;
1741 case GL_BLUE:
1742 for (i=0;i<n;i++)
1743 dst[i] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1744 break;
1745 case GL_ALPHA:
1746 for (i=0;i<n;i++)
1747 dst[i] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1748 break;
1749 case GL_LUMINANCE:
1750 for (i=0;i<n;i++)
1751 dst[i] = FLOAT_TO_UBYTE(luminance[i]);
1752 break;
1753 case GL_LUMINANCE_ALPHA:
1754 for (i=0;i<n;i++) {
1755 dst[i*2+0] = FLOAT_TO_UBYTE(luminance[i]);
1756 dst[i*2+1] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1757 }
1758 break;
1759 case GL_RGB:
1760 for (i=0;i<n;i++) {
1761 dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1762 dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1763 dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1764 }
1765 break;
1766 case GL_RGBA:
1767 for (i=0;i<n;i++) {
1768 dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1769 dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1770 dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1771 dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1772 }
1773 break;
1774 case GL_BGR:
1775 for (i=0;i<n;i++) {
1776 dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1777 dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1778 dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1779 }
1780 break;
1781 case GL_BGRA:
1782 for (i=0;i<n;i++) {
1783 dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1784 dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1785 dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1786 dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1787 }
1788 break;
1789 case GL_ABGR_EXT:
1790 for (i=0;i<n;i++) {
1791 dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1792 dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1793 dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1794 dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1795 }
1796 break;
1797 case GL_DUDV_ATI:
1798 case GL_DU8DV8_ATI:
1799 for (i=0;i<n;i++) {
1800 dst[i*2+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1801 dst[i*2+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1802 }
1803 break;
1804 default:
1805 _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1806 }
1807 }
1808 break;
1809 case GL_BYTE:
1810 {
1811 GLbyte *dst = (GLbyte *) dstAddr;
1812 switch (dstFormat) {
1813 case GL_RED:
1814 for (i=0;i<n;i++)
1815 dst[i] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1816 break;
1817 case GL_GREEN:
1818 for (i=0;i<n;i++)
1819 dst[i] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1820 break;
1821 case GL_BLUE:
1822 for (i=0;i<n;i++)
1823 dst[i] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1824 break;
1825 case GL_ALPHA:
1826 for (i=0;i<n;i++)
1827 dst[i] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1828 break;
1829 case GL_LUMINANCE:
1830 for (i=0;i<n;i++)
1831 dst[i] = FLOAT_TO_BYTE(luminance[i]);
1832 break;
1833 case GL_LUMINANCE_ALPHA:
1834 for (i=0;i<n;i++) {
1835 dst[i*2+0] = FLOAT_TO_BYTE(luminance[i]);
1836 dst[i*2+1] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1837 }
1838 break;
1839 case GL_RGB:
1840 for (i=0;i<n;i++) {
1841 dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1842 dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1843 dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1844 }
1845 break;
1846 case GL_RGBA:
1847 for (i=0;i<n;i++) {
1848 dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1849 dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1850 dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1851 dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1852 }
1853 break;
1854 case GL_BGR:
1855 for (i=0;i<n;i++) {
1856 dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1857 dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1858 dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1859 }
1860 break;
1861 case GL_BGRA:
1862 for (i=0;i<n;i++) {
1863 dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1864 dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1865 dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1866 dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1867 }
1868 break;
1869 case GL_ABGR_EXT:
1870 for (i=0;i<n;i++) {
1871 dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1872 dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1873 dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1874 dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1875 }
1876 break;
1877 case GL_DUDV_ATI:
1878 case GL_DU8DV8_ATI:
1879 for (i=0;i<n;i++) {
1880 dst[i*2+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1881 dst[i*2+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1882 }
1883 break;
1884 default:
1885 _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1886 }
1887 }
1888 break;
1889 case GL_UNSIGNED_SHORT:
1890 {
1891 GLushort *dst = (GLushort *) dstAddr;
1892 switch (dstFormat) {
1893 case GL_RED:
1894 for (i=0;i<n;i++)
1895 CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][RCOMP]);
1896 break;
1897 case GL_GREEN:
1898 for (i=0;i<n;i++)
1899 CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][GCOMP]);
1900 break;
1901 case GL_BLUE:
1902 for (i=0;i<n;i++)
1903 CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][BCOMP]);
1904 break;
1905 case GL_ALPHA:
1906 for (i=0;i<n;i++)
1907 CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][ACOMP]);
1908 break;
1909 case GL_LUMINANCE:
1910 for (i=0;i<n;i++)
1911 UNCLAMPED_FLOAT_TO_USHORT(dst[i], luminance[i]);
1912 break;
1913 case GL_LUMINANCE_ALPHA:
1914 for (i=0;i<n;i++) {
1915 UNCLAMPED_FLOAT_TO_USHORT(dst[i*2+0], luminance[i]);
1916 CLAMPED_FLOAT_TO_USHORT(dst[i*2+1], rgba[i][ACOMP]);
1917 }
1918 break;
1919 case GL_RGB:
1920 for (i=0;i<n;i++) {
1921 CLAMPED_FLOAT_TO_USHORT(dst[i*3+0], rgba[i][RCOMP]);
1922 CLAMPED_FLOAT_TO_USHORT(dst[i*3+1], rgba[i][GCOMP]);
1923 CLAMPED_FLOAT_TO_USHORT(dst[i*3+2], rgba[i][BCOMP]);
1924 }
1925 break;
1926 case GL_RGBA:
1927 for (i=0;i<n;i++) {
1928 CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][RCOMP]);
1929 CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][GCOMP]);
1930 CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][BCOMP]);
1931 CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][ACOMP]);
1932 }
1933 break;
1934 case GL_BGR:
1935 for (i=0;i<n;i++) {
1936 CLAMPED_FLOAT_TO_USHORT(dst[i*3+0], rgba[i][BCOMP]);
1937 CLAMPED_FLOAT_TO_USHORT(dst[i*3+1], rgba[i][GCOMP]);
1938 CLAMPED_FLOAT_TO_USHORT(dst[i*3+2], rgba[i][RCOMP]);
1939 }
1940 break;
1941 case GL_BGRA:
1942 for (i=0;i<n;i++) {
1943 CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][BCOMP]);
1944 CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][GCOMP]);
1945 CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][RCOMP]);
1946 CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][ACOMP]);
1947 }
1948 break;
1949 case GL_ABGR_EXT:
1950 for (i=0;i<n;i++) {
1951 CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][ACOMP]);
1952 CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][BCOMP]);
1953 CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][GCOMP]);
1954 CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][RCOMP]);
1955 }
1956 break;
1957 case GL_DUDV_ATI:
1958 case GL_DU8DV8_ATI:
1959 for (i=0;i<n;i++) {
1960 dst[i*2+0] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
1961 dst[i*2+1] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
1962 }
1963 break;
1964 default:
1965 _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1966 }
1967 }
1968 break;
1969 case GL_SHORT:
1970 {
1971 GLshort *dst = (GLshort *) dstAddr;
1972 switch (dstFormat) {
1973 case GL_RED:
1974 for (i=0;i<n;i++)
1975 dst[i] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1976 break;
1977 case GL_GREEN:
1978 for (i=0;i<n;i++)
1979 dst[i] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1980 break;
1981 case GL_BLUE:
1982 for (i=0;i<n;i++)
1983 dst[i] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1984 break;
1985 case GL_ALPHA:
1986 for (i=0;i<n;i++)
1987 dst[i] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1988 break;
1989 case GL_LUMINANCE:
1990 for (i=0;i<n;i++)
1991 dst[i] = FLOAT_TO_SHORT(luminance[i]);
1992 break;
1993 case GL_LUMINANCE_ALPHA:
1994 for (i=0;i<n;i++) {
1995 dst[i*2+0] = FLOAT_TO_SHORT(luminance[i]);
1996 dst[i*2+1] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1997 }
1998 break;
1999 case GL_RGB:
2000 for (i=0;i<n;i++) {
2001 dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
2002 dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
2003 dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
2004 }
2005 break;
2006 case GL_RGBA:
2007 for (i=0;i<n;i++) {
2008 dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
2009 dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
2010 dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
2011 dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
2012 }
2013 break;
2014 case GL_BGR:
2015 for (i=0;i<n;i++) {
2016 dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
2017 dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
2018 dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
2019 }
2020 break;
2021 case GL_BGRA:
2022 for (i=0;i<n;i++) {
2023 dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
2024 dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
2025 dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
2026 dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
2027 }
2028 break;
2029 case GL_ABGR_EXT:
2030 for (i=0;i<n;i++) {
2031 dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
2032 dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
2033 dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
2034 dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
2035 }
2036 break;
2037 case GL_DUDV_ATI:
2038 case GL_DU8DV8_ATI:
2039 for (i=0;i<n;i++) {
2040 dst[i*2+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
2041 dst[i*2+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
2042 }
2043 break;
2044 default:
2045 _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2046 }
2047 }
2048 break;
2049 case GL_UNSIGNED_INT:
2050 {
2051 GLuint *dst = (GLuint *) dstAddr;
2052 switch (dstFormat) {
2053 case GL_RED:
2054 for (i=0;i<n;i++)
2055 dst[i] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2056 break;
2057 case GL_GREEN:
2058 for (i=0;i<n;i++)
2059 dst[i] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2060 break;
2061 case GL_BLUE:
2062 for (i=0;i<n;i++)
2063 dst[i] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2064 break;
2065 case GL_ALPHA:
2066 for (i=0;i<n;i++)
2067 dst[i] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2068 break;
2069 case GL_LUMINANCE:
2070 for (i=0;i<n;i++)
2071 dst[i] = FLOAT_TO_UINT(luminance[i]);
2072 break;
2073 case GL_LUMINANCE_ALPHA:
2074 for (i=0;i<n;i++) {
2075 dst[i*2+0] = FLOAT_TO_UINT(luminance[i]);
2076 dst[i*2+1] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2077 }
2078 break;
2079 case GL_RGB:
2080 for (i=0;i<n;i++) {
2081 dst[i*3+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2082 dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2083 dst[i*3+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2084 }
2085 break;
2086 case GL_RGBA:
2087 for (i=0;i<n;i++) {
2088 dst[i*4+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2089 dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2090 dst[i*4+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2091 dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2092 }
2093 break;
2094 case GL_BGR:
2095 for (i=0;i<n;i++) {
2096 dst[i*3+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2097 dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2098 dst[i*3+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2099 }
2100 break;
2101 case GL_BGRA:
2102 for (i=0;i<n;i++) {
2103 dst[i*4+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2104 dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2105 dst[i*4+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2106 dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2107 }
2108 break;
2109 case GL_ABGR_EXT:
2110 for (i=0;i<n;i++) {
2111 dst[i*4+0] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2112 dst[i*4+1] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2113 dst[i*4+2] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2114 dst[i*4+3] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2115 }
2116 break;
2117 case GL_DUDV_ATI:
2118 case GL_DU8DV8_ATI:
2119 for (i=0;i<n;i++) {
2120 dst[i*2+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2121 dst[i*2+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2122 }
2123 break;
2124 default:
2125 _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2126 }
2127 }
2128 break;
2129 case GL_INT:
2130 {
2131 GLint *dst = (GLint *) dstAddr;
2132 switch (dstFormat) {
2133 case GL_RED:
2134 for (i=0;i<n;i++)
2135 dst[i] = FLOAT_TO_INT(rgba[i][RCOMP]);
2136 break;
2137 case GL_GREEN:
2138 for (i=0;i<n;i++)
2139 dst[i] = FLOAT_TO_INT(rgba[i][GCOMP]);
2140 break;
2141 case GL_BLUE:
2142 for (i=0;i<n;i++)
2143 dst[i] = FLOAT_TO_INT(rgba[i][BCOMP]);
2144 break;
2145 case GL_ALPHA:
2146 for (i=0;i<n;i++)
2147 dst[i] = FLOAT_TO_INT(rgba[i][ACOMP]);
2148 break;
2149 case GL_LUMINANCE:
2150 for (i=0;i<n;i++)
2151 dst[i] = FLOAT_TO_INT(luminance[i]);
2152 break;
2153 case GL_LUMINANCE_ALPHA:
2154 for (i=0;i<n;i++) {
2155 dst[i*2+0] = FLOAT_TO_INT(luminance[i]);
2156 dst[i*2+1] = FLOAT_TO_INT(rgba[i][ACOMP]);
2157 }
2158 break;
2159 case GL_RGB:
2160 for (i=0;i<n;i++) {
2161 dst[i*3+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
2162 dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2163 dst[i*3+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
2164 }
2165 break;
2166 case GL_RGBA:
2167 for (i=0;i<n;i++) {
2168 dst[i*4+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
2169 dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2170 dst[i*4+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
2171 dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
2172 }
2173 break;
2174 case GL_BGR:
2175 for (i=0;i<n;i++) {
2176 dst[i*3+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
2177 dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2178 dst[i*3+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
2179 }
2180 break;
2181 case GL_BGRA:
2182 for (i=0;i<n;i++) {
2183 dst[i*4+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
2184 dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2185 dst[i*4+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
2186 dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
2187 }
2188 break;
2189 case GL_ABGR_EXT:
2190 for (i=0;i<n;i++) {
2191 dst[i*4+0] = FLOAT_TO_INT(rgba[i][ACOMP]);
2192 dst[i*4+1] = FLOAT_TO_INT(rgba[i][BCOMP]);
2193 dst[i*4+2] = FLOAT_TO_INT(rgba[i][GCOMP]);
2194 dst[i*4+3] = FLOAT_TO_INT(rgba[i][RCOMP]);
2195 }
2196 break;
2197 case GL_DUDV_ATI:
2198 case GL_DU8DV8_ATI:
2199 for (i=0;i<n;i++) {
2200 dst[i*2+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
2201 dst[i*2+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2202 }
2203 break;
2204 default:
2205 _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2206 }
2207 }
2208 break;
2209 case GL_FLOAT:
2210 {
2211 GLfloat *dst = (GLfloat *) dstAddr;
2212 switch (dstFormat) {
2213 case GL_RED:
2214 for (i=0;i<n;i++)
2215 dst[i] = rgba[i][RCOMP];
2216 break;
2217 case GL_GREEN:
2218 for (i=0;i<n;i++)
2219 dst[i] = rgba[i][GCOMP];
2220 break;
2221 case GL_BLUE:
2222 for (i=0;i<n;i++)
2223 dst[i] = rgba[i][BCOMP];
2224 break;
2225 case GL_ALPHA:
2226 for (i=0;i<n;i++)
2227 dst[i] = rgba[i][ACOMP];
2228 break;
2229 case GL_LUMINANCE:
2230 for (i=0;i<n;i++)
2231 dst[i] = luminance[i];
2232 break;
2233 case GL_LUMINANCE_ALPHA:
2234 for (i=0;i<n;i++) {
2235 dst[i*2+0] = luminance[i];
2236 dst[i*2+1] = rgba[i][ACOMP];
2237 }
2238 break;
2239 case GL_RGB:
2240 for (i=0;i<n;i++) {
2241 dst[i*3+0] = rgba[i][RCOMP];
2242 dst[i*3+1] = rgba[i][GCOMP];
2243 dst[i*3+2] = rgba[i][BCOMP];
2244 }
2245 break;
2246 case GL_RGBA:
2247 for (i=0;i<n;i++) {
2248 dst[i*4+0] = rgba[i][RCOMP];
2249 dst[i*4+1] = rgba[i][GCOMP];
2250 dst[i*4+2] = rgba[i][BCOMP];
2251 dst[i*4+3] = rgba[i][ACOMP];
2252 }
2253 break;
2254 case GL_BGR:
2255 for (i=0;i<n;i++) {
2256 dst[i*3+0] = rgba[i][BCOMP];
2257 dst[i*3+1] = rgba[i][GCOMP];
2258 dst[i*3+2] = rgba[i][RCOMP];
2259 }
2260 break;
2261 case GL_BGRA:
2262 for (i=0;i<n;i++) {
2263 dst[i*4+0] = rgba[i][BCOMP];
2264 dst[i*4+1] = rgba[i][GCOMP];
2265 dst[i*4+2] = rgba[i][RCOMP];
2266 dst[i*4+3] = rgba[i][ACOMP];
2267 }
2268 break;
2269 case GL_ABGR_EXT:
2270 for (i=0;i<n;i++) {
2271 dst[i*4+0] = rgba[i][ACOMP];
2272 dst[i*4+1] = rgba[i][BCOMP];
2273 dst[i*4+2] = rgba[i][GCOMP];
2274 dst[i*4+3] = rgba[i][RCOMP];
2275 }
2276 break;
2277 case GL_DUDV_ATI:
2278 case GL_DU8DV8_ATI:
2279 for (i=0;i<n;i++) {
2280 dst[i*2+0] = rgba[i][RCOMP];
2281 dst[i*2+1] = rgba[i][GCOMP];
2282 }
2283 break;
2284 default:
2285 _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2286 }
2287 }
2288 break;
2289 case GL_HALF_FLOAT_ARB:
2290 {
2291 GLhalfARB *dst = (GLhalfARB *) dstAddr;
2292 switch (dstFormat) {
2293 case GL_RED:
2294 for (i=0;i<n;i++)
2295 dst[i] = _mesa_float_to_half(rgba[i][RCOMP]);
2296 break;
2297 case GL_GREEN:
2298 for (i=0;i<n;i++)
2299 dst[i] = _mesa_float_to_half(rgba[i][GCOMP]);
2300 break;
2301 case GL_BLUE:
2302 for (i=0;i<n;i++)
2303 dst[i] = _mesa_float_to_half(rgba[i][BCOMP]);
2304 break;
2305 case GL_ALPHA:
2306 for (i=0;i<n;i++)
2307 dst[i] = _mesa_float_to_half(rgba[i][ACOMP]);
2308 break;
2309 case GL_LUMINANCE:
2310 for (i=0;i<n;i++)
2311 dst[i] = _mesa_float_to_half(luminance[i]);
2312 break;
2313 case GL_LUMINANCE_ALPHA:
2314 for (i=0;i<n;i++) {
2315 dst[i*2+0] = _mesa_float_to_half(luminance[i]);
2316 dst[i*2+1] = _mesa_float_to_half(rgba[i][ACOMP]);
2317 }
2318 break;
2319 case GL_RGB:
2320 for (i=0;i<n;i++) {
2321 dst[i*3+0] = _mesa_float_to_half(rgba[i][RCOMP]);
2322 dst[i*3+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2323 dst[i*3+2] = _mesa_float_to_half(rgba[i][BCOMP]);
2324 }
2325 break;
2326 case GL_RGBA:
2327 for (i=0;i<n;i++) {
2328 dst[i*4+0] = _mesa_float_to_half(rgba[i][RCOMP]);
2329 dst[i*4+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2330 dst[i*4+2] = _mesa_float_to_half(rgba[i][BCOMP]);
2331 dst[i*4+3] = _mesa_float_to_half(rgba[i][ACOMP]);
2332 }
2333 break;
2334 case GL_BGR:
2335 for (i=0;i<n;i++) {
2336 dst[i*3+0] = _mesa_float_to_half(rgba[i][BCOMP]);
2337 dst[i*3+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2338 dst[i*3+2] = _mesa_float_to_half(rgba[i][RCOMP]);
2339 }
2340 break;
2341 case GL_BGRA:
2342 for (i=0;i<n;i++) {
2343 dst[i*4+0] = _mesa_float_to_half(rgba[i][BCOMP]);
2344 dst[i*4+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2345 dst[i*4+2] = _mesa_float_to_half(rgba[i][RCOMP]);
2346 dst[i*4+3] = _mesa_float_to_half(rgba[i][ACOMP]);
2347 }
2348 break;
2349 case GL_ABGR_EXT:
2350 for (i=0;i<n;i++) {
2351 dst[i*4+0] = _mesa_float_to_half(rgba[i][ACOMP]);
2352 dst[i*4+1] = _mesa_float_to_half(rgba[i][BCOMP]);
2353 dst[i*4+2] = _mesa_float_to_half(rgba[i][GCOMP]);
2354 dst[i*4+3] = _mesa_float_to_half(rgba[i][RCOMP]);
2355 }
2356 break;
2357 case GL_DUDV_ATI:
2358 case GL_DU8DV8_ATI:
2359 for (i=0;i<n;i++) {
2360 dst[i*2+0] = _mesa_float_to_half(rgba[i][RCOMP]);
2361 dst[i*2+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2362 }
2363 break;
2364 default:
2365 _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2366 }
2367 }
2368 break;
2369 case GL_UNSIGNED_BYTE_3_3_2:
2370 if (dstFormat == GL_RGB) {
2371 GLubyte *dst = (GLubyte *) dstAddr;
2372 for (i=0;i<n;i++) {
2373 dst[i] = (IROUND(rgba[i][RCOMP] * 7.0F) << 5)
2374 | (IROUND(rgba[i][GCOMP] * 7.0F) << 2)
2375 | (IROUND(rgba[i][BCOMP] * 3.0F) );
2376 }
2377 }
2378 break;
2379 case GL_UNSIGNED_BYTE_2_3_3_REV:
2380 if (dstFormat == GL_RGB) {
2381 GLubyte *dst = (GLubyte *) dstAddr;
2382 for (i=0;i<n;i++) {
2383 dst[i] = (IROUND(rgba[i][RCOMP] * 7.0F) )
2384 | (IROUND(rgba[i][GCOMP] * 7.0F) << 3)
2385 | (IROUND(rgba[i][BCOMP] * 3.0F) << 6);
2386 }
2387 }
2388 break;
2389 case GL_UNSIGNED_SHORT_5_6_5:
2390 if (dstFormat == GL_RGB) {
2391 GLushort *dst = (GLushort *) dstAddr;
2392 for (i=0;i<n;i++) {
2393 dst[i] = (IROUND(rgba[i][RCOMP] * 31.0F) << 11)
2394 | (IROUND(rgba[i][GCOMP] * 63.0F) << 5)
2395 | (IROUND(rgba[i][BCOMP] * 31.0F) );
2396 }
2397 }
2398 break;
2399 case GL_UNSIGNED_SHORT_5_6_5_REV:
2400 if (dstFormat == GL_RGB) {
2401 GLushort *dst = (GLushort *) dstAddr;
2402 for (i=0;i<n;i++) {
2403 dst[i] = (IROUND(rgba[i][RCOMP] * 31.0F) )
2404 | (IROUND(rgba[i][GCOMP] * 63.0F) << 5)
2405 | (IROUND(rgba[i][BCOMP] * 31.0F) << 11);
2406 }
2407 }
2408 break;
2409 case GL_UNSIGNED_SHORT_4_4_4_4:
2410 if (dstFormat == GL_RGBA) {
2411 GLushort *dst = (GLushort *) dstAddr;
2412 for (i=0;i<n;i++) {
2413 dst[i] = (IROUND(rgba[i][RCOMP] * 15.0F) << 12)
2414 | (IROUND(rgba[i][GCOMP] * 15.0F) << 8)
2415 | (IROUND(rgba[i][BCOMP] * 15.0F) << 4)
2416 | (IROUND(rgba[i][ACOMP] * 15.0F) );
2417 }
2418 }
2419 else if (dstFormat == GL_BGRA) {
2420 GLushort *dst = (GLushort *) dstAddr;
2421 for (i=0;i<n;i++) {
2422 dst[i] = (IROUND(rgba[i][BCOMP] * 15.0F) << 12)
2423 | (IROUND(rgba[i][GCOMP] * 15.0F) << 8)
2424 | (IROUND(rgba[i][RCOMP] * 15.0F) << 4)
2425 | (IROUND(rgba[i][ACOMP] * 15.0F) );
2426 }
2427 }
2428 else if (dstFormat == GL_ABGR_EXT) {
2429 GLushort *dst = (GLushort *) dstAddr;
2430 for (i=0;i<n;i++) {
2431 dst[i] = (IROUND(rgba[i][ACOMP] * 15.0F) << 12)
2432 | (IROUND(rgba[i][BCOMP] * 15.0F) << 8)
2433 | (IROUND(rgba[i][GCOMP] * 15.0F) << 4)
2434 | (IROUND(rgba[i][RCOMP] * 15.0F) );
2435 }
2436 }
2437 break;
2438 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
2439 if (dstFormat == GL_RGBA) {
2440 GLushort *dst = (GLushort *) dstAddr;
2441 for (i=0;i<n;i++) {
2442 dst[i] = (IROUND(rgba[i][RCOMP] * 15.0F) )
2443 | (IROUND(rgba[i][GCOMP] * 15.0F) << 4)
2444 | (IROUND(rgba[i][BCOMP] * 15.0F) << 8)
2445 | (IROUND(rgba[i][ACOMP] * 15.0F) << 12);
2446 }
2447 }
2448 else if (dstFormat == GL_BGRA) {
2449 GLushort *dst = (GLushort *) dstAddr;
2450 for (i=0;i<n;i++) {
2451 dst[i] = (IROUND(rgba[i][BCOMP] * 15.0F) )
2452 | (IROUND(rgba[i][GCOMP] * 15.0F) << 4)
2453 | (IROUND(rgba[i][RCOMP] * 15.0F) << 8)
2454 | (IROUND(rgba[i][ACOMP] * 15.0F) << 12);
2455 }
2456 }
2457 else if (dstFormat == GL_ABGR_EXT) {
2458 GLushort *dst = (GLushort *) dstAddr;
2459 for (i=0;i<n;i++) {
2460 dst[i] = (IROUND(rgba[i][ACOMP] * 15.0F) )
2461 | (IROUND(rgba[i][BCOMP] * 15.0F) << 4)
2462 | (IROUND(rgba[i][GCOMP] * 15.0F) << 8)
2463 | (IROUND(rgba[i][RCOMP] * 15.0F) << 12);
2464 }
2465 }
2466 break;
2467 case GL_UNSIGNED_SHORT_5_5_5_1:
2468 if (dstFormat == GL_RGBA) {
2469 GLushort *dst = (GLushort *) dstAddr;
2470 for (i=0;i<n;i++) {
2471 dst[i] = (IROUND(rgba[i][RCOMP] * 31.0F) << 11)
2472 | (IROUND(rgba[i][GCOMP] * 31.0F) << 6)
2473 | (IROUND(rgba[i][BCOMP] * 31.0F) << 1)
2474 | (IROUND(rgba[i][ACOMP] * 1.0F) );
2475 }
2476 }
2477 else if (dstFormat == GL_BGRA) {
2478 GLushort *dst = (GLushort *) dstAddr;
2479 for (i=0;i<n;i++) {
2480 dst[i] = (IROUND(rgba[i][BCOMP] * 31.0F) << 11)
2481 | (IROUND(rgba[i][GCOMP] * 31.0F) << 6)
2482 | (IROUND(rgba[i][RCOMP] * 31.0F) << 1)
2483 | (IROUND(rgba[i][ACOMP] * 1.0F) );
2484 }
2485 }
2486 else if (dstFormat == GL_ABGR_EXT) {
2487 GLushort *dst = (GLushort *) dstAddr;
2488 for (i=0;i<n;i++) {
2489 dst[i] = (IROUND(rgba[i][ACOMP] * 31.0F) << 11)
2490 | (IROUND(rgba[i][BCOMP] * 31.0F) << 6)
2491 | (IROUND(rgba[i][GCOMP] * 31.0F) << 1)
2492 | (IROUND(rgba[i][RCOMP] * 1.0F) );
2493 }
2494 }
2495 break;
2496 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
2497 if (dstFormat == GL_RGBA) {
2498 GLushort *dst = (GLushort *) dstAddr;
2499 for (i=0;i<n;i++) {
2500 dst[i] = (IROUND(rgba[i][RCOMP] * 31.0F) )
2501 | (IROUND(rgba[i][GCOMP] * 31.0F) << 5)
2502 | (IROUND(rgba[i][BCOMP] * 31.0F) << 10)
2503 | (IROUND(rgba[i][ACOMP] * 1.0F) << 15);
2504 }
2505 }
2506 else if (dstFormat == GL_BGRA) {
2507 GLushort *dst = (GLushort *) dstAddr;
2508 for (i=0;i<n;i++) {
2509 dst[i] = (IROUND(rgba[i][BCOMP] * 31.0F) )
2510 | (IROUND(rgba[i][GCOMP] * 31.0F) << 5)
2511 | (IROUND(rgba[i][RCOMP] * 31.0F) << 10)
2512 | (IROUND(rgba[i][ACOMP] * 1.0F) << 15);
2513 }
2514 }
2515 else if (dstFormat == GL_ABGR_EXT) {
2516 GLushort *dst = (GLushort *) dstAddr;
2517 for (i=0;i<n;i++) {
2518 dst[i] = (IROUND(rgba[i][ACOMP] * 31.0F) )
2519 | (IROUND(rgba[i][BCOMP] * 31.0F) << 5)
2520 | (IROUND(rgba[i][GCOMP] * 31.0F) << 10)
2521 | (IROUND(rgba[i][RCOMP] * 1.0F) << 15);
2522 }
2523 }
2524 break;
2525 case GL_UNSIGNED_INT_8_8_8_8:
2526 if (dstFormat == GL_RGBA) {
2527 GLuint *dst = (GLuint *) dstAddr;
2528 for (i=0;i<n;i++) {
2529 dst[i] = (IROUND(rgba[i][RCOMP] * 255.F) << 24)
2530 | (IROUND(rgba[i][GCOMP] * 255.F) << 16)
2531 | (IROUND(rgba[i][BCOMP] * 255.F) << 8)
2532 | (IROUND(rgba[i][ACOMP] * 255.F) );
2533 }
2534 }
2535 else if (dstFormat == GL_BGRA) {
2536 GLuint *dst = (GLuint *) dstAddr;
2537 for (i=0;i<n;i++) {
2538 dst[i] = (IROUND(rgba[i][BCOMP] * 255.F) << 24)
2539 | (IROUND(rgba[i][GCOMP] * 255.F) << 16)
2540 | (IROUND(rgba[i][RCOMP] * 255.F) << 8)
2541 | (IROUND(rgba[i][ACOMP] * 255.F) );
2542 }
2543 }
2544 else if (dstFormat == GL_ABGR_EXT) {
2545 GLuint *dst = (GLuint *) dstAddr;
2546 for (i=0;i<n;i++) {
2547 dst[i] = (IROUND(rgba[i][ACOMP] * 255.F) << 24)
2548 | (IROUND(rgba[i][BCOMP] * 255.F) << 16)
2549 | (IROUND(rgba[i][GCOMP] * 255.F) << 8)
2550 | (IROUND(rgba[i][RCOMP] * 255.F) );
2551 }
2552 }
2553 break;
2554 case GL_UNSIGNED_INT_8_8_8_8_REV:
2555 if (dstFormat == GL_RGBA) {
2556 GLuint *dst = (GLuint *) dstAddr;
2557 for (i=0;i<n;i++) {
2558 dst[i] = (IROUND(rgba[i][RCOMP] * 255.0F) )
2559 | (IROUND(rgba[i][GCOMP] * 255.0F) << 8)
2560 | (IROUND(rgba[i][BCOMP] * 255.0F) << 16)
2561 | (IROUND(rgba[i][ACOMP] * 255.0F) << 24);
2562 }
2563 }
2564 else if (dstFormat == GL_BGRA) {
2565 GLuint *dst = (GLuint *) dstAddr;
2566 for (i=0;i<n;i++) {
2567 dst[i] = (IROUND(rgba[i][BCOMP] * 255.0F) )
2568 | (IROUND(rgba[i][GCOMP] * 255.0F) << 8)
2569 | (IROUND(rgba[i][RCOMP] * 255.0F) << 16)
2570 | (IROUND(rgba[i][ACOMP] * 255.0F) << 24);
2571 }
2572 }
2573 else if (dstFormat == GL_ABGR_EXT) {
2574 GLuint *dst = (GLuint *) dstAddr;
2575 for (i=0;i<n;i++) {
2576 dst[i] = (IROUND(rgba[i][ACOMP] * 255.0F) )
2577 | (IROUND(rgba[i][BCOMP] * 255.0F) << 8)
2578 | (IROUND(rgba[i][GCOMP] * 255.0F) << 16)
2579 | (IROUND(rgba[i][RCOMP] * 255.0F) << 24);
2580 }
2581 }
2582 break;
2583 case GL_UNSIGNED_INT_10_10_10_2:
2584 if (dstFormat == GL_RGBA) {
2585 GLuint *dst = (GLuint *) dstAddr;
2586 for (i=0;i<n;i++) {
2587 dst[i] = (IROUND(rgba[i][RCOMP] * 1023.0F) << 22)
2588 | (IROUND(rgba[i][GCOMP] * 1023.0F) << 12)
2589 | (IROUND(rgba[i][BCOMP] * 1023.0F) << 2)
2590 | (IROUND(rgba[i][ACOMP] * 3.0F) );
2591 }
2592 }
2593 else if (dstFormat == GL_BGRA) {
2594 GLuint *dst = (GLuint *) dstAddr;
2595 for (i=0;i<n;i++) {
2596 dst[i] = (IROUND(rgba[i][BCOMP] * 1023.0F) << 22)
2597 | (IROUND(rgba[i][GCOMP] * 1023.0F) << 12)
2598 | (IROUND(rgba[i][RCOMP] * 1023.0F) << 2)
2599 | (IROUND(rgba[i][ACOMP] * 3.0F) );
2600 }
2601 }
2602 else if (dstFormat == GL_ABGR_EXT) {
2603 GLuint *dst = (GLuint *) dstAddr;
2604 for (i=0;i<n;i++) {
2605 dst[i] = (IROUND(rgba[i][ACOMP] * 1023.0F) << 22)
2606 | (IROUND(rgba[i][BCOMP] * 1023.0F) << 12)
2607 | (IROUND(rgba[i][GCOMP] * 1023.0F) << 2)
2608 | (IROUND(rgba[i][RCOMP] * 3.0F) );
2609 }
2610 }
2611 break;
2612 case GL_UNSIGNED_INT_2_10_10_10_REV:
2613 if (dstFormat == GL_RGBA) {
2614 GLuint *dst = (GLuint *) dstAddr;
2615 for (i=0;i<n;i++) {
2616 dst[i] = (IROUND(rgba[i][RCOMP] * 1023.0F) )
2617 | (IROUND(rgba[i][GCOMP] * 1023.0F) << 10)
2618 | (IROUND(rgba[i][BCOMP] * 1023.0F) << 20)
2619 | (IROUND(rgba[i][ACOMP] * 3.0F) << 30);
2620 }
2621 }
2622 else if (dstFormat == GL_BGRA) {
2623 GLuint *dst = (GLuint *) dstAddr;
2624 for (i=0;i<n;i++) {
2625 dst[i] = (IROUND(rgba[i][BCOMP] * 1023.0F) )
2626 | (IROUND(rgba[i][GCOMP] * 1023.0F) << 10)
2627 | (IROUND(rgba[i][RCOMP] * 1023.0F) << 20)
2628 | (IROUND(rgba[i][ACOMP] * 3.0F) << 30);
2629 }
2630 }
2631 else if (dstFormat == GL_ABGR_EXT) {
2632 GLuint *dst = (GLuint *) dstAddr;
2633 for (i=0;i<n;i++) {
2634 dst[i] = (IROUND(rgba[i][ACOMP] * 1023.0F) )
2635 | (IROUND(rgba[i][BCOMP] * 1023.0F) << 10)
2636 | (IROUND(rgba[i][GCOMP] * 1023.0F) << 20)
2637 | (IROUND(rgba[i][RCOMP] * 3.0F) << 30);
2638 }
2639 }
2640 break;
2641 default:
2642 _mesa_problem(ctx, "bad type in _mesa_pack_rgba_span_float");
2643 return;
2644 }
2645
2646 if (dstPacking->SwapBytes) {
2647 GLint swapSize = _mesa_sizeof_packed_type(dstType);
2648 if (swapSize == 2) {
2649 if (dstPacking->SwapBytes) {
2650 _mesa_swap2((GLushort *) dstAddr, n * comps);
2651 }
2652 }
2653 else if (swapSize == 4) {
2654 if (dstPacking->SwapBytes) {
2655 _mesa_swap4((GLuint *) dstAddr, n * comps);
2656 }
2657 }
2658 }
2659 }
2660
2661
2662 #define SWAP2BYTE(VALUE) \
2663 { \
2664 GLubyte *bytes = (GLubyte *) &(VALUE); \
2665 GLubyte tmp = bytes[0]; \
2666 bytes[0] = bytes[1]; \
2667 bytes[1] = tmp; \
2668 }
2669
2670 #define SWAP4BYTE(VALUE) \
2671 { \
2672 GLubyte *bytes = (GLubyte *) &(VALUE); \
2673 GLubyte tmp = bytes[0]; \
2674 bytes[0] = bytes[3]; \
2675 bytes[3] = tmp; \
2676 tmp = bytes[1]; \
2677 bytes[1] = bytes[2]; \
2678 bytes[2] = tmp; \
2679 }
2680
2681
2682 static void
2683 extract_uint_indexes(GLuint n, GLuint indexes[],
2684 GLenum srcFormat, GLenum srcType, const GLvoid *src,
2685 const struct gl_pixelstore_attrib *unpack )
2686 {
2687 ASSERT(srcFormat == GL_COLOR_INDEX || srcFormat == GL_STENCIL_INDEX);
2688
2689 ASSERT(srcType == GL_BITMAP ||
2690 srcType == GL_UNSIGNED_BYTE ||
2691 srcType == GL_BYTE ||
2692 srcType == GL_UNSIGNED_SHORT ||
2693 srcType == GL_SHORT ||
2694 srcType == GL_UNSIGNED_INT ||
2695 srcType == GL_INT ||
2696 srcType == GL_UNSIGNED_INT_24_8_EXT ||
2697 srcType == GL_HALF_FLOAT_ARB ||
2698 srcType == GL_FLOAT);
2699
2700 switch (srcType) {
2701 case GL_BITMAP:
2702 {
2703 GLubyte *ubsrc = (GLubyte *) src;
2704 if (unpack->LsbFirst) {
2705 GLubyte mask = 1 << (unpack->SkipPixels & 0x7);
2706 GLuint i;
2707 for (i = 0; i < n; i++) {
2708 indexes[i] = (*ubsrc & mask) ? 1 : 0;
2709 if (mask == 128) {
2710 mask = 1;
2711 ubsrc++;
2712 }
2713 else {
2714 mask = mask << 1;
2715 }
2716 }
2717 }
2718 else {
2719 GLubyte mask = 128 >> (unpack->SkipPixels & 0x7);
2720 GLuint i;
2721 for (i = 0; i < n; i++) {
2722 indexes[i] = (*ubsrc & mask) ? 1 : 0;
2723 if (mask == 1) {
2724 mask = 128;
2725 ubsrc++;
2726 }
2727 else {
2728 mask = mask >> 1;
2729 }
2730 }
2731 }
2732 }
2733 break;
2734 case GL_UNSIGNED_BYTE:
2735 {
2736 GLuint i;
2737 const GLubyte *s = (const GLubyte *) src;
2738 for (i = 0; i < n; i++)
2739 indexes[i] = s[i];
2740 }
2741 break;
2742 case GL_BYTE:
2743 {
2744 GLuint i;
2745 const GLbyte *s = (const GLbyte *) src;
2746 for (i = 0; i < n; i++)
2747 indexes[i] = s[i];
2748 }
2749 break;
2750 case GL_UNSIGNED_SHORT:
2751 {
2752 GLuint i;
2753 const GLushort *s = (const GLushort *) src;
2754 if (unpack->SwapBytes) {
2755 for (i = 0; i < n; i++) {
2756 GLushort value = s[i];
2757 SWAP2BYTE(value);
2758 indexes[i] = value;
2759 }
2760 }
2761 else {
2762 for (i = 0; i < n; i++)
2763 indexes[i] = s[i];
2764 }
2765 }
2766 break;
2767 case GL_SHORT:
2768 {
2769 GLuint i;
2770 const GLshort *s = (const GLshort *) src;
2771 if (unpack->SwapBytes) {
2772 for (i = 0; i < n; i++) {
2773 GLshort value = s[i];
2774 SWAP2BYTE(value);
2775 indexes[i] = value;
2776 }
2777 }
2778 else {
2779 for (i = 0; i < n; i++)
2780 indexes[i] = s[i];
2781 }
2782 }
2783 break;
2784 case GL_UNSIGNED_INT:
2785 {
2786 GLuint i;
2787 const GLuint *s = (const GLuint *) src;
2788 if (unpack->SwapBytes) {
2789 for (i = 0; i < n; i++) {
2790 GLuint value = s[i];
2791 SWAP4BYTE(value);
2792 indexes[i] = value;
2793 }
2794 }
2795 else {
2796 for (i = 0; i < n; i++)
2797 indexes[i] = s[i];
2798 }
2799 }
2800 break;
2801 case GL_INT:
2802 {
2803 GLuint i;
2804 const GLint *s = (const GLint *) src;
2805 if (unpack->SwapBytes) {
2806 for (i = 0; i < n; i++) {
2807 GLint value = s[i];
2808 SWAP4BYTE(value);
2809 indexes[i] = value;
2810 }
2811 }
2812 else {
2813 for (i = 0; i < n; i++)
2814 indexes[i] = s[i];
2815 }
2816 }
2817 break;
2818 case GL_FLOAT:
2819 {
2820 GLuint i;
2821 const GLfloat *s = (const GLfloat *) src;
2822 if (unpack->SwapBytes) {
2823 for (i = 0; i < n; i++) {
2824 GLfloat value = s[i];
2825 SWAP4BYTE(value);
2826 indexes[i] = (GLuint) value;
2827 }
2828 }
2829 else {
2830 for (i = 0; i < n; i++)
2831 indexes[i] = (GLuint) s[i];
2832 }
2833 }
2834 break;
2835 case GL_HALF_FLOAT_ARB:
2836 {
2837 GLuint i;
2838 const GLhalfARB *s = (const GLhalfARB *) src;
2839 if (unpack->SwapBytes) {
2840 for (i = 0; i < n; i++) {
2841 GLhalfARB value = s[i];
2842 SWAP2BYTE(value);
2843 indexes[i] = (GLuint) _mesa_half_to_float(value);
2844 }
2845 }
2846 else {
2847 for (i = 0; i < n; i++)
2848 indexes[i] = (GLuint) _mesa_half_to_float(s[i]);
2849 }
2850 }
2851 break;
2852 case GL_UNSIGNED_INT_24_8_EXT:
2853 {
2854 GLuint i;
2855 const GLuint *s = (const GLuint *) src;
2856 if (unpack->SwapBytes) {
2857 for (i = 0; i < n; i++) {
2858 GLuint value = s[i];
2859 SWAP4BYTE(value);
2860 indexes[i] = value & 0xff; /* lower 8 bits */
2861 }
2862 }
2863 else {
2864 for (i = 0; i < n; i++)
2865 indexes[i] = s[i] & 0xfff; /* lower 8 bits */
2866 }
2867 }
2868 break;
2869
2870 default:
2871 _mesa_problem(NULL, "bad srcType in extract_uint_indexes");
2872 return;
2873 }
2874 }
2875
2876
2877 /*
2878 * This function extracts floating point RGBA values from arbitrary
2879 * image data. srcFormat and srcType are the format and type parameters
2880 * passed to glDrawPixels, glTexImage[123]D, glTexSubImage[123]D, etc.
2881 *
2882 * Refering to section 3.6.4 of the OpenGL 1.2 spec, this function
2883 * implements the "Conversion to floating point", "Conversion to RGB",
2884 * and "Final Expansion to RGBA" operations.
2885 *
2886 * Args: n - number of pixels
2887 * rgba - output colors
2888 * srcFormat - format of incoming data
2889 * srcType - data type of incoming data
2890 * src - source data pointer
2891 * swapBytes - perform byteswapping of incoming data?
2892 */
2893 static void
2894 extract_float_rgba(GLuint n, GLfloat rgba[][4],
2895 GLenum srcFormat, GLenum srcType, const GLvoid *src,
2896 GLboolean swapBytes)
2897 {
2898 GLint redIndex, greenIndex, blueIndex, alphaIndex;
2899 GLint stride;
2900 GLint rComp, bComp, gComp, aComp;
2901
2902 ASSERT(srcFormat == GL_RED ||
2903 srcFormat == GL_GREEN ||
2904 srcFormat == GL_BLUE ||
2905 srcFormat == GL_ALPHA ||
2906 srcFormat == GL_LUMINANCE ||
2907 srcFormat == GL_LUMINANCE_ALPHA ||
2908 srcFormat == GL_INTENSITY ||
2909 srcFormat == GL_RGB ||
2910 srcFormat == GL_BGR ||
2911 srcFormat == GL_RGBA ||
2912 srcFormat == GL_BGRA ||
2913 srcFormat == GL_ABGR_EXT ||
2914 srcFormat == GL_DUDV_ATI);
2915
2916 ASSERT(srcType == GL_UNSIGNED_BYTE ||
2917 srcType == GL_BYTE ||
2918 srcType == GL_UNSIGNED_SHORT ||
2919 srcType == GL_SHORT ||
2920 srcType == GL_UNSIGNED_INT ||
2921 srcType == GL_INT ||
2922 srcType == GL_HALF_FLOAT_ARB ||
2923 srcType == GL_FLOAT ||
2924 srcType == GL_UNSIGNED_BYTE_3_3_2 ||
2925 srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
2926 srcType == GL_UNSIGNED_SHORT_5_6_5 ||
2927 srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
2928 srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
2929 srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
2930 srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
2931 srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
2932 srcType == GL_UNSIGNED_INT_8_8_8_8 ||
2933 srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
2934 srcType == GL_UNSIGNED_INT_10_10_10_2 ||
2935 srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
2936
2937 rComp = gComp = bComp = aComp = -1;
2938
2939 switch (srcFormat) {
2940 case GL_RED:
2941 redIndex = 0;
2942 greenIndex = blueIndex = alphaIndex = -1;
2943 stride = 1;
2944 break;
2945 case GL_GREEN:
2946 greenIndex = 0;
2947 redIndex = blueIndex = alphaIndex = -1;
2948 stride = 1;
2949 break;
2950 case GL_BLUE:
2951 blueIndex = 0;
2952 redIndex = greenIndex = alphaIndex = -1;
2953 stride = 1;
2954 break;
2955 case GL_ALPHA:
2956 redIndex = greenIndex = blueIndex = -1;
2957 alphaIndex = 0;
2958 stride = 1;
2959 break;
2960 case GL_LUMINANCE:
2961 redIndex = greenIndex = blueIndex = 0;
2962 alphaIndex = -1;
2963 stride = 1;
2964 break;
2965 case GL_LUMINANCE_ALPHA:
2966 redIndex = greenIndex = blueIndex = 0;
2967 alphaIndex = 1;
2968 stride = 2;
2969 break;
2970 case GL_INTENSITY:
2971 redIndex = greenIndex = blueIndex = alphaIndex = 0;
2972 stride = 1;
2973 break;
2974 case GL_RGB:
2975 redIndex = 0;
2976 greenIndex = 1;
2977 blueIndex = 2;
2978 alphaIndex = -1;
2979 rComp = 0;
2980 gComp = 1;
2981 bComp = 2;
2982 aComp = 3;
2983 stride = 3;
2984 break;
2985 case GL_BGR:
2986 redIndex = 2;
2987 greenIndex = 1;
2988 blueIndex = 0;
2989 alphaIndex = -1;
2990 rComp = 2;
2991 gComp = 1;
2992 bComp = 0;
2993 aComp = 3;
2994 stride = 3;
2995 break;
2996 case GL_RGBA:
2997 redIndex = 0;
2998 greenIndex = 1;
2999 blueIndex = 2;
3000 alphaIndex = 3;
3001 rComp = 0;
3002 gComp = 1;
3003 bComp = 2;
3004 aComp = 3;
3005 stride = 4;
3006 break;
3007 case GL_BGRA:
3008 redIndex = 2;
3009 greenIndex = 1;
3010 blueIndex = 0;
3011 alphaIndex = 3;
3012 rComp = 2;
3013 gComp = 1;
3014 bComp = 0;
3015 aComp = 3;
3016 stride = 4;
3017 break;
3018 case GL_ABGR_EXT:
3019 redIndex = 3;
3020 greenIndex = 2;
3021 blueIndex = 1;
3022 alphaIndex = 0;
3023 rComp = 3;
3024 gComp = 2;
3025 bComp = 1;
3026 aComp = 0;
3027 stride = 4;
3028 break;
3029 case GL_DUDV_ATI:
3030 redIndex = 0;
3031 greenIndex = 1;
3032 blueIndex = -1;
3033 alphaIndex = -1;
3034 stride = 2;
3035 break;
3036 default:
3037 _mesa_problem(NULL, "bad srcFormat in extract float data");
3038 return;
3039 }
3040
3041
3042 #define PROCESS(INDEX, CHANNEL, DEFAULT, TYPE, CONVERSION) \
3043 if ((INDEX) < 0) { \
3044 GLuint i; \
3045 for (i = 0; i < n; i++) { \
3046 rgba[i][CHANNEL] = DEFAULT; \
3047 } \
3048 } \
3049 else if (swapBytes) { \
3050 const TYPE *s = (const TYPE *) src; \
3051 GLuint i; \
3052 for (i = 0; i < n; i++) { \
3053 TYPE value = s[INDEX]; \
3054 if (sizeof(TYPE) == 2) { \
3055 SWAP2BYTE(value); \
3056 } \
3057 else if (sizeof(TYPE) == 4) { \
3058 SWAP4BYTE(value); \
3059 } \
3060 rgba[i][CHANNEL] = (GLfloat) CONVERSION(value); \
3061 s += stride; \
3062 } \
3063 } \
3064 else { \
3065 const TYPE *s = (const TYPE *) src; \
3066 GLuint i; \
3067 for (i = 0; i < n; i++) { \
3068 rgba[i][CHANNEL] = (GLfloat) CONVERSION(s[INDEX]); \
3069 s += stride; \
3070 } \
3071 }
3072
3073 switch (srcType) {
3074 case GL_UNSIGNED_BYTE:
3075 PROCESS(redIndex, RCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
3076 PROCESS(greenIndex, GCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
3077 PROCESS(blueIndex, BCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
3078 PROCESS(alphaIndex, ACOMP, 1.0F, GLubyte, UBYTE_TO_FLOAT);
3079 break;
3080 case GL_BYTE:
3081 PROCESS(redIndex, RCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
3082 PROCESS(greenIndex, GCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
3083 PROCESS(blueIndex, BCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
3084 PROCESS(alphaIndex, ACOMP, 1.0F, GLbyte, BYTE_TO_FLOAT);
3085 break;
3086 case GL_UNSIGNED_SHORT:
3087 PROCESS(redIndex, RCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
3088 PROCESS(greenIndex, GCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
3089 PROCESS(blueIndex, BCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
3090 PROCESS(alphaIndex, ACOMP, 1.0F, GLushort, USHORT_TO_FLOAT);
3091 break;
3092 case GL_SHORT:
3093 PROCESS(redIndex, RCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
3094 PROCESS(greenIndex, GCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
3095 PROCESS(blueIndex, BCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
3096 PROCESS(alphaIndex, ACOMP, 1.0F, GLshort, SHORT_TO_FLOAT);
3097 break;
3098 case GL_UNSIGNED_INT:
3099 PROCESS(redIndex, RCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
3100 PROCESS(greenIndex, GCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
3101 PROCESS(blueIndex, BCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
3102 PROCESS(alphaIndex, ACOMP, 1.0F, GLuint, UINT_TO_FLOAT);
3103 break;
3104 case GL_INT:
3105 PROCESS(redIndex, RCOMP, 0.0F, GLint, INT_TO_FLOAT);
3106 PROCESS(greenIndex, GCOMP, 0.0F, GLint, INT_TO_FLOAT);
3107 PROCESS(blueIndex, BCOMP, 0.0F, GLint, INT_TO_FLOAT);
3108 PROCESS(alphaIndex, ACOMP, 1.0F, GLint, INT_TO_FLOAT);
3109 break;
3110 case GL_FLOAT:
3111 PROCESS(redIndex, RCOMP, 0.0F, GLfloat, (GLfloat));
3112 PROCESS(greenIndex, GCOMP, 0.0F, GLfloat, (GLfloat));
3113 PROCESS(blueIndex, BCOMP, 0.0F, GLfloat, (GLfloat));
3114 PROCESS(alphaIndex, ACOMP, 1.0F, GLfloat, (GLfloat));
3115 break;
3116 case GL_HALF_FLOAT_ARB:
3117 PROCESS(redIndex, RCOMP, 0.0F, GLhalfARB, _mesa_half_to_float);
3118 PROCESS(greenIndex, GCOMP, 0.0F, GLhalfARB, _mesa_half_to_float);
3119 PROCESS(blueIndex, BCOMP, 0.0F, GLhalfARB, _mesa_half_to_float);
3120 PROCESS(alphaIndex, ACOMP, 1.0F, GLhalfARB, _mesa_half_to_float);
3121 break;
3122 case GL_UNSIGNED_BYTE_3_3_2:
3123 {
3124 const GLubyte *ubsrc = (const GLubyte *) src;
3125 GLuint i;
3126 for (i = 0; i < n; i ++) {
3127 GLubyte p = ubsrc[i];
3128 rgba[i][rComp] = ((p >> 5) ) * (1.0F / 7.0F);
3129 rgba[i][gComp] = ((p >> 2) & 0x7) * (1.0F / 7.0F);
3130 rgba[i][bComp] = ((p ) & 0x3) * (1.0F / 3.0F);
3131 rgba[i][aComp] = 1.0F;
3132 }
3133 }
3134 break;
3135 case GL_UNSIGNED_BYTE_2_3_3_REV:
3136 {
3137 const GLubyte *ubsrc = (const GLubyte *) src;
3138 GLuint i;
3139 for (i = 0; i < n; i ++) {
3140 GLubyte p = ubsrc[i];
3141 rgba[i][rComp] = ((p ) & 0x7) * (1.0F / 7.0F);
3142 rgba[i][gComp] = ((p >> 3) & 0x7) * (1.0F / 7.0F);
3143 rgba[i][bComp] = ((p >> 6) ) * (1.0F / 3.0F);
3144 rgba[i][aComp] = 1.0F;
3145 }
3146 }
3147 break;
3148 case GL_UNSIGNED_SHORT_5_6_5:
3149 if (swapBytes) {
3150 const GLushort *ussrc = (const GLushort *) src;
3151 GLuint i;
3152 for (i = 0; i < n; i ++) {
3153 GLushort p = ussrc[i];
3154 SWAP2BYTE(p);
3155 rgba[i][rComp] = ((p >> 11) ) * (1.0F / 31.0F);
3156 rgba[i][gComp] = ((p >> 5) & 0x3f) * (1.0F / 63.0F);
3157 rgba[i][bComp] = ((p ) & 0x1f) * (1.0F / 31.0F);
3158 rgba[i][aComp] = 1.0F;
3159 }
3160 }
3161 else {
3162 const GLushort *ussrc = (const GLushort *) src;
3163 GLuint i;
3164 for (i = 0; i < n; i ++) {
3165 GLushort p = ussrc[i];
3166 rgba[i][rComp] = ((p >> 11) ) * (1.0F / 31.0F);
3167 rgba[i][gComp] = ((p >> 5) & 0x3f) * (1.0F / 63.0F);
3168 rgba[i][bComp] = ((p ) & 0x1f) * (1.0F / 31.0F);
3169 rgba[i][aComp] = 1.0F;
3170 }
3171 }
3172 break;
3173 case GL_UNSIGNED_SHORT_5_6_5_REV:
3174 if (swapBytes) {
3175 const GLushort *ussrc = (const GLushort *) src;
3176 GLuint i;
3177 for (i = 0; i < n; i ++) {
3178 GLushort p = ussrc[i];
3179 SWAP2BYTE(p);
3180 rgba[i][rComp] = ((p ) & 0x1f) * (1.0F / 31.0F);
3181 rgba[i][gComp] = ((p >> 5) & 0x3f) * (1.0F / 63.0F);
3182 rgba[i][bComp] = ((p >> 11) ) * (1.0F / 31.0F);
3183 rgba[i][aComp] = 1.0F;
3184 }
3185 }
3186 else {
3187 const GLushort *ussrc = (const GLushort *) src;
3188 GLuint i;
3189 for (i = 0; i < n; i ++) {
3190 GLushort p = ussrc[i];
3191 rgba[i][rComp] = ((p ) & 0x1f) * (1.0F / 31.0F);
3192 rgba[i][gComp] = ((p >> 5) & 0x3f) * (1.0F / 63.0F);
3193 rgba[i][bComp] = ((p >> 11) ) * (1.0F / 31.0F);
3194 rgba[i][aComp] = 1.0F;
3195 }
3196 }
3197 break;
3198 case GL_UNSIGNED_SHORT_4_4_4_4:
3199 if (swapBytes) {
3200 const GLushort *ussrc = (const GLushort *) src;
3201 GLuint i;
3202 for (i = 0; i < n; i ++) {
3203 GLushort p = ussrc[i];
3204 SWAP2BYTE(p);
3205 rgba[i][rComp] = ((p >> 12) ) * (1.0F / 15.0F);
3206 rgba[i][gComp] = ((p >> 8) & 0xf) * (1.0F / 15.0F);
3207 rgba[i][bComp] = ((p >> 4) & 0xf) * (1.0F / 15.0F);
3208 rgba[i][aComp] = ((p ) & 0xf) * (1.0F / 15.0F);
3209 }
3210 }
3211 else {
3212 const GLushort *ussrc = (const GLushort *) src;
3213 GLuint i;
3214 for (i = 0; i < n; i ++) {
3215 GLushort p = ussrc[i];
3216 rgba[i][rComp] = ((p >> 12) ) * (1.0F / 15.0F);
3217 rgba[i][gComp] = ((p >> 8) & 0xf) * (1.0F / 15.0F);
3218 rgba[i][bComp] = ((p >> 4) & 0xf) * (1.0F / 15.0F);
3219 rgba[i][aComp] = ((p ) & 0xf) * (1.0F / 15.0F);
3220 }
3221 }
3222 break;
3223 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
3224 if (swapBytes) {
3225 const GLushort *ussrc = (const GLushort *) src;
3226 GLuint i;
3227 for (i = 0; i < n; i ++) {
3228 GLushort p = ussrc[i];
3229 SWAP2BYTE(p);
3230 rgba[i][rComp] = ((p ) & 0xf) * (1.0F / 15.0F);
3231 rgba[i][gComp] = ((p >> 4) & 0xf) * (1.0F / 15.0F);
3232 rgba[i][bComp] = ((p >> 8) & 0xf) * (1.0F / 15.0F);
3233 rgba[i][aComp] = ((p >> 12) ) * (1.0F / 15.0F);
3234 }
3235 }
3236 else {
3237 const GLushort *ussrc = (const GLushort *) src;
3238 GLuint i;
3239 for (i = 0; i < n; i ++) {
3240 GLushort p = ussrc[i];
3241 rgba[i][rComp] = ((p ) & 0xf) * (1.0F / 15.0F);
3242 rgba[i][gComp] = ((p >> 4) & 0xf) * (1.0F / 15.0F);
3243 rgba[i][bComp] = ((p >> 8) & 0xf) * (1.0F / 15.0F);
3244 rgba[i][aComp] = ((p >> 12) ) * (1.0F / 15.0F);
3245 }
3246 }
3247 break;
3248 case GL_UNSIGNED_SHORT_5_5_5_1:
3249 if (swapBytes) {
3250 const GLushort *ussrc = (const GLushort *) src;
3251 GLuint i;
3252 for (i = 0; i < n; i ++) {
3253 GLushort p = ussrc[i];
3254 SWAP2BYTE(p);
3255 rgba[i][rComp] = ((p >> 11) ) * (1.0F / 31.0F);
3256 rgba[i][gComp] = ((p >> 6) & 0x1f) * (1.0F / 31.0F);
3257 rgba[i][bComp] = ((p >> 1) & 0x1f) * (1.0F / 31.0F);
3258 rgba[i][aComp] = ((p ) & 0x1) * (1.0F / 1.0F);
3259 }
3260 }
3261 else {
3262 const GLushort *ussrc = (const GLushort *) src;
3263 GLuint i;
3264 for (i = 0; i < n; i ++) {
3265 GLushort p = ussrc[i];
3266 rgba[i][rComp] = ((p >> 11) ) * (1.0F / 31.0F);
3267 rgba[i][gComp] = ((p >> 6) & 0x1f) * (1.0F / 31.0F);
3268 rgba[i][bComp] = ((p >> 1) & 0x1f) * (1.0F / 31.0F);
3269 rgba[i][aComp] = ((p ) & 0x1) * (1.0F / 1.0F);
3270 }
3271 }
3272 break;
3273 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
3274 if (swapBytes) {
3275 const GLushort *ussrc = (const GLushort *) src;
3276 GLuint i;
3277 for (i = 0; i < n; i ++) {
3278 GLushort p = ussrc[i];
3279 SWAP2BYTE(p);
3280 rgba[i][rComp] = ((p ) & 0x1f) * (1.0F / 31.0F);
3281 rgba[i][gComp] = ((p >> 5) & 0x1f) * (1.0F / 31.0F);
3282 rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
3283 rgba[i][aComp] = ((p >> 15) ) * (1.0F / 1.0F);
3284 }
3285 }
3286 else {
3287 const GLushort *ussrc = (const GLushort *) src;
3288 GLuint i;
3289 for (i = 0; i < n; i ++) {
3290 GLushort p = ussrc[i];
3291 rgba[i][rComp] = ((p ) & 0x1f) * (1.0F / 31.0F);
3292 rgba[i][gComp] = ((p >> 5) & 0x1f) * (1.0F / 31.0F);
3293 rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
3294 rgba[i][aComp] = ((p >> 15) ) * (1.0F / 1.0F);
3295 }
3296 }
3297 break;
3298 case GL_UNSIGNED_INT_8_8_8_8:
3299 if (swapBytes) {
3300 const GLuint *uisrc = (const GLuint *) src;
3301 GLuint i;
3302 for (i = 0; i < n; i ++) {
3303 GLuint p = uisrc[i];
3304 rgba[i][rComp] = UBYTE_TO_FLOAT((p ) & 0xff);
3305 rgba[i][gComp] = UBYTE_TO_FLOAT((p >> 8) & 0xff);
3306 rgba[i][bComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
3307 rgba[i][aComp] = UBYTE_TO_FLOAT((p >> 24) );
3308 }
3309 }
3310 else {
3311 const GLuint *uisrc = (const GLuint *) src;
3312 GLuint i;
3313 for (i = 0; i < n; i ++) {
3314 GLuint p = uisrc[i];
3315 rgba[i][rComp] = UBYTE_TO_FLOAT((p >> 24) );
3316 rgba[i][gComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
3317 rgba[i][bComp] = UBYTE_TO_FLOAT((p >> 8) & 0xff);
3318 rgba[i][aComp] = UBYTE_TO_FLOAT((p ) & 0xff);
3319 }
3320 }
3321 break;
3322 case GL_UNSIGNED_INT_8_8_8_8_REV:
3323 if (swapBytes) {
3324 const GLuint *uisrc = (const GLuint *) src;
3325 GLuint i;
3326 for (i = 0; i < n; i ++) {
3327 GLuint p = uisrc[i];
3328 rgba[i][rComp] = UBYTE_TO_FLOAT((p >> 24) );
3329 rgba[i][gComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
3330 rgba[i][bComp] = UBYTE_TO_FLOAT((p >> 8) & 0xff);
3331 rgba[i][aComp] = UBYTE_TO_FLOAT((p ) & 0xff);
3332 }
3333 }
3334 else {
3335 const GLuint *uisrc = (const GLuint *) src;
3336 GLuint i;
3337 for (i = 0; i < n; i ++) {
3338 GLuint p = uisrc[i];
3339 rgba[i][rComp] = UBYTE_TO_FLOAT((p ) & 0xff);
3340 rgba[i][gComp] = UBYTE_TO_FLOAT((p >> 8) & 0xff);
3341 rgba[i][bComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
3342 rgba[i][aComp] = UBYTE_TO_FLOAT((p >> 24) );
3343 }
3344 }
3345 break;
3346 case GL_UNSIGNED_INT_10_10_10_2:
3347 if (swapBytes) {
3348 const GLuint *uisrc = (const GLuint *) src;
3349 GLuint i;
3350 for (i = 0; i < n; i ++) {
3351 GLuint p = uisrc[i];
3352 SWAP4BYTE(p);
3353 rgba[i][rComp] = ((p >> 22) ) * (1.0F / 1023.0F);
3354 rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
3355 rgba[i][bComp] = ((p >> 2) & 0x3ff) * (1.0F / 1023.0F);
3356 rgba[i][aComp] = ((p ) & 0x3 ) * (1.0F / 3.0F);
3357 }
3358 }
3359 else {
3360 const GLuint *uisrc = (const GLuint *) src;
3361 GLuint i;
3362 for (i = 0; i < n; i ++) {
3363 GLuint p = uisrc[i];
3364 rgba[i][rComp] = ((p >> 22) ) * (1.0F / 1023.0F);
3365 rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
3366 rgba[i][bComp] = ((p >> 2) & 0x3ff) * (1.0F / 1023.0F);
3367 rgba[i][aComp] = ((p ) & 0x3 ) * (1.0F / 3.0F);
3368 }
3369 }
3370 break;
3371 case GL_UNSIGNED_INT_2_10_10_10_REV:
3372 if (swapBytes) {
3373 const GLuint *uisrc = (const GLuint *) src;
3374 GLuint i;
3375 for (i = 0; i < n; i ++) {
3376 GLuint p = uisrc[i];
3377 SWAP4BYTE(p);
3378 rgba[i][rComp] = ((p ) & 0x3ff) * (1.0F / 1023.0F);
3379 rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
3380 rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
3381 rgba[i][aComp] = ((p >> 30) ) * (1.0F / 3.0F);
3382 }
3383 }
3384 else {
3385 const GLuint *uisrc = (const GLuint *) src;
3386 GLuint i;
3387 for (i = 0; i < n; i ++) {
3388 GLuint p = uisrc[i];
3389 rgba[i][rComp] = ((p ) & 0x3ff) * (1.0F / 1023.0F);
3390 rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
3391 rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
3392 rgba[i][aComp] = ((p >> 30) ) * (1.0F / 3.0F);
3393 }
3394 }
3395 break;
3396 default:
3397 _mesa_problem(NULL, "bad srcType in extract float data");
3398 break;
3399 }
3400 }
3401
3402
3403 /*
3404 * Unpack a row of color image data from a client buffer according to
3405 * the pixel unpacking parameters.
3406 * Return GLchan values in the specified dest image format.
3407 * This is used by glDrawPixels and glTexImage?D().
3408 * \param ctx - the context
3409 * n - number of pixels in the span
3410 * dstFormat - format of destination color array
3411 * dest - the destination color array
3412 * srcFormat - source image format
3413 * srcType - source image data type
3414 * source - source image pointer
3415 * srcPacking - pixel unpacking parameters
3416 * transferOps - bitmask of IMAGE_*_BIT values of operations to apply
3417 *
3418 * XXX perhaps expand this to process whole images someday.
3419 */
3420 void
3421 _mesa_unpack_color_span_chan( GLcontext *ctx,
3422 GLuint n, GLenum dstFormat, GLchan dest[],
3423 GLenum srcFormat, GLenum srcType,
3424 const GLvoid *source,
3425 const struct gl_pixelstore_attrib *srcPacking,
3426 GLbitfield transferOps )
3427 {
3428 ASSERT(dstFormat == GL_ALPHA ||
3429 dstFormat == GL_LUMINANCE ||
3430 dstFormat == GL_LUMINANCE_ALPHA ||
3431 dstFormat == GL_INTENSITY ||
3432 dstFormat == GL_RGB ||
3433 dstFormat == GL_RGBA ||
3434 dstFormat == GL_COLOR_INDEX);
3435
3436 ASSERT(srcFormat == GL_RED ||
3437 srcFormat == GL_GREEN ||
3438 srcFormat == GL_BLUE ||
3439 srcFormat == GL_ALPHA ||
3440 srcFormat == GL_LUMINANCE ||
3441 srcFormat == GL_LUMINANCE_ALPHA ||
3442 srcFormat == GL_INTENSITY ||
3443 srcFormat == GL_RGB ||
3444 srcFormat == GL_BGR ||
3445 srcFormat == GL_RGBA ||
3446 srcFormat == GL_BGRA ||
3447 srcFormat == GL_ABGR_EXT ||
3448 srcFormat == GL_COLOR_INDEX);
3449
3450 ASSERT(srcType == GL_BITMAP ||
3451 srcType == GL_UNSIGNED_BYTE ||
3452 srcType == GL_BYTE ||
3453 srcType == GL_UNSIGNED_SHORT ||
3454 srcType == GL_SHORT ||
3455 srcType == GL_UNSIGNED_INT ||
3456 srcType == GL_INT ||
3457 srcType == GL_HALF_FLOAT_ARB ||
3458 srcType == GL_FLOAT ||
3459 srcType == GL_UNSIGNED_BYTE_3_3_2 ||
3460 srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
3461 srcType == GL_UNSIGNED_SHORT_5_6_5 ||
3462 srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
3463 srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
3464 srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
3465 srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
3466 srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
3467 srcType == GL_UNSIGNED_INT_8_8_8_8 ||
3468 srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
3469 srcType == GL_UNSIGNED_INT_10_10_10_2 ||
3470 srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
3471
3472 /* Try simple cases first */
3473 if (transferOps == 0) {
3474 if (srcType == CHAN_TYPE) {
3475 if (dstFormat == GL_RGBA) {
3476 if (srcFormat == GL_RGBA) {
3477 _mesa_memcpy( dest, source, n * 4 * sizeof(GLchan) );
3478 return;
3479 }
3480 else if (srcFormat == GL_RGB) {
3481 GLuint i;
3482 const GLchan *src = (const GLchan *) source;
3483 GLchan *dst = dest;
3484 for (i = 0; i < n; i++) {
3485 dst[0] = src[0];
3486 dst[1] = src[1];
3487 dst[2] = src[2];
3488 dst[3] = CHAN_MAX;
3489 src += 3;
3490 dst += 4;
3491 }
3492 return;
3493 }
3494 }
3495 else if (dstFormat == GL_RGB) {
3496 if (srcFormat == GL_RGB) {
3497 _mesa_memcpy( dest, source, n * 3 * sizeof(GLchan) );
3498 return;
3499 }
3500 else if (srcFormat == GL_RGBA) {
3501 GLuint i;
3502 const GLchan *src = (const GLchan *) source;
3503 GLchan *dst = dest;
3504 for (i = 0; i < n; i++) {
3505 dst[0] = src[0];
3506 dst[1] = src[1];
3507 dst[2] = src[2];
3508 src += 4;
3509 dst += 3;
3510 }
3511 return;
3512 }
3513 }
3514 else if (dstFormat == srcFormat) {
3515 GLint comps = _mesa_components_in_format(srcFormat);
3516 assert(comps > 0);
3517 _mesa_memcpy( dest, source, n * comps * sizeof(GLchan) );
3518 return;
3519 }
3520 }
3521 /*
3522 * Common situation, loading 8bit RGBA/RGB source images
3523 * into 16/32 bit destination. (OSMesa16/32)
3524 */
3525 else if (srcType == GL_UNSIGNED_BYTE) {
3526 if (dstFormat == GL_RGBA) {
3527 if (srcFormat == GL_RGB) {
3528 GLuint i;
3529 const GLubyte *src = (const GLubyte *) source;
3530 GLchan *dst = dest;
3531 for (i = 0; i < n; i++) {
3532 dst[0] = UBYTE_TO_CHAN(src[0]);
3533 dst[1] = UBYTE_TO_CHAN(src[1]);
3534 dst[2] = UBYTE_TO_CHAN(src[2]);
3535 dst[3] = CHAN_MAX;
3536 src += 3;
3537 dst += 4;
3538 }
3539 return;
3540 }
3541 else if (srcFormat == GL_RGBA) {
3542 GLuint i;
3543 const GLubyte *src = (const GLubyte *) source;
3544 GLchan *dst = dest;
3545 for (i = 0; i < n; i++) {
3546 dst[0] = UBYTE_TO_CHAN(src[0]);
3547 dst[1] = UBYTE_TO_CHAN(src[1]);
3548 dst[2] = UBYTE_TO_CHAN(src[2]);
3549 dst[3] = UBYTE_TO_CHAN(src[3]);
3550 src += 4;
3551 dst += 4;
3552 }
3553 return;
3554 }
3555 }
3556 else if (dstFormat == GL_RGB) {
3557 if (srcFormat == GL_RGB) {
3558 GLuint i;
3559 const GLubyte *src = (const GLubyte *) source;
3560 GLchan *dst = dest;
3561 for (i = 0; i < n; i++) {
3562 dst[0] = UBYTE_TO_CHAN(src[0]);
3563 dst[1] = UBYTE_TO_CHAN(src[1]);
3564 dst[2] = UBYTE_TO_CHAN(src[2]);
3565 src += 3;
3566 dst += 3;
3567 }
3568 return;
3569 }
3570 else if (srcFormat == GL_RGBA) {
3571 GLuint i;
3572 const GLubyte *src = (const GLubyte *) source;
3573 GLchan *dst = dest;
3574 for (i = 0; i < n; i++) {
3575 dst[0] = UBYTE_TO_CHAN(src[0]);
3576 dst[1] = UBYTE_TO_CHAN(src[1]);
3577 dst[2] = UBYTE_TO_CHAN(src[2]);
3578 src += 4;
3579 dst += 3;
3580 }
3581 return;
3582 }
3583 }
3584 }
3585 }
3586
3587
3588 /* general solution begins here */
3589 {
3590 GLint dstComponents;
3591 GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
3592 GLint dstLuminanceIndex, dstIntensityIndex;
3593 GLfloat rgba[MAX_WIDTH][4];
3594
3595 dstComponents = _mesa_components_in_format( dstFormat );
3596 /* source & dest image formats should have been error checked by now */
3597 assert(dstComponents > 0);
3598
3599 /*
3600 * Extract image data and convert to RGBA floats
3601 */
3602 assert(n <= MAX_WIDTH);
3603 if (srcFormat == GL_COLOR_INDEX) {
3604 GLuint indexes[MAX_WIDTH];
3605 extract_uint_indexes(n, indexes, srcFormat, srcType, source,
3606 srcPacking);
3607
3608 if (dstFormat == GL_COLOR_INDEX) {
3609 GLuint i;
3610 _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
3611 /* convert to GLchan and return */
3612 for (i = 0; i < n; i++) {
3613 dest[i] = (GLchan) (indexes[i] & 0xff);
3614 }
3615 return;
3616 }
3617 else {
3618 /* Convert indexes to RGBA */
3619 if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
3620 shift_and_offset_ci(ctx, n, indexes);
3621 }
3622 _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
3623 }
3624
3625 /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
3626 * with color indexes.
3627 */
3628 transferOps &= ~(IMAGE_SCALE_BIAS_BIT | IMAGE_MAP_COLOR_BIT);
3629 }
3630 else {
3631 /* non-color index data */
3632 extract_float_rgba(n, rgba, srcFormat, srcType, source,
3633 srcPacking->SwapBytes);
3634 }
3635
3636 /* Need to clamp if returning GLubytes or GLushorts */
3637 #if CHAN_TYPE != GL_FLOAT
3638 transferOps |= IMAGE_CLAMP_BIT;
3639 #endif
3640
3641 if (transferOps) {
3642 _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
3643 }
3644
3645 /* Now determine which color channels we need to produce.
3646 * And determine the dest index (offset) within each color tuple.
3647 */
3648 switch (dstFormat) {
3649 case GL_ALPHA:
3650 dstAlphaIndex = 0;
3651 dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
3652 dstLuminanceIndex = dstIntensityIndex = -1;
3653 break;
3654 case GL_LUMINANCE:
3655 dstLuminanceIndex = 0;
3656 dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
3657 dstIntensityIndex = -1;
3658 break;
3659 case GL_LUMINANCE_ALPHA:
3660 dstLuminanceIndex = 0;
3661 dstAlphaIndex = 1;
3662 dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
3663 dstIntensityIndex = -1;
3664 break;
3665 case GL_INTENSITY:
3666 dstIntensityIndex = 0;
3667 dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
3668 dstLuminanceIndex = -1;
3669 break;
3670 case GL_RGB:
3671 dstRedIndex = 0;
3672 dstGreenIndex = 1;
3673 dstBlueIndex = 2;
3674 dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
3675 break;
3676 case GL_RGBA:
3677 dstRedIndex = 0;
3678 dstGreenIndex = 1;
3679 dstBlueIndex = 2;
3680 dstAlphaIndex = 3;
3681 dstLuminanceIndex = dstIntensityIndex = -1;
3682 break;
3683 default:
3684 _mesa_problem(ctx, "bad dstFormat in _mesa_unpack_chan_span()");
3685 return;
3686 }
3687
3688
3689 /* Now return the GLchan data in the requested dstFormat */
3690
3691 if (dstRedIndex >= 0) {
3692 GLchan *dst = dest;
3693 GLuint i;
3694 for (i = 0; i < n; i++) {
3695 CLAMPED_FLOAT_TO_CHAN(dst[dstRedIndex], rgba[i][RCOMP]);
3696 dst += dstComponents;
3697 }
3698 }
3699
3700 if (dstGreenIndex >= 0) {
3701 GLchan *dst = dest;
3702 GLuint i;
3703 for (i = 0; i < n; i++) {
3704 CLAMPED_FLOAT_TO_CHAN(dst[dstGreenIndex], rgba[i][GCOMP]);
3705 dst += dstComponents;
3706 }
3707 }
3708
3709 if (dstBlueIndex >= 0) {
3710 GLchan *dst = dest;
3711 GLuint i;
3712 for (i = 0; i < n; i++) {
3713 CLAMPED_FLOAT_TO_CHAN(dst[dstBlueIndex], rgba[i][BCOMP]);
3714 dst += dstComponents;
3715 }
3716 }
3717
3718 if (dstAlphaIndex >= 0) {
3719 GLchan *dst = dest;
3720 GLuint i;
3721 for (i = 0; i < n; i++) {
3722 CLAMPED_FLOAT_TO_CHAN(dst[dstAlphaIndex], rgba[i][ACOMP]);
3723 dst += dstComponents;
3724 }
3725 }
3726
3727 if (dstIntensityIndex >= 0) {
3728 GLchan *dst = dest;
3729 GLuint i;
3730 assert(dstIntensityIndex == 0);
3731 assert(dstComponents == 1);
3732 for (i = 0; i < n; i++) {
3733 /* Intensity comes from red channel */
3734 CLAMPED_FLOAT_TO_CHAN(dst[i], rgba[i][RCOMP]);
3735 }
3736 }
3737
3738 if (dstLuminanceIndex >= 0) {
3739 GLchan *dst = dest;
3740 GLuint i;
3741 assert(dstLuminanceIndex == 0);
3742 for (i = 0; i < n; i++) {
3743 /* Luminance comes from red channel */
3744 CLAMPED_FLOAT_TO_CHAN(dst[0], rgba[i][RCOMP]);
3745 dst += dstComponents;
3746 }
3747 }
3748 }
3749 }
3750
3751
3752 /**
3753 * Same as _mesa_unpack_color_span_chan(), but return GLfloat data
3754 * instead of GLchan.
3755 */
3756 void
3757 _mesa_unpack_color_span_float( GLcontext *ctx,
3758 GLuint n, GLenum dstFormat, GLfloat dest[],
3759 GLenum srcFormat, GLenum srcType,
3760 const GLvoid *source,
3761 const struct gl_pixelstore_attrib *srcPacking,
3762 GLbitfield transferOps )
3763 {
3764 ASSERT(dstFormat == GL_ALPHA ||
3765 dstFormat == GL_LUMINANCE ||
3766 dstFormat == GL_LUMINANCE_ALPHA ||
3767 dstFormat == GL_INTENSITY ||
3768 dstFormat == GL_RGB ||
3769 dstFormat == GL_RGBA ||
3770 dstFormat == GL_COLOR_INDEX);
3771
3772 ASSERT(srcFormat == GL_RED ||
3773 srcFormat == GL_GREEN ||
3774 srcFormat == GL_BLUE ||
3775 srcFormat == GL_ALPHA ||
3776 srcFormat == GL_LUMINANCE ||
3777 srcFormat == GL_LUMINANCE_ALPHA ||
3778 srcFormat == GL_INTENSITY ||
3779 srcFormat == GL_RGB ||
3780 srcFormat == GL_BGR ||
3781 srcFormat == GL_RGBA ||
3782 srcFormat == GL_BGRA ||
3783 srcFormat == GL_ABGR_EXT ||
3784 srcFormat == GL_COLOR_INDEX);
3785
3786 ASSERT(srcType == GL_BITMAP ||
3787 srcType == GL_UNSIGNED_BYTE ||
3788 srcType == GL_BYTE ||
3789 srcType == GL_UNSIGNED_SHORT ||
3790 srcType == GL_SHORT ||
3791 srcType == GL_UNSIGNED_INT ||
3792 srcType == GL_INT ||
3793 srcType == GL_HALF_FLOAT_ARB ||
3794 srcType == GL_FLOAT ||
3795 srcType == GL_UNSIGNED_BYTE_3_3_2 ||
3796 srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
3797 srcType == GL_UNSIGNED_SHORT_5_6_5 ||
3798 srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
3799 srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
3800 srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
3801 srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
3802 srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
3803 srcType == GL_UNSIGNED_INT_8_8_8_8 ||
3804 srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
3805 srcType == GL_UNSIGNED_INT_10_10_10_2 ||
3806 srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
3807
3808 /* general solution, no special cases, yet */
3809 {
3810 GLint dstComponents;
3811 GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
3812 GLint dstLuminanceIndex, dstIntensityIndex;
3813 GLfloat rgba[MAX_WIDTH][4];
3814
3815 dstComponents = _mesa_components_in_format( dstFormat );
3816 /* source & dest image formats should have been error checked by now */
3817 assert(dstComponents > 0);
3818
3819 /*
3820 * Extract image data and convert to RGBA floats
3821 */
3822 assert(n <= MAX_WIDTH);
3823 if (srcFormat == GL_COLOR_INDEX) {
3824 GLuint indexes[MAX_WIDTH];
3825 extract_uint_indexes(n, indexes, srcFormat, srcType, source,
3826 srcPacking);
3827
3828 if (dstFormat == GL_COLOR_INDEX) {
3829 GLuint i;
3830 _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
3831 /* convert to GLchan and return */
3832 for (i = 0; i < n; i++) {
3833 dest[i] = (GLchan) (indexes[i] & 0xff);
3834 }
3835 return;
3836 }
3837 else {
3838 /* Convert indexes to RGBA */
3839 if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
3840 shift_and_offset_ci(ctx, n, indexes);
3841 }
3842 _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
3843 }
3844
3845 /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
3846 * with color indexes.
3847 */
3848 transferOps &= ~(IMAGE_SCALE_BIAS_BIT | IMAGE_MAP_COLOR_BIT);
3849 }
3850 else {
3851 /* non-color index data */
3852 extract_float_rgba(n, rgba, srcFormat, srcType, source,
3853 srcPacking->SwapBytes);
3854 }
3855
3856 if (transferOps) {
3857 _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
3858 }
3859
3860 /* Now determine which color channels we need to produce.
3861 * And determine the dest index (offset) within each color tuple.
3862 */
3863 switch (dstFormat) {
3864 case GL_ALPHA:
3865 dstAlphaIndex = 0;
3866 dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
3867 dstLuminanceIndex = dstIntensityIndex = -1;
3868 break;
3869 case GL_LUMINANCE:
3870 dstLuminanceIndex = 0;
3871 dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
3872 dstIntensityIndex = -1;
3873 break;
3874 case GL_LUMINANCE_ALPHA:
3875 dstLuminanceIndex = 0;
3876 dstAlphaIndex = 1;
3877 dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
3878 dstIntensityIndex = -1;
3879 break;
3880 case GL_INTENSITY:
3881 dstIntensityIndex = 0;
3882 dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
3883 dstLuminanceIndex = -1;
3884 break;
3885 case GL_RGB:
3886 dstRedIndex = 0;
3887 dstGreenIndex = 1;
3888 dstBlueIndex = 2;
3889 dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
3890 break;
3891 case GL_RGBA:
3892 dstRedIndex = 0;
3893 dstGreenIndex = 1;
3894 dstBlueIndex = 2;
3895 dstAlphaIndex = 3;
3896 dstLuminanceIndex = dstIntensityIndex = -1;
3897 break;
3898 default:
3899 _mesa_problem(ctx, "bad dstFormat in _mesa_unpack_color_span_float()");
3900 return;
3901 }
3902
3903 /* Now pack results in the requested dstFormat */
3904 if (dstRedIndex >= 0) {
3905 GLfloat *dst = dest;
3906 GLuint i;
3907 for (i = 0; i < n; i++) {
3908 dst[dstRedIndex] = rgba[i][RCOMP];
3909 dst += dstComponents;
3910 }
3911 }
3912
3913 if (dstGreenIndex >= 0) {
3914 GLfloat *dst = dest;
3915 GLuint i;
3916 for (i = 0; i < n; i++) {
3917 dst[dstGreenIndex] = rgba[i][GCOMP];
3918 dst += dstComponents;
3919 }
3920 }
3921
3922 if (dstBlueIndex >= 0) {
3923 GLfloat *dst = dest;
3924 GLuint i;
3925 for (i = 0; i < n; i++) {
3926 dst[dstBlueIndex] = rgba[i][BCOMP];
3927 dst += dstComponents;
3928 }
3929 }
3930
3931 if (dstAlphaIndex >= 0) {
3932 GLfloat *dst = dest;
3933 GLuint i;
3934 for (i = 0; i < n; i++) {
3935 dst[dstAlphaIndex] = rgba[i][ACOMP];
3936 dst += dstComponents;
3937 }
3938 }
3939
3940 if (dstIntensityIndex >= 0) {
3941 GLfloat *dst = dest;
3942 GLuint i;
3943 assert(dstIntensityIndex == 0);
3944 assert(dstComponents == 1);
3945 for (i = 0; i < n; i++) {
3946 /* Intensity comes from red channel */
3947 dst[i] = rgba[i][RCOMP];
3948 }
3949 }
3950
3951 if (dstLuminanceIndex >= 0) {
3952 GLfloat *dst = dest;
3953 GLuint i;
3954 assert(dstLuminanceIndex == 0);
3955 for (i = 0; i < n; i++) {
3956 /* Luminance comes from red channel */
3957 dst[0] = rgba[i][RCOMP];
3958 dst += dstComponents;
3959 }
3960 }
3961 }
3962 }
3963
3964 /**
3965 * Similar to _mesa_unpack_color_span_float(), but for dudv data instead of rgba,
3966 * directly return GLbyte data, no transfer ops apply.
3967 */
3968 void
3969 _mesa_unpack_dudv_span_byte( GLcontext *ctx,
3970 GLuint n, GLenum dstFormat, GLbyte dest[],
3971 GLenum srcFormat, GLenum srcType,
3972 const GLvoid *source,
3973 const struct gl_pixelstore_attrib *srcPacking,
3974 GLbitfield transferOps )
3975 {
3976 ASSERT(dstFormat == GL_DUDV_ATI);
3977 ASSERT(srcFormat == GL_DUDV_ATI);
3978
3979 ASSERT(srcType == GL_UNSIGNED_BYTE ||
3980 srcType == GL_BYTE ||
3981 srcType == GL_UNSIGNED_SHORT ||
3982 srcType == GL_SHORT ||
3983 srcType == GL_UNSIGNED_INT ||
3984 srcType == GL_INT ||
3985 srcType == GL_HALF_FLOAT_ARB ||
3986 srcType == GL_FLOAT);
3987
3988 /* general solution */
3989 {
3990 GLint dstComponents;
3991 GLfloat rgba[MAX_WIDTH][4];
3992 GLbyte *dst = dest;
3993 GLuint i;
3994
3995 dstComponents = _mesa_components_in_format( dstFormat );
3996 /* source & dest image formats should have been error checked by now */
3997 assert(dstComponents > 0);
3998
3999 /*
4000 * Extract image data and convert to RGBA floats
4001 */
4002 assert(n <= MAX_WIDTH);
4003 extract_float_rgba(n, rgba, srcFormat, srcType, source,
4004 srcPacking->SwapBytes);
4005
4006
4007 /* Now determine which color channels we need to produce.
4008 * And determine the dest index (offset) within each color tuple.
4009 */
4010
4011 /* Now pack results in the requested dstFormat */
4012 for (i = 0; i < n; i++) {
4013 /* not sure - need clamp[-1,1] here? */
4014 dst[0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
4015 dst[1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
4016 dst += dstComponents;
4017 }
4018 }
4019 }
4020
4021 /*
4022 * Unpack a row of color index data from a client buffer according to
4023 * the pixel unpacking parameters.
4024 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
4025 *
4026 * Args: ctx - the context
4027 * n - number of pixels
4028 * dstType - destination data type
4029 * dest - destination array
4030 * srcType - source pixel type
4031 * source - source data pointer
4032 * srcPacking - pixel unpacking parameters
4033 * transferOps - the pixel transfer operations to apply
4034 */
4035 void
4036 _mesa_unpack_index_span( const GLcontext *ctx, GLuint n,
4037 GLenum dstType, GLvoid *dest,
4038 GLenum srcType, const GLvoid *source,
4039 const struct gl_pixelstore_attrib *srcPacking,
4040 GLbitfield transferOps )
4041 {
4042 ASSERT(srcType == GL_BITMAP ||
4043 srcType == GL_UNSIGNED_BYTE ||
4044 srcType == GL_BYTE ||
4045 srcType == GL_UNSIGNED_SHORT ||
4046 srcType == GL_SHORT ||
4047 srcType == GL_UNSIGNED_INT ||
4048 srcType == GL_INT ||
4049 srcType == GL_HALF_FLOAT_ARB ||
4050 srcType == GL_FLOAT);
4051
4052 ASSERT(dstType == GL_UNSIGNED_BYTE ||
4053 dstType == GL_UNSIGNED_SHORT ||
4054 dstType == GL_UNSIGNED_INT);
4055
4056
4057 transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
4058
4059 /*
4060 * Try simple cases first
4061 */
4062 if (transferOps == 0 && srcType == GL_UNSIGNED_BYTE
4063 && dstType == GL_UNSIGNED_BYTE) {
4064 _mesa_memcpy(dest, source, n * sizeof(GLubyte));
4065 }
4066 else if (transferOps == 0 && srcType == GL_UNSIGNED_INT
4067 && dstType == GL_UNSIGNED_INT && !srcPacking->SwapBytes) {
4068 _mesa_memcpy(dest, source, n * sizeof(GLuint));
4069 }
4070 else {
4071 /*
4072 * general solution
4073 */
4074 GLuint indexes[MAX_WIDTH];
4075 assert(n <= MAX_WIDTH);
4076
4077 extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
4078 srcPacking);
4079
4080 if (transferOps)
4081 _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
4082
4083 /* convert to dest type */
4084 switch (dstType) {
4085 case GL_UNSIGNED_BYTE:
4086 {
4087 GLubyte *dst = (GLubyte *) dest;
4088 GLuint i;
4089 for (i = 0; i < n; i++) {
4090 dst[i] = (GLubyte) (indexes[i] & 0xff);
4091 }
4092 }
4093 break;
4094 case GL_UNSIGNED_SHORT:
4095 {
4096 GLuint *dst = (GLuint *) dest;
4097 GLuint i;
4098 for (i = 0; i < n; i++) {
4099 dst[i] = (GLushort) (indexes[i] & 0xffff);
4100 }
4101 }
4102 break;
4103 case GL_UNSIGNED_INT:
4104 _mesa_memcpy(dest, indexes, n * sizeof(GLuint));
4105 break;
4106 default:
4107 _mesa_problem(ctx, "bad dstType in _mesa_unpack_index_span");
4108 }
4109 }
4110 }
4111
4112
4113 void
4114 _mesa_pack_index_span( const GLcontext *ctx, GLuint n,
4115 GLenum dstType, GLvoid *dest, const GLuint *source,
4116 const struct gl_pixelstore_attrib *dstPacking,
4117 GLbitfield transferOps )
4118 {
4119 GLuint indexes[MAX_WIDTH];
4120
4121 ASSERT(n <= MAX_WIDTH);
4122
4123 transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
4124
4125 if (transferOps & (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT)) {
4126 /* make a copy of input */
4127 _mesa_memcpy(indexes, source, n * sizeof(GLuint));
4128 _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
4129 source = indexes;
4130 }
4131
4132 switch (dstType) {
4133 case GL_UNSIGNED_BYTE:
4134 {
4135 GLubyte *dst = (GLubyte *) dest;
4136 GLuint i;
4137 for (i = 0; i < n; i++) {
4138 *dst++ = (GLubyte) source[i];
4139 }
4140 }
4141 break;
4142 case GL_BYTE:
4143 {
4144 GLbyte *dst = (GLbyte *) dest;
4145 GLuint i;
4146 for (i = 0; i < n; i++) {
4147 dst[i] = (GLbyte) source[i];
4148 }
4149 }
4150 break;
4151 case GL_UNSIGNED_SHORT:
4152 {
4153 GLushort *dst = (GLushort *) dest;
4154 GLuint i;
4155 for (i = 0; i < n; i++) {
4156 dst[i] = (GLushort) source[i];
4157 }
4158 if (dstPacking->SwapBytes) {
4159 _mesa_swap2( (GLushort *) dst, n );
4160 }
4161 }
4162 break;
4163 case GL_SHORT:
4164 {
4165 GLshort *dst = (GLshort *) dest;
4166 GLuint i;
4167 for (i = 0; i < n; i++) {
4168 dst[i] = (GLshort) source[i];
4169 }
4170 if (dstPacking->SwapBytes) {
4171 _mesa_swap2( (GLushort *) dst, n );
4172 }
4173 }
4174 break;
4175 case GL_UNSIGNED_INT:
4176 {
4177 GLuint *dst = (GLuint *) dest;
4178 GLuint i;
4179 for (i = 0; i < n; i++) {
4180 dst[i] = (GLuint) source[i];
4181 }
4182 if (dstPacking->SwapBytes) {
4183 _mesa_swap4( (GLuint *) dst, n );
4184 }
4185 }
4186 break;
4187 case GL_INT:
4188 {
4189 GLint *dst = (GLint *) dest;
4190 GLuint i;
4191 for (i = 0; i < n; i++) {
4192 dst[i] = (GLint) source[i];
4193 }
4194 if (dstPacking->SwapBytes) {
4195 _mesa_swap4( (GLuint *) dst, n );
4196 }
4197 }
4198 break;
4199 case GL_FLOAT:
4200 {
4201 GLfloat *dst = (GLfloat *) dest;
4202 GLuint i;
4203 for (i = 0; i < n; i++) {
4204 dst[i] = (GLfloat) source[i];
4205 }
4206 if (dstPacking->SwapBytes) {
4207 _mesa_swap4( (GLuint *) dst, n );
4208 }
4209 }
4210 break;
4211 case GL_HALF_FLOAT_ARB:
4212 {
4213 GLhalfARB *dst = (GLhalfARB *) dest;
4214 GLuint i;
4215 for (i = 0; i < n; i++) {
4216 dst[i] = _mesa_float_to_half((GLfloat) source[i]);
4217 }
4218 if (dstPacking->SwapBytes) {
4219 _mesa_swap2( (GLushort *) dst, n );
4220 }
4221 }
4222 break;
4223 default:
4224 _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
4225 }
4226 }
4227
4228
4229 /*
4230 * Unpack a row of stencil data from a client buffer according to
4231 * the pixel unpacking parameters.
4232 * This is (or will be) used by glDrawPixels
4233 *
4234 * Args: ctx - the context
4235 * n - number of pixels
4236 * dstType - destination data type
4237 * dest - destination array
4238 * srcType - source pixel type
4239 * source - source data pointer
4240 * srcPacking - pixel unpacking parameters
4241 * transferOps - apply offset/bias/lookup ops?
4242 */
4243 void
4244 _mesa_unpack_stencil_span( const GLcontext *ctx, GLuint n,
4245 GLenum dstType, GLvoid *dest,
4246 GLenum srcType, const GLvoid *source,
4247 const struct gl_pixelstore_attrib *srcPacking,
4248 GLbitfield transferOps )
4249 {
4250 ASSERT(srcType == GL_BITMAP ||
4251 srcType == GL_UNSIGNED_BYTE ||
4252 srcType == GL_BYTE ||
4253 srcType == GL_UNSIGNED_SHORT ||
4254 srcType == GL_SHORT ||
4255 srcType == GL_UNSIGNED_INT ||
4256 srcType == GL_INT ||
4257 srcType == GL_UNSIGNED_INT_24_8_EXT ||
4258 srcType == GL_HALF_FLOAT_ARB ||
4259 srcType == GL_FLOAT);
4260
4261 ASSERT(dstType == GL_UNSIGNED_BYTE ||
4262 dstType == GL_UNSIGNED_SHORT ||
4263 dstType == GL_UNSIGNED_INT);
4264
4265 /* only shift and offset apply to stencil */
4266 transferOps &= IMAGE_SHIFT_OFFSET_BIT;
4267
4268 /*
4269 * Try simple cases first
4270 */
4271 if (transferOps == 0 &&
4272 !ctx->Pixel.MapStencilFlag &&
4273 srcType == GL_UNSIGNED_BYTE &&
4274 dstType == GL_UNSIGNED_BYTE) {
4275 _mesa_memcpy(dest, source, n * sizeof(GLubyte));
4276 }
4277 else if (transferOps == 0 &&
4278 !ctx->Pixel.MapStencilFlag &&
4279 srcType == GL_UNSIGNED_INT &&
4280 dstType == GL_UNSIGNED_INT &&
4281 !srcPacking->SwapBytes) {
4282 _mesa_memcpy(dest, source, n * sizeof(GLuint));
4283 }
4284 else {
4285 /*
4286 * general solution
4287 */
4288 GLuint indexes[MAX_WIDTH];
4289 assert(n <= MAX_WIDTH);
4290
4291 extract_uint_indexes(n, indexes, GL_STENCIL_INDEX, srcType, source,
4292 srcPacking);
4293
4294 if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
4295 /* shift and offset indexes */
4296 shift_and_offset_ci(ctx, n, indexes);
4297 }
4298
4299 if (ctx->Pixel.MapStencilFlag) {
4300 /* Apply stencil lookup table */
4301 const GLuint mask = ctx->PixelMaps.StoS.Size - 1;
4302 GLuint i;
4303 for (i = 0; i < n; i++) {
4304 indexes[i] = (GLuint)ctx->PixelMaps.StoS.Map[ indexes[i] & mask ];
4305 }
4306 }
4307
4308 /* convert to dest type */
4309 switch (dstType) {
4310 case GL_UNSIGNED_BYTE:
4311 {
4312 GLubyte *dst = (GLubyte *) dest;
4313 GLuint i;
4314 for (i = 0; i < n; i++) {
4315 dst[i] = (GLubyte) (indexes[i] & 0xff);
4316 }
4317 }
4318 break;
4319 case GL_UNSIGNED_SHORT:
4320 {
4321 GLuint *dst = (GLuint *) dest;
4322 GLuint i;
4323 for (i = 0; i < n; i++) {
4324 dst[i] = (GLushort) (indexes[i] & 0xffff);
4325 }
4326 }
4327 break;
4328 case GL_UNSIGNED_INT:
4329 _mesa_memcpy(dest, indexes, n * sizeof(GLuint));
4330 break;
4331 default:
4332 _mesa_problem(ctx, "bad dstType in _mesa_unpack_stencil_span");
4333 }
4334 }
4335 }
4336
4337
4338 void
4339 _mesa_pack_stencil_span( const GLcontext *ctx, GLuint n,
4340 GLenum dstType, GLvoid *dest, const GLstencil *source,
4341 const struct gl_pixelstore_attrib *dstPacking )
4342 {
4343 GLstencil stencil[MAX_WIDTH];
4344
4345 ASSERT(n <= MAX_WIDTH);
4346
4347 if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset ||
4348 ctx->Pixel.MapStencilFlag) {
4349 /* make a copy of input */
4350 _mesa_memcpy(stencil, source, n * sizeof(GLstencil));
4351 _mesa_apply_stencil_transfer_ops(ctx, n, stencil);
4352 source = stencil;
4353 }
4354
4355 switch (dstType) {
4356 case GL_UNSIGNED_BYTE:
4357 if (sizeof(GLstencil) == 1) {
4358 _mesa_memcpy( dest, source, n );
4359 }
4360 else {
4361 GLubyte *dst = (GLubyte *) dest;
4362 GLuint i;
4363 for (i=0;i<n;i++) {
4364 dst[i] = (GLubyte) source[i];
4365 }
4366 }
4367 break;
4368 case GL_BYTE:
4369 {
4370 GLbyte *dst = (GLbyte *) dest;
4371 GLuint i;
4372 for (i=0;i<n;i++) {
4373 dst[i] = (GLbyte) (source[i] & 0x7f);
4374 }
4375 }
4376 break;
4377 case GL_UNSIGNED_SHORT:
4378 {
4379 GLushort *dst = (GLushort *) dest;
4380 GLuint i;
4381 for (i=0;i<n;i++) {
4382 dst[i] = (GLushort) source[i];
4383 }
4384 if (dstPacking->SwapBytes) {
4385 _mesa_swap2( (GLushort *) dst, n );
4386 }
4387 }
4388 break;
4389 case GL_SHORT:
4390 {
4391 GLshort *dst = (GLshort *) dest;
4392 GLuint i;
4393 for (i=0;i<n;i++) {
4394 dst[i] = (GLshort) source[i];
4395 }
4396 if (dstPacking->SwapBytes) {
4397 _mesa_swap2( (GLushort *) dst, n );
4398 }
4399 }
4400 break;
4401 case GL_UNSIGNED_INT:
4402 {
4403 GLuint *dst = (GLuint *) dest;
4404 GLuint i;
4405 for (i=0;i<n;i++) {
4406 dst[i] = (GLuint) source[i];
4407 }
4408 if (dstPacking->SwapBytes) {
4409 _mesa_swap4( (GLuint *) dst, n );
4410 }
4411 }
4412 break;
4413 case GL_INT:
4414 {
4415 GLint *dst = (GLint *) dest;
4416 GLuint i;
4417 for (i=0;i<n;i++) {
4418 dst[i] = (GLint) source[i];
4419 }
4420 if (dstPacking->SwapBytes) {
4421 _mesa_swap4( (GLuint *) dst, n );
4422 }
4423 }
4424 break;
4425 case GL_FLOAT:
4426 {
4427 GLfloat *dst = (GLfloat *) dest;
4428 GLuint i;
4429 for (i=0;i<n;i++) {
4430 dst[i] = (GLfloat) source[i];
4431 }
4432 if (dstPacking->SwapBytes) {
4433 _mesa_swap4( (GLuint *) dst, n );
4434 }
4435 }
4436 break;
4437 case GL_HALF_FLOAT_ARB:
4438 {
4439 GLhalfARB *dst = (GLhalfARB *) dest;
4440 GLuint i;
4441 for (i=0;i<n;i++) {
4442 dst[i] = _mesa_float_to_half( (float) source[i] );
4443 }
4444 if (dstPacking->SwapBytes) {
4445 _mesa_swap2( (GLushort *) dst, n );
4446 }
4447 }
4448 break;
4449 case GL_BITMAP:
4450 if (dstPacking->LsbFirst) {
4451 GLubyte *dst = (GLubyte *) dest;
4452 GLint shift = 0;
4453 GLuint i;
4454 for (i = 0; i < n; i++) {
4455 if (shift == 0)
4456 *dst = 0;
4457 *dst |= ((source[i] != 0) << shift);
4458 shift++;
4459 if (shift == 8) {
4460 shift = 0;
4461 dst++;
4462 }
4463 }
4464 }
4465 else {
4466 GLubyte *dst = (GLubyte *) dest;
4467 GLint shift = 7;
4468 GLuint i;
4469 for (i = 0; i < n; i++) {
4470 if (shift == 7)
4471 *dst = 0;
4472 *dst |= ((source[i] != 0) << shift);
4473 shift--;
4474 if (shift < 0) {
4475 shift = 7;
4476 dst++;
4477 }
4478 }
4479 }
4480 break;
4481 default:
4482 _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
4483 }
4484 }
4485
4486 #define DEPTH_VALUES(GLTYPE, GLTYPE2FLOAT) \
4487 do { \
4488 GLuint i; \
4489 const GLTYPE *src = (const GLTYPE *)source; \
4490 for (i = 0; i < n; i++) { \
4491 GLTYPE value = src[i]; \
4492 if (srcPacking->SwapBytes) { \
4493 if (sizeof(GLTYPE) == 2) { \
4494 SWAP2BYTE(value); \
4495 } else if (sizeof(GLTYPE) == 4) { \
4496 SWAP4BYTE(value); \
4497 } \
4498 } \
4499 depthValues[i] = GLTYPE2FLOAT(value); \
4500 } \
4501 } while (0)
4502
4503
4504 /**
4505 * Unpack a row of depth/z values from memory, returning GLushort, GLuint
4506 * or GLfloat values.
4507 * The glPixelTransfer (scale/bias) params will be applied.
4508 *
4509 * \param dstType one of GL_UNSIGNED_SHORT, GL_UNSIGNED_INT, GL_FLOAT
4510 * \param depthMax max value for returned GLushort or GLuint values
4511 * (ignored for GLfloat).
4512 */
4513 void
4514 _mesa_unpack_depth_span( const GLcontext *ctx, GLuint n,
4515 GLenum dstType, GLvoid *dest, GLuint depthMax,
4516 GLenum srcType, const GLvoid *source,
4517 const struct gl_pixelstore_attrib *srcPacking )
4518 {
4519 GLfloat depthTemp[MAX_WIDTH], *depthValues;
4520 GLboolean needClamp = GL_FALSE;
4521
4522 /* Look for special cases first.
4523 * Not only are these faster, they're less prone to numeric conversion
4524 * problems. Otherwise, converting from an int type to a float then
4525 * back to an int type can introduce errors that will show up as
4526 * artifacts in things like depth peeling which uses glCopyTexImage.
4527 */
4528 if (ctx->Pixel.DepthScale == 1.0 && ctx->Pixel.DepthBias == 0.0) {
4529 if (srcType == GL_UNSIGNED_INT && dstType == GL_UNSIGNED_SHORT) {
4530 const GLuint *src = (const GLuint *) source;
4531 GLushort *dst = (GLushort *) dest;
4532 GLuint i;
4533 for (i = 0; i < n; i++) {
4534 dst[i] = src[i] >> 16;
4535 }
4536 return;
4537 }
4538 if (srcType == GL_UNSIGNED_SHORT
4539 && dstType == GL_UNSIGNED_INT
4540 && depthMax == 0xffffffff) {
4541 const GLushort *src = (const GLushort *) source;
4542 GLuint *dst = (GLuint *) dest;
4543 GLuint i;
4544 for (i = 0; i < n; i++) {
4545 dst[i] = src[i] | (src[i] << 16);
4546 }
4547 return;
4548 }
4549 if (srcType == GL_UNSIGNED_INT_24_8
4550 && dstType == GL_UNSIGNED_INT
4551 && depthMax == 0xffffff) {
4552 const GLuint *src = (const GLuint *) source;
4553 GLuint *dst = (GLuint *) dest;
4554 GLuint i;
4555 for (i = 0; i < n; i++) {
4556 dst[i] = src[i] >> 8;
4557 }
4558 return;
4559 }
4560 /* XXX may want to add additional cases here someday */
4561 }
4562
4563 /* general case path follows */
4564
4565 if (dstType == GL_FLOAT) {
4566 depthValues = (GLfloat *) dest;
4567 }
4568 else {
4569 depthValues = depthTemp;
4570 }
4571
4572 /* Convert incoming values to GLfloat. Some conversions will require
4573 * clamping, below.
4574 */
4575 switch (srcType) {
4576 case GL_BYTE:
4577 DEPTH_VALUES(GLbyte, BYTE_TO_FLOAT);
4578 needClamp = GL_TRUE;
4579 break;
4580 case GL_UNSIGNED_BYTE:
4581 DEPTH_VALUES(GLubyte, UBYTE_TO_FLOAT);
4582 break;
4583 case GL_SHORT:
4584 DEPTH_VALUES(GLshort, SHORT_TO_FLOAT);
4585 needClamp = GL_TRUE;
4586 break;
4587 case GL_UNSIGNED_SHORT:
4588 DEPTH_VALUES(GLushort, USHORT_TO_FLOAT);
4589 break;
4590 case GL_INT:
4591 DEPTH_VALUES(GLint, INT_TO_FLOAT);
4592 needClamp = GL_TRUE;
4593 break;
4594 case GL_UNSIGNED_INT:
4595 DEPTH_VALUES(GLuint, UINT_TO_FLOAT);
4596 break;
4597 case GL_UNSIGNED_INT_24_8_EXT: /* GL_EXT_packed_depth_stencil */
4598 if (dstType == GL_UNSIGNED_INT_24_8_EXT &&
4599 depthMax == 0xffffff &&
4600 ctx->Pixel.DepthScale == 1.0 &&
4601 ctx->Pixel.DepthBias == 0.0) {
4602 const GLuint *src = (const GLuint *) source;
4603 GLuint *zValues = (GLuint *) dest;
4604 GLuint i;
4605 for (i = 0; i < n; i++) {
4606 GLuint value = src[i];
4607 if (srcPacking->SwapBytes) {
4608 SWAP4BYTE(value);
4609 }
4610 zValues[i] = value & 0xffffff00;
4611 }
4612 return;
4613 }
4614 else {
4615 const GLuint *src = (const GLuint *) source;
4616 const GLfloat scale = 1.0f / 0xffffff;
4617 GLuint i;
4618 for (i = 0; i < n; i++) {
4619 GLuint value = src[i];
4620 if (srcPacking->SwapBytes) {
4621 SWAP4BYTE(value);
4622 }
4623 depthValues[i] = (value >> 8) * scale;
4624 }
4625 }
4626 break;
4627 case GL_FLOAT:
4628 DEPTH_VALUES(GLfloat, 1*);
4629 needClamp = GL_TRUE;
4630 break;
4631 case GL_HALF_FLOAT_ARB:
4632 {
4633 GLuint i;
4634 const GLhalfARB *src = (const GLhalfARB *) source;
4635 for (i = 0; i < n; i++) {
4636 GLhalfARB value = src[i];
4637 if (srcPacking->SwapBytes) {
4638 SWAP2BYTE(value);
4639 }
4640 depthValues[i] = _mesa_half_to_float(value);
4641 }
4642 needClamp = GL_TRUE;
4643 }
4644 break;
4645 default:
4646 _mesa_problem(NULL, "bad type in _mesa_unpack_depth_span()");
4647 return;
4648 }
4649
4650 /* apply depth scale and bias */
4651 {
4652 const GLfloat scale = ctx->Pixel.DepthScale;
4653 const GLfloat bias = ctx->Pixel.DepthBias;
4654 if (scale != 1.0 || bias != 0.0) {
4655 GLuint i;
4656 for (i = 0; i < n; i++) {
4657 depthValues[i] = depthValues[i] * scale + bias;
4658 }
4659 needClamp = GL_TRUE;
4660 }
4661 }
4662
4663 /* clamp to [0, 1] */
4664 if (needClamp) {
4665 GLuint i;
4666 for (i = 0; i < n; i++) {
4667 depthValues[i] = (GLfloat)CLAMP(depthValues[i], 0.0, 1.0);
4668 }
4669 }
4670
4671 /*
4672 * Convert values to dstType
4673 */
4674 if (dstType == GL_UNSIGNED_INT) {
4675 GLuint *zValues = (GLuint *) dest;
4676 GLuint i;
4677 if (depthMax <= 0xffffff) {
4678 /* no overflow worries */
4679 for (i = 0; i < n; i++) {
4680 zValues[i] = (GLuint) (depthValues[i] * (GLfloat) depthMax);
4681 }
4682 }
4683 else {
4684 /* need to use double precision to prevent overflow problems */
4685 for (i = 0; i < n; i++) {
4686 GLdouble z = depthValues[i] * (GLfloat) depthMax;
4687 if (z >= (GLdouble) 0xffffffff)
4688 zValues[i] = 0xffffffff;
4689 else
4690 zValues[i] = (GLuint) z;
4691 }
4692 }
4693 }
4694 else if (dstType == GL_UNSIGNED_SHORT) {
4695 GLushort *zValues = (GLushort *) dest;
4696 GLuint i;
4697 ASSERT(depthMax <= 0xffff);
4698 for (i = 0; i < n; i++) {
4699 zValues[i] = (GLushort) (depthValues[i] * (GLfloat) depthMax);
4700 }
4701 }
4702 else {
4703 ASSERT(dstType == GL_FLOAT);
4704 /*ASSERT(depthMax == 1.0F);*/
4705 }
4706 }
4707
4708
4709 /*
4710 * Pack an array of depth values. The values are floats in [0,1].
4711 */
4712 void
4713 _mesa_pack_depth_span( const GLcontext *ctx, GLuint n, GLvoid *dest,
4714 GLenum dstType, const GLfloat *depthSpan,
4715 const struct gl_pixelstore_attrib *dstPacking )
4716 {
4717 GLfloat depthCopy[MAX_WIDTH];
4718
4719 ASSERT(n <= MAX_WIDTH);
4720
4721 if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
4722 _mesa_memcpy(depthCopy, depthSpan, n * sizeof(GLfloat));
4723 _mesa_scale_and_bias_depth(ctx, n, depthCopy);
4724 depthSpan = depthCopy;
4725 }
4726
4727 switch (dstType) {
4728 case GL_UNSIGNED_BYTE:
4729 {
4730 GLubyte *dst = (GLubyte *) dest;
4731 GLuint i;
4732 for (i = 0; i < n; i++) {
4733 dst[i] = FLOAT_TO_UBYTE( depthSpan[i] );
4734 }
4735 }
4736 break;
4737 case GL_BYTE:
4738 {
4739 GLbyte *dst = (GLbyte *) dest;
4740 GLuint i;
4741 for (i = 0; i < n; i++) {
4742 dst[i] = FLOAT_TO_BYTE( depthSpan[i] );
4743 }
4744 }
4745 break;
4746 case GL_UNSIGNED_SHORT:
4747 {
4748 GLushort *dst = (GLushort *) dest;
4749 GLuint i;
4750 for (i = 0; i < n; i++) {
4751 CLAMPED_FLOAT_TO_USHORT(dst[i], depthSpan[i]);
4752 }
4753 if (dstPacking->SwapBytes) {
4754 _mesa_swap2( (GLushort *) dst, n );
4755 }
4756 }
4757 break;
4758 case GL_SHORT:
4759 {
4760 GLshort *dst = (GLshort *) dest;
4761 GLuint i;
4762 for (i = 0; i < n; i++) {
4763 dst[i] = FLOAT_TO_SHORT( depthSpan[i] );
4764 }
4765 if (dstPacking->SwapBytes) {
4766 _mesa_swap2( (GLushort *) dst, n );
4767 }
4768 }
4769 break;
4770 case GL_UNSIGNED_INT:
4771 {
4772 GLuint *dst = (GLuint *) dest;
4773 GLuint i;
4774 for (i = 0; i < n; i++) {
4775 dst[i] = FLOAT_TO_UINT( depthSpan[i] );
4776 }
4777 if (dstPacking->SwapBytes) {
4778 _mesa_swap4( (GLuint *) dst, n );
4779 }
4780 }
4781 break;
4782 case GL_INT:
4783 {
4784 GLint *dst = (GLint *) dest;
4785 GLuint i;
4786 for (i = 0; i < n; i++) {
4787 dst[i] = FLOAT_TO_INT( depthSpan[i] );
4788 }
4789 if (dstPacking->SwapBytes) {
4790 _mesa_swap4( (GLuint *) dst, n );
4791 }
4792 }
4793 break;
4794 case GL_FLOAT:
4795 {
4796 GLfloat *dst = (GLfloat *) dest;
4797 GLuint i;
4798 for (i = 0; i < n; i++) {
4799 dst[i] = depthSpan[i];
4800 }
4801 if (dstPacking->SwapBytes) {
4802 _mesa_swap4( (GLuint *) dst, n );
4803 }
4804 }
4805 break;
4806 case GL_HALF_FLOAT_ARB:
4807 {
4808 GLhalfARB *dst = (GLhalfARB *) dest;
4809 GLuint i;
4810 for (i = 0; i < n; i++) {
4811 dst[i] = _mesa_float_to_half(depthSpan[i]);
4812 }
4813 if (dstPacking->SwapBytes) {
4814 _mesa_swap2( (GLushort *) dst, n );
4815 }
4816 }
4817 break;
4818 default:
4819 _mesa_problem(ctx, "bad type in _mesa_pack_depth_span");
4820 }
4821 }
4822
4823
4824
4825 /**
4826 * Pack depth and stencil values as GL_DEPTH_STENCIL/GL_UNSIGNED_INT_24_8.
4827 */
4828 void
4829 _mesa_pack_depth_stencil_span(const GLcontext *ctx, GLuint n, GLuint *dest,
4830 const GLfloat *depthVals,
4831 const GLstencil *stencilVals,
4832 const struct gl_pixelstore_attrib *dstPacking)
4833 {
4834 GLfloat depthCopy[MAX_WIDTH];
4835 GLstencil stencilCopy[MAX_WIDTH];
4836 GLuint i;
4837
4838 ASSERT(n <= MAX_WIDTH);
4839
4840 if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
4841 _mesa_memcpy(depthCopy, depthVals, n * sizeof(GLfloat));
4842 _mesa_scale_and_bias_depth(ctx, n, depthCopy);
4843 depthVals = depthCopy;
4844 }
4845
4846 if (ctx->Pixel.IndexShift ||
4847 ctx->Pixel.IndexOffset ||
4848 ctx->Pixel.MapStencilFlag) {
4849 _mesa_memcpy(stencilCopy, stencilVals, n * sizeof(GLstencil));
4850 _mesa_apply_stencil_transfer_ops(ctx, n, stencilCopy);
4851 stencilVals = stencilCopy;
4852 }
4853
4854 for (i = 0; i < n; i++) {
4855 GLuint z = (GLuint) (depthVals[i] * 0xffffff);
4856 dest[i] = (z << 8) | (stencilVals[i] & 0xff);
4857 }
4858
4859 if (dstPacking->SwapBytes) {
4860 _mesa_swap4(dest, n);
4861 }
4862 }
4863
4864
4865
4866
4867 /**
4868 * Unpack image data. Apply byte swapping, byte flipping (bitmap).
4869 * Return all image data in a contiguous block. This is used when we
4870 * compile glDrawPixels, glTexImage, etc into a display list. We
4871 * need a copy of the data in a standard format.
4872 */
4873 void *
4874 _mesa_unpack_image( GLuint dimensions,
4875 GLsizei width, GLsizei height, GLsizei depth,
4876 GLenum format, GLenum type, const GLvoid *pixels,
4877 const struct gl_pixelstore_attrib *unpack )
4878 {
4879 GLint bytesPerRow, compsPerRow;
4880 GLboolean flipBytes, swap2, swap4;
4881
4882 if (!pixels)
4883 return NULL; /* not necessarily an error */
4884
4885 if (width <= 0 || height <= 0 || depth <= 0)
4886 return NULL; /* generate error later */
4887
4888 if (type == GL_BITMAP) {
4889 bytesPerRow = (width + 7) >> 3;
4890 flipBytes = unpack->LsbFirst;
4891 swap2 = swap4 = GL_FALSE;
4892 compsPerRow = 0;
4893 }
4894 else {
4895 const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
4896 GLint components = _mesa_components_in_format(format);
4897 GLint bytesPerComp;
4898
4899 if (_mesa_type_is_packed(type))
4900 components = 1;
4901
4902 if (bytesPerPixel <= 0 || components <= 0)
4903 return NULL; /* bad format or type. generate error later */
4904 bytesPerRow = bytesPerPixel * width;
4905 bytesPerComp = bytesPerPixel / components;
4906 flipBytes = GL_FALSE;
4907 swap2 = (bytesPerComp == 2) && unpack->SwapBytes;
4908 swap4 = (bytesPerComp == 4) && unpack->SwapBytes;
4909 compsPerRow = components * width;
4910 assert(compsPerRow >= width);
4911 }
4912
4913 {
4914 GLubyte *destBuffer
4915 = (GLubyte *) _mesa_malloc(bytesPerRow * height * depth);
4916 GLubyte *dst;
4917 GLint img, row;
4918 if (!destBuffer)
4919 return NULL; /* generate GL_OUT_OF_MEMORY later */
4920
4921 dst = destBuffer;
4922 for (img = 0; img < depth; img++) {
4923 for (row = 0; row < height; row++) {
4924 const GLvoid *src = _mesa_image_address(dimensions, unpack, pixels,
4925 width, height, format, type, img, row, 0);
4926
4927 if ((type == GL_BITMAP) && (unpack->SkipPixels & 0x7)) {
4928 GLint i;
4929 flipBytes = GL_FALSE;
4930 if (unpack->LsbFirst) {
4931 GLubyte srcMask = 1 << (unpack->SkipPixels & 0x7);
4932 GLubyte dstMask = 128;
4933 const GLubyte *s = src;
4934 GLubyte *d = dst;
4935 *d = 0;
4936 for (i = 0; i < width; i++) {
4937 if (*s & srcMask) {
4938 *d |= dstMask;
4939 }
4940 if (srcMask == 128) {
4941 srcMask = 1;
4942 s++;
4943 }
4944 else {
4945 srcMask = srcMask << 1;
4946 }
4947 if (dstMask == 1) {
4948 dstMask = 128;
4949 d++;
4950 *d = 0;
4951 }
4952 else {
4953 dstMask = dstMask >> 1;
4954 }
4955 }
4956 }
4957 else {
4958 GLubyte srcMask = 128 >> (unpack->SkipPixels & 0x7);
4959 GLubyte dstMask = 128;
4960 const GLubyte *s = src;
4961 GLubyte *d = dst;
4962 *d = 0;
4963 for (i = 0; i < width; i++) {
4964 if (*s & srcMask) {
4965 *d |= dstMask;
4966 }
4967 if (srcMask == 1) {
4968 srcMask = 128;
4969 s++;
4970 }
4971 else {
4972 srcMask = srcMask >> 1;
4973 }
4974 if (dstMask == 1) {
4975 dstMask = 128;
4976 d++;
4977 *d = 0;
4978 }
4979 else {
4980 dstMask = dstMask >> 1;
4981 }
4982 }
4983 }
4984 }
4985 else {
4986 _mesa_memcpy(dst, src, bytesPerRow);
4987 }
4988
4989 /* byte flipping/swapping */
4990 if (flipBytes) {
4991 flip_bytes((GLubyte *) dst, bytesPerRow);
4992 }
4993 else if (swap2) {
4994 _mesa_swap2((GLushort*) dst, compsPerRow);
4995 }
4996 else if (swap4) {
4997 _mesa_swap4((GLuint*) dst, compsPerRow);
4998 }
4999 dst += bytesPerRow;
5000 }
5001 }
5002 return destBuffer;
5003 }
5004 }
5005
5006 #endif /* _HAVE_FULL_GL */
5007
5008
5009
5010 /**
5011 * Convert an array of RGBA colors from one datatype to another.
5012 * NOTE: src may equal dst. In that case, we use a temporary buffer.
5013 */
5014 void
5015 _mesa_convert_colors(GLenum srcType, const GLvoid *src,
5016 GLenum dstType, GLvoid *dst,
5017 GLuint count, const GLubyte mask[])
5018 {
5019 GLuint tempBuffer[MAX_WIDTH][4];
5020 const GLboolean useTemp = (src == dst);
5021
5022 ASSERT(srcType != dstType);
5023
5024 switch (srcType) {
5025 case GL_UNSIGNED_BYTE:
5026 if (dstType == GL_UNSIGNED_SHORT) {
5027 const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src;
5028 GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst);
5029 GLuint i;
5030 for (i = 0; i < count; i++) {
5031 if (!mask || mask[i]) {
5032 dst2[i][RCOMP] = UBYTE_TO_USHORT(src1[i][RCOMP]);
5033 dst2[i][GCOMP] = UBYTE_TO_USHORT(src1[i][GCOMP]);
5034 dst2[i][BCOMP] = UBYTE_TO_USHORT(src1[i][BCOMP]);
5035 dst2[i][ACOMP] = UBYTE_TO_USHORT(src1[i][ACOMP]);
5036 }
5037 }
5038 if (useTemp)
5039 _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort));
5040 }
5041 else {
5042 const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src;
5043 GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst);
5044 GLuint i;
5045 ASSERT(dstType == GL_FLOAT);
5046 for (i = 0; i < count; i++) {
5047 if (!mask || mask[i]) {
5048 dst4[i][RCOMP] = UBYTE_TO_FLOAT(src1[i][RCOMP]);
5049 dst4[i][GCOMP] = UBYTE_TO_FLOAT(src1[i][GCOMP]);
5050 dst4[i][BCOMP] = UBYTE_TO_FLOAT(src1[i][BCOMP]);
5051 dst4[i][ACOMP] = UBYTE_TO_FLOAT(src1[i][ACOMP]);
5052 }
5053 }
5054 if (useTemp)
5055 _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat));
5056 }
5057 break;
5058 case GL_UNSIGNED_SHORT:
5059 if (dstType == GL_UNSIGNED_BYTE) {
5060 const GLushort (*src2)[4] = (const GLushort (*)[4]) src;
5061 GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst);
5062 GLuint i;
5063 for (i = 0; i < count; i++) {
5064 if (!mask || mask[i]) {
5065 dst1[i][RCOMP] = USHORT_TO_UBYTE(src2[i][RCOMP]);
5066 dst1[i][GCOMP] = USHORT_TO_UBYTE(src2[i][GCOMP]);
5067 dst1[i][BCOMP] = USHORT_TO_UBYTE(src2[i][BCOMP]);
5068 dst1[i][ACOMP] = USHORT_TO_UBYTE(src2[i][ACOMP]);
5069 }
5070 }
5071 if (useTemp)
5072 _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte));
5073 }
5074 else {
5075 const GLushort (*src2)[4] = (const GLushort (*)[4]) src;
5076 GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst);
5077 GLuint i;
5078 ASSERT(dstType == GL_FLOAT);
5079 for (i = 0; i < count; i++) {
5080 if (!mask || mask[i]) {
5081 dst4[i][RCOMP] = USHORT_TO_FLOAT(src2[i][RCOMP]);
5082 dst4[i][GCOMP] = USHORT_TO_FLOAT(src2[i][GCOMP]);
5083 dst4[i][BCOMP] = USHORT_TO_FLOAT(src2[i][BCOMP]);
5084 dst4[i][ACOMP] = USHORT_TO_FLOAT(src2[i][ACOMP]);
5085 }
5086 }
5087 if (useTemp)
5088 _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat));
5089 }
5090 break;
5091 case GL_FLOAT:
5092 if (dstType == GL_UNSIGNED_BYTE) {
5093 const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src;
5094 GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst);
5095 GLuint i;
5096 for (i = 0; i < count; i++) {
5097 if (!mask || mask[i]) {
5098 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][RCOMP], src4[i][RCOMP]);
5099 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][GCOMP], src4[i][GCOMP]);
5100 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][BCOMP], src4[i][BCOMP]);
5101 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][ACOMP], src4[i][ACOMP]);
5102 }
5103 }
5104 if (useTemp)
5105 _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte));
5106 }
5107 else {
5108 const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src;
5109 GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst);
5110 GLuint i;
5111 ASSERT(dstType == GL_UNSIGNED_SHORT);
5112 for (i = 0; i < count; i++) {
5113 if (!mask || mask[i]) {
5114 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][RCOMP], src4[i][RCOMP]);
5115 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][GCOMP], src4[i][GCOMP]);
5116 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][BCOMP], src4[i][BCOMP]);
5117 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][ACOMP], src4[i][ACOMP]);
5118 }
5119 }
5120 if (useTemp)
5121 _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort));
5122 }
5123 break;
5124 default:
5125 _mesa_problem(NULL, "Invalid datatype in _mesa_convert_colors");
5126 }
5127 }
5128
5129
5130
5131
5132 /**
5133 * Perform basic clipping for glDrawPixels. The image's position and size
5134 * and the unpack SkipPixels and SkipRows are adjusted so that the image
5135 * region is entirely within the window and scissor bounds.
5136 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
5137 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
5138 * we'll actually write. Beforehand, *destY-1 is the first drawing row.
5139 *
5140 * \return GL_TRUE if image is ready for drawing or
5141 * GL_FALSE if image was completely clipped away (draw nothing)
5142 */
5143 GLboolean
5144 _mesa_clip_drawpixels(const GLcontext *ctx,
5145 GLint *destX, GLint *destY,
5146 GLsizei *width, GLsizei *height,
5147 struct gl_pixelstore_attrib *unpack)
5148 {
5149 const GLframebuffer *buffer = ctx->DrawBuffer;
5150
5151 if (unpack->RowLength == 0) {
5152 unpack->RowLength = *width;
5153 }
5154
5155 ASSERT(ctx->Pixel.ZoomX == 1.0F);
5156 ASSERT(ctx->Pixel.ZoomY == 1.0F || ctx->Pixel.ZoomY == -1.0F);
5157
5158 /* left clipping */
5159 if (*destX < buffer->_Xmin) {
5160 unpack->SkipPixels += (buffer->_Xmin - *destX);
5161 *width -= (buffer->_Xmin - *destX);
5162 *destX = buffer->_Xmin;
5163 }
5164 /* right clipping */
5165 if (*destX + *width > buffer->_Xmax)
5166 *width -= (*destX + *width - buffer->_Xmax);
5167
5168 if (*width <= 0)
5169 return GL_FALSE;
5170
5171 if (ctx->Pixel.ZoomY == 1.0F) {
5172 /* bottom clipping */
5173 if (*destY < buffer->_Ymin) {
5174 unpack->SkipRows += (buffer->_Ymin - *destY);
5175 *height -= (buffer->_Ymin - *destY);
5176 *destY = buffer->_Ymin;
5177 }
5178 /* top clipping */
5179 if (*destY + *height > buffer->_Ymax)
5180 *height -= (*destY + *height - buffer->_Ymax);
5181 }
5182 else { /* upside down */
5183 /* top clipping */
5184 if (*destY > buffer->_Ymax) {
5185 unpack->SkipRows += (*destY - buffer->_Ymax);
5186 *height -= (*destY - buffer->_Ymax);
5187 *destY = buffer->_Ymax;
5188 }
5189 /* bottom clipping */
5190 if (*destY - *height < buffer->_Ymin)
5191 *height -= (buffer->_Ymin - (*destY - *height));
5192 /* adjust destY so it's the first row to write to */
5193 (*destY)--;
5194 }
5195
5196 if (*height <= 0)
5197 return GL_TRUE;
5198
5199 return GL_TRUE;
5200 }
5201
5202
5203 /**
5204 * Perform clipping for glReadPixels. The image's window position
5205 * and size, and the pack skipPixels, skipRows and rowLength are adjusted
5206 * so that the image region is entirely within the window bounds.
5207 * Note: this is different from _mesa_clip_drawpixels() in that the
5208 * scissor box is ignored, and we use the bounds of the current readbuffer
5209 * surface.
5210 *
5211 * \return GL_TRUE if image is ready for drawing or
5212 * GL_FALSE if image was completely clipped away (draw nothing)
5213 */
5214 GLboolean
5215 _mesa_clip_readpixels(const GLcontext *ctx,
5216 GLint *srcX, GLint *srcY,
5217 GLsizei *width, GLsizei *height,
5218 struct gl_pixelstore_attrib *pack)
5219 {
5220 const GLframebuffer *buffer = ctx->ReadBuffer;
5221
5222 if (pack->RowLength == 0) {
5223 pack->RowLength = *width;
5224 }
5225
5226 /* left clipping */
5227 if (*srcX < 0) {
5228 pack->SkipPixels += (0 - *srcX);
5229 *width -= (0 - *srcX);
5230 *srcX = 0;
5231 }
5232 /* right clipping */
5233 if (*srcX + *width > (GLsizei) buffer->Width)
5234 *width -= (*srcX + *width - buffer->Width);
5235
5236 if (*width <= 0)
5237 return GL_FALSE;
5238
5239 /* bottom clipping */
5240 if (*srcY < 0) {
5241 pack->SkipRows += (0 - *srcY);
5242 *height -= (0 - *srcY);
5243 *srcY = 0;
5244 }
5245 /* top clipping */
5246 if (*srcY + *height > (GLsizei) buffer->Height)
5247 *height -= (*srcY + *height - buffer->Height);
5248
5249 if (*height <= 0)
5250 return GL_TRUE;
5251
5252 return GL_TRUE;
5253 }
5254
5255
5256 /**
5257 * Do clipping for a glCopyTexSubImage call.
5258 * The framebuffer source region might extend outside the framebuffer
5259 * bounds. Clip the source region against the framebuffer bounds and
5260 * adjust the texture/dest position and size accordingly.
5261 *
5262 * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise.
5263 */
5264 GLboolean
5265 _mesa_clip_copytexsubimage(const GLcontext *ctx,
5266 GLint *destX, GLint *destY,
5267 GLint *srcX, GLint *srcY,
5268 GLsizei *width, GLsizei *height)
5269 {
5270 const struct gl_framebuffer *fb = ctx->ReadBuffer;
5271 const GLint srcX0 = *srcX, srcY0 = *srcY;
5272
5273 if (_mesa_clip_to_region(0, 0, fb->Width, fb->Height,
5274 srcX, srcY, width, height)) {
5275 *destX = *destX + *srcX - srcX0;
5276 *destY = *destY + *srcY - srcY0;
5277
5278 return GL_TRUE;
5279 }
5280 else {
5281 return GL_FALSE;
5282 }
5283 }
5284
5285
5286
5287 /**
5288 * Clip the rectangle defined by (x, y, width, height) against the bounds
5289 * specified by [xmin, xmax) and [ymin, ymax).
5290 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
5291 */
5292 GLboolean
5293 _mesa_clip_to_region(GLint xmin, GLint ymin,
5294 GLint xmax, GLint ymax,
5295 GLint *x, GLint *y,
5296 GLsizei *width, GLsizei *height )
5297 {
5298 /* left clipping */
5299 if (*x < xmin) {
5300 *width -= (xmin - *x);
5301 *x = xmin;
5302 }
5303
5304 /* right clipping */
5305 if (*x + *width > xmax)
5306 *width -= (*x + *width - xmax);
5307
5308 if (*width <= 0)
5309 return GL_FALSE;
5310
5311 /* bottom (or top) clipping */
5312 if (*y < ymin) {
5313 *height -= (ymin - *y);
5314 *y = ymin;
5315 }
5316
5317 /* top (or bottom) clipping */
5318 if (*y + *height > ymax)
5319 *height -= (*y + *height - ymax);
5320
5321 if (*height <= 0)
5322 return GL_FALSE;
5323
5324 return GL_TRUE;
5325 }