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