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