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