4 * Gestion des images sous OpenGL
9 * Ce package a ete teste sur SGI, OSF, SUN, HP et WNT.
12 * Historique des modifications
13 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14 * 22-05-97: PCT ; creation
15 * 10-07-98: FGU ; Ajout : ReadScaledImage
16 * Mise a jour des dimensions de l image
17 * 02.15.100 JR : Implicit convertion
19 /*----------------------------------------------------------------------*/
21 /*----------------------------------------------------------------------*/
30 #include <OpenGl_ImageBox.hxx>
32 /*----------------------------------------------------------------------*/
37 typedef struct _ImageRec {
38 unsigned short imagic;
41 unsigned short xsize, ysize, zsize;
42 unsigned int min, max;
43 unsigned int wasteBytes;
45 unsigned long colorMap;
47 unsigned char *tmp, *tmpR, *tmpG, *tmpB;
49 unsigned int *rowStart;
53 /*----------------------------------------------------------------------*/
59 ConvertShort(unsigned short *array, long length) {
63 ptr = (unsigned char *)array;
67 *array++ = (unsigned short )((b1 << 8) | (b2));
70 /*----------------------------------------------------------------------*/
73 ConvertLong(unsigned *array, long length) {
74 unsigned long b1, b2, b3, b4;
77 ptr = (unsigned char *)array;
83 *array++ = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4);
86 /*----------------------------------------------------------------------*/
88 static ImageRec *ImageOpen(char *fileName)
98 endianTest.testWord = 1;
99 if (endianTest.testByte[0] == 1) {
105 image = (ImageRec *)malloc(sizeof(ImageRec));
107 fprintf(stderr, "Out of memory!\n");
110 if ((image->file = fopen(fileName, "rb")) == NULL) {
115 fread(image, 1, 12, image->file);
118 ConvertShort(&image->imagic, 6);
121 image->tmp = (unsigned char *)malloc(image->xsize*256);
122 image->tmpR = (unsigned char *)malloc(image->xsize*256);
123 image->tmpG = (unsigned char *)malloc(image->xsize*256);
124 image->tmpB = (unsigned char *)malloc(image->xsize*256);
125 if (image->tmp == NULL || image->tmpR == NULL || image->tmpG == NULL ||
126 image->tmpB == NULL) {
127 fprintf(stderr, "Out of memory!\n");
131 if ((image->type & 0xFF00) == 0x0100) {
132 x = image->ysize * image->zsize * sizeof(unsigned);
133 image->rowStart = (unsigned *)malloc(x);
134 image->rowSize = (int *)malloc(x);
135 if (image->rowStart == NULL || image->rowSize == NULL) {
136 fprintf(stderr, "Out of memory!\n");
139 image->rleEnd = 512 + (2 * x);
140 fseek(image->file, 512, SEEK_SET);
141 fread(image->rowStart, 1, x, image->file);
142 fread(image->rowSize, 1, x, image->file);
144 ConvertLong(image->rowStart, x/sizeof(unsigned));
145 ConvertLong((unsigned *)image->rowSize, x/sizeof(int));
150 /*----------------------------------------------------------------------*/
153 ImageClose(ImageRec *image) {
161 /*----------------------------------------------------------------------*/
164 ImageGetRow(ImageRec *image, unsigned char *buf, int y, int z) {
165 unsigned char *iPtr, *oPtr, pixel;
168 if ((image->type & 0xFF00) == 0x0100) {
169 fseek(image->file, image->rowStart[y+z*image->ysize], SEEK_SET);
170 fread(image->tmp, 1, (unsigned int)image->rowSize[y+z*image->ysize],
177 count = (int)(pixel & 0x7F);
193 fseek(image->file, 512+(y*image->xsize)+(z*image->xsize*image->ysize),
195 fread(buf, 1, image->xsize, image->file);
199 /*----------------------------------------------------------------------*/
201 * Fonctions publiques
204 /*----------------------------------------------------------------------*/
205 void ReadScaledImage(char *file, int xsize, int ysize, char *buf, unsigned short *zsize)
207 ImageRec *image = ImageOpen(file);
208 unsigned char *rbuf, *gbuf=NULL, *bbuf=NULL;
209 int row, rrow, i, ri;
212 *zsize = image->zsize;
214 /* Allocation memoire */
215 rbuf=(unsigned char *)malloc(image->xsize * sizeof(unsigned char));
217 if (image->zsize > 2) {
218 gbuf = (unsigned char *) malloc (image->xsize * sizeof(unsigned char));
219 bbuf = (unsigned char *) malloc (image->xsize * sizeof(unsigned char));
222 /* Lecture image rang apres rang */
223 for (row = 0; row < ysize; row++) {
225 rrow = (row*image->ysize)/ysize;
228 ImageGetRow(image, rbuf, rrow, 0);
229 ImageGetRow(image, gbuf, rrow, 1);
230 ImageGetRow(image, bbuf, rrow, 2);
233 ImageGetRow(image, rbuf, rrow, 0);
235 /* stockage au format RGB */
236 for (i=0; i < xsize; i++) {
237 ri = (i*image->xsize)/xsize;
251 /* delete image buffers */
261 /*----------------------------------------------------------------------*/
262 void ReadSizeImage(char *file, int *xsize, int *ysize)
265 ImageRec *image = ImageOpen(file);
267 /* Affectation taille */
268 *xsize = image->xsize;
269 *ysize = image->ysize;
272 /*----------------------------------------------------------------------*/
273 void bwtorgba(unsigned char *b,unsigned char *l,int n) {
283 /*----------------------------------------------------------------------*/
285 void rgbtorgba(unsigned char *r,unsigned char *g,unsigned char *b,unsigned char *l,int n) {
291 l += 4; r++; g++; b++;
295 /*----------------------------------------------------------------------*/
297 void rgbatorgba(unsigned char *r,unsigned char *g,unsigned char *b,unsigned char *a,unsigned char *l,int n) {
303 l += 4; r++; g++; b++; a++;
307 /*----------------------------------------------------------------------*/
310 read_texture(char *name, int *width, int *height, int *components) {
311 unsigned *base, *lptr;
312 unsigned char *rbuf, *gbuf, *bbuf, *abuf;
316 image = ImageOpen(name);
320 (*width)=image->xsize;
321 (*height)=image->ysize;
322 (*components)=image->zsize;
323 base = (unsigned *)malloc(image->xsize*image->ysize*sizeof(unsigned));
324 rbuf = (unsigned char *)malloc(image->xsize*sizeof(unsigned char));
325 gbuf = (unsigned char *)malloc(image->xsize*sizeof(unsigned char));
326 bbuf = (unsigned char *)malloc(image->xsize*sizeof(unsigned char));
327 abuf = (unsigned char *)malloc(image->xsize*sizeof(unsigned char));
328 if(!base || !rbuf || !gbuf || !bbuf)
331 for(y=0; y<image->ysize; y++) {
332 if(image->zsize>=4) {
333 ImageGetRow(image,rbuf,y,0);
334 ImageGetRow(image,gbuf,y,1);
335 ImageGetRow(image,bbuf,y,2);
336 ImageGetRow(image,abuf,y,3);
337 rgbatorgba(rbuf,gbuf,bbuf,abuf,(unsigned char *)lptr,image->xsize);
338 lptr += image->xsize;
339 } else if(image->zsize==3) {
340 ImageGetRow(image,rbuf,y,0);
341 ImageGetRow(image,gbuf,y,1);
342 ImageGetRow(image,bbuf,y,2);
343 rgbtorgba(rbuf,gbuf,bbuf,(unsigned char *)lptr,image->xsize);
344 lptr += image->xsize;
346 ImageGetRow(image,rbuf,y,0);
347 bwtorgba(rbuf,(unsigned char *)lptr,image->xsize);
348 lptr += image->xsize;
357 return (unsigned *) base;
360 /*----------------------------------------------------------------------*/