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