mesa: add const qualifier to _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(const struct gl_context *ctx,
372 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
523 /* integer-valued formats */
524 case GL_RED_INTEGER_EXT:
525 case GL_GREEN_INTEGER_EXT:
526 case GL_BLUE_INTEGER_EXT:
527 case GL_ALPHA_INTEGER_EXT:
528 switch (type) {
529 case GL_BYTE:
530 case GL_UNSIGNED_BYTE:
531 case GL_SHORT:
532 case GL_UNSIGNED_SHORT:
533 case GL_INT:
534 case GL_UNSIGNED_INT:
535 return ctx->Extensions.EXT_texture_integer;
536 default:
537 return GL_FALSE;
538 }
539
540 case GL_RGB_INTEGER_EXT:
541 switch (type) {
542 case GL_BYTE:
543 case GL_UNSIGNED_BYTE:
544 case GL_SHORT:
545 case GL_UNSIGNED_SHORT:
546 case GL_INT:
547 case GL_UNSIGNED_INT:
548 case GL_UNSIGNED_BYTE_3_3_2:
549 case GL_UNSIGNED_BYTE_2_3_3_REV:
550 case GL_UNSIGNED_SHORT_5_6_5:
551 case GL_UNSIGNED_SHORT_5_6_5_REV:
552 return ctx->Extensions.EXT_texture_integer;
553 default:
554 return GL_FALSE;
555 }
556
557 case GL_BGR_INTEGER_EXT:
558 switch (type) {
559 case GL_BYTE:
560 case GL_UNSIGNED_BYTE:
561 case GL_SHORT:
562 case GL_UNSIGNED_SHORT:
563 case GL_INT:
564 case GL_UNSIGNED_INT:
565 /* NOTE: no packed formats w/ BGR format */
566 return ctx->Extensions.EXT_texture_integer;
567 default:
568 return GL_FALSE;
569 }
570
571 case GL_RGBA_INTEGER_EXT:
572 case GL_BGRA_INTEGER_EXT:
573 switch (type) {
574 case GL_BYTE:
575 case GL_UNSIGNED_BYTE:
576 case GL_SHORT:
577 case GL_UNSIGNED_SHORT:
578 case GL_INT:
579 case GL_UNSIGNED_INT:
580 case GL_FLOAT:
581 case GL_UNSIGNED_SHORT_4_4_4_4:
582 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
583 case GL_UNSIGNED_SHORT_5_5_5_1:
584 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
585 case GL_UNSIGNED_INT_8_8_8_8:
586 case GL_UNSIGNED_INT_8_8_8_8_REV:
587 case GL_UNSIGNED_INT_10_10_10_2:
588 case GL_UNSIGNED_INT_2_10_10_10_REV:
589 return ctx->Extensions.EXT_texture_integer;
590 default:
591 return GL_FALSE;
592 }
593
594 case GL_LUMINANCE_INTEGER_EXT:
595 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
596 switch (type) {
597 case GL_BYTE:
598 case GL_UNSIGNED_BYTE:
599 case GL_SHORT:
600 case GL_UNSIGNED_SHORT:
601 case GL_INT:
602 case GL_UNSIGNED_INT:
603 return ctx->Extensions.EXT_texture_integer;
604 default:
605 return GL_FALSE;
606 }
607
608 default:
609 ; /* fall-through */
610 }
611 return GL_FALSE;
612 }
613
614
615 /**
616 * Test if the given image format is a color/RGBA format (i.e., not color
617 * index, depth, stencil, etc).
618 * \param format the image format value (may by an internal texture format)
619 * \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise.
620 */
621 GLboolean
622 _mesa_is_color_format(GLenum format)
623 {
624 switch (format) {
625 case GL_RED:
626 case GL_GREEN:
627 case GL_BLUE:
628 case GL_ALPHA:
629 case GL_ALPHA4:
630 case GL_ALPHA8:
631 case GL_ALPHA12:
632 case GL_ALPHA16:
633 case 1:
634 case GL_LUMINANCE:
635 case GL_LUMINANCE4:
636 case GL_LUMINANCE8:
637 case GL_LUMINANCE12:
638 case GL_LUMINANCE16:
639 case 2:
640 case GL_LUMINANCE_ALPHA:
641 case GL_LUMINANCE4_ALPHA4:
642 case GL_LUMINANCE6_ALPHA2:
643 case GL_LUMINANCE8_ALPHA8:
644 case GL_LUMINANCE12_ALPHA4:
645 case GL_LUMINANCE12_ALPHA12:
646 case GL_LUMINANCE16_ALPHA16:
647 case GL_INTENSITY:
648 case GL_INTENSITY4:
649 case GL_INTENSITY8:
650 case GL_INTENSITY12:
651 case GL_INTENSITY16:
652 case GL_R8:
653 case GL_R16:
654 case GL_RG:
655 case GL_RG8:
656 case GL_RG16:
657 case 3:
658 case GL_RGB:
659 case GL_BGR:
660 case GL_R3_G3_B2:
661 case GL_RGB4:
662 case GL_RGB5:
663 case GL_RGB8:
664 case GL_RGB10:
665 case GL_RGB12:
666 case GL_RGB16:
667 case 4:
668 case GL_ABGR_EXT:
669 case GL_RGBA:
670 case GL_BGRA:
671 case GL_RGBA2:
672 case GL_RGBA4:
673 case GL_RGB5_A1:
674 case GL_RGBA8:
675 case GL_RGB10_A2:
676 case GL_RGBA12:
677 case GL_RGBA16:
678 /* float texture formats */
679 case GL_ALPHA16F_ARB:
680 case GL_ALPHA32F_ARB:
681 case GL_LUMINANCE16F_ARB:
682 case GL_LUMINANCE32F_ARB:
683 case GL_LUMINANCE_ALPHA16F_ARB:
684 case GL_LUMINANCE_ALPHA32F_ARB:
685 case GL_INTENSITY16F_ARB:
686 case GL_INTENSITY32F_ARB:
687 case GL_R16F:
688 case GL_R32F:
689 case GL_RG16F:
690 case GL_RG32F:
691 case GL_RGB16F_ARB:
692 case GL_RGB32F_ARB:
693 case GL_RGBA16F_ARB:
694 case GL_RGBA32F_ARB:
695 /* compressed formats */
696 case GL_COMPRESSED_ALPHA:
697 case GL_COMPRESSED_LUMINANCE:
698 case GL_COMPRESSED_LUMINANCE_ALPHA:
699 case GL_COMPRESSED_INTENSITY:
700 case GL_COMPRESSED_RED:
701 case GL_COMPRESSED_RG:
702 case GL_COMPRESSED_RGB:
703 case GL_COMPRESSED_RGBA:
704 case GL_RGB_S3TC:
705 case GL_RGB4_S3TC:
706 case GL_RGBA_S3TC:
707 case GL_RGBA4_S3TC:
708 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
709 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
710 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
711 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
712 case GL_COMPRESSED_RGB_FXT1_3DFX:
713 case GL_COMPRESSED_RGBA_FXT1_3DFX:
714 #if FEATURE_EXT_texture_sRGB
715 case GL_SRGB_EXT:
716 case GL_SRGB8_EXT:
717 case GL_SRGB_ALPHA_EXT:
718 case GL_SRGB8_ALPHA8_EXT:
719 case GL_SLUMINANCE_ALPHA_EXT:
720 case GL_SLUMINANCE8_ALPHA8_EXT:
721 case GL_SLUMINANCE_EXT:
722 case GL_SLUMINANCE8_EXT:
723 case GL_COMPRESSED_SRGB_EXT:
724 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
725 case GL_COMPRESSED_SRGB_ALPHA_EXT:
726 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
727 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
728 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
729 case GL_COMPRESSED_SLUMINANCE_EXT:
730 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT:
731 #endif /* FEATURE_EXT_texture_sRGB */
732 case GL_COMPRESSED_RED_RGTC1:
733 case GL_COMPRESSED_SIGNED_RED_RGTC1:
734 case GL_COMPRESSED_RG_RGTC2:
735 case GL_COMPRESSED_SIGNED_RG_RGTC2:
736 /* signed, normalized texture formats */
737 case GL_RGBA_SNORM:
738 case GL_RGBA8_SNORM:
739 /* generic integer formats */
740 case GL_RED_INTEGER_EXT:
741 case GL_GREEN_INTEGER_EXT:
742 case GL_BLUE_INTEGER_EXT:
743 case GL_ALPHA_INTEGER_EXT:
744 case GL_RGB_INTEGER_EXT:
745 case GL_RGBA_INTEGER_EXT:
746 case GL_BGR_INTEGER_EXT:
747 case GL_BGRA_INTEGER_EXT:
748 case GL_LUMINANCE_INTEGER_EXT:
749 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
750 /* sized integer formats */
751 case GL_RGBA32UI_EXT:
752 case GL_RGB32UI_EXT:
753 case GL_ALPHA32UI_EXT:
754 case GL_INTENSITY32UI_EXT:
755 case GL_LUMINANCE32UI_EXT:
756 case GL_LUMINANCE_ALPHA32UI_EXT:
757 case GL_RGBA16UI_EXT:
758 case GL_RGB16UI_EXT:
759 case GL_ALPHA16UI_EXT:
760 case GL_INTENSITY16UI_EXT:
761 case GL_LUMINANCE16UI_EXT:
762 case GL_LUMINANCE_ALPHA16UI_EXT:
763 case GL_RGBA8UI_EXT:
764 case GL_RGB8UI_EXT:
765 case GL_ALPHA8UI_EXT:
766 case GL_INTENSITY8UI_EXT:
767 case GL_LUMINANCE8UI_EXT:
768 case GL_LUMINANCE_ALPHA8UI_EXT:
769 case GL_RGBA32I_EXT:
770 case GL_RGB32I_EXT:
771 case GL_ALPHA32I_EXT:
772 case GL_INTENSITY32I_EXT:
773 case GL_LUMINANCE32I_EXT:
774 case GL_LUMINANCE_ALPHA32I_EXT:
775 case GL_RGBA16I_EXT:
776 case GL_RGB16I_EXT:
777 case GL_ALPHA16I_EXT:
778 case GL_INTENSITY16I_EXT:
779 case GL_LUMINANCE16I_EXT:
780 case GL_LUMINANCE_ALPHA16I_EXT:
781 case GL_RGBA8I_EXT:
782 case GL_RGB8I_EXT:
783 case GL_ALPHA8I_EXT:
784 case GL_INTENSITY8I_EXT:
785 case GL_LUMINANCE8I_EXT:
786 case GL_LUMINANCE_ALPHA8I_EXT:
787 return GL_TRUE;
788 case GL_YCBCR_MESA: /* not considered to be RGB */
789 /* fall-through */
790 default:
791 return GL_FALSE;
792 }
793 }
794
795
796 /**
797 * Test if the given image format is a color index format.
798 */
799 GLboolean
800 _mesa_is_index_format(GLenum format)
801 {
802 switch (format) {
803 case GL_COLOR_INDEX:
804 case GL_COLOR_INDEX1_EXT:
805 case GL_COLOR_INDEX2_EXT:
806 case GL_COLOR_INDEX4_EXT:
807 case GL_COLOR_INDEX8_EXT:
808 case GL_COLOR_INDEX12_EXT:
809 case GL_COLOR_INDEX16_EXT:
810 return GL_TRUE;
811 default:
812 return GL_FALSE;
813 }
814 }
815
816
817 /**
818 * Test if the given image format is a depth component format.
819 */
820 GLboolean
821 _mesa_is_depth_format(GLenum format)
822 {
823 switch (format) {
824 case GL_DEPTH_COMPONENT:
825 case GL_DEPTH_COMPONENT16:
826 case GL_DEPTH_COMPONENT24:
827 case GL_DEPTH_COMPONENT32:
828 return GL_TRUE;
829 default:
830 return GL_FALSE;
831 }
832 }
833
834
835 /**
836 * Test if the given image format is a stencil format.
837 */
838 GLboolean
839 _mesa_is_stencil_format(GLenum format)
840 {
841 switch (format) {
842 case GL_STENCIL_INDEX:
843 case GL_DEPTH_STENCIL:
844 return GL_TRUE;
845 default:
846 return GL_FALSE;
847 }
848 }
849
850
851 /**
852 * Test if the given image format is a YCbCr format.
853 */
854 GLboolean
855 _mesa_is_ycbcr_format(GLenum format)
856 {
857 switch (format) {
858 case GL_YCBCR_MESA:
859 return GL_TRUE;
860 default:
861 return GL_FALSE;
862 }
863 }
864
865
866 /**
867 * Test if the given image format is a depth+stencil format.
868 */
869 GLboolean
870 _mesa_is_depthstencil_format(GLenum format)
871 {
872 switch (format) {
873 case GL_DEPTH24_STENCIL8_EXT:
874 case GL_DEPTH_STENCIL_EXT:
875 return GL_TRUE;
876 default:
877 return GL_FALSE;
878 }
879 }
880
881
882 /**
883 * Test if the given image format is a depth or stencil format.
884 */
885 GLboolean
886 _mesa_is_depth_or_stencil_format(GLenum format)
887 {
888 switch (format) {
889 case GL_DEPTH_COMPONENT:
890 case GL_DEPTH_COMPONENT16:
891 case GL_DEPTH_COMPONENT24:
892 case GL_DEPTH_COMPONENT32:
893 case GL_STENCIL_INDEX:
894 case GL_STENCIL_INDEX1_EXT:
895 case GL_STENCIL_INDEX4_EXT:
896 case GL_STENCIL_INDEX8_EXT:
897 case GL_STENCIL_INDEX16_EXT:
898 case GL_DEPTH_STENCIL_EXT:
899 case GL_DEPTH24_STENCIL8_EXT:
900 return GL_TRUE;
901 default:
902 return GL_FALSE;
903 }
904 }
905
906
907 /**
908 * Test if the given image format is a dudv format.
909 */
910 GLboolean
911 _mesa_is_dudv_format(GLenum format)
912 {
913 switch (format) {
914 case GL_DUDV_ATI:
915 case GL_DU8DV8_ATI:
916 return GL_TRUE;
917 default:
918 return GL_FALSE;
919 }
920 }
921
922
923 /**
924 * Test if the given format is an integer (non-normalized) format.
925 */
926 GLboolean
927 _mesa_is_integer_format(GLenum format)
928 {
929 switch (format) {
930 /* generic integer formats */
931 case GL_RED_INTEGER_EXT:
932 case GL_GREEN_INTEGER_EXT:
933 case GL_BLUE_INTEGER_EXT:
934 case GL_ALPHA_INTEGER_EXT:
935 case GL_RGB_INTEGER_EXT:
936 case GL_RGBA_INTEGER_EXT:
937 case GL_BGR_INTEGER_EXT:
938 case GL_BGRA_INTEGER_EXT:
939 case GL_LUMINANCE_INTEGER_EXT:
940 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
941 /* specific integer formats */
942 case GL_RGBA32UI_EXT:
943 case GL_RGB32UI_EXT:
944 case GL_ALPHA32UI_EXT:
945 case GL_INTENSITY32UI_EXT:
946 case GL_LUMINANCE32UI_EXT:
947 case GL_LUMINANCE_ALPHA32UI_EXT:
948 case GL_RGBA16UI_EXT:
949 case GL_RGB16UI_EXT:
950 case GL_ALPHA16UI_EXT:
951 case GL_INTENSITY16UI_EXT:
952 case GL_LUMINANCE16UI_EXT:
953 case GL_LUMINANCE_ALPHA16UI_EXT:
954 case GL_RGBA8UI_EXT:
955 case GL_RGB8UI_EXT:
956 case GL_ALPHA8UI_EXT:
957 case GL_INTENSITY8UI_EXT:
958 case GL_LUMINANCE8UI_EXT:
959 case GL_LUMINANCE_ALPHA8UI_EXT:
960 case GL_RGBA32I_EXT:
961 case GL_RGB32I_EXT:
962 case GL_ALPHA32I_EXT:
963 case GL_INTENSITY32I_EXT:
964 case GL_LUMINANCE32I_EXT:
965 case GL_LUMINANCE_ALPHA32I_EXT:
966 case GL_RGBA16I_EXT:
967 case GL_RGB16I_EXT:
968 case GL_ALPHA16I_EXT:
969 case GL_INTENSITY16I_EXT:
970 case GL_LUMINANCE16I_EXT:
971 case GL_LUMINANCE_ALPHA16I_EXT:
972 case GL_RGBA8I_EXT:
973 case GL_RGB8I_EXT:
974 case GL_ALPHA8I_EXT:
975 case GL_INTENSITY8I_EXT:
976 case GL_LUMINANCE8I_EXT:
977 case GL_LUMINANCE_ALPHA8I_EXT:
978 return GL_TRUE;
979 default:
980 return GL_FALSE;
981 }
982 }
983
984
985 /**
986 * Test if an image format is a supported compressed format.
987 * \param format the internal format token provided by the user.
988 * \return GL_TRUE if compressed, GL_FALSE if uncompressed
989 */
990 GLboolean
991 _mesa_is_compressed_format(struct gl_context *ctx, GLenum format)
992 {
993 switch (format) {
994 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
995 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
996 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
997 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
998 return ctx->Extensions.EXT_texture_compression_s3tc;
999 case GL_RGB_S3TC:
1000 case GL_RGB4_S3TC:
1001 case GL_RGBA_S3TC:
1002 case GL_RGBA4_S3TC:
1003 return ctx->Extensions.S3_s3tc;
1004 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
1005 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
1006 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
1007 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
1008 return ctx->Extensions.EXT_texture_sRGB
1009 && ctx->Extensions.EXT_texture_compression_s3tc;
1010 case GL_COMPRESSED_RGB_FXT1_3DFX:
1011 case GL_COMPRESSED_RGBA_FXT1_3DFX:
1012 return ctx->Extensions.TDFX_texture_compression_FXT1;
1013 case GL_COMPRESSED_RED_RGTC1:
1014 case GL_COMPRESSED_SIGNED_RED_RGTC1:
1015 case GL_COMPRESSED_RG_RGTC2:
1016 case GL_COMPRESSED_SIGNED_RG_RGTC2:
1017 return ctx->Extensions.ARB_texture_compression_rgtc;
1018 default:
1019 return GL_FALSE;
1020 }
1021 }
1022
1023
1024 /**
1025 * Return the address of a specific pixel in an image (1D, 2D or 3D).
1026 *
1027 * Pixel unpacking/packing parameters are observed according to \p packing.
1028 *
1029 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
1030 * \param image starting address of image data
1031 * \param width the image width
1032 * \param height theimage height
1033 * \param format the pixel format
1034 * \param type the pixel data type
1035 * \param packing the pixelstore attributes
1036 * \param img which image in the volume (0 for 1D or 2D images)
1037 * \param row row of pixel in the image (0 for 1D images)
1038 * \param column column of pixel in the image
1039 *
1040 * \return address of pixel on success, or NULL on error.
1041 *
1042 * \sa gl_pixelstore_attrib.
1043 */
1044 GLvoid *
1045 _mesa_image_address( GLuint dimensions,
1046 const struct gl_pixelstore_attrib *packing,
1047 const GLvoid *image,
1048 GLsizei width, GLsizei height,
1049 GLenum format, GLenum type,
1050 GLint img, GLint row, GLint column )
1051 {
1052 GLint alignment; /* 1, 2 or 4 */
1053 GLint pixels_per_row;
1054 GLint rows_per_image;
1055 GLint skiprows;
1056 GLint skippixels;
1057 GLint skipimages; /* for 3-D volume images */
1058 GLubyte *pixel_addr;
1059
1060 ASSERT(dimensions >= 1 && dimensions <= 3);
1061
1062 alignment = packing->Alignment;
1063 if (packing->RowLength > 0) {
1064 pixels_per_row = packing->RowLength;
1065 }
1066 else {
1067 pixels_per_row = width;
1068 }
1069 if (packing->ImageHeight > 0) {
1070 rows_per_image = packing->ImageHeight;
1071 }
1072 else {
1073 rows_per_image = height;
1074 }
1075
1076 skippixels = packing->SkipPixels;
1077 /* Note: SKIP_ROWS _is_ used for 1D images */
1078 skiprows = packing->SkipRows;
1079 /* Note: SKIP_IMAGES is only used for 3D images */
1080 skipimages = (dimensions == 3) ? packing->SkipImages : 0;
1081
1082 if (type == GL_BITMAP) {
1083 /* BITMAP data */
1084 GLint comp_per_pixel; /* components per pixel */
1085 GLint bytes_per_comp; /* bytes per component */
1086 GLint bytes_per_row;
1087 GLint bytes_per_image;
1088
1089 /* Compute bytes per component */
1090 bytes_per_comp = _mesa_sizeof_packed_type( type );
1091 if (bytes_per_comp < 0) {
1092 return NULL;
1093 }
1094
1095 /* Compute number of components per pixel */
1096 comp_per_pixel = _mesa_components_in_format( format );
1097 if (comp_per_pixel < 0) {
1098 return NULL;
1099 }
1100
1101 bytes_per_row = alignment
1102 * CEILING( comp_per_pixel*pixels_per_row, 8*alignment );
1103
1104 bytes_per_image = bytes_per_row * rows_per_image;
1105
1106 pixel_addr = (GLubyte *) image
1107 + (skipimages + img) * bytes_per_image
1108 + (skiprows + row) * bytes_per_row
1109 + (skippixels + column) / 8;
1110 }
1111 else {
1112 /* Non-BITMAP data */
1113 GLint bytes_per_pixel, bytes_per_row, remainder, bytes_per_image;
1114 GLint topOfImage;
1115
1116 bytes_per_pixel = _mesa_bytes_per_pixel( format, type );
1117
1118 /* The pixel type and format should have been error checked earlier */
1119 assert(bytes_per_pixel > 0);
1120
1121 bytes_per_row = pixels_per_row * bytes_per_pixel;
1122 remainder = bytes_per_row % alignment;
1123 if (remainder > 0)
1124 bytes_per_row += (alignment - remainder);
1125
1126 ASSERT(bytes_per_row % alignment == 0);
1127
1128 bytes_per_image = bytes_per_row * rows_per_image;
1129
1130 if (packing->Invert) {
1131 /* set pixel_addr to the last row */
1132 topOfImage = bytes_per_row * (height - 1);
1133 bytes_per_row = -bytes_per_row;
1134 }
1135 else {
1136 topOfImage = 0;
1137 }
1138
1139 /* compute final pixel address */
1140 pixel_addr = (GLubyte *) image
1141 + (skipimages + img) * bytes_per_image
1142 + topOfImage
1143 + (skiprows + row) * bytes_per_row
1144 + (skippixels + column) * bytes_per_pixel;
1145 }
1146
1147 return (GLvoid *) pixel_addr;
1148 }
1149
1150
1151 GLvoid *
1152 _mesa_image_address1d( const struct gl_pixelstore_attrib *packing,
1153 const GLvoid *image,
1154 GLsizei width,
1155 GLenum format, GLenum type,
1156 GLint column )
1157 {
1158 return _mesa_image_address(1, packing, image, width, 1,
1159 format, type, 0, 0, column);
1160 }
1161
1162
1163 GLvoid *
1164 _mesa_image_address2d( const struct gl_pixelstore_attrib *packing,
1165 const GLvoid *image,
1166 GLsizei width, GLsizei height,
1167 GLenum format, GLenum type,
1168 GLint row, GLint column )
1169 {
1170 return _mesa_image_address(2, packing, image, width, height,
1171 format, type, 0, row, column);
1172 }
1173
1174
1175 GLvoid *
1176 _mesa_image_address3d( const struct gl_pixelstore_attrib *packing,
1177 const GLvoid *image,
1178 GLsizei width, GLsizei height,
1179 GLenum format, GLenum type,
1180 GLint img, GLint row, GLint column )
1181 {
1182 return _mesa_image_address(3, packing, image, width, height,
1183 format, type, img, row, column);
1184 }
1185
1186
1187
1188 /**
1189 * Compute the stride (in bytes) between image rows.
1190 *
1191 * \param packing the pixelstore attributes
1192 * \param width image width.
1193 * \param format pixel format.
1194 * \param type pixel data type.
1195 *
1196 * \return the stride in bytes for the given parameters, or -1 if error
1197 */
1198 GLint
1199 _mesa_image_row_stride( const struct gl_pixelstore_attrib *packing,
1200 GLint width, GLenum format, GLenum type )
1201 {
1202 GLint bytesPerRow, remainder;
1203
1204 ASSERT(packing);
1205
1206 if (type == GL_BITMAP) {
1207 if (packing->RowLength == 0) {
1208 bytesPerRow = (width + 7) / 8;
1209 }
1210 else {
1211 bytesPerRow = (packing->RowLength + 7) / 8;
1212 }
1213 }
1214 else {
1215 /* Non-BITMAP data */
1216 const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
1217 if (bytesPerPixel <= 0)
1218 return -1; /* error */
1219 if (packing->RowLength == 0) {
1220 bytesPerRow = bytesPerPixel * width;
1221 }
1222 else {
1223 bytesPerRow = bytesPerPixel * packing->RowLength;
1224 }
1225 }
1226
1227 remainder = bytesPerRow % packing->Alignment;
1228 if (remainder > 0) {
1229 bytesPerRow += (packing->Alignment - remainder);
1230 }
1231
1232 if (packing->Invert) {
1233 /* negate the bytes per row (negative row stride) */
1234 bytesPerRow = -bytesPerRow;
1235 }
1236
1237 return bytesPerRow;
1238 }
1239
1240
1241 /*
1242 * Compute the stride between images in a 3D texture (in bytes) for the given
1243 * pixel packing parameters and image width, format and type.
1244 */
1245 GLint
1246 _mesa_image_image_stride( const struct gl_pixelstore_attrib *packing,
1247 GLint width, GLint height,
1248 GLenum format, GLenum type )
1249 {
1250 GLint bytesPerRow, bytesPerImage, remainder;
1251
1252 ASSERT(packing);
1253
1254 if (type == GL_BITMAP) {
1255 if (packing->RowLength == 0) {
1256 bytesPerRow = (width + 7) / 8;
1257 }
1258 else {
1259 bytesPerRow = (packing->RowLength + 7) / 8;
1260 }
1261 }
1262 else {
1263 const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
1264
1265 if (bytesPerPixel <= 0)
1266 return -1; /* error */
1267 if (packing->RowLength == 0) {
1268 bytesPerRow = bytesPerPixel * width;
1269 }
1270 else {
1271 bytesPerRow = bytesPerPixel * packing->RowLength;
1272 }
1273 }
1274
1275 remainder = bytesPerRow % packing->Alignment;
1276 if (remainder > 0)
1277 bytesPerRow += (packing->Alignment - remainder);
1278
1279 if (packing->ImageHeight == 0)
1280 bytesPerImage = bytesPerRow * height;
1281 else
1282 bytesPerImage = bytesPerRow * packing->ImageHeight;
1283
1284 return bytesPerImage;
1285 }
1286
1287
1288
1289 /**
1290 * "Expand" a bitmap from 1-bit per pixel to 8-bits per pixel.
1291 * This is typically used to convert a bitmap into a GLubyte/pixel texture.
1292 * "On" bits will set texels to \p onValue.
1293 * "Off" bits will not modify texels.
1294 * \param width src bitmap width in pixels
1295 * \param height src bitmap height in pixels
1296 * \param unpack bitmap unpacking state
1297 * \param bitmap the src bitmap data
1298 * \param destBuffer start of dest buffer
1299 * \param destStride row stride in dest buffer
1300 * \param onValue if bit is 1, set destBuffer pixel to this value
1301 */
1302 void
1303 _mesa_expand_bitmap(GLsizei width, GLsizei height,
1304 const struct gl_pixelstore_attrib *unpack,
1305 const GLubyte *bitmap,
1306 GLubyte *destBuffer, GLint destStride,
1307 GLubyte onValue)
1308 {
1309 const GLubyte *srcRow = (const GLubyte *)
1310 _mesa_image_address2d(unpack, bitmap, width, height,
1311 GL_COLOR_INDEX, GL_BITMAP, 0, 0);
1312 const GLint srcStride = _mesa_image_row_stride(unpack, width,
1313 GL_COLOR_INDEX, GL_BITMAP);
1314 GLint row, col;
1315
1316 #define SET_PIXEL(COL, ROW) \
1317 destBuffer[(ROW) * destStride + (COL)] = onValue;
1318
1319 for (row = 0; row < height; row++) {
1320 const GLubyte *src = srcRow;
1321
1322 if (unpack->LsbFirst) {
1323 /* Lsb first */
1324 GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
1325 for (col = 0; col < width; col++) {
1326
1327 if (*src & mask) {
1328 SET_PIXEL(col, row);
1329 }
1330
1331 if (mask == 128U) {
1332 src++;
1333 mask = 1U;
1334 }
1335 else {
1336 mask = mask << 1;
1337 }
1338 }
1339
1340 /* get ready for next row */
1341 if (mask != 1)
1342 src++;
1343 }
1344 else {
1345 /* Msb first */
1346 GLubyte mask = 128U >> (unpack->SkipPixels & 0x7);
1347 for (col = 0; col < width; col++) {
1348
1349 if (*src & mask) {
1350 SET_PIXEL(col, row);
1351 }
1352
1353 if (mask == 1U) {
1354 src++;
1355 mask = 128U;
1356 }
1357 else {
1358 mask = mask >> 1;
1359 }
1360 }
1361
1362 /* get ready for next row */
1363 if (mask != 128)
1364 src++;
1365 }
1366
1367 srcRow += srcStride;
1368 } /* row */
1369
1370 #undef SET_PIXEL
1371 }
1372
1373
1374
1375
1376 /**
1377 * Convert an array of RGBA colors from one datatype to another.
1378 * NOTE: src may equal dst. In that case, we use a temporary buffer.
1379 */
1380 void
1381 _mesa_convert_colors(GLenum srcType, const GLvoid *src,
1382 GLenum dstType, GLvoid *dst,
1383 GLuint count, const GLubyte mask[])
1384 {
1385 GLuint tempBuffer[MAX_WIDTH][4];
1386 const GLboolean useTemp = (src == dst);
1387
1388 ASSERT(srcType != dstType);
1389
1390 switch (srcType) {
1391 case GL_UNSIGNED_BYTE:
1392 if (dstType == GL_UNSIGNED_SHORT) {
1393 const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src;
1394 GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst);
1395 GLuint i;
1396 for (i = 0; i < count; i++) {
1397 if (!mask || mask[i]) {
1398 dst2[i][RCOMP] = UBYTE_TO_USHORT(src1[i][RCOMP]);
1399 dst2[i][GCOMP] = UBYTE_TO_USHORT(src1[i][GCOMP]);
1400 dst2[i][BCOMP] = UBYTE_TO_USHORT(src1[i][BCOMP]);
1401 dst2[i][ACOMP] = UBYTE_TO_USHORT(src1[i][ACOMP]);
1402 }
1403 }
1404 if (useTemp)
1405 memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort));
1406 }
1407 else {
1408 const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src;
1409 GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst);
1410 GLuint i;
1411 ASSERT(dstType == GL_FLOAT);
1412 for (i = 0; i < count; i++) {
1413 if (!mask || mask[i]) {
1414 dst4[i][RCOMP] = UBYTE_TO_FLOAT(src1[i][RCOMP]);
1415 dst4[i][GCOMP] = UBYTE_TO_FLOAT(src1[i][GCOMP]);
1416 dst4[i][BCOMP] = UBYTE_TO_FLOAT(src1[i][BCOMP]);
1417 dst4[i][ACOMP] = UBYTE_TO_FLOAT(src1[i][ACOMP]);
1418 }
1419 }
1420 if (useTemp)
1421 memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat));
1422 }
1423 break;
1424 case GL_UNSIGNED_SHORT:
1425 if (dstType == GL_UNSIGNED_BYTE) {
1426 const GLushort (*src2)[4] = (const GLushort (*)[4]) src;
1427 GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst);
1428 GLuint i;
1429 for (i = 0; i < count; i++) {
1430 if (!mask || mask[i]) {
1431 dst1[i][RCOMP] = USHORT_TO_UBYTE(src2[i][RCOMP]);
1432 dst1[i][GCOMP] = USHORT_TO_UBYTE(src2[i][GCOMP]);
1433 dst1[i][BCOMP] = USHORT_TO_UBYTE(src2[i][BCOMP]);
1434 dst1[i][ACOMP] = USHORT_TO_UBYTE(src2[i][ACOMP]);
1435 }
1436 }
1437 if (useTemp)
1438 memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte));
1439 }
1440 else {
1441 const GLushort (*src2)[4] = (const GLushort (*)[4]) src;
1442 GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst);
1443 GLuint i;
1444 ASSERT(dstType == GL_FLOAT);
1445 for (i = 0; i < count; i++) {
1446 if (!mask || mask[i]) {
1447 dst4[i][RCOMP] = USHORT_TO_FLOAT(src2[i][RCOMP]);
1448 dst4[i][GCOMP] = USHORT_TO_FLOAT(src2[i][GCOMP]);
1449 dst4[i][BCOMP] = USHORT_TO_FLOAT(src2[i][BCOMP]);
1450 dst4[i][ACOMP] = USHORT_TO_FLOAT(src2[i][ACOMP]);
1451 }
1452 }
1453 if (useTemp)
1454 memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat));
1455 }
1456 break;
1457 case GL_FLOAT:
1458 if (dstType == GL_UNSIGNED_BYTE) {
1459 const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src;
1460 GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst);
1461 GLuint i;
1462 for (i = 0; i < count; i++) {
1463 if (!mask || mask[i]) {
1464 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][RCOMP], src4[i][RCOMP]);
1465 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][GCOMP], src4[i][GCOMP]);
1466 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][BCOMP], src4[i][BCOMP]);
1467 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][ACOMP], src4[i][ACOMP]);
1468 }
1469 }
1470 if (useTemp)
1471 memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte));
1472 }
1473 else {
1474 const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src;
1475 GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst);
1476 GLuint i;
1477 ASSERT(dstType == GL_UNSIGNED_SHORT);
1478 for (i = 0; i < count; i++) {
1479 if (!mask || mask[i]) {
1480 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][RCOMP], src4[i][RCOMP]);
1481 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][GCOMP], src4[i][GCOMP]);
1482 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][BCOMP], src4[i][BCOMP]);
1483 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][ACOMP], src4[i][ACOMP]);
1484 }
1485 }
1486 if (useTemp)
1487 memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort));
1488 }
1489 break;
1490 default:
1491 _mesa_problem(NULL, "Invalid datatype in _mesa_convert_colors");
1492 }
1493 }
1494
1495
1496
1497
1498 /**
1499 * Perform basic clipping for glDrawPixels. The image's position and size
1500 * and the unpack SkipPixels and SkipRows are adjusted so that the image
1501 * region is entirely within the window and scissor bounds.
1502 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
1503 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
1504 * we'll actually write. Beforehand, *destY-1 is the first drawing row.
1505 *
1506 * \return GL_TRUE if image is ready for drawing or
1507 * GL_FALSE if image was completely clipped away (draw nothing)
1508 */
1509 GLboolean
1510 _mesa_clip_drawpixels(const struct gl_context *ctx,
1511 GLint *destX, GLint *destY,
1512 GLsizei *width, GLsizei *height,
1513 struct gl_pixelstore_attrib *unpack)
1514 {
1515 const struct gl_framebuffer *buffer = ctx->DrawBuffer;
1516
1517 if (unpack->RowLength == 0) {
1518 unpack->RowLength = *width;
1519 }
1520
1521 ASSERT(ctx->Pixel.ZoomX == 1.0F);
1522 ASSERT(ctx->Pixel.ZoomY == 1.0F || ctx->Pixel.ZoomY == -1.0F);
1523
1524 /* left clipping */
1525 if (*destX < buffer->_Xmin) {
1526 unpack->SkipPixels += (buffer->_Xmin - *destX);
1527 *width -= (buffer->_Xmin - *destX);
1528 *destX = buffer->_Xmin;
1529 }
1530 /* right clipping */
1531 if (*destX + *width > buffer->_Xmax)
1532 *width -= (*destX + *width - buffer->_Xmax);
1533
1534 if (*width <= 0)
1535 return GL_FALSE;
1536
1537 if (ctx->Pixel.ZoomY == 1.0F) {
1538 /* bottom clipping */
1539 if (*destY < buffer->_Ymin) {
1540 unpack->SkipRows += (buffer->_Ymin - *destY);
1541 *height -= (buffer->_Ymin - *destY);
1542 *destY = buffer->_Ymin;
1543 }
1544 /* top clipping */
1545 if (*destY + *height > buffer->_Ymax)
1546 *height -= (*destY + *height - buffer->_Ymax);
1547 }
1548 else { /* upside down */
1549 /* top clipping */
1550 if (*destY > buffer->_Ymax) {
1551 unpack->SkipRows += (*destY - buffer->_Ymax);
1552 *height -= (*destY - buffer->_Ymax);
1553 *destY = buffer->_Ymax;
1554 }
1555 /* bottom clipping */
1556 if (*destY - *height < buffer->_Ymin)
1557 *height -= (buffer->_Ymin - (*destY - *height));
1558 /* adjust destY so it's the first row to write to */
1559 (*destY)--;
1560 }
1561
1562 if (*height <= 0)
1563 return GL_FALSE;
1564
1565 return GL_TRUE;
1566 }
1567
1568
1569 /**
1570 * Perform clipping for glReadPixels. The image's window position
1571 * and size, and the pack skipPixels, skipRows and rowLength are adjusted
1572 * so that the image region is entirely within the window bounds.
1573 * Note: this is different from _mesa_clip_drawpixels() in that the
1574 * scissor box is ignored, and we use the bounds of the current readbuffer
1575 * surface.
1576 *
1577 * \return GL_TRUE if image is ready for drawing or
1578 * GL_FALSE if image was completely clipped away (draw nothing)
1579 */
1580 GLboolean
1581 _mesa_clip_readpixels(const struct gl_context *ctx,
1582 GLint *srcX, GLint *srcY,
1583 GLsizei *width, GLsizei *height,
1584 struct gl_pixelstore_attrib *pack)
1585 {
1586 const struct gl_framebuffer *buffer = ctx->ReadBuffer;
1587
1588 if (pack->RowLength == 0) {
1589 pack->RowLength = *width;
1590 }
1591
1592 /* left clipping */
1593 if (*srcX < 0) {
1594 pack->SkipPixels += (0 - *srcX);
1595 *width -= (0 - *srcX);
1596 *srcX = 0;
1597 }
1598 /* right clipping */
1599 if (*srcX + *width > (GLsizei) buffer->Width)
1600 *width -= (*srcX + *width - buffer->Width);
1601
1602 if (*width <= 0)
1603 return GL_FALSE;
1604
1605 /* bottom clipping */
1606 if (*srcY < 0) {
1607 pack->SkipRows += (0 - *srcY);
1608 *height -= (0 - *srcY);
1609 *srcY = 0;
1610 }
1611 /* top clipping */
1612 if (*srcY + *height > (GLsizei) buffer->Height)
1613 *height -= (*srcY + *height - buffer->Height);
1614
1615 if (*height <= 0)
1616 return GL_FALSE;
1617
1618 return GL_TRUE;
1619 }
1620
1621
1622 /**
1623 * Do clipping for a glCopyTexSubImage call.
1624 * The framebuffer source region might extend outside the framebuffer
1625 * bounds. Clip the source region against the framebuffer bounds and
1626 * adjust the texture/dest position and size accordingly.
1627 *
1628 * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise.
1629 */
1630 GLboolean
1631 _mesa_clip_copytexsubimage(const struct gl_context *ctx,
1632 GLint *destX, GLint *destY,
1633 GLint *srcX, GLint *srcY,
1634 GLsizei *width, GLsizei *height)
1635 {
1636 const struct gl_framebuffer *fb = ctx->ReadBuffer;
1637 const GLint srcX0 = *srcX, srcY0 = *srcY;
1638
1639 if (_mesa_clip_to_region(0, 0, fb->Width, fb->Height,
1640 srcX, srcY, width, height)) {
1641 *destX = *destX + *srcX - srcX0;
1642 *destY = *destY + *srcY - srcY0;
1643
1644 return GL_TRUE;
1645 }
1646 else {
1647 return GL_FALSE;
1648 }
1649 }
1650
1651
1652
1653 /**
1654 * Clip the rectangle defined by (x, y, width, height) against the bounds
1655 * specified by [xmin, xmax) and [ymin, ymax).
1656 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
1657 */
1658 GLboolean
1659 _mesa_clip_to_region(GLint xmin, GLint ymin,
1660 GLint xmax, GLint ymax,
1661 GLint *x, GLint *y,
1662 GLsizei *width, GLsizei *height )
1663 {
1664 /* left clipping */
1665 if (*x < xmin) {
1666 *width -= (xmin - *x);
1667 *x = xmin;
1668 }
1669
1670 /* right clipping */
1671 if (*x + *width > xmax)
1672 *width -= (*x + *width - xmax);
1673
1674 if (*width <= 0)
1675 return GL_FALSE;
1676
1677 /* bottom (or top) clipping */
1678 if (*y < ymin) {
1679 *height -= (ymin - *y);
1680 *y = ymin;
1681 }
1682
1683 /* top (or bottom) clipping */
1684 if (*y + *height > ymax)
1685 *height -= (*y + *height - ymax);
1686
1687 if (*height <= 0)
1688 return GL_FALSE;
1689
1690 return GL_TRUE;
1691 }
1692
1693
1694 /**
1695 * Clip dst coords against Xmax (or Ymax).
1696 */
1697 static INLINE void
1698 clip_right_or_top(GLint *srcX0, GLint *srcX1,
1699 GLint *dstX0, GLint *dstX1,
1700 GLint maxValue)
1701 {
1702 GLfloat t, bias;
1703
1704 if (*dstX1 > maxValue) {
1705 /* X1 outside right edge */
1706 ASSERT(*dstX0 < maxValue); /* X0 should be inside right edge */
1707 t = (GLfloat) (maxValue - *dstX0) / (GLfloat) (*dstX1 - *dstX0);
1708 /* chop off [t, 1] part */
1709 ASSERT(t >= 0.0 && t <= 1.0);
1710 *dstX1 = maxValue;
1711 bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F;
1712 *srcX1 = *srcX0 + (GLint) (t * (*srcX1 - *srcX0) + bias);
1713 }
1714 else if (*dstX0 > maxValue) {
1715 /* X0 outside right edge */
1716 ASSERT(*dstX1 < maxValue); /* X1 should be inside right edge */
1717 t = (GLfloat) (maxValue - *dstX1) / (GLfloat) (*dstX0 - *dstX1);
1718 /* chop off [t, 1] part */
1719 ASSERT(t >= 0.0 && t <= 1.0);
1720 *dstX0 = maxValue;
1721 bias = (*srcX0 < *srcX1) ? -0.5F : 0.5F;
1722 *srcX0 = *srcX1 + (GLint) (t * (*srcX0 - *srcX1) + bias);
1723 }
1724 }
1725
1726
1727 /**
1728 * Clip dst coords against Xmin (or Ymin).
1729 */
1730 static INLINE void
1731 clip_left_or_bottom(GLint *srcX0, GLint *srcX1,
1732 GLint *dstX0, GLint *dstX1,
1733 GLint minValue)
1734 {
1735 GLfloat t, bias;
1736
1737 if (*dstX0 < minValue) {
1738 /* X0 outside left edge */
1739 ASSERT(*dstX1 > minValue); /* X1 should be inside left edge */
1740 t = (GLfloat) (minValue - *dstX0) / (GLfloat) (*dstX1 - *dstX0);
1741 /* chop off [0, t] part */
1742 ASSERT(t >= 0.0 && t <= 1.0);
1743 *dstX0 = minValue;
1744 bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F; /* flipped??? */
1745 *srcX0 = *srcX0 + (GLint) (t * (*srcX1 - *srcX0) + bias);
1746 }
1747 else if (*dstX1 < minValue) {
1748 /* X1 outside left edge */
1749 ASSERT(*dstX0 > minValue); /* X0 should be inside left edge */
1750 t = (GLfloat) (minValue - *dstX1) / (GLfloat) (*dstX0 - *dstX1);
1751 /* chop off [0, t] part */
1752 ASSERT(t >= 0.0 && t <= 1.0);
1753 *dstX1 = minValue;
1754 bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F;
1755 *srcX1 = *srcX1 + (GLint) (t * (*srcX0 - *srcX1) + bias);
1756 }
1757 }
1758
1759
1760 /**
1761 * Do clipping of blit src/dest rectangles.
1762 * The dest rect is clipped against both the buffer bounds and scissor bounds.
1763 * The src rect is just clipped against the buffer bounds.
1764 *
1765 * When either the src or dest rect is clipped, the other is also clipped
1766 * proportionately!
1767 *
1768 * Note that X0 need not be less than X1 (same for Y) for either the source
1769 * and dest rects. That makes the clipping a little trickier.
1770 *
1771 * \return GL_TRUE if anything is left to draw, GL_FALSE if totally clipped
1772 */
1773 GLboolean
1774 _mesa_clip_blit(struct gl_context *ctx,
1775 GLint *srcX0, GLint *srcY0, GLint *srcX1, GLint *srcY1,
1776 GLint *dstX0, GLint *dstY0, GLint *dstX1, GLint *dstY1)
1777 {
1778 const GLint srcXmin = 0;
1779 const GLint srcXmax = ctx->ReadBuffer->Width;
1780 const GLint srcYmin = 0;
1781 const GLint srcYmax = ctx->ReadBuffer->Height;
1782
1783 /* these include scissor bounds */
1784 const GLint dstXmin = ctx->DrawBuffer->_Xmin;
1785 const GLint dstXmax = ctx->DrawBuffer->_Xmax;
1786 const GLint dstYmin = ctx->DrawBuffer->_Ymin;
1787 const GLint dstYmax = ctx->DrawBuffer->_Ymax;
1788
1789 /*
1790 printf("PreClipX: src: %d .. %d dst: %d .. %d\n",
1791 *srcX0, *srcX1, *dstX0, *dstX1);
1792 printf("PreClipY: src: %d .. %d dst: %d .. %d\n",
1793 *srcY0, *srcY1, *dstY0, *dstY1);
1794 */
1795
1796 /* trivial rejection tests */
1797 if (*dstX0 == *dstX1)
1798 return GL_FALSE; /* no width */
1799 if (*dstX0 <= dstXmin && *dstX1 <= dstXmin)
1800 return GL_FALSE; /* totally out (left) of bounds */
1801 if (*dstX0 >= dstXmax && *dstX1 >= dstXmax)
1802 return GL_FALSE; /* totally out (right) of bounds */
1803
1804 if (*dstY0 == *dstY1)
1805 return GL_FALSE;
1806 if (*dstY0 <= dstYmin && *dstY1 <= dstYmin)
1807 return GL_FALSE;
1808 if (*dstY0 >= dstYmax && *dstY1 >= dstYmax)
1809 return GL_FALSE;
1810
1811 if (*srcX0 == *srcX1)
1812 return GL_FALSE;
1813 if (*srcX0 <= srcXmin && *srcX1 <= srcXmin)
1814 return GL_FALSE;
1815 if (*srcX0 >= srcXmax && *srcX1 >= srcXmax)
1816 return GL_FALSE;
1817
1818 if (*srcY0 == *srcY1)
1819 return GL_FALSE;
1820 if (*srcY0 <= srcYmin && *srcY1 <= srcYmin)
1821 return GL_FALSE;
1822 if (*srcY0 >= srcYmax && *srcY1 >= srcYmax)
1823 return GL_FALSE;
1824
1825 /*
1826 * dest clip
1827 */
1828 clip_right_or_top(srcX0, srcX1, dstX0, dstX1, dstXmax);
1829 clip_right_or_top(srcY0, srcY1, dstY0, dstY1, dstYmax);
1830 clip_left_or_bottom(srcX0, srcX1, dstX0, dstX1, dstXmin);
1831 clip_left_or_bottom(srcY0, srcY1, dstY0, dstY1, dstYmin);
1832
1833 /*
1834 * src clip (just swap src/dst values from above)
1835 */
1836 clip_right_or_top(dstX0, dstX1, srcX0, srcX1, srcXmax);
1837 clip_right_or_top(dstY0, dstY1, srcY0, srcY1, srcYmax);
1838 clip_left_or_bottom(dstX0, dstX1, srcX0, srcX1, srcXmin);
1839 clip_left_or_bottom(dstY0, dstY1, srcY0, srcY1, srcYmin);
1840
1841 /*
1842 printf("PostClipX: src: %d .. %d dst: %d .. %d\n",
1843 *srcX0, *srcX1, *dstX0, *dstX1);
1844 printf("PostClipY: src: %d .. %d dst: %d .. %d\n",
1845 *srcY0, *srcY1, *dstY0, *dstY1);
1846 */
1847
1848 ASSERT(*dstX0 >= dstXmin);
1849 ASSERT(*dstX0 <= dstXmax);
1850 ASSERT(*dstX1 >= dstXmin);
1851 ASSERT(*dstX1 <= dstXmax);
1852
1853 ASSERT(*dstY0 >= dstYmin);
1854 ASSERT(*dstY0 <= dstYmax);
1855 ASSERT(*dstY1 >= dstYmin);
1856 ASSERT(*dstY1 <= dstYmax);
1857
1858 ASSERT(*srcX0 >= srcXmin);
1859 ASSERT(*srcX0 <= srcXmax);
1860 ASSERT(*srcX1 >= srcXmin);
1861 ASSERT(*srcX1 <= srcXmax);
1862
1863 ASSERT(*srcY0 >= srcYmin);
1864 ASSERT(*srcY0 <= srcYmax);
1865 ASSERT(*srcY1 >= srcYmin);
1866 ASSERT(*srcY1 <= srcYmax);
1867
1868 return GL_TRUE;
1869 }