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