Merge branch 'master' of ssh://git.freedesktop.org/git/mesa/mesa into pipe-video
[mesa.git] / src / mesa / main / image.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.5
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /**
28 * \file image.c
29 * Image handling.
30 */
31
32
33 #include "glheader.h"
34 #include "colormac.h"
35 #include "image.h"
36 #include "imports.h"
37 #include "macros.h"
38
39
40 /**
41 * NOTE:
42 * Normally, BYTE_TO_FLOAT(0) returns 0.00392 That causes problems when
43 * we later convert the float to a packed integer value (such as for
44 * GL_RGB5_A1) because we'll wind up with a non-zero value.
45 *
46 * We redefine the macros here so zero is handled correctly.
47 */
48 #undef BYTE_TO_FLOAT
49 #define BYTE_TO_FLOAT(B) ((B) == 0 ? 0.0F : ((2.0F * (B) + 1.0F) * (1.0F/255.0F)))
50
51 #undef SHORT_TO_FLOAT
52 #define SHORT_TO_FLOAT(S) ((S) == 0 ? 0.0F : ((2.0F * (S) + 1.0F) * (1.0F/65535.0F)))
53
54
55
56 /** Compute ceiling of integer quotient of A divided by B. */
57 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
58
59
60 /**
61 * \return GL_TRUE if type is packed pixel type, GL_FALSE otherwise.
62 */
63 GLboolean
64 _mesa_type_is_packed(GLenum type)
65 {
66 switch (type) {
67 case GL_UNSIGNED_BYTE_3_3_2:
68 case GL_UNSIGNED_BYTE_2_3_3_REV:
69 case GL_UNSIGNED_SHORT_5_6_5:
70 case GL_UNSIGNED_SHORT_5_6_5_REV:
71 case GL_UNSIGNED_SHORT_4_4_4_4:
72 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
73 case GL_UNSIGNED_SHORT_5_5_5_1:
74 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
75 case GL_UNSIGNED_INT_8_8_8_8:
76 case GL_UNSIGNED_INT_8_8_8_8_REV:
77 case GL_UNSIGNED_INT_10_10_10_2:
78 case GL_UNSIGNED_INT_2_10_10_10_REV:
79 case GL_UNSIGNED_SHORT_8_8_MESA:
80 case GL_UNSIGNED_SHORT_8_8_REV_MESA:
81 case GL_UNSIGNED_INT_24_8_EXT:
82 return GL_TRUE;
83 }
84
85 return GL_FALSE;
86 }
87
88
89
90 /**
91 * Flip the order of the 2 bytes in each word in the given array.
92 *
93 * \param p array.
94 * \param n number of words.
95 */
96 void
97 _mesa_swap2( GLushort *p, GLuint n )
98 {
99 GLuint i;
100 for (i = 0; i < n; i++) {
101 p[i] = (p[i] >> 8) | ((p[i] << 8) & 0xff00);
102 }
103 }
104
105
106
107 /*
108 * Flip the order of the 4 bytes in each word in the given array.
109 */
110 void
111 _mesa_swap4( GLuint *p, GLuint n )
112 {
113 GLuint i, a, b;
114 for (i = 0; i < n; i++) {
115 b = p[i];
116 a = (b >> 24)
117 | ((b >> 8) & 0xff00)
118 | ((b << 8) & 0xff0000)
119 | ((b << 24) & 0xff000000);
120 p[i] = a;
121 }
122 }
123
124
125 /**
126 * Get the size of a GL data type.
127 *
128 * \param type GL data type.
129 *
130 * \return the size, in bytes, of the given data type, 0 if a GL_BITMAP, or -1
131 * if an invalid type enum.
132 */
133 GLint
134 _mesa_sizeof_type( GLenum type )
135 {
136 switch (type) {
137 case GL_BITMAP:
138 return 0;
139 case GL_UNSIGNED_BYTE:
140 return sizeof(GLubyte);
141 case GL_BYTE:
142 return sizeof(GLbyte);
143 case GL_UNSIGNED_SHORT:
144 return sizeof(GLushort);
145 case GL_SHORT:
146 return sizeof(GLshort);
147 case GL_UNSIGNED_INT:
148 return sizeof(GLuint);
149 case GL_INT:
150 return sizeof(GLint);
151 case GL_FLOAT:
152 return sizeof(GLfloat);
153 case GL_DOUBLE:
154 return sizeof(GLdouble);
155 case GL_HALF_FLOAT_ARB:
156 return sizeof(GLhalfARB);
157 default:
158 return -1;
159 }
160 }
161
162
163 /**
164 * Same as _mesa_sizeof_type() but also accepting the packed pixel
165 * format data types.
166 */
167 GLint
168 _mesa_sizeof_packed_type( GLenum type )
169 {
170 switch (type) {
171 case GL_BITMAP:
172 return 0;
173 case GL_UNSIGNED_BYTE:
174 return sizeof(GLubyte);
175 case GL_BYTE:
176 return sizeof(GLbyte);
177 case GL_UNSIGNED_SHORT:
178 return sizeof(GLushort);
179 case GL_SHORT:
180 return sizeof(GLshort);
181 case GL_UNSIGNED_INT:
182 return sizeof(GLuint);
183 case GL_INT:
184 return sizeof(GLint);
185 case GL_HALF_FLOAT_ARB:
186 return sizeof(GLhalfARB);
187 case GL_FLOAT:
188 return sizeof(GLfloat);
189 case GL_UNSIGNED_BYTE_3_3_2:
190 return sizeof(GLubyte);
191 case GL_UNSIGNED_BYTE_2_3_3_REV:
192 return sizeof(GLubyte);
193 case GL_UNSIGNED_SHORT_5_6_5:
194 return sizeof(GLushort);
195 case GL_UNSIGNED_SHORT_5_6_5_REV:
196 return sizeof(GLushort);
197 case GL_UNSIGNED_SHORT_4_4_4_4:
198 return sizeof(GLushort);
199 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
200 return sizeof(GLushort);
201 case GL_UNSIGNED_SHORT_5_5_5_1:
202 return sizeof(GLushort);
203 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
204 return sizeof(GLushort);
205 case GL_UNSIGNED_INT_8_8_8_8:
206 return sizeof(GLuint);
207 case GL_UNSIGNED_INT_8_8_8_8_REV:
208 return sizeof(GLuint);
209 case GL_UNSIGNED_INT_10_10_10_2:
210 return sizeof(GLuint);
211 case GL_UNSIGNED_INT_2_10_10_10_REV:
212 return sizeof(GLuint);
213 case GL_UNSIGNED_SHORT_8_8_MESA:
214 case GL_UNSIGNED_SHORT_8_8_REV_MESA:
215 return sizeof(GLushort);
216 case GL_UNSIGNED_INT_24_8_EXT:
217 return sizeof(GLuint);
218 default:
219 return -1;
220 }
221 }
222
223
224 /**
225 * Get the number of components in a pixel format.
226 *
227 * \param format pixel format.
228 *
229 * \return the number of components in the given format, or -1 if a bad format.
230 */
231 GLint
232 _mesa_components_in_format( GLenum format )
233 {
234 switch (format) {
235 case GL_COLOR_INDEX:
236 case GL_COLOR_INDEX1_EXT:
237 case GL_COLOR_INDEX2_EXT:
238 case GL_COLOR_INDEX4_EXT:
239 case GL_COLOR_INDEX8_EXT:
240 case GL_COLOR_INDEX12_EXT:
241 case GL_COLOR_INDEX16_EXT:
242 case GL_STENCIL_INDEX:
243 case GL_DEPTH_COMPONENT:
244 case GL_RED:
245 case GL_RED_INTEGER_EXT:
246 case GL_GREEN:
247 case GL_GREEN_INTEGER_EXT:
248 case GL_BLUE:
249 case GL_BLUE_INTEGER_EXT:
250 case GL_ALPHA:
251 case GL_ALPHA_INTEGER_EXT:
252 case GL_LUMINANCE:
253 case GL_LUMINANCE_INTEGER_EXT:
254 case GL_INTENSITY:
255 return 1;
256 case GL_LUMINANCE_ALPHA:
257 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
258 case GL_RG:
259 return 2;
260 case GL_RGB:
261 case GL_RGB_INTEGER_EXT:
262 return 3;
263 case GL_RGBA:
264 case GL_RGBA_INTEGER_EXT:
265 return 4;
266 case GL_BGR:
267 return 3;
268 case GL_BGRA:
269 return 4;
270 case GL_ABGR_EXT:
271 return 4;
272 case GL_YCBCR_MESA:
273 return 2;
274 case GL_DEPTH_STENCIL_EXT:
275 return 2;
276 case GL_DUDV_ATI:
277 case GL_DU8DV8_ATI:
278 return 2;
279 default:
280 return -1;
281 }
282 }
283
284
285 /**
286 * Get the bytes per pixel of pixel format type pair.
287 *
288 * \param format pixel format.
289 * \param type pixel type.
290 *
291 * \return bytes per pixel, or -1 if a bad format or type was given.
292 */
293 GLint
294 _mesa_bytes_per_pixel( GLenum format, GLenum type )
295 {
296 GLint comps = _mesa_components_in_format( format );
297 if (comps < 0)
298 return -1;
299
300 switch (type) {
301 case GL_BITMAP:
302 return 0; /* special case */
303 case GL_BYTE:
304 case GL_UNSIGNED_BYTE:
305 return comps * sizeof(GLubyte);
306 case GL_SHORT:
307 case GL_UNSIGNED_SHORT:
308 return comps * sizeof(GLshort);
309 case GL_INT:
310 case GL_UNSIGNED_INT:
311 return comps * sizeof(GLint);
312 case GL_FLOAT:
313 return comps * sizeof(GLfloat);
314 case GL_HALF_FLOAT_ARB:
315 return comps * sizeof(GLhalfARB);
316 case GL_UNSIGNED_BYTE_3_3_2:
317 case GL_UNSIGNED_BYTE_2_3_3_REV:
318 if (format == GL_RGB || format == GL_BGR)
319 return sizeof(GLubyte);
320 else
321 return -1; /* error */
322 case GL_UNSIGNED_SHORT_5_6_5:
323 case GL_UNSIGNED_SHORT_5_6_5_REV:
324 if (format == GL_RGB || format == GL_BGR)
325 return sizeof(GLushort);
326 else
327 return -1; /* error */
328 case GL_UNSIGNED_SHORT_4_4_4_4:
329 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
330 case GL_UNSIGNED_SHORT_5_5_5_1:
331 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
332 if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT)
333 return sizeof(GLushort);
334 else
335 return -1;
336 case GL_UNSIGNED_INT_8_8_8_8:
337 case GL_UNSIGNED_INT_8_8_8_8_REV:
338 case GL_UNSIGNED_INT_10_10_10_2:
339 case GL_UNSIGNED_INT_2_10_10_10_REV:
340 if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT)
341 return sizeof(GLuint);
342 else
343 return -1;
344 case GL_UNSIGNED_SHORT_8_8_MESA:
345 case GL_UNSIGNED_SHORT_8_8_REV_MESA:
346 if (format == GL_YCBCR_MESA)
347 return sizeof(GLushort);
348 else
349 return -1;
350 case GL_UNSIGNED_INT_24_8_EXT:
351 if (format == GL_DEPTH_STENCIL_EXT)
352 return sizeof(GLuint);
353 else
354 return -1;
355 default:
356 return -1;
357 }
358 }
359
360
361 /**
362 * Test for a legal pixel format and type.
363 *
364 * \param format pixel format.
365 * \param type pixel type.
366 *
367 * \return GL_TRUE if the given pixel format and type are legal, or GL_FALSE
368 * otherwise.
369 */
370 GLboolean
371 _mesa_is_legal_format_and_type( struct gl_context *ctx, GLenum format, GLenum type )
372 {
373 switch (format) {
374 case GL_COLOR_INDEX:
375 case GL_STENCIL_INDEX:
376 switch (type) {
377 case GL_BITMAP:
378 case GL_BYTE:
379 case GL_UNSIGNED_BYTE:
380 case GL_SHORT:
381 case GL_UNSIGNED_SHORT:
382 case GL_INT:
383 case GL_UNSIGNED_INT:
384 case GL_FLOAT:
385 return GL_TRUE;
386 case GL_HALF_FLOAT_ARB:
387 return ctx->Extensions.ARB_half_float_pixel;
388 default:
389 return GL_FALSE;
390 }
391 case GL_RED:
392 case GL_GREEN:
393 case GL_BLUE:
394 case GL_ALPHA:
395 #if 0 /* not legal! see table 3.6 of the 1.5 spec */
396 case GL_INTENSITY:
397 #endif
398 case GL_LUMINANCE:
399 case GL_LUMINANCE_ALPHA:
400 case GL_DEPTH_COMPONENT:
401 switch (type) {
402 case GL_BYTE:
403 case GL_UNSIGNED_BYTE:
404 case GL_SHORT:
405 case GL_UNSIGNED_SHORT:
406 case GL_INT:
407 case GL_UNSIGNED_INT:
408 case GL_FLOAT:
409 return GL_TRUE;
410 case GL_HALF_FLOAT_ARB:
411 return ctx->Extensions.ARB_half_float_pixel;
412 default:
413 return GL_FALSE;
414 }
415 case GL_RG:
416 if (!ctx->Extensions.ARB_texture_rg)
417 return GL_FALSE;
418
419 switch (type) {
420 case GL_BYTE:
421 case GL_UNSIGNED_BYTE:
422 case GL_SHORT:
423 case GL_UNSIGNED_SHORT:
424 case GL_INT:
425 case GL_UNSIGNED_INT:
426 case GL_FLOAT:
427 return GL_TRUE;
428 case GL_HALF_FLOAT_ARB:
429 return ctx->Extensions.ARB_half_float_pixel;
430 default:
431 return GL_FALSE;
432 }
433 case GL_RGB:
434 switch (type) {
435 case GL_BYTE:
436 case GL_UNSIGNED_BYTE:
437 case GL_SHORT:
438 case GL_UNSIGNED_SHORT:
439 case GL_INT:
440 case GL_UNSIGNED_INT:
441 case GL_FLOAT:
442 case GL_UNSIGNED_BYTE_3_3_2:
443 case GL_UNSIGNED_BYTE_2_3_3_REV:
444 case GL_UNSIGNED_SHORT_5_6_5:
445 case GL_UNSIGNED_SHORT_5_6_5_REV:
446 return GL_TRUE;
447 case GL_HALF_FLOAT_ARB:
448 return ctx->Extensions.ARB_half_float_pixel;
449 default:
450 return GL_FALSE;
451 }
452 case GL_BGR:
453 switch (type) {
454 /* NOTE: no packed types are supported with BGR. That's
455 * intentional, according to the GL spec.
456 */
457 case GL_BYTE:
458 case GL_UNSIGNED_BYTE:
459 case GL_SHORT:
460 case GL_UNSIGNED_SHORT:
461 case GL_INT:
462 case GL_UNSIGNED_INT:
463 case GL_FLOAT:
464 return GL_TRUE;
465 case GL_HALF_FLOAT_ARB:
466 return ctx->Extensions.ARB_half_float_pixel;
467 default:
468 return GL_FALSE;
469 }
470 case GL_RGBA:
471 case GL_BGRA:
472 case GL_ABGR_EXT:
473 switch (type) {
474 case GL_BYTE:
475 case GL_UNSIGNED_BYTE:
476 case GL_SHORT:
477 case GL_UNSIGNED_SHORT:
478 case GL_INT:
479 case GL_UNSIGNED_INT:
480 case GL_FLOAT:
481 case GL_UNSIGNED_SHORT_4_4_4_4:
482 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
483 case GL_UNSIGNED_SHORT_5_5_5_1:
484 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
485 case GL_UNSIGNED_INT_8_8_8_8:
486 case GL_UNSIGNED_INT_8_8_8_8_REV:
487 case GL_UNSIGNED_INT_10_10_10_2:
488 case GL_UNSIGNED_INT_2_10_10_10_REV:
489 return GL_TRUE;
490 case GL_HALF_FLOAT_ARB:
491 return ctx->Extensions.ARB_half_float_pixel;
492 default:
493 return GL_FALSE;
494 }
495 case GL_YCBCR_MESA:
496 if (type == GL_UNSIGNED_SHORT_8_8_MESA ||
497 type == GL_UNSIGNED_SHORT_8_8_REV_MESA)
498 return GL_TRUE;
499 else
500 return GL_FALSE;
501 case GL_DEPTH_STENCIL_EXT:
502 if (ctx->Extensions.EXT_packed_depth_stencil
503 && type == GL_UNSIGNED_INT_24_8_EXT)
504 return GL_TRUE;
505 else
506 return GL_FALSE;
507 case GL_DUDV_ATI:
508 case GL_DU8DV8_ATI:
509 switch (type) {
510 case GL_BYTE:
511 case GL_UNSIGNED_BYTE:
512 case GL_SHORT:
513 case GL_UNSIGNED_SHORT:
514 case GL_INT:
515 case GL_UNSIGNED_INT:
516 case GL_FLOAT:
517 return GL_TRUE;
518 default:
519 return GL_FALSE;
520 }
521 case GL_RED_INTEGER_EXT:
522 case GL_GREEN_INTEGER_EXT:
523 case GL_BLUE_INTEGER_EXT:
524 case GL_ALPHA_INTEGER_EXT:
525 case GL_RGB_INTEGER_EXT:
526 case GL_RGBA_INTEGER_EXT:
527 case GL_BGR_INTEGER_EXT:
528 case GL_BGRA_INTEGER_EXT:
529 case GL_LUMINANCE_INTEGER_EXT:
530 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
531 switch (type) {
532 case GL_BYTE:
533 case GL_UNSIGNED_BYTE:
534 case GL_SHORT:
535 case GL_UNSIGNED_SHORT:
536 case GL_INT:
537 case GL_UNSIGNED_INT:
538 return ctx->Extensions.EXT_texture_integer;
539 default:
540 return GL_FALSE;
541 }
542
543 default:
544 ; /* fall-through */
545 }
546 return GL_FALSE;
547 }
548
549
550 /**
551 * Test if the given image format is a color/RGBA format (i.e., not color
552 * index, depth, stencil, etc).
553 * \param format the image format value (may by an internal texture format)
554 * \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise.
555 */
556 GLboolean
557 _mesa_is_color_format(GLenum format)
558 {
559 switch (format) {
560 case GL_RED:
561 case GL_GREEN:
562 case GL_BLUE:
563 case GL_ALPHA:
564 case GL_ALPHA4:
565 case GL_ALPHA8:
566 case GL_ALPHA12:
567 case GL_ALPHA16:
568 case 1:
569 case GL_LUMINANCE:
570 case GL_LUMINANCE4:
571 case GL_LUMINANCE8:
572 case GL_LUMINANCE12:
573 case GL_LUMINANCE16:
574 case 2:
575 case GL_LUMINANCE_ALPHA:
576 case GL_LUMINANCE4_ALPHA4:
577 case GL_LUMINANCE6_ALPHA2:
578 case GL_LUMINANCE8_ALPHA8:
579 case GL_LUMINANCE12_ALPHA4:
580 case GL_LUMINANCE12_ALPHA12:
581 case GL_LUMINANCE16_ALPHA16:
582 case GL_INTENSITY:
583 case GL_INTENSITY4:
584 case GL_INTENSITY8:
585 case GL_INTENSITY12:
586 case GL_INTENSITY16:
587 case GL_R8:
588 case GL_R16:
589 case GL_RG:
590 case GL_RG8:
591 case GL_RG16:
592 case 3:
593 case GL_RGB:
594 case GL_BGR:
595 case GL_R3_G3_B2:
596 case GL_RGB4:
597 case GL_RGB5:
598 case GL_RGB8:
599 case GL_RGB10:
600 case GL_RGB12:
601 case GL_RGB16:
602 case 4:
603 case GL_ABGR_EXT:
604 case GL_RGBA:
605 case GL_BGRA:
606 case GL_RGBA2:
607 case GL_RGBA4:
608 case GL_RGB5_A1:
609 case GL_RGBA8:
610 case GL_RGB10_A2:
611 case GL_RGBA12:
612 case GL_RGBA16:
613 /* float texture formats */
614 case GL_ALPHA16F_ARB:
615 case GL_ALPHA32F_ARB:
616 case GL_LUMINANCE16F_ARB:
617 case GL_LUMINANCE32F_ARB:
618 case GL_LUMINANCE_ALPHA16F_ARB:
619 case GL_LUMINANCE_ALPHA32F_ARB:
620 case GL_INTENSITY16F_ARB:
621 case GL_INTENSITY32F_ARB:
622 case GL_R16F:
623 case GL_R32F:
624 case GL_RG16F:
625 case GL_RG32F:
626 case GL_RGB16F_ARB:
627 case GL_RGB32F_ARB:
628 case GL_RGBA16F_ARB:
629 case GL_RGBA32F_ARB:
630 /* compressed formats */
631 case GL_COMPRESSED_ALPHA:
632 case GL_COMPRESSED_LUMINANCE:
633 case GL_COMPRESSED_LUMINANCE_ALPHA:
634 case GL_COMPRESSED_INTENSITY:
635 case GL_COMPRESSED_RED:
636 case GL_COMPRESSED_RG:
637 case GL_COMPRESSED_RGB:
638 case GL_COMPRESSED_RGBA:
639 case GL_RGB_S3TC:
640 case GL_RGB4_S3TC:
641 case GL_RGBA_S3TC:
642 case GL_RGBA4_S3TC:
643 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
644 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
645 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
646 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
647 case GL_COMPRESSED_RGB_FXT1_3DFX:
648 case GL_COMPRESSED_RGBA_FXT1_3DFX:
649 #if FEATURE_EXT_texture_sRGB
650 case GL_SRGB_EXT:
651 case GL_SRGB8_EXT:
652 case GL_SRGB_ALPHA_EXT:
653 case GL_SRGB8_ALPHA8_EXT:
654 case GL_SLUMINANCE_ALPHA_EXT:
655 case GL_SLUMINANCE8_ALPHA8_EXT:
656 case GL_SLUMINANCE_EXT:
657 case GL_SLUMINANCE8_EXT:
658 case GL_COMPRESSED_SRGB_EXT:
659 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
660 case GL_COMPRESSED_SRGB_ALPHA_EXT:
661 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
662 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
663 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
664 case GL_COMPRESSED_SLUMINANCE_EXT:
665 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT:
666 #endif /* FEATURE_EXT_texture_sRGB */
667 case GL_COMPRESSED_RED_RGTC1:
668 case GL_COMPRESSED_SIGNED_RED_RGTC1:
669 case GL_COMPRESSED_RG_RGTC2:
670 case GL_COMPRESSED_SIGNED_RG_RGTC2:
671 /* signed, normalized texture formats */
672 case GL_RGBA_SNORM:
673 case GL_RGBA8_SNORM:
674 /* generic integer formats */
675 case GL_RED_INTEGER_EXT:
676 case GL_GREEN_INTEGER_EXT:
677 case GL_BLUE_INTEGER_EXT:
678 case GL_ALPHA_INTEGER_EXT:
679 case GL_RGB_INTEGER_EXT:
680 case GL_RGBA_INTEGER_EXT:
681 case GL_BGR_INTEGER_EXT:
682 case GL_BGRA_INTEGER_EXT:
683 case GL_LUMINANCE_INTEGER_EXT:
684 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
685 /* sized integer formats */
686 case GL_RGBA32UI_EXT:
687 case GL_RGB32UI_EXT:
688 case GL_ALPHA32UI_EXT:
689 case GL_INTENSITY32UI_EXT:
690 case GL_LUMINANCE32UI_EXT:
691 case GL_LUMINANCE_ALPHA32UI_EXT:
692 case GL_RGBA16UI_EXT:
693 case GL_RGB16UI_EXT:
694 case GL_ALPHA16UI_EXT:
695 case GL_INTENSITY16UI_EXT:
696 case GL_LUMINANCE16UI_EXT:
697 case GL_LUMINANCE_ALPHA16UI_EXT:
698 case GL_RGBA8UI_EXT:
699 case GL_RGB8UI_EXT:
700 case GL_ALPHA8UI_EXT:
701 case GL_INTENSITY8UI_EXT:
702 case GL_LUMINANCE8UI_EXT:
703 case GL_LUMINANCE_ALPHA8UI_EXT:
704 case GL_RGBA32I_EXT:
705 case GL_RGB32I_EXT:
706 case GL_ALPHA32I_EXT:
707 case GL_INTENSITY32I_EXT:
708 case GL_LUMINANCE32I_EXT:
709 case GL_LUMINANCE_ALPHA32I_EXT:
710 case GL_RGBA16I_EXT:
711 case GL_RGB16I_EXT:
712 case GL_ALPHA16I_EXT:
713 case GL_INTENSITY16I_EXT:
714 case GL_LUMINANCE16I_EXT:
715 case GL_LUMINANCE_ALPHA16I_EXT:
716 case GL_RGBA8I_EXT:
717 case GL_RGB8I_EXT:
718 case GL_ALPHA8I_EXT:
719 case GL_INTENSITY8I_EXT:
720 case GL_LUMINANCE8I_EXT:
721 case GL_LUMINANCE_ALPHA8I_EXT:
722 return GL_TRUE;
723 case GL_YCBCR_MESA: /* not considered to be RGB */
724 /* fall-through */
725 default:
726 return GL_FALSE;
727 }
728 }
729
730
731 /**
732 * Test if the given image format is a color index format.
733 */
734 GLboolean
735 _mesa_is_index_format(GLenum format)
736 {
737 switch (format) {
738 case GL_COLOR_INDEX:
739 case GL_COLOR_INDEX1_EXT:
740 case GL_COLOR_INDEX2_EXT:
741 case GL_COLOR_INDEX4_EXT:
742 case GL_COLOR_INDEX8_EXT:
743 case GL_COLOR_INDEX12_EXT:
744 case GL_COLOR_INDEX16_EXT:
745 return GL_TRUE;
746 default:
747 return GL_FALSE;
748 }
749 }
750
751
752 /**
753 * Test if the given image format is a depth component format.
754 */
755 GLboolean
756 _mesa_is_depth_format(GLenum format)
757 {
758 switch (format) {
759 case GL_DEPTH_COMPONENT:
760 case GL_DEPTH_COMPONENT16:
761 case GL_DEPTH_COMPONENT24:
762 case GL_DEPTH_COMPONENT32:
763 return GL_TRUE;
764 default:
765 return GL_FALSE;
766 }
767 }
768
769
770 /**
771 * Test if the given image format is a stencil format.
772 */
773 GLboolean
774 _mesa_is_stencil_format(GLenum format)
775 {
776 switch (format) {
777 case GL_STENCIL_INDEX:
778 case GL_DEPTH_STENCIL:
779 return GL_TRUE;
780 default:
781 return GL_FALSE;
782 }
783 }
784
785
786 /**
787 * Test if the given image format is a YCbCr format.
788 */
789 GLboolean
790 _mesa_is_ycbcr_format(GLenum format)
791 {
792 switch (format) {
793 case GL_YCBCR_MESA:
794 return GL_TRUE;
795 default:
796 return GL_FALSE;
797 }
798 }
799
800
801 /**
802 * Test if the given image format is a depth+stencil format.
803 */
804 GLboolean
805 _mesa_is_depthstencil_format(GLenum format)
806 {
807 switch (format) {
808 case GL_DEPTH24_STENCIL8_EXT:
809 case GL_DEPTH_STENCIL_EXT:
810 return GL_TRUE;
811 default:
812 return GL_FALSE;
813 }
814 }
815
816
817 /**
818 * Test if the given image format is a depth or stencil format.
819 */
820 GLboolean
821 _mesa_is_depth_or_stencil_format(GLenum format)
822 {
823 switch (format) {
824 case GL_DEPTH_COMPONENT:
825 case GL_DEPTH_COMPONENT16:
826 case GL_DEPTH_COMPONENT24:
827 case GL_DEPTH_COMPONENT32:
828 case GL_STENCIL_INDEX:
829 case GL_STENCIL_INDEX1_EXT:
830 case GL_STENCIL_INDEX4_EXT:
831 case GL_STENCIL_INDEX8_EXT:
832 case GL_STENCIL_INDEX16_EXT:
833 case GL_DEPTH_STENCIL_EXT:
834 case GL_DEPTH24_STENCIL8_EXT:
835 return GL_TRUE;
836 default:
837 return GL_FALSE;
838 }
839 }
840
841
842 /**
843 * Test if the given image format is a dudv format.
844 */
845 GLboolean
846 _mesa_is_dudv_format(GLenum format)
847 {
848 switch (format) {
849 case GL_DUDV_ATI:
850 case GL_DU8DV8_ATI:
851 return GL_TRUE;
852 default:
853 return GL_FALSE;
854 }
855 }
856
857
858 /**
859 * Test if the given format is an integer (non-normalized) format.
860 */
861 GLboolean
862 _mesa_is_integer_format(GLenum format)
863 {
864 switch (format) {
865 /* generic integer formats */
866 case GL_RED_INTEGER_EXT:
867 case GL_GREEN_INTEGER_EXT:
868 case GL_BLUE_INTEGER_EXT:
869 case GL_ALPHA_INTEGER_EXT:
870 case GL_RGB_INTEGER_EXT:
871 case GL_RGBA_INTEGER_EXT:
872 case GL_BGR_INTEGER_EXT:
873 case GL_BGRA_INTEGER_EXT:
874 case GL_LUMINANCE_INTEGER_EXT:
875 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
876 /* specific integer formats */
877 case GL_RGBA32UI_EXT:
878 case GL_RGB32UI_EXT:
879 case GL_ALPHA32UI_EXT:
880 case GL_INTENSITY32UI_EXT:
881 case GL_LUMINANCE32UI_EXT:
882 case GL_LUMINANCE_ALPHA32UI_EXT:
883 case GL_RGBA16UI_EXT:
884 case GL_RGB16UI_EXT:
885 case GL_ALPHA16UI_EXT:
886 case GL_INTENSITY16UI_EXT:
887 case GL_LUMINANCE16UI_EXT:
888 case GL_LUMINANCE_ALPHA16UI_EXT:
889 case GL_RGBA8UI_EXT:
890 case GL_RGB8UI_EXT:
891 case GL_ALPHA8UI_EXT:
892 case GL_INTENSITY8UI_EXT:
893 case GL_LUMINANCE8UI_EXT:
894 case GL_LUMINANCE_ALPHA8UI_EXT:
895 case GL_RGBA32I_EXT:
896 case GL_RGB32I_EXT:
897 case GL_ALPHA32I_EXT:
898 case GL_INTENSITY32I_EXT:
899 case GL_LUMINANCE32I_EXT:
900 case GL_LUMINANCE_ALPHA32I_EXT:
901 case GL_RGBA16I_EXT:
902 case GL_RGB16I_EXT:
903 case GL_ALPHA16I_EXT:
904 case GL_INTENSITY16I_EXT:
905 case GL_LUMINANCE16I_EXT:
906 case GL_LUMINANCE_ALPHA16I_EXT:
907 case GL_RGBA8I_EXT:
908 case GL_RGB8I_EXT:
909 case GL_ALPHA8I_EXT:
910 case GL_INTENSITY8I_EXT:
911 case GL_LUMINANCE8I_EXT:
912 case GL_LUMINANCE_ALPHA8I_EXT:
913 return GL_TRUE;
914 default:
915 return GL_FALSE;
916 }
917 }
918
919
920 /**
921 * Test if an image format is a supported compressed format.
922 * \param format the internal format token provided by the user.
923 * \return GL_TRUE if compressed, GL_FALSE if uncompressed
924 */
925 GLboolean
926 _mesa_is_compressed_format(struct gl_context *ctx, GLenum format)
927 {
928 switch (format) {
929 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
930 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
931 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
932 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
933 return ctx->Extensions.EXT_texture_compression_s3tc;
934 case GL_RGB_S3TC:
935 case GL_RGB4_S3TC:
936 case GL_RGBA_S3TC:
937 case GL_RGBA4_S3TC:
938 return ctx->Extensions.S3_s3tc;
939 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
940 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
941 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
942 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
943 return ctx->Extensions.EXT_texture_sRGB
944 && ctx->Extensions.EXT_texture_compression_s3tc;
945 case GL_COMPRESSED_RGB_FXT1_3DFX:
946 case GL_COMPRESSED_RGBA_FXT1_3DFX:
947 return ctx->Extensions.TDFX_texture_compression_FXT1;
948 case GL_COMPRESSED_RED_RGTC1:
949 case GL_COMPRESSED_SIGNED_RED_RGTC1:
950 case GL_COMPRESSED_RG_RGTC2:
951 case GL_COMPRESSED_SIGNED_RG_RGTC2:
952 return ctx->Extensions.ARB_texture_compression_rgtc;
953 default:
954 return GL_FALSE;
955 }
956 }
957
958
959 /**
960 * Return the address of a specific pixel in an image (1D, 2D or 3D).
961 *
962 * Pixel unpacking/packing parameters are observed according to \p packing.
963 *
964 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
965 * \param image starting address of image data
966 * \param width the image width
967 * \param height theimage height
968 * \param format the pixel format
969 * \param type the pixel data type
970 * \param packing the pixelstore attributes
971 * \param img which image in the volume (0 for 1D or 2D images)
972 * \param row row of pixel in the image (0 for 1D images)
973 * \param column column of pixel in the image
974 *
975 * \return address of pixel on success, or NULL on error.
976 *
977 * \sa gl_pixelstore_attrib.
978 */
979 GLvoid *
980 _mesa_image_address( GLuint dimensions,
981 const struct gl_pixelstore_attrib *packing,
982 const GLvoid *image,
983 GLsizei width, GLsizei height,
984 GLenum format, GLenum type,
985 GLint img, GLint row, GLint column )
986 {
987 GLint alignment; /* 1, 2 or 4 */
988 GLint pixels_per_row;
989 GLint rows_per_image;
990 GLint skiprows;
991 GLint skippixels;
992 GLint skipimages; /* for 3-D volume images */
993 GLubyte *pixel_addr;
994
995 ASSERT(dimensions >= 1 && dimensions <= 3);
996
997 alignment = packing->Alignment;
998 if (packing->RowLength > 0) {
999 pixels_per_row = packing->RowLength;
1000 }
1001 else {
1002 pixels_per_row = width;
1003 }
1004 if (packing->ImageHeight > 0) {
1005 rows_per_image = packing->ImageHeight;
1006 }
1007 else {
1008 rows_per_image = height;
1009 }
1010
1011 skippixels = packing->SkipPixels;
1012 /* Note: SKIP_ROWS _is_ used for 1D images */
1013 skiprows = packing->SkipRows;
1014 /* Note: SKIP_IMAGES is only used for 3D images */
1015 skipimages = (dimensions == 3) ? packing->SkipImages : 0;
1016
1017 if (type == GL_BITMAP) {
1018 /* BITMAP data */
1019 GLint comp_per_pixel; /* components per pixel */
1020 GLint bytes_per_comp; /* bytes per component */
1021 GLint bytes_per_row;
1022 GLint bytes_per_image;
1023
1024 /* Compute bytes per component */
1025 bytes_per_comp = _mesa_sizeof_packed_type( type );
1026 if (bytes_per_comp < 0) {
1027 return NULL;
1028 }
1029
1030 /* Compute number of components per pixel */
1031 comp_per_pixel = _mesa_components_in_format( format );
1032 if (comp_per_pixel < 0) {
1033 return NULL;
1034 }
1035
1036 bytes_per_row = alignment
1037 * CEILING( comp_per_pixel*pixels_per_row, 8*alignment );
1038
1039 bytes_per_image = bytes_per_row * rows_per_image;
1040
1041 pixel_addr = (GLubyte *) image
1042 + (skipimages + img) * bytes_per_image
1043 + (skiprows + row) * bytes_per_row
1044 + (skippixels + column) / 8;
1045 }
1046 else {
1047 /* Non-BITMAP data */
1048 GLint bytes_per_pixel, bytes_per_row, remainder, bytes_per_image;
1049 GLint topOfImage;
1050
1051 bytes_per_pixel = _mesa_bytes_per_pixel( format, type );
1052
1053 /* The pixel type and format should have been error checked earlier */
1054 assert(bytes_per_pixel > 0);
1055
1056 bytes_per_row = pixels_per_row * bytes_per_pixel;
1057 remainder = bytes_per_row % alignment;
1058 if (remainder > 0)
1059 bytes_per_row += (alignment - remainder);
1060
1061 ASSERT(bytes_per_row % alignment == 0);
1062
1063 bytes_per_image = bytes_per_row * rows_per_image;
1064
1065 if (packing->Invert) {
1066 /* set pixel_addr to the last row */
1067 topOfImage = bytes_per_row * (height - 1);
1068 bytes_per_row = -bytes_per_row;
1069 }
1070 else {
1071 topOfImage = 0;
1072 }
1073
1074 /* compute final pixel address */
1075 pixel_addr = (GLubyte *) image
1076 + (skipimages + img) * bytes_per_image
1077 + topOfImage
1078 + (skiprows + row) * bytes_per_row
1079 + (skippixels + column) * bytes_per_pixel;
1080 }
1081
1082 return (GLvoid *) pixel_addr;
1083 }
1084
1085
1086 GLvoid *
1087 _mesa_image_address1d( const struct gl_pixelstore_attrib *packing,
1088 const GLvoid *image,
1089 GLsizei width,
1090 GLenum format, GLenum type,
1091 GLint column )
1092 {
1093 return _mesa_image_address(1, packing, image, width, 1,
1094 format, type, 0, 0, column);
1095 }
1096
1097
1098 GLvoid *
1099 _mesa_image_address2d( const struct gl_pixelstore_attrib *packing,
1100 const GLvoid *image,
1101 GLsizei width, GLsizei height,
1102 GLenum format, GLenum type,
1103 GLint row, GLint column )
1104 {
1105 return _mesa_image_address(2, packing, image, width, height,
1106 format, type, 0, row, column);
1107 }
1108
1109
1110 GLvoid *
1111 _mesa_image_address3d( const struct gl_pixelstore_attrib *packing,
1112 const GLvoid *image,
1113 GLsizei width, GLsizei height,
1114 GLenum format, GLenum type,
1115 GLint img, GLint row, GLint column )
1116 {
1117 return _mesa_image_address(3, packing, image, width, height,
1118 format, type, img, row, column);
1119 }
1120
1121
1122
1123 /**
1124 * Compute the stride (in bytes) between image rows.
1125 *
1126 * \param packing the pixelstore attributes
1127 * \param width image width.
1128 * \param format pixel format.
1129 * \param type pixel data type.
1130 *
1131 * \return the stride in bytes for the given parameters, or -1 if error
1132 */
1133 GLint
1134 _mesa_image_row_stride( const struct gl_pixelstore_attrib *packing,
1135 GLint width, GLenum format, GLenum type )
1136 {
1137 GLint bytesPerRow, remainder;
1138
1139 ASSERT(packing);
1140
1141 if (type == GL_BITMAP) {
1142 if (packing->RowLength == 0) {
1143 bytesPerRow = (width + 7) / 8;
1144 }
1145 else {
1146 bytesPerRow = (packing->RowLength + 7) / 8;
1147 }
1148 }
1149 else {
1150 /* Non-BITMAP data */
1151 const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
1152 if (bytesPerPixel <= 0)
1153 return -1; /* error */
1154 if (packing->RowLength == 0) {
1155 bytesPerRow = bytesPerPixel * width;
1156 }
1157 else {
1158 bytesPerRow = bytesPerPixel * packing->RowLength;
1159 }
1160 }
1161
1162 remainder = bytesPerRow % packing->Alignment;
1163 if (remainder > 0) {
1164 bytesPerRow += (packing->Alignment - remainder);
1165 }
1166
1167 if (packing->Invert) {
1168 /* negate the bytes per row (negative row stride) */
1169 bytesPerRow = -bytesPerRow;
1170 }
1171
1172 return bytesPerRow;
1173 }
1174
1175
1176 /*
1177 * Compute the stride between images in a 3D texture (in bytes) for the given
1178 * pixel packing parameters and image width, format and type.
1179 */
1180 GLint
1181 _mesa_image_image_stride( const struct gl_pixelstore_attrib *packing,
1182 GLint width, GLint height,
1183 GLenum format, GLenum type )
1184 {
1185 GLint bytesPerRow, bytesPerImage, remainder;
1186
1187 ASSERT(packing);
1188
1189 if (type == GL_BITMAP) {
1190 if (packing->RowLength == 0) {
1191 bytesPerRow = (width + 7) / 8;
1192 }
1193 else {
1194 bytesPerRow = (packing->RowLength + 7) / 8;
1195 }
1196 }
1197 else {
1198 const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
1199
1200 if (bytesPerPixel <= 0)
1201 return -1; /* error */
1202 if (packing->RowLength == 0) {
1203 bytesPerRow = bytesPerPixel * width;
1204 }
1205 else {
1206 bytesPerRow = bytesPerPixel * packing->RowLength;
1207 }
1208 }
1209
1210 remainder = bytesPerRow % packing->Alignment;
1211 if (remainder > 0)
1212 bytesPerRow += (packing->Alignment - remainder);
1213
1214 if (packing->ImageHeight == 0)
1215 bytesPerImage = bytesPerRow * height;
1216 else
1217 bytesPerImage = bytesPerRow * packing->ImageHeight;
1218
1219 return bytesPerImage;
1220 }
1221
1222
1223
1224 /**
1225 * "Expand" a bitmap from 1-bit per pixel to 8-bits per pixel.
1226 * This is typically used to convert a bitmap into a GLubyte/pixel texture.
1227 * "On" bits will set texels to \p onValue.
1228 * "Off" bits will not modify texels.
1229 * \param width src bitmap width in pixels
1230 * \param height src bitmap height in pixels
1231 * \param unpack bitmap unpacking state
1232 * \param bitmap the src bitmap data
1233 * \param destBuffer start of dest buffer
1234 * \param destStride row stride in dest buffer
1235 * \param onValue if bit is 1, set destBuffer pixel to this value
1236 */
1237 void
1238 _mesa_expand_bitmap(GLsizei width, GLsizei height,
1239 const struct gl_pixelstore_attrib *unpack,
1240 const GLubyte *bitmap,
1241 GLubyte *destBuffer, GLint destStride,
1242 GLubyte onValue)
1243 {
1244 const GLubyte *srcRow = (const GLubyte *)
1245 _mesa_image_address2d(unpack, bitmap, width, height,
1246 GL_COLOR_INDEX, GL_BITMAP, 0, 0);
1247 const GLint srcStride = _mesa_image_row_stride(unpack, width,
1248 GL_COLOR_INDEX, GL_BITMAP);
1249 GLint row, col;
1250
1251 #define SET_PIXEL(COL, ROW) \
1252 destBuffer[(ROW) * destStride + (COL)] = onValue;
1253
1254 for (row = 0; row < height; row++) {
1255 const GLubyte *src = srcRow;
1256
1257 if (unpack->LsbFirst) {
1258 /* Lsb first */
1259 GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
1260 for (col = 0; col < width; col++) {
1261
1262 if (*src & mask) {
1263 SET_PIXEL(col, row);
1264 }
1265
1266 if (mask == 128U) {
1267 src++;
1268 mask = 1U;
1269 }
1270 else {
1271 mask = mask << 1;
1272 }
1273 }
1274
1275 /* get ready for next row */
1276 if (mask != 1)
1277 src++;
1278 }
1279 else {
1280 /* Msb first */
1281 GLubyte mask = 128U >> (unpack->SkipPixels & 0x7);
1282 for (col = 0; col < width; col++) {
1283
1284 if (*src & mask) {
1285 SET_PIXEL(col, row);
1286 }
1287
1288 if (mask == 1U) {
1289 src++;
1290 mask = 128U;
1291 }
1292 else {
1293 mask = mask >> 1;
1294 }
1295 }
1296
1297 /* get ready for next row */
1298 if (mask != 128)
1299 src++;
1300 }
1301
1302 srcRow += srcStride;
1303 } /* row */
1304
1305 #undef SET_PIXEL
1306 }
1307
1308
1309
1310
1311 /**
1312 * Convert an array of RGBA colors from one datatype to another.
1313 * NOTE: src may equal dst. In that case, we use a temporary buffer.
1314 */
1315 void
1316 _mesa_convert_colors(GLenum srcType, const GLvoid *src,
1317 GLenum dstType, GLvoid *dst,
1318 GLuint count, const GLubyte mask[])
1319 {
1320 GLuint tempBuffer[MAX_WIDTH][4];
1321 const GLboolean useTemp = (src == dst);
1322
1323 ASSERT(srcType != dstType);
1324
1325 switch (srcType) {
1326 case GL_UNSIGNED_BYTE:
1327 if (dstType == GL_UNSIGNED_SHORT) {
1328 const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src;
1329 GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst);
1330 GLuint i;
1331 for (i = 0; i < count; i++) {
1332 if (!mask || mask[i]) {
1333 dst2[i][RCOMP] = UBYTE_TO_USHORT(src1[i][RCOMP]);
1334 dst2[i][GCOMP] = UBYTE_TO_USHORT(src1[i][GCOMP]);
1335 dst2[i][BCOMP] = UBYTE_TO_USHORT(src1[i][BCOMP]);
1336 dst2[i][ACOMP] = UBYTE_TO_USHORT(src1[i][ACOMP]);
1337 }
1338 }
1339 if (useTemp)
1340 memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort));
1341 }
1342 else {
1343 const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src;
1344 GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst);
1345 GLuint i;
1346 ASSERT(dstType == GL_FLOAT);
1347 for (i = 0; i < count; i++) {
1348 if (!mask || mask[i]) {
1349 dst4[i][RCOMP] = UBYTE_TO_FLOAT(src1[i][RCOMP]);
1350 dst4[i][GCOMP] = UBYTE_TO_FLOAT(src1[i][GCOMP]);
1351 dst4[i][BCOMP] = UBYTE_TO_FLOAT(src1[i][BCOMP]);
1352 dst4[i][ACOMP] = UBYTE_TO_FLOAT(src1[i][ACOMP]);
1353 }
1354 }
1355 if (useTemp)
1356 memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat));
1357 }
1358 break;
1359 case GL_UNSIGNED_SHORT:
1360 if (dstType == GL_UNSIGNED_BYTE) {
1361 const GLushort (*src2)[4] = (const GLushort (*)[4]) src;
1362 GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst);
1363 GLuint i;
1364 for (i = 0; i < count; i++) {
1365 if (!mask || mask[i]) {
1366 dst1[i][RCOMP] = USHORT_TO_UBYTE(src2[i][RCOMP]);
1367 dst1[i][GCOMP] = USHORT_TO_UBYTE(src2[i][GCOMP]);
1368 dst1[i][BCOMP] = USHORT_TO_UBYTE(src2[i][BCOMP]);
1369 dst1[i][ACOMP] = USHORT_TO_UBYTE(src2[i][ACOMP]);
1370 }
1371 }
1372 if (useTemp)
1373 memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte));
1374 }
1375 else {
1376 const GLushort (*src2)[4] = (const GLushort (*)[4]) src;
1377 GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst);
1378 GLuint i;
1379 ASSERT(dstType == GL_FLOAT);
1380 for (i = 0; i < count; i++) {
1381 if (!mask || mask[i]) {
1382 dst4[i][RCOMP] = USHORT_TO_FLOAT(src2[i][RCOMP]);
1383 dst4[i][GCOMP] = USHORT_TO_FLOAT(src2[i][GCOMP]);
1384 dst4[i][BCOMP] = USHORT_TO_FLOAT(src2[i][BCOMP]);
1385 dst4[i][ACOMP] = USHORT_TO_FLOAT(src2[i][ACOMP]);
1386 }
1387 }
1388 if (useTemp)
1389 memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat));
1390 }
1391 break;
1392 case GL_FLOAT:
1393 if (dstType == GL_UNSIGNED_BYTE) {
1394 const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src;
1395 GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst);
1396 GLuint i;
1397 for (i = 0; i < count; i++) {
1398 if (!mask || mask[i]) {
1399 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][RCOMP], src4[i][RCOMP]);
1400 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][GCOMP], src4[i][GCOMP]);
1401 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][BCOMP], src4[i][BCOMP]);
1402 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][ACOMP], src4[i][ACOMP]);
1403 }
1404 }
1405 if (useTemp)
1406 memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte));
1407 }
1408 else {
1409 const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src;
1410 GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst);
1411 GLuint i;
1412 ASSERT(dstType == GL_UNSIGNED_SHORT);
1413 for (i = 0; i < count; i++) {
1414 if (!mask || mask[i]) {
1415 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][RCOMP], src4[i][RCOMP]);
1416 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][GCOMP], src4[i][GCOMP]);
1417 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][BCOMP], src4[i][BCOMP]);
1418 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][ACOMP], src4[i][ACOMP]);
1419 }
1420 }
1421 if (useTemp)
1422 memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort));
1423 }
1424 break;
1425 default:
1426 _mesa_problem(NULL, "Invalid datatype in _mesa_convert_colors");
1427 }
1428 }
1429
1430
1431
1432
1433 /**
1434 * Perform basic clipping for glDrawPixels. The image's position and size
1435 * and the unpack SkipPixels and SkipRows are adjusted so that the image
1436 * region is entirely within the window and scissor bounds.
1437 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
1438 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
1439 * we'll actually write. Beforehand, *destY-1 is the first drawing row.
1440 *
1441 * \return GL_TRUE if image is ready for drawing or
1442 * GL_FALSE if image was completely clipped away (draw nothing)
1443 */
1444 GLboolean
1445 _mesa_clip_drawpixels(const struct gl_context *ctx,
1446 GLint *destX, GLint *destY,
1447 GLsizei *width, GLsizei *height,
1448 struct gl_pixelstore_attrib *unpack)
1449 {
1450 const struct gl_framebuffer *buffer = ctx->DrawBuffer;
1451
1452 if (unpack->RowLength == 0) {
1453 unpack->RowLength = *width;
1454 }
1455
1456 ASSERT(ctx->Pixel.ZoomX == 1.0F);
1457 ASSERT(ctx->Pixel.ZoomY == 1.0F || ctx->Pixel.ZoomY == -1.0F);
1458
1459 /* left clipping */
1460 if (*destX < buffer->_Xmin) {
1461 unpack->SkipPixels += (buffer->_Xmin - *destX);
1462 *width -= (buffer->_Xmin - *destX);
1463 *destX = buffer->_Xmin;
1464 }
1465 /* right clipping */
1466 if (*destX + *width > buffer->_Xmax)
1467 *width -= (*destX + *width - buffer->_Xmax);
1468
1469 if (*width <= 0)
1470 return GL_FALSE;
1471
1472 if (ctx->Pixel.ZoomY == 1.0F) {
1473 /* bottom clipping */
1474 if (*destY < buffer->_Ymin) {
1475 unpack->SkipRows += (buffer->_Ymin - *destY);
1476 *height -= (buffer->_Ymin - *destY);
1477 *destY = buffer->_Ymin;
1478 }
1479 /* top clipping */
1480 if (*destY + *height > buffer->_Ymax)
1481 *height -= (*destY + *height - buffer->_Ymax);
1482 }
1483 else { /* upside down */
1484 /* top clipping */
1485 if (*destY > buffer->_Ymax) {
1486 unpack->SkipRows += (*destY - buffer->_Ymax);
1487 *height -= (*destY - buffer->_Ymax);
1488 *destY = buffer->_Ymax;
1489 }
1490 /* bottom clipping */
1491 if (*destY - *height < buffer->_Ymin)
1492 *height -= (buffer->_Ymin - (*destY - *height));
1493 /* adjust destY so it's the first row to write to */
1494 (*destY)--;
1495 }
1496
1497 if (*height <= 0)
1498 return GL_FALSE;
1499
1500 return GL_TRUE;
1501 }
1502
1503
1504 /**
1505 * Perform clipping for glReadPixels. The image's window position
1506 * and size, and the pack skipPixels, skipRows and rowLength are adjusted
1507 * so that the image region is entirely within the window bounds.
1508 * Note: this is different from _mesa_clip_drawpixels() in that the
1509 * scissor box is ignored, and we use the bounds of the current readbuffer
1510 * surface.
1511 *
1512 * \return GL_TRUE if image is ready for drawing or
1513 * GL_FALSE if image was completely clipped away (draw nothing)
1514 */
1515 GLboolean
1516 _mesa_clip_readpixels(const struct gl_context *ctx,
1517 GLint *srcX, GLint *srcY,
1518 GLsizei *width, GLsizei *height,
1519 struct gl_pixelstore_attrib *pack)
1520 {
1521 const struct gl_framebuffer *buffer = ctx->ReadBuffer;
1522
1523 if (pack->RowLength == 0) {
1524 pack->RowLength = *width;
1525 }
1526
1527 /* left clipping */
1528 if (*srcX < 0) {
1529 pack->SkipPixels += (0 - *srcX);
1530 *width -= (0 - *srcX);
1531 *srcX = 0;
1532 }
1533 /* right clipping */
1534 if (*srcX + *width > (GLsizei) buffer->Width)
1535 *width -= (*srcX + *width - buffer->Width);
1536
1537 if (*width <= 0)
1538 return GL_FALSE;
1539
1540 /* bottom clipping */
1541 if (*srcY < 0) {
1542 pack->SkipRows += (0 - *srcY);
1543 *height -= (0 - *srcY);
1544 *srcY = 0;
1545 }
1546 /* top clipping */
1547 if (*srcY + *height > (GLsizei) buffer->Height)
1548 *height -= (*srcY + *height - buffer->Height);
1549
1550 if (*height <= 0)
1551 return GL_FALSE;
1552
1553 return GL_TRUE;
1554 }
1555
1556
1557 /**
1558 * Do clipping for a glCopyTexSubImage call.
1559 * The framebuffer source region might extend outside the framebuffer
1560 * bounds. Clip the source region against the framebuffer bounds and
1561 * adjust the texture/dest position and size accordingly.
1562 *
1563 * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise.
1564 */
1565 GLboolean
1566 _mesa_clip_copytexsubimage(const struct gl_context *ctx,
1567 GLint *destX, GLint *destY,
1568 GLint *srcX, GLint *srcY,
1569 GLsizei *width, GLsizei *height)
1570 {
1571 const struct gl_framebuffer *fb = ctx->ReadBuffer;
1572 const GLint srcX0 = *srcX, srcY0 = *srcY;
1573
1574 if (_mesa_clip_to_region(0, 0, fb->Width, fb->Height,
1575 srcX, srcY, width, height)) {
1576 *destX = *destX + *srcX - srcX0;
1577 *destY = *destY + *srcY - srcY0;
1578
1579 return GL_TRUE;
1580 }
1581 else {
1582 return GL_FALSE;
1583 }
1584 }
1585
1586
1587
1588 /**
1589 * Clip the rectangle defined by (x, y, width, height) against the bounds
1590 * specified by [xmin, xmax) and [ymin, ymax).
1591 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
1592 */
1593 GLboolean
1594 _mesa_clip_to_region(GLint xmin, GLint ymin,
1595 GLint xmax, GLint ymax,
1596 GLint *x, GLint *y,
1597 GLsizei *width, GLsizei *height )
1598 {
1599 /* left clipping */
1600 if (*x < xmin) {
1601 *width -= (xmin - *x);
1602 *x = xmin;
1603 }
1604
1605 /* right clipping */
1606 if (*x + *width > xmax)
1607 *width -= (*x + *width - xmax);
1608
1609 if (*width <= 0)
1610 return GL_FALSE;
1611
1612 /* bottom (or top) clipping */
1613 if (*y < ymin) {
1614 *height -= (ymin - *y);
1615 *y = ymin;
1616 }
1617
1618 /* top (or bottom) clipping */
1619 if (*y + *height > ymax)
1620 *height -= (*y + *height - ymax);
1621
1622 if (*height <= 0)
1623 return GL_FALSE;
1624
1625 return GL_TRUE;
1626 }
1627
1628
1629 /**
1630 * Clip dst coords against Xmax (or Ymax).
1631 */
1632 static INLINE void
1633 clip_right_or_top(GLint *srcX0, GLint *srcX1,
1634 GLint *dstX0, GLint *dstX1,
1635 GLint maxValue)
1636 {
1637 GLfloat t, bias;
1638
1639 if (*dstX1 > maxValue) {
1640 /* X1 outside right edge */
1641 ASSERT(*dstX0 < maxValue); /* X0 should be inside right edge */
1642 t = (GLfloat) (maxValue - *dstX0) / (GLfloat) (*dstX1 - *dstX0);
1643 /* chop off [t, 1] part */
1644 ASSERT(t >= 0.0 && t <= 1.0);
1645 *dstX1 = maxValue;
1646 bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F;
1647 *srcX1 = *srcX0 + (GLint) (t * (*srcX1 - *srcX0) + bias);
1648 }
1649 else if (*dstX0 > maxValue) {
1650 /* X0 outside right edge */
1651 ASSERT(*dstX1 < maxValue); /* X1 should be inside right edge */
1652 t = (GLfloat) (maxValue - *dstX1) / (GLfloat) (*dstX0 - *dstX1);
1653 /* chop off [t, 1] part */
1654 ASSERT(t >= 0.0 && t <= 1.0);
1655 *dstX0 = maxValue;
1656 bias = (*srcX0 < *srcX1) ? -0.5F : 0.5F;
1657 *srcX0 = *srcX1 + (GLint) (t * (*srcX0 - *srcX1) + bias);
1658 }
1659 }
1660
1661
1662 /**
1663 * Clip dst coords against Xmin (or Ymin).
1664 */
1665 static INLINE void
1666 clip_left_or_bottom(GLint *srcX0, GLint *srcX1,
1667 GLint *dstX0, GLint *dstX1,
1668 GLint minValue)
1669 {
1670 GLfloat t, bias;
1671
1672 if (*dstX0 < minValue) {
1673 /* X0 outside left edge */
1674 ASSERT(*dstX1 > minValue); /* X1 should be inside left edge */
1675 t = (GLfloat) (minValue - *dstX0) / (GLfloat) (*dstX1 - *dstX0);
1676 /* chop off [0, t] part */
1677 ASSERT(t >= 0.0 && t <= 1.0);
1678 *dstX0 = minValue;
1679 bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F; /* flipped??? */
1680 *srcX0 = *srcX0 + (GLint) (t * (*srcX1 - *srcX0) + bias);
1681 }
1682 else if (*dstX1 < minValue) {
1683 /* X1 outside left edge */
1684 ASSERT(*dstX0 > minValue); /* X0 should be inside left edge */
1685 t = (GLfloat) (minValue - *dstX1) / (GLfloat) (*dstX0 - *dstX1);
1686 /* chop off [0, t] part */
1687 ASSERT(t >= 0.0 && t <= 1.0);
1688 *dstX1 = minValue;
1689 bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F;
1690 *srcX1 = *srcX1 + (GLint) (t * (*srcX0 - *srcX1) + bias);
1691 }
1692 }
1693
1694
1695 /**
1696 * Do clipping of blit src/dest rectangles.
1697 * The dest rect is clipped against both the buffer bounds and scissor bounds.
1698 * The src rect is just clipped against the buffer bounds.
1699 *
1700 * When either the src or dest rect is clipped, the other is also clipped
1701 * proportionately!
1702 *
1703 * Note that X0 need not be less than X1 (same for Y) for either the source
1704 * and dest rects. That makes the clipping a little trickier.
1705 *
1706 * \return GL_TRUE if anything is left to draw, GL_FALSE if totally clipped
1707 */
1708 GLboolean
1709 _mesa_clip_blit(struct gl_context *ctx,
1710 GLint *srcX0, GLint *srcY0, GLint *srcX1, GLint *srcY1,
1711 GLint *dstX0, GLint *dstY0, GLint *dstX1, GLint *dstY1)
1712 {
1713 const GLint srcXmin = 0;
1714 const GLint srcXmax = ctx->ReadBuffer->Width;
1715 const GLint srcYmin = 0;
1716 const GLint srcYmax = ctx->ReadBuffer->Height;
1717
1718 /* these include scissor bounds */
1719 const GLint dstXmin = ctx->DrawBuffer->_Xmin;
1720 const GLint dstXmax = ctx->DrawBuffer->_Xmax;
1721 const GLint dstYmin = ctx->DrawBuffer->_Ymin;
1722 const GLint dstYmax = ctx->DrawBuffer->_Ymax;
1723
1724 /*
1725 printf("PreClipX: src: %d .. %d dst: %d .. %d\n",
1726 *srcX0, *srcX1, *dstX0, *dstX1);
1727 printf("PreClipY: src: %d .. %d dst: %d .. %d\n",
1728 *srcY0, *srcY1, *dstY0, *dstY1);
1729 */
1730
1731 /* trivial rejection tests */
1732 if (*dstX0 == *dstX1)
1733 return GL_FALSE; /* no width */
1734 if (*dstX0 <= dstXmin && *dstX1 <= dstXmin)
1735 return GL_FALSE; /* totally out (left) of bounds */
1736 if (*dstX0 >= dstXmax && *dstX1 >= dstXmax)
1737 return GL_FALSE; /* totally out (right) of bounds */
1738
1739 if (*dstY0 == *dstY1)
1740 return GL_FALSE;
1741 if (*dstY0 <= dstYmin && *dstY1 <= dstYmin)
1742 return GL_FALSE;
1743 if (*dstY0 >= dstYmax && *dstY1 >= dstYmax)
1744 return GL_FALSE;
1745
1746 if (*srcX0 == *srcX1)
1747 return GL_FALSE;
1748 if (*srcX0 <= srcXmin && *srcX1 <= srcXmin)
1749 return GL_FALSE;
1750 if (*srcX0 >= srcXmax && *srcX1 >= srcXmax)
1751 return GL_FALSE;
1752
1753 if (*srcY0 == *srcY1)
1754 return GL_FALSE;
1755 if (*srcY0 <= srcYmin && *srcY1 <= srcYmin)
1756 return GL_FALSE;
1757 if (*srcY0 >= srcYmax && *srcY1 >= srcYmax)
1758 return GL_FALSE;
1759
1760 /*
1761 * dest clip
1762 */
1763 clip_right_or_top(srcX0, srcX1, dstX0, dstX1, dstXmax);
1764 clip_right_or_top(srcY0, srcY1, dstY0, dstY1, dstYmax);
1765 clip_left_or_bottom(srcX0, srcX1, dstX0, dstX1, dstXmin);
1766 clip_left_or_bottom(srcY0, srcY1, dstY0, dstY1, dstYmin);
1767
1768 /*
1769 * src clip (just swap src/dst values from above)
1770 */
1771 clip_right_or_top(dstX0, dstX1, srcX0, srcX1, srcXmax);
1772 clip_right_or_top(dstY0, dstY1, srcY0, srcY1, srcYmax);
1773 clip_left_or_bottom(dstX0, dstX1, srcX0, srcX1, srcXmin);
1774 clip_left_or_bottom(dstY0, dstY1, srcY0, srcY1, srcYmin);
1775
1776 /*
1777 printf("PostClipX: src: %d .. %d dst: %d .. %d\n",
1778 *srcX0, *srcX1, *dstX0, *dstX1);
1779 printf("PostClipY: src: %d .. %d dst: %d .. %d\n",
1780 *srcY0, *srcY1, *dstY0, *dstY1);
1781 */
1782
1783 ASSERT(*dstX0 >= dstXmin);
1784 ASSERT(*dstX0 <= dstXmax);
1785 ASSERT(*dstX1 >= dstXmin);
1786 ASSERT(*dstX1 <= dstXmax);
1787
1788 ASSERT(*dstY0 >= dstYmin);
1789 ASSERT(*dstY0 <= dstYmax);
1790 ASSERT(*dstY1 >= dstYmin);
1791 ASSERT(*dstY1 <= dstYmax);
1792
1793 ASSERT(*srcX0 >= srcXmin);
1794 ASSERT(*srcX0 <= srcXmax);
1795 ASSERT(*srcX1 >= srcXmin);
1796 ASSERT(*srcX1 <= srcXmax);
1797
1798 ASSERT(*srcY0 >= srcYmin);
1799 ASSERT(*srcY0 <= srcYmax);
1800 ASSERT(*srcY1 >= srcYmin);
1801 ASSERT(*srcY1 <= srcYmax);
1802
1803 return GL_TRUE;
1804 }