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