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