VirtualBox

source: vbox/trunk/src/VBox/GuestHost/OpenGL/util/pixel.c@ 27396

最後變更 在這個檔案從27396是 27396,由 vboxsync 提交於 15 年 前

crOpenGL: opengl 2.1 support

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 64.5 KB
 
1/* Cop(c) 2001, Stanford University
2 * All rights reserved
3 *
4 * See the file LICENSE.txt for information on redistributing this software.
5 */
6
7#include "cr_pixeldata.h"
8#include "cr_error.h"
9#include "cr_mem.h"
10#include "cr_version.h"
11
12#if defined(WINDOWS)
13# include <math.h>
14# include <float.h>
15# define isnan(x) _isnan(x)
16#else
17# include <cmath>
18#endif
19
20/**
21 * Maybe export this someday.
22 */
23static int crSizeOfType( GLenum type )
24{
25 switch (type) {
26#ifdef CR_OPENGL_VERSION_1_2
27 case GL_UNSIGNED_BYTE_3_3_2:
28 case GL_UNSIGNED_BYTE_2_3_3_REV:
29#endif
30 case GL_UNSIGNED_BYTE:
31 case GL_BYTE:
32 return 1;
33 case GL_BITMAP:
34 return 0; /* special case */
35#ifdef CR_OPENGL_VERSION_1_2
36 case GL_UNSIGNED_SHORT_5_6_5:
37 case GL_UNSIGNED_SHORT_5_6_5_REV:
38 case GL_UNSIGNED_SHORT_5_5_5_1:
39 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
40 case GL_UNSIGNED_SHORT_4_4_4_4:
41 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
42#endif
43 case GL_UNSIGNED_SHORT:
44 case GL_SHORT:
45 return 2;
46#ifdef CR_OPENGL_VERSION_1_2
47 case GL_UNSIGNED_INT_8_8_8_8:
48 case GL_UNSIGNED_INT_8_8_8_8_REV:
49 case GL_UNSIGNED_INT_10_10_10_2:
50 case GL_UNSIGNED_INT_2_10_10_10_REV:
51#endif
52 case GL_UNSIGNED_INT:
53 case GL_INT:
54 case GL_FLOAT:
55 return 4;
56 case GL_DOUBLE:
57 return 8;
58 default:
59 crError( "Unknown pixel type in crSizeOfType: 0x%x", (unsigned int) type );
60 return 0;
61 }
62}
63
64
65/**
66 * Compute bytes per pixel for the given format/type combination.
67 * \return bytes per pixel or -1 for invalid format or type, 0 for bitmap data.
68 */
69int crPixelSize( GLenum format, GLenum type )
70{
71 int bytes = 1; /* picky Windows compiler, we override later */
72
73 switch (type) {
74#ifdef CR_OPENGL_VERSION_1_2
75 case GL_UNSIGNED_BYTE_3_3_2:
76 case GL_UNSIGNED_BYTE_2_3_3_REV:
77 return 1;
78#endif
79 case GL_UNSIGNED_BYTE:
80 case GL_BYTE:
81 bytes = 1;
82 break;
83 case GL_BITMAP:
84 return 0; /* special case */
85#ifdef CR_OPENGL_VERSION_1_2
86 case GL_UNSIGNED_SHORT_5_6_5:
87 case GL_UNSIGNED_SHORT_5_6_5_REV:
88 case GL_UNSIGNED_SHORT_5_5_5_1:
89 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
90 case GL_UNSIGNED_SHORT_4_4_4_4:
91 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
92 return 2;
93#endif
94 case GL_UNSIGNED_SHORT:
95 case GL_SHORT:
96 bytes = 2;
97 break;
98#ifdef CR_OPENGL_VERSION_1_2
99 case GL_UNSIGNED_INT_8_8_8_8:
100 case GL_UNSIGNED_INT_8_8_8_8_REV:
101 case GL_UNSIGNED_INT_10_10_10_2:
102 case GL_UNSIGNED_INT_2_10_10_10_REV:
103 return 4;
104#endif
105 case GL_UNSIGNED_INT:
106 case GL_INT:
107 case GL_FLOAT:
108 bytes = 4;
109 break;
110 default:
111 crWarning( "Unknown pixel type in crPixelSize: type:0x%x(fmt:0x%x)", (unsigned int) type, (unsigned int) format);
112 return 0;
113 }
114
115 switch (format) {
116 case GL_COLOR_INDEX:
117 case GL_STENCIL_INDEX:
118 case GL_DEPTH_COMPONENT:
119 case GL_RED:
120 case GL_GREEN:
121 case GL_BLUE:
122 case GL_ALPHA:
123 case GL_LUMINANCE:
124 case GL_INTENSITY:
125#ifdef CR_EXT_texture_sRGB
126 case GL_SLUMINANCE_EXT:
127 case GL_SLUMINANCE8_EXT:
128#endif
129 break;
130 case GL_LUMINANCE_ALPHA:
131#ifdef CR_EXT_texture_sRGB
132 case GL_SLUMINANCE_ALPHA_EXT:
133 case GL_SLUMINANCE8_ALPHA8_EXT:
134#endif
135 bytes *= 2;
136 break;
137 case GL_RGB:
138#ifdef CR_OPENGL_VERSION_1_2
139 case GL_BGR:
140#endif
141#ifdef CR_EXT_texture_sRGB
142 case GL_SRGB_EXT:
143 case GL_SRGB8_EXT:
144#endif
145 bytes *= 3;
146 break;
147 case GL_RGBA:
148#ifdef GL_ABGR_EXT
149 case GL_ABGR_EXT:
150#endif
151#ifdef CR_OPENGL_VERSION_1_2
152 case GL_BGRA:
153#endif
154#ifdef CR_EXT_texture_sRGB
155 case GL_SRGB_ALPHA_EXT:
156 case GL_SRGB8_ALPHA8_EXT:
157#endif
158 bytes *= 4;
159 break;
160 default:
161 crWarning( "Unknown pixel format in crPixelSize: type:0x%x(fmt:0x%x)", (unsigned int) type, (unsigned int) format);
162 return 0;
163 }
164
165 return bytes;
166}
167
168
169#define BYTE_TO_FLOAT(b) ((b) * (1.0/127.0))
170#define FLOAT_TO_BYTE(f) ((GLbyte) ((f) * 127.0))
171
172#define UBYTE_TO_FLOAT(b) ((b) * (1.0/255.0))
173#define FLOAT_TO_UBYTE(f) ((GLbyte) ((f) * 255.0))
174
175#define SHORT_TO_FLOAT(s) ((s) * (1.0/32768.0))
176#define FLOAT_TO_SHORT(f) ((GLshort) ((f) * 32768.0))
177
178#define USHORT_TO_FLOAT(s) ((s) * (1.0/65535.0))
179#define FLOAT_TO_USHORT(f) ((GLushort) ((f) * 65535.0))
180
181#define INT_TO_FLOAT(i) ((i) * (1.0F/2147483647.0))
182#define FLOAT_TO_INT(f) ((GLint) ((f) * 2147483647.0))
183
184#define UINT_TO_FLOAT(i) ((i) * (1.0F / 4294967295.0F))
185#define FLOAT_TO_UINT(f) ((GLuint) ((f) * 4294967295.0))
186
187
188static float SRGBF_TO_RGBF(float f)
189{
190 if (isnan(f)) return 0.f;
191
192 if (f<=0.04045f)
193 {
194 return f/12.92f;
195 }
196 else
197 {
198 return pow((f+0.055f)/1.055f, 2.4f);
199 }
200}
201
202static float RGBF_TO_SRGBF(float f)
203{
204 if (isnan(f)) return 0.f;
205 if (f>1.f) return 1.f;
206 if (f<0.f) return 0.f;
207
208 if (f<0.0031308f)
209 {
210 return f*12.92f;
211 }
212 else
213 {
214 return 1.055f*pow(f, 0.41666f) - 0.055f;
215 }
216}
217
218#ifdef _MSC_VER
219/** @todo bird: MSC takes 5..20 mins to compile get_row and/or put_row with global
220 * optimizations enabled. It varies a bit between 8.0/64 and 7.1/32 - the latter seems
221 * to be fine with just disabling opts for get_row, while the former needs both to be
222 * disabled to compile it a decent time. It seems this isn't important code after all,
223 * so we're not overly worried about the performance degration. Even so, we might want
224 * to look into it before long, perhaps checking with Visual C++ 9.0 (aka 2008). */
225# pragma optimize("g", off)
226#endif
227
228/*
229 * Pack src pixel data into tmpRow array as either GLfloat[][1] or
230 * GLfloat[][4] depending on whether the format is for colors.
231 */
232static void
233get_row(const char *src, GLenum srcFormat, GLenum srcType,
234 GLsizei width, GLfloat *tmpRow)
235{
236 const GLbyte *bSrc = (GLbyte *) src;
237 const GLubyte *ubSrc = (GLubyte *) src;
238 const GLshort *sSrc = (GLshort *) src;
239 const GLushort *usSrc = (GLushort *) src;
240 const GLint *iSrc = (GLint *) src;
241 const GLuint *uiSrc = (GLuint *) src;
242 const GLfloat *fSrc = (GLfloat *) src;
243 const GLdouble *dSrc = (GLdouble *) src;
244 int i;
245
246 if (srcFormat == GL_COLOR_INDEX || srcFormat == GL_STENCIL_INDEX) {
247 switch (srcType) {
248 case GL_BYTE:
249 for (i = 0; i < width; i++)
250 tmpRow[i] = (GLfloat) bSrc[i];
251 break;
252 case GL_UNSIGNED_BYTE:
253 for (i = 0; i < width; i++)
254 tmpRow[i] = (GLfloat) ubSrc[i];
255 break;
256 case GL_SHORT:
257 for (i = 0; i < width; i++)
258 tmpRow[i] = (GLfloat) sSrc[i];
259 break;
260 case GL_UNSIGNED_SHORT:
261 for (i = 0; i < width; i++)
262 tmpRow[i] = (GLfloat) usSrc[i];
263 break;
264 case GL_INT:
265 for (i = 0; i < width; i++)
266 tmpRow[i] = (GLfloat) iSrc[i];
267 break;
268 case GL_UNSIGNED_INT:
269 for (i = 0; i < width; i++)
270 tmpRow[i] = (GLfloat) uiSrc[i];
271 break;
272 case GL_FLOAT:
273 for (i = 0; i < width; i++)
274 tmpRow[i] = fSrc[i];
275 break;
276 case GL_DOUBLE:
277 for (i = 0; i < width; i++)
278 tmpRow[i] = (GLfloat) dSrc[i];
279 break;
280 default:
281 crError("unexpected type in get_row in pixel.c");
282 }
283 }
284 else if (srcFormat == GL_DEPTH_COMPONENT) {
285 switch (srcType) {
286 case GL_BYTE:
287 for (i = 0; i < width; i++)
288 tmpRow[i] = (GLfloat) BYTE_TO_FLOAT(bSrc[i]);
289 break;
290 case GL_UNSIGNED_BYTE:
291 for (i = 0; i < width; i++)
292 tmpRow[i] = (GLfloat) UBYTE_TO_FLOAT(ubSrc[i]);
293 break;
294 case GL_SHORT:
295 for (i = 0; i < width; i++)
296 tmpRow[i] = (GLfloat) SHORT_TO_FLOAT(sSrc[i]);
297 break;
298 case GL_UNSIGNED_SHORT:
299 for (i = 0; i < width; i++)
300 tmpRow[i] = (GLfloat) USHORT_TO_FLOAT(usSrc[i]);
301 break;
302 case GL_INT:
303 for (i = 0; i < width; i++)
304 tmpRow[i] = (GLfloat) INT_TO_FLOAT(bSrc[i]);
305 break;
306 case GL_UNSIGNED_INT:
307 for (i = 0; i < width; i++)
308 tmpRow[i] = (GLfloat) UINT_TO_FLOAT(bSrc[i]);
309 break;
310 case GL_FLOAT:
311 for (i = 0; i < width; i++)
312 tmpRow[i] = fSrc[i];
313 break;
314 case GL_DOUBLE:
315 for (i = 0; i < width; i++)
316 tmpRow[i] = (GLfloat) dSrc[i];
317 break;
318 default:
319 crError("unexpected type in get_row in pixel.c");
320 }
321 }
322 else if (srcFormat == GL_RED || srcFormat == GL_GREEN ||
323 srcFormat == GL_BLUE || srcFormat == GL_ALPHA) {
324 int dst;
325 if (srcFormat == GL_RED)
326 dst = 0;
327 else if (srcFormat == GL_GREEN)
328 dst = 1;
329 else if (srcFormat == GL_BLUE)
330 dst = 2;
331 else
332 dst = 3;
333 for (i = 0; i < width; i++) {
334 tmpRow[i*4+0] = 0.0;
335 tmpRow[i*4+1] = 0.0;
336 tmpRow[i*4+2] = 0.0;
337 tmpRow[i*4+3] = 1.0;
338 }
339 switch (srcType) {
340 case GL_BYTE:
341 for (i = 0; i < width; i++, dst += 4)
342 tmpRow[dst] = (GLfloat) BYTE_TO_FLOAT(bSrc[i]);
343 break;
344 case GL_UNSIGNED_BYTE:
345 for (i = 0; i < width; i++, dst += 4)
346 tmpRow[dst] = (GLfloat) UBYTE_TO_FLOAT(ubSrc[i]);
347 break;
348 case GL_SHORT:
349 for (i = 0; i < width; i++, dst += 4)
350 tmpRow[dst] = (GLfloat) SHORT_TO_FLOAT(sSrc[i]);
351 break;
352 case GL_UNSIGNED_SHORT:
353 for (i = 0; i < width; i++, dst += 4)
354 tmpRow[dst] = (GLfloat) USHORT_TO_FLOAT(usSrc[i]);
355 break;
356 case GL_INT:
357 for (i = 0; i < width; i++, dst += 4)
358 tmpRow[dst] = (GLfloat) INT_TO_FLOAT(iSrc[i]);
359 break;
360 case GL_UNSIGNED_INT:
361 for (i = 0; i < width; i++, dst += 4)
362 tmpRow[dst] = (GLfloat) UINT_TO_FLOAT(uiSrc[i]);
363 break;
364 case GL_FLOAT:
365 for (i = 0; i < width; i++, dst += 4)
366 tmpRow[dst] = fSrc[i];
367 break;
368 case GL_DOUBLE:
369 for (i = 0; i < width; i++, dst += 4)
370 tmpRow[dst] = (GLfloat) fSrc[i];
371 break;
372 default:
373 crError("unexpected type in get_row in pixel.c");
374 }
375 }
376 else if (srcFormat == GL_LUMINANCE
377#ifdef CR_EXT_texture_sRGB
378 || srcFormat == GL_SLUMINANCE_EXT
379 || srcFormat == GL_SLUMINANCE8_EXT
380#endif
381 ) {
382 switch (srcType) {
383 case GL_BYTE:
384 for (i = 0; i < width; i++) {
385 tmpRow[i*4+0] = tmpRow[i*4+1] = tmpRow[i*4+2]
386 = (GLfloat) BYTE_TO_FLOAT(bSrc[i]);
387 tmpRow[i*4+3] = 1.0;
388 }
389 break;
390 case GL_UNSIGNED_BYTE:
391 for (i = 0; i < width; i++) {
392 tmpRow[i*4+0] = tmpRow[i*4+1] = tmpRow[i*4+2]
393 = (GLfloat) UBYTE_TO_FLOAT(ubSrc[i]);
394 tmpRow[i*4+3] = 1.0;
395 }
396 break;
397 case GL_SHORT:
398 for (i = 0; i < width; i++) {
399 tmpRow[i*4+0] = tmpRow[i*4+1] = tmpRow[i*4+2]
400 = (GLfloat) SHORT_TO_FLOAT(sSrc[i]);
401 tmpRow[i*4+3] = 1.0;
402 }
403 break;
404 case GL_UNSIGNED_SHORT:
405 for (i = 0; i < width; i++) {
406 tmpRow[i*4+0] = tmpRow[i*4+1] = tmpRow[i*4+2]
407 = (GLfloat) USHORT_TO_FLOAT(usSrc[i]);
408 tmpRow[i*4+3] = 1.0;
409 }
410 break;
411 case GL_INT:
412 for (i = 0; i < width; i++) {
413 tmpRow[i*4+0] = tmpRow[i*4+1] = tmpRow[i*4+2]
414 = (GLfloat) INT_TO_FLOAT(iSrc[i]);
415 tmpRow[i*4+3] = 1.0;
416 }
417 break;
418 case GL_UNSIGNED_INT:
419 for (i = 0; i < width; i++) {
420 tmpRow[i*4+0] = tmpRow[i*4+1] = tmpRow[i*4+2]
421 = (GLfloat) UINT_TO_FLOAT(uiSrc[i]);
422 tmpRow[i*4+3] = 1.0;
423 }
424 break;
425 case GL_FLOAT:
426 for (i = 0; i < width; i++) {
427 tmpRow[i*4+0] = tmpRow[i*4+1] = tmpRow[i*4+2] = fSrc[i];
428 tmpRow[i*4+3] = 1.0;
429 }
430 break;
431 case GL_DOUBLE:
432 for (i = 0; i < width; i++) {
433 tmpRow[i*4+0] = tmpRow[i*4+1] = tmpRow[i*4+2] = (GLfloat) dSrc[i];
434 tmpRow[i*4+3] = 1.0;
435 }
436 break;
437 default:
438 crError("unexpected type in get_row in pixel.c");
439 }
440 }
441 else if (srcFormat == GL_INTENSITY) {
442 switch (srcType) {
443 case GL_BYTE:
444 for (i = 0; i < width; i++)
445 tmpRow[i*4+0] = tmpRow[i*4+1] = tmpRow[i*4+2] = tmpRow[i*4+3]
446 = (GLfloat) BYTE_TO_FLOAT(bSrc[i]);
447 break;
448 case GL_UNSIGNED_BYTE:
449 for (i = 0; i < width; i++)
450 tmpRow[i*4+0] = tmpRow[i*4+1] = tmpRow[i*4+2] = tmpRow[i*4+3]
451 = (GLfloat) UBYTE_TO_FLOAT(ubSrc[i]);
452 break;
453 case GL_SHORT:
454 for (i = 0; i < width; i++)
455 tmpRow[i*4+0] = tmpRow[i*4+1] = tmpRow[i*4+2] = tmpRow[i*4+3]
456 = (GLfloat) SHORT_TO_FLOAT(sSrc[i]);
457 break;
458 case GL_UNSIGNED_SHORT:
459 for (i = 0; i < width; i++)
460 tmpRow[i*4+0] = tmpRow[i*4+1] = tmpRow[i*4+2] = tmpRow[i*4+3]
461 = (GLfloat) USHORT_TO_FLOAT(usSrc[i]);
462 break;
463 case GL_INT:
464 for (i = 0; i < width; i++)
465 tmpRow[i*4+0] = tmpRow[i*4+1] = tmpRow[i*4+2] = tmpRow[i*4+3]
466 = (GLfloat) INT_TO_FLOAT(iSrc[i]);
467 break;
468 case GL_UNSIGNED_INT:
469 for (i = 0; i < width; i++)
470 tmpRow[i*4+0] = tmpRow[i*4+1] = tmpRow[i*4+2] = tmpRow[i*4+3]
471 = UINT_TO_FLOAT(uiSrc[i]);
472 break;
473 case GL_FLOAT:
474 for (i = 0; i < width; i++)
475 tmpRow[i*4+0] = tmpRow[i*4+1] = tmpRow[i*4+2] = tmpRow[i*4+3]
476 = fSrc[i];
477 break;
478 case GL_DOUBLE:
479 for (i = 0; i < width; i++)
480 tmpRow[i*4+0] = tmpRow[i*4+1] = tmpRow[i*4+2] = tmpRow[i*4+3]
481 = (GLfloat) dSrc[i];
482 break;
483 default:
484 crError("unexpected type in get_row in pixel.c");
485 }
486 }
487 else if (srcFormat == GL_LUMINANCE_ALPHA
488#ifdef CR_EXT_texture_sRGB
489 || srcFormat == GL_SLUMINANCE_ALPHA_EXT
490 || srcFormat == GL_SLUMINANCE8_ALPHA8_EXT
491#endif
492 ) {
493 switch (srcType) {
494 case GL_BYTE:
495 for (i = 0; i < width; i++) {
496 tmpRow[i*4+0] = tmpRow[i*4+1] = tmpRow[i*4+2]
497 = (GLfloat) BYTE_TO_FLOAT(bSrc[i*2+0]);
498 tmpRow[i*4+3] = (GLfloat) BYTE_TO_FLOAT(bSrc[i*2+1]);
499 }
500 break;
501 case GL_UNSIGNED_BYTE:
502 for (i = 0; i < width; i++) {
503 tmpRow[i*4+0] = tmpRow[i*4+1] = tmpRow[i*4+2]
504 = (GLfloat) UBYTE_TO_FLOAT(ubSrc[i*2+0]);
505 tmpRow[i*4+3] = (GLfloat) UBYTE_TO_FLOAT(ubSrc[i*2+1]);
506 }
507 break;
508 case GL_SHORT:
509 for (i = 0; i < width; i++) {
510 tmpRow[i*4+0] = tmpRow[i*4+1] = tmpRow[i*4+2]
511 = (GLfloat) SHORT_TO_FLOAT(sSrc[i*2+0]);
512 tmpRow[i*4+3] = (GLfloat) SHORT_TO_FLOAT(sSrc[i*2+1]);
513 }
514 break;
515 case GL_UNSIGNED_SHORT:
516 for (i = 0; i < width; i++) {
517 tmpRow[i*4+0] = tmpRow[i*4+1] = tmpRow[i*4+2]
518 = (GLfloat) USHORT_TO_FLOAT(usSrc[i*2+0]);
519 tmpRow[i*4+3] = (GLfloat) USHORT_TO_FLOAT(usSrc[i*2+1]);
520 }
521 break;
522 case GL_INT:
523 for (i = 0; i < width; i++) {
524 tmpRow[i*4+0] = tmpRow[i*4+1] = tmpRow[i*4+2]
525 = (GLfloat) INT_TO_FLOAT(iSrc[i*2+0]);
526 tmpRow[i*4+3] = (GLfloat) INT_TO_FLOAT(iSrc[i*2+1]);
527 }
528 break;
529 case GL_UNSIGNED_INT:
530 for (i = 0; i < width; i++) {
531 tmpRow[i*4+0] = tmpRow[i*4+1] = tmpRow[i*4+2]
532 = (GLfloat) UINT_TO_FLOAT(uiSrc[i*2+0]);
533 tmpRow[i*4+3] = (GLfloat) UINT_TO_FLOAT(uiSrc[i*2+1]);
534 }
535 break;
536 case GL_FLOAT:
537 for (i = 0; i < width; i++) {
538 tmpRow[i*4+0] = tmpRow[i*4+1] = tmpRow[i*4+2] = fSrc[i*2+0];
539 tmpRow[i*4+3] = fSrc[i*2+1];
540 }
541 break;
542 case GL_DOUBLE:
543 for (i = 0; i < width; i++) {
544 tmpRow[i*4+0] = tmpRow[i*4+1] = tmpRow[i*4+2]
545 = (GLfloat) dSrc[i*2+0];
546 tmpRow[i*4+3] = (GLfloat) dSrc[i*2+1];
547 }
548 break;
549 default:
550 crError("unexpected type in get_row in pixel.c");
551 }
552 }
553 else if (srcFormat == GL_RGB
554#ifdef CR_OPENGL_VERSION_1_2
555 || srcFormat == GL_BGR
556#endif
557#ifdef CR_EXT_texture_sRGB
558 || srcFormat == GL_SRGB_EXT
559 || srcFormat == GL_SRGB8_EXT
560#endif
561 ) {
562 int r, b;
563 if (srcFormat == GL_RGB
564#ifdef CR_EXT_texture_sRGB
565 || srcFormat == GL_SRGB_EXT
566 || srcFormat == GL_SRGB8_EXT
567#endif
568 ) {
569 r = 0; b = 2;
570 }
571 else {
572 r = 2; b = 0;
573 }
574 switch (srcType) {
575 case GL_BYTE:
576 for (i = 0; i < width; i++) {
577 tmpRow[i*4+0] = (GLfloat) BYTE_TO_FLOAT(bSrc[i*3+r]);
578 tmpRow[i*4+1] = (GLfloat) BYTE_TO_FLOAT(bSrc[i*3+1]);
579 tmpRow[i*4+2] = (GLfloat) BYTE_TO_FLOAT(bSrc[i*3+b]);
580 tmpRow[i*4+3] = 1.0;
581 }
582 break;
583 case GL_UNSIGNED_BYTE:
584 for (i = 0; i < width; i++) {
585 tmpRow[i*4+0] = (GLfloat) UBYTE_TO_FLOAT(ubSrc[i*3+r]);
586 tmpRow[i*4+1] = (GLfloat) UBYTE_TO_FLOAT(ubSrc[i*3+1]);
587 tmpRow[i*4+2] = (GLfloat) UBYTE_TO_FLOAT(ubSrc[i*3+b]);
588 tmpRow[i*4+3] = 1.0;
589 }
590 break;
591 case GL_SHORT:
592 for (i = 0; i < width; i++) {
593 tmpRow[i*4+0] = (GLfloat) SHORT_TO_FLOAT(sSrc[i*3+r]);
594 tmpRow[i*4+1] = (GLfloat) SHORT_TO_FLOAT(sSrc[i*3+1]);
595 tmpRow[i*4+2] = (GLfloat) SHORT_TO_FLOAT(sSrc[i*3+b]);
596 tmpRow[i*4+3] = 1.0;
597 }
598 break;
599 case GL_UNSIGNED_SHORT:
600 for (i = 0; i < width; i++) {
601 tmpRow[i*4+0] = (GLfloat) USHORT_TO_FLOAT(usSrc[i*3+r]);
602 tmpRow[i*4+1] = (GLfloat) USHORT_TO_FLOAT(usSrc[i*3+1]);
603 tmpRow[i*4+2] = (GLfloat) USHORT_TO_FLOAT(usSrc[i*3+b]);
604 tmpRow[i*4+3] = 1.0;
605 }
606 break;
607 case GL_INT:
608 for (i = 0; i < width; i++) {
609 tmpRow[i*4+0] = (GLfloat) INT_TO_FLOAT(iSrc[i*3+r]);
610 tmpRow[i*4+1] = (GLfloat) INT_TO_FLOAT(iSrc[i*3+1]);
611 tmpRow[i*4+2] = (GLfloat) INT_TO_FLOAT(iSrc[i*3+b]);
612 tmpRow[i*4+3] = 1.0;
613 }
614 break;
615 case GL_UNSIGNED_INT:
616 for (i = 0; i < width; i++) {
617 tmpRow[i*4+0] = (GLfloat) UINT_TO_FLOAT(uiSrc[i*3+r]);
618 tmpRow[i*4+1] = (GLfloat) UINT_TO_FLOAT(uiSrc[i*3+1]);
619 tmpRow[i*4+2] = (GLfloat) UINT_TO_FLOAT(uiSrc[i*3+b]);
620 tmpRow[i*4+3] = 1.0;
621 }
622 break;
623 case GL_FLOAT:
624 for (i = 0; i < width; i++) {
625 tmpRow[i*4+0] = fSrc[i*3+r];
626 tmpRow[i*4+1] = fSrc[i*3+1];
627 tmpRow[i*4+2] = fSrc[i*3+b];
628 tmpRow[i*4+3] = 1.0;
629 }
630 break;
631 case GL_DOUBLE:
632 for (i = 0; i < width; i++) {
633 tmpRow[i*4+0] = (GLfloat) dSrc[i*3+r];
634 tmpRow[i*4+1] = (GLfloat) dSrc[i*3+1];
635 tmpRow[i*4+2] = (GLfloat) dSrc[i*3+b];
636 tmpRow[i*4+3] = 1.0;
637 }
638 break;
639#ifdef CR_OPENGL_VERSION_1_2
640 case GL_UNSIGNED_BYTE_3_3_2:
641 for (i = 0; i < width; i++) {
642 tmpRow[i*4+r] = (GLfloat) ((ubSrc[i] >> 5) ) / 7.0f;
643 tmpRow[i*4+1] = (GLfloat) ((ubSrc[i] >> 2) & 0x7) / 7.0f;
644 tmpRow[i*4+b] = (GLfloat) ((ubSrc[i] ) & 0x3) / 3.0f;
645 tmpRow[i*4+3] = 1.0;
646 }
647 break;
648 case GL_UNSIGNED_BYTE_2_3_3_REV:
649 for (i = 0; i < width; i++) {
650 tmpRow[i*4+r] = (GLfloat) ((ubSrc[i] ) & 0x7) / 7.0f;
651 tmpRow[i*4+1] = (GLfloat) ((ubSrc[i] >> 3) & 0x7) / 7.0f;
652 tmpRow[i*4+b] = (GLfloat) ((ubSrc[i] >> 6) ) / 3.0f;
653 tmpRow[i*4+3] = 1.0;
654 }
655 break;
656 case GL_UNSIGNED_SHORT_5_6_5:
657 for (i = 0; i < width; i++) {
658 tmpRow[i*4+r] = (GLfloat) ((usSrc[i] >> 11) & 0x1f) / 31.0f;
659 tmpRow[i*4+1] = (GLfloat) ((usSrc[i] >> 5) & 0x3f) / 63.0f;
660 tmpRow[i*4+b] = (GLfloat) ((usSrc[i] ) & 0x1f) / 31.0f;
661 tmpRow[i*4+3] = 1.0;
662 }
663 break;
664 case GL_UNSIGNED_SHORT_5_6_5_REV:
665 for (i = 0; i < width; i++) {
666 tmpRow[i*4+r] = (GLfloat) ((usSrc[i] ) & 0x1f) / 31.0f;
667 tmpRow[i*4+1] = (GLfloat) ((usSrc[i] >> 5) & 0x3f) / 63.0f;
668 tmpRow[i*4+b] = (GLfloat) ((usSrc[i] >> 11) & 0x1f) / 31.0f;
669 tmpRow[i*4+3] = 1.0;
670 }
671 break;
672#endif
673 default:
674 crError("unexpected type in get_row in pixel.c");
675 }
676 }
677 else if (srcFormat == GL_RGBA
678#ifdef CR_OPENGL_VERSION_1_2
679 || srcFormat == GL_BGRA
680#endif
681#ifdef CR_EXT_texture_sRGB
682 || srcFormat == GL_SRGB_ALPHA_EXT
683 || srcFormat == GL_SRGB8_ALPHA8_EXT
684#endif
685 ) {
686 int r, b;
687 if (srcFormat == GL_RGBA
688#ifdef CR_EXT_texture_sRGB
689 || srcFormat == GL_SRGB_ALPHA_EXT
690 || srcFormat == GL_SRGB8_ALPHA8_EXT
691#endif
692 )
693 {
694 r = 0; b = 2;
695 }
696 else {
697 r = 2; b = 0;
698 }
699 switch (srcType) {
700 case GL_BYTE:
701 for (i = 0; i < width; i++) {
702 tmpRow[i*4+0] = (GLfloat) BYTE_TO_FLOAT(bSrc[i*4+r]);
703 tmpRow[i*4+1] = (GLfloat) BYTE_TO_FLOAT(bSrc[i*4+1]);
704 tmpRow[i*4+2] = (GLfloat) BYTE_TO_FLOAT(bSrc[i*4+b]);
705 tmpRow[i*4+3] = (GLfloat) BYTE_TO_FLOAT(bSrc[i*4+3]);
706 }
707 break;
708 case GL_UNSIGNED_BYTE:
709 for (i = 0; i < width; i++) {
710 tmpRow[i*4+0] = (GLfloat) UBYTE_TO_FLOAT(ubSrc[i*4+r]);
711 tmpRow[i*4+1] = (GLfloat) UBYTE_TO_FLOAT(ubSrc[i*4+1]);
712 tmpRow[i*4+2] = (GLfloat) UBYTE_TO_FLOAT(ubSrc[i*4+b]);
713 tmpRow[i*4+3] = (GLfloat) UBYTE_TO_FLOAT(ubSrc[i*4+3]);
714 }
715 break;
716 case GL_SHORT:
717 for (i = 0; i < width; i++) {
718 tmpRow[i*4+0] = (GLfloat) SHORT_TO_FLOAT(sSrc[i*4+r]);
719 tmpRow[i*4+1] = (GLfloat) SHORT_TO_FLOAT(sSrc[i*4+1]);
720 tmpRow[i*4+2] = (GLfloat) SHORT_TO_FLOAT(sSrc[i*4+b]);
721 tmpRow[i*4+3] = (GLfloat) SHORT_TO_FLOAT(sSrc[i*4+3]);
722 }
723 break;
724 case GL_UNSIGNED_SHORT:
725 for (i = 0; i < width; i++) {
726 tmpRow[i*4+0] = (GLfloat) USHORT_TO_FLOAT(usSrc[i*4+r]);
727 tmpRow[i*4+1] = (GLfloat) USHORT_TO_FLOAT(usSrc[i*4+1]);
728 tmpRow[i*4+2] = (GLfloat) USHORT_TO_FLOAT(usSrc[i*4+b]);
729 tmpRow[i*4+3] = (GLfloat) USHORT_TO_FLOAT(usSrc[i*4+3]);
730 }
731 break;
732 case GL_INT:
733 for (i = 0; i < width; i++) {
734 tmpRow[i*4+0] = (GLfloat) INT_TO_FLOAT(iSrc[i*4+r]);
735 tmpRow[i*4+1] = (GLfloat) INT_TO_FLOAT(iSrc[i*4+1]);
736 tmpRow[i*4+2] = (GLfloat) INT_TO_FLOAT(iSrc[i*4+b]);
737 tmpRow[i*4+3] = (GLfloat) INT_TO_FLOAT(iSrc[i*4+3]);
738 }
739 break;
740 case GL_UNSIGNED_INT:
741 for (i = 0; i < width; i++) {
742 tmpRow[i*4+0] = (GLfloat) UINT_TO_FLOAT(uiSrc[i*4+r]);
743 tmpRow[i*4+1] = (GLfloat) UINT_TO_FLOAT(uiSrc[i*4+1]);
744 tmpRow[i*4+2] = (GLfloat) UINT_TO_FLOAT(uiSrc[i*4+b]);
745 tmpRow[i*4+3] = (GLfloat) UINT_TO_FLOAT(uiSrc[i*4+3]);
746 }
747 break;
748 case GL_FLOAT:
749 for (i = 0; i < width; i++) {
750 tmpRow[i*4+0] = fSrc[i*4+r];
751 tmpRow[i*4+1] = fSrc[i*4+1];
752 tmpRow[i*4+2] = fSrc[i*4+b];
753 tmpRow[i*4+3] = fSrc[i*4+3];
754 }
755 break;
756 case GL_DOUBLE:
757 for (i = 0; i < width; i++) {
758 tmpRow[i*4+0] = (GLfloat) dSrc[i*4+r];
759 tmpRow[i*4+1] = (GLfloat) dSrc[i*4+1];
760 tmpRow[i*4+2] = (GLfloat) dSrc[i*4+b];
761 tmpRow[i*4+3] = (GLfloat) dSrc[i*4+3];
762 }
763 break;
764#ifdef CR_OPENGL_VERSION_1_2
765 case GL_UNSIGNED_SHORT_5_5_5_1:
766 for (i = 0; i < width; i++) {
767 tmpRow[i*4+r] = (GLfloat) ((usSrc[i] >> 11) ) / 31.0f;
768 tmpRow[i*4+1] = (GLfloat) ((usSrc[i] >> 6) & 0x1f) / 31.0f;
769 tmpRow[i*4+b] = (GLfloat) ((usSrc[i] >> 1) & 0x1f) / 31.0f;
770 tmpRow[i*4+3] = (GLfloat) ((usSrc[i] ) & 0x01);
771 }
772 break;
773 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
774 for (i = 0; i < width; i++) {
775 tmpRow[i*4+r] = (GLfloat) ((usSrc[i] ) & 0x1f) / 31.0f;
776 tmpRow[i*4+1] = (GLfloat) ((usSrc[i] >> 5) & 0x1f) / 31.0f;
777 tmpRow[i*4+b] = (GLfloat) ((usSrc[i] >> 10) & 0x1f) / 31.0f;
778 tmpRow[i*4+3] = (GLfloat) ((usSrc[i] >> 15) );
779 }
780 break;
781 case GL_UNSIGNED_SHORT_4_4_4_4:
782 for (i = 0; i < width; i++) {
783 tmpRow[i*4+r] = (GLfloat) ((usSrc[i] >> 12) & 0xf) / 15.0f;
784 tmpRow[i*4+1] = (GLfloat) ((usSrc[i] >> 8) & 0xf) / 15.0f;
785 tmpRow[i*4+b] = (GLfloat) ((usSrc[i] >> 4) & 0xf) / 15.0f;
786 tmpRow[i*4+3] = (GLfloat) ((usSrc[i] ) & 0xf) / 15.0f;
787 }
788 break;
789 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
790 for (i = 0; i < width; i++) {
791 tmpRow[i*4+r] = (GLfloat) ((usSrc[i] ) & 0xf) / 15.0f;
792 tmpRow[i*4+1] = (GLfloat) ((usSrc[i] >> 4) & 0xf) / 15.0f;
793 tmpRow[i*4+b] = (GLfloat) ((usSrc[i] >> 8) & 0xf) / 15.0f;
794 tmpRow[i*4+3] = (GLfloat) ((usSrc[i] >> 12) & 0xf) / 15.0f;
795 }
796 break;
797 case GL_UNSIGNED_INT_8_8_8_8:
798 for (i = 0; i < width; i++) {
799 tmpRow[i*4+r] = (GLfloat) ((uiSrc[i] >> 24) & 0xff) / 255.0f;
800 tmpRow[i*4+1] = (GLfloat) ((uiSrc[i] >> 16) & 0xff) / 255.0f;
801 tmpRow[i*4+b] = (GLfloat) ((uiSrc[i] >> 8) & 0xff) / 255.0f;
802 tmpRow[i*4+3] = (GLfloat) ((uiSrc[i] ) & 0xff) / 255.0f;
803 }
804 break;
805 case GL_UNSIGNED_INT_8_8_8_8_REV:
806 for (i = 0; i < width; i++) {
807 tmpRow[i*4+r] = (GLfloat) ((uiSrc[i] ) & 0xff) / 255.0f;
808 tmpRow[i*4+1] = (GLfloat) ((uiSrc[i] >> 8) & 0xff) / 255.0f;
809 tmpRow[i*4+b] = (GLfloat) ((uiSrc[i] >> 16) & 0xff) / 255.0f;
810 tmpRow[i*4+3] = (GLfloat) ((uiSrc[i] >> 24) & 0xff) / 255.0f;
811 }
812 break;
813 case GL_UNSIGNED_INT_10_10_10_2:
814 for (i = 0; i < width; i++) {
815 tmpRow[i*4+r] = (GLfloat) ((uiSrc[i] >> 22) & 0x3ff) / 1023.0f;
816 tmpRow[i*4+1] = (GLfloat) ((uiSrc[i] >> 12) & 0x3ff) / 1023.0f;
817 tmpRow[i*4+b] = (GLfloat) ((uiSrc[i] >> 2) & 0x3ff) / 1023.0f;
818 tmpRow[i*4+3] = (GLfloat) ((uiSrc[i] ) & 0x003) / 3.0f;
819 }
820 break;
821 case GL_UNSIGNED_INT_2_10_10_10_REV:
822 for (i = 0; i < width; i++) {
823 tmpRow[i*4+r] = (GLfloat) ((uiSrc[i] ) & 0x3ff) / 1023.0f;
824 tmpRow[i*4+1] = (GLfloat) ((uiSrc[i] >> 10) & 0x3ff) / 1023.0f;
825 tmpRow[i*4+b] = (GLfloat) ((uiSrc[i] >> 20) & 0x3ff) / 1023.0f;
826 tmpRow[i*4+3] = (GLfloat) ((uiSrc[i] >> 30) & 0x003) / 3.0f;
827 }
828 break;
829#endif
830 default:
831 crError("unexpected type in get_row in pixel.c");
832 }
833 }
834 else{
835 crError("unexpected source format in get_row in pixel.c");
836 }
837
838#ifdef CR_EXT_texture_sRGB
839 if (srcFormat == GL_SRGB_EXT
840 || srcFormat == GL_SRGB8_EXT
841 || srcFormat == GL_SRGB_ALPHA_EXT
842 || srcFormat == GL_SRGB8_ALPHA8_EXT
843 || srcFormat == GL_SLUMINANCE_ALPHA_EXT
844 || srcFormat == GL_SLUMINANCE8_ALPHA8_EXT
845 || srcFormat == GL_SLUMINANCE_EXT
846 || srcFormat == GL_SLUMINANCE8_EXT
847 )
848 {
849 for (i=0; i<width; ++i)
850 {
851 tmpRow[i*4+0] = SRGBF_TO_RGBF(tmpRow[i*4+0]);
852 tmpRow[i*4+1] = SRGBF_TO_RGBF(tmpRow[i*4+1]);
853 tmpRow[i*4+2] = SRGBF_TO_RGBF(tmpRow[i*4+2]);
854 }
855 }
856#endif
857}
858
859
860static void put_row(char *dst, GLenum dstFormat, GLenum dstType,
861 GLsizei width, GLfloat *tmpRow)
862{
863 GLbyte *bDst = (GLbyte *) dst;
864 GLubyte *ubDst = (GLubyte *) dst;
865 GLshort *sDst = (GLshort *) dst;
866 GLushort *usDst = (GLushort *) dst;
867 GLint *iDst = (GLint *) dst;
868 GLuint *uiDst = (GLuint *) dst;
869 GLfloat *fDst = (GLfloat *) dst;
870 GLdouble *dDst = (GLdouble *) dst;
871 int i;
872
873#ifdef CR_EXT_texture_sRGB
874 if (dstFormat == GL_SRGB_EXT
875 || dstFormat == GL_SRGB8_EXT
876 || dstFormat == GL_SRGB_ALPHA_EXT
877 || dstFormat == GL_SRGB8_ALPHA8_EXT
878 || dstFormat == GL_SLUMINANCE_ALPHA_EXT
879 || dstFormat == GL_SLUMINANCE8_ALPHA8_EXT
880 || dstFormat == GL_SLUMINANCE_EXT
881 || dstFormat == GL_SLUMINANCE8_EXT
882 )
883 {
884 for (i=0; i<width; ++i)
885 {
886 tmpRow[i*4+0] = RGBF_TO_SRGBF(tmpRow[i*4+0]);
887 tmpRow[i*4+1] = RGBF_TO_SRGBF(tmpRow[i*4+1]);
888 tmpRow[i*4+2] = RGBF_TO_SRGBF(tmpRow[i*4+2]);
889 }
890 }
891#endif
892
893 if (dstFormat == GL_COLOR_INDEX || dstFormat == GL_STENCIL_INDEX) {
894 switch (dstType) {
895 case GL_BYTE:
896 for (i = 0; i < width; i++)
897 bDst[i] = (GLbyte) tmpRow[i];
898 break;
899 case GL_UNSIGNED_BYTE:
900 for (i = 0; i < width; i++)
901 ubDst[i] = (GLubyte) tmpRow[i];
902 break;
903 case GL_SHORT:
904 for (i = 0; i < width; i++)
905 sDst[i] = (GLshort) tmpRow[i];
906 break;
907 case GL_UNSIGNED_SHORT:
908 for (i = 0; i < width; i++)
909 usDst[i] = (GLushort) tmpRow[i];
910 break;
911 case GL_INT:
912 for (i = 0; i < width; i++)
913 iDst[i] = (GLint) tmpRow[i];
914 break;
915 case GL_UNSIGNED_INT:
916 for (i = 0; i < width; i++)
917 uiDst[i] = (GLuint) tmpRow[i];
918 break;
919 case GL_FLOAT:
920 for (i = 0; i < width; i++)
921 fDst[i] = tmpRow[i];
922 break;
923 case GL_DOUBLE:
924 for (i = 0; i < width; i++)
925 dDst[i] = tmpRow[i];
926 break;
927 default:
928 crError("unexpected type in put_row in pixel.c");
929 }
930 }
931 else if (dstFormat == GL_DEPTH_COMPONENT) {
932 switch (dstType) {
933 case GL_BYTE:
934 for (i = 0; i < width; i++)
935 bDst[i] = FLOAT_TO_BYTE(tmpRow[i]);
936 break;
937 case GL_UNSIGNED_BYTE:
938 for (i = 0; i < width; i++)
939 ubDst[i] = FLOAT_TO_UBYTE(tmpRow[i]);
940 break;
941 case GL_SHORT:
942 for (i = 0; i < width; i++)
943 sDst[i] = FLOAT_TO_SHORT(tmpRow[i]);
944 break;
945 case GL_UNSIGNED_SHORT:
946 for (i = 0; i < width; i++)
947 sDst[i] = FLOAT_TO_SHORT(tmpRow[i]);
948 break;
949 case GL_INT:
950 for (i = 0; i < width; i++)
951 bDst[i] = (GLbyte) FLOAT_TO_INT(tmpRow[i]);
952 break;
953 case GL_UNSIGNED_INT:
954 for (i = 0; i < width; i++)
955 bDst[i] = (GLbyte) FLOAT_TO_UINT(tmpRow[i]);
956 break;
957 case GL_FLOAT:
958 for (i = 0; i < width; i++)
959 fDst[i] = tmpRow[i];
960 break;
961 case GL_DOUBLE:
962 for (i = 0; i < width; i++)
963 dDst[i] = tmpRow[i];
964 break;
965 default:
966 crError("unexpected type in put_row in pixel.c");
967 }
968 }
969 else if (dstFormat == GL_RED || dstFormat == GL_GREEN ||
970 dstFormat == GL_BLUE || dstFormat == GL_ALPHA ||
971 dstFormat == GL_LUMINANCE || dstFormat == GL_INTENSITY
972#ifdef CR_EXT_texture_sRGB
973 || dstFormat == GL_SLUMINANCE_EXT
974 || dstFormat == GL_SLUMINANCE8_EXT
975#endif
976 ) {
977 int index;
978 if (dstFormat == GL_RED)
979 index = 0;
980 else if (dstFormat == GL_LUMINANCE
981#ifdef CR_EXT_texture_sRGB
982 || dstFormat == GL_SLUMINANCE_EXT
983 || dstFormat == GL_SLUMINANCE8_EXT
984#endif
985 )
986 index = 0;
987 else if (dstFormat == GL_INTENSITY)
988 index = 0;
989 else if (dstFormat == GL_GREEN)
990 index = 1;
991 else if (dstFormat == GL_BLUE)
992 index = 2;
993 else
994 index = 3;
995 switch (dstType) {
996 case GL_BYTE:
997 for (i = 0; i < width; i++)
998 bDst[i] = FLOAT_TO_BYTE(tmpRow[i*4+index]);
999 break;
1000 case GL_UNSIGNED_BYTE:
1001 for (i = 0; i < width; i++)
1002 ubDst[i] = FLOAT_TO_UBYTE(tmpRow[i*4+index]);
1003 break;
1004 case GL_SHORT:
1005 for (i = 0; i < width; i++)
1006 sDst[i] = FLOAT_TO_SHORT(tmpRow[i*4+index]);
1007 break;
1008 case GL_UNSIGNED_SHORT:
1009 for (i = 0; i < width; i++)
1010 usDst[i] = FLOAT_TO_USHORT(tmpRow[i*4+index]);
1011 break;
1012 case GL_INT:
1013 for (i = 0; i < width; i++)
1014 iDst[i] = FLOAT_TO_INT(tmpRow[i*4+index]);
1015 break;
1016 case GL_UNSIGNED_INT:
1017 for (i = 0; i < width; i++)
1018 uiDst[i] = FLOAT_TO_UINT(tmpRow[i*4+index]);
1019 break;
1020 case GL_FLOAT:
1021 for (i = 0; i < width; i++)
1022 fDst[i] = tmpRow[i*4+index];
1023 break;
1024 case GL_DOUBLE:
1025 for (i = 0; i < width; i++)
1026 dDst[i] = tmpRow[i*4+index];
1027 break;
1028 default:
1029 crError("unexpected type in put_row in pixel.c");
1030 }
1031 }
1032 else if (dstFormat == GL_LUMINANCE_ALPHA
1033#ifdef CR_EXT_texture_sRGB
1034 || dstFormat == GL_SLUMINANCE_ALPHA_EXT
1035 || dstFormat == GL_SLUMINANCE8_ALPHA8_EXT
1036#endif
1037 ) {
1038 switch (dstType) {
1039 case GL_BYTE:
1040 for (i = 0; i < width; i++) {
1041 bDst[i*2+0] = FLOAT_TO_BYTE(tmpRow[i*4+0]);
1042 bDst[i*2+1] = FLOAT_TO_BYTE(tmpRow[i*4+3]);
1043 }
1044 break;
1045 case GL_UNSIGNED_BYTE:
1046 for (i = 0; i < width; i++) {
1047 ubDst[i*2+0] = FLOAT_TO_UBYTE(tmpRow[i*4+0]);
1048 ubDst[i*2+1] = FLOAT_TO_UBYTE(tmpRow[i*4+3]);
1049 }
1050 break;
1051 case GL_SHORT:
1052 for (i = 0; i < width; i++) {
1053 sDst[i*2+0] = FLOAT_TO_SHORT(tmpRow[i*4+0]);
1054 sDst[i*2+1] = FLOAT_TO_SHORT(tmpRow[i*4+3]);
1055 }
1056 break;
1057 case GL_UNSIGNED_SHORT:
1058 for (i = 0; i < width; i++) {
1059 usDst[i*2+0] = FLOAT_TO_USHORT(tmpRow[i*4+0]);
1060 usDst[i*2+1] = FLOAT_TO_USHORT(tmpRow[i*4+3]);
1061 }
1062 break;
1063 case GL_INT:
1064 for (i = 0; i < width; i++) {
1065 iDst[i*2+0] = FLOAT_TO_INT(tmpRow[i*4+0]);
1066 iDst[i*2+1] = FLOAT_TO_INT(tmpRow[i*4+3]);
1067 }
1068 break;
1069 case GL_UNSIGNED_INT:
1070 for (i = 0; i < width; i++) {
1071 uiDst[i*2+0] = FLOAT_TO_UINT(tmpRow[i*4+0]);
1072 uiDst[i*2+1] = FLOAT_TO_UINT(tmpRow[i*4+3]);
1073 }
1074 break;
1075 case GL_FLOAT:
1076 for (i = 0; i < width; i++) {
1077 fDst[i*2+0] = tmpRow[i*4+0];
1078 fDst[i*2+1] = tmpRow[i*4+3];
1079 }
1080 break;
1081 case GL_DOUBLE:
1082 for (i = 0; i < width; i++) {
1083 dDst[i*2+0] = tmpRow[i*4+0];
1084 dDst[i*2+1] = tmpRow[i*4+3];
1085 }
1086 break;
1087 default:
1088 crError("unexpected type in put_row in pixel.c");
1089 }
1090 }
1091 else if (dstFormat == GL_RGB
1092#ifdef CR_OPENGL_VERSION_1_2
1093 || dstFormat == GL_BGR
1094#endif
1095#ifdef CR_EXT_texture_sRGB
1096 || dstFormat == GL_SRGB_EXT
1097 || dstFormat == GL_SRGB8_EXT
1098#endif
1099 ) {
1100 int r, b;
1101 if (dstFormat == GL_RGB
1102#ifdef CR_EXT_texture_sRGB
1103 || dstFormat == GL_SRGB_EXT
1104 || dstFormat == GL_SRGB8_EXT
1105#endif
1106 ) {
1107 r = 0; b = 2;
1108 }
1109 else {
1110 r = 2; b = 0;
1111 }
1112 switch (dstType) {
1113 case GL_BYTE:
1114 for (i = 0; i < width; i++) {
1115 bDst[i*3+r] = FLOAT_TO_BYTE(tmpRow[i*4+0]);
1116 bDst[i*3+1] = FLOAT_TO_BYTE(tmpRow[i*4+1]);
1117 bDst[i*3+b] = FLOAT_TO_BYTE(tmpRow[i*4+2]);
1118 }
1119 break;
1120 case GL_UNSIGNED_BYTE:
1121 for (i = 0; i < width; i++) {
1122 ubDst[i*3+r] = FLOAT_TO_UBYTE(tmpRow[i*4+0]);
1123 ubDst[i*3+1] = FLOAT_TO_UBYTE(tmpRow[i*4+1]);
1124 ubDst[i*3+b] = FLOAT_TO_UBYTE(tmpRow[i*4+2]);
1125 }
1126 break;
1127 case GL_SHORT:
1128 for (i = 0; i < width; i++) {
1129 sDst[i*3+r] = FLOAT_TO_SHORT(tmpRow[i*4+0]);
1130 sDst[i*3+1] = FLOAT_TO_SHORT(tmpRow[i*4+1]);
1131 sDst[i*3+b] = FLOAT_TO_SHORT(tmpRow[i*4+2]);
1132 }
1133 break;
1134 case GL_UNSIGNED_SHORT:
1135 for (i = 0; i < width; i++) {
1136 usDst[i*3+r] = FLOAT_TO_USHORT(tmpRow[i*4+0]);
1137 usDst[i*3+1] = FLOAT_TO_USHORT(tmpRow[i*4+1]);
1138 usDst[i*3+b] = FLOAT_TO_USHORT(tmpRow[i*4+2]);
1139 }
1140 break;
1141 case GL_INT:
1142 for (i = 0; i < width; i++) {
1143 iDst[i*3+r] = FLOAT_TO_INT(tmpRow[i*4+0]);
1144 iDst[i*3+1] = FLOAT_TO_INT(tmpRow[i*4+1]);
1145 iDst[i*3+b] = FLOAT_TO_INT(tmpRow[i*4+2]);
1146 }
1147 break;
1148 case GL_UNSIGNED_INT:
1149 for (i = 0; i < width; i++) {
1150 uiDst[i*3+r] = FLOAT_TO_UINT(tmpRow[i*4+0]);
1151 uiDst[i*3+1] = FLOAT_TO_UINT(tmpRow[i*4+1]);
1152 uiDst[i*3+b] = FLOAT_TO_UINT(tmpRow[i*4+2]);
1153 }
1154 break;
1155 case GL_FLOAT:
1156 for (i = 0; i < width; i++) {
1157 fDst[i*3+r] = tmpRow[i*4+0];
1158 fDst[i*3+1] = tmpRow[i*4+1];
1159 fDst[i*3+b] = tmpRow[i*4+2];
1160 }
1161 break;
1162 case GL_DOUBLE:
1163 for (i = 0; i < width; i++) {
1164 dDst[i*3+r] = tmpRow[i*4+0];
1165 dDst[i*3+1] = tmpRow[i*4+1];
1166 dDst[i*3+b] = tmpRow[i*4+2];
1167 }
1168 break;
1169#ifdef CR_OPENGL_VERSION_1_2
1170 case GL_UNSIGNED_BYTE_3_3_2:
1171 for (i = 0; i < width; i++) {
1172 int red = (int) (tmpRow[i*4+r] * 7.0);
1173 int green = (int) (tmpRow[i*4+1] * 7.0);
1174 int blue = (int) (tmpRow[i*4+b] * 3.0);
1175 ubDst[i] = (red << 5) | (green << 2) | blue;
1176 }
1177 break;
1178 case GL_UNSIGNED_BYTE_2_3_3_REV:
1179 for (i = 0; i < width; i++) {
1180 int red = (int) (tmpRow[i*4+r] * 7.0);
1181 int green = (int) (tmpRow[i*4+1] * 7.0);
1182 int blue = (int) (tmpRow[i*4+b] * 3.0);
1183 ubDst[i] = red | (green << 3) | (blue << 6);
1184 }
1185 break;
1186 case GL_UNSIGNED_SHORT_5_6_5:
1187 for (i = 0; i < width; i++) {
1188 int red = (int) (tmpRow[i*4+r] * 31.0);
1189 int green = (int) (tmpRow[i*4+1] * 63.0);
1190 int blue = (int) (tmpRow[i*4+b] * 31.0);
1191 usDst[i] = (red << 11) | (green << 5) | blue;
1192 }
1193 break;
1194 case GL_UNSIGNED_SHORT_5_6_5_REV:
1195 for (i = 0; i < width; i++) {
1196 int red = (int) (tmpRow[i*4+r] * 31.0);
1197 int green = (int) (tmpRow[i*4+1] * 63.0);
1198 int blue = (int) (tmpRow[i*4+b] * 31.0);
1199 usDst[i] = (blue << 11) | (green << 5) | red;
1200 }
1201 break;
1202#endif
1203 default:
1204 crError("unexpected type in put_row in pixel.c");
1205 }
1206 }
1207 else if (dstFormat == GL_RGBA
1208#ifdef CR_OPENGL_VERSION_1_2
1209 || dstFormat == GL_BGRA
1210#endif
1211#ifdef CR_EXT_texture_sRGB
1212 || dstFormat == GL_SRGB_ALPHA_EXT
1213 || dstFormat == GL_SRGB8_ALPHA8_EXT
1214#endif
1215 ) {
1216 int r, b;
1217 if (dstFormat == GL_RGBA
1218#ifdef CR_EXT_texture_sRGB
1219 || dstFormat == GL_SRGB_ALPHA_EXT
1220 || dstFormat == GL_SRGB8_ALPHA8_EXT
1221#endif
1222 ) {
1223 r = 0; b = 2;
1224 }
1225 else {
1226 r = 2; b = 0;
1227 }
1228 switch (dstType) {
1229 case GL_BYTE:
1230 for (i = 0; i < width; i++) {
1231 bDst[i*4+r] = FLOAT_TO_BYTE(tmpRow[i*4+0]);
1232 bDst[i*4+1] = FLOAT_TO_BYTE(tmpRow[i*4+1]);
1233 bDst[i*4+b] = FLOAT_TO_BYTE(tmpRow[i*4+2]);
1234 bDst[i*4+3] = FLOAT_TO_BYTE(tmpRow[i*4+3]);
1235 }
1236 break;
1237 case GL_UNSIGNED_BYTE:
1238 for (i = 0; i < width; i++) {
1239 ubDst[i*4+r] = FLOAT_TO_UBYTE(tmpRow[i*4+0]);
1240 ubDst[i*4+1] = FLOAT_TO_UBYTE(tmpRow[i*4+1]);
1241 ubDst[i*4+b] = FLOAT_TO_UBYTE(tmpRow[i*4+2]);
1242 ubDst[i*4+3] = FLOAT_TO_UBYTE(tmpRow[i*4+3]);
1243 }
1244 break;
1245 case GL_SHORT:
1246 for (i = 0; i < width; i++) {
1247 sDst[i*4+r] = FLOAT_TO_SHORT(tmpRow[i*4+0]);
1248 sDst[i*4+1] = FLOAT_TO_SHORT(tmpRow[i*4+1]);
1249 sDst[i*4+b] = FLOAT_TO_SHORT(tmpRow[i*4+2]);
1250 sDst[i*4+3] = FLOAT_TO_SHORT(tmpRow[i*4+3]);
1251 }
1252 break;
1253 case GL_UNSIGNED_SHORT:
1254 for (i = 0; i < width; i++) {
1255 usDst[i*4+r] = FLOAT_TO_USHORT(tmpRow[i*4+0]);
1256 usDst[i*4+1] = FLOAT_TO_USHORT(tmpRow[i*4+1]);
1257 usDst[i*4+b] = FLOAT_TO_USHORT(tmpRow[i*4+2]);
1258 usDst[i*4+3] = FLOAT_TO_USHORT(tmpRow[i*4+3]);
1259 }
1260 break;
1261 case GL_INT:
1262 for (i = 0; i < width; i++) {
1263 iDst[i*4+r] = FLOAT_TO_INT(tmpRow[i*4+0]);
1264 iDst[i*4+1] = FLOAT_TO_INT(tmpRow[i*4+1]);
1265 iDst[i*4+b] = FLOAT_TO_INT(tmpRow[i*4+2]);
1266 iDst[i*4+3] = FLOAT_TO_INT(tmpRow[i*4+3]);
1267 }
1268 break;
1269 case GL_UNSIGNED_INT:
1270 for (i = 0; i < width; i++) {
1271 uiDst[i*4+r] = FLOAT_TO_UINT(tmpRow[i*4+0]);
1272 uiDst[i*4+1] = FLOAT_TO_UINT(tmpRow[i*4+1]);
1273 uiDst[i*4+b] = FLOAT_TO_UINT(tmpRow[i*4+2]);
1274 uiDst[i*4+3] = FLOAT_TO_UINT(tmpRow[i*4+3]);
1275 }
1276 break;
1277 case GL_FLOAT:
1278 for (i = 0; i < width; i++) {
1279 fDst[i*4+r] = tmpRow[i*4+0];
1280 fDst[i*4+1] = tmpRow[i*4+1];
1281 fDst[i*4+b] = tmpRow[i*4+2];
1282 fDst[i*4+3] = tmpRow[i*4+3];
1283 }
1284 break;
1285 case GL_DOUBLE:
1286 for (i = 0; i < width; i++) {
1287 dDst[i*4+r] = tmpRow[i*4+0];
1288 dDst[i*4+1] = tmpRow[i*4+1];
1289 dDst[i*4+b] = tmpRow[i*4+2];
1290 dDst[i*4+3] = tmpRow[i*4+3];
1291 }
1292 break;
1293#ifdef CR_OPENGL_VERSION_1_2
1294 case GL_UNSIGNED_SHORT_5_5_5_1:
1295 for (i = 0; i < width; i++) {
1296 int red = (int) (tmpRow[i*4+r] * 31.0);
1297 int green = (int) (tmpRow[i*4+1] * 31.0);
1298 int blue = (int) (tmpRow[i*4+b] * 31.0);
1299 int alpha = (int) (tmpRow[i*4+3]);
1300 usDst[i] = (red << 11) | (green << 6) | (blue << 1) | alpha;
1301 }
1302 break;
1303 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
1304 for (i = 0; i < width; i++) {
1305 int red = (int) (tmpRow[i*4+r] * 31.0);
1306 int green = (int) (tmpRow[i*4+1] * 31.0);
1307 int blue = (int) (tmpRow[i*4+b] * 31.0);
1308 int alpha = (int) (tmpRow[i*4+3]);
1309 usDst[i] = (alpha << 15) | (blue << 10) | (green << 5) | red;
1310 }
1311 break;
1312 case GL_UNSIGNED_SHORT_4_4_4_4:
1313 for (i = 0; i < width; i++) {
1314 int red = (int) (tmpRow[i*4+r] * 15.0f);
1315 int green = (int) (tmpRow[i*4+1] * 15.0f);
1316 int blue = (int) (tmpRow[i*4+b] * 15.0f);
1317 int alpha = (int) (tmpRow[i*4+3] * 15.0f);
1318 usDst[i] = (red << 12) | (green << 8) | (blue << 4) | alpha;
1319 }
1320 break;
1321 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1322 for (i = 0; i < width; i++) {
1323 int red = (int) (tmpRow[i*4+r] * 15.0f);
1324 int green = (int) (tmpRow[i*4+1] * 15.0f);
1325 int blue = (int) (tmpRow[i*4+b] * 15.0f);
1326 int alpha = (int) (tmpRow[i*4+3] * 15.0f);
1327 usDst[i] = (alpha << 12) | (blue << 8) | (green << 4) | red;
1328 }
1329 break;
1330 case GL_UNSIGNED_INT_8_8_8_8:
1331 for (i = 0; i < width; i++) {
1332 int red = (int) (tmpRow[i*4+r] * 255.0f);
1333 int green = (int) (tmpRow[i*4+1] * 255.0f);
1334 int blue = (int) (tmpRow[i*4+b] * 255.0f);
1335 int alpha = (int) (tmpRow[i*4+3] * 255.0f);
1336 uiDst[i] = (red << 24) | (green << 16) | (blue << 8) | alpha;
1337 }
1338 break;
1339 case GL_UNSIGNED_INT_8_8_8_8_REV:
1340 for (i = 0; i < width; i++) {
1341 int red = (int) (tmpRow[i*4+r] * 255.0f);
1342 int green = (int) (tmpRow[i*4+1] * 255.0f);
1343 int blue = (int) (tmpRow[i*4+b] * 255.0f);
1344 int alpha = (int) (tmpRow[i*4+3] * 255.0f);
1345 uiDst[i] = (alpha << 24) | (blue << 16) | (green << 8) | red;
1346 }
1347 break;
1348 case GL_UNSIGNED_INT_10_10_10_2:
1349 for (i = 0; i < width; i++) {
1350 int red = (int) (tmpRow[i*4+r] * 1023.0f);
1351 int green = (int) (tmpRow[i*4+1] * 1023.0f);
1352 int blue = (int) (tmpRow[i*4+b] * 1023.0f);
1353 int alpha = (int) (tmpRow[i*4+3] * 3.0f);
1354 uiDst[i] = (red << 22) | (green << 12) | (blue << 2) | alpha;
1355 }
1356 break;
1357 case GL_UNSIGNED_INT_2_10_10_10_REV:
1358 for (i = 0; i < width; i++) {
1359 int red = (int) (tmpRow[i*4+r] * 1023.0f);
1360 int green = (int) (tmpRow[i*4+1] * 1023.0f);
1361 int blue = (int) (tmpRow[i*4+b] * 1023.0f);
1362 int alpha = (int) (tmpRow[i*4+3] * 3.0f);
1363 uiDst[i] = (alpha << 30) | (blue << 20) | (green << 10) | red;
1364 }
1365 break;
1366#endif
1367 default:
1368 crError("unexpected type in put_row in pixel.c");
1369 }
1370 }
1371 else{
1372 crError("unexpected dest type in put_row in pixel.c");
1373 }
1374}
1375
1376#ifdef _MSC_VER
1377# pragma optimize("", on) /* bird: see #pragma optimize("g", off) above. */
1378#endif
1379
1380/**
1381 * Byte-swap an array of GLushorts
1382 */
1383static void
1384swap2(GLushort *us, GLuint n)
1385{
1386 GLuint i;
1387 for (i = 0; i < n; i++) {
1388 us[i] = (us[i] >> 8) | (us[i] << 8);
1389 }
1390}
1391
1392
1393/**
1394 * Byte-swap an array of GLuints
1395 */
1396static void
1397swap4(GLuint *ui, GLuint n)
1398{
1399 GLuint i;
1400
1401 for (i = 0; i < n; i++) {
1402 GLuint b = ui[i];
1403 ui[i] = (b >> 24)
1404 | ((b >> 8) & 0xff00)
1405 | ((b << 8) & 0xff0000)
1406 | ((b << 24) & 0xff000000);
1407 }
1408}
1409
1410
1411/**
1412 * Return number of bytes of storage needed to accomodate an
1413 * image with the given format, type, and size.
1414 * \return size in bytes or -1 if bad format or type
1415 */
1416unsigned int crImageSize( GLenum format, GLenum type, GLsizei width, GLsizei height )
1417{
1418 unsigned int bytes = width * height;
1419
1420 if (type == GL_BITMAP)
1421 {
1422 /* This was wrong in the old code! */
1423 bytes = ((width + 7) / 8) * height;
1424 }
1425 else if (GL_DEPTH_COMPONENT==format && type!=GL_FLOAT)
1426 {
1427 /*GL_DEPTH_COMPONENT with GL_UNSIGNED_BYTE seems to be more than 1 byte per pixel*/
1428 bytes = 4 * width * height * crPixelSize( format, type );
1429 }
1430 else
1431 {
1432 bytes = width * height * crPixelSize( format, type );
1433 }
1434
1435 return bytes;
1436}
1437
1438/**
1439 * Return number of bytes of storage needed to accomodate a
1440 * 3D texture with the give format, type, and size.
1441 * \return size in bytes or -1 if bad format or type
1442 */
1443unsigned int crTextureSize( GLenum format, GLenum type, GLsizei width, GLsizei height, GLsizei depth )
1444{
1445 unsigned int bytes = width * height;
1446
1447 if (type == GL_BITMAP)
1448 {
1449 /*
1450 * Not sure about this one, so just multiply
1451 * by the depth?
1452 */
1453 bytes = ((width + 7) / 8) * height * depth;
1454 }
1455 else
1456 {
1457 bytes = width * height * depth * crPixelSize( format, type );
1458 }
1459
1460 return bytes;
1461}
1462
1463static const CRPixelPackState defaultPacking = {
1464 0, /* rowLength */
1465 0, /* skipRows */
1466 0, /* skipPixels */
1467 1, /* alignment */
1468 0, /* imageHeight */
1469 0, /* skipImages */
1470 GL_FALSE, /* swapBytes */
1471 GL_FALSE /* psLSBFirst */
1472};
1473
1474
1475void crPixelCopy1D( GLvoid *dstPtr, GLenum dstFormat, GLenum dstType,
1476 const GLvoid *srcPtr, GLenum srcFormat, GLenum srcType,
1477 GLsizei width, const CRPixelPackState *srcPacking )
1478{
1479 crPixelCopy2D( width, 1,
1480 dstPtr, dstFormat, dstType, NULL, /* dst */
1481 srcPtr, srcFormat, srcType, srcPacking ); /* src */
1482}
1483
1484void crPixelCopy2D( GLsizei width, GLsizei height,
1485 GLvoid *dstPtr, GLenum dstFormat, GLenum dstType,
1486 const CRPixelPackState *dstPacking,
1487 const GLvoid *srcPtr, GLenum srcFormat, GLenum srcType,
1488 const CRPixelPackState *srcPacking )
1489
1490{
1491 const char *src = (const char *) srcPtr;
1492 char *dst = (char *) dstPtr;
1493 int srcBytesPerPixel;
1494 int dstBytesPerPixel;
1495 int srcBytesPerRow;
1496 int dstBytesPerRow;
1497 int srcRowStrideBytes;
1498 int dstRowStrideBytes;
1499 int bytesPerRow;
1500 int i;
1501
1502 if (!dstPacking)
1503 dstPacking = &defaultPacking;
1504
1505 if (!srcPacking)
1506 srcPacking = &defaultPacking;
1507
1508 if (srcType == GL_BITMAP)
1509 {
1510 CRASSERT(dstType == GL_BITMAP);
1511 bytesPerRow = (width + 7) / 8;
1512 if (srcPacking->rowLength > 0)
1513 srcRowStrideBytes = (srcPacking->rowLength + 7) / 8;
1514 else
1515 srcRowStrideBytes = bytesPerRow;
1516 dstRowStrideBytes = bytesPerRow;
1517
1518 for (i=0; i<height; i++) {
1519 crMemcpy( (void *) dst, (const void *) src, bytesPerRow );
1520 dst += dstRowStrideBytes;
1521 src += srcRowStrideBytes;
1522 }
1523 }
1524 else
1525 {
1526 CRASSERT(dstType != GL_BITMAP);
1527 srcBytesPerPixel = crPixelSize( srcFormat, srcType );
1528 dstBytesPerPixel = crPixelSize( dstFormat, dstType );
1529 if (srcBytesPerPixel < 0 || dstBytesPerPixel < 0)
1530 return;
1531
1532 /* Stride between rows (in bytes) */
1533 if (srcPacking->rowLength > 0)
1534 srcRowStrideBytes = srcPacking->rowLength * srcBytesPerPixel;
1535 else
1536 srcRowStrideBytes = width * srcBytesPerPixel;
1537
1538 if (dstPacking->rowLength > 0)
1539 dstRowStrideBytes = dstPacking->rowLength * dstBytesPerPixel;
1540 else
1541 dstRowStrideBytes = width * dstBytesPerPixel;
1542
1543 /* bytes per row */
1544 srcBytesPerRow = width * srcBytesPerPixel;
1545 dstBytesPerRow = width * dstBytesPerPixel;
1546
1547 /* handle the alignment */
1548 if (srcPacking->alignment != 1) {
1549 i = ((long) src) % srcPacking->alignment;
1550 if (i)
1551 src += srcPacking->alignment - i;
1552 i = (long) srcRowStrideBytes % srcPacking->alignment;
1553 if (i)
1554 srcRowStrideBytes += srcPacking->alignment - i;
1555 }
1556
1557 if (dstPacking->alignment != 1) {
1558 i = ((long) dst) % dstPacking->alignment;
1559 if (i)
1560 dst += dstPacking->alignment - i;
1561 i = (long) dstRowStrideBytes % dstPacking->alignment;
1562 if (i)
1563 dstRowStrideBytes += dstPacking->alignment - i;
1564 }
1565
1566 /* handle skip rows */
1567 src += srcPacking->skipRows * srcRowStrideBytes;
1568 dst += dstPacking->skipRows * dstRowStrideBytes;
1569
1570 /* handle skip pixels */
1571 src += srcPacking->skipPixels * srcBytesPerPixel;
1572 dst += dstPacking->skipPixels * dstBytesPerPixel;
1573
1574 /* we don't do LSBFirst yet */
1575 if (srcPacking->psLSBFirst)
1576 crError( "Sorry, no lsbfirst for you" );
1577 if (dstPacking->psLSBFirst)
1578 crError( "Sorry, no lsbfirst for you" );
1579
1580 if (srcFormat == dstFormat && srcType == dstType)
1581 {
1582 CRASSERT(srcBytesPerRow == dstBytesPerRow);
1583
1584 if (srcBytesPerRow==srcRowStrideBytes
1585 && srcRowStrideBytes==dstRowStrideBytes)
1586 {
1587 crMemcpy( (void *) dst, (const void *) src, height * srcBytesPerRow );
1588 }
1589 else
1590 //crDebug("Sending texture, BytesPerRow!=RowStrideBytes");
1591 for (i = 0; i < height; i++)
1592 {
1593 crMemcpy( (void *) dst, (const void *) src, srcBytesPerRow );
1594#if 0
1595 /* check if src XOR dst swapping */
1596 if (srcPacking->swapBytes ^ dstPacking->swapBytes) {
1597 const GLint size = crSizeOfType(srcType);
1598 CRASSERT(srcType == dstType);
1599 if (size == 2) {
1600 swap2((GLushort *) dst, srcBytesPerRow / size);
1601 }
1602 else if (size == 4) {
1603 swap4((GLuint *) dst, srcBytesPerRow / size);
1604 }
1605 }
1606#endif
1607 dst += dstRowStrideBytes;
1608 src += srcRowStrideBytes;
1609 }
1610 }
1611 else
1612 {
1613 /* need to do format and/or type conversion */
1614 char *swapRow = NULL;
1615 GLfloat *tmpRow = crAlloc( 4 * width * sizeof(GLfloat) );
1616
1617 crDebug("Converting texture format");
1618
1619 if (!tmpRow)
1620 crError("Out of memory in crPixelCopy2D");
1621
1622 if (srcPacking->swapBytes) {
1623 swapRow = (char *) crAlloc(width * srcBytesPerPixel);
1624 if (!swapRow) {
1625 crError("Out of memory in crPixelCopy2D");
1626 }
1627 }
1628
1629 for (i = 0; i < height; i++)
1630 {
1631 /* get src row as floats */
1632 if (srcPacking->swapBytes) {
1633 const GLint size = crSizeOfType(srcType);
1634 const GLint bytes = width * srcBytesPerPixel;
1635 crMemcpy(swapRow, src, bytes);
1636 if (size == 2)
1637 swap2((GLushort *) swapRow, bytes / 2);
1638 else if (size == 4)
1639 swap4((GLuint *) swapRow, bytes / 4);
1640 get_row(swapRow, srcFormat, srcType, width, tmpRow);
1641 }
1642 else {
1643 get_row(src, srcFormat, srcType, width, tmpRow);
1644 }
1645
1646 /* store floats in dest row */
1647 if (dstPacking->swapBytes) {
1648 const GLint size = crSizeOfType(dstType);
1649 const GLint bytes = dstBytesPerPixel * width;
1650 put_row(dst, dstFormat, dstType, width, tmpRow);
1651 if (size == 2)
1652 swap2((GLushort *) dst, bytes / 2);
1653 else if (size == 4)
1654 swap4((GLuint *) dst, bytes / 4);
1655 }
1656 else {
1657 put_row(dst, dstFormat, dstType, width, tmpRow);
1658 }
1659
1660 /* increment pointers for next row */
1661 dst += dstRowStrideBytes;
1662 src += srcRowStrideBytes;
1663 }
1664
1665 crFree(tmpRow);
1666 if (swapRow)
1667 crFree(swapRow);
1668 }
1669 }
1670}
1671
1672void crPixelCopy3D( GLsizei width, GLsizei height, GLsizei depth,
1673 GLvoid *dstPtr, GLenum dstFormat, GLenum dstType,
1674 const CRPixelPackState *dstPacking,
1675 const GLvoid *srcPtr, GLenum srcFormat, GLenum srcType,
1676 const CRPixelPackState *srcPacking )
1677
1678{
1679 int tex_size = 0;
1680
1681 (void)srcPacking;
1682 (void)srcType;
1683 (void)srcFormat;
1684 (void)dstPacking;
1685
1686 /*@todo this should be implemented properly*/
1687
1688 crWarning( "crPixelCopy3D: simply crMemcpy'ing from srcPtr to dstPtr" );
1689 if (dstFormat != srcFormat)
1690 crWarning( "crPixelCopy3D: formats don't match!" );
1691 if (dstType != srcType)
1692 crWarning( "crPixelCopy3D: formats don't match!" );
1693
1694 tex_size = RT_MIN (crTextureSize( dstFormat, dstType, width, height, depth ),
1695 crTextureSize( srcFormat, srcType, width, height, depth ));
1696 crMemcpy( (void *) dstPtr, (void *) srcPtr, tex_size );
1697}
1698
1699/* Round N up to the next multiple of 8 */
1700#define CEIL8(N) (((N) + 7) & ~0x7)
1701
1702void crBitmapCopy( GLsizei width, GLsizei height, GLubyte *dstPtr,
1703 const GLubyte *srcPtr, const CRPixelPackState *srcPacking )
1704{
1705 if (srcPacking->psLSBFirst == GL_FALSE &&
1706 (srcPacking->rowLength == 0 || srcPacking->rowLength == width) &&
1707 srcPacking->skipRows == 0 &&
1708 srcPacking->skipPixels == 0 &&
1709 srcPacking->alignment == 1) {
1710 /* simple case */
1711 crMemcpy(dstPtr, srcPtr, CEIL8(width) * height / 8);
1712 }
1713 else {
1714 /* general case */
1715 const GLubyte *srcRow;
1716 const GLint dst_row_length = CEIL8(width) / 8;
1717 GLubyte *dstRow;
1718 GLint src_row_length;
1719 GLint i, j;
1720
1721 if (srcPacking->rowLength > 0)
1722 src_row_length = srcPacking->rowLength;
1723 else
1724 src_row_length = width;
1725
1726 switch (srcPacking->alignment) {
1727 case 1:
1728 src_row_length = ( ( src_row_length + 7 ) & ~7 ) >> 3;
1729 break;
1730 case 2:
1731 src_row_length = ( ( src_row_length + 15 ) & ~15 ) >> 3;
1732 break;
1733 case 4:
1734 src_row_length = ( ( src_row_length + 31 ) & ~31 ) >> 3;
1735 break;
1736 case 8:
1737 src_row_length = ( ( src_row_length + 63 ) & ~63 ) >> 3;
1738 break;
1739 default:
1740 crError( "Invalid unpack alignment in crBitmapCopy");
1741 return;
1742 }
1743
1744 /* src_row_length and dst_row_length are in bytes */
1745
1746 srcRow = srcPtr + src_row_length * srcPacking->skipRows;
1747 dstRow = dstPtr;
1748
1749 if (srcPacking->psLSBFirst) {
1750 for (j = 0; j < height; j++) {
1751 crMemZero(dstRow, dst_row_length);
1752 for (i = 0; i < width; i++) {
1753 const GLint iByte = (i + srcPacking->skipPixels) / 8;
1754 const GLint iBit = (i + srcPacking->skipPixels) % 8;
1755 const GLubyte b = srcRow[iByte];
1756 if (b & (1 << iBit))
1757 dstRow[i / 8] |= (128 >> (i % 8));
1758 }
1759 srcRow += src_row_length;
1760 dstRow += dst_row_length;
1761 }
1762 }
1763 else {
1764 /* unpack MSB first */
1765 for (j = 0; j < height; j++) {
1766 crMemZero(dstRow, dst_row_length);
1767 for (i = 0; i < width; i++) {
1768 const GLint iByte = (i + srcPacking->skipPixels) / 8;
1769 const GLint iBit = (i + srcPacking->skipPixels) % 8;
1770 const GLubyte b = srcRow[iByte];
1771 if (b & (128 >> iBit))
1772 dstRow[i / 8] |= (128 >> (i % 8));
1773 }
1774 srcRow += src_row_length;
1775 dstRow += dst_row_length;
1776 }
1777 }
1778 }
1779}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette