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