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