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