mesa: Remove _mesa_pack_rgba_span_float and tmp_pack.h
[mesa.git] / src / mesa / main / pack.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009-2010 VMware, Inc. 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 * THEA AUTHORS 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 pack.c
28 * Image and pixel span packing and unpacking.
29 */
30
31
32 /*
33 * XXX: MSVC takes forever to compile this module for x86_64 unless we disable
34 * this global optimization.
35 *
36 * See also:
37 * - http://msdn.microsoft.com/en-us/library/1yk3ydd7.aspx
38 * - http://msdn.microsoft.com/en-us/library/chh3fb0k.aspx
39 */
40 #if defined(_MSC_VER) && defined(_M_X64)
41 # pragma optimize( "g", off )
42 #endif
43
44
45 #include "glheader.h"
46 #include "colormac.h"
47 #include "enums.h"
48 #include "image.h"
49 #include "imports.h"
50 #include "macros.h"
51 #include "mtypes.h"
52 #include "pack.h"
53 #include "pixeltransfer.h"
54 #include "imports.h"
55 #include "glformats.h"
56 #include "format_utils.h"
57 #include "format_pack.h"
58
59
60 /**
61 * Flip the 8 bits in each byte of the given array.
62 *
63 * \param p array.
64 * \param n number of bytes.
65 *
66 * \todo try this trick to flip bytes someday:
67 * \code
68 * v = ((v & 0x55555555) << 1) | ((v >> 1) & 0x55555555);
69 * v = ((v & 0x33333333) << 2) | ((v >> 2) & 0x33333333);
70 * v = ((v & 0x0f0f0f0f) << 4) | ((v >> 4) & 0x0f0f0f0f);
71 * \endcode
72 */
73 static void
74 flip_bytes( GLubyte *p, GLuint n )
75 {
76 GLuint i, a, b;
77 for (i = 0; i < n; i++) {
78 b = (GLuint) p[i]; /* words are often faster than bytes */
79 a = ((b & 0x01) << 7) |
80 ((b & 0x02) << 5) |
81 ((b & 0x04) << 3) |
82 ((b & 0x08) << 1) |
83 ((b & 0x10) >> 1) |
84 ((b & 0x20) >> 3) |
85 ((b & 0x40) >> 5) |
86 ((b & 0x80) >> 7);
87 p[i] = (GLubyte) a;
88 }
89 }
90
91
92
93 /*
94 * Unpack a 32x32 pixel polygon stipple from user memory using the
95 * current pixel unpack settings.
96 */
97 void
98 _mesa_unpack_polygon_stipple( const GLubyte *pattern, GLuint dest[32],
99 const struct gl_pixelstore_attrib *unpacking )
100 {
101 GLubyte *ptrn = (GLubyte *) _mesa_unpack_image(2, 32, 32, 1, GL_COLOR_INDEX,
102 GL_BITMAP, pattern, unpacking);
103 if (ptrn) {
104 /* Convert pattern from GLubytes to GLuints and handle big/little
105 * endian differences
106 */
107 GLubyte *p = ptrn;
108 GLint i;
109 for (i = 0; i < 32; i++) {
110 dest[i] = (p[0] << 24)
111 | (p[1] << 16)
112 | (p[2] << 8)
113 | (p[3] );
114 p += 4;
115 }
116 free(ptrn);
117 }
118 }
119
120
121 /*
122 * Pack polygon stipple into user memory given current pixel packing
123 * settings.
124 */
125 void
126 _mesa_pack_polygon_stipple( const GLuint pattern[32], GLubyte *dest,
127 const struct gl_pixelstore_attrib *packing )
128 {
129 /* Convert pattern from GLuints to GLubytes to handle big/little
130 * endian differences.
131 */
132 GLubyte ptrn[32*4];
133 GLint i;
134 for (i = 0; i < 32; i++) {
135 ptrn[i * 4 + 0] = (GLubyte) ((pattern[i] >> 24) & 0xff);
136 ptrn[i * 4 + 1] = (GLubyte) ((pattern[i] >> 16) & 0xff);
137 ptrn[i * 4 + 2] = (GLubyte) ((pattern[i] >> 8 ) & 0xff);
138 ptrn[i * 4 + 3] = (GLubyte) ((pattern[i] ) & 0xff);
139 }
140
141 _mesa_pack_bitmap(32, 32, ptrn, dest, packing);
142 }
143
144
145 /*
146 * Pack bitmap data.
147 */
148 void
149 _mesa_pack_bitmap( GLint width, GLint height, const GLubyte *source,
150 GLubyte *dest, const struct gl_pixelstore_attrib *packing )
151 {
152 GLint row, width_in_bytes;
153 const GLubyte *src;
154
155 if (!source)
156 return;
157
158 width_in_bytes = CEILING( width, 8 );
159 src = source;
160 for (row = 0; row < height; row++) {
161 GLubyte *dst = (GLubyte *) _mesa_image_address2d(packing, dest,
162 width, height, GL_COLOR_INDEX, GL_BITMAP, row, 0);
163 if (!dst)
164 return;
165
166 if ((packing->SkipPixels & 7) == 0) {
167 memcpy( dst, src, width_in_bytes );
168 if (packing->LsbFirst) {
169 flip_bytes( dst, width_in_bytes );
170 }
171 }
172 else {
173 /* handling SkipPixels is a bit tricky (no pun intended!) */
174 GLint i;
175 if (packing->LsbFirst) {
176 GLubyte srcMask = 128;
177 GLubyte dstMask = 1 << (packing->SkipPixels & 0x7);
178 const GLubyte *s = src;
179 GLubyte *d = dst;
180 *d = 0;
181 for (i = 0; i < width; i++) {
182 if (*s & srcMask) {
183 *d |= dstMask;
184 }
185 if (srcMask == 1) {
186 srcMask = 128;
187 s++;
188 }
189 else {
190 srcMask = srcMask >> 1;
191 }
192 if (dstMask == 128) {
193 dstMask = 1;
194 d++;
195 *d = 0;
196 }
197 else {
198 dstMask = dstMask << 1;
199 }
200 }
201 }
202 else {
203 GLubyte srcMask = 128;
204 GLubyte dstMask = 128 >> (packing->SkipPixels & 0x7);
205 const GLubyte *s = src;
206 GLubyte *d = dst;
207 *d = 0;
208 for (i = 0; i < width; i++) {
209 if (*s & srcMask) {
210 *d |= dstMask;
211 }
212 if (srcMask == 1) {
213 srcMask = 128;
214 s++;
215 }
216 else {
217 srcMask = srcMask >> 1;
218 }
219 if (dstMask == 1) {
220 dstMask = 128;
221 d++;
222 *d = 0;
223 }
224 else {
225 dstMask = dstMask >> 1;
226 }
227 }
228 }
229 }
230 src += width_in_bytes;
231 }
232 }
233
234
235 #define SWAP2BYTE(VALUE) \
236 { \
237 GLubyte *bytes = (GLubyte *) &(VALUE); \
238 GLubyte tmp = bytes[0]; \
239 bytes[0] = bytes[1]; \
240 bytes[1] = tmp; \
241 }
242
243 #define SWAP4BYTE(VALUE) \
244 { \
245 GLubyte *bytes = (GLubyte *) &(VALUE); \
246 GLubyte tmp = bytes[0]; \
247 bytes[0] = bytes[3]; \
248 bytes[3] = tmp; \
249 tmp = bytes[1]; \
250 bytes[1] = bytes[2]; \
251 bytes[2] = tmp; \
252 }
253
254
255 static void
256 extract_uint_indexes(GLuint n, GLuint indexes[],
257 GLenum srcFormat, GLenum srcType, const GLvoid *src,
258 const struct gl_pixelstore_attrib *unpack )
259 {
260 ASSERT(srcFormat == GL_COLOR_INDEX || srcFormat == GL_STENCIL_INDEX);
261
262 ASSERT(srcType == GL_BITMAP ||
263 srcType == GL_UNSIGNED_BYTE ||
264 srcType == GL_BYTE ||
265 srcType == GL_UNSIGNED_SHORT ||
266 srcType == GL_SHORT ||
267 srcType == GL_UNSIGNED_INT ||
268 srcType == GL_INT ||
269 srcType == GL_UNSIGNED_INT_24_8_EXT ||
270 srcType == GL_HALF_FLOAT_ARB ||
271 srcType == GL_FLOAT ||
272 srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
273
274 switch (srcType) {
275 case GL_BITMAP:
276 {
277 GLubyte *ubsrc = (GLubyte *) src;
278 if (unpack->LsbFirst) {
279 GLubyte mask = 1 << (unpack->SkipPixels & 0x7);
280 GLuint i;
281 for (i = 0; i < n; i++) {
282 indexes[i] = (*ubsrc & mask) ? 1 : 0;
283 if (mask == 128) {
284 mask = 1;
285 ubsrc++;
286 }
287 else {
288 mask = mask << 1;
289 }
290 }
291 }
292 else {
293 GLubyte mask = 128 >> (unpack->SkipPixels & 0x7);
294 GLuint i;
295 for (i = 0; i < n; i++) {
296 indexes[i] = (*ubsrc & mask) ? 1 : 0;
297 if (mask == 1) {
298 mask = 128;
299 ubsrc++;
300 }
301 else {
302 mask = mask >> 1;
303 }
304 }
305 }
306 }
307 break;
308 case GL_UNSIGNED_BYTE:
309 {
310 GLuint i;
311 const GLubyte *s = (const GLubyte *) src;
312 for (i = 0; i < n; i++)
313 indexes[i] = s[i];
314 }
315 break;
316 case GL_BYTE:
317 {
318 GLuint i;
319 const GLbyte *s = (const GLbyte *) src;
320 for (i = 0; i < n; i++)
321 indexes[i] = s[i];
322 }
323 break;
324 case GL_UNSIGNED_SHORT:
325 {
326 GLuint i;
327 const GLushort *s = (const GLushort *) src;
328 if (unpack->SwapBytes) {
329 for (i = 0; i < n; i++) {
330 GLushort value = s[i];
331 SWAP2BYTE(value);
332 indexes[i] = value;
333 }
334 }
335 else {
336 for (i = 0; i < n; i++)
337 indexes[i] = s[i];
338 }
339 }
340 break;
341 case GL_SHORT:
342 {
343 GLuint i;
344 const GLshort *s = (const GLshort *) src;
345 if (unpack->SwapBytes) {
346 for (i = 0; i < n; i++) {
347 GLshort value = s[i];
348 SWAP2BYTE(value);
349 indexes[i] = value;
350 }
351 }
352 else {
353 for (i = 0; i < n; i++)
354 indexes[i] = s[i];
355 }
356 }
357 break;
358 case GL_UNSIGNED_INT:
359 {
360 GLuint i;
361 const GLuint *s = (const GLuint *) src;
362 if (unpack->SwapBytes) {
363 for (i = 0; i < n; i++) {
364 GLuint value = s[i];
365 SWAP4BYTE(value);
366 indexes[i] = value;
367 }
368 }
369 else {
370 for (i = 0; i < n; i++)
371 indexes[i] = s[i];
372 }
373 }
374 break;
375 case GL_INT:
376 {
377 GLuint i;
378 const GLint *s = (const GLint *) src;
379 if (unpack->SwapBytes) {
380 for (i = 0; i < n; i++) {
381 GLint value = s[i];
382 SWAP4BYTE(value);
383 indexes[i] = value;
384 }
385 }
386 else {
387 for (i = 0; i < n; i++)
388 indexes[i] = s[i];
389 }
390 }
391 break;
392 case GL_FLOAT:
393 {
394 GLuint i;
395 const GLfloat *s = (const GLfloat *) src;
396 if (unpack->SwapBytes) {
397 for (i = 0; i < n; i++) {
398 GLfloat value = s[i];
399 SWAP4BYTE(value);
400 indexes[i] = (GLuint) value;
401 }
402 }
403 else {
404 for (i = 0; i < n; i++)
405 indexes[i] = (GLuint) s[i];
406 }
407 }
408 break;
409 case GL_HALF_FLOAT_ARB:
410 {
411 GLuint i;
412 const GLhalfARB *s = (const GLhalfARB *) src;
413 if (unpack->SwapBytes) {
414 for (i = 0; i < n; i++) {
415 GLhalfARB value = s[i];
416 SWAP2BYTE(value);
417 indexes[i] = (GLuint) _mesa_half_to_float(value);
418 }
419 }
420 else {
421 for (i = 0; i < n; i++)
422 indexes[i] = (GLuint) _mesa_half_to_float(s[i]);
423 }
424 }
425 break;
426 case GL_UNSIGNED_INT_24_8_EXT:
427 {
428 GLuint i;
429 const GLuint *s = (const GLuint *) src;
430 if (unpack->SwapBytes) {
431 for (i = 0; i < n; i++) {
432 GLuint value = s[i];
433 SWAP4BYTE(value);
434 indexes[i] = value & 0xff; /* lower 8 bits */
435 }
436 }
437 else {
438 for (i = 0; i < n; i++)
439 indexes[i] = s[i] & 0xff; /* lower 8 bits */
440 }
441 }
442 break;
443 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
444 {
445 GLuint i;
446 const GLuint *s = (const GLuint *) src;
447 if (unpack->SwapBytes) {
448 for (i = 0; i < n; i++) {
449 GLuint value = s[i*2+1];
450 SWAP4BYTE(value);
451 indexes[i] = value & 0xff; /* lower 8 bits */
452 }
453 }
454 else {
455 for (i = 0; i < n; i++)
456 indexes[i] = s[i*2+1] & 0xff; /* lower 8 bits */
457 }
458 }
459 break;
460
461 default:
462 _mesa_problem(NULL, "bad srcType in extract_uint_indexes");
463 return;
464 }
465 }
466
467
468 static inline GLuint
469 clamp_float_to_uint(GLfloat f)
470 {
471 return f < 0.0F ? 0 : F_TO_I(f);
472 }
473
474
475 static inline GLuint
476 clamp_half_to_uint(GLhalfARB h)
477 {
478 GLfloat f = _mesa_half_to_float(h);
479 return f < 0.0F ? 0 : F_TO_I(f);
480 }
481
482
483 /*
484 * Unpack a row of color index data from a client buffer according to
485 * the pixel unpacking parameters.
486 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
487 *
488 * Args: ctx - the context
489 * n - number of pixels
490 * dstType - destination data type
491 * dest - destination array
492 * srcType - source pixel type
493 * source - source data pointer
494 * srcPacking - pixel unpacking parameters
495 * transferOps - the pixel transfer operations to apply
496 */
497 void
498 _mesa_unpack_index_span( struct gl_context *ctx, GLuint n,
499 GLenum dstType, GLvoid *dest,
500 GLenum srcType, const GLvoid *source,
501 const struct gl_pixelstore_attrib *srcPacking,
502 GLbitfield transferOps )
503 {
504 ASSERT(srcType == GL_BITMAP ||
505 srcType == GL_UNSIGNED_BYTE ||
506 srcType == GL_BYTE ||
507 srcType == GL_UNSIGNED_SHORT ||
508 srcType == GL_SHORT ||
509 srcType == GL_UNSIGNED_INT ||
510 srcType == GL_INT ||
511 srcType == GL_HALF_FLOAT_ARB ||
512 srcType == GL_FLOAT);
513
514 ASSERT(dstType == GL_UNSIGNED_BYTE ||
515 dstType == GL_UNSIGNED_SHORT ||
516 dstType == GL_UNSIGNED_INT);
517
518
519 transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
520
521 /*
522 * Try simple cases first
523 */
524 if (transferOps == 0 && srcType == GL_UNSIGNED_BYTE
525 && dstType == GL_UNSIGNED_BYTE) {
526 memcpy(dest, source, n * sizeof(GLubyte));
527 }
528 else if (transferOps == 0 && srcType == GL_UNSIGNED_INT
529 && dstType == GL_UNSIGNED_INT && !srcPacking->SwapBytes) {
530 memcpy(dest, source, n * sizeof(GLuint));
531 }
532 else {
533 /*
534 * general solution
535 */
536 GLuint *indexes = malloc(n * sizeof(GLuint));
537
538 if (!indexes) {
539 _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
540 return;
541 }
542
543 extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
544 srcPacking);
545
546 if (transferOps)
547 _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
548
549 /* convert to dest type */
550 switch (dstType) {
551 case GL_UNSIGNED_BYTE:
552 {
553 GLubyte *dst = (GLubyte *) dest;
554 GLuint i;
555 for (i = 0; i < n; i++) {
556 dst[i] = (GLubyte) (indexes[i] & 0xff);
557 }
558 }
559 break;
560 case GL_UNSIGNED_SHORT:
561 {
562 GLuint *dst = (GLuint *) dest;
563 GLuint i;
564 for (i = 0; i < n; i++) {
565 dst[i] = (GLushort) (indexes[i] & 0xffff);
566 }
567 }
568 break;
569 case GL_UNSIGNED_INT:
570 memcpy(dest, indexes, n * sizeof(GLuint));
571 break;
572 default:
573 _mesa_problem(ctx, "bad dstType in _mesa_unpack_index_span");
574 }
575
576 free(indexes);
577 }
578 }
579
580
581 void
582 _mesa_pack_index_span( struct gl_context *ctx, GLuint n,
583 GLenum dstType, GLvoid *dest, const GLuint *source,
584 const struct gl_pixelstore_attrib *dstPacking,
585 GLbitfield transferOps )
586 {
587 GLuint *indexes = malloc(n * sizeof(GLuint));
588
589 if (!indexes) {
590 _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel packing");
591 return;
592 }
593
594 transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
595
596 if (transferOps & (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT)) {
597 /* make a copy of input */
598 memcpy(indexes, source, n * sizeof(GLuint));
599 _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
600 source = indexes;
601 }
602
603 switch (dstType) {
604 case GL_UNSIGNED_BYTE:
605 {
606 GLubyte *dst = (GLubyte *) dest;
607 GLuint i;
608 for (i = 0; i < n; i++) {
609 *dst++ = (GLubyte) source[i];
610 }
611 }
612 break;
613 case GL_BYTE:
614 {
615 GLbyte *dst = (GLbyte *) dest;
616 GLuint i;
617 for (i = 0; i < n; i++) {
618 dst[i] = (GLbyte) source[i];
619 }
620 }
621 break;
622 case GL_UNSIGNED_SHORT:
623 {
624 GLushort *dst = (GLushort *) dest;
625 GLuint i;
626 for (i = 0; i < n; i++) {
627 dst[i] = (GLushort) source[i];
628 }
629 if (dstPacking->SwapBytes) {
630 _mesa_swap2( (GLushort *) dst, n );
631 }
632 }
633 break;
634 case GL_SHORT:
635 {
636 GLshort *dst = (GLshort *) dest;
637 GLuint i;
638 for (i = 0; i < n; i++) {
639 dst[i] = (GLshort) source[i];
640 }
641 if (dstPacking->SwapBytes) {
642 _mesa_swap2( (GLushort *) dst, n );
643 }
644 }
645 break;
646 case GL_UNSIGNED_INT:
647 {
648 GLuint *dst = (GLuint *) dest;
649 GLuint i;
650 for (i = 0; i < n; i++) {
651 dst[i] = (GLuint) source[i];
652 }
653 if (dstPacking->SwapBytes) {
654 _mesa_swap4( (GLuint *) dst, n );
655 }
656 }
657 break;
658 case GL_INT:
659 {
660 GLint *dst = (GLint *) dest;
661 GLuint i;
662 for (i = 0; i < n; i++) {
663 dst[i] = (GLint) source[i];
664 }
665 if (dstPacking->SwapBytes) {
666 _mesa_swap4( (GLuint *) dst, n );
667 }
668 }
669 break;
670 case GL_FLOAT:
671 {
672 GLfloat *dst = (GLfloat *) dest;
673 GLuint i;
674 for (i = 0; i < n; i++) {
675 dst[i] = (GLfloat) source[i];
676 }
677 if (dstPacking->SwapBytes) {
678 _mesa_swap4( (GLuint *) dst, n );
679 }
680 }
681 break;
682 case GL_HALF_FLOAT_ARB:
683 {
684 GLhalfARB *dst = (GLhalfARB *) dest;
685 GLuint i;
686 for (i = 0; i < n; i++) {
687 dst[i] = _mesa_float_to_half((GLfloat) source[i]);
688 }
689 if (dstPacking->SwapBytes) {
690 _mesa_swap2( (GLushort *) dst, n );
691 }
692 }
693 break;
694 default:
695 _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
696 }
697
698 free(indexes);
699 }
700
701
702 /*
703 * Unpack a row of stencil data from a client buffer according to
704 * the pixel unpacking parameters.
705 * This is (or will be) used by glDrawPixels
706 *
707 * Args: ctx - the context
708 * n - number of pixels
709 * dstType - destination data type
710 * dest - destination array
711 * srcType - source pixel type
712 * source - source data pointer
713 * srcPacking - pixel unpacking parameters
714 * transferOps - apply offset/bias/lookup ops?
715 */
716 void
717 _mesa_unpack_stencil_span( struct gl_context *ctx, GLuint n,
718 GLenum dstType, GLvoid *dest,
719 GLenum srcType, const GLvoid *source,
720 const struct gl_pixelstore_attrib *srcPacking,
721 GLbitfield transferOps )
722 {
723 ASSERT(srcType == GL_BITMAP ||
724 srcType == GL_UNSIGNED_BYTE ||
725 srcType == GL_BYTE ||
726 srcType == GL_UNSIGNED_SHORT ||
727 srcType == GL_SHORT ||
728 srcType == GL_UNSIGNED_INT ||
729 srcType == GL_INT ||
730 srcType == GL_UNSIGNED_INT_24_8_EXT ||
731 srcType == GL_HALF_FLOAT_ARB ||
732 srcType == GL_FLOAT ||
733 srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
734
735 ASSERT(dstType == GL_UNSIGNED_BYTE ||
736 dstType == GL_UNSIGNED_SHORT ||
737 dstType == GL_UNSIGNED_INT ||
738 dstType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
739
740 /* only shift and offset apply to stencil */
741 transferOps &= IMAGE_SHIFT_OFFSET_BIT;
742
743 /*
744 * Try simple cases first
745 */
746 if (transferOps == 0 &&
747 !ctx->Pixel.MapStencilFlag &&
748 srcType == GL_UNSIGNED_BYTE &&
749 dstType == GL_UNSIGNED_BYTE) {
750 memcpy(dest, source, n * sizeof(GLubyte));
751 }
752 else if (transferOps == 0 &&
753 !ctx->Pixel.MapStencilFlag &&
754 srcType == GL_UNSIGNED_INT &&
755 dstType == GL_UNSIGNED_INT &&
756 !srcPacking->SwapBytes) {
757 memcpy(dest, source, n * sizeof(GLuint));
758 }
759 else {
760 /*
761 * general solution
762 */
763 GLuint *indexes = malloc(n * sizeof(GLuint));
764
765 if (!indexes) {
766 _mesa_error(ctx, GL_OUT_OF_MEMORY, "stencil unpacking");
767 return;
768 }
769
770 extract_uint_indexes(n, indexes, GL_STENCIL_INDEX, srcType, source,
771 srcPacking);
772
773 if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
774 /* shift and offset indexes */
775 _mesa_shift_and_offset_ci(ctx, n, indexes);
776 }
777
778 if (ctx->Pixel.MapStencilFlag) {
779 /* Apply stencil lookup table */
780 const GLuint mask = ctx->PixelMaps.StoS.Size - 1;
781 GLuint i;
782 for (i = 0; i < n; i++) {
783 indexes[i] = (GLuint)ctx->PixelMaps.StoS.Map[ indexes[i] & mask ];
784 }
785 }
786
787 /* convert to dest type */
788 switch (dstType) {
789 case GL_UNSIGNED_BYTE:
790 {
791 GLubyte *dst = (GLubyte *) dest;
792 GLuint i;
793 for (i = 0; i < n; i++) {
794 dst[i] = (GLubyte) (indexes[i] & 0xff);
795 }
796 }
797 break;
798 case GL_UNSIGNED_SHORT:
799 {
800 GLuint *dst = (GLuint *) dest;
801 GLuint i;
802 for (i = 0; i < n; i++) {
803 dst[i] = (GLushort) (indexes[i] & 0xffff);
804 }
805 }
806 break;
807 case GL_UNSIGNED_INT:
808 memcpy(dest, indexes, n * sizeof(GLuint));
809 break;
810 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
811 {
812 GLuint *dst = (GLuint *) dest;
813 GLuint i;
814 for (i = 0; i < n; i++) {
815 dst[i*2+1] = indexes[i] & 0xff; /* lower 8 bits */
816 }
817 }
818 break;
819 default:
820 _mesa_problem(ctx, "bad dstType in _mesa_unpack_stencil_span");
821 }
822
823 free(indexes);
824 }
825 }
826
827
828 void
829 _mesa_pack_stencil_span( struct gl_context *ctx, GLuint n,
830 GLenum dstType, GLvoid *dest, const GLubyte *source,
831 const struct gl_pixelstore_attrib *dstPacking )
832 {
833 GLubyte *stencil = malloc(n * sizeof(GLubyte));
834
835 if (!stencil) {
836 _mesa_error(ctx, GL_OUT_OF_MEMORY, "stencil packing");
837 return;
838 }
839
840 if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset ||
841 ctx->Pixel.MapStencilFlag) {
842 /* make a copy of input */
843 memcpy(stencil, source, n * sizeof(GLubyte));
844 _mesa_apply_stencil_transfer_ops(ctx, n, stencil);
845 source = stencil;
846 }
847
848 switch (dstType) {
849 case GL_UNSIGNED_BYTE:
850 memcpy(dest, source, n);
851 break;
852 case GL_BYTE:
853 {
854 GLbyte *dst = (GLbyte *) dest;
855 GLuint i;
856 for (i=0;i<n;i++) {
857 dst[i] = (GLbyte) (source[i] & 0x7f);
858 }
859 }
860 break;
861 case GL_UNSIGNED_SHORT:
862 {
863 GLushort *dst = (GLushort *) dest;
864 GLuint i;
865 for (i=0;i<n;i++) {
866 dst[i] = (GLushort) source[i];
867 }
868 if (dstPacking->SwapBytes) {
869 _mesa_swap2( (GLushort *) dst, n );
870 }
871 }
872 break;
873 case GL_SHORT:
874 {
875 GLshort *dst = (GLshort *) dest;
876 GLuint i;
877 for (i=0;i<n;i++) {
878 dst[i] = (GLshort) source[i];
879 }
880 if (dstPacking->SwapBytes) {
881 _mesa_swap2( (GLushort *) dst, n );
882 }
883 }
884 break;
885 case GL_UNSIGNED_INT:
886 {
887 GLuint *dst = (GLuint *) dest;
888 GLuint i;
889 for (i=0;i<n;i++) {
890 dst[i] = (GLuint) source[i];
891 }
892 if (dstPacking->SwapBytes) {
893 _mesa_swap4( (GLuint *) dst, n );
894 }
895 }
896 break;
897 case GL_INT:
898 {
899 GLint *dst = (GLint *) dest;
900 GLuint i;
901 for (i=0;i<n;i++) {
902 dst[i] = (GLint) source[i];
903 }
904 if (dstPacking->SwapBytes) {
905 _mesa_swap4( (GLuint *) dst, n );
906 }
907 }
908 break;
909 case GL_FLOAT:
910 {
911 GLfloat *dst = (GLfloat *) dest;
912 GLuint i;
913 for (i=0;i<n;i++) {
914 dst[i] = (GLfloat) source[i];
915 }
916 if (dstPacking->SwapBytes) {
917 _mesa_swap4( (GLuint *) dst, n );
918 }
919 }
920 break;
921 case GL_HALF_FLOAT_ARB:
922 {
923 GLhalfARB *dst = (GLhalfARB *) dest;
924 GLuint i;
925 for (i=0;i<n;i++) {
926 dst[i] = _mesa_float_to_half( (float) source[i] );
927 }
928 if (dstPacking->SwapBytes) {
929 _mesa_swap2( (GLushort *) dst, n );
930 }
931 }
932 break;
933 case GL_BITMAP:
934 if (dstPacking->LsbFirst) {
935 GLubyte *dst = (GLubyte *) dest;
936 GLint shift = 0;
937 GLuint i;
938 for (i = 0; i < n; i++) {
939 if (shift == 0)
940 *dst = 0;
941 *dst |= ((source[i] != 0) << shift);
942 shift++;
943 if (shift == 8) {
944 shift = 0;
945 dst++;
946 }
947 }
948 }
949 else {
950 GLubyte *dst = (GLubyte *) dest;
951 GLint shift = 7;
952 GLuint i;
953 for (i = 0; i < n; i++) {
954 if (shift == 7)
955 *dst = 0;
956 *dst |= ((source[i] != 0) << shift);
957 shift--;
958 if (shift < 0) {
959 shift = 7;
960 dst++;
961 }
962 }
963 }
964 break;
965 default:
966 _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
967 }
968
969 free(stencil);
970 }
971
972 #define DEPTH_VALUES(GLTYPE, GLTYPE2FLOAT) \
973 do { \
974 GLuint i; \
975 const GLTYPE *src = (const GLTYPE *)source; \
976 for (i = 0; i < n; i++) { \
977 GLTYPE value = src[i]; \
978 if (srcPacking->SwapBytes) { \
979 if (sizeof(GLTYPE) == 2) { \
980 SWAP2BYTE(value); \
981 } else if (sizeof(GLTYPE) == 4) { \
982 SWAP4BYTE(value); \
983 } \
984 } \
985 depthValues[i] = GLTYPE2FLOAT(value); \
986 } \
987 } while (0)
988
989
990 /**
991 * Unpack a row of depth/z values from memory, returning GLushort, GLuint
992 * or GLfloat values.
993 * The glPixelTransfer (scale/bias) params will be applied.
994 *
995 * \param dstType one of GL_UNSIGNED_SHORT, GL_UNSIGNED_INT, GL_FLOAT
996 * \param depthMax max value for returned GLushort or GLuint values
997 * (ignored for GLfloat).
998 */
999 void
1000 _mesa_unpack_depth_span( struct gl_context *ctx, GLuint n,
1001 GLenum dstType, GLvoid *dest, GLuint depthMax,
1002 GLenum srcType, const GLvoid *source,
1003 const struct gl_pixelstore_attrib *srcPacking )
1004 {
1005 GLfloat *depthTemp = NULL, *depthValues;
1006 GLboolean needClamp = GL_FALSE;
1007
1008 /* Look for special cases first.
1009 * Not only are these faster, they're less prone to numeric conversion
1010 * problems. Otherwise, converting from an int type to a float then
1011 * back to an int type can introduce errors that will show up as
1012 * artifacts in things like depth peeling which uses glCopyTexImage.
1013 */
1014 if (ctx->Pixel.DepthScale == 1.0 && ctx->Pixel.DepthBias == 0.0) {
1015 if (srcType == GL_UNSIGNED_INT && dstType == GL_UNSIGNED_SHORT) {
1016 const GLuint *src = (const GLuint *) source;
1017 GLushort *dst = (GLushort *) dest;
1018 GLuint i;
1019 for (i = 0; i < n; i++) {
1020 dst[i] = src[i] >> 16;
1021 }
1022 return;
1023 }
1024 if (srcType == GL_UNSIGNED_SHORT
1025 && dstType == GL_UNSIGNED_INT
1026 && depthMax == 0xffffffff) {
1027 const GLushort *src = (const GLushort *) source;
1028 GLuint *dst = (GLuint *) dest;
1029 GLuint i;
1030 for (i = 0; i < n; i++) {
1031 dst[i] = src[i] | (src[i] << 16);
1032 }
1033 return;
1034 }
1035 if (srcType == GL_UNSIGNED_INT_24_8
1036 && dstType == GL_UNSIGNED_INT
1037 && depthMax == 0xffffff) {
1038 const GLuint *src = (const GLuint *) source;
1039 GLuint *dst = (GLuint *) dest;
1040 GLuint i;
1041 for (i = 0; i < n; i++) {
1042 dst[i] = src[i] >> 8;
1043 }
1044 return;
1045 }
1046 /* XXX may want to add additional cases here someday */
1047 }
1048
1049 /* general case path follows */
1050
1051 if (dstType == GL_FLOAT) {
1052 depthValues = (GLfloat *) dest;
1053 }
1054 else {
1055 depthTemp = malloc(n * sizeof(GLfloat));
1056 if (!depthTemp) {
1057 _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
1058 return;
1059 }
1060
1061 depthValues = depthTemp;
1062 }
1063
1064 /* Convert incoming values to GLfloat. Some conversions will require
1065 * clamping, below.
1066 */
1067 switch (srcType) {
1068 case GL_BYTE:
1069 DEPTH_VALUES(GLbyte, BYTE_TO_FLOATZ);
1070 needClamp = GL_TRUE;
1071 break;
1072 case GL_UNSIGNED_BYTE:
1073 DEPTH_VALUES(GLubyte, UBYTE_TO_FLOAT);
1074 break;
1075 case GL_SHORT:
1076 DEPTH_VALUES(GLshort, SHORT_TO_FLOATZ);
1077 needClamp = GL_TRUE;
1078 break;
1079 case GL_UNSIGNED_SHORT:
1080 DEPTH_VALUES(GLushort, USHORT_TO_FLOAT);
1081 break;
1082 case GL_INT:
1083 DEPTH_VALUES(GLint, INT_TO_FLOAT);
1084 needClamp = GL_TRUE;
1085 break;
1086 case GL_UNSIGNED_INT:
1087 DEPTH_VALUES(GLuint, UINT_TO_FLOAT);
1088 break;
1089 case GL_UNSIGNED_INT_24_8_EXT: /* GL_EXT_packed_depth_stencil */
1090 if (dstType == GL_UNSIGNED_INT_24_8_EXT &&
1091 depthMax == 0xffffff &&
1092 ctx->Pixel.DepthScale == 1.0 &&
1093 ctx->Pixel.DepthBias == 0.0) {
1094 const GLuint *src = (const GLuint *) source;
1095 GLuint *zValues = (GLuint *) dest;
1096 GLuint i;
1097 for (i = 0; i < n; i++) {
1098 GLuint value = src[i];
1099 if (srcPacking->SwapBytes) {
1100 SWAP4BYTE(value);
1101 }
1102 zValues[i] = value & 0xffffff00;
1103 }
1104 free(depthTemp);
1105 return;
1106 }
1107 else {
1108 const GLuint *src = (const GLuint *) source;
1109 const GLfloat scale = 1.0f / 0xffffff;
1110 GLuint i;
1111 for (i = 0; i < n; i++) {
1112 GLuint value = src[i];
1113 if (srcPacking->SwapBytes) {
1114 SWAP4BYTE(value);
1115 }
1116 depthValues[i] = (value >> 8) * scale;
1117 }
1118 }
1119 break;
1120 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
1121 {
1122 GLuint i;
1123 const GLfloat *src = (const GLfloat *)source;
1124 for (i = 0; i < n; i++) {
1125 GLfloat value = src[i * 2];
1126 if (srcPacking->SwapBytes) {
1127 SWAP4BYTE(value);
1128 }
1129 depthValues[i] = value;
1130 }
1131 needClamp = GL_TRUE;
1132 }
1133 break;
1134 case GL_FLOAT:
1135 DEPTH_VALUES(GLfloat, 1*);
1136 needClamp = GL_TRUE;
1137 break;
1138 case GL_HALF_FLOAT_ARB:
1139 {
1140 GLuint i;
1141 const GLhalfARB *src = (const GLhalfARB *) source;
1142 for (i = 0; i < n; i++) {
1143 GLhalfARB value = src[i];
1144 if (srcPacking->SwapBytes) {
1145 SWAP2BYTE(value);
1146 }
1147 depthValues[i] = _mesa_half_to_float(value);
1148 }
1149 needClamp = GL_TRUE;
1150 }
1151 break;
1152 default:
1153 _mesa_problem(NULL, "bad type in _mesa_unpack_depth_span()");
1154 free(depthTemp);
1155 return;
1156 }
1157
1158 /* apply depth scale and bias */
1159 {
1160 const GLfloat scale = ctx->Pixel.DepthScale;
1161 const GLfloat bias = ctx->Pixel.DepthBias;
1162 if (scale != 1.0 || bias != 0.0) {
1163 GLuint i;
1164 for (i = 0; i < n; i++) {
1165 depthValues[i] = depthValues[i] * scale + bias;
1166 }
1167 needClamp = GL_TRUE;
1168 }
1169 }
1170
1171 /* clamp to [0, 1] */
1172 if (needClamp) {
1173 GLuint i;
1174 for (i = 0; i < n; i++) {
1175 depthValues[i] = (GLfloat)CLAMP(depthValues[i], 0.0, 1.0);
1176 }
1177 }
1178
1179 /*
1180 * Convert values to dstType
1181 */
1182 if (dstType == GL_UNSIGNED_INT) {
1183 GLuint *zValues = (GLuint *) dest;
1184 GLuint i;
1185 if (depthMax <= 0xffffff) {
1186 /* no overflow worries */
1187 for (i = 0; i < n; i++) {
1188 zValues[i] = (GLuint) (depthValues[i] * (GLfloat) depthMax);
1189 }
1190 }
1191 else {
1192 /* need to use double precision to prevent overflow problems */
1193 for (i = 0; i < n; i++) {
1194 GLdouble z = depthValues[i] * (GLdouble) depthMax;
1195 if (z >= (GLdouble) 0xffffffff)
1196 zValues[i] = 0xffffffff;
1197 else
1198 zValues[i] = (GLuint) z;
1199 }
1200 }
1201 }
1202 else if (dstType == GL_UNSIGNED_SHORT) {
1203 GLushort *zValues = (GLushort *) dest;
1204 GLuint i;
1205 ASSERT(depthMax <= 0xffff);
1206 for (i = 0; i < n; i++) {
1207 zValues[i] = (GLushort) (depthValues[i] * (GLfloat) depthMax);
1208 }
1209 }
1210 else if (dstType == GL_FLOAT) {
1211 /* Nothing to do. depthValues is pointing to dest. */
1212 }
1213 else if (dstType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV) {
1214 GLfloat *zValues = (GLfloat*) dest;
1215 GLuint i;
1216 for (i = 0; i < n; i++) {
1217 zValues[i*2] = depthValues[i];
1218 }
1219 }
1220 else {
1221 ASSERT(0);
1222 }
1223
1224 free(depthTemp);
1225 }
1226
1227
1228 /*
1229 * Pack an array of depth values. The values are floats in [0,1].
1230 */
1231 void
1232 _mesa_pack_depth_span( struct gl_context *ctx, GLuint n, GLvoid *dest,
1233 GLenum dstType, const GLfloat *depthSpan,
1234 const struct gl_pixelstore_attrib *dstPacking )
1235 {
1236 GLfloat *depthCopy = malloc(n * sizeof(GLfloat));
1237 if (!depthCopy) {
1238 _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel packing");
1239 return;
1240 }
1241
1242 if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
1243 memcpy(depthCopy, depthSpan, n * sizeof(GLfloat));
1244 _mesa_scale_and_bias_depth(ctx, n, depthCopy);
1245 depthSpan = depthCopy;
1246 }
1247
1248 switch (dstType) {
1249 case GL_UNSIGNED_BYTE:
1250 {
1251 GLubyte *dst = (GLubyte *) dest;
1252 GLuint i;
1253 for (i = 0; i < n; i++) {
1254 dst[i] = FLOAT_TO_UBYTE( depthSpan[i] );
1255 }
1256 }
1257 break;
1258 case GL_BYTE:
1259 {
1260 GLbyte *dst = (GLbyte *) dest;
1261 GLuint i;
1262 for (i = 0; i < n; i++) {
1263 dst[i] = FLOAT_TO_BYTE( depthSpan[i] );
1264 }
1265 }
1266 break;
1267 case GL_UNSIGNED_SHORT:
1268 {
1269 GLushort *dst = (GLushort *) dest;
1270 GLuint i;
1271 for (i = 0; i < n; i++) {
1272 CLAMPED_FLOAT_TO_USHORT(dst[i], depthSpan[i]);
1273 }
1274 if (dstPacking->SwapBytes) {
1275 _mesa_swap2( (GLushort *) dst, n );
1276 }
1277 }
1278 break;
1279 case GL_SHORT:
1280 {
1281 GLshort *dst = (GLshort *) dest;
1282 GLuint i;
1283 for (i = 0; i < n; i++) {
1284 dst[i] = FLOAT_TO_SHORT( depthSpan[i] );
1285 }
1286 if (dstPacking->SwapBytes) {
1287 _mesa_swap2( (GLushort *) dst, n );
1288 }
1289 }
1290 break;
1291 case GL_UNSIGNED_INT:
1292 {
1293 GLuint *dst = (GLuint *) dest;
1294 GLuint i;
1295 for (i = 0; i < n; i++) {
1296 dst[i] = FLOAT_TO_UINT( depthSpan[i] );
1297 }
1298 if (dstPacking->SwapBytes) {
1299 _mesa_swap4( (GLuint *) dst, n );
1300 }
1301 }
1302 break;
1303 case GL_INT:
1304 {
1305 GLint *dst = (GLint *) dest;
1306 GLuint i;
1307 for (i = 0; i < n; i++) {
1308 dst[i] = FLOAT_TO_INT( depthSpan[i] );
1309 }
1310 if (dstPacking->SwapBytes) {
1311 _mesa_swap4( (GLuint *) dst, n );
1312 }
1313 }
1314 break;
1315 case GL_FLOAT:
1316 {
1317 GLfloat *dst = (GLfloat *) dest;
1318 GLuint i;
1319 for (i = 0; i < n; i++) {
1320 dst[i] = depthSpan[i];
1321 }
1322 if (dstPacking->SwapBytes) {
1323 _mesa_swap4( (GLuint *) dst, n );
1324 }
1325 }
1326 break;
1327 case GL_HALF_FLOAT_ARB:
1328 {
1329 GLhalfARB *dst = (GLhalfARB *) dest;
1330 GLuint i;
1331 for (i = 0; i < n; i++) {
1332 dst[i] = _mesa_float_to_half(depthSpan[i]);
1333 }
1334 if (dstPacking->SwapBytes) {
1335 _mesa_swap2( (GLushort *) dst, n );
1336 }
1337 }
1338 break;
1339 default:
1340 _mesa_problem(ctx, "bad type in _mesa_pack_depth_span");
1341 }
1342
1343 free(depthCopy);
1344 }
1345
1346
1347
1348 /**
1349 * Pack depth and stencil values as GL_DEPTH_STENCIL (GL_UNSIGNED_INT_24_8 etc)
1350 */
1351 void
1352 _mesa_pack_depth_stencil_span(struct gl_context *ctx,GLuint n,
1353 GLenum dstType, GLuint *dest,
1354 const GLfloat *depthVals,
1355 const GLubyte *stencilVals,
1356 const struct gl_pixelstore_attrib *dstPacking)
1357 {
1358 GLfloat *depthCopy = malloc(n * sizeof(GLfloat));
1359 GLubyte *stencilCopy = malloc(n * sizeof(GLubyte));
1360 GLuint i;
1361
1362 if (!depthCopy || !stencilCopy) {
1363 _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel packing");
1364 free(depthCopy);
1365 free(stencilCopy);
1366 return;
1367 }
1368
1369 if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
1370 memcpy(depthCopy, depthVals, n * sizeof(GLfloat));
1371 _mesa_scale_and_bias_depth(ctx, n, depthCopy);
1372 depthVals = depthCopy;
1373 }
1374
1375 if (ctx->Pixel.IndexShift ||
1376 ctx->Pixel.IndexOffset ||
1377 ctx->Pixel.MapStencilFlag) {
1378 memcpy(stencilCopy, stencilVals, n * sizeof(GLubyte));
1379 _mesa_apply_stencil_transfer_ops(ctx, n, stencilCopy);
1380 stencilVals = stencilCopy;
1381 }
1382
1383 switch (dstType) {
1384 case GL_UNSIGNED_INT_24_8:
1385 for (i = 0; i < n; i++) {
1386 GLuint z = (GLuint) (depthVals[i] * 0xffffff);
1387 dest[i] = (z << 8) | (stencilVals[i] & 0xff);
1388 }
1389 break;
1390 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
1391 for (i = 0; i < n; i++) {
1392 ((GLfloat*)dest)[i*2] = depthVals[i];
1393 dest[i*2+1] = stencilVals[i] & 0xff;
1394 }
1395 break;
1396 }
1397
1398 if (dstPacking->SwapBytes) {
1399 _mesa_swap4(dest, n);
1400 }
1401
1402 free(depthCopy);
1403 free(stencilCopy);
1404 }
1405
1406
1407
1408 /**
1409 * Unpack image data. Apply byte swapping, byte flipping (bitmap).
1410 * Return all image data in a contiguous block. This is used when we
1411 * compile glDrawPixels, glTexImage, etc into a display list. We
1412 * need a copy of the data in a standard format.
1413 */
1414 void *
1415 _mesa_unpack_image( GLuint dimensions,
1416 GLsizei width, GLsizei height, GLsizei depth,
1417 GLenum format, GLenum type, const GLvoid *pixels,
1418 const struct gl_pixelstore_attrib *unpack )
1419 {
1420 GLint bytesPerRow, compsPerRow;
1421 GLboolean flipBytes, swap2, swap4;
1422
1423 if (!pixels)
1424 return NULL; /* not necessarily an error */
1425
1426 if (width <= 0 || height <= 0 || depth <= 0)
1427 return NULL; /* generate error later */
1428
1429 if (type == GL_BITMAP) {
1430 bytesPerRow = (width + 7) >> 3;
1431 flipBytes = unpack->LsbFirst;
1432 swap2 = swap4 = GL_FALSE;
1433 compsPerRow = 0;
1434 }
1435 else {
1436 const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
1437 GLint components = _mesa_components_in_format(format);
1438 GLint bytesPerComp;
1439
1440 if (_mesa_type_is_packed(type))
1441 components = 1;
1442
1443 if (bytesPerPixel <= 0 || components <= 0)
1444 return NULL; /* bad format or type. generate error later */
1445 bytesPerRow = bytesPerPixel * width;
1446 bytesPerComp = bytesPerPixel / components;
1447 flipBytes = GL_FALSE;
1448 swap2 = (bytesPerComp == 2) && unpack->SwapBytes;
1449 swap4 = (bytesPerComp == 4) && unpack->SwapBytes;
1450 compsPerRow = components * width;
1451 assert(compsPerRow >= width);
1452 }
1453
1454 {
1455 GLubyte *destBuffer
1456 = malloc(bytesPerRow * height * depth);
1457 GLubyte *dst;
1458 GLint img, row;
1459 if (!destBuffer)
1460 return NULL; /* generate GL_OUT_OF_MEMORY later */
1461
1462 dst = destBuffer;
1463 for (img = 0; img < depth; img++) {
1464 for (row = 0; row < height; row++) {
1465 const GLvoid *src = _mesa_image_address(dimensions, unpack, pixels,
1466 width, height, format, type, img, row, 0);
1467
1468 if ((type == GL_BITMAP) && (unpack->SkipPixels & 0x7)) {
1469 GLint i;
1470 flipBytes = GL_FALSE;
1471 if (unpack->LsbFirst) {
1472 GLubyte srcMask = 1 << (unpack->SkipPixels & 0x7);
1473 GLubyte dstMask = 128;
1474 const GLubyte *s = src;
1475 GLubyte *d = dst;
1476 *d = 0;
1477 for (i = 0; i < width; i++) {
1478 if (*s & srcMask) {
1479 *d |= dstMask;
1480 }
1481 if (srcMask == 128) {
1482 srcMask = 1;
1483 s++;
1484 }
1485 else {
1486 srcMask = srcMask << 1;
1487 }
1488 if (dstMask == 1) {
1489 dstMask = 128;
1490 d++;
1491 *d = 0;
1492 }
1493 else {
1494 dstMask = dstMask >> 1;
1495 }
1496 }
1497 }
1498 else {
1499 GLubyte srcMask = 128 >> (unpack->SkipPixels & 0x7);
1500 GLubyte dstMask = 128;
1501 const GLubyte *s = src;
1502 GLubyte *d = dst;
1503 *d = 0;
1504 for (i = 0; i < width; i++) {
1505 if (*s & srcMask) {
1506 *d |= dstMask;
1507 }
1508 if (srcMask == 1) {
1509 srcMask = 128;
1510 s++;
1511 }
1512 else {
1513 srcMask = srcMask >> 1;
1514 }
1515 if (dstMask == 1) {
1516 dstMask = 128;
1517 d++;
1518 *d = 0;
1519 }
1520 else {
1521 dstMask = dstMask >> 1;
1522 }
1523 }
1524 }
1525 }
1526 else {
1527 memcpy(dst, src, bytesPerRow);
1528 }
1529
1530 /* byte flipping/swapping */
1531 if (flipBytes) {
1532 flip_bytes((GLubyte *) dst, bytesPerRow);
1533 }
1534 else if (swap2) {
1535 _mesa_swap2((GLushort*) dst, compsPerRow);
1536 }
1537 else if (swap4) {
1538 _mesa_swap4((GLuint*) dst, compsPerRow);
1539 }
1540 dst += bytesPerRow;
1541 }
1542 }
1543 return destBuffer;
1544 }
1545 }
1546
1547
1548
1549 /**
1550 * If we unpack colors from a luminance surface, we'll get pixel colors
1551 * such as (l, l, l, a).
1552 * When we call _mesa_pack_rgba_span_float(format=GL_LUMINANCE), that
1553 * function will compute L=R+G+B before packing. The net effect is we'll
1554 * accidentally store luminance values = 3*l.
1555 * This function compensates for that by converting (aka rebasing) (l,l,l,a)
1556 * to be (l,0,0,a).
1557 * It's a similar story for other formats such as LUMINANCE_ALPHA, ALPHA
1558 * and INTENSITY.
1559 *
1560 * Finally, we also need to do this when the actual surface format does
1561 * not match the logical surface format. For example, suppose the user
1562 * requests a GL_LUMINANCE texture but the driver stores it as RGBA.
1563 * Again, we'll get pixel values like (l,l,l,a).
1564 */
1565 void
1566 _mesa_rebase_rgba_float(GLuint n, GLfloat rgba[][4], GLenum baseFormat)
1567 {
1568 GLuint i;
1569
1570 switch (baseFormat) {
1571 case GL_ALPHA:
1572 for (i = 0; i < n; i++) {
1573 rgba[i][RCOMP] = 0.0F;
1574 rgba[i][GCOMP] = 0.0F;
1575 rgba[i][BCOMP] = 0.0F;
1576 }
1577 break;
1578 case GL_INTENSITY:
1579 /* fall-through */
1580 case GL_LUMINANCE:
1581 for (i = 0; i < n; i++) {
1582 rgba[i][GCOMP] = 0.0F;
1583 rgba[i][BCOMP] = 0.0F;
1584 rgba[i][ACOMP] = 1.0F;
1585 }
1586 break;
1587 case GL_LUMINANCE_ALPHA:
1588 for (i = 0; i < n; i++) {
1589 rgba[i][GCOMP] = 0.0F;
1590 rgba[i][BCOMP] = 0.0F;
1591 }
1592 break;
1593 case GL_RGB:
1594 for (i = 0; i < n; i++) {
1595 rgba[i][ACOMP] = 1.0F;
1596 }
1597 break;
1598 case GL_RG:
1599 for (i = 0; i < n; i++) {
1600 rgba[i][BCOMP] = 0.0F;
1601 rgba[i][ACOMP] = 1.0F;
1602 }
1603 break;
1604 case GL_RED:
1605 for (i = 0; i < n; i++) {
1606 rgba[i][GCOMP] = 0.0F;
1607 rgba[i][BCOMP] = 0.0F;
1608 rgba[i][ACOMP] = 1.0F;
1609 }
1610 break;
1611
1612 default:
1613 /* no-op */
1614 ;
1615 }
1616 }
1617
1618
1619 /**
1620 * As above, but GLuint components.
1621 */
1622 void
1623 _mesa_rebase_rgba_uint(GLuint n, GLuint rgba[][4], GLenum baseFormat)
1624 {
1625 GLuint i;
1626
1627 switch (baseFormat) {
1628 case GL_ALPHA:
1629 for (i = 0; i < n; i++) {
1630 rgba[i][RCOMP] = 0;
1631 rgba[i][GCOMP] = 0;
1632 rgba[i][BCOMP] = 0;
1633 }
1634 break;
1635 case GL_INTENSITY:
1636 /* fall-through */
1637 case GL_LUMINANCE:
1638 for (i = 0; i < n; i++) {
1639 rgba[i][GCOMP] = 0;
1640 rgba[i][BCOMP] = 0;
1641 rgba[i][ACOMP] = 1;
1642 }
1643 break;
1644 case GL_LUMINANCE_ALPHA:
1645 for (i = 0; i < n; i++) {
1646 rgba[i][GCOMP] = 0;
1647 rgba[i][BCOMP] = 0;
1648 }
1649 break;
1650 case GL_RGB:
1651 for (i = 0; i < n; i++) {
1652 rgba[i][ACOMP] = 1;
1653 }
1654 break;
1655 case GL_RG:
1656 for (i = 0; i < n; i++) {
1657 rgba[i][BCOMP] = 0;
1658 rgba[i][ACOMP] = 1;
1659 }
1660 break;
1661 case GL_RED:
1662 for (i = 0; i < n; i++) {
1663 rgba[i][GCOMP] = 0;
1664 rgba[i][BCOMP] = 0;
1665 rgba[i][ACOMP] = 1;
1666 }
1667 default:
1668 /* no-op */
1669 ;
1670 }
1671 }
1672
1673 void
1674 _mesa_pack_luminance_from_rgba_float(GLuint n, GLfloat rgba[][4],
1675 GLvoid *dstAddr, GLenum dst_format,
1676 GLbitfield transferOps)
1677 {
1678 int i;
1679 GLfloat *dst = (GLfloat *) dstAddr;
1680
1681 switch (dst_format) {
1682 case GL_LUMINANCE:
1683 if (transferOps & IMAGE_CLAMP_BIT) {
1684 for (i = 0; i < n; i++) {
1685 GLfloat sum = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
1686 dst[i] = CLAMP(sum, 0.0F, 1.0F);
1687 }
1688 } else {
1689 for (i = 0; i < n; i++) {
1690 dst[i] = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
1691 }
1692 }
1693 return;
1694 case GL_LUMINANCE_ALPHA:
1695 if (transferOps & IMAGE_CLAMP_BIT) {
1696 for (i = 0; i < n; i++) {
1697 GLfloat sum = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
1698 dst[2*i] = CLAMP(sum, 0.0F, 1.0F);
1699 dst[2*i+1] = rgba[i][ACOMP];
1700 }
1701 } else {
1702 for (i = 0; i < n; i++) {
1703 dst[2*i] = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
1704 dst[2*i+1] = rgba[i][ACOMP];
1705 }
1706 }
1707 return;
1708 default:
1709 assert(!"Unsupported format");
1710 }
1711 }
1712
1713 static int32_t
1714 clamp_sint64_to_sint32(int64_t src)
1715 {
1716 return CLAMP(src, INT32_MIN, INT32_MAX);
1717 }
1718
1719 static int32_t
1720 clamp_sint64_to_uint32(int64_t src)
1721 {
1722 return CLAMP(src, 0, UINT32_MAX);
1723 }
1724
1725 static int32_t
1726 clamp_uint64_to_uint32(uint64_t src)
1727 {
1728 return MIN2(src, UINT32_MAX);
1729 }
1730
1731 static int32_t
1732 clamp_uint64_to_sint32(uint64_t src)
1733 {
1734 return MIN2(src, INT32_MAX);
1735 }
1736
1737 static int32_t
1738 convert_integer_luminance64(int64_t src64, int bits,
1739 bool dst_is_signed, bool src_is_signed)
1740 {
1741 int32_t src32;
1742
1743 /* Clamp Luminance value from 64-bit to 32-bit. Consider if we need
1744 * any signed<->unsigned conversion too.
1745 */
1746 if (src_is_signed && dst_is_signed)
1747 src32 = clamp_sint64_to_sint32(src64);
1748 else if (src_is_signed && !dst_is_signed)
1749 src32 = clamp_sint64_to_uint32(src64);
1750 else if (!src_is_signed && dst_is_signed)
1751 src32 = clamp_uint64_to_sint32(src64);
1752 else
1753 src32 = clamp_uint64_to_uint32(src64);
1754
1755 /* If the dst type is < 32-bit, we need an extra clamp */
1756 if (bits == 32) {
1757 return src32;
1758 } else {
1759 if (dst_is_signed)
1760 return _mesa_signed_to_signed(src32, bits);
1761 else
1762 return _mesa_unsigned_to_unsigned(src32, bits);
1763 }
1764 }
1765
1766 static int32_t
1767 convert_integer(int32_t src, int bits, bool dst_is_signed, bool src_is_signed)
1768 {
1769 if (src_is_signed && dst_is_signed)
1770 return _mesa_signed_to_signed(src, bits);
1771 else if (src_is_signed && !dst_is_signed)
1772 return _mesa_signed_to_unsigned(src, bits);
1773 else if (!src_is_signed && dst_is_signed)
1774 return _mesa_unsigned_to_signed(src, bits);
1775 else
1776 return _mesa_unsigned_to_unsigned(src, bits);
1777 }
1778
1779 void
1780 _mesa_pack_luminance_from_rgba_integer(GLuint n,
1781 GLuint rgba[][4], bool rgba_is_signed,
1782 GLvoid *dstAddr,
1783 GLenum dst_format,
1784 GLenum dst_type)
1785 {
1786 assert(dst_format == GL_LUMINANCE_INTEGER_EXT ||
1787 dst_format == GL_LUMINANCE_ALPHA_INTEGER_EXT);
1788
1789 int i;
1790 int64_t lum64;
1791 int32_t lum32, alpha;
1792 bool dst_is_signed;
1793 int dst_bits;
1794
1795 /* We first compute luminance values as a 64-bit addition of the
1796 * 32-bit R,G,B components, then we clamp the result to the dst type size.
1797 *
1798 * Notice that this operation involves casting the 32-bit R,G,B components
1799 * to 64-bit before the addition. Since rgba is defined as a GLuint array
1800 * we need to be careful when rgba packs signed data and make sure
1801 * that we cast to a 32-bit signed integer values before casting them to
1802 * 64-bit signed integers.
1803 */
1804 dst_is_signed = (dst_type == GL_BYTE || dst_type == GL_SHORT ||
1805 dst_type == GL_INT);
1806
1807 dst_bits = _mesa_sizeof_type(dst_type) * 8;
1808 assert(dst_bits > 0);
1809
1810 switch (dst_format) {
1811 case GL_LUMINANCE_INTEGER_EXT:
1812 for (i = 0; i < n; i++) {
1813 if (!rgba_is_signed) {
1814 lum64 = (uint64_t) rgba[i][RCOMP] +
1815 (uint64_t) rgba[i][GCOMP] +
1816 (uint64_t) rgba[i][BCOMP];
1817 } else {
1818 lum64 = (int64_t) ((int32_t) rgba[i][RCOMP]) +
1819 (int64_t) ((int32_t) rgba[i][GCOMP]) +
1820 (int64_t) ((int32_t) rgba[i][BCOMP]);
1821 }
1822 lum32 = convert_integer_luminance64(lum64, dst_bits,
1823 dst_is_signed, rgba_is_signed);
1824 switch (dst_type) {
1825 case GL_BYTE:
1826 case GL_UNSIGNED_BYTE: {
1827 GLbyte *dst = (GLbyte *) dstAddr;
1828 dst[i] = lum32;
1829 }
1830 break;
1831 case GL_SHORT:
1832 case GL_UNSIGNED_SHORT: {
1833 GLshort *dst = (GLshort *) dstAddr;
1834 dst[i] = lum32;
1835 }
1836 break;
1837 case GL_INT:
1838 case GL_UNSIGNED_INT: {
1839 GLint *dst = (GLint *) dstAddr;
1840 dst[i] = lum32;
1841 }
1842 break;
1843 }
1844 }
1845 return;
1846 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
1847 for (i = 0; i < n; i++) {
1848 if (!rgba_is_signed) {
1849 lum64 = (uint64_t) rgba[i][RCOMP] +
1850 (uint64_t) rgba[i][GCOMP] +
1851 (uint64_t) rgba[i][BCOMP];
1852 } else {
1853 lum64 = (int64_t) ((int32_t) rgba[i][RCOMP]) +
1854 (int64_t) ((int32_t) rgba[i][GCOMP]) +
1855 (int64_t) ((int32_t) rgba[i][BCOMP]);
1856 }
1857 lum32 = convert_integer_luminance64(lum64, dst_bits,
1858 dst_is_signed, rgba_is_signed);
1859 alpha = convert_integer(rgba[i][ACOMP], dst_bits,
1860 dst_is_signed, rgba_is_signed);
1861 switch (dst_type) {
1862 case GL_BYTE:
1863 case GL_UNSIGNED_BYTE: {
1864 GLbyte *dst = (GLbyte *) dstAddr;
1865 dst[2*i] = lum32;
1866 dst[2*i+1] = alpha;
1867 }
1868 case GL_SHORT:
1869 case GL_UNSIGNED_SHORT: {
1870 GLshort *dst = (GLshort *) dstAddr;
1871 dst[i] = lum32;
1872 dst[2*i+1] = alpha;
1873 }
1874 break;
1875 case GL_INT:
1876 case GL_UNSIGNED_INT: {
1877 GLint *dst = (GLint *) dstAddr;
1878 dst[i] = lum32;
1879 dst[2*i+1] = alpha;
1880 }
1881 break;
1882 }
1883 }
1884 return;
1885 }
1886 }
1887
1888 GLfloat *
1889 _mesa_unpack_color_index_to_rgba_float(struct gl_context *ctx, GLuint dims,
1890 const void *src, GLenum srcFormat, GLenum srcType,
1891 int srcWidth, int srcHeight, int srcDepth,
1892 const struct gl_pixelstore_attrib *srcPacking,
1893 GLbitfield transferOps)
1894 {
1895 int count, img;
1896 GLuint *indexes;
1897 GLfloat *rgba, *dstPtr;
1898
1899 count = srcWidth * srcHeight;
1900 indexes = malloc(count * sizeof(GLuint));
1901 if (!indexes) {
1902 _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
1903 return NULL;
1904 }
1905
1906 rgba = malloc(4 * count * srcDepth * sizeof(GLfloat));
1907 if (!rgba) {
1908 free(indexes);
1909 _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
1910 return NULL;
1911 }
1912
1913 /* Convert indexes to RGBA float */
1914 dstPtr = rgba;
1915 for (img = 0; img < srcDepth; img++) {
1916 const GLubyte *srcPtr =
1917 (const GLubyte *) _mesa_image_address(dims, srcPacking, src,
1918 srcWidth, srcHeight,
1919 srcFormat, srcType,
1920 img, 0, 0);
1921
1922 extract_uint_indexes(count, indexes, srcFormat, srcType, srcPtr, srcPacking);
1923
1924 if (transferOps & IMAGE_SHIFT_OFFSET_BIT)
1925 _mesa_shift_and_offset_ci(ctx, count, indexes);
1926
1927 _mesa_map_ci_to_rgba(ctx, count, indexes, (float (*)[4])dstPtr);
1928
1929 /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
1930 * with color indexes.
1931 */
1932 transferOps &= ~(IMAGE_SCALE_BIAS_BIT | IMAGE_MAP_COLOR_BIT);
1933 _mesa_apply_rgba_transfer_ops(ctx, transferOps, count, (float (*)[4])dstPtr);
1934
1935 dstPtr += srcHeight * srcWidth * 4;
1936 }
1937
1938 free(indexes);
1939
1940 return rgba;
1941 }
1942
1943 GLubyte *
1944 _mesa_unpack_color_index_to_rgba_ubyte(struct gl_context *ctx, GLuint dims,
1945 const void *src, GLenum srcFormat, GLenum srcType,
1946 int srcWidth, int srcHeight, int srcDepth,
1947 const struct gl_pixelstore_attrib *srcPacking,
1948 GLbitfield transferOps)
1949 {
1950 GLfloat *rgba;
1951 GLubyte *dst;
1952 int count, i;
1953
1954 transferOps |= IMAGE_CLAMP_BIT;
1955 rgba = _mesa_unpack_color_index_to_rgba_float(ctx, dims,
1956 src, srcFormat, srcType,
1957 srcWidth, srcHeight, srcDepth,
1958 srcPacking, transferOps);
1959
1960 count = srcWidth * srcHeight * srcDepth;
1961 dst = malloc(count * 4 * sizeof(GLubyte));
1962 for (i = 0; i < count; i++) {
1963 CLAMPED_FLOAT_TO_UBYTE(dst[i * 4 + 0], rgba[i * 4 + 0]);
1964 CLAMPED_FLOAT_TO_UBYTE(dst[i * 4 + 1], rgba[i * 4 + 1]);
1965 CLAMPED_FLOAT_TO_UBYTE(dst[i * 4 + 2], rgba[i * 4 + 2]);
1966 CLAMPED_FLOAT_TO_UBYTE(dst[i * 4 + 3], rgba[i * 4 + 3]);
1967 }
1968
1969 free(rgba);
1970
1971 return dst;
1972 }