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