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