image.c revision 9520f483b8f1e45fa474674b415554988de5d8d3
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#if FEATURE_ES
1072   case GL_PALETTE4_RGB8_OES:
1073   case GL_PALETTE4_RGBA8_OES:
1074   case GL_PALETTE4_R5_G6_B5_OES:
1075   case GL_PALETTE4_RGBA4_OES:
1076   case GL_PALETTE4_RGB5_A1_OES:
1077   case GL_PALETTE8_RGB8_OES:
1078   case GL_PALETTE8_RGBA8_OES:
1079   case GL_PALETTE8_R5_G6_B5_OES:
1080   case GL_PALETTE8_RGBA4_OES:
1081   case GL_PALETTE8_RGB5_A1_OES:
1082      return ctx->API == API_OPENGLES;
1083#endif
1084   default:
1085      return GL_FALSE;
1086   }
1087}
1088
1089
1090/**
1091 * Return the address of a specific pixel in an image (1D, 2D or 3D).
1092 *
1093 * Pixel unpacking/packing parameters are observed according to \p packing.
1094 *
1095 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
1096 * \param image  starting address of image data
1097 * \param width  the image width
1098 * \param height  theimage height
1099 * \param format  the pixel format
1100 * \param type  the pixel data type
1101 * \param packing  the pixelstore attributes
1102 * \param img  which image in the volume (0 for 1D or 2D images)
1103 * \param row  row of pixel in the image (0 for 1D images)
1104 * \param column column of pixel in the image
1105 *
1106 * \return address of pixel on success, or NULL on error.
1107 *
1108 * \sa gl_pixelstore_attrib.
1109 */
1110GLvoid *
1111_mesa_image_address( GLuint dimensions,
1112                     const struct gl_pixelstore_attrib *packing,
1113                     const GLvoid *image,
1114                     GLsizei width, GLsizei height,
1115                     GLenum format, GLenum type,
1116                     GLint img, GLint row, GLint column )
1117{
1118   GLint alignment;        /* 1, 2 or 4 */
1119   GLint pixels_per_row;
1120   GLint rows_per_image;
1121   GLint skiprows;
1122   GLint skippixels;
1123   GLint skipimages;       /* for 3-D volume images */
1124   GLubyte *pixel_addr;
1125
1126   ASSERT(dimensions >= 1 && dimensions <= 3);
1127
1128   alignment = packing->Alignment;
1129   if (packing->RowLength > 0) {
1130      pixels_per_row = packing->RowLength;
1131   }
1132   else {
1133      pixels_per_row = width;
1134   }
1135   if (packing->ImageHeight > 0) {
1136      rows_per_image = packing->ImageHeight;
1137   }
1138   else {
1139      rows_per_image = height;
1140   }
1141
1142   skippixels = packing->SkipPixels;
1143   /* Note: SKIP_ROWS _is_ used for 1D images */
1144   skiprows = packing->SkipRows;
1145   /* Note: SKIP_IMAGES is only used for 3D images */
1146   skipimages = (dimensions == 3) ? packing->SkipImages : 0;
1147
1148   if (type == GL_BITMAP) {
1149      /* BITMAP data */
1150      GLint comp_per_pixel;   /* components per pixel */
1151      GLint bytes_per_comp;   /* bytes per component */
1152      GLint bytes_per_row;
1153      GLint bytes_per_image;
1154
1155      /* Compute bytes per component */
1156      bytes_per_comp = _mesa_sizeof_packed_type( type );
1157      if (bytes_per_comp < 0) {
1158         return NULL;
1159      }
1160
1161      /* Compute number of components per pixel */
1162      comp_per_pixel = _mesa_components_in_format( format );
1163      if (comp_per_pixel < 0) {
1164         return NULL;
1165      }
1166
1167      bytes_per_row = alignment
1168                    * CEILING( comp_per_pixel*pixels_per_row, 8*alignment );
1169
1170      bytes_per_image = bytes_per_row * rows_per_image;
1171
1172      pixel_addr = (GLubyte *) image
1173                 + (skipimages + img) * bytes_per_image
1174                 + (skiprows + row) * bytes_per_row
1175                 + (skippixels + column) / 8;
1176   }
1177   else {
1178      /* Non-BITMAP data */
1179      GLint bytes_per_pixel, bytes_per_row, remainder, bytes_per_image;
1180      GLint topOfImage;
1181
1182      bytes_per_pixel = _mesa_bytes_per_pixel( format, type );
1183
1184      /* The pixel type and format should have been error checked earlier */
1185      assert(bytes_per_pixel > 0);
1186
1187      bytes_per_row = pixels_per_row * bytes_per_pixel;
1188      remainder = bytes_per_row % alignment;
1189      if (remainder > 0)
1190         bytes_per_row += (alignment - remainder);
1191
1192      ASSERT(bytes_per_row % alignment == 0);
1193
1194      bytes_per_image = bytes_per_row * rows_per_image;
1195
1196      if (packing->Invert) {
1197         /* set pixel_addr to the last row */
1198         topOfImage = bytes_per_row * (height - 1);
1199         bytes_per_row = -bytes_per_row;
1200      }
1201      else {
1202         topOfImage = 0;
1203      }
1204
1205      /* compute final pixel address */
1206      pixel_addr = (GLubyte *) image
1207                 + (skipimages + img) * bytes_per_image
1208                 + topOfImage
1209                 + (skiprows + row) * bytes_per_row
1210                 + (skippixels + column) * bytes_per_pixel;
1211   }
1212
1213   return (GLvoid *) pixel_addr;
1214}
1215
1216
1217GLvoid *
1218_mesa_image_address1d( const struct gl_pixelstore_attrib *packing,
1219                       const GLvoid *image,
1220                       GLsizei width,
1221                       GLenum format, GLenum type,
1222                       GLint column )
1223{
1224   return _mesa_image_address(1, packing, image, width, 1,
1225                              format, type, 0, 0, column);
1226}
1227
1228
1229GLvoid *
1230_mesa_image_address2d( const struct gl_pixelstore_attrib *packing,
1231                       const GLvoid *image,
1232                       GLsizei width, GLsizei height,
1233                       GLenum format, GLenum type,
1234                       GLint row, GLint column )
1235{
1236   return _mesa_image_address(2, packing, image, width, height,
1237                              format, type, 0, row, column);
1238}
1239
1240
1241GLvoid *
1242_mesa_image_address3d( const struct gl_pixelstore_attrib *packing,
1243                       const GLvoid *image,
1244                       GLsizei width, GLsizei height,
1245                       GLenum format, GLenum type,
1246                       GLint img, GLint row, GLint column )
1247{
1248   return _mesa_image_address(3, packing, image, width, height,
1249                              format, type, img, row, column);
1250}
1251
1252
1253
1254/**
1255 * Compute the stride (in bytes) between image rows.
1256 *
1257 * \param packing the pixelstore attributes
1258 * \param width image width.
1259 * \param format pixel format.
1260 * \param type pixel data type.
1261 *
1262 * \return the stride in bytes for the given parameters, or -1 if error
1263 */
1264GLint
1265_mesa_image_row_stride( const struct gl_pixelstore_attrib *packing,
1266                        GLint width, GLenum format, GLenum type )
1267{
1268   GLint bytesPerRow, remainder;
1269
1270   ASSERT(packing);
1271
1272   if (type == GL_BITMAP) {
1273      if (packing->RowLength == 0) {
1274         bytesPerRow = (width + 7) / 8;
1275      }
1276      else {
1277         bytesPerRow = (packing->RowLength + 7) / 8;
1278      }
1279   }
1280   else {
1281      /* Non-BITMAP data */
1282      const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
1283      if (bytesPerPixel <= 0)
1284         return -1;  /* error */
1285      if (packing->RowLength == 0) {
1286         bytesPerRow = bytesPerPixel * width;
1287      }
1288      else {
1289         bytesPerRow = bytesPerPixel * packing->RowLength;
1290      }
1291   }
1292
1293   remainder = bytesPerRow % packing->Alignment;
1294   if (remainder > 0) {
1295      bytesPerRow += (packing->Alignment - remainder);
1296   }
1297
1298   if (packing->Invert) {
1299      /* negate the bytes per row (negative row stride) */
1300      bytesPerRow = -bytesPerRow;
1301   }
1302
1303   return bytesPerRow;
1304}
1305
1306
1307/*
1308 * Compute the stride between images in a 3D texture (in bytes) for the given
1309 * pixel packing parameters and image width, format and type.
1310 */
1311GLint
1312_mesa_image_image_stride( const struct gl_pixelstore_attrib *packing,
1313                          GLint width, GLint height,
1314                          GLenum format, GLenum type )
1315{
1316   GLint bytesPerRow, bytesPerImage, remainder;
1317
1318   ASSERT(packing);
1319
1320   if (type == GL_BITMAP) {
1321      if (packing->RowLength == 0) {
1322         bytesPerRow = (width + 7) / 8;
1323      }
1324      else {
1325         bytesPerRow = (packing->RowLength + 7) / 8;
1326      }
1327   }
1328   else {
1329      const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
1330
1331      if (bytesPerPixel <= 0)
1332         return -1;  /* error */
1333      if (packing->RowLength == 0) {
1334         bytesPerRow = bytesPerPixel * width;
1335      }
1336      else {
1337         bytesPerRow = bytesPerPixel * packing->RowLength;
1338      }
1339   }
1340
1341   remainder = bytesPerRow % packing->Alignment;
1342   if (remainder > 0)
1343      bytesPerRow += (packing->Alignment - remainder);
1344
1345   if (packing->ImageHeight == 0)
1346      bytesPerImage = bytesPerRow * height;
1347   else
1348      bytesPerImage = bytesPerRow * packing->ImageHeight;
1349
1350   return bytesPerImage;
1351}
1352
1353
1354
1355/**
1356 * "Expand" a bitmap from 1-bit per pixel to 8-bits per pixel.
1357 * This is typically used to convert a bitmap into a GLubyte/pixel texture.
1358 * "On" bits will set texels to \p onValue.
1359 * "Off" bits will not modify texels.
1360 * \param width  src bitmap width in pixels
1361 * \param height  src bitmap height in pixels
1362 * \param unpack  bitmap unpacking state
1363 * \param bitmap  the src bitmap data
1364 * \param destBuffer  start of dest buffer
1365 * \param destStride  row stride in dest buffer
1366 * \param onValue  if bit is 1, set destBuffer pixel to this value
1367 */
1368void
1369_mesa_expand_bitmap(GLsizei width, GLsizei height,
1370                    const struct gl_pixelstore_attrib *unpack,
1371                    const GLubyte *bitmap,
1372                    GLubyte *destBuffer, GLint destStride,
1373                    GLubyte onValue)
1374{
1375   const GLubyte *srcRow = (const GLubyte *)
1376      _mesa_image_address2d(unpack, bitmap, width, height,
1377                            GL_COLOR_INDEX, GL_BITMAP, 0, 0);
1378   const GLint srcStride = _mesa_image_row_stride(unpack, width,
1379                                                  GL_COLOR_INDEX, GL_BITMAP);
1380   GLint row, col;
1381
1382#define SET_PIXEL(COL, ROW) \
1383   destBuffer[(ROW) * destStride + (COL)] = onValue;
1384
1385   for (row = 0; row < height; row++) {
1386      const GLubyte *src = srcRow;
1387
1388      if (unpack->LsbFirst) {
1389         /* Lsb first */
1390         GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
1391         for (col = 0; col < width; col++) {
1392
1393            if (*src & mask) {
1394               SET_PIXEL(col, row);
1395            }
1396
1397            if (mask == 128U) {
1398               src++;
1399               mask = 1U;
1400            }
1401            else {
1402               mask = mask << 1;
1403            }
1404         }
1405
1406         /* get ready for next row */
1407         if (mask != 1)
1408            src++;
1409      }
1410      else {
1411         /* Msb first */
1412         GLubyte mask = 128U >> (unpack->SkipPixels & 0x7);
1413         for (col = 0; col < width; col++) {
1414
1415            if (*src & mask) {
1416               SET_PIXEL(col, row);
1417            }
1418
1419            if (mask == 1U) {
1420               src++;
1421               mask = 128U;
1422            }
1423            else {
1424               mask = mask >> 1;
1425            }
1426         }
1427
1428         /* get ready for next row */
1429         if (mask != 128)
1430            src++;
1431      }
1432
1433      srcRow += srcStride;
1434   } /* row */
1435
1436#undef SET_PIXEL
1437}
1438
1439
1440
1441
1442/**
1443 * Convert an array of RGBA colors from one datatype to another.
1444 * NOTE: src may equal dst.  In that case, we use a temporary buffer.
1445 */
1446void
1447_mesa_convert_colors(GLenum srcType, const GLvoid *src,
1448                     GLenum dstType, GLvoid *dst,
1449                     GLuint count, const GLubyte mask[])
1450{
1451   GLuint tempBuffer[MAX_WIDTH][4];
1452   const GLboolean useTemp = (src == dst);
1453
1454   ASSERT(srcType != dstType);
1455
1456   switch (srcType) {
1457   case GL_UNSIGNED_BYTE:
1458      if (dstType == GL_UNSIGNED_SHORT) {
1459         const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src;
1460         GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst);
1461         GLuint i;
1462         for (i = 0; i < count; i++) {
1463            if (!mask || mask[i]) {
1464               dst2[i][RCOMP] = UBYTE_TO_USHORT(src1[i][RCOMP]);
1465               dst2[i][GCOMP] = UBYTE_TO_USHORT(src1[i][GCOMP]);
1466               dst2[i][BCOMP] = UBYTE_TO_USHORT(src1[i][BCOMP]);
1467               dst2[i][ACOMP] = UBYTE_TO_USHORT(src1[i][ACOMP]);
1468            }
1469         }
1470         if (useTemp)
1471            memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort));
1472      }
1473      else {
1474         const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src;
1475         GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst);
1476         GLuint i;
1477         ASSERT(dstType == GL_FLOAT);
1478         for (i = 0; i < count; i++) {
1479            if (!mask || mask[i]) {
1480               dst4[i][RCOMP] = UBYTE_TO_FLOAT(src1[i][RCOMP]);
1481               dst4[i][GCOMP] = UBYTE_TO_FLOAT(src1[i][GCOMP]);
1482               dst4[i][BCOMP] = UBYTE_TO_FLOAT(src1[i][BCOMP]);
1483               dst4[i][ACOMP] = UBYTE_TO_FLOAT(src1[i][ACOMP]);
1484            }
1485         }
1486         if (useTemp)
1487            memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat));
1488      }
1489      break;
1490   case GL_UNSIGNED_SHORT:
1491      if (dstType == GL_UNSIGNED_BYTE) {
1492         const GLushort (*src2)[4] = (const GLushort (*)[4]) src;
1493         GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst);
1494         GLuint i;
1495         for (i = 0; i < count; i++) {
1496            if (!mask || mask[i]) {
1497               dst1[i][RCOMP] = USHORT_TO_UBYTE(src2[i][RCOMP]);
1498               dst1[i][GCOMP] = USHORT_TO_UBYTE(src2[i][GCOMP]);
1499               dst1[i][BCOMP] = USHORT_TO_UBYTE(src2[i][BCOMP]);
1500               dst1[i][ACOMP] = USHORT_TO_UBYTE(src2[i][ACOMP]);
1501            }
1502         }
1503         if (useTemp)
1504            memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte));
1505      }
1506      else {
1507         const GLushort (*src2)[4] = (const GLushort (*)[4]) src;
1508         GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst);
1509         GLuint i;
1510         ASSERT(dstType == GL_FLOAT);
1511         for (i = 0; i < count; i++) {
1512            if (!mask || mask[i]) {
1513               dst4[i][RCOMP] = USHORT_TO_FLOAT(src2[i][RCOMP]);
1514               dst4[i][GCOMP] = USHORT_TO_FLOAT(src2[i][GCOMP]);
1515               dst4[i][BCOMP] = USHORT_TO_FLOAT(src2[i][BCOMP]);
1516               dst4[i][ACOMP] = USHORT_TO_FLOAT(src2[i][ACOMP]);
1517            }
1518         }
1519         if (useTemp)
1520            memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat));
1521      }
1522      break;
1523   case GL_FLOAT:
1524      if (dstType == GL_UNSIGNED_BYTE) {
1525         const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src;
1526         GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst);
1527         GLuint i;
1528         for (i = 0; i < count; i++) {
1529            if (!mask || mask[i])
1530               _mesa_unclamped_float_rgba_to_ubyte(dst1[i], src4[i]);
1531         }
1532         if (useTemp)
1533            memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte));
1534      }
1535      else {
1536         const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src;
1537         GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst);
1538         GLuint i;
1539         ASSERT(dstType == GL_UNSIGNED_SHORT);
1540         for (i = 0; i < count; i++) {
1541            if (!mask || mask[i]) {
1542               UNCLAMPED_FLOAT_TO_USHORT(dst2[i][RCOMP], src4[i][RCOMP]);
1543               UNCLAMPED_FLOAT_TO_USHORT(dst2[i][GCOMP], src4[i][GCOMP]);
1544               UNCLAMPED_FLOAT_TO_USHORT(dst2[i][BCOMP], src4[i][BCOMP]);
1545               UNCLAMPED_FLOAT_TO_USHORT(dst2[i][ACOMP], src4[i][ACOMP]);
1546            }
1547         }
1548         if (useTemp)
1549            memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort));
1550      }
1551      break;
1552   default:
1553      _mesa_problem(NULL, "Invalid datatype in _mesa_convert_colors");
1554   }
1555}
1556
1557
1558
1559
1560/**
1561 * Perform basic clipping for glDrawPixels.  The image's position and size
1562 * and the unpack SkipPixels and SkipRows are adjusted so that the image
1563 * region is entirely within the window and scissor bounds.
1564 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
1565 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
1566 * we'll actually write.  Beforehand, *destY-1 is the first drawing row.
1567 *
1568 * \return  GL_TRUE if image is ready for drawing or
1569 *          GL_FALSE if image was completely clipped away (draw nothing)
1570 */
1571GLboolean
1572_mesa_clip_drawpixels(const struct gl_context *ctx,
1573                      GLint *destX, GLint *destY,
1574                      GLsizei *width, GLsizei *height,
1575                      struct gl_pixelstore_attrib *unpack)
1576{
1577   const struct gl_framebuffer *buffer = ctx->DrawBuffer;
1578
1579   if (unpack->RowLength == 0) {
1580      unpack->RowLength = *width;
1581   }
1582
1583   ASSERT(ctx->Pixel.ZoomX == 1.0F);
1584   ASSERT(ctx->Pixel.ZoomY == 1.0F || ctx->Pixel.ZoomY == -1.0F);
1585
1586   /* left clipping */
1587   if (*destX < buffer->_Xmin) {
1588      unpack->SkipPixels += (buffer->_Xmin - *destX);
1589      *width -= (buffer->_Xmin - *destX);
1590      *destX = buffer->_Xmin;
1591   }
1592   /* right clipping */
1593   if (*destX + *width > buffer->_Xmax)
1594      *width -= (*destX + *width - buffer->_Xmax);
1595
1596   if (*width <= 0)
1597      return GL_FALSE;
1598
1599   if (ctx->Pixel.ZoomY == 1.0F) {
1600      /* bottom clipping */
1601      if (*destY < buffer->_Ymin) {
1602         unpack->SkipRows += (buffer->_Ymin - *destY);
1603         *height -= (buffer->_Ymin - *destY);
1604         *destY = buffer->_Ymin;
1605      }
1606      /* top clipping */
1607      if (*destY + *height > buffer->_Ymax)
1608         *height -= (*destY + *height - buffer->_Ymax);
1609   }
1610   else { /* upside down */
1611      /* top clipping */
1612      if (*destY > buffer->_Ymax) {
1613         unpack->SkipRows += (*destY - buffer->_Ymax);
1614         *height -= (*destY - buffer->_Ymax);
1615         *destY = buffer->_Ymax;
1616      }
1617      /* bottom clipping */
1618      if (*destY - *height < buffer->_Ymin)
1619         *height -= (buffer->_Ymin - (*destY - *height));
1620      /* adjust destY so it's the first row to write to */
1621      (*destY)--;
1622   }
1623
1624   if (*height <= 0)
1625      return GL_FALSE;
1626
1627   return GL_TRUE;
1628}
1629
1630
1631/**
1632 * Perform clipping for glReadPixels.  The image's window position
1633 * and size, and the pack skipPixels, skipRows and rowLength are adjusted
1634 * so that the image region is entirely within the window bounds.
1635 * Note: this is different from _mesa_clip_drawpixels() in that the
1636 * scissor box is ignored, and we use the bounds of the current readbuffer
1637 * surface.
1638 *
1639 * \return  GL_TRUE if region to read is in bounds
1640 *          GL_FALSE if region is completely out of bounds (nothing to read)
1641 */
1642GLboolean
1643_mesa_clip_readpixels(const struct gl_context *ctx,
1644                      GLint *srcX, GLint *srcY,
1645                      GLsizei *width, GLsizei *height,
1646                      struct gl_pixelstore_attrib *pack)
1647{
1648   const struct gl_framebuffer *buffer = ctx->ReadBuffer;
1649
1650   if (pack->RowLength == 0) {
1651      pack->RowLength = *width;
1652   }
1653
1654   /* left clipping */
1655   if (*srcX < 0) {
1656      pack->SkipPixels += (0 - *srcX);
1657      *width -= (0 - *srcX);
1658      *srcX = 0;
1659   }
1660   /* right clipping */
1661   if (*srcX + *width > (GLsizei) buffer->Width)
1662      *width -= (*srcX + *width - buffer->Width);
1663
1664   if (*width <= 0)
1665      return GL_FALSE;
1666
1667   /* bottom clipping */
1668   if (*srcY < 0) {
1669      pack->SkipRows += (0 - *srcY);
1670      *height -= (0 - *srcY);
1671      *srcY = 0;
1672   }
1673   /* top clipping */
1674   if (*srcY + *height > (GLsizei) buffer->Height)
1675      *height -= (*srcY + *height - buffer->Height);
1676
1677   if (*height <= 0)
1678      return GL_FALSE;
1679
1680   return GL_TRUE;
1681}
1682
1683
1684/**
1685 * Do clipping for a glCopyTexSubImage call.
1686 * The framebuffer source region might extend outside the framebuffer
1687 * bounds.  Clip the source region against the framebuffer bounds and
1688 * adjust the texture/dest position and size accordingly.
1689 *
1690 * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise.
1691 */
1692GLboolean
1693_mesa_clip_copytexsubimage(const struct gl_context *ctx,
1694                           GLint *destX, GLint *destY,
1695                           GLint *srcX, GLint *srcY,
1696                           GLsizei *width, GLsizei *height)
1697{
1698   const struct gl_framebuffer *fb = ctx->ReadBuffer;
1699   const GLint srcX0 = *srcX, srcY0 = *srcY;
1700
1701   if (_mesa_clip_to_region(0, 0, fb->Width, fb->Height,
1702                            srcX, srcY, width, height)) {
1703      *destX = *destX + *srcX - srcX0;
1704      *destY = *destY + *srcY - srcY0;
1705
1706      return GL_TRUE;
1707   }
1708   else {
1709      return GL_FALSE;
1710   }
1711}
1712
1713
1714
1715/**
1716 * Clip the rectangle defined by (x, y, width, height) against the bounds
1717 * specified by [xmin, xmax) and [ymin, ymax).
1718 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
1719 */
1720GLboolean
1721_mesa_clip_to_region(GLint xmin, GLint ymin,
1722                     GLint xmax, GLint ymax,
1723                     GLint *x, GLint *y,
1724                     GLsizei *width, GLsizei *height )
1725{
1726   /* left clipping */
1727   if (*x < xmin) {
1728      *width -= (xmin - *x);
1729      *x = xmin;
1730   }
1731
1732   /* right clipping */
1733   if (*x + *width > xmax)
1734      *width -= (*x + *width - xmax);
1735
1736   if (*width <= 0)
1737      return GL_FALSE;
1738
1739   /* bottom (or top) clipping */
1740   if (*y < ymin) {
1741      *height -= (ymin - *y);
1742      *y = ymin;
1743   }
1744
1745   /* top (or bottom) clipping */
1746   if (*y + *height > ymax)
1747      *height -= (*y + *height - ymax);
1748
1749   if (*height <= 0)
1750      return GL_FALSE;
1751
1752   return GL_TRUE;
1753}
1754
1755
1756/**
1757 * Clip dst coords against Xmax (or Ymax).
1758 */
1759static inline void
1760clip_right_or_top(GLint *srcX0, GLint *srcX1,
1761                  GLint *dstX0, GLint *dstX1,
1762                  GLint maxValue)
1763{
1764   GLfloat t, bias;
1765
1766   if (*dstX1 > maxValue) {
1767      /* X1 outside right edge */
1768      ASSERT(*dstX0 < maxValue); /* X0 should be inside right edge */
1769      t = (GLfloat) (maxValue - *dstX0) / (GLfloat) (*dstX1 - *dstX0);
1770      /* chop off [t, 1] part */
1771      ASSERT(t >= 0.0 && t <= 1.0);
1772      *dstX1 = maxValue;
1773      bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F;
1774      *srcX1 = *srcX0 + (GLint) (t * (*srcX1 - *srcX0) + bias);
1775   }
1776   else if (*dstX0 > maxValue) {
1777      /* X0 outside right edge */
1778      ASSERT(*dstX1 < maxValue); /* X1 should be inside right edge */
1779      t = (GLfloat) (maxValue - *dstX1) / (GLfloat) (*dstX0 - *dstX1);
1780      /* chop off [t, 1] part */
1781      ASSERT(t >= 0.0 && t <= 1.0);
1782      *dstX0 = maxValue;
1783      bias = (*srcX0 < *srcX1) ? -0.5F : 0.5F;
1784      *srcX0 = *srcX1 + (GLint) (t * (*srcX0 - *srcX1) + bias);
1785   }
1786}
1787
1788
1789/**
1790 * Clip dst coords against Xmin (or Ymin).
1791 */
1792static inline void
1793clip_left_or_bottom(GLint *srcX0, GLint *srcX1,
1794                    GLint *dstX0, GLint *dstX1,
1795                    GLint minValue)
1796{
1797   GLfloat t, bias;
1798
1799   if (*dstX0 < minValue) {
1800      /* X0 outside left edge */
1801      ASSERT(*dstX1 > minValue); /* X1 should be inside left edge */
1802      t = (GLfloat) (minValue - *dstX0) / (GLfloat) (*dstX1 - *dstX0);
1803      /* chop off [0, t] part */
1804      ASSERT(t >= 0.0 && t <= 1.0);
1805      *dstX0 = minValue;
1806      bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F; /* flipped??? */
1807      *srcX0 = *srcX0 + (GLint) (t * (*srcX1 - *srcX0) + bias);
1808   }
1809   else if (*dstX1 < minValue) {
1810      /* X1 outside left edge */
1811      ASSERT(*dstX0 > minValue); /* X0 should be inside left edge */
1812      t = (GLfloat) (minValue - *dstX1) / (GLfloat) (*dstX0 - *dstX1);
1813      /* chop off [0, t] part */
1814      ASSERT(t >= 0.0 && t <= 1.0);
1815      *dstX1 = minValue;
1816      bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F;
1817      *srcX1 = *srcX1 + (GLint) (t * (*srcX0 - *srcX1) + bias);
1818   }
1819}
1820
1821
1822/**
1823 * Do clipping of blit src/dest rectangles.
1824 * The dest rect is clipped against both the buffer bounds and scissor bounds.
1825 * The src rect is just clipped against the buffer bounds.
1826 *
1827 * When either the src or dest rect is clipped, the other is also clipped
1828 * proportionately!
1829 *
1830 * Note that X0 need not be less than X1 (same for Y) for either the source
1831 * and dest rects.  That makes the clipping a little trickier.
1832 *
1833 * \return GL_TRUE if anything is left to draw, GL_FALSE if totally clipped
1834 */
1835GLboolean
1836_mesa_clip_blit(struct gl_context *ctx,
1837                GLint *srcX0, GLint *srcY0, GLint *srcX1, GLint *srcY1,
1838                GLint *dstX0, GLint *dstY0, GLint *dstX1, GLint *dstY1)
1839{
1840   const GLint srcXmin = 0;
1841   const GLint srcXmax = ctx->ReadBuffer->Width;
1842   const GLint srcYmin = 0;
1843   const GLint srcYmax = ctx->ReadBuffer->Height;
1844
1845   /* these include scissor bounds */
1846   const GLint dstXmin = ctx->DrawBuffer->_Xmin;
1847   const GLint dstXmax = ctx->DrawBuffer->_Xmax;
1848   const GLint dstYmin = ctx->DrawBuffer->_Ymin;
1849   const GLint dstYmax = ctx->DrawBuffer->_Ymax;
1850
1851   /*
1852   printf("PreClipX:  src: %d .. %d  dst: %d .. %d\n",
1853          *srcX0, *srcX1, *dstX0, *dstX1);
1854   printf("PreClipY:  src: %d .. %d  dst: %d .. %d\n",
1855          *srcY0, *srcY1, *dstY0, *dstY1);
1856   */
1857
1858   /* trivial rejection tests */
1859   if (*dstX0 == *dstX1)
1860      return GL_FALSE; /* no width */
1861   if (*dstX0 <= dstXmin && *dstX1 <= dstXmin)
1862      return GL_FALSE; /* totally out (left) of bounds */
1863   if (*dstX0 >= dstXmax && *dstX1 >= dstXmax)
1864      return GL_FALSE; /* totally out (right) of bounds */
1865
1866   if (*dstY0 == *dstY1)
1867      return GL_FALSE;
1868   if (*dstY0 <= dstYmin && *dstY1 <= dstYmin)
1869      return GL_FALSE;
1870   if (*dstY0 >= dstYmax && *dstY1 >= dstYmax)
1871      return GL_FALSE;
1872
1873   if (*srcX0 == *srcX1)
1874      return GL_FALSE;
1875   if (*srcX0 <= srcXmin && *srcX1 <= srcXmin)
1876      return GL_FALSE;
1877   if (*srcX0 >= srcXmax && *srcX1 >= srcXmax)
1878      return GL_FALSE;
1879
1880   if (*srcY0 == *srcY1)
1881      return GL_FALSE;
1882   if (*srcY0 <= srcYmin && *srcY1 <= srcYmin)
1883      return GL_FALSE;
1884   if (*srcY0 >= srcYmax && *srcY1 >= srcYmax)
1885      return GL_FALSE;
1886
1887   /*
1888    * dest clip
1889    */
1890   clip_right_or_top(srcX0, srcX1, dstX0, dstX1, dstXmax);
1891   clip_right_or_top(srcY0, srcY1, dstY0, dstY1, dstYmax);
1892   clip_left_or_bottom(srcX0, srcX1, dstX0, dstX1, dstXmin);
1893   clip_left_or_bottom(srcY0, srcY1, dstY0, dstY1, dstYmin);
1894
1895   /*
1896    * src clip (just swap src/dst values from above)
1897    */
1898   clip_right_or_top(dstX0, dstX1, srcX0, srcX1, srcXmax);
1899   clip_right_or_top(dstY0, dstY1, srcY0, srcY1, srcYmax);
1900   clip_left_or_bottom(dstX0, dstX1, srcX0, srcX1, srcXmin);
1901   clip_left_or_bottom(dstY0, dstY1, srcY0, srcY1, srcYmin);
1902
1903   /*
1904   printf("PostClipX: src: %d .. %d  dst: %d .. %d\n",
1905          *srcX0, *srcX1, *dstX0, *dstX1);
1906   printf("PostClipY: src: %d .. %d  dst: %d .. %d\n",
1907          *srcY0, *srcY1, *dstY0, *dstY1);
1908   */
1909
1910   ASSERT(*dstX0 >= dstXmin);
1911   ASSERT(*dstX0 <= dstXmax);
1912   ASSERT(*dstX1 >= dstXmin);
1913   ASSERT(*dstX1 <= dstXmax);
1914
1915   ASSERT(*dstY0 >= dstYmin);
1916   ASSERT(*dstY0 <= dstYmax);
1917   ASSERT(*dstY1 >= dstYmin);
1918   ASSERT(*dstY1 <= dstYmax);
1919
1920   ASSERT(*srcX0 >= srcXmin);
1921   ASSERT(*srcX0 <= srcXmax);
1922   ASSERT(*srcX1 >= srcXmin);
1923   ASSERT(*srcX1 <= srcXmax);
1924
1925   ASSERT(*srcY0 >= srcYmin);
1926   ASSERT(*srcY0 <= srcYmax);
1927   ASSERT(*srcY1 >= srcYmin);
1928   ASSERT(*srcY1 <= srcYmax);
1929
1930   return GL_TRUE;
1931}
1932