blob: 3ccc8772bb352c9e0a761c26effe010bc86ef54e [file] [log] [blame]
DRC2e7b76b2009-04-03 12:04:24 +00001/* Copyright (C)2004 Landmark Graphics Corporation
2 * Copyright (C)2005 Sun Microsystems, Inc.
3 *
4 * This library is free software and may be redistributed and/or modified under
5 * the terms of the wxWindows Library License, Version 3.1 or (at your option)
6 * any later version. The full license is in the LICENSE.txt file included
7 * with this distribution.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * wxWindows Library License for more details.
13*/
14
15#include <fcntl.h>
16#include <sys/types.h>
17#include <sys/stat.h>
18#include <errno.h>
19#include <stdlib.h>
20#include <stdio.h>
21#include <string.h>
22#ifdef _WIN32
23 #include <io.h>
24#else
25 #include <unistd.h>
26#endif
27#include "./rrutil.h"
28#include "./bmp.h"
29
30#ifndef BI_BITFIELDS
31#define BI_BITFIELDS 3L
32#endif
33#ifndef BI_RGB
34#define BI_RGB 0L
35#endif
36
37#define BMPHDRSIZE 54
38typedef struct _bmphdr
39{
40 unsigned short bfType;
41 unsigned int bfSize;
42 unsigned short bfReserved1, bfReserved2;
43 unsigned int bfOffBits;
44
45 unsigned int biSize;
46 int biWidth, biHeight;
47 unsigned short biPlanes, biBitCount;
48 unsigned int biCompression, biSizeImage;
49 int biXPelsPerMeter, biYPelsPerMeter;
50 unsigned int biClrUsed, biClrImportant;
51} bmphdr;
52
53static const char *__bmperr="No error";
54
55static const int ps[BMPPIXELFORMATS]={3, 4, 3, 4, 4, 4};
56static const int roffset[BMPPIXELFORMATS]={0, 0, 2, 2, 3, 1};
57static const int goffset[BMPPIXELFORMATS]={1, 1, 1, 1, 2, 2};
58static const int boffset[BMPPIXELFORMATS]={2, 2, 0, 0, 1, 3};
59
60#define _throw(m) {__bmperr=m; retcode=-1; goto finally;}
61#define _unix(f) {if((f)==-1) _throw(strerror(errno));}
62#define _catch(f) {if((f)==-1) {retcode=-1; goto finally;}}
63
64#define readme(fd, addr, size) \
65 if((bytesread=read(fd, addr, (size)))==-1) _throw(strerror(errno)); \
66 if(bytesread!=(size)) _throw("Read error");
67
68void pixelconvert(unsigned char *srcbuf, enum BMPPIXELFORMAT srcformat,
69 int srcpitch, unsigned char *dstbuf, enum BMPPIXELFORMAT dstformat, int dstpitch,
70 int w, int h, int flip)
71{
72 unsigned char *srcptr, *srcptr0, *dstptr, *dstptr0;
73 int i, j;
74
75 srcptr=flip? &srcbuf[srcpitch*(h-1)]:srcbuf;
76 for(j=0, dstptr=dstbuf; j<h; j++,
77 srcptr+=flip? -srcpitch:srcpitch, dstptr+=dstpitch)
78 {
79 for(i=0, srcptr0=srcptr, dstptr0=dstptr; i<w; i++,
80 srcptr0+=ps[srcformat], dstptr0+=ps[dstformat])
81 {
82 dstptr0[roffset[dstformat]]=srcptr0[roffset[srcformat]];
83 dstptr0[goffset[dstformat]]=srcptr0[goffset[srcformat]];
84 dstptr0[boffset[dstformat]]=srcptr0[boffset[srcformat]];
85 }
86 }
87}
88
89int loadppm(int *fd, unsigned char **buf, int *w, int *h,
90 enum BMPPIXELFORMAT f, int align, int dstbottomup, int ascii)
91{
92 FILE *fs=NULL; int retcode=0, scalefactor, dstpitch;
93 unsigned char *tempbuf=NULL; char temps[255], temps2[255];
94 int numread=0, totalread=0, pixel[3], i, j;
95
96 if((fs=fdopen(*fd, "r"))==NULL) _throw(strerror(errno));
97
98 do
99 {
100 if(!fgets(temps, 255, fs)) _throw("Read error");
101 if(strlen(temps)==0 || temps[0]=='\n') continue;
102 if(sscanf(temps, "%s", temps2)==1 && temps2[1]=='#') continue;
103 switch(totalread)
104 {
105 case 0:
106 if((numread=sscanf(temps, "%d %d %d", w, h, &scalefactor))==EOF)
107 _throw("Read error");
108 break;
109 case 1:
110 if((numread=sscanf(temps, "%d %d", h, &scalefactor))==EOF)
111 _throw("Read error");
112 break;
113 case 2:
114 if((numread=sscanf(temps, "%d", &scalefactor))==EOF)
115 _throw("Read error");
116 break;
117 }
118 totalread+=numread;
119 } while(totalread<3);
120 if((*w)<1 || (*h)<1 || scalefactor<1) _throw("Corrupt PPM header");
121
122 dstpitch=(((*w)*ps[f])+(align-1))&(~(align-1));
123 if((*buf=(unsigned char *)malloc(dstpitch*(*h)))==NULL)
124 _throw("Memory allocation error");
125 if(ascii)
126 {
127 for(j=0; j<*h; j++)
128 {
129 for(i=0; i<*w; i++)
130 {
131 if(fscanf(fs, "%d%d%d", &pixel[0], &pixel[1], &pixel[2])!=3)
132 _throw("Read error");
133 (*buf)[j*dstpitch+i*ps[f]+roffset[f]]=(unsigned char)(pixel[0]*255/scalefactor);
134 (*buf)[j*dstpitch+i*ps[f]+goffset[f]]=(unsigned char)(pixel[1]*255/scalefactor);
135 (*buf)[j*dstpitch+i*ps[f]+boffset[f]]=(unsigned char)(pixel[2]*255/scalefactor);
136 }
137 }
138 }
139 else
140 {
141 if(scalefactor!=255)
142 _throw("Binary PPMs must have 8-bit components");
143 if((tempbuf=(unsigned char *)malloc((*w)*(*h)*3))==NULL)
144 _throw("Memory allocation error");
145 if(fread(tempbuf, (*w)*(*h)*3, 1, fs)!=1) _throw("Read error");
146 pixelconvert(tempbuf, BMP_RGB, (*w)*3, *buf, f, dstpitch, *w, *h, dstbottomup);
147 }
148
149 finally:
150 if(fs) {fclose(fs); *fd=-1;}
151 if(tempbuf) free(tempbuf);
152 return retcode;
153}
154
155
156int loadbmp(char *filename, unsigned char **buf, int *w, int *h,
157 enum BMPPIXELFORMAT f, int align, int dstbottomup)
158{
159 int fd=-1, bytesread, srcpitch, srcbottomup=1, srcps, dstpitch,
160 retcode=0;
161 unsigned char *tempbuf=NULL;
162 bmphdr bh; int flags=O_RDONLY;
163
164 dstbottomup=dstbottomup? 1:0;
165 #ifdef _WIN32
166 flags|=O_BINARY;
167 #endif
168 if(!filename || !buf || !w || !h || f<0 || f>BMPPIXELFORMATS-1 || align<1)
169 _throw("invalid argument to loadbmp()");
170 if((align&(align-1))!=0)
171 _throw("Alignment must be a power of 2");
172 _unix(fd=open(filename, flags));
173
174 readme(fd, &bh.bfType, sizeof(unsigned short));
175 if(!littleendian()) bh.bfType=byteswap16(bh.bfType);
176
177 if(bh.bfType==0x3650)
178 {
179 _catch(loadppm(&fd, buf, w, h, f, align, dstbottomup, 0));
180 goto finally;
181 }
182 if(bh.bfType==0x3350)
183 {
184 _catch(loadppm(&fd, buf, w, h, f, align, dstbottomup, 1));
185 goto finally;
186 }
187
188 readme(fd, &bh.bfSize, sizeof(unsigned int));
189 readme(fd, &bh.bfReserved1, sizeof(unsigned short));
190 readme(fd, &bh.bfReserved2, sizeof(unsigned short));
191 readme(fd, &bh.bfOffBits, sizeof(unsigned int));
192 readme(fd, &bh.biSize, sizeof(unsigned int));
193 readme(fd, &bh.biWidth, sizeof(int));
194 readme(fd, &bh.biHeight, sizeof(int));
195 readme(fd, &bh.biPlanes, sizeof(unsigned short));
196 readme(fd, &bh.biBitCount, sizeof(unsigned short));
197 readme(fd, &bh.biCompression, sizeof(unsigned int));
198 readme(fd, &bh.biSizeImage, sizeof(unsigned int));
199 readme(fd, &bh.biXPelsPerMeter, sizeof(int));
200 readme(fd, &bh.biYPelsPerMeter, sizeof(int));
201 readme(fd, &bh.biClrUsed, sizeof(unsigned int));
202 readme(fd, &bh.biClrImportant, sizeof(unsigned int));
203
204 if(!littleendian())
205 {
206 bh.bfSize=byteswap(bh.bfSize);
207 bh.bfOffBits=byteswap(bh.bfOffBits);
208 bh.biSize=byteswap(bh.biSize);
209 bh.biWidth=byteswap(bh.biWidth);
210 bh.biHeight=byteswap(bh.biHeight);
211 bh.biPlanes=byteswap16(bh.biPlanes);
212 bh.biBitCount=byteswap16(bh.biBitCount);
213 bh.biCompression=byteswap(bh.biCompression);
214 bh.biSizeImage=byteswap(bh.biSizeImage);
215 bh.biXPelsPerMeter=byteswap(bh.biXPelsPerMeter);
216 bh.biYPelsPerMeter=byteswap(bh.biYPelsPerMeter);
217 bh.biClrUsed=byteswap(bh.biClrUsed);
218 bh.biClrImportant=byteswap(bh.biClrImportant);
219 }
220
221 if(bh.bfType!=0x4d42 || bh.bfOffBits<BMPHDRSIZE
222 || bh.biWidth<1 || bh.biHeight==0)
223 _throw("Corrupt bitmap header");
224 if((bh.biBitCount!=24 && bh.biBitCount!=32) || bh.biCompression!=BI_RGB)
225 _throw("Only uncompessed RGB bitmaps are supported");
226
227 *w=bh.biWidth; *h=bh.biHeight; srcps=bh.biBitCount/8;
228 if(*h<0) {*h=-(*h); srcbottomup=0;}
229 srcpitch=(((*w)*srcps)+3)&(~3);
230 dstpitch=(((*w)*ps[f])+(align-1))&(~(align-1));
231
232 if(srcpitch*(*h)+bh.bfOffBits!=bh.bfSize) _throw("Corrupt bitmap header");
233 if((tempbuf=(unsigned char *)malloc(srcpitch*(*h)))==NULL
234 || (*buf=(unsigned char *)malloc(dstpitch*(*h)))==NULL)
235 _throw("Memory allocation error");
236 if(lseek(fd, (long)bh.bfOffBits, SEEK_SET)!=(long)bh.bfOffBits)
237 _throw(strerror(errno));
238 _unix(bytesread=read(fd, tempbuf, srcpitch*(*h)));
239 if(bytesread!=srcpitch*(*h)) _throw("Read error");
240
241 pixelconvert(tempbuf, BMP_BGR, srcpitch, *buf, f, dstpitch, *w, *h,
242 srcbottomup!=dstbottomup);
243
244 finally:
245 if(tempbuf) free(tempbuf);
246 if(fd!=-1) close(fd);
247 return retcode;
248}
249
250#define writeme(fd, addr, size) \
251 if((byteswritten=write(fd, addr, (size)))==-1) _throw(strerror(errno)); \
252 if(byteswritten!=(size)) _throw("Write error");
253
254int saveppm(char *filename, unsigned char *buf, int w, int h,
255 enum BMPPIXELFORMAT f, int srcpitch, int srcbottomup)
256{
257 FILE *fs=NULL; int retcode=0;
258 unsigned char *tempbuf=NULL;
259
260 if((fs=fopen(filename, "wb"))==NULL) _throw(strerror(errno));
261 if(fprintf(fs, "P6\n")<1) _throw("Write error");
262 if(fprintf(fs, "%d %d\n", w, h)<1) _throw("Write error");
263 if(fprintf(fs, "255\n")<1) _throw("Write error");
264
265 if((tempbuf=(unsigned char *)malloc(w*h*3))==NULL)
266 _throw("Memory allocation error");
267
268 pixelconvert(buf, f, srcpitch, tempbuf, BMP_RGB, w*3, w, h,
269 srcbottomup);
270
271 if((fwrite(tempbuf, w*h*3, 1, fs))!=1) _throw("Write error");
272
273 finally:
274 if(tempbuf) free(tempbuf);
275 if(fs) fclose(fs);
276 return retcode;
277}
278
279int savebmp(char *filename, unsigned char *buf, int w, int h,
280 enum BMPPIXELFORMAT f, int srcpitch, int srcbottomup)
281{
282 int fd=-1, byteswritten, dstpitch, retcode=0;
283 int flags=O_RDWR|O_CREAT|O_TRUNC;
284 unsigned char *tempbuf=NULL; char *temp;
285 bmphdr bh; int mode;
286
287 #ifdef _WIN32
288 flags|=O_BINARY; mode=_S_IREAD|_S_IWRITE;
289 #else
290 mode=S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
291 #endif
292 if(!filename || !buf || w<1 || h<1 || f<0 || f>BMPPIXELFORMATS-1 || srcpitch<0)
293 _throw("bad argument to savebmp()");
294
295 if(srcpitch==0) srcpitch=w*ps[f];
296
297 if((temp=strrchr(filename, '.'))!=NULL)
298 {
299 if(!stricmp(temp, ".ppm"))
300 return saveppm(filename, buf, w, h, f, srcpitch, srcbottomup);
301 }
302
303 _unix(fd=open(filename, flags, mode));
304 dstpitch=((w*3)+3)&(~3);
305
306 bh.bfType=0x4d42;
307 bh.bfSize=BMPHDRSIZE+dstpitch*h;
308 bh.bfReserved1=0; bh.bfReserved2=0;
309 bh.bfOffBits=BMPHDRSIZE;
310 bh.biSize=40;
311 bh.biWidth=w; bh.biHeight=h;
312 bh.biPlanes=0; bh.biBitCount=24;
313 bh.biCompression=BI_RGB; bh.biSizeImage=0;
314 bh.biXPelsPerMeter=0; bh.biYPelsPerMeter=0;
315 bh.biClrUsed=0; bh.biClrImportant=0;
316
317 if(!littleendian())
318 {
319 bh.bfType=byteswap16(bh.bfType);
320 bh.bfSize=byteswap(bh.bfSize);
321 bh.bfOffBits=byteswap(bh.bfOffBits);
322 bh.biSize=byteswap(bh.biSize);
323 bh.biWidth=byteswap(bh.biWidth);
324 bh.biHeight=byteswap(bh.biHeight);
325 bh.biPlanes=byteswap16(bh.biPlanes);
326 bh.biBitCount=byteswap16(bh.biBitCount);
327 bh.biCompression=byteswap(bh.biCompression);
328 bh.biSizeImage=byteswap(bh.biSizeImage);
329 bh.biXPelsPerMeter=byteswap(bh.biXPelsPerMeter);
330 bh.biYPelsPerMeter=byteswap(bh.biYPelsPerMeter);
331 bh.biClrUsed=byteswap(bh.biClrUsed);
332 bh.biClrImportant=byteswap(bh.biClrImportant);
333 }
334
335 writeme(fd, &bh.bfType, sizeof(unsigned short));
336 writeme(fd, &bh.bfSize, sizeof(unsigned int));
337 writeme(fd, &bh.bfReserved1, sizeof(unsigned short));
338 writeme(fd, &bh.bfReserved2, sizeof(unsigned short));
339 writeme(fd, &bh.bfOffBits, sizeof(unsigned int));
340 writeme(fd, &bh.biSize, sizeof(unsigned int));
341 writeme(fd, &bh.biWidth, sizeof(int));
342 writeme(fd, &bh.biHeight, sizeof(int));
343 writeme(fd, &bh.biPlanes, sizeof(unsigned short));
344 writeme(fd, &bh.biBitCount, sizeof(unsigned short));
345 writeme(fd, &bh.biCompression, sizeof(unsigned int));
346 writeme(fd, &bh.biSizeImage, sizeof(unsigned int));
347 writeme(fd, &bh.biXPelsPerMeter, sizeof(int));
348 writeme(fd, &bh.biYPelsPerMeter, sizeof(int));
349 writeme(fd, &bh.biClrUsed, sizeof(unsigned int));
350 writeme(fd, &bh.biClrImportant, sizeof(unsigned int));
351
352 if((tempbuf=(unsigned char *)malloc(dstpitch*h))==NULL)
353 _throw("Memory allocation error");
354
355 pixelconvert(buf, f, srcpitch, tempbuf, BMP_BGR, dstpitch, w, h,
356 !srcbottomup);
357
358 if((byteswritten=write(fd, tempbuf, dstpitch*h))!=dstpitch*h)
359 _throw(strerror(errno));
360
361 finally:
362 if(tempbuf) free(tempbuf);
363 if(fd!=-1) close(fd);
364 return retcode;
365}
366
367const char *bmpgeterr(void)
368{
369 return __bmperr;
370}