Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Side by Side Diff: third_party/libjpeg_turbo/bmp.c

Issue 7554002: Updates libjpeg-turbo to 1.1.90 (r677) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/deps/
Patch Set: '' Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/libjpeg_turbo/bmp.h ('k') | third_party/libjpeg_turbo/cderror.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* Copyright (C)2004 Landmark Graphics Corporation 1 /*
2 * Copyright (C)2005 Sun Microsystems, Inc. 2 * Copyright (C)2011 D. R. Commander. All Rights Reserved.
3 * 3 *
4 * This library is free software and may be redistributed and/or modified under 4 * Redistribution and use in source and binary forms, with or without
5 * the terms of the wxWindows Library License, Version 3.1 or (at your option) 5 * modification, are permitted provided that the following conditions are met:
6 * any later version. The full license is in the LICENSE.txt file included
7 * with this distribution.
8 * 6 *
9 * This library is distributed in the hope that it will be useful, 7 * - Redistributions of source code must retain the above copyright notice,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 8 * this list of conditions and the following disclaimer.
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 9 * - Redistributions in binary form must reproduce the above copyright notice,
12 * wxWindows Library License for more details. 10 * this list of conditions and the following disclaimer in the documentation
13 */ 11 * and/or other materials provided with the distribution.
14 12 * - Neither the name of the libjpeg-turbo Project nor the names of its
15 #include <fcntl.h> 13 * contributors may be used to endorse or promote products derived from this
16 #include <sys/types.h> 14 * software without specific prior written permission.
17 #include <sys/stat.h> 15 *
18 #include <errno.h> 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
19 #include <stdlib.h> 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
20 #include <stdio.h> 29 #include <stdio.h>
21 #include <string.h> 30 #include <string.h>
22 #ifdef _WIN32 31 #include <setjmp.h>
23 #include <io.h> 32 #include <errno.h>
24 #else 33 #include "cdjpeg.h"
25 #include <unistd.h> 34 #include <jpeglib.h>
26 #endif 35 #include <jpegint.h>
27 #include "./rrutil.h" 36 #include "tjutil.h"
28 #include "./bmp.h" 37 #include "bmp.h"
29 38
30 #ifndef BI_BITFIELDS 39
31 #define BI_BITFIELDS 3L 40 /* This duplicates the functionality of the VirtualGL bitmap library using
32 #endif 41 the components from cjpeg and djpeg */
33 #ifndef BI_RGB 42
34 #define BI_RGB 0L 43
35 #endif 44 /* Error handling (based on example in example.c) */
36 45
37 #define BMPHDRSIZE 54 46 static char errStr[JMSG_LENGTH_MAX]="No error";
38 typedef struct _bmphdr 47
39 { 48 struct my_error_mgr
40 » unsigned short bfType; 49 {
41 » unsigned int bfSize; 50 » struct jpeg_error_mgr pub;
42 » unsigned short bfReserved1, bfReserved2; 51 » jmp_buf setjmp_buffer;
43 » unsigned int bfOffBits; 52 };
44 53 typedef struct my_error_mgr *my_error_ptr;
45 » unsigned int biSize; 54
46 » int biWidth, biHeight; 55 static void my_error_exit(j_common_ptr cinfo)
47 » unsigned short biPlanes, biBitCount; 56 {
48 » unsigned int biCompression, biSizeImage; 57 » my_error_ptr myerr=(my_error_ptr)cinfo->err;
49 » int biXPelsPerMeter, biYPelsPerMeter; 58 » (*cinfo->err->output_message)(cinfo);
50 » unsigned int biClrUsed, biClrImportant; 59 » longjmp(myerr->setjmp_buffer, 1);
51 } bmphdr; 60 }
52 61
53 static const char *__bmperr="No error"; 62 /* Based on output_message() in jerror.c */
54 63
55 static const int ps[BMPPIXELFORMATS]={3, 4, 3, 4, 4, 4}; 64 static void my_output_message(j_common_ptr cinfo)
56 static const int roffset[BMPPIXELFORMATS]={0, 0, 2, 2, 3, 1}; 65 {
57 static const int goffset[BMPPIXELFORMATS]={1, 1, 1, 1, 2, 2}; 66 » (*cinfo->err->format_message)(cinfo, errStr);
58 static const int boffset[BMPPIXELFORMATS]={2, 2, 0, 0, 1, 3}; 67 }
59 68
60 #define _throw(m) {__bmperr=m; retcode=-1; goto finally;} 69 #define _throw(m) {snprintf(errStr, JMSG_LENGTH_MAX, "%s", m); \
61 #define _unix(f) {if((f)==-1) _throw(strerror(errno));} 70 » retval=-1; goto bailout;}
62 #define _catch(f) {if((f)==-1) {retcode=-1; goto finally;}} 71 #define _throwunix(m) {snprintf(errStr, JMSG_LENGTH_MAX, "%s\n%s", m, \
63 72 » strerror(errno)); retval=-1; goto bailout;}
64 #define readme(fd, addr, size) \ 73
65 » if((bytesread=read(fd, addr, (size)))==-1) _throw(strerror(errno)); \ 74
66 » if(bytesread!=(size)) _throw("Read error"); 75 static void pixelconvert(unsigned char *srcbuf, int srcpf, int srcbottomup,
67 76 » unsigned char *dstbuf, int dstpf, int dstbottomup, int w, int h)
68 void pixelconvert(unsigned char *srcbuf, enum BMPPIXELFORMAT srcformat, 77 {
69 » int srcpitch, unsigned char *dstbuf, enum BMPPIXELFORMAT dstformat, int dstpitch, 78 » unsigned char *srcptr=srcbuf, *srcptr2;
70 » int w, int h, int flip) 79 » int srcps=tjPixelSize[srcpf];
71 { 80 » int srcstride=srcbottomup? -w*srcps:w*srcps;
72 » unsigned char *srcptr, *srcptr0, *dstptr, *dstptr0; 81 » unsigned char *dstptr=dstbuf, *dstptr2;
73 » int i, j; 82 » int dstps=tjPixelSize[dstpf];
74 83 » int dststride=dstbottomup? -w*dstps:w*dstps;
75 » srcptr=flip? &srcbuf[srcpitch*(h-1)]:srcbuf; 84 » int row, col;
76 » for(j=0, dstptr=dstbuf; j<h; j++, 85
77 » » srcptr+=flip? -srcpitch:srcpitch, dstptr+=dstpitch) 86 » if(srcbottomup) srcptr=&srcbuf[w*srcps*(h-1)];
78 » { 87 » if(dstbottomup) dstptr=&dstbuf[w*dstps*(h-1)];
79 » » for(i=0, srcptr0=srcptr, dstptr0=dstptr; i<w; i++, 88 » for(row=0; row<h; row++, srcptr+=srcstride, dstptr+=dststride)
80 » » » srcptr0+=ps[srcformat], dstptr0+=ps[dstformat]) 89 » {
90 » » for(col=0, srcptr2=srcptr, dstptr2=dstptr; col<w; col++, srcptr2 +=srcps,
91 » » » dstptr2+=dstps)
81 { 92 {
82 » » » dstptr0[roffset[dstformat]]=srcptr0[roffset[srcformat]]; 93 » » » dstptr2[tjRedOffset[dstpf]]=srcptr2[tjRedOffset[srcpf]];
83 » » » dstptr0[goffset[dstformat]]=srcptr0[goffset[srcformat]]; 94 » » » dstptr2[tjGreenOffset[dstpf]]=srcptr2[tjGreenOffset[srcp f]];
84 » » » dstptr0[boffset[dstformat]]=srcptr0[boffset[srcformat]]; 95 » » » dstptr2[tjBlueOffset[dstpf]]=srcptr2[tjBlueOffset[srcpf] ];
85 } 96 }
86 } 97 }
87 } 98 }
88 99
89 int loadppm(int *fd, unsigned char **buf, int *w, int *h, 100
90 » enum BMPPIXELFORMAT f, int align, int dstbottomup, int ascii) 101 int loadbmp(char *filename, unsigned char **buf, int *w, int *h,
91 { 102 » int dstpf, int bottomup)
92 » FILE *fs=NULL; int retcode=0, scalefactor, dstpitch; 103 {
93 » unsigned char *tempbuf=NULL; char temps[255], temps2[255]; 104 » int retval=0, dstps, srcpf, tempc;
94 » int numread=0, totalread=0, pixel[3], i, j; 105 » struct jpeg_compress_struct cinfo;
95 106 » struct my_error_mgr jerr;
96 » if((fs=fdopen(*fd, "r"))==NULL) _throw(strerror(errno)); 107 » cjpeg_source_ptr src;
97 108 » FILE *file=NULL;
98 » do 109
99 » { 110 » if(!filename || !buf || !w || !h || dstpf<0 || dstpf>=TJ_NUMPF)
100 » » if(!fgets(temps, 255, fs)) _throw("Read error"); 111 » » _throw("loadbmp(): Invalid argument");
101 » » if(strlen(temps)==0 || temps[0]=='\n') continue; 112
102 » » if(sscanf(temps, "%s", temps2)==1 && temps2[1]=='#') continue; 113 » if((file=fopen(filename, "rb"))==NULL)
103 » » switch(totalread) 114 » » _throwunix("loadbmp(): Cannot open input file");
115
116 » cinfo.err=jpeg_std_error(&jerr.pub);
117 » jerr.pub.error_exit=my_error_exit;
118 » jerr.pub.output_message=my_output_message;
119
120 » if(setjmp(jerr.setjmp_buffer))
121 » {
122 » » /* If we get here, the JPEG code has signaled an error. */
123 » » retval=-1; goto bailout;
124 » }
125
126 » jpeg_create_compress(&cinfo);
127 » if((tempc=getc(file))<0 || ungetc(tempc, file)==EOF)
128 » » _throwunix("loadbmp(): Could not read input file")
129 » else if(tempc==EOF) _throw("loadbmp(): Input file contains no data");
130
131 » if(tempc=='B')
132 » {
133 » » if((src=jinit_read_bmp(&cinfo))==NULL)
134 » » » _throw("loadbmp(): Could not initialize bitmap loader");
135 » }
136 » else if(tempc=='P')
137 » {
138 » » if((src=jinit_read_ppm(&cinfo))==NULL)
139 » » » _throw("loadbmp(): Could not initialize bitmap loader");
140 » }
141 » else _throw("loadbmp(): Unsupported file type");
142
143 » src->input_file=file;
144 » (*src->start_input)(&cinfo, src);
145 » (*cinfo.mem->realize_virt_arrays)((j_common_ptr)&cinfo);
146
147 » *w=cinfo.image_width; *h=cinfo.image_height;
148
149 » if(cinfo.input_components==1 && cinfo.in_color_space==JCS_RGB)
150 » » srcpf=TJPF_GRAY;
151 » else srcpf=TJPF_RGB;
152
153 » dstps=tjPixelSize[dstpf];
154 » if((*buf=(unsigned char *)malloc((*w)*(*h)*dstps))==NULL)
155 » » _throw("loadbmp(): Memory allocation failure");
156
157 » while(cinfo.next_scanline<cinfo.image_height)
158 » {
159 » » int i, nlines=(*src->get_pixel_rows)(&cinfo, src);
160 » » for(i=0; i<nlines; i++)
104 { 161 {
105 » » » case 0: 162 » » » unsigned char *outbuf; int row;
106 » » » » if((numread=sscanf(temps, "%d %d %d", w, h, &sca lefactor))==EOF) 163 » » » row=cinfo.next_scanline+i;
107 » » » » » _throw("Read error"); 164 » » » if(bottomup) outbuf=&(*buf)[((*h)-row-1)*(*w)*dstps];
108 » » » » break; 165 » » » else outbuf=&(*buf)[row*(*w)*dstps];
109 » » » case 1: 166 » » » pixelconvert(src->buffer[i], srcpf, 0, outbuf, dstpf, bo ttomup, *w,
110 » » » » if((numread=sscanf(temps, "%d %d", h, &scalefact or))==EOF) 167 » » » » nlines);
111 » » » » » _throw("Read error");
112 » » » » break;
113 » » » case 2:
114 » » » » if((numread=sscanf(temps, "%d", &scalefactor))== EOF)
115 » » » » » _throw("Read error");
116 » » » » break;
117 } 168 }
118 » » totalread+=numread; 169 » » cinfo.next_scanline+=nlines;
119 » } while(totalread<3); 170 }
120 » if((*w)<1 || (*h)<1 || scalefactor<1) _throw("Corrupt PPM header"); 171
121 172 » (*src->finish_input)(&cinfo, src);
122 » dstpitch=(((*w)*ps[f])+(align-1))&(~(align-1)); 173
123 » if((*buf=(unsigned char *)malloc(dstpitch*(*h)))==NULL) 174 » bailout:
124 » » _throw("Memory allocation error"); 175 » jpeg_destroy_compress(&cinfo);
125 » if(ascii) 176 » if(file) fclose(file);
126 » { 177 » if(retval<0 && buf && *buf) {free(*buf); *buf=NULL;}
127 » » for(j=0; j<*h; j++) 178 » return retval;
179 }
180
181
182 int savebmp(char *filename, unsigned char *buf, int w, int h, int srcpf,
183 » int bottomup)
184 {
185 » int retval=0, srcps, dstpf;
186 » struct jpeg_decompress_struct dinfo;
187 » struct my_error_mgr jerr;
188 » djpeg_dest_ptr dst;
189 » FILE *file=NULL;
190 » char *ptr=NULL;
191
192 » if(!filename || !buf || w<1 || h<1 || srcpf<0 || srcpf>=TJ_NUMPF)
193 » » _throw("savebmp(): Invalid argument");
194
195 » if((file=fopen(filename, "wb"))==NULL)
196 » » _throwunix("savebmp(): Cannot open output file");
197
198 » dinfo.err=jpeg_std_error(&jerr.pub);
199 » jerr.pub.error_exit=my_error_exit;
200 » jerr.pub.output_message=my_output_message;
201
202 » if(setjmp(jerr.setjmp_buffer))
203 » {
204 » » /* If we get here, the JPEG code has signaled an error. */
205 » » retval=-1; goto bailout;
206 » }
207
208 » jpeg_create_decompress(&dinfo);
209 » if(srcpf==TJPF_GRAY)
210 » {
211 » » dinfo.out_color_components=dinfo.output_components=1;
212 » » dinfo.out_color_space=JCS_GRAYSCALE;
213 » }
214 » else
215 » {
216 » » dinfo.out_color_components=dinfo.output_components=3;
217 » » dinfo.out_color_space=JCS_RGB;
218 » }
219 » dinfo.image_width=w; dinfo.image_height=h;
220 » dinfo.global_state=DSTATE_READY;
221 » dinfo.scale_num=dinfo.scale_denom=1;
222
223 » ptr=strrchr(filename, '.');
224 » if(ptr && !strcasecmp(ptr, ".bmp"))
225 » {
226 » » if((dst=jinit_write_bmp(&dinfo, 0))==NULL)
227 » » » _throw("savebmp(): Could not initialize bitmap writer");
228 » }
229 » else
230 » {
231 » » if((dst=jinit_write_ppm(&dinfo))==NULL)
232 » » » _throw("savebmp(): Could not initialize PPM writer");
233 » }
234
235 dst->output_file=file;
236 » (*dst->start_output)(&dinfo, dst);
237 » (*dinfo.mem->realize_virt_arrays)((j_common_ptr)&dinfo);
238
239 » if(srcpf==TJPF_GRAY) dstpf=srcpf;
240 » else dstpf=TJPF_RGB;
241 » srcps=tjPixelSize[srcpf];
242
243 » while(dinfo.output_scanline<dinfo.output_height)
244 » {
245 » » int i, nlines=dst->buffer_height;
246 » » for(i=0; i<nlines; i++)
128 { 247 {
129 » » » for(i=0; i<*w; i++) 248 » » » unsigned char *inbuf; int row;
130 » » » { 249 » » » row=dinfo.output_scanline+i;
131 » » » » if(fscanf(fs, "%d%d%d", &pixel[0], &pixel[1], &p ixel[2])!=3) 250 » » » if(bottomup) inbuf=&buf[(h-row-1)*w*srcps];
132 » » » » » _throw("Read error"); 251 » » » else inbuf=&buf[row*w*srcps];
133 » » » » (*buf)[j*dstpitch+i*ps[f]+roffset[f]]=(unsigned char)(pixel[0]*255/scalefactor); 252 » » » pixelconvert(inbuf, srcpf, bottomup, dst->buffer[i], dst pf, 0, w,
134 » » » » (*buf)[j*dstpitch+i*ps[f]+goffset[f]]=(unsigned char)(pixel[1]*255/scalefactor); 253 » » » » nlines);
135 » » » » (*buf)[j*dstpitch+i*ps[f]+boffset[f]]=(unsigned char)(pixel[2]*255/scalefactor);
136 » » » }
137 } 254 }
138 » } 255 » » (*dst->put_pixel_rows)(&dinfo, dst, nlines);
139 » else 256 » » dinfo.output_scanline+=nlines;
140 » { 257 }
141 » » if(scalefactor!=255) 258
142 » » » _throw("Binary PPMs must have 8-bit components"); 259 » (*dst->finish_output)(&dinfo, dst);
143 » » if((tempbuf=(unsigned char *)malloc((*w)*(*h)*3))==NULL) 260
144 » » » _throw("Memory allocation error"); 261 » bailout:
145 » » if(fread(tempbuf, (*w)*(*h)*3, 1, fs)!=1) _throw("Read error"); 262 » jpeg_destroy_decompress(&dinfo);
146 » » pixelconvert(tempbuf, BMP_RGB, (*w)*3, *buf, f, dstpitch, *w, *h , dstbottomup); 263 » if(file) fclose(file);
147 » } 264 » return retval;
148
149 » finally:
150 » if(fs) {fclose(fs); *fd=-1;}
151 » if(tempbuf) free(tempbuf);
152 » return retcode;
153 }
154
155
156 int 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
254 int 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
279 int 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 || srcp itch<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, srcbott omup);
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 } 265 }
366 266
367 const char *bmpgeterr(void) 267 const char *bmpgeterr(void)
368 { 268 {
369 » return __bmperr; 269 » return errStr;
370 } 270 }
OLDNEW
« no previous file with comments | « third_party/libjpeg_turbo/bmp.h ('k') | third_party/libjpeg_turbo/cderror.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698