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