mesa: split up the image.c file
[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 return GL_TRUE;
673 /* signed texture formats */
674 case GL_RGBA_SNORM:
675 case GL_RGBA8_SNORM:
676 return GL_TRUE;
677 case GL_YCBCR_MESA: /* not considered to be RGB */
678 /* fall-through */
679 default:
680 return GL_FALSE;
681 }
682 }
683
684
685 /**
686 * Test if the given image format is a color index format.
687 */
688 GLboolean
689 _mesa_is_index_format(GLenum format)
690 {
691 switch (format) {
692 case GL_COLOR_INDEX:
693 case GL_COLOR_INDEX1_EXT:
694 case GL_COLOR_INDEX2_EXT:
695 case GL_COLOR_INDEX4_EXT:
696 case GL_COLOR_INDEX8_EXT:
697 case GL_COLOR_INDEX12_EXT:
698 case GL_COLOR_INDEX16_EXT:
699 return GL_TRUE;
700 default:
701 return GL_FALSE;
702 }
703 }
704
705
706 /**
707 * Test if the given image format is a depth component format.
708 */
709 GLboolean
710 _mesa_is_depth_format(GLenum format)
711 {
712 switch (format) {
713 case GL_DEPTH_COMPONENT:
714 case GL_DEPTH_COMPONENT16:
715 case GL_DEPTH_COMPONENT24:
716 case GL_DEPTH_COMPONENT32:
717 return GL_TRUE;
718 default:
719 return GL_FALSE;
720 }
721 }
722
723
724 /**
725 * Test if the given image format is a stencil format.
726 */
727 GLboolean
728 _mesa_is_stencil_format(GLenum format)
729 {
730 switch (format) {
731 case GL_STENCIL_INDEX:
732 case GL_DEPTH_STENCIL:
733 return GL_TRUE;
734 default:
735 return GL_FALSE;
736 }
737 }
738
739
740 /**
741 * Test if the given image format is a YCbCr format.
742 */
743 GLboolean
744 _mesa_is_ycbcr_format(GLenum format)
745 {
746 switch (format) {
747 case GL_YCBCR_MESA:
748 return GL_TRUE;
749 default:
750 return GL_FALSE;
751 }
752 }
753
754
755 /**
756 * Test if the given image format is a depth+stencil format.
757 */
758 GLboolean
759 _mesa_is_depthstencil_format(GLenum format)
760 {
761 switch (format) {
762 case GL_DEPTH24_STENCIL8_EXT:
763 case GL_DEPTH_STENCIL_EXT:
764 return GL_TRUE;
765 default:
766 return GL_FALSE;
767 }
768 }
769
770
771 /**
772 * Test if the given image format is a depth or stencil format.
773 */
774 GLboolean
775 _mesa_is_depth_or_stencil_format(GLenum format)
776 {
777 switch (format) {
778 case GL_DEPTH_COMPONENT:
779 case GL_DEPTH_COMPONENT16:
780 case GL_DEPTH_COMPONENT24:
781 case GL_DEPTH_COMPONENT32:
782 case GL_STENCIL_INDEX:
783 case GL_STENCIL_INDEX1_EXT:
784 case GL_STENCIL_INDEX4_EXT:
785 case GL_STENCIL_INDEX8_EXT:
786 case GL_STENCIL_INDEX16_EXT:
787 case GL_DEPTH_STENCIL_EXT:
788 case GL_DEPTH24_STENCIL8_EXT:
789 return GL_TRUE;
790 default:
791 return GL_FALSE;
792 }
793 }
794
795
796 /**
797 * Test if the given image format is a dudv format.
798 */
799 GLboolean
800 _mesa_is_dudv_format(GLenum format)
801 {
802 switch (format) {
803 case GL_DUDV_ATI:
804 case GL_DU8DV8_ATI:
805 return GL_TRUE;
806 default:
807 return GL_FALSE;
808 }
809 }
810
811
812 /**
813 * Test if the given format is an integer (non-normalized) format.
814 */
815 GLboolean
816 _mesa_is_integer_format(GLenum format)
817 {
818 switch (format) {
819 case GL_RED_INTEGER_EXT:
820 case GL_GREEN_INTEGER_EXT:
821 case GL_BLUE_INTEGER_EXT:
822 case GL_ALPHA_INTEGER_EXT:
823 case GL_RGB_INTEGER_EXT:
824 case GL_RGBA_INTEGER_EXT:
825 case GL_BGR_INTEGER_EXT:
826 case GL_BGRA_INTEGER_EXT:
827 case GL_LUMINANCE_INTEGER_EXT:
828 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
829 return GL_TRUE;
830 default:
831 return GL_FALSE;
832 }
833 }
834
835
836 /**
837 * Test if an image format is a supported compressed format.
838 * \param format the internal format token provided by the user.
839 * \return GL_TRUE if compressed, GL_FALSE if uncompressed
840 */
841 GLboolean
842 _mesa_is_compressed_format(struct gl_context *ctx, GLenum format)
843 {
844 switch (format) {
845 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
846 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
847 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
848 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
849 return ctx->Extensions.EXT_texture_compression_s3tc;
850 case GL_RGB_S3TC:
851 case GL_RGB4_S3TC:
852 case GL_RGBA_S3TC:
853 case GL_RGBA4_S3TC:
854 return ctx->Extensions.S3_s3tc;
855 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
856 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
857 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
858 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
859 return ctx->Extensions.EXT_texture_sRGB
860 && ctx->Extensions.EXT_texture_compression_s3tc;
861 case GL_COMPRESSED_RGB_FXT1_3DFX:
862 case GL_COMPRESSED_RGBA_FXT1_3DFX:
863 return ctx->Extensions.TDFX_texture_compression_FXT1;
864 case GL_COMPRESSED_RED_RGTC1:
865 case GL_COMPRESSED_SIGNED_RED_RGTC1:
866 case GL_COMPRESSED_RG_RGTC2:
867 case GL_COMPRESSED_SIGNED_RG_RGTC2:
868 return ctx->Extensions.ARB_texture_compression_rgtc;
869 default:
870 return GL_FALSE;
871 }
872 }
873
874
875 /**
876 * Return the address of a specific pixel in an image (1D, 2D or 3D).
877 *
878 * Pixel unpacking/packing parameters are observed according to \p packing.
879 *
880 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
881 * \param image starting address of image data
882 * \param width the image width
883 * \param height theimage height
884 * \param format the pixel format
885 * \param type the pixel data type
886 * \param packing the pixelstore attributes
887 * \param img which image in the volume (0 for 1D or 2D images)
888 * \param row row of pixel in the image (0 for 1D images)
889 * \param column column of pixel in the image
890 *
891 * \return address of pixel on success, or NULL on error.
892 *
893 * \sa gl_pixelstore_attrib.
894 */
895 GLvoid *
896 _mesa_image_address( GLuint dimensions,
897 const struct gl_pixelstore_attrib *packing,
898 const GLvoid *image,
899 GLsizei width, GLsizei height,
900 GLenum format, GLenum type,
901 GLint img, GLint row, GLint column )
902 {
903 GLint alignment; /* 1, 2 or 4 */
904 GLint pixels_per_row;
905 GLint rows_per_image;
906 GLint skiprows;
907 GLint skippixels;
908 GLint skipimages; /* for 3-D volume images */
909 GLubyte *pixel_addr;
910
911 ASSERT(dimensions >= 1 && dimensions <= 3);
912
913 alignment = packing->Alignment;
914 if (packing->RowLength > 0) {
915 pixels_per_row = packing->RowLength;
916 }
917 else {
918 pixels_per_row = width;
919 }
920 if (packing->ImageHeight > 0) {
921 rows_per_image = packing->ImageHeight;
922 }
923 else {
924 rows_per_image = height;
925 }
926
927 skippixels = packing->SkipPixels;
928 /* Note: SKIP_ROWS _is_ used for 1D images */
929 skiprows = packing->SkipRows;
930 /* Note: SKIP_IMAGES is only used for 3D images */
931 skipimages = (dimensions == 3) ? packing->SkipImages : 0;
932
933 if (type == GL_BITMAP) {
934 /* BITMAP data */
935 GLint comp_per_pixel; /* components per pixel */
936 GLint bytes_per_comp; /* bytes per component */
937 GLint bytes_per_row;
938 GLint bytes_per_image;
939
940 /* Compute bytes per component */
941 bytes_per_comp = _mesa_sizeof_packed_type( type );
942 if (bytes_per_comp < 0) {
943 return NULL;
944 }
945
946 /* Compute number of components per pixel */
947 comp_per_pixel = _mesa_components_in_format( format );
948 if (comp_per_pixel < 0) {
949 return NULL;
950 }
951
952 bytes_per_row = alignment
953 * CEILING( comp_per_pixel*pixels_per_row, 8*alignment );
954
955 bytes_per_image = bytes_per_row * rows_per_image;
956
957 pixel_addr = (GLubyte *) image
958 + (skipimages + img) * bytes_per_image
959 + (skiprows + row) * bytes_per_row
960 + (skippixels + column) / 8;
961 }
962 else {
963 /* Non-BITMAP data */
964 GLint bytes_per_pixel, bytes_per_row, remainder, bytes_per_image;
965 GLint topOfImage;
966
967 bytes_per_pixel = _mesa_bytes_per_pixel( format, type );
968
969 /* The pixel type and format should have been error checked earlier */
970 assert(bytes_per_pixel > 0);
971
972 bytes_per_row = pixels_per_row * bytes_per_pixel;
973 remainder = bytes_per_row % alignment;
974 if (remainder > 0)
975 bytes_per_row += (alignment - remainder);
976
977 ASSERT(bytes_per_row % alignment == 0);
978
979 bytes_per_image = bytes_per_row * rows_per_image;
980
981 if (packing->Invert) {
982 /* set pixel_addr to the last row */
983 topOfImage = bytes_per_row * (height - 1);
984 bytes_per_row = -bytes_per_row;
985 }
986 else {
987 topOfImage = 0;
988 }
989
990 /* compute final pixel address */
991 pixel_addr = (GLubyte *) image
992 + (skipimages + img) * bytes_per_image
993 + topOfImage
994 + (skiprows + row) * bytes_per_row
995 + (skippixels + column) * bytes_per_pixel;
996 }
997
998 return (GLvoid *) pixel_addr;
999 }
1000
1001
1002 GLvoid *
1003 _mesa_image_address1d( const struct gl_pixelstore_attrib *packing,
1004 const GLvoid *image,
1005 GLsizei width,
1006 GLenum format, GLenum type,
1007 GLint column )
1008 {
1009 return _mesa_image_address(1, packing, image, width, 1,
1010 format, type, 0, 0, column);
1011 }
1012
1013
1014 GLvoid *
1015 _mesa_image_address2d( const struct gl_pixelstore_attrib *packing,
1016 const GLvoid *image,
1017 GLsizei width, GLsizei height,
1018 GLenum format, GLenum type,
1019 GLint row, GLint column )
1020 {
1021 return _mesa_image_address(2, packing, image, width, height,
1022 format, type, 0, row, column);
1023 }
1024
1025
1026 GLvoid *
1027 _mesa_image_address3d( const struct gl_pixelstore_attrib *packing,
1028 const GLvoid *image,
1029 GLsizei width, GLsizei height,
1030 GLenum format, GLenum type,
1031 GLint img, GLint row, GLint column )
1032 {
1033 return _mesa_image_address(3, packing, image, width, height,
1034 format, type, img, row, column);
1035 }
1036
1037
1038
1039 /**
1040 * Compute the stride (in bytes) between image rows.
1041 *
1042 * \param packing the pixelstore attributes
1043 * \param width image width.
1044 * \param format pixel format.
1045 * \param type pixel data type.
1046 *
1047 * \return the stride in bytes for the given parameters, or -1 if error
1048 */
1049 GLint
1050 _mesa_image_row_stride( const struct gl_pixelstore_attrib *packing,
1051 GLint width, GLenum format, GLenum type )
1052 {
1053 GLint bytesPerRow, remainder;
1054
1055 ASSERT(packing);
1056
1057 if (type == GL_BITMAP) {
1058 if (packing->RowLength == 0) {
1059 bytesPerRow = (width + 7) / 8;
1060 }
1061 else {
1062 bytesPerRow = (packing->RowLength + 7) / 8;
1063 }
1064 }
1065 else {
1066 /* Non-BITMAP data */
1067 const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
1068 if (bytesPerPixel <= 0)
1069 return -1; /* error */
1070 if (packing->RowLength == 0) {
1071 bytesPerRow = bytesPerPixel * width;
1072 }
1073 else {
1074 bytesPerRow = bytesPerPixel * packing->RowLength;
1075 }
1076 }
1077
1078 remainder = bytesPerRow % packing->Alignment;
1079 if (remainder > 0) {
1080 bytesPerRow += (packing->Alignment - remainder);
1081 }
1082
1083 if (packing->Invert) {
1084 /* negate the bytes per row (negative row stride) */
1085 bytesPerRow = -bytesPerRow;
1086 }
1087
1088 return bytesPerRow;
1089 }
1090
1091
1092 /*
1093 * Compute the stride between images in a 3D texture (in bytes) for the given
1094 * pixel packing parameters and image width, format and type.
1095 */
1096 GLint
1097 _mesa_image_image_stride( const struct gl_pixelstore_attrib *packing,
1098 GLint width, GLint height,
1099 GLenum format, GLenum type )
1100 {
1101 GLint bytesPerRow, bytesPerImage, remainder;
1102
1103 ASSERT(packing);
1104
1105 if (type == GL_BITMAP) {
1106 if (packing->RowLength == 0) {
1107 bytesPerRow = (width + 7) / 8;
1108 }
1109 else {
1110 bytesPerRow = (packing->RowLength + 7) / 8;
1111 }
1112 }
1113 else {
1114 const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
1115
1116 if (bytesPerPixel <= 0)
1117 return -1; /* error */
1118 if (packing->RowLength == 0) {
1119 bytesPerRow = bytesPerPixel * width;
1120 }
1121 else {
1122 bytesPerRow = bytesPerPixel * packing->RowLength;
1123 }
1124 }
1125
1126 remainder = bytesPerRow % packing->Alignment;
1127 if (remainder > 0)
1128 bytesPerRow += (packing->Alignment - remainder);
1129
1130 if (packing->ImageHeight == 0)
1131 bytesPerImage = bytesPerRow * height;
1132 else
1133 bytesPerImage = bytesPerRow * packing->ImageHeight;
1134
1135 return bytesPerImage;
1136 }
1137
1138
1139
1140 /**
1141 * "Expand" a bitmap from 1-bit per pixel to 8-bits per pixel.
1142 * This is typically used to convert a bitmap into a GLubyte/pixel texture.
1143 * "On" bits will set texels to \p onValue.
1144 * "Off" bits will not modify texels.
1145 * \param width src bitmap width in pixels
1146 * \param height src bitmap height in pixels
1147 * \param unpack bitmap unpacking state
1148 * \param bitmap the src bitmap data
1149 * \param destBuffer start of dest buffer
1150 * \param destStride row stride in dest buffer
1151 * \param onValue if bit is 1, set destBuffer pixel to this value
1152 */
1153 void
1154 _mesa_expand_bitmap(GLsizei width, GLsizei height,
1155 const struct gl_pixelstore_attrib *unpack,
1156 const GLubyte *bitmap,
1157 GLubyte *destBuffer, GLint destStride,
1158 GLubyte onValue)
1159 {
1160 const GLubyte *srcRow = (const GLubyte *)
1161 _mesa_image_address2d(unpack, bitmap, width, height,
1162 GL_COLOR_INDEX, GL_BITMAP, 0, 0);
1163 const GLint srcStride = _mesa_image_row_stride(unpack, width,
1164 GL_COLOR_INDEX, GL_BITMAP);
1165 GLint row, col;
1166
1167 #define SET_PIXEL(COL, ROW) \
1168 destBuffer[(ROW) * destStride + (COL)] = onValue;
1169
1170 for (row = 0; row < height; row++) {
1171 const GLubyte *src = srcRow;
1172
1173 if (unpack->LsbFirst) {
1174 /* Lsb first */
1175 GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
1176 for (col = 0; col < width; col++) {
1177
1178 if (*src & mask) {
1179 SET_PIXEL(col, row);
1180 }
1181
1182 if (mask == 128U) {
1183 src++;
1184 mask = 1U;
1185 }
1186 else {
1187 mask = mask << 1;
1188 }
1189 }
1190
1191 /* get ready for next row */
1192 if (mask != 1)
1193 src++;
1194 }
1195 else {
1196 /* Msb first */
1197 GLubyte mask = 128U >> (unpack->SkipPixels & 0x7);
1198 for (col = 0; col < width; col++) {
1199
1200 if (*src & mask) {
1201 SET_PIXEL(col, row);
1202 }
1203
1204 if (mask == 1U) {
1205 src++;
1206 mask = 128U;
1207 }
1208 else {
1209 mask = mask >> 1;
1210 }
1211 }
1212
1213 /* get ready for next row */
1214 if (mask != 128)
1215 src++;
1216 }
1217
1218 srcRow += srcStride;
1219 } /* row */
1220
1221 #undef SET_PIXEL
1222 }
1223
1224
1225
1226
1227 /**
1228 * Convert an array of RGBA colors from one datatype to another.
1229 * NOTE: src may equal dst. In that case, we use a temporary buffer.
1230 */
1231 void
1232 _mesa_convert_colors(GLenum srcType, const GLvoid *src,
1233 GLenum dstType, GLvoid *dst,
1234 GLuint count, const GLubyte mask[])
1235 {
1236 GLuint tempBuffer[MAX_WIDTH][4];
1237 const GLboolean useTemp = (src == dst);
1238
1239 ASSERT(srcType != dstType);
1240
1241 switch (srcType) {
1242 case GL_UNSIGNED_BYTE:
1243 if (dstType == GL_UNSIGNED_SHORT) {
1244 const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src;
1245 GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst);
1246 GLuint i;
1247 for (i = 0; i < count; i++) {
1248 if (!mask || mask[i]) {
1249 dst2[i][RCOMP] = UBYTE_TO_USHORT(src1[i][RCOMP]);
1250 dst2[i][GCOMP] = UBYTE_TO_USHORT(src1[i][GCOMP]);
1251 dst2[i][BCOMP] = UBYTE_TO_USHORT(src1[i][BCOMP]);
1252 dst2[i][ACOMP] = UBYTE_TO_USHORT(src1[i][ACOMP]);
1253 }
1254 }
1255 if (useTemp)
1256 memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort));
1257 }
1258 else {
1259 const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src;
1260 GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst);
1261 GLuint i;
1262 ASSERT(dstType == GL_FLOAT);
1263 for (i = 0; i < count; i++) {
1264 if (!mask || mask[i]) {
1265 dst4[i][RCOMP] = UBYTE_TO_FLOAT(src1[i][RCOMP]);
1266 dst4[i][GCOMP] = UBYTE_TO_FLOAT(src1[i][GCOMP]);
1267 dst4[i][BCOMP] = UBYTE_TO_FLOAT(src1[i][BCOMP]);
1268 dst4[i][ACOMP] = UBYTE_TO_FLOAT(src1[i][ACOMP]);
1269 }
1270 }
1271 if (useTemp)
1272 memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat));
1273 }
1274 break;
1275 case GL_UNSIGNED_SHORT:
1276 if (dstType == GL_UNSIGNED_BYTE) {
1277 const GLushort (*src2)[4] = (const GLushort (*)[4]) src;
1278 GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst);
1279 GLuint i;
1280 for (i = 0; i < count; i++) {
1281 if (!mask || mask[i]) {
1282 dst1[i][RCOMP] = USHORT_TO_UBYTE(src2[i][RCOMP]);
1283 dst1[i][GCOMP] = USHORT_TO_UBYTE(src2[i][GCOMP]);
1284 dst1[i][BCOMP] = USHORT_TO_UBYTE(src2[i][BCOMP]);
1285 dst1[i][ACOMP] = USHORT_TO_UBYTE(src2[i][ACOMP]);
1286 }
1287 }
1288 if (useTemp)
1289 memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte));
1290 }
1291 else {
1292 const GLushort (*src2)[4] = (const GLushort (*)[4]) src;
1293 GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst);
1294 GLuint i;
1295 ASSERT(dstType == GL_FLOAT);
1296 for (i = 0; i < count; i++) {
1297 if (!mask || mask[i]) {
1298 dst4[i][RCOMP] = USHORT_TO_FLOAT(src2[i][RCOMP]);
1299 dst4[i][GCOMP] = USHORT_TO_FLOAT(src2[i][GCOMP]);
1300 dst4[i][BCOMP] = USHORT_TO_FLOAT(src2[i][BCOMP]);
1301 dst4[i][ACOMP] = USHORT_TO_FLOAT(src2[i][ACOMP]);
1302 }
1303 }
1304 if (useTemp)
1305 memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat));
1306 }
1307 break;
1308 case GL_FLOAT:
1309 if (dstType == GL_UNSIGNED_BYTE) {
1310 const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src;
1311 GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst);
1312 GLuint i;
1313 for (i = 0; i < count; i++) {
1314 if (!mask || mask[i]) {
1315 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][RCOMP], src4[i][RCOMP]);
1316 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][GCOMP], src4[i][GCOMP]);
1317 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][BCOMP], src4[i][BCOMP]);
1318 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][ACOMP], src4[i][ACOMP]);
1319 }
1320 }
1321 if (useTemp)
1322 memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte));
1323 }
1324 else {
1325 const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src;
1326 GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst);
1327 GLuint i;
1328 ASSERT(dstType == GL_UNSIGNED_SHORT);
1329 for (i = 0; i < count; i++) {
1330 if (!mask || mask[i]) {
1331 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][RCOMP], src4[i][RCOMP]);
1332 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][GCOMP], src4[i][GCOMP]);
1333 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][BCOMP], src4[i][BCOMP]);
1334 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][ACOMP], src4[i][ACOMP]);
1335 }
1336 }
1337 if (useTemp)
1338 memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort));
1339 }
1340 break;
1341 default:
1342 _mesa_problem(NULL, "Invalid datatype in _mesa_convert_colors");
1343 }
1344 }
1345
1346
1347
1348
1349 /**
1350 * Perform basic clipping for glDrawPixels. The image's position and size
1351 * and the unpack SkipPixels and SkipRows are adjusted so that the image
1352 * region is entirely within the window and scissor bounds.
1353 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
1354 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
1355 * we'll actually write. Beforehand, *destY-1 is the first drawing row.
1356 *
1357 * \return GL_TRUE if image is ready for drawing or
1358 * GL_FALSE if image was completely clipped away (draw nothing)
1359 */
1360 GLboolean
1361 _mesa_clip_drawpixels(const struct gl_context *ctx,
1362 GLint *destX, GLint *destY,
1363 GLsizei *width, GLsizei *height,
1364 struct gl_pixelstore_attrib *unpack)
1365 {
1366 const struct gl_framebuffer *buffer = ctx->DrawBuffer;
1367
1368 if (unpack->RowLength == 0) {
1369 unpack->RowLength = *width;
1370 }
1371
1372 ASSERT(ctx->Pixel.ZoomX == 1.0F);
1373 ASSERT(ctx->Pixel.ZoomY == 1.0F || ctx->Pixel.ZoomY == -1.0F);
1374
1375 /* left clipping */
1376 if (*destX < buffer->_Xmin) {
1377 unpack->SkipPixels += (buffer->_Xmin - *destX);
1378 *width -= (buffer->_Xmin - *destX);
1379 *destX = buffer->_Xmin;
1380 }
1381 /* right clipping */
1382 if (*destX + *width > buffer->_Xmax)
1383 *width -= (*destX + *width - buffer->_Xmax);
1384
1385 if (*width <= 0)
1386 return GL_FALSE;
1387
1388 if (ctx->Pixel.ZoomY == 1.0F) {
1389 /* bottom clipping */
1390 if (*destY < buffer->_Ymin) {
1391 unpack->SkipRows += (buffer->_Ymin - *destY);
1392 *height -= (buffer->_Ymin - *destY);
1393 *destY = buffer->_Ymin;
1394 }
1395 /* top clipping */
1396 if (*destY + *height > buffer->_Ymax)
1397 *height -= (*destY + *height - buffer->_Ymax);
1398 }
1399 else { /* upside down */
1400 /* top clipping */
1401 if (*destY > buffer->_Ymax) {
1402 unpack->SkipRows += (*destY - buffer->_Ymax);
1403 *height -= (*destY - buffer->_Ymax);
1404 *destY = buffer->_Ymax;
1405 }
1406 /* bottom clipping */
1407 if (*destY - *height < buffer->_Ymin)
1408 *height -= (buffer->_Ymin - (*destY - *height));
1409 /* adjust destY so it's the first row to write to */
1410 (*destY)--;
1411 }
1412
1413 if (*height <= 0)
1414 return GL_FALSE;
1415
1416 return GL_TRUE;
1417 }
1418
1419
1420 /**
1421 * Perform clipping for glReadPixels. The image's window position
1422 * and size, and the pack skipPixels, skipRows and rowLength are adjusted
1423 * so that the image region is entirely within the window bounds.
1424 * Note: this is different from _mesa_clip_drawpixels() in that the
1425 * scissor box is ignored, and we use the bounds of the current readbuffer
1426 * surface.
1427 *
1428 * \return GL_TRUE if image is ready for drawing or
1429 * GL_FALSE if image was completely clipped away (draw nothing)
1430 */
1431 GLboolean
1432 _mesa_clip_readpixels(const struct gl_context *ctx,
1433 GLint *srcX, GLint *srcY,
1434 GLsizei *width, GLsizei *height,
1435 struct gl_pixelstore_attrib *pack)
1436 {
1437 const struct gl_framebuffer *buffer = ctx->ReadBuffer;
1438
1439 if (pack->RowLength == 0) {
1440 pack->RowLength = *width;
1441 }
1442
1443 /* left clipping */
1444 if (*srcX < 0) {
1445 pack->SkipPixels += (0 - *srcX);
1446 *width -= (0 - *srcX);
1447 *srcX = 0;
1448 }
1449 /* right clipping */
1450 if (*srcX + *width > (GLsizei) buffer->Width)
1451 *width -= (*srcX + *width - buffer->Width);
1452
1453 if (*width <= 0)
1454 return GL_FALSE;
1455
1456 /* bottom clipping */
1457 if (*srcY < 0) {
1458 pack->SkipRows += (0 - *srcY);
1459 *height -= (0 - *srcY);
1460 *srcY = 0;
1461 }
1462 /* top clipping */
1463 if (*srcY + *height > (GLsizei) buffer->Height)
1464 *height -= (*srcY + *height - buffer->Height);
1465
1466 if (*height <= 0)
1467 return GL_FALSE;
1468
1469 return GL_TRUE;
1470 }
1471
1472
1473 /**
1474 * Do clipping for a glCopyTexSubImage call.
1475 * The framebuffer source region might extend outside the framebuffer
1476 * bounds. Clip the source region against the framebuffer bounds and
1477 * adjust the texture/dest position and size accordingly.
1478 *
1479 * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise.
1480 */
1481 GLboolean
1482 _mesa_clip_copytexsubimage(const struct gl_context *ctx,
1483 GLint *destX, GLint *destY,
1484 GLint *srcX, GLint *srcY,
1485 GLsizei *width, GLsizei *height)
1486 {
1487 const struct gl_framebuffer *fb = ctx->ReadBuffer;
1488 const GLint srcX0 = *srcX, srcY0 = *srcY;
1489
1490 if (_mesa_clip_to_region(0, 0, fb->Width, fb->Height,
1491 srcX, srcY, width, height)) {
1492 *destX = *destX + *srcX - srcX0;
1493 *destY = *destY + *srcY - srcY0;
1494
1495 return GL_TRUE;
1496 }
1497 else {
1498 return GL_FALSE;
1499 }
1500 }
1501
1502
1503
1504 /**
1505 * Clip the rectangle defined by (x, y, width, height) against the bounds
1506 * specified by [xmin, xmax) and [ymin, ymax).
1507 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
1508 */
1509 GLboolean
1510 _mesa_clip_to_region(GLint xmin, GLint ymin,
1511 GLint xmax, GLint ymax,
1512 GLint *x, GLint *y,
1513 GLsizei *width, GLsizei *height )
1514 {
1515 /* left clipping */
1516 if (*x < xmin) {
1517 *width -= (xmin - *x);
1518 *x = xmin;
1519 }
1520
1521 /* right clipping */
1522 if (*x + *width > xmax)
1523 *width -= (*x + *width - xmax);
1524
1525 if (*width <= 0)
1526 return GL_FALSE;
1527
1528 /* bottom (or top) clipping */
1529 if (*y < ymin) {
1530 *height -= (ymin - *y);
1531 *y = ymin;
1532 }
1533
1534 /* top (or bottom) clipping */
1535 if (*y + *height > ymax)
1536 *height -= (*y + *height - ymax);
1537
1538 if (*height <= 0)
1539 return GL_FALSE;
1540
1541 return GL_TRUE;
1542 }
1543
1544
1545 /**
1546 * Clip dst coords against Xmax (or Ymax).
1547 */
1548 static INLINE void
1549 clip_right_or_top(GLint *srcX0, GLint *srcX1,
1550 GLint *dstX0, GLint *dstX1,
1551 GLint maxValue)
1552 {
1553 GLfloat t, bias;
1554
1555 if (*dstX1 > maxValue) {
1556 /* X1 outside right edge */
1557 ASSERT(*dstX0 < maxValue); /* X0 should be inside right edge */
1558 t = (GLfloat) (maxValue - *dstX0) / (GLfloat) (*dstX1 - *dstX0);
1559 /* chop off [t, 1] part */
1560 ASSERT(t >= 0.0 && t <= 1.0);
1561 *dstX1 = maxValue;
1562 bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F;
1563 *srcX1 = *srcX0 + (GLint) (t * (*srcX1 - *srcX0) + bias);
1564 }
1565 else if (*dstX0 > maxValue) {
1566 /* X0 outside right edge */
1567 ASSERT(*dstX1 < maxValue); /* X1 should be inside right edge */
1568 t = (GLfloat) (maxValue - *dstX1) / (GLfloat) (*dstX0 - *dstX1);
1569 /* chop off [t, 1] part */
1570 ASSERT(t >= 0.0 && t <= 1.0);
1571 *dstX0 = maxValue;
1572 bias = (*srcX0 < *srcX1) ? -0.5F : 0.5F;
1573 *srcX0 = *srcX1 + (GLint) (t * (*srcX0 - *srcX1) + bias);
1574 }
1575 }
1576
1577
1578 /**
1579 * Clip dst coords against Xmin (or Ymin).
1580 */
1581 static INLINE void
1582 clip_left_or_bottom(GLint *srcX0, GLint *srcX1,
1583 GLint *dstX0, GLint *dstX1,
1584 GLint minValue)
1585 {
1586 GLfloat t, bias;
1587
1588 if (*dstX0 < minValue) {
1589 /* X0 outside left edge */
1590 ASSERT(*dstX1 > minValue); /* X1 should be inside left edge */
1591 t = (GLfloat) (minValue - *dstX0) / (GLfloat) (*dstX1 - *dstX0);
1592 /* chop off [0, t] part */
1593 ASSERT(t >= 0.0 && t <= 1.0);
1594 *dstX0 = minValue;
1595 bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F; /* flipped??? */
1596 *srcX0 = *srcX0 + (GLint) (t * (*srcX1 - *srcX0) + bias);
1597 }
1598 else if (*dstX1 < minValue) {
1599 /* X1 outside left edge */
1600 ASSERT(*dstX0 > minValue); /* X0 should be inside left edge */
1601 t = (GLfloat) (minValue - *dstX1) / (GLfloat) (*dstX0 - *dstX1);
1602 /* chop off [0, t] part */
1603 ASSERT(t >= 0.0 && t <= 1.0);
1604 *dstX1 = minValue;
1605 bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F;
1606 *srcX1 = *srcX1 + (GLint) (t * (*srcX0 - *srcX1) + bias);
1607 }
1608 }
1609
1610
1611 /**
1612 * Do clipping of blit src/dest rectangles.
1613 * The dest rect is clipped against both the buffer bounds and scissor bounds.
1614 * The src rect is just clipped against the buffer bounds.
1615 *
1616 * When either the src or dest rect is clipped, the other is also clipped
1617 * proportionately!
1618 *
1619 * Note that X0 need not be less than X1 (same for Y) for either the source
1620 * and dest rects. That makes the clipping a little trickier.
1621 *
1622 * \return GL_TRUE if anything is left to draw, GL_FALSE if totally clipped
1623 */
1624 GLboolean
1625 _mesa_clip_blit(struct gl_context *ctx,
1626 GLint *srcX0, GLint *srcY0, GLint *srcX1, GLint *srcY1,
1627 GLint *dstX0, GLint *dstY0, GLint *dstX1, GLint *dstY1)
1628 {
1629 const GLint srcXmin = 0;
1630 const GLint srcXmax = ctx->ReadBuffer->Width;
1631 const GLint srcYmin = 0;
1632 const GLint srcYmax = ctx->ReadBuffer->Height;
1633
1634 /* these include scissor bounds */
1635 const GLint dstXmin = ctx->DrawBuffer->_Xmin;
1636 const GLint dstXmax = ctx->DrawBuffer->_Xmax;
1637 const GLint dstYmin = ctx->DrawBuffer->_Ymin;
1638 const GLint dstYmax = ctx->DrawBuffer->_Ymax;
1639
1640 /*
1641 printf("PreClipX: src: %d .. %d dst: %d .. %d\n",
1642 *srcX0, *srcX1, *dstX0, *dstX1);
1643 printf("PreClipY: src: %d .. %d dst: %d .. %d\n",
1644 *srcY0, *srcY1, *dstY0, *dstY1);
1645 */
1646
1647 /* trivial rejection tests */
1648 if (*dstX0 == *dstX1)
1649 return GL_FALSE; /* no width */
1650 if (*dstX0 <= dstXmin && *dstX1 <= dstXmin)
1651 return GL_FALSE; /* totally out (left) of bounds */
1652 if (*dstX0 >= dstXmax && *dstX1 >= dstXmax)
1653 return GL_FALSE; /* totally out (right) of bounds */
1654
1655 if (*dstY0 == *dstY1)
1656 return GL_FALSE;
1657 if (*dstY0 <= dstYmin && *dstY1 <= dstYmin)
1658 return GL_FALSE;
1659 if (*dstY0 >= dstYmax && *dstY1 >= dstYmax)
1660 return GL_FALSE;
1661
1662 if (*srcX0 == *srcX1)
1663 return GL_FALSE;
1664 if (*srcX0 <= srcXmin && *srcX1 <= srcXmin)
1665 return GL_FALSE;
1666 if (*srcX0 >= srcXmax && *srcX1 >= srcXmax)
1667 return GL_FALSE;
1668
1669 if (*srcY0 == *srcY1)
1670 return GL_FALSE;
1671 if (*srcY0 <= srcYmin && *srcY1 <= srcYmin)
1672 return GL_FALSE;
1673 if (*srcY0 >= srcYmax && *srcY1 >= srcYmax)
1674 return GL_FALSE;
1675
1676 /*
1677 * dest clip
1678 */
1679 clip_right_or_top(srcX0, srcX1, dstX0, dstX1, dstXmax);
1680 clip_right_or_top(srcY0, srcY1, dstY0, dstY1, dstYmax);
1681 clip_left_or_bottom(srcX0, srcX1, dstX0, dstX1, dstXmin);
1682 clip_left_or_bottom(srcY0, srcY1, dstY0, dstY1, dstYmin);
1683
1684 /*
1685 * src clip (just swap src/dst values from above)
1686 */
1687 clip_right_or_top(dstX0, dstX1, srcX0, srcX1, srcXmax);
1688 clip_right_or_top(dstY0, dstY1, srcY0, srcY1, srcYmax);
1689 clip_left_or_bottom(dstX0, dstX1, srcX0, srcX1, srcXmin);
1690 clip_left_or_bottom(dstY0, dstY1, srcY0, srcY1, srcYmin);
1691
1692 /*
1693 printf("PostClipX: src: %d .. %d dst: %d .. %d\n",
1694 *srcX0, *srcX1, *dstX0, *dstX1);
1695 printf("PostClipY: src: %d .. %d dst: %d .. %d\n",
1696 *srcY0, *srcY1, *dstY0, *dstY1);
1697 */
1698
1699 ASSERT(*dstX0 >= dstXmin);
1700 ASSERT(*dstX0 <= dstXmax);
1701 ASSERT(*dstX1 >= dstXmin);
1702 ASSERT(*dstX1 <= dstXmax);
1703
1704 ASSERT(*dstY0 >= dstYmin);
1705 ASSERT(*dstY0 <= dstYmax);
1706 ASSERT(*dstY1 >= dstYmin);
1707 ASSERT(*dstY1 <= dstYmax);
1708
1709 ASSERT(*srcX0 >= srcXmin);
1710 ASSERT(*srcX0 <= srcXmax);
1711 ASSERT(*srcX1 >= srcXmin);
1712 ASSERT(*srcX1 <= srcXmax);
1713
1714 ASSERT(*srcY0 >= srcYmin);
1715 ASSERT(*srcY0 <= srcYmax);
1716 ASSERT(*srcY1 >= srcYmin);
1717 ASSERT(*srcY1 <= srcYmax);
1718
1719 return GL_TRUE;
1720 }