mesa: Remove _mesa_unpack_color_span_float
[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 /**
236 * For small integer types, return the min and max possible values.
237 * Used for clamping floats to unscaled integer types.
238 * \return GL_TRUE if type is handled, GL_FALSE otherwise.
239 */
240 static GLboolean
241 get_type_min_max(GLenum type, GLfloat *min, GLfloat *max)
242 {
243 switch (type) {
244 case GL_BYTE:
245 *min = -128.0;
246 *max = 127.0;
247 return GL_TRUE;
248 case GL_UNSIGNED_BYTE:
249 *min = 0.0;
250 *max = 255.0;
251 return GL_TRUE;
252 case GL_SHORT:
253 *min = -32768.0;
254 *max = 32767.0;
255 return GL_TRUE;
256 case GL_UNSIGNED_SHORT:
257 *min = 0.0;
258 *max = 65535.0;
259 return GL_TRUE;
260 default:
261 return GL_FALSE;
262 }
263 }
264
265 /* Customization of float packing.
266 */
267 #define SRC_TYPE GLfloat
268
269 #define DST_TYPE GLuint
270 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_UINT(x)
271 #define SRC_CONVERT(x) (GLuint) x
272 #define FN_NAME pack_uint_from_float_rgba
273 #include "pack_tmp.h"
274 #undef DST_TYPE
275 #undef SRC_CONVERT
276 #undef FLOAT_SRC_CONVERT
277 #undef FN_NAME
278
279 #define DST_TYPE GLint
280 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_INT(x)
281 #define SRC_CONVERT(x) (GLint) x
282 #define FN_NAME pack_int_from_float_rgba
283 #include "pack_tmp.h"
284 #undef DST_TYPE
285 #undef SRC_CONVERT
286 #undef FLOAT_SRC_CONVERT
287 #undef FN_NAME
288
289 #define DST_TYPE GLshort
290 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_SHORT_TEX(x)
291 #define SRC_CONVERT(x) (GLshort) x
292 #define FN_NAME pack_short_from_float_rgba
293 #include "pack_tmp.h"
294 #undef DST_TYPE
295 #undef SRC_CONVERT
296 #undef FLOAT_SRC_CONVERT
297 #undef FN_NAME
298
299 #define DST_TYPE GLubyte
300 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_UBYTE(x)
301 #define SRC_CONVERT(x) (GLubyte) x
302 #define FN_NAME pack_ubyte_from_float_rgba
303 #include "pack_tmp.h"
304 #undef DST_TYPE
305 #undef SRC_CONVERT
306 #undef FLOAT_SRC_CONVERT
307 #undef FN_NAME
308
309 #define DST_TYPE GLbyte
310 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_BYTE_TEX(x)
311 #define SRC_CONVERT(x) (GLbyte) x
312 #define FN_NAME pack_byte_from_float_rgba
313 #include "pack_tmp.h"
314 #undef DST_TYPE
315 #undef SRC_CONVERT
316 #undef FLOAT_SRC_CONVERT
317 #undef FN_NAME
318
319 #define DST_TYPE GLfloat
320 #define FLOAT_SRC_CONVERT(x) x
321 #define SRC_CONVERT(x) x
322 #define FN_NAME pack_float_from_float_rgba
323 #include "pack_tmp.h"
324 #undef DST_TYPE
325 #undef SRC_CONVERT
326 #undef FLOAT_SRC_CONVERT
327 #undef FN_NAME
328
329 #define DST_TYPE GLhalfARB
330 #define FLOAT_SRC_CONVERT(x) _mesa_float_to_half(x)
331 #define FN_NAME pack_half_float_from_float_rgba
332 #include "pack_tmp.h"
333 #undef DST_TYPE
334 #undef SRC_CONVERT
335 #undef FLOAT_SRC_CONVERT
336 #undef FN_NAME
337
338 #undef SRC_TYPE
339
340 /**
341 * Used to pack an array [][4] of RGBA float colors as specified
342 * by the dstFormat, dstType and dstPacking. Used by glReadPixels.
343 * Historically, the RGBA values were in [0,1] and rescaled to fit
344 * into GLubytes, etc. But with new integer formats, the RGBA values
345 * may have any value and we don't always rescale when converting to
346 * integers.
347 *
348 * Note: the rgba values will be modified by this function when any pixel
349 * transfer ops are enabled.
350 */
351 void
352 _mesa_pack_rgba_span_float(struct gl_context *ctx, GLuint n, GLfloat rgba[][4],
353 GLenum dstFormat, GLenum dstType,
354 GLvoid *dstAddr,
355 const struct gl_pixelstore_attrib *dstPacking,
356 GLbitfield transferOps)
357 {
358 GLfloat *luminance;
359 const GLint comps = _mesa_components_in_format(dstFormat);
360 const GLboolean intDstFormat = _mesa_is_enum_format_integer(dstFormat);
361 GLuint i;
362 uint32_t dstMesaFormat;
363
364 if (dstFormat == GL_LUMINANCE ||
365 dstFormat == GL_LUMINANCE_ALPHA ||
366 dstFormat == GL_LUMINANCE_INTEGER_EXT ||
367 dstFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT) {
368 luminance = malloc(n * sizeof(GLfloat));
369 if (!luminance) {
370 _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel packing");
371 return;
372 }
373 }
374 else {
375 luminance = NULL;
376 }
377
378 /* EXT_texture_integer specifies no transfer ops on integer
379 * types in the resolved issues section. Just set them to 0
380 * for integer surfaces.
381 */
382 if (intDstFormat)
383 transferOps = 0;
384
385 if (transferOps) {
386 _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
387 }
388
389 /*
390 * Component clamping (besides clamping to [0,1] in
391 * _mesa_apply_rgba_transfer_ops()).
392 */
393 if (intDstFormat) {
394 /* clamping to dest type's min/max values */
395 GLfloat min, max;
396 if (get_type_min_max(dstType, &min, &max)) {
397 for (i = 0; i < n; i++) {
398 rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], min, max);
399 rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], min, max);
400 rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], min, max);
401 rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], min, max);
402 }
403 }
404 }
405 else if (dstFormat == GL_LUMINANCE || dstFormat == GL_LUMINANCE_ALPHA) {
406 /* compute luminance values */
407 if (transferOps & IMAGE_CLAMP_BIT) {
408 for (i = 0; i < n; i++) {
409 GLfloat sum = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
410 luminance[i] = CLAMP(sum, 0.0F, 1.0F);
411 }
412 }
413 else {
414 for (i = 0; i < n; i++) {
415 luminance[i] = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
416 }
417 }
418 }
419
420 /*
421 * Pack/store the pixels. Ugh! Lots of cases!!!
422 */
423 switch (dstType) {
424 case GL_UNSIGNED_BYTE:
425 pack_ubyte_from_float_rgba(ctx, dstAddr, dstFormat, rgba, luminance, n);
426 break;
427 case GL_BYTE:
428 pack_byte_from_float_rgba(ctx, dstAddr, dstFormat, rgba, luminance, n);
429 break;
430 case GL_UNSIGNED_SHORT:
431 {
432 GLushort *dst = (GLushort *) dstAddr;
433 switch (dstFormat) {
434 case GL_RED:
435 for (i=0;i<n;i++)
436 CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][RCOMP]);
437 break;
438 case GL_GREEN:
439 for (i=0;i<n;i++)
440 CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][GCOMP]);
441 break;
442 case GL_BLUE:
443 for (i=0;i<n;i++)
444 CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][BCOMP]);
445 break;
446 case GL_ALPHA:
447 for (i=0;i<n;i++)
448 CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][ACOMP]);
449 break;
450 case GL_LUMINANCE:
451 for (i=0;i<n;i++)
452 UNCLAMPED_FLOAT_TO_USHORT(dst[i], luminance[i]);
453 break;
454 case GL_LUMINANCE_ALPHA:
455 for (i=0;i<n;i++) {
456 UNCLAMPED_FLOAT_TO_USHORT(dst[i*2+0], luminance[i]);
457 CLAMPED_FLOAT_TO_USHORT(dst[i*2+1], rgba[i][ACOMP]);
458 }
459 break;
460 case GL_RG:
461 for (i=0;i<n;i++) {
462 CLAMPED_FLOAT_TO_USHORT(dst[i*2+0], rgba[i][RCOMP]);
463 CLAMPED_FLOAT_TO_USHORT(dst[i*2+1], rgba[i][GCOMP]);
464 }
465 break;
466 case GL_RGB:
467 for (i=0;i<n;i++) {
468 CLAMPED_FLOAT_TO_USHORT(dst[i*3+0], rgba[i][RCOMP]);
469 CLAMPED_FLOAT_TO_USHORT(dst[i*3+1], rgba[i][GCOMP]);
470 CLAMPED_FLOAT_TO_USHORT(dst[i*3+2], rgba[i][BCOMP]);
471 }
472 break;
473 case GL_RGBA:
474 for (i=0;i<n;i++) {
475 CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][RCOMP]);
476 CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][GCOMP]);
477 CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][BCOMP]);
478 CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][ACOMP]);
479 }
480 break;
481 case GL_BGR:
482 for (i=0;i<n;i++) {
483 CLAMPED_FLOAT_TO_USHORT(dst[i*3+0], rgba[i][BCOMP]);
484 CLAMPED_FLOAT_TO_USHORT(dst[i*3+1], rgba[i][GCOMP]);
485 CLAMPED_FLOAT_TO_USHORT(dst[i*3+2], rgba[i][RCOMP]);
486 }
487 break;
488 case GL_BGRA:
489 for (i=0;i<n;i++) {
490 CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][BCOMP]);
491 CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][GCOMP]);
492 CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][RCOMP]);
493 CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][ACOMP]);
494 }
495 break;
496 case GL_ABGR_EXT:
497 for (i=0;i<n;i++) {
498 CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][ACOMP]);
499 CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][BCOMP]);
500 CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][GCOMP]);
501 CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][RCOMP]);
502 }
503 break;
504 case GL_RED_INTEGER_EXT:
505 for (i=0;i<n;i++) {
506 dst[i] = (GLushort) rgba[i][RCOMP];
507 }
508 break;
509 case GL_GREEN_INTEGER_EXT:
510 for (i=0;i<n;i++) {
511 dst[i] = (GLushort) rgba[i][GCOMP];
512 }
513 break;
514 case GL_BLUE_INTEGER_EXT:
515 for (i=0;i<n;i++) {
516 dst[i] = (GLushort) rgba[i][BCOMP];
517 }
518 break;
519 case GL_ALPHA_INTEGER_EXT:
520 for (i=0;i<n;i++) {
521 dst[i] = (GLushort) rgba[i][ACOMP];
522 }
523 break;
524 case GL_RG_INTEGER:
525 for (i=0;i<n;i++) {
526 dst[i*2+0] = (GLushort) rgba[i][RCOMP];
527 dst[i*2+1] = (GLushort) rgba[i][GCOMP];
528 }
529 break;
530 case GL_RGB_INTEGER_EXT:
531 for (i=0;i<n;i++) {
532 dst[i*3+0] = (GLushort) rgba[i][RCOMP];
533 dst[i*3+1] = (GLushort) rgba[i][GCOMP];
534 dst[i*3+2] = (GLushort) rgba[i][BCOMP];
535 }
536 break;
537 case GL_RGBA_INTEGER_EXT:
538 for (i=0;i<n;i++) {
539 dst[i*4+0] = (GLushort) rgba[i][RCOMP];
540 dst[i*4+1] = (GLushort) rgba[i][GCOMP];
541 dst[i*4+2] = (GLushort) rgba[i][BCOMP];
542 dst[i*4+3] = (GLushort) rgba[i][ACOMP];
543 }
544 break;
545 case GL_BGR_INTEGER_EXT:
546 for (i=0;i<n;i++) {
547 dst[i*3+0] = (GLushort) rgba[i][BCOMP];
548 dst[i*3+1] = (GLushort) rgba[i][GCOMP];
549 dst[i*3+2] = (GLushort) rgba[i][RCOMP];
550 }
551 break;
552 case GL_BGRA_INTEGER_EXT:
553 for (i=0;i<n;i++) {
554 dst[i*4+0] = (GLushort) rgba[i][BCOMP];
555 dst[i*4+1] = (GLushort) rgba[i][GCOMP];
556 dst[i*4+2] = (GLushort) rgba[i][RCOMP];
557 dst[i*4+3] = (GLushort) rgba[i][ACOMP];
558 }
559 break;
560 case GL_LUMINANCE_INTEGER_EXT:
561 for (i=0;i<n;i++) {
562 dst[i*2+0] = (GLushort) (rgba[i][RCOMP] +
563 rgba[i][GCOMP] +
564 rgba[i][BCOMP]);
565 dst[i*2+1] = (GLushort) rgba[i][ACOMP];
566 }
567 break;
568 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
569 for (i=0;i<n;i++) {
570 dst[i] = (GLushort) (rgba[i][RCOMP] +
571 rgba[i][GCOMP] +
572 rgba[i][BCOMP]);
573 }
574 break;
575 default:
576 _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
577 }
578 }
579 break;
580 case GL_SHORT:
581 pack_short_from_float_rgba(ctx, dstAddr, dstFormat, rgba, luminance, n);
582 break;
583 case GL_UNSIGNED_INT:
584 pack_uint_from_float_rgba(ctx, dstAddr, dstFormat, rgba, luminance, n);
585 break;
586 case GL_INT:
587 pack_int_from_float_rgba(ctx, dstAddr, dstFormat, rgba, luminance, n);
588 break;
589 case GL_FLOAT:
590 /* No conversion necessary. */
591 pack_float_from_float_rgba(ctx, dstAddr, dstFormat, rgba, luminance, n);
592 break;
593 case GL_HALF_FLOAT_ARB:
594 pack_half_float_from_float_rgba(ctx, dstAddr, dstFormat, rgba, luminance, n);
595 break;
596 case GL_UNSIGNED_BYTE_3_3_2:
597 case GL_UNSIGNED_BYTE_2_3_3_REV:
598 case GL_UNSIGNED_SHORT_5_6_5:
599 case GL_UNSIGNED_SHORT_5_6_5_REV:
600 case GL_UNSIGNED_SHORT_4_4_4_4:
601 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
602 case GL_UNSIGNED_SHORT_5_5_5_1:
603 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
604 case GL_UNSIGNED_INT_8_8_8_8:
605 case GL_UNSIGNED_INT_8_8_8_8_REV:
606 case GL_UNSIGNED_INT_10_10_10_2:
607 case GL_UNSIGNED_INT_2_10_10_10_REV:
608 case GL_UNSIGNED_INT_5_9_9_9_REV:
609 case GL_UNSIGNED_INT_10F_11F_11F_REV:
610 dstMesaFormat = _mesa_format_from_format_and_type(dstFormat, dstType);
611 if (!(dstMesaFormat & MESA_ARRAY_FORMAT_BIT)) {
612 _mesa_pack_float_rgba_row(dstMesaFormat, n, (void *)rgba[0], (void *)dstAddr);
613 break;
614 } else {
615 /* Fall through */
616 }
617 default:
618 _mesa_problem(ctx, "bad type in _mesa_pack_rgba_span_float");
619 free(luminance);
620 return;
621 }
622
623 if (dstPacking->SwapBytes) {
624 GLint swapSize = _mesa_sizeof_packed_type(dstType);
625 if (swapSize == 2) {
626 _mesa_swap2((GLushort *) dstAddr, n * comps);
627 }
628 else if (swapSize == 4) {
629 _mesa_swap4((GLuint *) dstAddr, n * comps);
630 }
631 }
632
633 free(luminance);
634 }
635
636
637
638 #define SWAP2BYTE(VALUE) \
639 { \
640 GLubyte *bytes = (GLubyte *) &(VALUE); \
641 GLubyte tmp = bytes[0]; \
642 bytes[0] = bytes[1]; \
643 bytes[1] = tmp; \
644 }
645
646 #define SWAP4BYTE(VALUE) \
647 { \
648 GLubyte *bytes = (GLubyte *) &(VALUE); \
649 GLubyte tmp = bytes[0]; \
650 bytes[0] = bytes[3]; \
651 bytes[3] = tmp; \
652 tmp = bytes[1]; \
653 bytes[1] = bytes[2]; \
654 bytes[2] = tmp; \
655 }
656
657
658 static void
659 extract_uint_indexes(GLuint n, GLuint indexes[],
660 GLenum srcFormat, GLenum srcType, const GLvoid *src,
661 const struct gl_pixelstore_attrib *unpack )
662 {
663 ASSERT(srcFormat == GL_COLOR_INDEX || srcFormat == GL_STENCIL_INDEX);
664
665 ASSERT(srcType == GL_BITMAP ||
666 srcType == GL_UNSIGNED_BYTE ||
667 srcType == GL_BYTE ||
668 srcType == GL_UNSIGNED_SHORT ||
669 srcType == GL_SHORT ||
670 srcType == GL_UNSIGNED_INT ||
671 srcType == GL_INT ||
672 srcType == GL_UNSIGNED_INT_24_8_EXT ||
673 srcType == GL_HALF_FLOAT_ARB ||
674 srcType == GL_FLOAT ||
675 srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
676
677 switch (srcType) {
678 case GL_BITMAP:
679 {
680 GLubyte *ubsrc = (GLubyte *) src;
681 if (unpack->LsbFirst) {
682 GLubyte mask = 1 << (unpack->SkipPixels & 0x7);
683 GLuint i;
684 for (i = 0; i < n; i++) {
685 indexes[i] = (*ubsrc & mask) ? 1 : 0;
686 if (mask == 128) {
687 mask = 1;
688 ubsrc++;
689 }
690 else {
691 mask = mask << 1;
692 }
693 }
694 }
695 else {
696 GLubyte mask = 128 >> (unpack->SkipPixels & 0x7);
697 GLuint i;
698 for (i = 0; i < n; i++) {
699 indexes[i] = (*ubsrc & mask) ? 1 : 0;
700 if (mask == 1) {
701 mask = 128;
702 ubsrc++;
703 }
704 else {
705 mask = mask >> 1;
706 }
707 }
708 }
709 }
710 break;
711 case GL_UNSIGNED_BYTE:
712 {
713 GLuint i;
714 const GLubyte *s = (const GLubyte *) src;
715 for (i = 0; i < n; i++)
716 indexes[i] = s[i];
717 }
718 break;
719 case GL_BYTE:
720 {
721 GLuint i;
722 const GLbyte *s = (const GLbyte *) src;
723 for (i = 0; i < n; i++)
724 indexes[i] = s[i];
725 }
726 break;
727 case GL_UNSIGNED_SHORT:
728 {
729 GLuint i;
730 const GLushort *s = (const GLushort *) src;
731 if (unpack->SwapBytes) {
732 for (i = 0; i < n; i++) {
733 GLushort value = s[i];
734 SWAP2BYTE(value);
735 indexes[i] = value;
736 }
737 }
738 else {
739 for (i = 0; i < n; i++)
740 indexes[i] = s[i];
741 }
742 }
743 break;
744 case GL_SHORT:
745 {
746 GLuint i;
747 const GLshort *s = (const GLshort *) src;
748 if (unpack->SwapBytes) {
749 for (i = 0; i < n; i++) {
750 GLshort value = s[i];
751 SWAP2BYTE(value);
752 indexes[i] = value;
753 }
754 }
755 else {
756 for (i = 0; i < n; i++)
757 indexes[i] = s[i];
758 }
759 }
760 break;
761 case GL_UNSIGNED_INT:
762 {
763 GLuint i;
764 const GLuint *s = (const GLuint *) src;
765 if (unpack->SwapBytes) {
766 for (i = 0; i < n; i++) {
767 GLuint value = s[i];
768 SWAP4BYTE(value);
769 indexes[i] = value;
770 }
771 }
772 else {
773 for (i = 0; i < n; i++)
774 indexes[i] = s[i];
775 }
776 }
777 break;
778 case GL_INT:
779 {
780 GLuint i;
781 const GLint *s = (const GLint *) src;
782 if (unpack->SwapBytes) {
783 for (i = 0; i < n; i++) {
784 GLint value = s[i];
785 SWAP4BYTE(value);
786 indexes[i] = value;
787 }
788 }
789 else {
790 for (i = 0; i < n; i++)
791 indexes[i] = s[i];
792 }
793 }
794 break;
795 case GL_FLOAT:
796 {
797 GLuint i;
798 const GLfloat *s = (const GLfloat *) src;
799 if (unpack->SwapBytes) {
800 for (i = 0; i < n; i++) {
801 GLfloat value = s[i];
802 SWAP4BYTE(value);
803 indexes[i] = (GLuint) value;
804 }
805 }
806 else {
807 for (i = 0; i < n; i++)
808 indexes[i] = (GLuint) s[i];
809 }
810 }
811 break;
812 case GL_HALF_FLOAT_ARB:
813 {
814 GLuint i;
815 const GLhalfARB *s = (const GLhalfARB *) src;
816 if (unpack->SwapBytes) {
817 for (i = 0; i < n; i++) {
818 GLhalfARB value = s[i];
819 SWAP2BYTE(value);
820 indexes[i] = (GLuint) _mesa_half_to_float(value);
821 }
822 }
823 else {
824 for (i = 0; i < n; i++)
825 indexes[i] = (GLuint) _mesa_half_to_float(s[i]);
826 }
827 }
828 break;
829 case GL_UNSIGNED_INT_24_8_EXT:
830 {
831 GLuint i;
832 const GLuint *s = (const GLuint *) src;
833 if (unpack->SwapBytes) {
834 for (i = 0; i < n; i++) {
835 GLuint value = s[i];
836 SWAP4BYTE(value);
837 indexes[i] = value & 0xff; /* lower 8 bits */
838 }
839 }
840 else {
841 for (i = 0; i < n; i++)
842 indexes[i] = s[i] & 0xff; /* lower 8 bits */
843 }
844 }
845 break;
846 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
847 {
848 GLuint i;
849 const GLuint *s = (const GLuint *) src;
850 if (unpack->SwapBytes) {
851 for (i = 0; i < n; i++) {
852 GLuint value = s[i*2+1];
853 SWAP4BYTE(value);
854 indexes[i] = value & 0xff; /* lower 8 bits */
855 }
856 }
857 else {
858 for (i = 0; i < n; i++)
859 indexes[i] = s[i*2+1] & 0xff; /* lower 8 bits */
860 }
861 }
862 break;
863
864 default:
865 _mesa_problem(NULL, "bad srcType in extract_uint_indexes");
866 return;
867 }
868 }
869
870
871 static inline GLuint
872 clamp_float_to_uint(GLfloat f)
873 {
874 return f < 0.0F ? 0 : F_TO_I(f);
875 }
876
877
878 static inline GLuint
879 clamp_half_to_uint(GLhalfARB h)
880 {
881 GLfloat f = _mesa_half_to_float(h);
882 return f < 0.0F ? 0 : F_TO_I(f);
883 }
884
885
886 /*
887 * Unpack a row of color index data from a client buffer according to
888 * the pixel unpacking parameters.
889 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
890 *
891 * Args: ctx - the context
892 * n - number of pixels
893 * dstType - destination data type
894 * dest - destination array
895 * srcType - source pixel type
896 * source - source data pointer
897 * srcPacking - pixel unpacking parameters
898 * transferOps - the pixel transfer operations to apply
899 */
900 void
901 _mesa_unpack_index_span( struct gl_context *ctx, GLuint n,
902 GLenum dstType, GLvoid *dest,
903 GLenum srcType, const GLvoid *source,
904 const struct gl_pixelstore_attrib *srcPacking,
905 GLbitfield transferOps )
906 {
907 ASSERT(srcType == GL_BITMAP ||
908 srcType == GL_UNSIGNED_BYTE ||
909 srcType == GL_BYTE ||
910 srcType == GL_UNSIGNED_SHORT ||
911 srcType == GL_SHORT ||
912 srcType == GL_UNSIGNED_INT ||
913 srcType == GL_INT ||
914 srcType == GL_HALF_FLOAT_ARB ||
915 srcType == GL_FLOAT);
916
917 ASSERT(dstType == GL_UNSIGNED_BYTE ||
918 dstType == GL_UNSIGNED_SHORT ||
919 dstType == GL_UNSIGNED_INT);
920
921
922 transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
923
924 /*
925 * Try simple cases first
926 */
927 if (transferOps == 0 && srcType == GL_UNSIGNED_BYTE
928 && dstType == GL_UNSIGNED_BYTE) {
929 memcpy(dest, source, n * sizeof(GLubyte));
930 }
931 else if (transferOps == 0 && srcType == GL_UNSIGNED_INT
932 && dstType == GL_UNSIGNED_INT && !srcPacking->SwapBytes) {
933 memcpy(dest, source, n * sizeof(GLuint));
934 }
935 else {
936 /*
937 * general solution
938 */
939 GLuint *indexes = malloc(n * sizeof(GLuint));
940
941 if (!indexes) {
942 _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
943 return;
944 }
945
946 extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
947 srcPacking);
948
949 if (transferOps)
950 _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
951
952 /* convert to dest type */
953 switch (dstType) {
954 case GL_UNSIGNED_BYTE:
955 {
956 GLubyte *dst = (GLubyte *) dest;
957 GLuint i;
958 for (i = 0; i < n; i++) {
959 dst[i] = (GLubyte) (indexes[i] & 0xff);
960 }
961 }
962 break;
963 case GL_UNSIGNED_SHORT:
964 {
965 GLuint *dst = (GLuint *) dest;
966 GLuint i;
967 for (i = 0; i < n; i++) {
968 dst[i] = (GLushort) (indexes[i] & 0xffff);
969 }
970 }
971 break;
972 case GL_UNSIGNED_INT:
973 memcpy(dest, indexes, n * sizeof(GLuint));
974 break;
975 default:
976 _mesa_problem(ctx, "bad dstType in _mesa_unpack_index_span");
977 }
978
979 free(indexes);
980 }
981 }
982
983
984 void
985 _mesa_pack_index_span( struct gl_context *ctx, GLuint n,
986 GLenum dstType, GLvoid *dest, const GLuint *source,
987 const struct gl_pixelstore_attrib *dstPacking,
988 GLbitfield transferOps )
989 {
990 GLuint *indexes = malloc(n * sizeof(GLuint));
991
992 if (!indexes) {
993 _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel packing");
994 return;
995 }
996
997 transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
998
999 if (transferOps & (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT)) {
1000 /* make a copy of input */
1001 memcpy(indexes, source, n * sizeof(GLuint));
1002 _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
1003 source = indexes;
1004 }
1005
1006 switch (dstType) {
1007 case GL_UNSIGNED_BYTE:
1008 {
1009 GLubyte *dst = (GLubyte *) dest;
1010 GLuint i;
1011 for (i = 0; i < n; i++) {
1012 *dst++ = (GLubyte) source[i];
1013 }
1014 }
1015 break;
1016 case GL_BYTE:
1017 {
1018 GLbyte *dst = (GLbyte *) dest;
1019 GLuint i;
1020 for (i = 0; i < n; i++) {
1021 dst[i] = (GLbyte) source[i];
1022 }
1023 }
1024 break;
1025 case GL_UNSIGNED_SHORT:
1026 {
1027 GLushort *dst = (GLushort *) dest;
1028 GLuint i;
1029 for (i = 0; i < n; i++) {
1030 dst[i] = (GLushort) source[i];
1031 }
1032 if (dstPacking->SwapBytes) {
1033 _mesa_swap2( (GLushort *) dst, n );
1034 }
1035 }
1036 break;
1037 case GL_SHORT:
1038 {
1039 GLshort *dst = (GLshort *) dest;
1040 GLuint i;
1041 for (i = 0; i < n; i++) {
1042 dst[i] = (GLshort) source[i];
1043 }
1044 if (dstPacking->SwapBytes) {
1045 _mesa_swap2( (GLushort *) dst, n );
1046 }
1047 }
1048 break;
1049 case GL_UNSIGNED_INT:
1050 {
1051 GLuint *dst = (GLuint *) dest;
1052 GLuint i;
1053 for (i = 0; i < n; i++) {
1054 dst[i] = (GLuint) source[i];
1055 }
1056 if (dstPacking->SwapBytes) {
1057 _mesa_swap4( (GLuint *) dst, n );
1058 }
1059 }
1060 break;
1061 case GL_INT:
1062 {
1063 GLint *dst = (GLint *) dest;
1064 GLuint i;
1065 for (i = 0; i < n; i++) {
1066 dst[i] = (GLint) source[i];
1067 }
1068 if (dstPacking->SwapBytes) {
1069 _mesa_swap4( (GLuint *) dst, n );
1070 }
1071 }
1072 break;
1073 case GL_FLOAT:
1074 {
1075 GLfloat *dst = (GLfloat *) dest;
1076 GLuint i;
1077 for (i = 0; i < n; i++) {
1078 dst[i] = (GLfloat) source[i];
1079 }
1080 if (dstPacking->SwapBytes) {
1081 _mesa_swap4( (GLuint *) dst, n );
1082 }
1083 }
1084 break;
1085 case GL_HALF_FLOAT_ARB:
1086 {
1087 GLhalfARB *dst = (GLhalfARB *) dest;
1088 GLuint i;
1089 for (i = 0; i < n; i++) {
1090 dst[i] = _mesa_float_to_half((GLfloat) source[i]);
1091 }
1092 if (dstPacking->SwapBytes) {
1093 _mesa_swap2( (GLushort *) dst, n );
1094 }
1095 }
1096 break;
1097 default:
1098 _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
1099 }
1100
1101 free(indexes);
1102 }
1103
1104
1105 /*
1106 * Unpack a row of stencil data from a client buffer according to
1107 * the pixel unpacking parameters.
1108 * This is (or will be) used by glDrawPixels
1109 *
1110 * Args: ctx - the context
1111 * n - number of pixels
1112 * dstType - destination data type
1113 * dest - destination array
1114 * srcType - source pixel type
1115 * source - source data pointer
1116 * srcPacking - pixel unpacking parameters
1117 * transferOps - apply offset/bias/lookup ops?
1118 */
1119 void
1120 _mesa_unpack_stencil_span( struct gl_context *ctx, GLuint n,
1121 GLenum dstType, GLvoid *dest,
1122 GLenum srcType, const GLvoid *source,
1123 const struct gl_pixelstore_attrib *srcPacking,
1124 GLbitfield transferOps )
1125 {
1126 ASSERT(srcType == GL_BITMAP ||
1127 srcType == GL_UNSIGNED_BYTE ||
1128 srcType == GL_BYTE ||
1129 srcType == GL_UNSIGNED_SHORT ||
1130 srcType == GL_SHORT ||
1131 srcType == GL_UNSIGNED_INT ||
1132 srcType == GL_INT ||
1133 srcType == GL_UNSIGNED_INT_24_8_EXT ||
1134 srcType == GL_HALF_FLOAT_ARB ||
1135 srcType == GL_FLOAT ||
1136 srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
1137
1138 ASSERT(dstType == GL_UNSIGNED_BYTE ||
1139 dstType == GL_UNSIGNED_SHORT ||
1140 dstType == GL_UNSIGNED_INT ||
1141 dstType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
1142
1143 /* only shift and offset apply to stencil */
1144 transferOps &= IMAGE_SHIFT_OFFSET_BIT;
1145
1146 /*
1147 * Try simple cases first
1148 */
1149 if (transferOps == 0 &&
1150 !ctx->Pixel.MapStencilFlag &&
1151 srcType == GL_UNSIGNED_BYTE &&
1152 dstType == GL_UNSIGNED_BYTE) {
1153 memcpy(dest, source, n * sizeof(GLubyte));
1154 }
1155 else if (transferOps == 0 &&
1156 !ctx->Pixel.MapStencilFlag &&
1157 srcType == GL_UNSIGNED_INT &&
1158 dstType == GL_UNSIGNED_INT &&
1159 !srcPacking->SwapBytes) {
1160 memcpy(dest, source, n * sizeof(GLuint));
1161 }
1162 else {
1163 /*
1164 * general solution
1165 */
1166 GLuint *indexes = malloc(n * sizeof(GLuint));
1167
1168 if (!indexes) {
1169 _mesa_error(ctx, GL_OUT_OF_MEMORY, "stencil unpacking");
1170 return;
1171 }
1172
1173 extract_uint_indexes(n, indexes, GL_STENCIL_INDEX, srcType, source,
1174 srcPacking);
1175
1176 if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
1177 /* shift and offset indexes */
1178 _mesa_shift_and_offset_ci(ctx, n, indexes);
1179 }
1180
1181 if (ctx->Pixel.MapStencilFlag) {
1182 /* Apply stencil lookup table */
1183 const GLuint mask = ctx->PixelMaps.StoS.Size - 1;
1184 GLuint i;
1185 for (i = 0; i < n; i++) {
1186 indexes[i] = (GLuint)ctx->PixelMaps.StoS.Map[ indexes[i] & mask ];
1187 }
1188 }
1189
1190 /* convert to dest type */
1191 switch (dstType) {
1192 case GL_UNSIGNED_BYTE:
1193 {
1194 GLubyte *dst = (GLubyte *) dest;
1195 GLuint i;
1196 for (i = 0; i < n; i++) {
1197 dst[i] = (GLubyte) (indexes[i] & 0xff);
1198 }
1199 }
1200 break;
1201 case GL_UNSIGNED_SHORT:
1202 {
1203 GLuint *dst = (GLuint *) dest;
1204 GLuint i;
1205 for (i = 0; i < n; i++) {
1206 dst[i] = (GLushort) (indexes[i] & 0xffff);
1207 }
1208 }
1209 break;
1210 case GL_UNSIGNED_INT:
1211 memcpy(dest, indexes, n * sizeof(GLuint));
1212 break;
1213 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
1214 {
1215 GLuint *dst = (GLuint *) dest;
1216 GLuint i;
1217 for (i = 0; i < n; i++) {
1218 dst[i*2+1] = indexes[i] & 0xff; /* lower 8 bits */
1219 }
1220 }
1221 break;
1222 default:
1223 _mesa_problem(ctx, "bad dstType in _mesa_unpack_stencil_span");
1224 }
1225
1226 free(indexes);
1227 }
1228 }
1229
1230
1231 void
1232 _mesa_pack_stencil_span( struct gl_context *ctx, GLuint n,
1233 GLenum dstType, GLvoid *dest, const GLubyte *source,
1234 const struct gl_pixelstore_attrib *dstPacking )
1235 {
1236 GLubyte *stencil = malloc(n * sizeof(GLubyte));
1237
1238 if (!stencil) {
1239 _mesa_error(ctx, GL_OUT_OF_MEMORY, "stencil packing");
1240 return;
1241 }
1242
1243 if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset ||
1244 ctx->Pixel.MapStencilFlag) {
1245 /* make a copy of input */
1246 memcpy(stencil, source, n * sizeof(GLubyte));
1247 _mesa_apply_stencil_transfer_ops(ctx, n, stencil);
1248 source = stencil;
1249 }
1250
1251 switch (dstType) {
1252 case GL_UNSIGNED_BYTE:
1253 memcpy(dest, source, n);
1254 break;
1255 case GL_BYTE:
1256 {
1257 GLbyte *dst = (GLbyte *) dest;
1258 GLuint i;
1259 for (i=0;i<n;i++) {
1260 dst[i] = (GLbyte) (source[i] & 0x7f);
1261 }
1262 }
1263 break;
1264 case GL_UNSIGNED_SHORT:
1265 {
1266 GLushort *dst = (GLushort *) dest;
1267 GLuint i;
1268 for (i=0;i<n;i++) {
1269 dst[i] = (GLushort) source[i];
1270 }
1271 if (dstPacking->SwapBytes) {
1272 _mesa_swap2( (GLushort *) dst, n );
1273 }
1274 }
1275 break;
1276 case GL_SHORT:
1277 {
1278 GLshort *dst = (GLshort *) dest;
1279 GLuint i;
1280 for (i=0;i<n;i++) {
1281 dst[i] = (GLshort) source[i];
1282 }
1283 if (dstPacking->SwapBytes) {
1284 _mesa_swap2( (GLushort *) dst, n );
1285 }
1286 }
1287 break;
1288 case GL_UNSIGNED_INT:
1289 {
1290 GLuint *dst = (GLuint *) dest;
1291 GLuint i;
1292 for (i=0;i<n;i++) {
1293 dst[i] = (GLuint) source[i];
1294 }
1295 if (dstPacking->SwapBytes) {
1296 _mesa_swap4( (GLuint *) dst, n );
1297 }
1298 }
1299 break;
1300 case GL_INT:
1301 {
1302 GLint *dst = (GLint *) dest;
1303 GLuint i;
1304 for (i=0;i<n;i++) {
1305 dst[i] = (GLint) source[i];
1306 }
1307 if (dstPacking->SwapBytes) {
1308 _mesa_swap4( (GLuint *) dst, n );
1309 }
1310 }
1311 break;
1312 case GL_FLOAT:
1313 {
1314 GLfloat *dst = (GLfloat *) dest;
1315 GLuint i;
1316 for (i=0;i<n;i++) {
1317 dst[i] = (GLfloat) source[i];
1318 }
1319 if (dstPacking->SwapBytes) {
1320 _mesa_swap4( (GLuint *) dst, n );
1321 }
1322 }
1323 break;
1324 case GL_HALF_FLOAT_ARB:
1325 {
1326 GLhalfARB *dst = (GLhalfARB *) dest;
1327 GLuint i;
1328 for (i=0;i<n;i++) {
1329 dst[i] = _mesa_float_to_half( (float) source[i] );
1330 }
1331 if (dstPacking->SwapBytes) {
1332 _mesa_swap2( (GLushort *) dst, n );
1333 }
1334 }
1335 break;
1336 case GL_BITMAP:
1337 if (dstPacking->LsbFirst) {
1338 GLubyte *dst = (GLubyte *) dest;
1339 GLint shift = 0;
1340 GLuint i;
1341 for (i = 0; i < n; i++) {
1342 if (shift == 0)
1343 *dst = 0;
1344 *dst |= ((source[i] != 0) << shift);
1345 shift++;
1346 if (shift == 8) {
1347 shift = 0;
1348 dst++;
1349 }
1350 }
1351 }
1352 else {
1353 GLubyte *dst = (GLubyte *) dest;
1354 GLint shift = 7;
1355 GLuint i;
1356 for (i = 0; i < n; i++) {
1357 if (shift == 7)
1358 *dst = 0;
1359 *dst |= ((source[i] != 0) << shift);
1360 shift--;
1361 if (shift < 0) {
1362 shift = 7;
1363 dst++;
1364 }
1365 }
1366 }
1367 break;
1368 default:
1369 _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
1370 }
1371
1372 free(stencil);
1373 }
1374
1375 #define DEPTH_VALUES(GLTYPE, GLTYPE2FLOAT) \
1376 do { \
1377 GLuint i; \
1378 const GLTYPE *src = (const GLTYPE *)source; \
1379 for (i = 0; i < n; i++) { \
1380 GLTYPE value = src[i]; \
1381 if (srcPacking->SwapBytes) { \
1382 if (sizeof(GLTYPE) == 2) { \
1383 SWAP2BYTE(value); \
1384 } else if (sizeof(GLTYPE) == 4) { \
1385 SWAP4BYTE(value); \
1386 } \
1387 } \
1388 depthValues[i] = GLTYPE2FLOAT(value); \
1389 } \
1390 } while (0)
1391
1392
1393 /**
1394 * Unpack a row of depth/z values from memory, returning GLushort, GLuint
1395 * or GLfloat values.
1396 * The glPixelTransfer (scale/bias) params will be applied.
1397 *
1398 * \param dstType one of GL_UNSIGNED_SHORT, GL_UNSIGNED_INT, GL_FLOAT
1399 * \param depthMax max value for returned GLushort or GLuint values
1400 * (ignored for GLfloat).
1401 */
1402 void
1403 _mesa_unpack_depth_span( struct gl_context *ctx, GLuint n,
1404 GLenum dstType, GLvoid *dest, GLuint depthMax,
1405 GLenum srcType, const GLvoid *source,
1406 const struct gl_pixelstore_attrib *srcPacking )
1407 {
1408 GLfloat *depthTemp = NULL, *depthValues;
1409 GLboolean needClamp = GL_FALSE;
1410
1411 /* Look for special cases first.
1412 * Not only are these faster, they're less prone to numeric conversion
1413 * problems. Otherwise, converting from an int type to a float then
1414 * back to an int type can introduce errors that will show up as
1415 * artifacts in things like depth peeling which uses glCopyTexImage.
1416 */
1417 if (ctx->Pixel.DepthScale == 1.0 && ctx->Pixel.DepthBias == 0.0) {
1418 if (srcType == GL_UNSIGNED_INT && dstType == GL_UNSIGNED_SHORT) {
1419 const GLuint *src = (const GLuint *) source;
1420 GLushort *dst = (GLushort *) dest;
1421 GLuint i;
1422 for (i = 0; i < n; i++) {
1423 dst[i] = src[i] >> 16;
1424 }
1425 return;
1426 }
1427 if (srcType == GL_UNSIGNED_SHORT
1428 && dstType == GL_UNSIGNED_INT
1429 && depthMax == 0xffffffff) {
1430 const GLushort *src = (const GLushort *) source;
1431 GLuint *dst = (GLuint *) dest;
1432 GLuint i;
1433 for (i = 0; i < n; i++) {
1434 dst[i] = src[i] | (src[i] << 16);
1435 }
1436 return;
1437 }
1438 if (srcType == GL_UNSIGNED_INT_24_8
1439 && dstType == GL_UNSIGNED_INT
1440 && depthMax == 0xffffff) {
1441 const GLuint *src = (const GLuint *) source;
1442 GLuint *dst = (GLuint *) dest;
1443 GLuint i;
1444 for (i = 0; i < n; i++) {
1445 dst[i] = src[i] >> 8;
1446 }
1447 return;
1448 }
1449 /* XXX may want to add additional cases here someday */
1450 }
1451
1452 /* general case path follows */
1453
1454 if (dstType == GL_FLOAT) {
1455 depthValues = (GLfloat *) dest;
1456 }
1457 else {
1458 depthTemp = malloc(n * sizeof(GLfloat));
1459 if (!depthTemp) {
1460 _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
1461 return;
1462 }
1463
1464 depthValues = depthTemp;
1465 }
1466
1467 /* Convert incoming values to GLfloat. Some conversions will require
1468 * clamping, below.
1469 */
1470 switch (srcType) {
1471 case GL_BYTE:
1472 DEPTH_VALUES(GLbyte, BYTE_TO_FLOATZ);
1473 needClamp = GL_TRUE;
1474 break;
1475 case GL_UNSIGNED_BYTE:
1476 DEPTH_VALUES(GLubyte, UBYTE_TO_FLOAT);
1477 break;
1478 case GL_SHORT:
1479 DEPTH_VALUES(GLshort, SHORT_TO_FLOATZ);
1480 needClamp = GL_TRUE;
1481 break;
1482 case GL_UNSIGNED_SHORT:
1483 DEPTH_VALUES(GLushort, USHORT_TO_FLOAT);
1484 break;
1485 case GL_INT:
1486 DEPTH_VALUES(GLint, INT_TO_FLOAT);
1487 needClamp = GL_TRUE;
1488 break;
1489 case GL_UNSIGNED_INT:
1490 DEPTH_VALUES(GLuint, UINT_TO_FLOAT);
1491 break;
1492 case GL_UNSIGNED_INT_24_8_EXT: /* GL_EXT_packed_depth_stencil */
1493 if (dstType == GL_UNSIGNED_INT_24_8_EXT &&
1494 depthMax == 0xffffff &&
1495 ctx->Pixel.DepthScale == 1.0 &&
1496 ctx->Pixel.DepthBias == 0.0) {
1497 const GLuint *src = (const GLuint *) source;
1498 GLuint *zValues = (GLuint *) dest;
1499 GLuint i;
1500 for (i = 0; i < n; i++) {
1501 GLuint value = src[i];
1502 if (srcPacking->SwapBytes) {
1503 SWAP4BYTE(value);
1504 }
1505 zValues[i] = value & 0xffffff00;
1506 }
1507 free(depthTemp);
1508 return;
1509 }
1510 else {
1511 const GLuint *src = (const GLuint *) source;
1512 const GLfloat scale = 1.0f / 0xffffff;
1513 GLuint i;
1514 for (i = 0; i < n; i++) {
1515 GLuint value = src[i];
1516 if (srcPacking->SwapBytes) {
1517 SWAP4BYTE(value);
1518 }
1519 depthValues[i] = (value >> 8) * scale;
1520 }
1521 }
1522 break;
1523 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
1524 {
1525 GLuint i;
1526 const GLfloat *src = (const GLfloat *)source;
1527 for (i = 0; i < n; i++) {
1528 GLfloat value = src[i * 2];
1529 if (srcPacking->SwapBytes) {
1530 SWAP4BYTE(value);
1531 }
1532 depthValues[i] = value;
1533 }
1534 needClamp = GL_TRUE;
1535 }
1536 break;
1537 case GL_FLOAT:
1538 DEPTH_VALUES(GLfloat, 1*);
1539 needClamp = GL_TRUE;
1540 break;
1541 case GL_HALF_FLOAT_ARB:
1542 {
1543 GLuint i;
1544 const GLhalfARB *src = (const GLhalfARB *) source;
1545 for (i = 0; i < n; i++) {
1546 GLhalfARB value = src[i];
1547 if (srcPacking->SwapBytes) {
1548 SWAP2BYTE(value);
1549 }
1550 depthValues[i] = _mesa_half_to_float(value);
1551 }
1552 needClamp = GL_TRUE;
1553 }
1554 break;
1555 default:
1556 _mesa_problem(NULL, "bad type in _mesa_unpack_depth_span()");
1557 free(depthTemp);
1558 return;
1559 }
1560
1561 /* apply depth scale and bias */
1562 {
1563 const GLfloat scale = ctx->Pixel.DepthScale;
1564 const GLfloat bias = ctx->Pixel.DepthBias;
1565 if (scale != 1.0 || bias != 0.0) {
1566 GLuint i;
1567 for (i = 0; i < n; i++) {
1568 depthValues[i] = depthValues[i] * scale + bias;
1569 }
1570 needClamp = GL_TRUE;
1571 }
1572 }
1573
1574 /* clamp to [0, 1] */
1575 if (needClamp) {
1576 GLuint i;
1577 for (i = 0; i < n; i++) {
1578 depthValues[i] = (GLfloat)CLAMP(depthValues[i], 0.0, 1.0);
1579 }
1580 }
1581
1582 /*
1583 * Convert values to dstType
1584 */
1585 if (dstType == GL_UNSIGNED_INT) {
1586 GLuint *zValues = (GLuint *) dest;
1587 GLuint i;
1588 if (depthMax <= 0xffffff) {
1589 /* no overflow worries */
1590 for (i = 0; i < n; i++) {
1591 zValues[i] = (GLuint) (depthValues[i] * (GLfloat) depthMax);
1592 }
1593 }
1594 else {
1595 /* need to use double precision to prevent overflow problems */
1596 for (i = 0; i < n; i++) {
1597 GLdouble z = depthValues[i] * (GLdouble) depthMax;
1598 if (z >= (GLdouble) 0xffffffff)
1599 zValues[i] = 0xffffffff;
1600 else
1601 zValues[i] = (GLuint) z;
1602 }
1603 }
1604 }
1605 else if (dstType == GL_UNSIGNED_SHORT) {
1606 GLushort *zValues = (GLushort *) dest;
1607 GLuint i;
1608 ASSERT(depthMax <= 0xffff);
1609 for (i = 0; i < n; i++) {
1610 zValues[i] = (GLushort) (depthValues[i] * (GLfloat) depthMax);
1611 }
1612 }
1613 else if (dstType == GL_FLOAT) {
1614 /* Nothing to do. depthValues is pointing to dest. */
1615 }
1616 else if (dstType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV) {
1617 GLfloat *zValues = (GLfloat*) dest;
1618 GLuint i;
1619 for (i = 0; i < n; i++) {
1620 zValues[i*2] = depthValues[i];
1621 }
1622 }
1623 else {
1624 ASSERT(0);
1625 }
1626
1627 free(depthTemp);
1628 }
1629
1630
1631 /*
1632 * Pack an array of depth values. The values are floats in [0,1].
1633 */
1634 void
1635 _mesa_pack_depth_span( struct gl_context *ctx, GLuint n, GLvoid *dest,
1636 GLenum dstType, const GLfloat *depthSpan,
1637 const struct gl_pixelstore_attrib *dstPacking )
1638 {
1639 GLfloat *depthCopy = malloc(n * sizeof(GLfloat));
1640 if (!depthCopy) {
1641 _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel packing");
1642 return;
1643 }
1644
1645 if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
1646 memcpy(depthCopy, depthSpan, n * sizeof(GLfloat));
1647 _mesa_scale_and_bias_depth(ctx, n, depthCopy);
1648 depthSpan = depthCopy;
1649 }
1650
1651 switch (dstType) {
1652 case GL_UNSIGNED_BYTE:
1653 {
1654 GLubyte *dst = (GLubyte *) dest;
1655 GLuint i;
1656 for (i = 0; i < n; i++) {
1657 dst[i] = FLOAT_TO_UBYTE( depthSpan[i] );
1658 }
1659 }
1660 break;
1661 case GL_BYTE:
1662 {
1663 GLbyte *dst = (GLbyte *) dest;
1664 GLuint i;
1665 for (i = 0; i < n; i++) {
1666 dst[i] = FLOAT_TO_BYTE( depthSpan[i] );
1667 }
1668 }
1669 break;
1670 case GL_UNSIGNED_SHORT:
1671 {
1672 GLushort *dst = (GLushort *) dest;
1673 GLuint i;
1674 for (i = 0; i < n; i++) {
1675 CLAMPED_FLOAT_TO_USHORT(dst[i], depthSpan[i]);
1676 }
1677 if (dstPacking->SwapBytes) {
1678 _mesa_swap2( (GLushort *) dst, n );
1679 }
1680 }
1681 break;
1682 case GL_SHORT:
1683 {
1684 GLshort *dst = (GLshort *) dest;
1685 GLuint i;
1686 for (i = 0; i < n; i++) {
1687 dst[i] = FLOAT_TO_SHORT( depthSpan[i] );
1688 }
1689 if (dstPacking->SwapBytes) {
1690 _mesa_swap2( (GLushort *) dst, n );
1691 }
1692 }
1693 break;
1694 case GL_UNSIGNED_INT:
1695 {
1696 GLuint *dst = (GLuint *) dest;
1697 GLuint i;
1698 for (i = 0; i < n; i++) {
1699 dst[i] = FLOAT_TO_UINT( depthSpan[i] );
1700 }
1701 if (dstPacking->SwapBytes) {
1702 _mesa_swap4( (GLuint *) dst, n );
1703 }
1704 }
1705 break;
1706 case GL_INT:
1707 {
1708 GLint *dst = (GLint *) dest;
1709 GLuint i;
1710 for (i = 0; i < n; i++) {
1711 dst[i] = FLOAT_TO_INT( depthSpan[i] );
1712 }
1713 if (dstPacking->SwapBytes) {
1714 _mesa_swap4( (GLuint *) dst, n );
1715 }
1716 }
1717 break;
1718 case GL_FLOAT:
1719 {
1720 GLfloat *dst = (GLfloat *) dest;
1721 GLuint i;
1722 for (i = 0; i < n; i++) {
1723 dst[i] = depthSpan[i];
1724 }
1725 if (dstPacking->SwapBytes) {
1726 _mesa_swap4( (GLuint *) dst, n );
1727 }
1728 }
1729 break;
1730 case GL_HALF_FLOAT_ARB:
1731 {
1732 GLhalfARB *dst = (GLhalfARB *) dest;
1733 GLuint i;
1734 for (i = 0; i < n; i++) {
1735 dst[i] = _mesa_float_to_half(depthSpan[i]);
1736 }
1737 if (dstPacking->SwapBytes) {
1738 _mesa_swap2( (GLushort *) dst, n );
1739 }
1740 }
1741 break;
1742 default:
1743 _mesa_problem(ctx, "bad type in _mesa_pack_depth_span");
1744 }
1745
1746 free(depthCopy);
1747 }
1748
1749
1750
1751 /**
1752 * Pack depth and stencil values as GL_DEPTH_STENCIL (GL_UNSIGNED_INT_24_8 etc)
1753 */
1754 void
1755 _mesa_pack_depth_stencil_span(struct gl_context *ctx,GLuint n,
1756 GLenum dstType, GLuint *dest,
1757 const GLfloat *depthVals,
1758 const GLubyte *stencilVals,
1759 const struct gl_pixelstore_attrib *dstPacking)
1760 {
1761 GLfloat *depthCopy = malloc(n * sizeof(GLfloat));
1762 GLubyte *stencilCopy = malloc(n * sizeof(GLubyte));
1763 GLuint i;
1764
1765 if (!depthCopy || !stencilCopy) {
1766 _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel packing");
1767 free(depthCopy);
1768 free(stencilCopy);
1769 return;
1770 }
1771
1772 if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
1773 memcpy(depthCopy, depthVals, n * sizeof(GLfloat));
1774 _mesa_scale_and_bias_depth(ctx, n, depthCopy);
1775 depthVals = depthCopy;
1776 }
1777
1778 if (ctx->Pixel.IndexShift ||
1779 ctx->Pixel.IndexOffset ||
1780 ctx->Pixel.MapStencilFlag) {
1781 memcpy(stencilCopy, stencilVals, n * sizeof(GLubyte));
1782 _mesa_apply_stencil_transfer_ops(ctx, n, stencilCopy);
1783 stencilVals = stencilCopy;
1784 }
1785
1786 switch (dstType) {
1787 case GL_UNSIGNED_INT_24_8:
1788 for (i = 0; i < n; i++) {
1789 GLuint z = (GLuint) (depthVals[i] * 0xffffff);
1790 dest[i] = (z << 8) | (stencilVals[i] & 0xff);
1791 }
1792 break;
1793 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
1794 for (i = 0; i < n; i++) {
1795 ((GLfloat*)dest)[i*2] = depthVals[i];
1796 dest[i*2+1] = stencilVals[i] & 0xff;
1797 }
1798 break;
1799 }
1800
1801 if (dstPacking->SwapBytes) {
1802 _mesa_swap4(dest, n);
1803 }
1804
1805 free(depthCopy);
1806 free(stencilCopy);
1807 }
1808
1809
1810
1811 /**
1812 * Unpack image data. Apply byte swapping, byte flipping (bitmap).
1813 * Return all image data in a contiguous block. This is used when we
1814 * compile glDrawPixels, glTexImage, etc into a display list. We
1815 * need a copy of the data in a standard format.
1816 */
1817 void *
1818 _mesa_unpack_image( GLuint dimensions,
1819 GLsizei width, GLsizei height, GLsizei depth,
1820 GLenum format, GLenum type, const GLvoid *pixels,
1821 const struct gl_pixelstore_attrib *unpack )
1822 {
1823 GLint bytesPerRow, compsPerRow;
1824 GLboolean flipBytes, swap2, swap4;
1825
1826 if (!pixels)
1827 return NULL; /* not necessarily an error */
1828
1829 if (width <= 0 || height <= 0 || depth <= 0)
1830 return NULL; /* generate error later */
1831
1832 if (type == GL_BITMAP) {
1833 bytesPerRow = (width + 7) >> 3;
1834 flipBytes = unpack->LsbFirst;
1835 swap2 = swap4 = GL_FALSE;
1836 compsPerRow = 0;
1837 }
1838 else {
1839 const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
1840 GLint components = _mesa_components_in_format(format);
1841 GLint bytesPerComp;
1842
1843 if (_mesa_type_is_packed(type))
1844 components = 1;
1845
1846 if (bytesPerPixel <= 0 || components <= 0)
1847 return NULL; /* bad format or type. generate error later */
1848 bytesPerRow = bytesPerPixel * width;
1849 bytesPerComp = bytesPerPixel / components;
1850 flipBytes = GL_FALSE;
1851 swap2 = (bytesPerComp == 2) && unpack->SwapBytes;
1852 swap4 = (bytesPerComp == 4) && unpack->SwapBytes;
1853 compsPerRow = components * width;
1854 assert(compsPerRow >= width);
1855 }
1856
1857 {
1858 GLubyte *destBuffer
1859 = malloc(bytesPerRow * height * depth);
1860 GLubyte *dst;
1861 GLint img, row;
1862 if (!destBuffer)
1863 return NULL; /* generate GL_OUT_OF_MEMORY later */
1864
1865 dst = destBuffer;
1866 for (img = 0; img < depth; img++) {
1867 for (row = 0; row < height; row++) {
1868 const GLvoid *src = _mesa_image_address(dimensions, unpack, pixels,
1869 width, height, format, type, img, row, 0);
1870
1871 if ((type == GL_BITMAP) && (unpack->SkipPixels & 0x7)) {
1872 GLint i;
1873 flipBytes = GL_FALSE;
1874 if (unpack->LsbFirst) {
1875 GLubyte srcMask = 1 << (unpack->SkipPixels & 0x7);
1876 GLubyte dstMask = 128;
1877 const GLubyte *s = src;
1878 GLubyte *d = dst;
1879 *d = 0;
1880 for (i = 0; i < width; i++) {
1881 if (*s & srcMask) {
1882 *d |= dstMask;
1883 }
1884 if (srcMask == 128) {
1885 srcMask = 1;
1886 s++;
1887 }
1888 else {
1889 srcMask = srcMask << 1;
1890 }
1891 if (dstMask == 1) {
1892 dstMask = 128;
1893 d++;
1894 *d = 0;
1895 }
1896 else {
1897 dstMask = dstMask >> 1;
1898 }
1899 }
1900 }
1901 else {
1902 GLubyte srcMask = 128 >> (unpack->SkipPixels & 0x7);
1903 GLubyte dstMask = 128;
1904 const GLubyte *s = src;
1905 GLubyte *d = dst;
1906 *d = 0;
1907 for (i = 0; i < width; i++) {
1908 if (*s & srcMask) {
1909 *d |= dstMask;
1910 }
1911 if (srcMask == 1) {
1912 srcMask = 128;
1913 s++;
1914 }
1915 else {
1916 srcMask = srcMask >> 1;
1917 }
1918 if (dstMask == 1) {
1919 dstMask = 128;
1920 d++;
1921 *d = 0;
1922 }
1923 else {
1924 dstMask = dstMask >> 1;
1925 }
1926 }
1927 }
1928 }
1929 else {
1930 memcpy(dst, src, bytesPerRow);
1931 }
1932
1933 /* byte flipping/swapping */
1934 if (flipBytes) {
1935 flip_bytes((GLubyte *) dst, bytesPerRow);
1936 }
1937 else if (swap2) {
1938 _mesa_swap2((GLushort*) dst, compsPerRow);
1939 }
1940 else if (swap4) {
1941 _mesa_swap4((GLuint*) dst, compsPerRow);
1942 }
1943 dst += bytesPerRow;
1944 }
1945 }
1946 return destBuffer;
1947 }
1948 }
1949
1950
1951
1952 /**
1953 * If we unpack colors from a luminance surface, we'll get pixel colors
1954 * such as (l, l, l, a).
1955 * When we call _mesa_pack_rgba_span_float(format=GL_LUMINANCE), that
1956 * function will compute L=R+G+B before packing. The net effect is we'll
1957 * accidentally store luminance values = 3*l.
1958 * This function compensates for that by converting (aka rebasing) (l,l,l,a)
1959 * to be (l,0,0,a).
1960 * It's a similar story for other formats such as LUMINANCE_ALPHA, ALPHA
1961 * and INTENSITY.
1962 *
1963 * Finally, we also need to do this when the actual surface format does
1964 * not match the logical surface format. For example, suppose the user
1965 * requests a GL_LUMINANCE texture but the driver stores it as RGBA.
1966 * Again, we'll get pixel values like (l,l,l,a).
1967 */
1968 void
1969 _mesa_rebase_rgba_float(GLuint n, GLfloat rgba[][4], GLenum baseFormat)
1970 {
1971 GLuint i;
1972
1973 switch (baseFormat) {
1974 case GL_ALPHA:
1975 for (i = 0; i < n; i++) {
1976 rgba[i][RCOMP] = 0.0F;
1977 rgba[i][GCOMP] = 0.0F;
1978 rgba[i][BCOMP] = 0.0F;
1979 }
1980 break;
1981 case GL_INTENSITY:
1982 /* fall-through */
1983 case GL_LUMINANCE:
1984 for (i = 0; i < n; i++) {
1985 rgba[i][GCOMP] = 0.0F;
1986 rgba[i][BCOMP] = 0.0F;
1987 rgba[i][ACOMP] = 1.0F;
1988 }
1989 break;
1990 case GL_LUMINANCE_ALPHA:
1991 for (i = 0; i < n; i++) {
1992 rgba[i][GCOMP] = 0.0F;
1993 rgba[i][BCOMP] = 0.0F;
1994 }
1995 break;
1996 case GL_RGB:
1997 for (i = 0; i < n; i++) {
1998 rgba[i][ACOMP] = 1.0F;
1999 }
2000 break;
2001 case GL_RG:
2002 for (i = 0; i < n; i++) {
2003 rgba[i][BCOMP] = 0.0F;
2004 rgba[i][ACOMP] = 1.0F;
2005 }
2006 break;
2007 case GL_RED:
2008 for (i = 0; i < n; i++) {
2009 rgba[i][GCOMP] = 0.0F;
2010 rgba[i][BCOMP] = 0.0F;
2011 rgba[i][ACOMP] = 1.0F;
2012 }
2013 break;
2014
2015 default:
2016 /* no-op */
2017 ;
2018 }
2019 }
2020
2021
2022 /**
2023 * As above, but GLuint components.
2024 */
2025 void
2026 _mesa_rebase_rgba_uint(GLuint n, GLuint rgba[][4], GLenum baseFormat)
2027 {
2028 GLuint i;
2029
2030 switch (baseFormat) {
2031 case GL_ALPHA:
2032 for (i = 0; i < n; i++) {
2033 rgba[i][RCOMP] = 0;
2034 rgba[i][GCOMP] = 0;
2035 rgba[i][BCOMP] = 0;
2036 }
2037 break;
2038 case GL_INTENSITY:
2039 /* fall-through */
2040 case GL_LUMINANCE:
2041 for (i = 0; i < n; i++) {
2042 rgba[i][GCOMP] = 0;
2043 rgba[i][BCOMP] = 0;
2044 rgba[i][ACOMP] = 1;
2045 }
2046 break;
2047 case GL_LUMINANCE_ALPHA:
2048 for (i = 0; i < n; i++) {
2049 rgba[i][GCOMP] = 0;
2050 rgba[i][BCOMP] = 0;
2051 }
2052 break;
2053 case GL_RGB:
2054 for (i = 0; i < n; i++) {
2055 rgba[i][ACOMP] = 1;
2056 }
2057 break;
2058 case GL_RG:
2059 for (i = 0; i < n; i++) {
2060 rgba[i][BCOMP] = 0;
2061 rgba[i][ACOMP] = 1;
2062 }
2063 break;
2064 case GL_RED:
2065 for (i = 0; i < n; i++) {
2066 rgba[i][GCOMP] = 0;
2067 rgba[i][BCOMP] = 0;
2068 rgba[i][ACOMP] = 1;
2069 }
2070 default:
2071 /* no-op */
2072 ;
2073 }
2074 }
2075
2076 void
2077 _mesa_pack_luminance_from_rgba_float(GLuint n, GLfloat rgba[][4],
2078 GLvoid *dstAddr, GLenum dst_format,
2079 GLbitfield transferOps)
2080 {
2081 int i;
2082 GLfloat *dst = (GLfloat *) dstAddr;
2083
2084 switch (dst_format) {
2085 case GL_LUMINANCE:
2086 if (transferOps & IMAGE_CLAMP_BIT) {
2087 for (i = 0; i < n; i++) {
2088 GLfloat sum = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
2089 dst[i] = CLAMP(sum, 0.0F, 1.0F);
2090 }
2091 } else {
2092 for (i = 0; i < n; i++) {
2093 dst[i] = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
2094 }
2095 }
2096 return;
2097 case GL_LUMINANCE_ALPHA:
2098 if (transferOps & IMAGE_CLAMP_BIT) {
2099 for (i = 0; i < n; i++) {
2100 GLfloat sum = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
2101 dst[2*i] = CLAMP(sum, 0.0F, 1.0F);
2102 dst[2*i+1] = rgba[i][ACOMP];
2103 }
2104 } else {
2105 for (i = 0; i < n; i++) {
2106 dst[2*i] = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
2107 dst[2*i+1] = rgba[i][ACOMP];
2108 }
2109 }
2110 return;
2111 default:
2112 assert(!"Unsupported format");
2113 }
2114 }
2115
2116 static int32_t
2117 clamp_sint64_to_sint32(int64_t src)
2118 {
2119 return CLAMP(src, INT32_MIN, INT32_MAX);
2120 }
2121
2122 static int32_t
2123 clamp_sint64_to_uint32(int64_t src)
2124 {
2125 return CLAMP(src, 0, UINT32_MAX);
2126 }
2127
2128 static int32_t
2129 clamp_uint64_to_uint32(uint64_t src)
2130 {
2131 return MIN2(src, UINT32_MAX);
2132 }
2133
2134 static int32_t
2135 clamp_uint64_to_sint32(uint64_t src)
2136 {
2137 return MIN2(src, INT32_MAX);
2138 }
2139
2140 static int32_t
2141 convert_integer_luminance64(int64_t src64, int bits,
2142 bool dst_is_signed, bool src_is_signed)
2143 {
2144 int32_t src32;
2145
2146 /* Clamp Luminance value from 64-bit to 32-bit. Consider if we need
2147 * any signed<->unsigned conversion too.
2148 */
2149 if (src_is_signed && dst_is_signed)
2150 src32 = clamp_sint64_to_sint32(src64);
2151 else if (src_is_signed && !dst_is_signed)
2152 src32 = clamp_sint64_to_uint32(src64);
2153 else if (!src_is_signed && dst_is_signed)
2154 src32 = clamp_uint64_to_sint32(src64);
2155 else
2156 src32 = clamp_uint64_to_uint32(src64);
2157
2158 /* If the dst type is < 32-bit, we need an extra clamp */
2159 if (bits == 32) {
2160 return src32;
2161 } else {
2162 if (dst_is_signed)
2163 return _mesa_signed_to_signed(src32, bits);
2164 else
2165 return _mesa_unsigned_to_unsigned(src32, bits);
2166 }
2167 }
2168
2169 static int32_t
2170 convert_integer(int32_t src, int bits, bool dst_is_signed, bool src_is_signed)
2171 {
2172 if (src_is_signed && dst_is_signed)
2173 return _mesa_signed_to_signed(src, bits);
2174 else if (src_is_signed && !dst_is_signed)
2175 return _mesa_signed_to_unsigned(src, bits);
2176 else if (!src_is_signed && dst_is_signed)
2177 return _mesa_unsigned_to_signed(src, bits);
2178 else
2179 return _mesa_unsigned_to_unsigned(src, bits);
2180 }
2181
2182 void
2183 _mesa_pack_luminance_from_rgba_integer(GLuint n,
2184 GLuint rgba[][4], bool rgba_is_signed,
2185 GLvoid *dstAddr,
2186 GLenum dst_format,
2187 GLenum dst_type)
2188 {
2189 assert(dst_format == GL_LUMINANCE_INTEGER_EXT ||
2190 dst_format == GL_LUMINANCE_ALPHA_INTEGER_EXT);
2191
2192 int i;
2193 int64_t lum64;
2194 int32_t lum32, alpha;
2195 bool dst_is_signed;
2196 int dst_bits;
2197
2198 /* We first compute luminance values as a 64-bit addition of the
2199 * 32-bit R,G,B components, then we clamp the result to the dst type size.
2200 *
2201 * Notice that this operation involves casting the 32-bit R,G,B components
2202 * to 64-bit before the addition. Since rgba is defined as a GLuint array
2203 * we need to be careful when rgba packs signed data and make sure
2204 * that we cast to a 32-bit signed integer values before casting them to
2205 * 64-bit signed integers.
2206 */
2207 dst_is_signed = (dst_type == GL_BYTE || dst_type == GL_SHORT ||
2208 dst_type == GL_INT);
2209
2210 dst_bits = _mesa_sizeof_type(dst_type) * 8;
2211 assert(dst_bits > 0);
2212
2213 switch (dst_format) {
2214 case GL_LUMINANCE_INTEGER_EXT:
2215 for (i = 0; i < n; i++) {
2216 if (!rgba_is_signed) {
2217 lum64 = (uint64_t) rgba[i][RCOMP] +
2218 (uint64_t) rgba[i][GCOMP] +
2219 (uint64_t) rgba[i][BCOMP];
2220 } else {
2221 lum64 = (int64_t) ((int32_t) rgba[i][RCOMP]) +
2222 (int64_t) ((int32_t) rgba[i][GCOMP]) +
2223 (int64_t) ((int32_t) rgba[i][BCOMP]);
2224 }
2225 lum32 = convert_integer_luminance64(lum64, dst_bits,
2226 dst_is_signed, rgba_is_signed);
2227 switch (dst_type) {
2228 case GL_BYTE:
2229 case GL_UNSIGNED_BYTE: {
2230 GLbyte *dst = (GLbyte *) dstAddr;
2231 dst[i] = lum32;
2232 }
2233 break;
2234 case GL_SHORT:
2235 case GL_UNSIGNED_SHORT: {
2236 GLshort *dst = (GLshort *) dstAddr;
2237 dst[i] = lum32;
2238 }
2239 break;
2240 case GL_INT:
2241 case GL_UNSIGNED_INT: {
2242 GLint *dst = (GLint *) dstAddr;
2243 dst[i] = lum32;
2244 }
2245 break;
2246 }
2247 }
2248 return;
2249 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
2250 for (i = 0; i < n; i++) {
2251 if (!rgba_is_signed) {
2252 lum64 = (uint64_t) rgba[i][RCOMP] +
2253 (uint64_t) rgba[i][GCOMP] +
2254 (uint64_t) rgba[i][BCOMP];
2255 } else {
2256 lum64 = (int64_t) ((int32_t) rgba[i][RCOMP]) +
2257 (int64_t) ((int32_t) rgba[i][GCOMP]) +
2258 (int64_t) ((int32_t) rgba[i][BCOMP]);
2259 }
2260 lum32 = convert_integer_luminance64(lum64, dst_bits,
2261 dst_is_signed, rgba_is_signed);
2262 alpha = convert_integer(rgba[i][ACOMP], dst_bits,
2263 dst_is_signed, rgba_is_signed);
2264 switch (dst_type) {
2265 case GL_BYTE:
2266 case GL_UNSIGNED_BYTE: {
2267 GLbyte *dst = (GLbyte *) dstAddr;
2268 dst[2*i] = lum32;
2269 dst[2*i+1] = alpha;
2270 }
2271 case GL_SHORT:
2272 case GL_UNSIGNED_SHORT: {
2273 GLshort *dst = (GLshort *) dstAddr;
2274 dst[i] = lum32;
2275 dst[2*i+1] = alpha;
2276 }
2277 break;
2278 case GL_INT:
2279 case GL_UNSIGNED_INT: {
2280 GLint *dst = (GLint *) dstAddr;
2281 dst[i] = lum32;
2282 dst[2*i+1] = alpha;
2283 }
2284 break;
2285 }
2286 }
2287 return;
2288 }
2289 }
2290
2291 GLfloat *
2292 _mesa_unpack_color_index_to_rgba_float(struct gl_context *ctx, GLuint dims,
2293 const void *src, GLenum srcFormat, GLenum srcType,
2294 int srcWidth, int srcHeight, int srcDepth,
2295 const struct gl_pixelstore_attrib *srcPacking,
2296 GLbitfield transferOps)
2297 {
2298 int count, img;
2299 GLuint *indexes;
2300 GLfloat *rgba, *dstPtr;
2301
2302 count = srcWidth * srcHeight;
2303 indexes = malloc(count * sizeof(GLuint));
2304 if (!indexes) {
2305 _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
2306 return NULL;
2307 }
2308
2309 rgba = malloc(4 * count * srcDepth * sizeof(GLfloat));
2310 if (!rgba) {
2311 free(indexes);
2312 _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
2313 return NULL;
2314 }
2315
2316 /* Convert indexes to RGBA float */
2317 dstPtr = rgba;
2318 for (img = 0; img < srcDepth; img++) {
2319 const GLubyte *srcPtr =
2320 (const GLubyte *) _mesa_image_address(dims, srcPacking, src,
2321 srcWidth, srcHeight,
2322 srcFormat, srcType,
2323 img, 0, 0);
2324
2325 extract_uint_indexes(count, indexes, srcFormat, srcType, srcPtr, srcPacking);
2326
2327 if (transferOps & IMAGE_SHIFT_OFFSET_BIT)
2328 _mesa_shift_and_offset_ci(ctx, count, indexes);
2329
2330 _mesa_map_ci_to_rgba(ctx, count, indexes, (float (*)[4])dstPtr);
2331
2332 /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
2333 * with color indexes.
2334 */
2335 transferOps &= ~(IMAGE_SCALE_BIAS_BIT | IMAGE_MAP_COLOR_BIT);
2336 _mesa_apply_rgba_transfer_ops(ctx, transferOps, count, (float (*)[4])dstPtr);
2337
2338 dstPtr += srcHeight * srcWidth * 4;
2339 }
2340
2341 free(indexes);
2342
2343 return rgba;
2344 }
2345
2346 GLubyte *
2347 _mesa_unpack_color_index_to_rgba_ubyte(struct gl_context *ctx, GLuint dims,
2348 const void *src, GLenum srcFormat, GLenum srcType,
2349 int srcWidth, int srcHeight, int srcDepth,
2350 const struct gl_pixelstore_attrib *srcPacking,
2351 GLbitfield transferOps)
2352 {
2353 GLfloat *rgba;
2354 GLubyte *dst;
2355 int count, i;
2356
2357 transferOps |= IMAGE_CLAMP_BIT;
2358 rgba = _mesa_unpack_color_index_to_rgba_float(ctx, dims,
2359 src, srcFormat, srcType,
2360 srcWidth, srcHeight, srcDepth,
2361 srcPacking, transferOps);
2362
2363 count = srcWidth * srcHeight * srcDepth;
2364 dst = malloc(count * 4 * sizeof(GLubyte));
2365 for (i = 0; i < count; i++) {
2366 CLAMPED_FLOAT_TO_UBYTE(dst[i * 4 + 0], rgba[i * 4 + 0]);
2367 CLAMPED_FLOAT_TO_UBYTE(dst[i * 4 + 1], rgba[i * 4 + 1]);
2368 CLAMPED_FLOAT_TO_UBYTE(dst[i * 4 + 2], rgba[i * 4 + 2]);
2369 CLAMPED_FLOAT_TO_UBYTE(dst[i * 4 + 3], rgba[i * 4 + 3]);
2370 }
2371
2372 free(rgba);
2373
2374 return dst;
2375 }