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