Debian Codegraph @

debian/external_deps/fetk/sg/src/gl/image.c

raw
1 /* $Id: image.c,v 1.2 2000/10/27 15:21:40 mholst Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 2.2
6 * Copyright (C) 1995-1997 Brian Paul
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the Free
20 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include "image.h"
27 #include "macros.h"
28 #include "pixel.h"
29 #include "types.h"
30
31
32
33
34 /*
35 * Flip the 8 bits in each byte of the given array.
36 */
37 void gl_flip_bytes( GLubyte *p, GLuint n )
38 {
39 register GLuint i, a, b;
40
41 for (i=0;i<n;i++) {
42 b = (GLuint) p[i];
43 a = ((b & 0x01) << 7) |
44 ((b & 0x02) << 5) |
45 ((b & 0x04) << 3) |
46 ((b & 0x08) << 1) |
47 ((b & 0x10) >> 1) |
48 ((b & 0x20) >> 3) |
49 ((b & 0x40) >> 5) |
50 ((b & 0x80) >> 7);
51 p[i] = (GLubyte) a;
52 }
53 }
54
55
56 /*
57 * Flip the order of the 2 bytes in each word in the given array.
58 */
59 void gl_swap2( GLushort *p, GLuint n )
60 {
61 register GLuint i;
62
63 for (i=0;i<n;i++) {
64 p[i] = (p[i] >> 8) | ((p[i] << 8) & 0xff00);
65 }
66 }
67
68
69
70 /*
71 * Flip the order of the 4 bytes in each word in the given array.
72 */
73 void gl_swap4( GLuint *p, GLuint n )
74 {
75 register GLuint i, a, b;
76
77 for (i=0;i<n;i++) {
78 b = p[i];
79 a = (b >> 24)
80 | ((b >> 8) & 0xff00)
81 | ((b << 8) & 0xff0000)
82 | ((b << 24) & 0xff000000);
83 p[i] = a;
84 }
85 }
86
87
88
89
90 /*
91 * Return the size, in bytes, of the given GL datatype.
92 * Return 0 if GL_BITMAP.
93 * Return -1 if invalid type enum.
94 */
95 GLint gl_sizeof_type( GLenum type )
96 {
97 switch (type) {
98 case GL_BITMAP:
99 return 0;
100 case GL_UNSIGNED_BYTE:
101 return sizeof(GLubyte);
102 case GL_BYTE:
103 return sizeof(GLbyte);
104 case GL_UNSIGNED_SHORT:
105 return sizeof(GLushort);
106 case GL_SHORT:
107 return sizeof(GLshort);
108 case GL_UNSIGNED_INT:
109 return sizeof(GLuint);
110 case GL_INT:
111 return sizeof(GLint);
112 case GL_FLOAT:
113 return sizeof(GLfloat);
114 default:
115 return -1;
116 }
117 }
118
119
120
121 /*
122 * Return the number of components in a GL enum pixel type.
123 * Return -1 if bad format.
124 */
125 GLint gl_components_in_format( GLenum format )
126 {
127 switch (format) {
128 case GL_COLOR_INDEX:
129 case GL_STENCIL_INDEX:
130 case GL_DEPTH_COMPONENT:
131 case GL_RED:
132 case GL_GREEN:
133 case GL_BLUE:
134 case GL_ALPHA:
135 case GL_LUMINANCE:
136 return 1;
137 case GL_LUMINANCE_ALPHA:
138 return 2;
139 case GL_RGB:
140 return 3;
141 case GL_RGBA:
142 return 4;
143 default:
144 return -1;
145 }
146 }
147
148
149 /*
150 * Return the address of a pixel in an image (actually a volume).
151 * Pixel unpacking/packing parameters are observed according to 'packing'.
152 * Input: image - start of image data
153 * width, height - size of image
154 * format - image format
155 * type - pixel component type
156 * packing - GL_TRUE = use packing params
157 * GL_FALSE = use unpacking params.
158 * img - which image in the volume (0 for 2-D images)
159 * row, column - location of pixel in the image
160 * Return: address of pixel at (image,row,column) in image or NULL if error.
161 */
162 GLvoid *gl_pixel_addr_in_image( struct gl_pixelstore_attrib *packing,
163 const GLvoid *image, GLsizei width,
164 GLsizei height, GLenum format, GLenum type,
165 GLint img, GLint row, GLint column )
166 {
167 GLint bytes_per_comp; /* bytes per component */
168 GLint comp_per_pixel; /* components per pixel */
169 GLint comps_per_row; /* components per row */
170 GLint pixels_per_row; /* pixels per row */
171 GLint bytes_per_image;
172 GLint rows_per_image;
173 GLint alignment; /* 1, 2 or 4 */
174 GLint skiprows;
175 GLint skippixels;
176 GLint skipimages; /* for 3-D */
177 GLubyte *pixel_addr;
178
179 /* Compute bytes per component */
180 bytes_per_comp = gl_sizeof_type( type );
181 if (bytes_per_comp<0) {
182 return NULL;
183 }
184
185 /* Compute number of components per pixel */
186 comp_per_pixel = gl_components_in_format( format );
187 if (comp_per_pixel<0) {
188 return NULL;
189 }
190
191 alignment = packing->Alignment;
192 if (packing->RowLength>0) {
193 pixels_per_row = packing->RowLength;
194 }
195 else {
196 pixels_per_row = width;
197 }
198 if (packing->ImageHeight>0) {
199 rows_per_image = packing->ImageHeight;
200 }
201 else {
202 rows_per_image = height;
203 }
204 skiprows = packing->SkipRows;
205 skippixels = packing->SkipPixels;
206 skipimages = packing->SkipImages;
207
208 if (type==GL_BITMAP) {
209 /* BITMAP data */
210 GLint bytes_per_row;
211
212 bytes_per_row = alignment
213 * CEILING( comp_per_pixel*pixels_per_row, 8*alignment );
214
215 bytes_per_image = bytes_per_row * rows_per_image;
216
217 pixel_addr = (GLubyte *) image
218 + (skipimages + img) * bytes_per_image
219 + (skiprows + row) * bytes_per_row
220 + (skippixels + column) / 8;
221 }
222 else {
223 /* Non-BITMAP data */
224
225 if (bytes_per_comp>=alignment) {
226 comps_per_row = comp_per_pixel * pixels_per_row;
227 }
228 else {
229 GLint bytes_per_row = bytes_per_comp * comp_per_pixel
230 * pixels_per_row;
231
232 comps_per_row = alignment / bytes_per_comp
233 * CEILING( bytes_per_row, alignment );
234 }
235
236 bytes_per_image = bytes_per_comp * comps_per_row * rows_per_image;
237
238 /* Copy/unpack pixel data to buffer */
239 pixel_addr = (GLubyte *) image
240 + (skipimages + img) * bytes_per_image
241 + (skiprows + row) * bytes_per_comp * comps_per_row
242 + (skippixels + column) * bytes_per_comp * comp_per_pixel;
243 }
244
245 return (GLvoid *) pixel_addr;
246 }
247
248
249
250 /*
251 * Unpack a 2-D image from user-supplied address, returning a pointer to
252 * a new gl_image struct.
253 * This function is always called by a higher-level unpack function such
254 * as gl_unpack_texsubimage() or gl_unpack_bitmap().
255 *
256 * Input: width, height - size in pixels
257 * srcFormat - format of incoming pixel data, ignored
258 * if srcType and destType is BITMAP.
259 * srcType - GL_UNSIGNED_BYTE .. GL_FLOAT
260 * destType - store image as GL_UNSIGNED_BYTE, GL_FLOAT, or GL_BITMAP.
261 * if GL_UNSIGNED_BYTE, srctype must be GL_UNSIGNED_BYTE.
262 * if GL_BITMAP, srctype must be GL_BITMAP.
263 * interleave - if TRUE, srctype and destType must be GL_UNSIGNED_BYTE
264 * pixels - pointer to unpacked image.
265 */
266 struct gl_image *gl_unpack_image( GLcontext *ctx,
267 GLint width, GLint height,
268 GLenum srcFormat, GLenum srcType,
269 GLenum destType,
270 const GLvoid *pixels,
271 GLboolean interleave )
272 {
273 return gl_unpack_image3D(ctx, width, height, 1,
274 srcFormat, srcType, destType, pixels, interleave);
275 }
276
277
278
279 /*
280 * Unpack a 2-D/3-D image from user-supplied address, returning a pointer to
281 * a new gl_image struct.
282 * This function is always called by a higher-level unpack function such
283 * as gl_unpack_texsubimage() or gl_unpack_bitmap().
284 *
285 * Input: width, height, depth - size in pixels
286 * srcFormat - format of incoming pixel data, ignored
287 * if srcType and destType is BITMAP.
288 * srcType - GL_UNSIGNED_BYTE .. GL_FLOAT
289 * destType - store image as GL_UNSIGNED_BYTE, GL_FLOAT, or GL_BITMAP.
290 * if GL_UNSIGNED_BYTE, srctype must be GL_UNSIGNED_BYTE.
291 * if GL_BITMAP, srctype must be GL_BITMAP.
292 * interleave - if TRUE, srctype and destType must be GL_UNSIGNED_BYTE
293 * pixels - pointer to unpacked image.
294 */
295 struct gl_image *gl_unpack_image3D( GLcontext *ctx,
296 GLint width, GLint height, GLint depth,
297 GLenum srcFormat, GLenum srcType,
298 GLenum destType,
299 const GLvoid *pixels,
300 GLboolean interleave )
301 {
302 GLint components;
303
304 components = gl_components_in_format( srcFormat );
305
306 if (srcType==GL_BITMAP || destType==GL_BITMAP) {
307 struct gl_image *image;
308 GLint bytes, i, width_in_bytes, d;
309 GLubyte *buffer, *dst;
310 assert( srcType==GL_BITMAP );
311 assert( destType==GL_BITMAP );
312
313 /* Alloc dest storage */
314 bytes = ((width+7)/8 * height) * depth;
315 if (bytes>0 && pixels!=NULL) {
316 buffer = (GLubyte *) malloc( bytes );
317 if (!buffer) {
318 return NULL;
319 }
320 /* Copy/unpack pixel data to buffer */
321 width_in_bytes = CEILING( width, 8 );
322 dst = buffer;
323 for (d=0; d<depth; d++) {
324 for (i=0; i<height; i++) {
325 GLvoid *src = gl_pixel_addr_in_image( &ctx->Unpack, pixels,
326 width, height,
327 GL_COLOR_INDEX, srcType,
328 d, i, 0 );
329 if (!src) {
330 free(buffer);
331 return NULL;
332 }
333 MEMCPY( dst, src, width_in_bytes );
334 dst += width_in_bytes;
335 }
336 }
337 /* Bit flipping */
338 if (ctx->Unpack.LsbFirst) {
339 gl_flip_bytes( buffer, bytes );
340 }
341 }
342 else {
343 /* a 'null' bitmap */
344 buffer = NULL;
345 }
346
347 image = (struct gl_image *) malloc( sizeof(struct gl_image) );
348 if (image) {
349 image->Width = width;
350 image->Height = height;
351 image->Depth = depth;
352 image->Components = 0;
353 image->Format = GL_COLOR_INDEX;
354 image->Type = GL_BITMAP;
355 image->Interleaved = GL_FALSE;
356 image->Data = buffer;
357 image->RefCount = 0;
358 }
359 else {
360 free( buffer );
361 return NULL;
362 }
363 return image;
364 }
365 else if (srcFormat==GL_DEPTH_COMPONENT) {
366 /* TODO: pack as GLdepth values (GLushort or GLuint) */
367
368 }
369 else if (srcFormat==GL_STENCIL_INDEX) {
370 /* TODO: pack as GLstencil (GLubyte or GLushort) */
371
372 }
373 else if (destType==GL_UNSIGNED_BYTE) {
374 struct gl_image *image;
375 GLint width_in_bytes;
376 GLubyte *buffer, *dst;
377 GLint i, d;
378 assert( srcType==GL_UNSIGNED_BYTE );
379
380 width_in_bytes = width * components * sizeof(GLubyte);
381 buffer = malloc( height * width_in_bytes * depth );
382 if (!buffer) {
383 return NULL;
384 }
385 /* Copy/unpack pixel data to buffer */
386 dst = buffer;
387 for (d=0; d<depth; d++ ) {
388 for (i=0;i<height;i++) {
389 GLubyte *src = (GLubyte *) gl_pixel_addr_in_image( &ctx->Unpack,
390 pixels, width, height, srcFormat, srcType, d, i, 0 );
391 if (!src) {
392 free(buffer);
393 return NULL;
394 }
395 if (interleave) {
396 GLint j, k;
397 for (j=0;j<width;j++) {
398 for (k=0;k<components;k++) {
399 dst[k*width+j] = src[j*components+k];
400 }
401 }
402 }
403 else {
404 MEMCPY( dst, src, width_in_bytes );
405 }
406 dst += width_in_bytes;
407 }
408 }
409
410 if (ctx->Unpack.LsbFirst) {
411 gl_flip_bytes( buffer, height * width_in_bytes * depth );
412 }
413
414 image = (struct gl_image *) malloc( sizeof(struct gl_image) );
415 if (image) {
416 image->Width = width;
417 image->Height = height;
418 image->Depth = depth;
419 image->Components = components;
420 image->Format = srcFormat;
421 image->Type = GL_UNSIGNED_BYTE;
422 image->Interleaved = interleave;
423 image->Data = buffer;
424 }
425 else {
426 free( buffer );
427 return NULL;
428 }
429 return image;
430 }
431 else if (destType==GL_FLOAT) {
432 struct gl_image *image;
433 GLfloat *buffer, *dst;
434 GLint elems_per_row;
435 GLint i, j, d;
436 elems_per_row = width * components;
437 buffer = (GLfloat *) malloc( height * elems_per_row * sizeof(GLfloat) * depth);
438 if (!buffer) {
439 return NULL;
440 }
441
442 dst = buffer;
443 /** img_pixels= pixels;*/
444 for (d=0; d<depth; d++) {
445 for (i=0;i<height;i++) {
446 GLvoid *src = gl_pixel_addr_in_image( &ctx->Unpack, pixels,
447 width, height,
448 srcFormat, srcType,
449 d, i, 0 );
450 if (!src) {
451 free(buffer);
452 return NULL;
453 }
454
455 switch (srcType) {
456 case GL_UNSIGNED_BYTE:
457 for (j=0;j<elems_per_row;j++) {
458 *dst++ = (GLfloat) ((GLubyte*)src)[j];
459 }
460 break;
461 case GL_BYTE:
462 for (j=0;j<elems_per_row;j++) {
463 *dst++ = (GLfloat) ((GLbyte*)src)[j];
464 }
465 break;
466 case GL_UNSIGNED_SHORT:
467 if (ctx->Unpack.SwapBytes) {
468 for (j=0;j<elems_per_row;j++) {
469 GLushort value = ((GLushort*)src)[j];
470 value = ((value >> 8) & 0xff) | ((value&0xff) << 8);
471 *dst++ = (GLfloat) value;
472 }
473 }
474 else {
475 for (j=0;j<elems_per_row;j++) {
476 *dst++ = (GLfloat) ((GLushort*)src)[j];
477 }
478 }
479 break;
480 case GL_SHORT:
481 if (ctx->Unpack.SwapBytes) {
482 for (j=0;j<elems_per_row;j++) {
483 GLshort value = ((GLshort*)src)[j];
484 value = ((value >> 8) & 0xff) | ((value&0xff) << 8);
485 *dst++ = (GLfloat) value;
486 }
487 }
488 else {
489 for (j=0;j<elems_per_row;j++) {
490 *dst++ = (GLfloat) ((GLshort*)src)[j];
491 }
492 }
493 break;
494 case GL_UNSIGNED_INT:
495 if (ctx->Unpack.SwapBytes) {
496 GLuint value;
497 for (j=0;j<elems_per_row;j++) {
498 value = ((GLuint*)src)[j];
499 value = ((value & 0xff000000) >> 24)
500 | ((value & 0x00ff0000) >> 8)
501 | ((value & 0x0000ff00) << 8)
502 | ((value & 0x000000ff) << 24);
503 *dst++ = (GLfloat) value;
504 }
505 }
506 else {
507 for (j=0;j<elems_per_row;j++) {
508 *dst++ = (GLfloat) ((GLuint*)src)[j];
509 }
510 }
511 break;
512 case GL_INT:
513 if (ctx->Unpack.SwapBytes) {
514 GLint value;
515 for (j=0;j<elems_per_row;j++) {
516 value = ((GLint*)src)[j];
517 value = ((value & 0xff000000) >> 24)
518 | ((value & 0x00ff0000) >> 8)
519 | ((value & 0x0000ff00) << 8)
520 | ((value & 0x000000ff) << 24);
521 *dst++ = (GLfloat) value;
522 }
523 }
524 else {
525 for (j=0;j<elems_per_row;j++) {
526 *dst++ = (GLfloat) ((GLint*)src)[j];
527 }
528 }
529 break;
530 case GL_FLOAT:
531 if (ctx->Unpack.SwapBytes) {
532 GLint value;
533 for (j=0;j<elems_per_row;j++) {
534 value = ((GLuint*)src)[j];
535 value = ((value & 0xff000000) >> 24)
536 | ((value & 0x00ff0000) >> 8)
537 | ((value & 0x0000ff00) << 8)
538 | ((value & 0x000000ff) << 24);
539 *dst++ = *((GLfloat*) &value);
540 }
541 }
542 else {
543 MEMCPY( dst, src, elems_per_row*sizeof(GLfloat) );
544 dst += elems_per_row;
545 }
546 break;
547 default:
548 abort();
549 } /*switch*/
550 } /* for height */
551 } /*for*/
552
553 image = (struct gl_image *) malloc( sizeof(struct gl_image) );
554 if (image) {
555 image->Width = width;
556 image->Height = height;
557 image->Depth = depth;
558 image->Components = components;
559 image->Format = srcFormat;
560 image->Type = GL_FLOAT;
561 image->Interleaved = GL_FALSE;
562 image->Data = buffer;
563 }
564 else {
565 free( buffer );
566 return NULL;
567 }
568 return image;
569 }
570 else {
571 abort();
572 }
573 return NULL; /* never get here */
574 }
575
576
577
578 void gl_free_image( struct gl_image *image )
579 {
580 if (image->Data) {
581 free(image->Data);
582 }
583 free(image);
584 }