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