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