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