mesa: don't call ctx->Driver.Draw/CopyPixels() if width or height is zero
[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 * Incoming colors will be clamped to [0,1] if needed.
1681 * Note: the rgba values will be modified by this function when any pixel
1682 * transfer ops are enabled.
1683 */
1684 void
1685 _mesa_pack_rgba_span_float(GLcontext *ctx, GLuint n, GLfloat rgba[][4],
1686 GLenum dstFormat, GLenum dstType,
1687 GLvoid *dstAddr,
1688 const struct gl_pixelstore_attrib *dstPacking,
1689 GLbitfield transferOps, GLboolean noClamp)
1690 {
1691 GLfloat luminance[MAX_WIDTH];
1692 const GLint comps = _mesa_components_in_format(dstFormat);
1693 GLuint i;
1694
1695 if ((!noClamp) && (dstType != GL_FLOAT || ctx->Color.ClampReadColor == GL_TRUE)) {
1696 /* need to clamp to [0, 1] */
1697 transferOps |= IMAGE_CLAMP_BIT;
1698 }
1699
1700 if (transferOps) {
1701 _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
1702 if ((transferOps & IMAGE_MIN_MAX_BIT) && ctx->MinMax.Sink) {
1703 return;
1704 }
1705 }
1706
1707 if (dstFormat == GL_LUMINANCE || dstFormat == GL_LUMINANCE_ALPHA) {
1708 /* compute luminance values */
1709 if (transferOps & IMAGE_CLAMP_BIT) {
1710 for (i = 0; i < n; i++) {
1711 GLfloat sum = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
1712 luminance[i] = CLAMP(sum, 0.0F, 1.0F);
1713 }
1714 }
1715 else {
1716 for (i = 0; i < n; i++) {
1717 luminance[i] = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
1718 }
1719 }
1720 }
1721
1722 /*
1723 * Pack/store the pixels. Ugh! Lots of cases!!!
1724 */
1725 switch (dstType) {
1726 case GL_UNSIGNED_BYTE:
1727 {
1728 GLubyte *dst = (GLubyte *) dstAddr;
1729 switch (dstFormat) {
1730 case GL_RED:
1731 for (i=0;i<n;i++)
1732 dst[i] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1733 break;
1734 case GL_GREEN:
1735 for (i=0;i<n;i++)
1736 dst[i] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1737 break;
1738 case GL_BLUE:
1739 for (i=0;i<n;i++)
1740 dst[i] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1741 break;
1742 case GL_ALPHA:
1743 for (i=0;i<n;i++)
1744 dst[i] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1745 break;
1746 case GL_LUMINANCE:
1747 for (i=0;i<n;i++)
1748 dst[i] = FLOAT_TO_UBYTE(luminance[i]);
1749 break;
1750 case GL_LUMINANCE_ALPHA:
1751 for (i=0;i<n;i++) {
1752 dst[i*2+0] = FLOAT_TO_UBYTE(luminance[i]);
1753 dst[i*2+1] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1754 }
1755 break;
1756 case GL_RGB:
1757 for (i=0;i<n;i++) {
1758 dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1759 dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1760 dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1761 }
1762 break;
1763 case GL_RGBA:
1764 for (i=0;i<n;i++) {
1765 dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1766 dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1767 dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1768 dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1769 }
1770 break;
1771 case GL_BGR:
1772 for (i=0;i<n;i++) {
1773 dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1774 dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1775 dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1776 }
1777 break;
1778 case GL_BGRA:
1779 for (i=0;i<n;i++) {
1780 dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1781 dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1782 dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1783 dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1784 }
1785 break;
1786 case GL_ABGR_EXT:
1787 for (i=0;i<n;i++) {
1788 dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1789 dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1790 dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1791 dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1792 }
1793 break;
1794 case GL_DUDV_ATI:
1795 case GL_DU8DV8_ATI:
1796 for (i=0;i<n;i++) {
1797 dst[i*2+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1798 dst[i*2+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1799 }
1800 break;
1801 default:
1802 _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1803 }
1804 }
1805 break;
1806 case GL_BYTE:
1807 {
1808 GLbyte *dst = (GLbyte *) dstAddr;
1809 switch (dstFormat) {
1810 case GL_RED:
1811 for (i=0;i<n;i++)
1812 dst[i] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1813 break;
1814 case GL_GREEN:
1815 for (i=0;i<n;i++)
1816 dst[i] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1817 break;
1818 case GL_BLUE:
1819 for (i=0;i<n;i++)
1820 dst[i] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1821 break;
1822 case GL_ALPHA:
1823 for (i=0;i<n;i++)
1824 dst[i] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1825 break;
1826 case GL_LUMINANCE:
1827 for (i=0;i<n;i++)
1828 dst[i] = FLOAT_TO_BYTE(luminance[i]);
1829 break;
1830 case GL_LUMINANCE_ALPHA:
1831 for (i=0;i<n;i++) {
1832 dst[i*2+0] = FLOAT_TO_BYTE(luminance[i]);
1833 dst[i*2+1] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1834 }
1835 break;
1836 case GL_RGB:
1837 for (i=0;i<n;i++) {
1838 dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1839 dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1840 dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1841 }
1842 break;
1843 case GL_RGBA:
1844 for (i=0;i<n;i++) {
1845 dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1846 dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1847 dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1848 dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1849 }
1850 break;
1851 case GL_BGR:
1852 for (i=0;i<n;i++) {
1853 dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1854 dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1855 dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1856 }
1857 break;
1858 case GL_BGRA:
1859 for (i=0;i<n;i++) {
1860 dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1861 dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1862 dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1863 dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1864 }
1865 break;
1866 case GL_ABGR_EXT:
1867 for (i=0;i<n;i++) {
1868 dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1869 dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1870 dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1871 dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1872 }
1873 break;
1874 case GL_DUDV_ATI:
1875 case GL_DU8DV8_ATI:
1876 for (i=0;i<n;i++) {
1877 dst[i*2+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1878 dst[i*2+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1879 }
1880 break;
1881 default:
1882 _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1883 }
1884 }
1885 break;
1886 case GL_UNSIGNED_SHORT:
1887 {
1888 GLushort *dst = (GLushort *) dstAddr;
1889 switch (dstFormat) {
1890 case GL_RED:
1891 for (i=0;i<n;i++)
1892 CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][RCOMP]);
1893 break;
1894 case GL_GREEN:
1895 for (i=0;i<n;i++)
1896 CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][GCOMP]);
1897 break;
1898 case GL_BLUE:
1899 for (i=0;i<n;i++)
1900 CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][BCOMP]);
1901 break;
1902 case GL_ALPHA:
1903 for (i=0;i<n;i++)
1904 CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][ACOMP]);
1905 break;
1906 case GL_LUMINANCE:
1907 for (i=0;i<n;i++)
1908 UNCLAMPED_FLOAT_TO_USHORT(dst[i], luminance[i]);
1909 break;
1910 case GL_LUMINANCE_ALPHA:
1911 for (i=0;i<n;i++) {
1912 UNCLAMPED_FLOAT_TO_USHORT(dst[i*2+0], luminance[i]);
1913 CLAMPED_FLOAT_TO_USHORT(dst[i*2+1], rgba[i][ACOMP]);
1914 }
1915 break;
1916 case GL_RGB:
1917 for (i=0;i<n;i++) {
1918 CLAMPED_FLOAT_TO_USHORT(dst[i*3+0], rgba[i][RCOMP]);
1919 CLAMPED_FLOAT_TO_USHORT(dst[i*3+1], rgba[i][GCOMP]);
1920 CLAMPED_FLOAT_TO_USHORT(dst[i*3+2], rgba[i][BCOMP]);
1921 }
1922 break;
1923 case GL_RGBA:
1924 for (i=0;i<n;i++) {
1925 CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][RCOMP]);
1926 CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][GCOMP]);
1927 CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][BCOMP]);
1928 CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][ACOMP]);
1929 }
1930 break;
1931 case GL_BGR:
1932 for (i=0;i<n;i++) {
1933 CLAMPED_FLOAT_TO_USHORT(dst[i*3+0], rgba[i][BCOMP]);
1934 CLAMPED_FLOAT_TO_USHORT(dst[i*3+1], rgba[i][GCOMP]);
1935 CLAMPED_FLOAT_TO_USHORT(dst[i*3+2], rgba[i][RCOMP]);
1936 }
1937 break;
1938 case GL_BGRA:
1939 for (i=0;i<n;i++) {
1940 CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][BCOMP]);
1941 CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][GCOMP]);
1942 CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][RCOMP]);
1943 CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][ACOMP]);
1944 }
1945 break;
1946 case GL_ABGR_EXT:
1947 for (i=0;i<n;i++) {
1948 CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][ACOMP]);
1949 CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][BCOMP]);
1950 CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][GCOMP]);
1951 CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][RCOMP]);
1952 }
1953 break;
1954 case GL_DUDV_ATI:
1955 case GL_DU8DV8_ATI:
1956 for (i=0;i<n;i++) {
1957 dst[i*2+0] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
1958 dst[i*2+1] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
1959 }
1960 break;
1961 default:
1962 _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1963 }
1964 }
1965 break;
1966 case GL_SHORT:
1967 {
1968 GLshort *dst = (GLshort *) dstAddr;
1969 switch (dstFormat) {
1970 case GL_RED:
1971 for (i=0;i<n;i++)
1972 dst[i] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1973 break;
1974 case GL_GREEN:
1975 for (i=0;i<n;i++)
1976 dst[i] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1977 break;
1978 case GL_BLUE:
1979 for (i=0;i<n;i++)
1980 dst[i] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1981 break;
1982 case GL_ALPHA:
1983 for (i=0;i<n;i++)
1984 dst[i] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1985 break;
1986 case GL_LUMINANCE:
1987 for (i=0;i<n;i++)
1988 dst[i] = FLOAT_TO_SHORT(luminance[i]);
1989 break;
1990 case GL_LUMINANCE_ALPHA:
1991 for (i=0;i<n;i++) {
1992 dst[i*2+0] = FLOAT_TO_SHORT(luminance[i]);
1993 dst[i*2+1] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1994 }
1995 break;
1996 case GL_RGB:
1997 for (i=0;i<n;i++) {
1998 dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1999 dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
2000 dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
2001 }
2002 break;
2003 case GL_RGBA:
2004 for (i=0;i<n;i++) {
2005 dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
2006 dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
2007 dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
2008 dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
2009 }
2010 break;
2011 case GL_BGR:
2012 for (i=0;i<n;i++) {
2013 dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
2014 dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
2015 dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
2016 }
2017 break;
2018 case GL_BGRA:
2019 for (i=0;i<n;i++) {
2020 dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
2021 dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
2022 dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
2023 dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
2024 }
2025 break;
2026 case GL_ABGR_EXT:
2027 for (i=0;i<n;i++) {
2028 dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
2029 dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
2030 dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
2031 dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
2032 }
2033 break;
2034 case GL_DUDV_ATI:
2035 case GL_DU8DV8_ATI:
2036 for (i=0;i<n;i++) {
2037 dst[i*2+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
2038 dst[i*2+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
2039 }
2040 break;
2041 default:
2042 _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2043 }
2044 }
2045 break;
2046 case GL_UNSIGNED_INT:
2047 {
2048 GLuint *dst = (GLuint *) dstAddr;
2049 switch (dstFormat) {
2050 case GL_RED:
2051 for (i=0;i<n;i++)
2052 dst[i] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2053 break;
2054 case GL_GREEN:
2055 for (i=0;i<n;i++)
2056 dst[i] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2057 break;
2058 case GL_BLUE:
2059 for (i=0;i<n;i++)
2060 dst[i] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2061 break;
2062 case GL_ALPHA:
2063 for (i=0;i<n;i++)
2064 dst[i] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2065 break;
2066 case GL_LUMINANCE:
2067 for (i=0;i<n;i++)
2068 dst[i] = FLOAT_TO_UINT(luminance[i]);
2069 break;
2070 case GL_LUMINANCE_ALPHA:
2071 for (i=0;i<n;i++) {
2072 dst[i*2+0] = FLOAT_TO_UINT(luminance[i]);
2073 dst[i*2+1] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2074 }
2075 break;
2076 case GL_RGB:
2077 for (i=0;i<n;i++) {
2078 dst[i*3+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2079 dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2080 dst[i*3+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2081 }
2082 break;
2083 case GL_RGBA:
2084 for (i=0;i<n;i++) {
2085 dst[i*4+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2086 dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2087 dst[i*4+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2088 dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2089 }
2090 break;
2091 case GL_BGR:
2092 for (i=0;i<n;i++) {
2093 dst[i*3+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2094 dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2095 dst[i*3+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2096 }
2097 break;
2098 case GL_BGRA:
2099 for (i=0;i<n;i++) {
2100 dst[i*4+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2101 dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2102 dst[i*4+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2103 dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2104 }
2105 break;
2106 case GL_ABGR_EXT:
2107 for (i=0;i<n;i++) {
2108 dst[i*4+0] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2109 dst[i*4+1] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2110 dst[i*4+2] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2111 dst[i*4+3] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2112 }
2113 break;
2114 case GL_DUDV_ATI:
2115 case GL_DU8DV8_ATI:
2116 for (i=0;i<n;i++) {
2117 dst[i*2+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2118 dst[i*2+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2119 }
2120 break;
2121 default:
2122 _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2123 }
2124 }
2125 break;
2126 case GL_INT:
2127 {
2128 GLint *dst = (GLint *) dstAddr;
2129 switch (dstFormat) {
2130 case GL_RED:
2131 for (i=0;i<n;i++)
2132 dst[i] = FLOAT_TO_INT(rgba[i][RCOMP]);
2133 break;
2134 case GL_GREEN:
2135 for (i=0;i<n;i++)
2136 dst[i] = FLOAT_TO_INT(rgba[i][GCOMP]);
2137 break;
2138 case GL_BLUE:
2139 for (i=0;i<n;i++)
2140 dst[i] = FLOAT_TO_INT(rgba[i][BCOMP]);
2141 break;
2142 case GL_ALPHA:
2143 for (i=0;i<n;i++)
2144 dst[i] = FLOAT_TO_INT(rgba[i][ACOMP]);
2145 break;
2146 case GL_LUMINANCE:
2147 for (i=0;i<n;i++)
2148 dst[i] = FLOAT_TO_INT(luminance[i]);
2149 break;
2150 case GL_LUMINANCE_ALPHA:
2151 for (i=0;i<n;i++) {
2152 dst[i*2+0] = FLOAT_TO_INT(luminance[i]);
2153 dst[i*2+1] = FLOAT_TO_INT(rgba[i][ACOMP]);
2154 }
2155 break;
2156 case GL_RGB:
2157 for (i=0;i<n;i++) {
2158 dst[i*3+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
2159 dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2160 dst[i*3+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
2161 }
2162 break;
2163 case GL_RGBA:
2164 for (i=0;i<n;i++) {
2165 dst[i*4+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
2166 dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2167 dst[i*4+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
2168 dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
2169 }
2170 break;
2171 case GL_BGR:
2172 for (i=0;i<n;i++) {
2173 dst[i*3+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
2174 dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2175 dst[i*3+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
2176 }
2177 break;
2178 case GL_BGRA:
2179 for (i=0;i<n;i++) {
2180 dst[i*4+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
2181 dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2182 dst[i*4+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
2183 dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
2184 }
2185 break;
2186 case GL_ABGR_EXT:
2187 for (i=0;i<n;i++) {
2188 dst[i*4+0] = FLOAT_TO_INT(rgba[i][ACOMP]);
2189 dst[i*4+1] = FLOAT_TO_INT(rgba[i][BCOMP]);
2190 dst[i*4+2] = FLOAT_TO_INT(rgba[i][GCOMP]);
2191 dst[i*4+3] = FLOAT_TO_INT(rgba[i][RCOMP]);
2192 }
2193 break;
2194 case GL_DUDV_ATI:
2195 case GL_DU8DV8_ATI:
2196 for (i=0;i<n;i++) {
2197 dst[i*2+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
2198 dst[i*2+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2199 }
2200 break;
2201 default:
2202 _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2203 }
2204 }
2205 break;
2206 case GL_FLOAT:
2207 {
2208 GLfloat *dst = (GLfloat *) dstAddr;
2209 switch (dstFormat) {
2210 case GL_RED:
2211 for (i=0;i<n;i++)
2212 dst[i] = rgba[i][RCOMP];
2213 break;
2214 case GL_GREEN:
2215 for (i=0;i<n;i++)
2216 dst[i] = rgba[i][GCOMP];
2217 break;
2218 case GL_BLUE:
2219 for (i=0;i<n;i++)
2220 dst[i] = rgba[i][BCOMP];
2221 break;
2222 case GL_ALPHA:
2223 for (i=0;i<n;i++)
2224 dst[i] = rgba[i][ACOMP];
2225 break;
2226 case GL_LUMINANCE:
2227 for (i=0;i<n;i++)
2228 dst[i] = luminance[i];
2229 break;
2230 case GL_LUMINANCE_ALPHA:
2231 for (i=0;i<n;i++) {
2232 dst[i*2+0] = luminance[i];
2233 dst[i*2+1] = rgba[i][ACOMP];
2234 }
2235 break;
2236 case GL_RGB:
2237 for (i=0;i<n;i++) {
2238 dst[i*3+0] = rgba[i][RCOMP];
2239 dst[i*3+1] = rgba[i][GCOMP];
2240 dst[i*3+2] = rgba[i][BCOMP];
2241 }
2242 break;
2243 case GL_RGBA:
2244 for (i=0;i<n;i++) {
2245 dst[i*4+0] = rgba[i][RCOMP];
2246 dst[i*4+1] = rgba[i][GCOMP];
2247 dst[i*4+2] = rgba[i][BCOMP];
2248 dst[i*4+3] = rgba[i][ACOMP];
2249 }
2250 break;
2251 case GL_BGR:
2252 for (i=0;i<n;i++) {
2253 dst[i*3+0] = rgba[i][BCOMP];
2254 dst[i*3+1] = rgba[i][GCOMP];
2255 dst[i*3+2] = rgba[i][RCOMP];
2256 }
2257 break;
2258 case GL_BGRA:
2259 for (i=0;i<n;i++) {
2260 dst[i*4+0] = rgba[i][BCOMP];
2261 dst[i*4+1] = rgba[i][GCOMP];
2262 dst[i*4+2] = rgba[i][RCOMP];
2263 dst[i*4+3] = rgba[i][ACOMP];
2264 }
2265 break;
2266 case GL_ABGR_EXT:
2267 for (i=0;i<n;i++) {
2268 dst[i*4+0] = rgba[i][ACOMP];
2269 dst[i*4+1] = rgba[i][BCOMP];
2270 dst[i*4+2] = rgba[i][GCOMP];
2271 dst[i*4+3] = rgba[i][RCOMP];
2272 }
2273 break;
2274 case GL_DUDV_ATI:
2275 case GL_DU8DV8_ATI:
2276 for (i=0;i<n;i++) {
2277 dst[i*2+0] = rgba[i][RCOMP];
2278 dst[i*2+1] = rgba[i][GCOMP];
2279 }
2280 break;
2281 default:
2282 _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2283 }
2284 }
2285 break;
2286 case GL_HALF_FLOAT_ARB:
2287 {
2288 GLhalfARB *dst = (GLhalfARB *) dstAddr;
2289 switch (dstFormat) {
2290 case GL_RED:
2291 for (i=0;i<n;i++)
2292 dst[i] = _mesa_float_to_half(rgba[i][RCOMP]);
2293 break;
2294 case GL_GREEN:
2295 for (i=0;i<n;i++)
2296 dst[i] = _mesa_float_to_half(rgba[i][GCOMP]);
2297 break;
2298 case GL_BLUE:
2299 for (i=0;i<n;i++)
2300 dst[i] = _mesa_float_to_half(rgba[i][BCOMP]);
2301 break;
2302 case GL_ALPHA:
2303 for (i=0;i<n;i++)
2304 dst[i] = _mesa_float_to_half(rgba[i][ACOMP]);
2305 break;
2306 case GL_LUMINANCE:
2307 for (i=0;i<n;i++)
2308 dst[i] = _mesa_float_to_half(luminance[i]);
2309 break;
2310 case GL_LUMINANCE_ALPHA:
2311 for (i=0;i<n;i++) {
2312 dst[i*2+0] = _mesa_float_to_half(luminance[i]);
2313 dst[i*2+1] = _mesa_float_to_half(rgba[i][ACOMP]);
2314 }
2315 break;
2316 case GL_RGB:
2317 for (i=0;i<n;i++) {
2318 dst[i*3+0] = _mesa_float_to_half(rgba[i][RCOMP]);
2319 dst[i*3+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2320 dst[i*3+2] = _mesa_float_to_half(rgba[i][BCOMP]);
2321 }
2322 break;
2323 case GL_RGBA:
2324 for (i=0;i<n;i++) {
2325 dst[i*4+0] = _mesa_float_to_half(rgba[i][RCOMP]);
2326 dst[i*4+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2327 dst[i*4+2] = _mesa_float_to_half(rgba[i][BCOMP]);
2328 dst[i*4+3] = _mesa_float_to_half(rgba[i][ACOMP]);
2329 }
2330 break;
2331 case GL_BGR:
2332 for (i=0;i<n;i++) {
2333 dst[i*3+0] = _mesa_float_to_half(rgba[i][BCOMP]);
2334 dst[i*3+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2335 dst[i*3+2] = _mesa_float_to_half(rgba[i][RCOMP]);
2336 }
2337 break;
2338 case GL_BGRA:
2339 for (i=0;i<n;i++) {
2340 dst[i*4+0] = _mesa_float_to_half(rgba[i][BCOMP]);
2341 dst[i*4+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2342 dst[i*4+2] = _mesa_float_to_half(rgba[i][RCOMP]);
2343 dst[i*4+3] = _mesa_float_to_half(rgba[i][ACOMP]);
2344 }
2345 break;
2346 case GL_ABGR_EXT:
2347 for (i=0;i<n;i++) {
2348 dst[i*4+0] = _mesa_float_to_half(rgba[i][ACOMP]);
2349 dst[i*4+1] = _mesa_float_to_half(rgba[i][BCOMP]);
2350 dst[i*4+2] = _mesa_float_to_half(rgba[i][GCOMP]);
2351 dst[i*4+3] = _mesa_float_to_half(rgba[i][RCOMP]);
2352 }
2353 break;
2354 case GL_DUDV_ATI:
2355 case GL_DU8DV8_ATI:
2356 for (i=0;i<n;i++) {
2357 dst[i*2+0] = _mesa_float_to_half(rgba[i][RCOMP]);
2358 dst[i*2+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2359 }
2360 break;
2361 default:
2362 _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2363 }
2364 }
2365 break;
2366 case GL_UNSIGNED_BYTE_3_3_2:
2367 if (dstFormat == GL_RGB) {
2368 GLubyte *dst = (GLubyte *) dstAddr;
2369 for (i=0;i<n;i++) {
2370 dst[i] = (IROUND(rgba[i][RCOMP] * 7.0F) << 5)
2371 | (IROUND(rgba[i][GCOMP] * 7.0F) << 2)
2372 | (IROUND(rgba[i][BCOMP] * 3.0F) );
2373 }
2374 }
2375 break;
2376 case GL_UNSIGNED_BYTE_2_3_3_REV:
2377 if (dstFormat == GL_RGB) {
2378 GLubyte *dst = (GLubyte *) dstAddr;
2379 for (i=0;i<n;i++) {
2380 dst[i] = (IROUND(rgba[i][RCOMP] * 7.0F) )
2381 | (IROUND(rgba[i][GCOMP] * 7.0F) << 3)
2382 | (IROUND(rgba[i][BCOMP] * 3.0F) << 6);
2383 }
2384 }
2385 break;
2386 case GL_UNSIGNED_SHORT_5_6_5:
2387 if (dstFormat == GL_RGB) {
2388 GLushort *dst = (GLushort *) dstAddr;
2389 for (i=0;i<n;i++) {
2390 dst[i] = (IROUND(rgba[i][RCOMP] * 31.0F) << 11)
2391 | (IROUND(rgba[i][GCOMP] * 63.0F) << 5)
2392 | (IROUND(rgba[i][BCOMP] * 31.0F) );
2393 }
2394 }
2395 break;
2396 case GL_UNSIGNED_SHORT_5_6_5_REV:
2397 if (dstFormat == GL_RGB) {
2398 GLushort *dst = (GLushort *) dstAddr;
2399 for (i=0;i<n;i++) {
2400 dst[i] = (IROUND(rgba[i][RCOMP] * 31.0F) )
2401 | (IROUND(rgba[i][GCOMP] * 63.0F) << 5)
2402 | (IROUND(rgba[i][BCOMP] * 31.0F) << 11);
2403 }
2404 }
2405 break;
2406 case GL_UNSIGNED_SHORT_4_4_4_4:
2407 if (dstFormat == GL_RGBA) {
2408 GLushort *dst = (GLushort *) dstAddr;
2409 for (i=0;i<n;i++) {
2410 dst[i] = (IROUND(rgba[i][RCOMP] * 15.0F) << 12)
2411 | (IROUND(rgba[i][GCOMP] * 15.0F) << 8)
2412 | (IROUND(rgba[i][BCOMP] * 15.0F) << 4)
2413 | (IROUND(rgba[i][ACOMP] * 15.0F) );
2414 }
2415 }
2416 else if (dstFormat == GL_BGRA) {
2417 GLushort *dst = (GLushort *) dstAddr;
2418 for (i=0;i<n;i++) {
2419 dst[i] = (IROUND(rgba[i][BCOMP] * 15.0F) << 12)
2420 | (IROUND(rgba[i][GCOMP] * 15.0F) << 8)
2421 | (IROUND(rgba[i][RCOMP] * 15.0F) << 4)
2422 | (IROUND(rgba[i][ACOMP] * 15.0F) );
2423 }
2424 }
2425 else if (dstFormat == GL_ABGR_EXT) {
2426 GLushort *dst = (GLushort *) dstAddr;
2427 for (i=0;i<n;i++) {
2428 dst[i] = (IROUND(rgba[i][ACOMP] * 15.0F) << 12)
2429 | (IROUND(rgba[i][BCOMP] * 15.0F) << 8)
2430 | (IROUND(rgba[i][GCOMP] * 15.0F) << 4)
2431 | (IROUND(rgba[i][RCOMP] * 15.0F) );
2432 }
2433 }
2434 break;
2435 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
2436 if (dstFormat == GL_RGBA) {
2437 GLushort *dst = (GLushort *) dstAddr;
2438 for (i=0;i<n;i++) {
2439 dst[i] = (IROUND(rgba[i][RCOMP] * 15.0F) )
2440 | (IROUND(rgba[i][GCOMP] * 15.0F) << 4)
2441 | (IROUND(rgba[i][BCOMP] * 15.0F) << 8)
2442 | (IROUND(rgba[i][ACOMP] * 15.0F) << 12);
2443 }
2444 }
2445 else if (dstFormat == GL_BGRA) {
2446 GLushort *dst = (GLushort *) dstAddr;
2447 for (i=0;i<n;i++) {
2448 dst[i] = (IROUND(rgba[i][BCOMP] * 15.0F) )
2449 | (IROUND(rgba[i][GCOMP] * 15.0F) << 4)
2450 | (IROUND(rgba[i][RCOMP] * 15.0F) << 8)
2451 | (IROUND(rgba[i][ACOMP] * 15.0F) << 12);
2452 }
2453 }
2454 else if (dstFormat == GL_ABGR_EXT) {
2455 GLushort *dst = (GLushort *) dstAddr;
2456 for (i=0;i<n;i++) {
2457 dst[i] = (IROUND(rgba[i][ACOMP] * 15.0F) )
2458 | (IROUND(rgba[i][BCOMP] * 15.0F) << 4)
2459 | (IROUND(rgba[i][GCOMP] * 15.0F) << 8)
2460 | (IROUND(rgba[i][RCOMP] * 15.0F) << 12);
2461 }
2462 }
2463 break;
2464 case GL_UNSIGNED_SHORT_5_5_5_1:
2465 if (dstFormat == GL_RGBA) {
2466 GLushort *dst = (GLushort *) dstAddr;
2467 for (i=0;i<n;i++) {
2468 dst[i] = (IROUND(rgba[i][RCOMP] * 31.0F) << 11)
2469 | (IROUND(rgba[i][GCOMP] * 31.0F) << 6)
2470 | (IROUND(rgba[i][BCOMP] * 31.0F) << 1)
2471 | (IROUND(rgba[i][ACOMP] * 1.0F) );
2472 }
2473 }
2474 else if (dstFormat == GL_BGRA) {
2475 GLushort *dst = (GLushort *) dstAddr;
2476 for (i=0;i<n;i++) {
2477 dst[i] = (IROUND(rgba[i][BCOMP] * 31.0F) << 11)
2478 | (IROUND(rgba[i][GCOMP] * 31.0F) << 6)
2479 | (IROUND(rgba[i][RCOMP] * 31.0F) << 1)
2480 | (IROUND(rgba[i][ACOMP] * 1.0F) );
2481 }
2482 }
2483 else if (dstFormat == GL_ABGR_EXT) {
2484 GLushort *dst = (GLushort *) dstAddr;
2485 for (i=0;i<n;i++) {
2486 dst[i] = (IROUND(rgba[i][ACOMP] * 31.0F) << 11)
2487 | (IROUND(rgba[i][BCOMP] * 31.0F) << 6)
2488 | (IROUND(rgba[i][GCOMP] * 31.0F) << 1)
2489 | (IROUND(rgba[i][RCOMP] * 1.0F) );
2490 }
2491 }
2492 break;
2493 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
2494 if (dstFormat == GL_RGBA) {
2495 GLushort *dst = (GLushort *) dstAddr;
2496 for (i=0;i<n;i++) {
2497 dst[i] = (IROUND(rgba[i][RCOMP] * 31.0F) )
2498 | (IROUND(rgba[i][GCOMP] * 31.0F) << 5)
2499 | (IROUND(rgba[i][BCOMP] * 31.0F) << 10)
2500 | (IROUND(rgba[i][ACOMP] * 1.0F) << 15);
2501 }
2502 }
2503 else if (dstFormat == GL_BGRA) {
2504 GLushort *dst = (GLushort *) dstAddr;
2505 for (i=0;i<n;i++) {
2506 dst[i] = (IROUND(rgba[i][BCOMP] * 31.0F) )
2507 | (IROUND(rgba[i][GCOMP] * 31.0F) << 5)
2508 | (IROUND(rgba[i][RCOMP] * 31.0F) << 10)
2509 | (IROUND(rgba[i][ACOMP] * 1.0F) << 15);
2510 }
2511 }
2512 else if (dstFormat == GL_ABGR_EXT) {
2513 GLushort *dst = (GLushort *) dstAddr;
2514 for (i=0;i<n;i++) {
2515 dst[i] = (IROUND(rgba[i][ACOMP] * 31.0F) )
2516 | (IROUND(rgba[i][BCOMP] * 31.0F) << 5)
2517 | (IROUND(rgba[i][GCOMP] * 31.0F) << 10)
2518 | (IROUND(rgba[i][RCOMP] * 1.0F) << 15);
2519 }
2520 }
2521 break;
2522 case GL_UNSIGNED_INT_8_8_8_8:
2523 if (dstFormat == GL_RGBA) {
2524 GLuint *dst = (GLuint *) dstAddr;
2525 for (i=0;i<n;i++) {
2526 dst[i] = (IROUND(rgba[i][RCOMP] * 255.F) << 24)
2527 | (IROUND(rgba[i][GCOMP] * 255.F) << 16)
2528 | (IROUND(rgba[i][BCOMP] * 255.F) << 8)
2529 | (IROUND(rgba[i][ACOMP] * 255.F) );
2530 }
2531 }
2532 else if (dstFormat == GL_BGRA) {
2533 GLuint *dst = (GLuint *) dstAddr;
2534 for (i=0;i<n;i++) {
2535 dst[i] = (IROUND(rgba[i][BCOMP] * 255.F) << 24)
2536 | (IROUND(rgba[i][GCOMP] * 255.F) << 16)
2537 | (IROUND(rgba[i][RCOMP] * 255.F) << 8)
2538 | (IROUND(rgba[i][ACOMP] * 255.F) );
2539 }
2540 }
2541 else if (dstFormat == GL_ABGR_EXT) {
2542 GLuint *dst = (GLuint *) dstAddr;
2543 for (i=0;i<n;i++) {
2544 dst[i] = (IROUND(rgba[i][ACOMP] * 255.F) << 24)
2545 | (IROUND(rgba[i][BCOMP] * 255.F) << 16)
2546 | (IROUND(rgba[i][GCOMP] * 255.F) << 8)
2547 | (IROUND(rgba[i][RCOMP] * 255.F) );
2548 }
2549 }
2550 break;
2551 case GL_UNSIGNED_INT_8_8_8_8_REV:
2552 if (dstFormat == GL_RGBA) {
2553 GLuint *dst = (GLuint *) dstAddr;
2554 for (i=0;i<n;i++) {
2555 dst[i] = (IROUND(rgba[i][RCOMP] * 255.0F) )
2556 | (IROUND(rgba[i][GCOMP] * 255.0F) << 8)
2557 | (IROUND(rgba[i][BCOMP] * 255.0F) << 16)
2558 | (IROUND(rgba[i][ACOMP] * 255.0F) << 24);
2559 }
2560 }
2561 else if (dstFormat == GL_BGRA) {
2562 GLuint *dst = (GLuint *) dstAddr;
2563 for (i=0;i<n;i++) {
2564 dst[i] = (IROUND(rgba[i][BCOMP] * 255.0F) )
2565 | (IROUND(rgba[i][GCOMP] * 255.0F) << 8)
2566 | (IROUND(rgba[i][RCOMP] * 255.0F) << 16)
2567 | (IROUND(rgba[i][ACOMP] * 255.0F) << 24);
2568 }
2569 }
2570 else if (dstFormat == GL_ABGR_EXT) {
2571 GLuint *dst = (GLuint *) dstAddr;
2572 for (i=0;i<n;i++) {
2573 dst[i] = (IROUND(rgba[i][ACOMP] * 255.0F) )
2574 | (IROUND(rgba[i][BCOMP] * 255.0F) << 8)
2575 | (IROUND(rgba[i][GCOMP] * 255.0F) << 16)
2576 | (IROUND(rgba[i][RCOMP] * 255.0F) << 24);
2577 }
2578 }
2579 break;
2580 case GL_UNSIGNED_INT_10_10_10_2:
2581 if (dstFormat == GL_RGBA) {
2582 GLuint *dst = (GLuint *) dstAddr;
2583 for (i=0;i<n;i++) {
2584 dst[i] = (IROUND(rgba[i][RCOMP] * 1023.0F) << 22)
2585 | (IROUND(rgba[i][GCOMP] * 1023.0F) << 12)
2586 | (IROUND(rgba[i][BCOMP] * 1023.0F) << 2)
2587 | (IROUND(rgba[i][ACOMP] * 3.0F) );
2588 }
2589 }
2590 else if (dstFormat == GL_BGRA) {
2591 GLuint *dst = (GLuint *) dstAddr;
2592 for (i=0;i<n;i++) {
2593 dst[i] = (IROUND(rgba[i][BCOMP] * 1023.0F) << 22)
2594 | (IROUND(rgba[i][GCOMP] * 1023.0F) << 12)
2595 | (IROUND(rgba[i][RCOMP] * 1023.0F) << 2)
2596 | (IROUND(rgba[i][ACOMP] * 3.0F) );
2597 }
2598 }
2599 else if (dstFormat == GL_ABGR_EXT) {
2600 GLuint *dst = (GLuint *) dstAddr;
2601 for (i=0;i<n;i++) {
2602 dst[i] = (IROUND(rgba[i][ACOMP] * 1023.0F) << 22)
2603 | (IROUND(rgba[i][BCOMP] * 1023.0F) << 12)
2604 | (IROUND(rgba[i][GCOMP] * 1023.0F) << 2)
2605 | (IROUND(rgba[i][RCOMP] * 3.0F) );
2606 }
2607 }
2608 break;
2609 case GL_UNSIGNED_INT_2_10_10_10_REV:
2610 if (dstFormat == GL_RGBA) {
2611 GLuint *dst = (GLuint *) dstAddr;
2612 for (i=0;i<n;i++) {
2613 dst[i] = (IROUND(rgba[i][RCOMP] * 1023.0F) )
2614 | (IROUND(rgba[i][GCOMP] * 1023.0F) << 10)
2615 | (IROUND(rgba[i][BCOMP] * 1023.0F) << 20)
2616 | (IROUND(rgba[i][ACOMP] * 3.0F) << 30);
2617 }
2618 }
2619 else if (dstFormat == GL_BGRA) {
2620 GLuint *dst = (GLuint *) dstAddr;
2621 for (i=0;i<n;i++) {
2622 dst[i] = (IROUND(rgba[i][BCOMP] * 1023.0F) )
2623 | (IROUND(rgba[i][GCOMP] * 1023.0F) << 10)
2624 | (IROUND(rgba[i][RCOMP] * 1023.0F) << 20)
2625 | (IROUND(rgba[i][ACOMP] * 3.0F) << 30);
2626 }
2627 }
2628 else if (dstFormat == GL_ABGR_EXT) {
2629 GLuint *dst = (GLuint *) dstAddr;
2630 for (i=0;i<n;i++) {
2631 dst[i] = (IROUND(rgba[i][ACOMP] * 1023.0F) )
2632 | (IROUND(rgba[i][BCOMP] * 1023.0F) << 10)
2633 | (IROUND(rgba[i][GCOMP] * 1023.0F) << 20)
2634 | (IROUND(rgba[i][RCOMP] * 3.0F) << 30);
2635 }
2636 }
2637 break;
2638 default:
2639 _mesa_problem(ctx, "bad type in _mesa_pack_rgba_span_float");
2640 return;
2641 }
2642
2643 if (dstPacking->SwapBytes) {
2644 GLint swapSize = _mesa_sizeof_packed_type(dstType);
2645 if (swapSize == 2) {
2646 if (dstPacking->SwapBytes) {
2647 _mesa_swap2((GLushort *) dstAddr, n * comps);
2648 }
2649 }
2650 else if (swapSize == 4) {
2651 if (dstPacking->SwapBytes) {
2652 _mesa_swap4((GLuint *) dstAddr, n * comps);
2653 }
2654 }
2655 }
2656 }
2657
2658
2659 #define SWAP2BYTE(VALUE) \
2660 { \
2661 GLubyte *bytes = (GLubyte *) &(VALUE); \
2662 GLubyte tmp = bytes[0]; \
2663 bytes[0] = bytes[1]; \
2664 bytes[1] = tmp; \
2665 }
2666
2667 #define SWAP4BYTE(VALUE) \
2668 { \
2669 GLubyte *bytes = (GLubyte *) &(VALUE); \
2670 GLubyte tmp = bytes[0]; \
2671 bytes[0] = bytes[3]; \
2672 bytes[3] = tmp; \
2673 tmp = bytes[1]; \
2674 bytes[1] = bytes[2]; \
2675 bytes[2] = tmp; \
2676 }
2677
2678
2679 static void
2680 extract_uint_indexes(GLuint n, GLuint indexes[],
2681 GLenum srcFormat, GLenum srcType, const GLvoid *src,
2682 const struct gl_pixelstore_attrib *unpack )
2683 {
2684 ASSERT(srcFormat == GL_COLOR_INDEX || srcFormat == GL_STENCIL_INDEX);
2685
2686 ASSERT(srcType == GL_BITMAP ||
2687 srcType == GL_UNSIGNED_BYTE ||
2688 srcType == GL_BYTE ||
2689 srcType == GL_UNSIGNED_SHORT ||
2690 srcType == GL_SHORT ||
2691 srcType == GL_UNSIGNED_INT ||
2692 srcType == GL_INT ||
2693 srcType == GL_UNSIGNED_INT_24_8_EXT ||
2694 srcType == GL_HALF_FLOAT_ARB ||
2695 srcType == GL_FLOAT);
2696
2697 switch (srcType) {
2698 case GL_BITMAP:
2699 {
2700 GLubyte *ubsrc = (GLubyte *) src;
2701 if (unpack->LsbFirst) {
2702 GLubyte mask = 1 << (unpack->SkipPixels & 0x7);
2703 GLuint i;
2704 for (i = 0; i < n; i++) {
2705 indexes[i] = (*ubsrc & mask) ? 1 : 0;
2706 if (mask == 128) {
2707 mask = 1;
2708 ubsrc++;
2709 }
2710 else {
2711 mask = mask << 1;
2712 }
2713 }
2714 }
2715 else {
2716 GLubyte mask = 128 >> (unpack->SkipPixels & 0x7);
2717 GLuint i;
2718 for (i = 0; i < n; i++) {
2719 indexes[i] = (*ubsrc & mask) ? 1 : 0;
2720 if (mask == 1) {
2721 mask = 128;
2722 ubsrc++;
2723 }
2724 else {
2725 mask = mask >> 1;
2726 }
2727 }
2728 }
2729 }
2730 break;
2731 case GL_UNSIGNED_BYTE:
2732 {
2733 GLuint i;
2734 const GLubyte *s = (const GLubyte *) src;
2735 for (i = 0; i < n; i++)
2736 indexes[i] = s[i];
2737 }
2738 break;
2739 case GL_BYTE:
2740 {
2741 GLuint i;
2742 const GLbyte *s = (const GLbyte *) src;
2743 for (i = 0; i < n; i++)
2744 indexes[i] = s[i];
2745 }
2746 break;
2747 case GL_UNSIGNED_SHORT:
2748 {
2749 GLuint i;
2750 const GLushort *s = (const GLushort *) src;
2751 if (unpack->SwapBytes) {
2752 for (i = 0; i < n; i++) {
2753 GLushort value = s[i];
2754 SWAP2BYTE(value);
2755 indexes[i] = value;
2756 }
2757 }
2758 else {
2759 for (i = 0; i < n; i++)
2760 indexes[i] = s[i];
2761 }
2762 }
2763 break;
2764 case GL_SHORT:
2765 {
2766 GLuint i;
2767 const GLshort *s = (const GLshort *) src;
2768 if (unpack->SwapBytes) {
2769 for (i = 0; i < n; i++) {
2770 GLshort value = s[i];
2771 SWAP2BYTE(value);
2772 indexes[i] = value;
2773 }
2774 }
2775 else {
2776 for (i = 0; i < n; i++)
2777 indexes[i] = s[i];
2778 }
2779 }
2780 break;
2781 case GL_UNSIGNED_INT:
2782 {
2783 GLuint i;
2784 const GLuint *s = (const GLuint *) src;
2785 if (unpack->SwapBytes) {
2786 for (i = 0; i < n; i++) {
2787 GLuint value = s[i];
2788 SWAP4BYTE(value);
2789 indexes[i] = value;
2790 }
2791 }
2792 else {
2793 for (i = 0; i < n; i++)
2794 indexes[i] = s[i];
2795 }
2796 }
2797 break;
2798 case GL_INT:
2799 {
2800 GLuint i;
2801 const GLint *s = (const GLint *) src;
2802 if (unpack->SwapBytes) {
2803 for (i = 0; i < n; i++) {
2804 GLint value = s[i];
2805 SWAP4BYTE(value);
2806 indexes[i] = value;
2807 }
2808 }
2809 else {
2810 for (i = 0; i < n; i++)
2811 indexes[i] = s[i];
2812 }
2813 }
2814 break;
2815 case GL_FLOAT:
2816 {
2817 GLuint i;
2818 const GLfloat *s = (const GLfloat *) src;
2819 if (unpack->SwapBytes) {
2820 for (i = 0; i < n; i++) {
2821 GLfloat value = s[i];
2822 SWAP4BYTE(value);
2823 indexes[i] = (GLuint) value;
2824 }
2825 }
2826 else {
2827 for (i = 0; i < n; i++)
2828 indexes[i] = (GLuint) s[i];
2829 }
2830 }
2831 break;
2832 case GL_HALF_FLOAT_ARB:
2833 {
2834 GLuint i;
2835 const GLhalfARB *s = (const GLhalfARB *) src;
2836 if (unpack->SwapBytes) {
2837 for (i = 0; i < n; i++) {
2838 GLhalfARB value = s[i];
2839 SWAP2BYTE(value);
2840 indexes[i] = (GLuint) _mesa_half_to_float(value);
2841 }
2842 }
2843 else {
2844 for (i = 0; i < n; i++)
2845 indexes[i] = (GLuint) _mesa_half_to_float(s[i]);
2846 }
2847 }
2848 break;
2849 case GL_UNSIGNED_INT_24_8_EXT:
2850 {
2851 GLuint i;
2852 const GLuint *s = (const GLuint *) src;
2853 if (unpack->SwapBytes) {
2854 for (i = 0; i < n; i++) {
2855 GLuint value = s[i];
2856 SWAP4BYTE(value);
2857 indexes[i] = value & 0xff; /* lower 8 bits */
2858 }
2859 }
2860 else {
2861 for (i = 0; i < n; i++)
2862 indexes[i] = s[i] & 0xfff; /* lower 8 bits */
2863 }
2864 }
2865 break;
2866
2867 default:
2868 _mesa_problem(NULL, "bad srcType in extract_uint_indexes");
2869 return;
2870 }
2871 }
2872
2873
2874 /*
2875 * This function extracts floating point RGBA values from arbitrary
2876 * image data. srcFormat and srcType are the format and type parameters
2877 * passed to glDrawPixels, glTexImage[123]D, glTexSubImage[123]D, etc.
2878 *
2879 * Refering to section 3.6.4 of the OpenGL 1.2 spec, this function
2880 * implements the "Conversion to floating point", "Conversion to RGB",
2881 * and "Final Expansion to RGBA" operations.
2882 *
2883 * Args: n - number of pixels
2884 * rgba - output colors
2885 * srcFormat - format of incoming data
2886 * srcType - data type of incoming data
2887 * src - source data pointer
2888 * swapBytes - perform byteswapping of incoming data?
2889 */
2890 static void
2891 extract_float_rgba(GLuint n, GLfloat rgba[][4],
2892 GLenum srcFormat, GLenum srcType, const GLvoid *src,
2893 GLboolean swapBytes)
2894 {
2895 GLint redIndex, greenIndex, blueIndex, alphaIndex;
2896 GLint stride;
2897 GLint rComp, bComp, gComp, aComp;
2898
2899 ASSERT(srcFormat == GL_RED ||
2900 srcFormat == GL_GREEN ||
2901 srcFormat == GL_BLUE ||
2902 srcFormat == GL_ALPHA ||
2903 srcFormat == GL_LUMINANCE ||
2904 srcFormat == GL_LUMINANCE_ALPHA ||
2905 srcFormat == GL_INTENSITY ||
2906 srcFormat == GL_RGB ||
2907 srcFormat == GL_BGR ||
2908 srcFormat == GL_RGBA ||
2909 srcFormat == GL_BGRA ||
2910 srcFormat == GL_ABGR_EXT ||
2911 srcFormat == GL_DUDV_ATI);
2912
2913 ASSERT(srcType == GL_UNSIGNED_BYTE ||
2914 srcType == GL_BYTE ||
2915 srcType == GL_UNSIGNED_SHORT ||
2916 srcType == GL_SHORT ||
2917 srcType == GL_UNSIGNED_INT ||
2918 srcType == GL_INT ||
2919 srcType == GL_HALF_FLOAT_ARB ||
2920 srcType == GL_FLOAT ||
2921 srcType == GL_UNSIGNED_BYTE_3_3_2 ||
2922 srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
2923 srcType == GL_UNSIGNED_SHORT_5_6_5 ||
2924 srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
2925 srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
2926 srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
2927 srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
2928 srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
2929 srcType == GL_UNSIGNED_INT_8_8_8_8 ||
2930 srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
2931 srcType == GL_UNSIGNED_INT_10_10_10_2 ||
2932 srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
2933
2934 rComp = gComp = bComp = aComp = -1;
2935
2936 switch (srcFormat) {
2937 case GL_RED:
2938 redIndex = 0;
2939 greenIndex = blueIndex = alphaIndex = -1;
2940 stride = 1;
2941 break;
2942 case GL_GREEN:
2943 greenIndex = 0;
2944 redIndex = blueIndex = alphaIndex = -1;
2945 stride = 1;
2946 break;
2947 case GL_BLUE:
2948 blueIndex = 0;
2949 redIndex = greenIndex = alphaIndex = -1;
2950 stride = 1;
2951 break;
2952 case GL_ALPHA:
2953 redIndex = greenIndex = blueIndex = -1;
2954 alphaIndex = 0;
2955 stride = 1;
2956 break;
2957 case GL_LUMINANCE:
2958 redIndex = greenIndex = blueIndex = 0;
2959 alphaIndex = -1;
2960 stride = 1;
2961 break;
2962 case GL_LUMINANCE_ALPHA:
2963 redIndex = greenIndex = blueIndex = 0;
2964 alphaIndex = 1;
2965 stride = 2;
2966 break;
2967 case GL_INTENSITY:
2968 redIndex = greenIndex = blueIndex = alphaIndex = 0;
2969 stride = 1;
2970 break;
2971 case GL_RGB:
2972 redIndex = 0;
2973 greenIndex = 1;
2974 blueIndex = 2;
2975 alphaIndex = -1;
2976 rComp = 0;
2977 gComp = 1;
2978 bComp = 2;
2979 aComp = 3;
2980 stride = 3;
2981 break;
2982 case GL_BGR:
2983 redIndex = 2;
2984 greenIndex = 1;
2985 blueIndex = 0;
2986 alphaIndex = -1;
2987 rComp = 2;
2988 gComp = 1;
2989 bComp = 0;
2990 aComp = 3;
2991 stride = 3;
2992 break;
2993 case GL_RGBA:
2994 redIndex = 0;
2995 greenIndex = 1;
2996 blueIndex = 2;
2997 alphaIndex = 3;
2998 rComp = 0;
2999 gComp = 1;
3000 bComp = 2;
3001 aComp = 3;
3002 stride = 4;
3003 break;
3004 case GL_BGRA:
3005 redIndex = 2;
3006 greenIndex = 1;
3007 blueIndex = 0;
3008 alphaIndex = 3;
3009 rComp = 2;
3010 gComp = 1;
3011 bComp = 0;
3012 aComp = 3;
3013 stride = 4;
3014 break;
3015 case GL_ABGR_EXT:
3016 redIndex = 3;
3017 greenIndex = 2;
3018 blueIndex = 1;
3019 alphaIndex = 0;
3020 rComp = 3;
3021 gComp = 2;
3022 bComp = 1;
3023 aComp = 0;
3024 stride = 4;
3025 break;
3026 case GL_DUDV_ATI:
3027 redIndex = 0;
3028 greenIndex = 1;
3029 blueIndex = -1;
3030 alphaIndex = -1;
3031 stride = 2;
3032 break;
3033 default:
3034 _mesa_problem(NULL, "bad srcFormat in extract float data");
3035 return;
3036 }
3037
3038
3039 #define PROCESS(INDEX, CHANNEL, DEFAULT, TYPE, CONVERSION) \
3040 if ((INDEX) < 0) { \
3041 GLuint i; \
3042 for (i = 0; i < n; i++) { \
3043 rgba[i][CHANNEL] = DEFAULT; \
3044 } \
3045 } \
3046 else if (swapBytes) { \
3047 const TYPE *s = (const TYPE *) src; \
3048 GLuint i; \
3049 for (i = 0; i < n; i++) { \
3050 TYPE value = s[INDEX]; \
3051 if (sizeof(TYPE) == 2) { \
3052 SWAP2BYTE(value); \
3053 } \
3054 else if (sizeof(TYPE) == 4) { \
3055 SWAP4BYTE(value); \
3056 } \
3057 rgba[i][CHANNEL] = (GLfloat) CONVERSION(value); \
3058 s += stride; \
3059 } \
3060 } \
3061 else { \
3062 const TYPE *s = (const TYPE *) src; \
3063 GLuint i; \
3064 for (i = 0; i < n; i++) { \
3065 rgba[i][CHANNEL] = (GLfloat) CONVERSION(s[INDEX]); \
3066 s += stride; \
3067 } \
3068 }
3069
3070 switch (srcType) {
3071 case GL_UNSIGNED_BYTE:
3072 PROCESS(redIndex, RCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
3073 PROCESS(greenIndex, GCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
3074 PROCESS(blueIndex, BCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
3075 PROCESS(alphaIndex, ACOMP, 1.0F, GLubyte, UBYTE_TO_FLOAT);
3076 break;
3077 case GL_BYTE:
3078 PROCESS(redIndex, RCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
3079 PROCESS(greenIndex, GCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
3080 PROCESS(blueIndex, BCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
3081 PROCESS(alphaIndex, ACOMP, 1.0F, GLbyte, BYTE_TO_FLOAT);
3082 break;
3083 case GL_UNSIGNED_SHORT:
3084 PROCESS(redIndex, RCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
3085 PROCESS(greenIndex, GCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
3086 PROCESS(blueIndex, BCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
3087 PROCESS(alphaIndex, ACOMP, 1.0F, GLushort, USHORT_TO_FLOAT);
3088 break;
3089 case GL_SHORT:
3090 PROCESS(redIndex, RCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
3091 PROCESS(greenIndex, GCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
3092 PROCESS(blueIndex, BCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
3093 PROCESS(alphaIndex, ACOMP, 1.0F, GLshort, SHORT_TO_FLOAT);
3094 break;
3095 case GL_UNSIGNED_INT:
3096 PROCESS(redIndex, RCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
3097 PROCESS(greenIndex, GCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
3098 PROCESS(blueIndex, BCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
3099 PROCESS(alphaIndex, ACOMP, 1.0F, GLuint, UINT_TO_FLOAT);
3100 break;
3101 case GL_INT:
3102 PROCESS(redIndex, RCOMP, 0.0F, GLint, INT_TO_FLOAT);
3103 PROCESS(greenIndex, GCOMP, 0.0F, GLint, INT_TO_FLOAT);
3104 PROCESS(blueIndex, BCOMP, 0.0F, GLint, INT_TO_FLOAT);
3105 PROCESS(alphaIndex, ACOMP, 1.0F, GLint, INT_TO_FLOAT);
3106 break;
3107 case GL_FLOAT:
3108 PROCESS(redIndex, RCOMP, 0.0F, GLfloat, (GLfloat));
3109 PROCESS(greenIndex, GCOMP, 0.0F, GLfloat, (GLfloat));
3110 PROCESS(blueIndex, BCOMP, 0.0F, GLfloat, (GLfloat));
3111 PROCESS(alphaIndex, ACOMP, 1.0F, GLfloat, (GLfloat));
3112 break;
3113 case GL_HALF_FLOAT_ARB:
3114 PROCESS(redIndex, RCOMP, 0.0F, GLhalfARB, _mesa_half_to_float);
3115 PROCESS(greenIndex, GCOMP, 0.0F, GLhalfARB, _mesa_half_to_float);
3116 PROCESS(blueIndex, BCOMP, 0.0F, GLhalfARB, _mesa_half_to_float);
3117 PROCESS(alphaIndex, ACOMP, 1.0F, GLhalfARB, _mesa_half_to_float);
3118 break;
3119 case GL_UNSIGNED_BYTE_3_3_2:
3120 {
3121 const GLubyte *ubsrc = (const GLubyte *) src;
3122 GLuint i;
3123 for (i = 0; i < n; i ++) {
3124 GLubyte p = ubsrc[i];
3125 rgba[i][rComp] = ((p >> 5) ) * (1.0F / 7.0F);
3126 rgba[i][gComp] = ((p >> 2) & 0x7) * (1.0F / 7.0F);
3127 rgba[i][bComp] = ((p ) & 0x3) * (1.0F / 3.0F);
3128 rgba[i][aComp] = 1.0F;
3129 }
3130 }
3131 break;
3132 case GL_UNSIGNED_BYTE_2_3_3_REV:
3133 {
3134 const GLubyte *ubsrc = (const GLubyte *) src;
3135 GLuint i;
3136 for (i = 0; i < n; i ++) {
3137 GLubyte p = ubsrc[i];
3138 rgba[i][rComp] = ((p ) & 0x7) * (1.0F / 7.0F);
3139 rgba[i][gComp] = ((p >> 3) & 0x7) * (1.0F / 7.0F);
3140 rgba[i][bComp] = ((p >> 6) ) * (1.0F / 3.0F);
3141 rgba[i][aComp] = 1.0F;
3142 }
3143 }
3144 break;
3145 case GL_UNSIGNED_SHORT_5_6_5:
3146 if (swapBytes) {
3147 const GLushort *ussrc = (const GLushort *) src;
3148 GLuint i;
3149 for (i = 0; i < n; i ++) {
3150 GLushort p = ussrc[i];
3151 SWAP2BYTE(p);
3152 rgba[i][rComp] = ((p >> 11) ) * (1.0F / 31.0F);
3153 rgba[i][gComp] = ((p >> 5) & 0x3f) * (1.0F / 63.0F);
3154 rgba[i][bComp] = ((p ) & 0x1f) * (1.0F / 31.0F);
3155 rgba[i][aComp] = 1.0F;
3156 }
3157 }
3158 else {
3159 const GLushort *ussrc = (const GLushort *) src;
3160 GLuint i;
3161 for (i = 0; i < n; i ++) {
3162 GLushort p = ussrc[i];
3163 rgba[i][rComp] = ((p >> 11) ) * (1.0F / 31.0F);
3164 rgba[i][gComp] = ((p >> 5) & 0x3f) * (1.0F / 63.0F);
3165 rgba[i][bComp] = ((p ) & 0x1f) * (1.0F / 31.0F);
3166 rgba[i][aComp] = 1.0F;
3167 }
3168 }
3169 break;
3170 case GL_UNSIGNED_SHORT_5_6_5_REV:
3171 if (swapBytes) {
3172 const GLushort *ussrc = (const GLushort *) src;
3173 GLuint i;
3174 for (i = 0; i < n; i ++) {
3175 GLushort p = ussrc[i];
3176 SWAP2BYTE(p);
3177 rgba[i][rComp] = ((p ) & 0x1f) * (1.0F / 31.0F);
3178 rgba[i][gComp] = ((p >> 5) & 0x3f) * (1.0F / 63.0F);
3179 rgba[i][bComp] = ((p >> 11) ) * (1.0F / 31.0F);
3180 rgba[i][aComp] = 1.0F;
3181 }
3182 }
3183 else {
3184 const GLushort *ussrc = (const GLushort *) src;
3185 GLuint i;
3186 for (i = 0; i < n; i ++) {
3187 GLushort p = ussrc[i];
3188 rgba[i][rComp] = ((p ) & 0x1f) * (1.0F / 31.0F);
3189 rgba[i][gComp] = ((p >> 5) & 0x3f) * (1.0F / 63.0F);
3190 rgba[i][bComp] = ((p >> 11) ) * (1.0F / 31.0F);
3191 rgba[i][aComp] = 1.0F;
3192 }
3193 }
3194 break;
3195 case GL_UNSIGNED_SHORT_4_4_4_4:
3196 if (swapBytes) {
3197 const GLushort *ussrc = (const GLushort *) src;
3198 GLuint i;
3199 for (i = 0; i < n; i ++) {
3200 GLushort p = ussrc[i];
3201 SWAP2BYTE(p);
3202 rgba[i][rComp] = ((p >> 12) ) * (1.0F / 15.0F);
3203 rgba[i][gComp] = ((p >> 8) & 0xf) * (1.0F / 15.0F);
3204 rgba[i][bComp] = ((p >> 4) & 0xf) * (1.0F / 15.0F);
3205 rgba[i][aComp] = ((p ) & 0xf) * (1.0F / 15.0F);
3206 }
3207 }
3208 else {
3209 const GLushort *ussrc = (const GLushort *) src;
3210 GLuint i;
3211 for (i = 0; i < n; i ++) {
3212 GLushort p = ussrc[i];
3213 rgba[i][rComp] = ((p >> 12) ) * (1.0F / 15.0F);
3214 rgba[i][gComp] = ((p >> 8) & 0xf) * (1.0F / 15.0F);
3215 rgba[i][bComp] = ((p >> 4) & 0xf) * (1.0F / 15.0F);
3216 rgba[i][aComp] = ((p ) & 0xf) * (1.0F / 15.0F);
3217 }
3218 }
3219 break;
3220 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
3221 if (swapBytes) {
3222 const GLushort *ussrc = (const GLushort *) src;
3223 GLuint i;
3224 for (i = 0; i < n; i ++) {
3225 GLushort p = ussrc[i];
3226 SWAP2BYTE(p);
3227 rgba[i][rComp] = ((p ) & 0xf) * (1.0F / 15.0F);
3228 rgba[i][gComp] = ((p >> 4) & 0xf) * (1.0F / 15.0F);
3229 rgba[i][bComp] = ((p >> 8) & 0xf) * (1.0F / 15.0F);
3230 rgba[i][aComp] = ((p >> 12) ) * (1.0F / 15.0F);
3231 }
3232 }
3233 else {
3234 const GLushort *ussrc = (const GLushort *) src;
3235 GLuint i;
3236 for (i = 0; i < n; i ++) {
3237 GLushort p = ussrc[i];
3238 rgba[i][rComp] = ((p ) & 0xf) * (1.0F / 15.0F);
3239 rgba[i][gComp] = ((p >> 4) & 0xf) * (1.0F / 15.0F);
3240 rgba[i][bComp] = ((p >> 8) & 0xf) * (1.0F / 15.0F);
3241 rgba[i][aComp] = ((p >> 12) ) * (1.0F / 15.0F);
3242 }
3243 }
3244 break;
3245 case GL_UNSIGNED_SHORT_5_5_5_1:
3246 if (swapBytes) {
3247 const GLushort *ussrc = (const GLushort *) src;
3248 GLuint i;
3249 for (i = 0; i < n; i ++) {
3250 GLushort p = ussrc[i];
3251 SWAP2BYTE(p);
3252 rgba[i][rComp] = ((p >> 11) ) * (1.0F / 31.0F);
3253 rgba[i][gComp] = ((p >> 6) & 0x1f) * (1.0F / 31.0F);
3254 rgba[i][bComp] = ((p >> 1) & 0x1f) * (1.0F / 31.0F);
3255 rgba[i][aComp] = ((p ) & 0x1) * (1.0F / 1.0F);
3256 }
3257 }
3258 else {
3259 const GLushort *ussrc = (const GLushort *) src;
3260 GLuint i;
3261 for (i = 0; i < n; i ++) {
3262 GLushort p = ussrc[i];
3263 rgba[i][rComp] = ((p >> 11) ) * (1.0F / 31.0F);
3264 rgba[i][gComp] = ((p >> 6) & 0x1f) * (1.0F / 31.0F);
3265 rgba[i][bComp] = ((p >> 1) & 0x1f) * (1.0F / 31.0F);
3266 rgba[i][aComp] = ((p ) & 0x1) * (1.0F / 1.0F);
3267 }
3268 }
3269 break;
3270 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
3271 if (swapBytes) {
3272 const GLushort *ussrc = (const GLushort *) src;
3273 GLuint i;
3274 for (i = 0; i < n; i ++) {
3275 GLushort p = ussrc[i];
3276 SWAP2BYTE(p);
3277 rgba[i][rComp] = ((p ) & 0x1f) * (1.0F / 31.0F);
3278 rgba[i][gComp] = ((p >> 5) & 0x1f) * (1.0F / 31.0F);
3279 rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
3280 rgba[i][aComp] = ((p >> 15) ) * (1.0F / 1.0F);
3281 }
3282 }
3283 else {
3284 const GLushort *ussrc = (const GLushort *) src;
3285 GLuint i;
3286 for (i = 0; i < n; i ++) {
3287 GLushort p = ussrc[i];
3288 rgba[i][rComp] = ((p ) & 0x1f) * (1.0F / 31.0F);
3289 rgba[i][gComp] = ((p >> 5) & 0x1f) * (1.0F / 31.0F);
3290 rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
3291 rgba[i][aComp] = ((p >> 15) ) * (1.0F / 1.0F);
3292 }
3293 }
3294 break;
3295 case GL_UNSIGNED_INT_8_8_8_8:
3296 if (swapBytes) {
3297 const GLuint *uisrc = (const GLuint *) src;
3298 GLuint i;
3299 for (i = 0; i < n; i ++) {
3300 GLuint p = uisrc[i];
3301 rgba[i][rComp] = UBYTE_TO_FLOAT((p ) & 0xff);
3302 rgba[i][gComp] = UBYTE_TO_FLOAT((p >> 8) & 0xff);
3303 rgba[i][bComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
3304 rgba[i][aComp] = UBYTE_TO_FLOAT((p >> 24) );
3305 }
3306 }
3307 else {
3308 const GLuint *uisrc = (const GLuint *) src;
3309 GLuint i;
3310 for (i = 0; i < n; i ++) {
3311 GLuint p = uisrc[i];
3312 rgba[i][rComp] = UBYTE_TO_FLOAT((p >> 24) );
3313 rgba[i][gComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
3314 rgba[i][bComp] = UBYTE_TO_FLOAT((p >> 8) & 0xff);
3315 rgba[i][aComp] = UBYTE_TO_FLOAT((p ) & 0xff);
3316 }
3317 }
3318 break;
3319 case GL_UNSIGNED_INT_8_8_8_8_REV:
3320 if (swapBytes) {
3321 const GLuint *uisrc = (const GLuint *) src;
3322 GLuint i;
3323 for (i = 0; i < n; i ++) {
3324 GLuint p = uisrc[i];
3325 rgba[i][rComp] = UBYTE_TO_FLOAT((p >> 24) );
3326 rgba[i][gComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
3327 rgba[i][bComp] = UBYTE_TO_FLOAT((p >> 8) & 0xff);
3328 rgba[i][aComp] = UBYTE_TO_FLOAT((p ) & 0xff);
3329 }
3330 }
3331 else {
3332 const GLuint *uisrc = (const GLuint *) src;
3333 GLuint i;
3334 for (i = 0; i < n; i ++) {
3335 GLuint p = uisrc[i];
3336 rgba[i][rComp] = UBYTE_TO_FLOAT((p ) & 0xff);
3337 rgba[i][gComp] = UBYTE_TO_FLOAT((p >> 8) & 0xff);
3338 rgba[i][bComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
3339 rgba[i][aComp] = UBYTE_TO_FLOAT((p >> 24) );
3340 }
3341 }
3342 break;
3343 case GL_UNSIGNED_INT_10_10_10_2:
3344 if (swapBytes) {
3345 const GLuint *uisrc = (const GLuint *) src;
3346 GLuint i;
3347 for (i = 0; i < n; i ++) {
3348 GLuint p = uisrc[i];
3349 SWAP4BYTE(p);
3350 rgba[i][rComp] = ((p >> 22) ) * (1.0F / 1023.0F);
3351 rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
3352 rgba[i][bComp] = ((p >> 2) & 0x3ff) * (1.0F / 1023.0F);
3353 rgba[i][aComp] = ((p ) & 0x3 ) * (1.0F / 3.0F);
3354 }
3355 }
3356 else {
3357 const GLuint *uisrc = (const GLuint *) src;
3358 GLuint i;
3359 for (i = 0; i < n; i ++) {
3360 GLuint p = uisrc[i];
3361 rgba[i][rComp] = ((p >> 22) ) * (1.0F / 1023.0F);
3362 rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
3363 rgba[i][bComp] = ((p >> 2) & 0x3ff) * (1.0F / 1023.0F);
3364 rgba[i][aComp] = ((p ) & 0x3 ) * (1.0F / 3.0F);
3365 }
3366 }
3367 break;
3368 case GL_UNSIGNED_INT_2_10_10_10_REV:
3369 if (swapBytes) {
3370 const GLuint *uisrc = (const GLuint *) src;
3371 GLuint i;
3372 for (i = 0; i < n; i ++) {
3373 GLuint p = uisrc[i];
3374 SWAP4BYTE(p);
3375 rgba[i][rComp] = ((p ) & 0x3ff) * (1.0F / 1023.0F);
3376 rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
3377 rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
3378 rgba[i][aComp] = ((p >> 30) ) * (1.0F / 3.0F);
3379 }
3380 }
3381 else {
3382 const GLuint *uisrc = (const GLuint *) src;
3383 GLuint i;
3384 for (i = 0; i < n; i ++) {
3385 GLuint p = uisrc[i];
3386 rgba[i][rComp] = ((p ) & 0x3ff) * (1.0F / 1023.0F);
3387 rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
3388 rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
3389 rgba[i][aComp] = ((p >> 30) ) * (1.0F / 3.0F);
3390 }
3391 }
3392 break;
3393 default:
3394 _mesa_problem(NULL, "bad srcType in extract float data");
3395 break;
3396 }
3397 }
3398
3399
3400 /*
3401 * Unpack a row of color image data from a client buffer according to
3402 * the pixel unpacking parameters.
3403 * Return GLchan values in the specified dest image format.
3404 * This is used by glDrawPixels and glTexImage?D().
3405 * \param ctx - the context
3406 * n - number of pixels in the span
3407 * dstFormat - format of destination color array
3408 * dest - the destination color array
3409 * srcFormat - source image format
3410 * srcType - source image data type
3411 * source - source image pointer
3412 * srcPacking - pixel unpacking parameters
3413 * transferOps - bitmask of IMAGE_*_BIT values of operations to apply
3414 *
3415 * XXX perhaps expand this to process whole images someday.
3416 */
3417 void
3418 _mesa_unpack_color_span_chan( GLcontext *ctx,
3419 GLuint n, GLenum dstFormat, GLchan dest[],
3420 GLenum srcFormat, GLenum srcType,
3421 const GLvoid *source,
3422 const struct gl_pixelstore_attrib *srcPacking,
3423 GLbitfield transferOps )
3424 {
3425 ASSERT(dstFormat == GL_ALPHA ||
3426 dstFormat == GL_LUMINANCE ||
3427 dstFormat == GL_LUMINANCE_ALPHA ||
3428 dstFormat == GL_INTENSITY ||
3429 dstFormat == GL_RGB ||
3430 dstFormat == GL_RGBA ||
3431 dstFormat == GL_COLOR_INDEX);
3432
3433 ASSERT(srcFormat == GL_RED ||
3434 srcFormat == GL_GREEN ||
3435 srcFormat == GL_BLUE ||
3436 srcFormat == GL_ALPHA ||
3437 srcFormat == GL_LUMINANCE ||
3438 srcFormat == GL_LUMINANCE_ALPHA ||
3439 srcFormat == GL_INTENSITY ||
3440 srcFormat == GL_RGB ||
3441 srcFormat == GL_BGR ||
3442 srcFormat == GL_RGBA ||
3443 srcFormat == GL_BGRA ||
3444 srcFormat == GL_ABGR_EXT ||
3445 srcFormat == GL_COLOR_INDEX);
3446
3447 ASSERT(srcType == GL_BITMAP ||
3448 srcType == GL_UNSIGNED_BYTE ||
3449 srcType == GL_BYTE ||
3450 srcType == GL_UNSIGNED_SHORT ||
3451 srcType == GL_SHORT ||
3452 srcType == GL_UNSIGNED_INT ||
3453 srcType == GL_INT ||
3454 srcType == GL_HALF_FLOAT_ARB ||
3455 srcType == GL_FLOAT ||
3456 srcType == GL_UNSIGNED_BYTE_3_3_2 ||
3457 srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
3458 srcType == GL_UNSIGNED_SHORT_5_6_5 ||
3459 srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
3460 srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
3461 srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
3462 srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
3463 srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
3464 srcType == GL_UNSIGNED_INT_8_8_8_8 ||
3465 srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
3466 srcType == GL_UNSIGNED_INT_10_10_10_2 ||
3467 srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
3468
3469 /* Try simple cases first */
3470 if (transferOps == 0) {
3471 if (srcType == CHAN_TYPE) {
3472 if (dstFormat == GL_RGBA) {
3473 if (srcFormat == GL_RGBA) {
3474 _mesa_memcpy( dest, source, n * 4 * sizeof(GLchan) );
3475 return;
3476 }
3477 else if (srcFormat == GL_RGB) {
3478 GLuint i;
3479 const GLchan *src = (const GLchan *) source;
3480 GLchan *dst = dest;
3481 for (i = 0; i < n; i++) {
3482 dst[0] = src[0];
3483 dst[1] = src[1];
3484 dst[2] = src[2];
3485 dst[3] = CHAN_MAX;
3486 src += 3;
3487 dst += 4;
3488 }
3489 return;
3490 }
3491 }
3492 else if (dstFormat == GL_RGB) {
3493 if (srcFormat == GL_RGB) {
3494 _mesa_memcpy( dest, source, n * 3 * sizeof(GLchan) );
3495 return;
3496 }
3497 else if (srcFormat == GL_RGBA) {
3498 GLuint i;
3499 const GLchan *src = (const GLchan *) source;
3500 GLchan *dst = dest;
3501 for (i = 0; i < n; i++) {
3502 dst[0] = src[0];
3503 dst[1] = src[1];
3504 dst[2] = src[2];
3505 src += 4;
3506 dst += 3;
3507 }
3508 return;
3509 }
3510 }
3511 else if (dstFormat == srcFormat) {
3512 GLint comps = _mesa_components_in_format(srcFormat);
3513 assert(comps > 0);
3514 _mesa_memcpy( dest, source, n * comps * sizeof(GLchan) );
3515 return;
3516 }
3517 }
3518 /*
3519 * Common situation, loading 8bit RGBA/RGB source images
3520 * into 16/32 bit destination. (OSMesa16/32)
3521 */
3522 else if (srcType == GL_UNSIGNED_BYTE) {
3523 if (dstFormat == GL_RGBA) {
3524 if (srcFormat == GL_RGB) {
3525 GLuint i;
3526 const GLubyte *src = (const GLubyte *) source;
3527 GLchan *dst = dest;
3528 for (i = 0; i < n; i++) {
3529 dst[0] = UBYTE_TO_CHAN(src[0]);
3530 dst[1] = UBYTE_TO_CHAN(src[1]);
3531 dst[2] = UBYTE_TO_CHAN(src[2]);
3532 dst[3] = CHAN_MAX;
3533 src += 3;
3534 dst += 4;
3535 }
3536 return;
3537 }
3538 else if (srcFormat == GL_RGBA) {
3539 GLuint i;
3540 const GLubyte *src = (const GLubyte *) source;
3541 GLchan *dst = dest;
3542 for (i = 0; i < n; i++) {
3543 dst[0] = UBYTE_TO_CHAN(src[0]);
3544 dst[1] = UBYTE_TO_CHAN(src[1]);
3545 dst[2] = UBYTE_TO_CHAN(src[2]);
3546 dst[3] = UBYTE_TO_CHAN(src[3]);
3547 src += 4;
3548 dst += 4;
3549 }
3550 return;
3551 }
3552 }
3553 else if (dstFormat == GL_RGB) {
3554 if (srcFormat == GL_RGB) {
3555 GLuint i;
3556 const GLubyte *src = (const GLubyte *) source;
3557 GLchan *dst = dest;
3558 for (i = 0; i < n; i++) {
3559 dst[0] = UBYTE_TO_CHAN(src[0]);
3560 dst[1] = UBYTE_TO_CHAN(src[1]);
3561 dst[2] = UBYTE_TO_CHAN(src[2]);
3562 src += 3;
3563 dst += 3;
3564 }
3565 return;
3566 }
3567 else if (srcFormat == GL_RGBA) {
3568 GLuint i;
3569 const GLubyte *src = (const GLubyte *) source;
3570 GLchan *dst = dest;
3571 for (i = 0; i < n; i++) {
3572 dst[0] = UBYTE_TO_CHAN(src[0]);
3573 dst[1] = UBYTE_TO_CHAN(src[1]);
3574 dst[2] = UBYTE_TO_CHAN(src[2]);
3575 src += 4;
3576 dst += 3;
3577 }
3578 return;
3579 }
3580 }
3581 }
3582 }
3583
3584
3585 /* general solution begins here */
3586 {
3587 GLint dstComponents;
3588 GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
3589 GLint dstLuminanceIndex, dstIntensityIndex;
3590 GLfloat rgba[MAX_WIDTH][4];
3591
3592 dstComponents = _mesa_components_in_format( dstFormat );
3593 /* source & dest image formats should have been error checked by now */
3594 assert(dstComponents > 0);
3595
3596 /*
3597 * Extract image data and convert to RGBA floats
3598 */
3599 assert(n <= MAX_WIDTH);
3600 if (srcFormat == GL_COLOR_INDEX) {
3601 GLuint indexes[MAX_WIDTH];
3602 extract_uint_indexes(n, indexes, srcFormat, srcType, source,
3603 srcPacking);
3604
3605 if (dstFormat == GL_COLOR_INDEX) {
3606 GLuint i;
3607 _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
3608 /* convert to GLchan and return */
3609 for (i = 0; i < n; i++) {
3610 dest[i] = (GLchan) (indexes[i] & 0xff);
3611 }
3612 return;
3613 }
3614 else {
3615 /* Convert indexes to RGBA */
3616 if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
3617 shift_and_offset_ci(ctx, n, indexes);
3618 }
3619 _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
3620 }
3621
3622 /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
3623 * with color indexes.
3624 */
3625 transferOps &= ~(IMAGE_SCALE_BIAS_BIT | IMAGE_MAP_COLOR_BIT);
3626 }
3627 else {
3628 /* non-color index data */
3629 extract_float_rgba(n, rgba, srcFormat, srcType, source,
3630 srcPacking->SwapBytes);
3631 }
3632
3633 /* Need to clamp if returning GLubytes or GLushorts */
3634 #if CHAN_TYPE != GL_FLOAT
3635 transferOps |= IMAGE_CLAMP_BIT;
3636 #endif
3637
3638 if (transferOps) {
3639 _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
3640 }
3641
3642 /* Now determine which color channels we need to produce.
3643 * And determine the dest index (offset) within each color tuple.
3644 */
3645 switch (dstFormat) {
3646 case GL_ALPHA:
3647 dstAlphaIndex = 0;
3648 dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
3649 dstLuminanceIndex = dstIntensityIndex = -1;
3650 break;
3651 case GL_LUMINANCE:
3652 dstLuminanceIndex = 0;
3653 dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
3654 dstIntensityIndex = -1;
3655 break;
3656 case GL_LUMINANCE_ALPHA:
3657 dstLuminanceIndex = 0;
3658 dstAlphaIndex = 1;
3659 dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
3660 dstIntensityIndex = -1;
3661 break;
3662 case GL_INTENSITY:
3663 dstIntensityIndex = 0;
3664 dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
3665 dstLuminanceIndex = -1;
3666 break;
3667 case GL_RGB:
3668 dstRedIndex = 0;
3669 dstGreenIndex = 1;
3670 dstBlueIndex = 2;
3671 dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
3672 break;
3673 case GL_RGBA:
3674 dstRedIndex = 0;
3675 dstGreenIndex = 1;
3676 dstBlueIndex = 2;
3677 dstAlphaIndex = 3;
3678 dstLuminanceIndex = dstIntensityIndex = -1;
3679 break;
3680 default:
3681 _mesa_problem(ctx, "bad dstFormat in _mesa_unpack_chan_span()");
3682 return;
3683 }
3684
3685
3686 /* Now return the GLchan data in the requested dstFormat */
3687
3688 if (dstRedIndex >= 0) {
3689 GLchan *dst = dest;
3690 GLuint i;
3691 for (i = 0; i < n; i++) {
3692 CLAMPED_FLOAT_TO_CHAN(dst[dstRedIndex], rgba[i][RCOMP]);
3693 dst += dstComponents;
3694 }
3695 }
3696
3697 if (dstGreenIndex >= 0) {
3698 GLchan *dst = dest;
3699 GLuint i;
3700 for (i = 0; i < n; i++) {
3701 CLAMPED_FLOAT_TO_CHAN(dst[dstGreenIndex], rgba[i][GCOMP]);
3702 dst += dstComponents;
3703 }
3704 }
3705
3706 if (dstBlueIndex >= 0) {
3707 GLchan *dst = dest;
3708 GLuint i;
3709 for (i = 0; i < n; i++) {
3710 CLAMPED_FLOAT_TO_CHAN(dst[dstBlueIndex], rgba[i][BCOMP]);
3711 dst += dstComponents;
3712 }
3713 }
3714
3715 if (dstAlphaIndex >= 0) {
3716 GLchan *dst = dest;
3717 GLuint i;
3718 for (i = 0; i < n; i++) {
3719 CLAMPED_FLOAT_TO_CHAN(dst[dstAlphaIndex], rgba[i][ACOMP]);
3720 dst += dstComponents;
3721 }
3722 }
3723
3724 if (dstIntensityIndex >= 0) {
3725 GLchan *dst = dest;
3726 GLuint i;
3727 assert(dstIntensityIndex == 0);
3728 assert(dstComponents == 1);
3729 for (i = 0; i < n; i++) {
3730 /* Intensity comes from red channel */
3731 CLAMPED_FLOAT_TO_CHAN(dst[i], rgba[i][RCOMP]);
3732 }
3733 }
3734
3735 if (dstLuminanceIndex >= 0) {
3736 GLchan *dst = dest;
3737 GLuint i;
3738 assert(dstLuminanceIndex == 0);
3739 for (i = 0; i < n; i++) {
3740 /* Luminance comes from red channel */
3741 CLAMPED_FLOAT_TO_CHAN(dst[0], rgba[i][RCOMP]);
3742 dst += dstComponents;
3743 }
3744 }
3745 }
3746 }
3747
3748
3749 /**
3750 * Same as _mesa_unpack_color_span_chan(), but return GLfloat data
3751 * instead of GLchan.
3752 */
3753 void
3754 _mesa_unpack_color_span_float( GLcontext *ctx,
3755 GLuint n, GLenum dstFormat, GLfloat dest[],
3756 GLenum srcFormat, GLenum srcType,
3757 const GLvoid *source,
3758 const struct gl_pixelstore_attrib *srcPacking,
3759 GLbitfield transferOps )
3760 {
3761 ASSERT(dstFormat == GL_ALPHA ||
3762 dstFormat == GL_LUMINANCE ||
3763 dstFormat == GL_LUMINANCE_ALPHA ||
3764 dstFormat == GL_INTENSITY ||
3765 dstFormat == GL_RGB ||
3766 dstFormat == GL_RGBA ||
3767 dstFormat == GL_COLOR_INDEX);
3768
3769 ASSERT(srcFormat == GL_RED ||
3770 srcFormat == GL_GREEN ||
3771 srcFormat == GL_BLUE ||
3772 srcFormat == GL_ALPHA ||
3773 srcFormat == GL_LUMINANCE ||
3774 srcFormat == GL_LUMINANCE_ALPHA ||
3775 srcFormat == GL_INTENSITY ||
3776 srcFormat == GL_RGB ||
3777 srcFormat == GL_BGR ||
3778 srcFormat == GL_RGBA ||
3779 srcFormat == GL_BGRA ||
3780 srcFormat == GL_ABGR_EXT ||
3781 srcFormat == GL_COLOR_INDEX);
3782
3783 ASSERT(srcType == GL_BITMAP ||
3784 srcType == GL_UNSIGNED_BYTE ||
3785 srcType == GL_BYTE ||
3786 srcType == GL_UNSIGNED_SHORT ||
3787 srcType == GL_SHORT ||
3788 srcType == GL_UNSIGNED_INT ||
3789 srcType == GL_INT ||
3790 srcType == GL_HALF_FLOAT_ARB ||
3791 srcType == GL_FLOAT ||
3792 srcType == GL_UNSIGNED_BYTE_3_3_2 ||
3793 srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
3794 srcType == GL_UNSIGNED_SHORT_5_6_5 ||
3795 srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
3796 srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
3797 srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
3798 srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
3799 srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
3800 srcType == GL_UNSIGNED_INT_8_8_8_8 ||
3801 srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
3802 srcType == GL_UNSIGNED_INT_10_10_10_2 ||
3803 srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
3804
3805 /* general solution, no special cases, yet */
3806 {
3807 GLint dstComponents;
3808 GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
3809 GLint dstLuminanceIndex, dstIntensityIndex;
3810 GLfloat rgba[MAX_WIDTH][4];
3811
3812 dstComponents = _mesa_components_in_format( dstFormat );
3813 /* source & dest image formats should have been error checked by now */
3814 assert(dstComponents > 0);
3815
3816 /*
3817 * Extract image data and convert to RGBA floats
3818 */
3819 assert(n <= MAX_WIDTH);
3820 if (srcFormat == GL_COLOR_INDEX) {
3821 GLuint indexes[MAX_WIDTH];
3822 extract_uint_indexes(n, indexes, srcFormat, srcType, source,
3823 srcPacking);
3824
3825 if (dstFormat == GL_COLOR_INDEX) {
3826 GLuint i;
3827 _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
3828 /* convert to GLchan and return */
3829 for (i = 0; i < n; i++) {
3830 dest[i] = (GLchan) (indexes[i] & 0xff);
3831 }
3832 return;
3833 }
3834 else {
3835 /* Convert indexes to RGBA */
3836 if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
3837 shift_and_offset_ci(ctx, n, indexes);
3838 }
3839 _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
3840 }
3841
3842 /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
3843 * with color indexes.
3844 */
3845 transferOps &= ~(IMAGE_SCALE_BIAS_BIT | IMAGE_MAP_COLOR_BIT);
3846 }
3847 else {
3848 /* non-color index data */
3849 extract_float_rgba(n, rgba, srcFormat, srcType, source,
3850 srcPacking->SwapBytes);
3851 }
3852
3853 if (transferOps) {
3854 _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
3855 }
3856
3857 /* Now determine which color channels we need to produce.
3858 * And determine the dest index (offset) within each color tuple.
3859 */
3860 switch (dstFormat) {
3861 case GL_ALPHA:
3862 dstAlphaIndex = 0;
3863 dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
3864 dstLuminanceIndex = dstIntensityIndex = -1;
3865 break;
3866 case GL_LUMINANCE:
3867 dstLuminanceIndex = 0;
3868 dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
3869 dstIntensityIndex = -1;
3870 break;
3871 case GL_LUMINANCE_ALPHA:
3872 dstLuminanceIndex = 0;
3873 dstAlphaIndex = 1;
3874 dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
3875 dstIntensityIndex = -1;
3876 break;
3877 case GL_INTENSITY:
3878 dstIntensityIndex = 0;
3879 dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
3880 dstLuminanceIndex = -1;
3881 break;
3882 case GL_RGB:
3883 dstRedIndex = 0;
3884 dstGreenIndex = 1;
3885 dstBlueIndex = 2;
3886 dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
3887 break;
3888 case GL_RGBA:
3889 dstRedIndex = 0;
3890 dstGreenIndex = 1;
3891 dstBlueIndex = 2;
3892 dstAlphaIndex = 3;
3893 dstLuminanceIndex = dstIntensityIndex = -1;
3894 break;
3895 default:
3896 _mesa_problem(ctx, "bad dstFormat in _mesa_unpack_color_span_float()");
3897 return;
3898 }
3899
3900 /* Now pack results in the requested dstFormat */
3901 if (dstRedIndex >= 0) {
3902 GLfloat *dst = dest;
3903 GLuint i;
3904 for (i = 0; i < n; i++) {
3905 dst[dstRedIndex] = rgba[i][RCOMP];
3906 dst += dstComponents;
3907 }
3908 }
3909
3910 if (dstGreenIndex >= 0) {
3911 GLfloat *dst = dest;
3912 GLuint i;
3913 for (i = 0; i < n; i++) {
3914 dst[dstGreenIndex] = rgba[i][GCOMP];
3915 dst += dstComponents;
3916 }
3917 }
3918
3919 if (dstBlueIndex >= 0) {
3920 GLfloat *dst = dest;
3921 GLuint i;
3922 for (i = 0; i < n; i++) {
3923 dst[dstBlueIndex] = rgba[i][BCOMP];
3924 dst += dstComponents;
3925 }
3926 }
3927
3928 if (dstAlphaIndex >= 0) {
3929 GLfloat *dst = dest;
3930 GLuint i;
3931 for (i = 0; i < n; i++) {
3932 dst[dstAlphaIndex] = rgba[i][ACOMP];
3933 dst += dstComponents;
3934 }
3935 }
3936
3937 if (dstIntensityIndex >= 0) {
3938 GLfloat *dst = dest;
3939 GLuint i;
3940 assert(dstIntensityIndex == 0);
3941 assert(dstComponents == 1);
3942 for (i = 0; i < n; i++) {
3943 /* Intensity comes from red channel */
3944 dst[i] = rgba[i][RCOMP];
3945 }
3946 }
3947
3948 if (dstLuminanceIndex >= 0) {
3949 GLfloat *dst = dest;
3950 GLuint i;
3951 assert(dstLuminanceIndex == 0);
3952 for (i = 0; i < n; i++) {
3953 /* Luminance comes from red channel */
3954 dst[0] = rgba[i][RCOMP];
3955 dst += dstComponents;
3956 }
3957 }
3958 }
3959 }
3960
3961 /**
3962 * Similar to _mesa_unpack_color_span_float(), but for dudv data instead of rgba,
3963 * directly return GLbyte data, no transfer ops apply.
3964 */
3965 void
3966 _mesa_unpack_dudv_span_byte( GLcontext *ctx,
3967 GLuint n, GLenum dstFormat, GLbyte dest[],
3968 GLenum srcFormat, GLenum srcType,
3969 const GLvoid *source,
3970 const struct gl_pixelstore_attrib *srcPacking,
3971 GLbitfield transferOps )
3972 {
3973 ASSERT(dstFormat == GL_DUDV_ATI);
3974 ASSERT(srcFormat == GL_DUDV_ATI);
3975
3976 ASSERT(srcType == GL_UNSIGNED_BYTE ||
3977 srcType == GL_BYTE ||
3978 srcType == GL_UNSIGNED_SHORT ||
3979 srcType == GL_SHORT ||
3980 srcType == GL_UNSIGNED_INT ||
3981 srcType == GL_INT ||
3982 srcType == GL_HALF_FLOAT_ARB ||
3983 srcType == GL_FLOAT);
3984
3985 /* general solution */
3986 {
3987 GLint dstComponents;
3988 GLfloat rgba[MAX_WIDTH][4];
3989 GLbyte *dst = dest;
3990 GLuint i;
3991
3992 dstComponents = _mesa_components_in_format( dstFormat );
3993 /* source & dest image formats should have been error checked by now */
3994 assert(dstComponents > 0);
3995
3996 /*
3997 * Extract image data and convert to RGBA floats
3998 */
3999 assert(n <= MAX_WIDTH);
4000 extract_float_rgba(n, rgba, srcFormat, srcType, source,
4001 srcPacking->SwapBytes);
4002
4003
4004 /* Now determine which color channels we need to produce.
4005 * And determine the dest index (offset) within each color tuple.
4006 */
4007
4008 /* Now pack results in the requested dstFormat */
4009 for (i = 0; i < n; i++) {
4010 /* not sure - need clamp[-1,1] here? */
4011 dst[0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
4012 dst[1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
4013 dst += dstComponents;
4014 }
4015 }
4016 }
4017
4018 /*
4019 * Unpack a row of color index data from a client buffer according to
4020 * the pixel unpacking parameters.
4021 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
4022 *
4023 * Args: ctx - the context
4024 * n - number of pixels
4025 * dstType - destination data type
4026 * dest - destination array
4027 * srcType - source pixel type
4028 * source - source data pointer
4029 * srcPacking - pixel unpacking parameters
4030 * transferOps - the pixel transfer operations to apply
4031 */
4032 void
4033 _mesa_unpack_index_span( const GLcontext *ctx, GLuint n,
4034 GLenum dstType, GLvoid *dest,
4035 GLenum srcType, const GLvoid *source,
4036 const struct gl_pixelstore_attrib *srcPacking,
4037 GLbitfield transferOps )
4038 {
4039 ASSERT(srcType == GL_BITMAP ||
4040 srcType == GL_UNSIGNED_BYTE ||
4041 srcType == GL_BYTE ||
4042 srcType == GL_UNSIGNED_SHORT ||
4043 srcType == GL_SHORT ||
4044 srcType == GL_UNSIGNED_INT ||
4045 srcType == GL_INT ||
4046 srcType == GL_HALF_FLOAT_ARB ||
4047 srcType == GL_FLOAT);
4048
4049 ASSERT(dstType == GL_UNSIGNED_BYTE ||
4050 dstType == GL_UNSIGNED_SHORT ||
4051 dstType == GL_UNSIGNED_INT);
4052
4053
4054 transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
4055
4056 /*
4057 * Try simple cases first
4058 */
4059 if (transferOps == 0 && srcType == GL_UNSIGNED_BYTE
4060 && dstType == GL_UNSIGNED_BYTE) {
4061 _mesa_memcpy(dest, source, n * sizeof(GLubyte));
4062 }
4063 else if (transferOps == 0 && srcType == GL_UNSIGNED_INT
4064 && dstType == GL_UNSIGNED_INT && !srcPacking->SwapBytes) {
4065 _mesa_memcpy(dest, source, n * sizeof(GLuint));
4066 }
4067 else {
4068 /*
4069 * general solution
4070 */
4071 GLuint indexes[MAX_WIDTH];
4072 assert(n <= MAX_WIDTH);
4073
4074 extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
4075 srcPacking);
4076
4077 if (transferOps)
4078 _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
4079
4080 /* convert to dest type */
4081 switch (dstType) {
4082 case GL_UNSIGNED_BYTE:
4083 {
4084 GLubyte *dst = (GLubyte *) dest;
4085 GLuint i;
4086 for (i = 0; i < n; i++) {
4087 dst[i] = (GLubyte) (indexes[i] & 0xff);
4088 }
4089 }
4090 break;
4091 case GL_UNSIGNED_SHORT:
4092 {
4093 GLuint *dst = (GLuint *) dest;
4094 GLuint i;
4095 for (i = 0; i < n; i++) {
4096 dst[i] = (GLushort) (indexes[i] & 0xffff);
4097 }
4098 }
4099 break;
4100 case GL_UNSIGNED_INT:
4101 _mesa_memcpy(dest, indexes, n * sizeof(GLuint));
4102 break;
4103 default:
4104 _mesa_problem(ctx, "bad dstType in _mesa_unpack_index_span");
4105 }
4106 }
4107 }
4108
4109
4110 void
4111 _mesa_pack_index_span( const GLcontext *ctx, GLuint n,
4112 GLenum dstType, GLvoid *dest, const GLuint *source,
4113 const struct gl_pixelstore_attrib *dstPacking,
4114 GLbitfield transferOps )
4115 {
4116 GLuint indexes[MAX_WIDTH];
4117
4118 ASSERT(n <= MAX_WIDTH);
4119
4120 transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
4121
4122 if (transferOps & (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT)) {
4123 /* make a copy of input */
4124 _mesa_memcpy(indexes, source, n * sizeof(GLuint));
4125 _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
4126 source = indexes;
4127 }
4128
4129 switch (dstType) {
4130 case GL_UNSIGNED_BYTE:
4131 {
4132 GLubyte *dst = (GLubyte *) dest;
4133 GLuint i;
4134 for (i = 0; i < n; i++) {
4135 *dst++ = (GLubyte) source[i];
4136 }
4137 }
4138 break;
4139 case GL_BYTE:
4140 {
4141 GLbyte *dst = (GLbyte *) dest;
4142 GLuint i;
4143 for (i = 0; i < n; i++) {
4144 dst[i] = (GLbyte) source[i];
4145 }
4146 }
4147 break;
4148 case GL_UNSIGNED_SHORT:
4149 {
4150 GLushort *dst = (GLushort *) dest;
4151 GLuint i;
4152 for (i = 0; i < n; i++) {
4153 dst[i] = (GLushort) source[i];
4154 }
4155 if (dstPacking->SwapBytes) {
4156 _mesa_swap2( (GLushort *) dst, n );
4157 }
4158 }
4159 break;
4160 case GL_SHORT:
4161 {
4162 GLshort *dst = (GLshort *) dest;
4163 GLuint i;
4164 for (i = 0; i < n; i++) {
4165 dst[i] = (GLshort) source[i];
4166 }
4167 if (dstPacking->SwapBytes) {
4168 _mesa_swap2( (GLushort *) dst, n );
4169 }
4170 }
4171 break;
4172 case GL_UNSIGNED_INT:
4173 {
4174 GLuint *dst = (GLuint *) dest;
4175 GLuint i;
4176 for (i = 0; i < n; i++) {
4177 dst[i] = (GLuint) source[i];
4178 }
4179 if (dstPacking->SwapBytes) {
4180 _mesa_swap4( (GLuint *) dst, n );
4181 }
4182 }
4183 break;
4184 case GL_INT:
4185 {
4186 GLint *dst = (GLint *) dest;
4187 GLuint i;
4188 for (i = 0; i < n; i++) {
4189 dst[i] = (GLint) source[i];
4190 }
4191 if (dstPacking->SwapBytes) {
4192 _mesa_swap4( (GLuint *) dst, n );
4193 }
4194 }
4195 break;
4196 case GL_FLOAT:
4197 {
4198 GLfloat *dst = (GLfloat *) dest;
4199 GLuint i;
4200 for (i = 0; i < n; i++) {
4201 dst[i] = (GLfloat) source[i];
4202 }
4203 if (dstPacking->SwapBytes) {
4204 _mesa_swap4( (GLuint *) dst, n );
4205 }
4206 }
4207 break;
4208 case GL_HALF_FLOAT_ARB:
4209 {
4210 GLhalfARB *dst = (GLhalfARB *) dest;
4211 GLuint i;
4212 for (i = 0; i < n; i++) {
4213 dst[i] = _mesa_float_to_half((GLfloat) source[i]);
4214 }
4215 if (dstPacking->SwapBytes) {
4216 _mesa_swap2( (GLushort *) dst, n );
4217 }
4218 }
4219 break;
4220 default:
4221 _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
4222 }
4223 }
4224
4225
4226 /*
4227 * Unpack a row of stencil data from a client buffer according to
4228 * the pixel unpacking parameters.
4229 * This is (or will be) used by glDrawPixels
4230 *
4231 * Args: ctx - the context
4232 * n - number of pixels
4233 * dstType - destination data type
4234 * dest - destination array
4235 * srcType - source pixel type
4236 * source - source data pointer
4237 * srcPacking - pixel unpacking parameters
4238 * transferOps - apply offset/bias/lookup ops?
4239 */
4240 void
4241 _mesa_unpack_stencil_span( const GLcontext *ctx, GLuint n,
4242 GLenum dstType, GLvoid *dest,
4243 GLenum srcType, const GLvoid *source,
4244 const struct gl_pixelstore_attrib *srcPacking,
4245 GLbitfield transferOps )
4246 {
4247 ASSERT(srcType == GL_BITMAP ||
4248 srcType == GL_UNSIGNED_BYTE ||
4249 srcType == GL_BYTE ||
4250 srcType == GL_UNSIGNED_SHORT ||
4251 srcType == GL_SHORT ||
4252 srcType == GL_UNSIGNED_INT ||
4253 srcType == GL_INT ||
4254 srcType == GL_UNSIGNED_INT_24_8_EXT ||
4255 srcType == GL_HALF_FLOAT_ARB ||
4256 srcType == GL_FLOAT);
4257
4258 ASSERT(dstType == GL_UNSIGNED_BYTE ||
4259 dstType == GL_UNSIGNED_SHORT ||
4260 dstType == GL_UNSIGNED_INT);
4261
4262 /* only shift and offset apply to stencil */
4263 transferOps &= IMAGE_SHIFT_OFFSET_BIT;
4264
4265 /*
4266 * Try simple cases first
4267 */
4268 if (transferOps == 0 &&
4269 !ctx->Pixel.MapStencilFlag &&
4270 srcType == GL_UNSIGNED_BYTE &&
4271 dstType == GL_UNSIGNED_BYTE) {
4272 _mesa_memcpy(dest, source, n * sizeof(GLubyte));
4273 }
4274 else if (transferOps == 0 &&
4275 !ctx->Pixel.MapStencilFlag &&
4276 srcType == GL_UNSIGNED_INT &&
4277 dstType == GL_UNSIGNED_INT &&
4278 !srcPacking->SwapBytes) {
4279 _mesa_memcpy(dest, source, n * sizeof(GLuint));
4280 }
4281 else {
4282 /*
4283 * general solution
4284 */
4285 GLuint indexes[MAX_WIDTH];
4286 assert(n <= MAX_WIDTH);
4287
4288 extract_uint_indexes(n, indexes, GL_STENCIL_INDEX, srcType, source,
4289 srcPacking);
4290
4291 if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
4292 /* shift and offset indexes */
4293 shift_and_offset_ci(ctx, n, indexes);
4294 }
4295
4296 if (ctx->Pixel.MapStencilFlag) {
4297 /* Apply stencil lookup table */
4298 const GLuint mask = ctx->PixelMaps.StoS.Size - 1;
4299 GLuint i;
4300 for (i = 0; i < n; i++) {
4301 indexes[i] = (GLuint)ctx->PixelMaps.StoS.Map[ indexes[i] & mask ];
4302 }
4303 }
4304
4305 /* convert to dest type */
4306 switch (dstType) {
4307 case GL_UNSIGNED_BYTE:
4308 {
4309 GLubyte *dst = (GLubyte *) dest;
4310 GLuint i;
4311 for (i = 0; i < n; i++) {
4312 dst[i] = (GLubyte) (indexes[i] & 0xff);
4313 }
4314 }
4315 break;
4316 case GL_UNSIGNED_SHORT:
4317 {
4318 GLuint *dst = (GLuint *) dest;
4319 GLuint i;
4320 for (i = 0; i < n; i++) {
4321 dst[i] = (GLushort) (indexes[i] & 0xffff);
4322 }
4323 }
4324 break;
4325 case GL_UNSIGNED_INT:
4326 _mesa_memcpy(dest, indexes, n * sizeof(GLuint));
4327 break;
4328 default:
4329 _mesa_problem(ctx, "bad dstType in _mesa_unpack_stencil_span");
4330 }
4331 }
4332 }
4333
4334
4335 void
4336 _mesa_pack_stencil_span( const GLcontext *ctx, GLuint n,
4337 GLenum dstType, GLvoid *dest, const GLstencil *source,
4338 const struct gl_pixelstore_attrib *dstPacking )
4339 {
4340 GLstencil stencil[MAX_WIDTH];
4341
4342 ASSERT(n <= MAX_WIDTH);
4343
4344 if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset ||
4345 ctx->Pixel.MapStencilFlag) {
4346 /* make a copy of input */
4347 _mesa_memcpy(stencil, source, n * sizeof(GLstencil));
4348 _mesa_apply_stencil_transfer_ops(ctx, n, stencil);
4349 source = stencil;
4350 }
4351
4352 switch (dstType) {
4353 case GL_UNSIGNED_BYTE:
4354 if (sizeof(GLstencil) == 1) {
4355 _mesa_memcpy( dest, source, n );
4356 }
4357 else {
4358 GLubyte *dst = (GLubyte *) dest;
4359 GLuint i;
4360 for (i=0;i<n;i++) {
4361 dst[i] = (GLubyte) source[i];
4362 }
4363 }
4364 break;
4365 case GL_BYTE:
4366 {
4367 GLbyte *dst = (GLbyte *) dest;
4368 GLuint i;
4369 for (i=0;i<n;i++) {
4370 dst[i] = (GLbyte) (source[i] & 0x7f);
4371 }
4372 }
4373 break;
4374 case GL_UNSIGNED_SHORT:
4375 {
4376 GLushort *dst = (GLushort *) dest;
4377 GLuint i;
4378 for (i=0;i<n;i++) {
4379 dst[i] = (GLushort) source[i];
4380 }
4381 if (dstPacking->SwapBytes) {
4382 _mesa_swap2( (GLushort *) dst, n );
4383 }
4384 }
4385 break;
4386 case GL_SHORT:
4387 {
4388 GLshort *dst = (GLshort *) dest;
4389 GLuint i;
4390 for (i=0;i<n;i++) {
4391 dst[i] = (GLshort) source[i];
4392 }
4393 if (dstPacking->SwapBytes) {
4394 _mesa_swap2( (GLushort *) dst, n );
4395 }
4396 }
4397 break;
4398 case GL_UNSIGNED_INT:
4399 {
4400 GLuint *dst = (GLuint *) dest;
4401 GLuint i;
4402 for (i=0;i<n;i++) {
4403 dst[i] = (GLuint) source[i];
4404 }
4405 if (dstPacking->SwapBytes) {
4406 _mesa_swap4( (GLuint *) dst, n );
4407 }
4408 }
4409 break;
4410 case GL_INT:
4411 {
4412 GLint *dst = (GLint *) dest;
4413 GLuint i;
4414 for (i=0;i<n;i++) {
4415 dst[i] = (GLint) source[i];
4416 }
4417 if (dstPacking->SwapBytes) {
4418 _mesa_swap4( (GLuint *) dst, n );
4419 }
4420 }
4421 break;
4422 case GL_FLOAT:
4423 {
4424 GLfloat *dst = (GLfloat *) dest;
4425 GLuint i;
4426 for (i=0;i<n;i++) {
4427 dst[i] = (GLfloat) source[i];
4428 }
4429 if (dstPacking->SwapBytes) {
4430 _mesa_swap4( (GLuint *) dst, n );
4431 }
4432 }
4433 break;
4434 case GL_HALF_FLOAT_ARB:
4435 {
4436 GLhalfARB *dst = (GLhalfARB *) dest;
4437 GLuint i;
4438 for (i=0;i<n;i++) {
4439 dst[i] = _mesa_float_to_half( (float) source[i] );
4440 }
4441 if (dstPacking->SwapBytes) {
4442 _mesa_swap2( (GLushort *) dst, n );
4443 }
4444 }
4445 break;
4446 case GL_BITMAP:
4447 if (dstPacking->LsbFirst) {
4448 GLubyte *dst = (GLubyte *) dest;
4449 GLint shift = 0;
4450 GLuint i;
4451 for (i = 0; i < n; i++) {
4452 if (shift == 0)
4453 *dst = 0;
4454 *dst |= ((source[i] != 0) << shift);
4455 shift++;
4456 if (shift == 8) {
4457 shift = 0;
4458 dst++;
4459 }
4460 }
4461 }
4462 else {
4463 GLubyte *dst = (GLubyte *) dest;
4464 GLint shift = 7;
4465 GLuint i;
4466 for (i = 0; i < n; i++) {
4467 if (shift == 7)
4468 *dst = 0;
4469 *dst |= ((source[i] != 0) << shift);
4470 shift--;
4471 if (shift < 0) {
4472 shift = 7;
4473 dst++;
4474 }
4475 }
4476 }
4477 break;
4478 default:
4479 _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
4480 }
4481 }
4482
4483 #define DEPTH_VALUES(GLTYPE, GLTYPE2FLOAT) \
4484 do { \
4485 GLuint i; \
4486 const GLTYPE *src = (const GLTYPE *)source; \
4487 for (i = 0; i < n; i++) { \
4488 GLTYPE value = src[i]; \
4489 if (srcPacking->SwapBytes) { \
4490 if (sizeof(GLTYPE) == 2) { \
4491 SWAP2BYTE(value); \
4492 } else if (sizeof(GLTYPE) == 4) { \
4493 SWAP4BYTE(value); \
4494 } \
4495 } \
4496 depthValues[i] = GLTYPE2FLOAT(value); \
4497 } \
4498 } while (0)
4499
4500
4501 /**
4502 * Unpack a row of depth/z values from memory, returning GLushort, GLuint
4503 * or GLfloat values.
4504 * The glPixelTransfer (scale/bias) params will be applied.
4505 *
4506 * \param dstType one of GL_UNSIGNED_SHORT, GL_UNSIGNED_INT, GL_FLOAT
4507 * \param depthMax max value for returned GLushort or GLuint values
4508 * (ignored for GLfloat).
4509 */
4510 void
4511 _mesa_unpack_depth_span( const GLcontext *ctx, GLuint n,
4512 GLenum dstType, GLvoid *dest, GLuint depthMax,
4513 GLenum srcType, const GLvoid *source,
4514 const struct gl_pixelstore_attrib *srcPacking )
4515 {
4516 GLfloat depthTemp[MAX_WIDTH], *depthValues;
4517 GLboolean needClamp = GL_FALSE;
4518
4519 /* Look for special cases first.
4520 * Not only are these faster, they're less prone to numeric conversion
4521 * problems. Otherwise, converting from an int type to a float then
4522 * back to an int type can introduce errors that will show up as
4523 * artifacts in things like depth peeling which uses glCopyTexImage.
4524 */
4525 if (ctx->Pixel.DepthScale == 1.0 && ctx->Pixel.DepthBias == 0.0) {
4526 if (srcType == GL_UNSIGNED_INT && dstType == GL_UNSIGNED_SHORT) {
4527 const GLuint *src = (const GLuint *) source;
4528 GLushort *dst = (GLushort *) dest;
4529 GLuint i;
4530 for (i = 0; i < n; i++) {
4531 dst[i] = src[i] >> 16;
4532 }
4533 return;
4534 }
4535 if (srcType == GL_UNSIGNED_SHORT
4536 && dstType == GL_UNSIGNED_INT
4537 && depthMax == 0xffffffff) {
4538 const GLushort *src = (const GLushort *) source;
4539 GLuint *dst = (GLuint *) dest;
4540 GLuint i;
4541 for (i = 0; i < n; i++) {
4542 dst[i] = src[i] | (src[i] << 16);
4543 }
4544 return;
4545 }
4546 /* XXX may want to add additional cases here someday */
4547 }
4548
4549 /* general case path follows */
4550
4551 if (dstType == GL_FLOAT) {
4552 depthValues = (GLfloat *) dest;
4553 }
4554 else {
4555 depthValues = depthTemp;
4556 }
4557
4558 /* Convert incoming values to GLfloat. Some conversions will require
4559 * clamping, below.
4560 */
4561 switch (srcType) {
4562 case GL_BYTE:
4563 DEPTH_VALUES(GLbyte, BYTE_TO_FLOAT);
4564 needClamp = GL_TRUE;
4565 break;
4566 case GL_UNSIGNED_BYTE:
4567 DEPTH_VALUES(GLubyte, UBYTE_TO_FLOAT);
4568 break;
4569 case GL_SHORT:
4570 DEPTH_VALUES(GLshort, SHORT_TO_FLOAT);
4571 needClamp = GL_TRUE;
4572 break;
4573 case GL_UNSIGNED_SHORT:
4574 DEPTH_VALUES(GLushort, USHORT_TO_FLOAT);
4575 break;
4576 case GL_INT:
4577 DEPTH_VALUES(GLint, INT_TO_FLOAT);
4578 needClamp = GL_TRUE;
4579 break;
4580 case GL_UNSIGNED_INT:
4581 DEPTH_VALUES(GLuint, UINT_TO_FLOAT);
4582 break;
4583 case GL_UNSIGNED_INT_24_8_EXT: /* GL_EXT_packed_depth_stencil */
4584 if (dstType == GL_UNSIGNED_INT_24_8_EXT &&
4585 depthMax == 0xffffff &&
4586 ctx->Pixel.DepthScale == 1.0 &&
4587 ctx->Pixel.DepthBias == 0.0) {
4588 const GLuint *src = (const GLuint *) source;
4589 GLuint *zValues = (GLuint *) dest;
4590 GLuint i;
4591 for (i = 0; i < n; i++) {
4592 GLuint value = src[i];
4593 if (srcPacking->SwapBytes) {
4594 SWAP4BYTE(value);
4595 }
4596 zValues[i] = value & 0xffffff00;
4597 }
4598 return;
4599 }
4600 else {
4601 const GLuint *src = (const GLuint *) source;
4602 const GLfloat scale = 1.0f / 0xffffff;
4603 GLuint i;
4604 for (i = 0; i < n; i++) {
4605 GLuint value = src[i];
4606 if (srcPacking->SwapBytes) {
4607 SWAP4BYTE(value);
4608 }
4609 depthValues[i] = (value >> 8) * scale;
4610 }
4611 }
4612 break;
4613 case GL_FLOAT:
4614 DEPTH_VALUES(GLfloat, 1*);
4615 needClamp = GL_TRUE;
4616 break;
4617 case GL_HALF_FLOAT_ARB:
4618 {
4619 GLuint i;
4620 const GLhalfARB *src = (const GLhalfARB *) source;
4621 for (i = 0; i < n; i++) {
4622 GLhalfARB value = src[i];
4623 if (srcPacking->SwapBytes) {
4624 SWAP2BYTE(value);
4625 }
4626 depthValues[i] = _mesa_half_to_float(value);
4627 }
4628 needClamp = GL_TRUE;
4629 }
4630 break;
4631 default:
4632 _mesa_problem(NULL, "bad type in _mesa_unpack_depth_span()");
4633 return;
4634 }
4635
4636 /* apply depth scale and bias */
4637 {
4638 const GLfloat scale = ctx->Pixel.DepthScale;
4639 const GLfloat bias = ctx->Pixel.DepthBias;
4640 if (scale != 1.0 || bias != 0.0) {
4641 GLuint i;
4642 for (i = 0; i < n; i++) {
4643 depthValues[i] = depthValues[i] * scale + bias;
4644 }
4645 needClamp = GL_TRUE;
4646 }
4647 }
4648
4649 /* clamp to [0, 1] */
4650 if (needClamp) {
4651 GLuint i;
4652 for (i = 0; i < n; i++) {
4653 depthValues[i] = (GLfloat)CLAMP(depthValues[i], 0.0, 1.0);
4654 }
4655 }
4656
4657 /*
4658 * Convert values to dstType
4659 */
4660 if (dstType == GL_UNSIGNED_INT) {
4661 GLuint *zValues = (GLuint *) dest;
4662 GLuint i;
4663 if (depthMax <= 0xffffff) {
4664 /* no overflow worries */
4665 for (i = 0; i < n; i++) {
4666 zValues[i] = (GLuint) (depthValues[i] * (GLfloat) depthMax);
4667 }
4668 }
4669 else {
4670 /* need to use double precision to prevent overflow problems */
4671 for (i = 0; i < n; i++) {
4672 GLdouble z = depthValues[i] * (GLfloat) depthMax;
4673 if (z >= (GLdouble) 0xffffffff)
4674 zValues[i] = 0xffffffff;
4675 else
4676 zValues[i] = (GLuint) z;
4677 }
4678 }
4679 }
4680 else if (dstType == GL_UNSIGNED_SHORT) {
4681 GLushort *zValues = (GLushort *) dest;
4682 GLuint i;
4683 ASSERT(depthMax <= 0xffff);
4684 for (i = 0; i < n; i++) {
4685 zValues[i] = (GLushort) (depthValues[i] * (GLfloat) depthMax);
4686 }
4687 }
4688 else {
4689 ASSERT(dstType == GL_FLOAT);
4690 /*ASSERT(depthMax == 1.0F);*/
4691 }
4692 }
4693
4694
4695 /*
4696 * Pack an array of depth values. The values are floats in [0,1].
4697 */
4698 void
4699 _mesa_pack_depth_span( const GLcontext *ctx, GLuint n, GLvoid *dest,
4700 GLenum dstType, const GLfloat *depthSpan,
4701 const struct gl_pixelstore_attrib *dstPacking )
4702 {
4703 GLfloat depthCopy[MAX_WIDTH];
4704
4705 ASSERT(n <= MAX_WIDTH);
4706
4707 if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
4708 _mesa_memcpy(depthCopy, depthSpan, n * sizeof(GLfloat));
4709 _mesa_scale_and_bias_depth(ctx, n, depthCopy);
4710 depthSpan = depthCopy;
4711 }
4712
4713 switch (dstType) {
4714 case GL_UNSIGNED_BYTE:
4715 {
4716 GLubyte *dst = (GLubyte *) dest;
4717 GLuint i;
4718 for (i = 0; i < n; i++) {
4719 dst[i] = FLOAT_TO_UBYTE( depthSpan[i] );
4720 }
4721 }
4722 break;
4723 case GL_BYTE:
4724 {
4725 GLbyte *dst = (GLbyte *) dest;
4726 GLuint i;
4727 for (i = 0; i < n; i++) {
4728 dst[i] = FLOAT_TO_BYTE( depthSpan[i] );
4729 }
4730 }
4731 break;
4732 case GL_UNSIGNED_SHORT:
4733 {
4734 GLushort *dst = (GLushort *) dest;
4735 GLuint i;
4736 for (i = 0; i < n; i++) {
4737 CLAMPED_FLOAT_TO_USHORT(dst[i], depthSpan[i]);
4738 }
4739 if (dstPacking->SwapBytes) {
4740 _mesa_swap2( (GLushort *) dst, n );
4741 }
4742 }
4743 break;
4744 case GL_SHORT:
4745 {
4746 GLshort *dst = (GLshort *) dest;
4747 GLuint i;
4748 for (i = 0; i < n; i++) {
4749 dst[i] = FLOAT_TO_SHORT( depthSpan[i] );
4750 }
4751 if (dstPacking->SwapBytes) {
4752 _mesa_swap2( (GLushort *) dst, n );
4753 }
4754 }
4755 break;
4756 case GL_UNSIGNED_INT:
4757 {
4758 GLuint *dst = (GLuint *) dest;
4759 GLuint i;
4760 for (i = 0; i < n; i++) {
4761 dst[i] = FLOAT_TO_UINT( depthSpan[i] );
4762 }
4763 if (dstPacking->SwapBytes) {
4764 _mesa_swap4( (GLuint *) dst, n );
4765 }
4766 }
4767 break;
4768 case GL_INT:
4769 {
4770 GLint *dst = (GLint *) dest;
4771 GLuint i;
4772 for (i = 0; i < n; i++) {
4773 dst[i] = FLOAT_TO_INT( depthSpan[i] );
4774 }
4775 if (dstPacking->SwapBytes) {
4776 _mesa_swap4( (GLuint *) dst, n );
4777 }
4778 }
4779 break;
4780 case GL_FLOAT:
4781 {
4782 GLfloat *dst = (GLfloat *) dest;
4783 GLuint i;
4784 for (i = 0; i < n; i++) {
4785 dst[i] = depthSpan[i];
4786 }
4787 if (dstPacking->SwapBytes) {
4788 _mesa_swap4( (GLuint *) dst, n );
4789 }
4790 }
4791 break;
4792 case GL_HALF_FLOAT_ARB:
4793 {
4794 GLhalfARB *dst = (GLhalfARB *) dest;
4795 GLuint i;
4796 for (i = 0; i < n; i++) {
4797 dst[i] = _mesa_float_to_half(depthSpan[i]);
4798 }
4799 if (dstPacking->SwapBytes) {
4800 _mesa_swap2( (GLushort *) dst, n );
4801 }
4802 }
4803 break;
4804 default:
4805 _mesa_problem(ctx, "bad type in _mesa_pack_depth_span");
4806 }
4807 }
4808
4809
4810
4811 /**
4812 * Pack depth and stencil values as GL_DEPTH_STENCIL/GL_UNSIGNED_INT_24_8.
4813 */
4814 void
4815 _mesa_pack_depth_stencil_span(const GLcontext *ctx, GLuint n, GLuint *dest,
4816 const GLfloat *depthVals,
4817 const GLstencil *stencilVals,
4818 const struct gl_pixelstore_attrib *dstPacking)
4819 {
4820 GLfloat depthCopy[MAX_WIDTH];
4821 GLstencil stencilCopy[MAX_WIDTH];
4822 GLuint i;
4823
4824 ASSERT(n <= MAX_WIDTH);
4825
4826 if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
4827 _mesa_memcpy(depthCopy, depthVals, n * sizeof(GLfloat));
4828 _mesa_scale_and_bias_depth(ctx, n, depthCopy);
4829 depthVals = depthCopy;
4830 }
4831
4832 if (ctx->Pixel.IndexShift ||
4833 ctx->Pixel.IndexOffset ||
4834 ctx->Pixel.MapStencilFlag) {
4835 _mesa_memcpy(stencilCopy, stencilVals, n * sizeof(GLstencil));
4836 _mesa_apply_stencil_transfer_ops(ctx, n, stencilCopy);
4837 stencilVals = stencilCopy;
4838 }
4839
4840 for (i = 0; i < n; i++) {
4841 GLuint z = (GLuint) (depthVals[i] * 0xffffff);
4842 dest[i] = (z << 8) | (stencilVals[i] & 0xff);
4843 }
4844
4845 if (dstPacking->SwapBytes) {
4846 _mesa_swap4(dest, n);
4847 }
4848 }
4849
4850
4851
4852
4853 /**
4854 * Unpack image data. Apply byte swapping, byte flipping (bitmap).
4855 * Return all image data in a contiguous block. This is used when we
4856 * compile glDrawPixels, glTexImage, etc into a display list. We
4857 * need a copy of the data in a standard format.
4858 */
4859 void *
4860 _mesa_unpack_image( GLuint dimensions,
4861 GLsizei width, GLsizei height, GLsizei depth,
4862 GLenum format, GLenum type, const GLvoid *pixels,
4863 const struct gl_pixelstore_attrib *unpack )
4864 {
4865 GLint bytesPerRow, compsPerRow;
4866 GLboolean flipBytes, swap2, swap4;
4867
4868 if (!pixels)
4869 return NULL; /* not necessarily an error */
4870
4871 if (width <= 0 || height <= 0 || depth <= 0)
4872 return NULL; /* generate error later */
4873
4874 if (type == GL_BITMAP) {
4875 bytesPerRow = (width + 7) >> 3;
4876 flipBytes = unpack->LsbFirst;
4877 swap2 = swap4 = GL_FALSE;
4878 compsPerRow = 0;
4879 }
4880 else {
4881 const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
4882 GLint components = _mesa_components_in_format(format);
4883 GLint bytesPerComp;
4884
4885 if (_mesa_type_is_packed(type))
4886 components = 1;
4887
4888 if (bytesPerPixel <= 0 || components <= 0)
4889 return NULL; /* bad format or type. generate error later */
4890 bytesPerRow = bytesPerPixel * width;
4891 bytesPerComp = bytesPerPixel / components;
4892 flipBytes = GL_FALSE;
4893 swap2 = (bytesPerComp == 2) && unpack->SwapBytes;
4894 swap4 = (bytesPerComp == 4) && unpack->SwapBytes;
4895 compsPerRow = components * width;
4896 assert(compsPerRow >= width);
4897 }
4898
4899 {
4900 GLubyte *destBuffer
4901 = (GLubyte *) _mesa_malloc(bytesPerRow * height * depth);
4902 GLubyte *dst;
4903 GLint img, row;
4904 if (!destBuffer)
4905 return NULL; /* generate GL_OUT_OF_MEMORY later */
4906
4907 dst = destBuffer;
4908 for (img = 0; img < depth; img++) {
4909 for (row = 0; row < height; row++) {
4910 const GLvoid *src = _mesa_image_address(dimensions, unpack, pixels,
4911 width, height, format, type, img, row, 0);
4912
4913 if ((type == GL_BITMAP) && (unpack->SkipPixels & 0x7)) {
4914 GLint i;
4915 flipBytes = GL_FALSE;
4916 if (unpack->LsbFirst) {
4917 GLubyte srcMask = 1 << (unpack->SkipPixels & 0x7);
4918 GLubyte dstMask = 128;
4919 const GLubyte *s = src;
4920 GLubyte *d = dst;
4921 *d = 0;
4922 for (i = 0; i < width; i++) {
4923 if (*s & srcMask) {
4924 *d |= dstMask;
4925 }
4926 if (srcMask == 128) {
4927 srcMask = 1;
4928 s++;
4929 }
4930 else {
4931 srcMask = srcMask << 1;
4932 }
4933 if (dstMask == 1) {
4934 dstMask = 128;
4935 d++;
4936 *d = 0;
4937 }
4938 else {
4939 dstMask = dstMask >> 1;
4940 }
4941 }
4942 }
4943 else {
4944 GLubyte srcMask = 128 >> (unpack->SkipPixels & 0x7);
4945 GLubyte dstMask = 128;
4946 const GLubyte *s = src;
4947 GLubyte *d = dst;
4948 *d = 0;
4949 for (i = 0; i < width; i++) {
4950 if (*s & srcMask) {
4951 *d |= dstMask;
4952 }
4953 if (srcMask == 1) {
4954 srcMask = 128;
4955 s++;
4956 }
4957 else {
4958 srcMask = srcMask >> 1;
4959 }
4960 if (dstMask == 1) {
4961 dstMask = 128;
4962 d++;
4963 *d = 0;
4964 }
4965 else {
4966 dstMask = dstMask >> 1;
4967 }
4968 }
4969 }
4970 }
4971 else {
4972 _mesa_memcpy(dst, src, bytesPerRow);
4973 }
4974
4975 /* byte flipping/swapping */
4976 if (flipBytes) {
4977 flip_bytes((GLubyte *) dst, bytesPerRow);
4978 }
4979 else if (swap2) {
4980 _mesa_swap2((GLushort*) dst, compsPerRow);
4981 }
4982 else if (swap4) {
4983 _mesa_swap4((GLuint*) dst, compsPerRow);
4984 }
4985 dst += bytesPerRow;
4986 }
4987 }
4988 return destBuffer;
4989 }
4990 }
4991
4992 #endif /* _HAVE_FULL_GL */
4993
4994
4995
4996 /**
4997 * Convert an array of RGBA colors from one datatype to another.
4998 * NOTE: src may equal dst. In that case, we use a temporary buffer.
4999 */
5000 void
5001 _mesa_convert_colors(GLenum srcType, const GLvoid *src,
5002 GLenum dstType, GLvoid *dst,
5003 GLuint count, const GLubyte mask[])
5004 {
5005 GLuint tempBuffer[MAX_WIDTH][4];
5006 const GLboolean useTemp = (src == dst);
5007
5008 ASSERT(srcType != dstType);
5009
5010 switch (srcType) {
5011 case GL_UNSIGNED_BYTE:
5012 if (dstType == GL_UNSIGNED_SHORT) {
5013 const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src;
5014 GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst);
5015 GLuint i;
5016 for (i = 0; i < count; i++) {
5017 if (!mask || mask[i]) {
5018 dst2[i][RCOMP] = UBYTE_TO_USHORT(src1[i][RCOMP]);
5019 dst2[i][GCOMP] = UBYTE_TO_USHORT(src1[i][GCOMP]);
5020 dst2[i][BCOMP] = UBYTE_TO_USHORT(src1[i][BCOMP]);
5021 dst2[i][ACOMP] = UBYTE_TO_USHORT(src1[i][ACOMP]);
5022 }
5023 }
5024 if (useTemp)
5025 _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort));
5026 }
5027 else {
5028 const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src;
5029 GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst);
5030 GLuint i;
5031 ASSERT(dstType == GL_FLOAT);
5032 for (i = 0; i < count; i++) {
5033 if (!mask || mask[i]) {
5034 dst4[i][RCOMP] = UBYTE_TO_FLOAT(src1[i][RCOMP]);
5035 dst4[i][GCOMP] = UBYTE_TO_FLOAT(src1[i][GCOMP]);
5036 dst4[i][BCOMP] = UBYTE_TO_FLOAT(src1[i][BCOMP]);
5037 dst4[i][ACOMP] = UBYTE_TO_FLOAT(src1[i][ACOMP]);
5038 }
5039 }
5040 if (useTemp)
5041 _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat));
5042 }
5043 break;
5044 case GL_UNSIGNED_SHORT:
5045 if (dstType == GL_UNSIGNED_BYTE) {
5046 const GLushort (*src2)[4] = (const GLushort (*)[4]) src;
5047 GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst);
5048 GLuint i;
5049 for (i = 0; i < count; i++) {
5050 if (!mask || mask[i]) {
5051 dst1[i][RCOMP] = USHORT_TO_UBYTE(src2[i][RCOMP]);
5052 dst1[i][GCOMP] = USHORT_TO_UBYTE(src2[i][GCOMP]);
5053 dst1[i][BCOMP] = USHORT_TO_UBYTE(src2[i][BCOMP]);
5054 dst1[i][ACOMP] = USHORT_TO_UBYTE(src2[i][ACOMP]);
5055 }
5056 }
5057 if (useTemp)
5058 _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte));
5059 }
5060 else {
5061 const GLushort (*src2)[4] = (const GLushort (*)[4]) src;
5062 GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst);
5063 GLuint i;
5064 ASSERT(dstType == GL_FLOAT);
5065 for (i = 0; i < count; i++) {
5066 if (!mask || mask[i]) {
5067 dst4[i][RCOMP] = USHORT_TO_FLOAT(src2[i][RCOMP]);
5068 dst4[i][GCOMP] = USHORT_TO_FLOAT(src2[i][GCOMP]);
5069 dst4[i][BCOMP] = USHORT_TO_FLOAT(src2[i][BCOMP]);
5070 dst4[i][ACOMP] = USHORT_TO_FLOAT(src2[i][ACOMP]);
5071 }
5072 }
5073 if (useTemp)
5074 _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat));
5075 }
5076 break;
5077 case GL_FLOAT:
5078 if (dstType == GL_UNSIGNED_BYTE) {
5079 const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src;
5080 GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst);
5081 GLuint i;
5082 for (i = 0; i < count; i++) {
5083 if (!mask || mask[i]) {
5084 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][RCOMP], src4[i][RCOMP]);
5085 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][GCOMP], src4[i][GCOMP]);
5086 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][BCOMP], src4[i][BCOMP]);
5087 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][ACOMP], src4[i][ACOMP]);
5088 }
5089 }
5090 if (useTemp)
5091 _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte));
5092 }
5093 else {
5094 const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src;
5095 GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst);
5096 GLuint i;
5097 ASSERT(dstType == GL_UNSIGNED_SHORT);
5098 for (i = 0; i < count; i++) {
5099 if (!mask || mask[i]) {
5100 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][RCOMP], src4[i][RCOMP]);
5101 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][GCOMP], src4[i][GCOMP]);
5102 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][BCOMP], src4[i][BCOMP]);
5103 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][ACOMP], src4[i][ACOMP]);
5104 }
5105 }
5106 if (useTemp)
5107 _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort));
5108 }
5109 break;
5110 default:
5111 _mesa_problem(NULL, "Invalid datatype in _mesa_convert_colors");
5112 }
5113 }
5114
5115
5116
5117
5118 /**
5119 * Perform basic clipping for glDrawPixels. The image's position and size
5120 * and the unpack SkipPixels and SkipRows are adjusted so that the image
5121 * region is entirely within the window and scissor bounds.
5122 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
5123 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
5124 * we'll actually write. Beforehand, *destY-1 is the first drawing row.
5125 *
5126 * \return GL_TRUE if image is ready for drawing or
5127 * GL_FALSE if image was completely clipped away (draw nothing)
5128 */
5129 GLboolean
5130 _mesa_clip_drawpixels(const GLcontext *ctx,
5131 GLint *destX, GLint *destY,
5132 GLsizei *width, GLsizei *height,
5133 struct gl_pixelstore_attrib *unpack)
5134 {
5135 const GLframebuffer *buffer = ctx->DrawBuffer;
5136
5137 if (unpack->RowLength == 0) {
5138 unpack->RowLength = *width;
5139 }
5140
5141 ASSERT(ctx->Pixel.ZoomX == 1.0F);
5142 ASSERT(ctx->Pixel.ZoomY == 1.0F || ctx->Pixel.ZoomY == -1.0F);
5143
5144 /* left clipping */
5145 if (*destX < buffer->_Xmin) {
5146 unpack->SkipPixels += (buffer->_Xmin - *destX);
5147 *width -= (buffer->_Xmin - *destX);
5148 *destX = buffer->_Xmin;
5149 }
5150 /* right clipping */
5151 if (*destX + *width > buffer->_Xmax)
5152 *width -= (*destX + *width - buffer->_Xmax);
5153
5154 if (*width <= 0)
5155 return GL_FALSE;
5156
5157 if (ctx->Pixel.ZoomY == 1.0F) {
5158 /* bottom clipping */
5159 if (*destY < buffer->_Ymin) {
5160 unpack->SkipRows += (buffer->_Ymin - *destY);
5161 *height -= (buffer->_Ymin - *destY);
5162 *destY = buffer->_Ymin;
5163 }
5164 /* top clipping */
5165 if (*destY + *height > buffer->_Ymax)
5166 *height -= (*destY + *height - buffer->_Ymax);
5167 }
5168 else { /* upside down */
5169 /* top clipping */
5170 if (*destY > buffer->_Ymax) {
5171 unpack->SkipRows += (*destY - buffer->_Ymax);
5172 *height -= (*destY - buffer->_Ymax);
5173 *destY = buffer->_Ymax;
5174 }
5175 /* bottom clipping */
5176 if (*destY - *height < buffer->_Ymin)
5177 *height -= (buffer->_Ymin - (*destY - *height));
5178 /* adjust destY so it's the first row to write to */
5179 (*destY)--;
5180 }
5181
5182 if (*height <= 0)
5183 return GL_TRUE;
5184
5185 return GL_TRUE;
5186 }
5187
5188
5189 /**
5190 * Perform clipping for glReadPixels. The image's window position
5191 * and size, and the pack skipPixels, skipRows and rowLength are adjusted
5192 * so that the image region is entirely within the window bounds.
5193 * Note: this is different from _mesa_clip_drawpixels() in that the
5194 * scissor box is ignored, and we use the bounds of the current readbuffer
5195 * surface.
5196 *
5197 * \return GL_TRUE if image is ready for drawing or
5198 * GL_FALSE if image was completely clipped away (draw nothing)
5199 */
5200 GLboolean
5201 _mesa_clip_readpixels(const GLcontext *ctx,
5202 GLint *srcX, GLint *srcY,
5203 GLsizei *width, GLsizei *height,
5204 struct gl_pixelstore_attrib *pack)
5205 {
5206 const GLframebuffer *buffer = ctx->ReadBuffer;
5207
5208 if (pack->RowLength == 0) {
5209 pack->RowLength = *width;
5210 }
5211
5212 /* left clipping */
5213 if (*srcX < 0) {
5214 pack->SkipPixels += (0 - *srcX);
5215 *width -= (0 - *srcX);
5216 *srcX = 0;
5217 }
5218 /* right clipping */
5219 if (*srcX + *width > (GLsizei) buffer->Width)
5220 *width -= (*srcX + *width - buffer->Width);
5221
5222 if (*width <= 0)
5223 return GL_FALSE;
5224
5225 /* bottom clipping */
5226 if (*srcY < 0) {
5227 pack->SkipRows += (0 - *srcY);
5228 *height -= (0 - *srcY);
5229 *srcY = 0;
5230 }
5231 /* top clipping */
5232 if (*srcY + *height > (GLsizei) buffer->Height)
5233 *height -= (*srcY + *height - buffer->Height);
5234
5235 if (*height <= 0)
5236 return GL_TRUE;
5237
5238 return GL_TRUE;
5239 }
5240
5241
5242 /**
5243 * Do clipping for a glCopyTexSubImage call.
5244 * The framebuffer source region might extend outside the framebuffer
5245 * bounds. Clip the source region against the framebuffer bounds and
5246 * adjust the texture/dest position and size accordingly.
5247 *
5248 * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise.
5249 */
5250 GLboolean
5251 _mesa_clip_copytexsubimage(const GLcontext *ctx,
5252 GLint *destX, GLint *destY,
5253 GLint *srcX, GLint *srcY,
5254 GLsizei *width, GLsizei *height)
5255 {
5256 const struct gl_framebuffer *fb = ctx->ReadBuffer;
5257 const GLint srcX0 = *srcX, srcY0 = *srcY;
5258
5259 if (_mesa_clip_to_region(0, 0, fb->Width, fb->Height,
5260 srcX, srcY, width, height)) {
5261 *destX = *destX + *srcX - srcX0;
5262 *destY = *destY + *srcY - srcY0;
5263
5264 return GL_TRUE;
5265 }
5266 else {
5267 return GL_FALSE;
5268 }
5269 }
5270
5271
5272
5273 /**
5274 * Clip the rectangle defined by (x, y, width, height) against the bounds
5275 * specified by [xmin, xmax) and [ymin, ymax).
5276 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
5277 */
5278 GLboolean
5279 _mesa_clip_to_region(GLint xmin, GLint ymin,
5280 GLint xmax, GLint ymax,
5281 GLint *x, GLint *y,
5282 GLsizei *width, GLsizei *height )
5283 {
5284 /* left clipping */
5285 if (*x < xmin) {
5286 *width -= (xmin - *x);
5287 *x = xmin;
5288 }
5289
5290 /* right clipping */
5291 if (*x + *width > xmax)
5292 *width -= (*x + *width - xmax);
5293
5294 if (*width <= 0)
5295 return GL_FALSE;
5296
5297 /* bottom (or top) clipping */
5298 if (*y < ymin) {
5299 *height -= (ymin - *y);
5300 *y = ymin;
5301 }
5302
5303 /* top (or bottom) clipping */
5304 if (*y + *height > ymax)
5305 *height -= (*y + *height - ymax);
5306
5307 if (*height <= 0)
5308 return GL_FALSE;
5309
5310 return GL_TRUE;
5311 }