blob: 82b2966e61ac24e6922d54ba320b53c3cb38b637 [file] [log] [blame]
DRC0fc884a2011-05-24 09:13:17 +00001/*
2 * Copyright (C)2011 D. R. Commander. All Rights Reserved.
DRC2e7b76b2009-04-03 12:04:24 +00003 *
DRC0fc884a2011-05-24 09:13:17 +00004 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
DRC2e7b76b2009-04-03 12:04:24 +00006 *
DRC0fc884a2011-05-24 09:13:17 +00007 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of the libjpeg-turbo Project nor the names of its
13 * contributors may be used to endorse or promote products derived from this
14 * software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
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 */
DRC2e7b76b2009-04-03 12:04:24 +000028
DRC2e7b76b2009-04-03 12:04:24 +000029#include <stdio.h>
30#include <string.h>
DRC0fc884a2011-05-24 09:13:17 +000031#include <setjmp.h>
32#include <errno.h>
DRC296c71b2011-05-25 04:12:52 +000033#include "cdjpeg.h"
DRC0fc884a2011-05-24 09:13:17 +000034#include <jpeglib.h>
35#include <jpegint.h>
DRC43484642011-05-24 17:03:51 +000036#include "tjutil.h"
DRC0fc884a2011-05-24 09:13:17 +000037#include "bmp.h"
DRC2e7b76b2009-04-03 12:04:24 +000038
DRC2e7b76b2009-04-03 12:04:24 +000039
DRC0fc884a2011-05-24 09:13:17 +000040/* This duplicates the functionality of the VirtualGL bitmap library using
41 the components from cjpeg and djpeg */
42
43
44/* Error handling (based on example in example.c) */
45
46static char errStr[JMSG_LENGTH_MAX]="No error";
47
48struct my_error_mgr
DRC2e7b76b2009-04-03 12:04:24 +000049{
DRC0fc884a2011-05-24 09:13:17 +000050 struct jpeg_error_mgr pub;
51 jmp_buf setjmp_buffer;
52};
53typedef struct my_error_mgr *my_error_ptr;
DRC2e7b76b2009-04-03 12:04:24 +000054
DRC0fc884a2011-05-24 09:13:17 +000055static void my_error_exit(j_common_ptr cinfo)
DRC2e7b76b2009-04-03 12:04:24 +000056{
DRC0fc884a2011-05-24 09:13:17 +000057 my_error_ptr myerr=(my_error_ptr)cinfo->err;
58 (*cinfo->err->output_message)(cinfo);
59 longjmp(myerr->setjmp_buffer, 1);
DRC2e7b76b2009-04-03 12:04:24 +000060}
61
DRC0fc884a2011-05-24 09:13:17 +000062/* Based on output_message() in jerror.c */
63
64static void my_output_message(j_common_ptr cinfo)
DRC2e7b76b2009-04-03 12:04:24 +000065{
DRC0fc884a2011-05-24 09:13:17 +000066 (*cinfo->err->format_message)(cinfo, errStr);
67}
DRC2e7b76b2009-04-03 12:04:24 +000068
DRC0fc884a2011-05-24 09:13:17 +000069#define _throw(m) {snprintf(errStr, JMSG_LENGTH_MAX, "%s", m); \
70 retval=-1; goto bailout;}
71#define _throwunix(m) {snprintf(errStr, JMSG_LENGTH_MAX, "%s\n%s", m, \
72 strerror(errno)); retval=-1; goto bailout;}
DRC2e7b76b2009-04-03 12:04:24 +000073
DRC0fc884a2011-05-24 09:13:17 +000074
75static void pixelconvert(unsigned char *srcbuf, int srcpf, int srcbottomup,
76 unsigned char *dstbuf, int dstpf, int dstbottomup, int w, int h)
77{
78 unsigned char *srcptr=srcbuf, *srcptr2;
79 int srcps=tjPixelSize[srcpf];
80 int srcstride=srcbottomup? -w*srcps:w*srcps;
81 unsigned char *dstptr=dstbuf, *dstptr2;
82 int dstps=tjPixelSize[dstpf];
83 int dststride=dstbottomup? -w*dstps:w*dstps;
84 int row, col;
85
86 if(srcbottomup) srcptr=&srcbuf[w*srcps*(h-1)];
87 if(dstbottomup) dstptr=&dstbuf[w*dstps*(h-1)];
88 for(row=0; row<h; row++, srcptr+=srcstride, dstptr+=dststride)
DRC2e7b76b2009-04-03 12:04:24 +000089 {
DRC0fc884a2011-05-24 09:13:17 +000090 for(col=0, srcptr2=srcptr, dstptr2=dstptr; col<w; col++, srcptr2+=srcps,
91 dstptr2+=dstps)
DRC2e7b76b2009-04-03 12:04:24 +000092 {
DRC0fc884a2011-05-24 09:13:17 +000093 dstptr2[tjRedOffset[dstpf]]=srcptr2[tjRedOffset[srcpf]];
94 dstptr2[tjGreenOffset[dstpf]]=srcptr2[tjGreenOffset[srcpf]];
95 dstptr2[tjBlueOffset[dstpf]]=srcptr2[tjBlueOffset[srcpf]];
DRC2e7b76b2009-04-03 12:04:24 +000096 }
97 }
DRC2e7b76b2009-04-03 12:04:24 +000098}
99
100
101int loadbmp(char *filename, unsigned char **buf, int *w, int *h,
DRC0fc884a2011-05-24 09:13:17 +0000102 int dstpf, int bottomup)
DRC2e7b76b2009-04-03 12:04:24 +0000103{
DRC0fc884a2011-05-24 09:13:17 +0000104 int retval=0, dstps, srcpf, tempc;
105 struct jpeg_compress_struct cinfo;
106 struct my_error_mgr jerr;
107 cjpeg_source_ptr src;
108 FILE *file=NULL;
DRC2e7b76b2009-04-03 12:04:24 +0000109
DRC0fc884a2011-05-24 09:13:17 +0000110 if(!filename || !buf || !w || !h || dstpf<0 || dstpf>=TJ_NUMPF)
111 _throw("loadbmp(): Invalid argument");
DRC2e7b76b2009-04-03 12:04:24 +0000112
DRC0fc884a2011-05-24 09:13:17 +0000113 if((file=fopen(filename, "rb"))==NULL)
114 _throwunix("loadbmp(): Cannot open input file");
DRC2e7b76b2009-04-03 12:04:24 +0000115
DRC0fc884a2011-05-24 09:13:17 +0000116 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))
DRC2e7b76b2009-04-03 12:04:24 +0000121 {
DRC0fc884a2011-05-24 09:13:17 +0000122 /* If we get here, the JPEG code has signaled an error. */
123 retval=-1; goto bailout;
DRC2e7b76b2009-04-03 12:04:24 +0000124 }
125
DRC0fc884a2011-05-24 09:13:17 +0000126 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");
DRC2e7b76b2009-04-03 12:04:24 +0000130
DRC0fc884a2011-05-24 09:13:17 +0000131 if(tempc=='B')
DRC2e7b76b2009-04-03 12:04:24 +0000132 {
DRC0fc884a2011-05-24 09:13:17 +0000133 if((src=jinit_read_bmp(&cinfo))==NULL)
134 _throw("loadbmp(): Could not initialize bitmap loader");
DRC2e7b76b2009-04-03 12:04:24 +0000135 }
DRC0fc884a2011-05-24 09:13:17 +0000136 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");
DRC2e7b76b2009-04-03 12:04:24 +0000142
DRC0fc884a2011-05-24 09:13:17 +0000143 src->input_file=file;
144 (*src->start_input)(&cinfo, src);
145 (*cinfo.mem->realize_virt_arrays)((j_common_ptr)&cinfo);
DRC2e7b76b2009-04-03 12:04:24 +0000146
DRC0fc884a2011-05-24 09:13:17 +0000147 *w=cinfo.image_width; *h=cinfo.image_height;
DRC2e7b76b2009-04-03 12:04:24 +0000148
DRC0fc884a2011-05-24 09:13:17 +0000149 if(cinfo.input_components==1 && cinfo.in_color_space==JCS_RGB)
150 srcpf=TJPF_GRAY;
151 else srcpf=TJPF_RGB;
DRC2e7b76b2009-04-03 12:04:24 +0000152
DRC0fc884a2011-05-24 09:13:17 +0000153 dstps=tjPixelSize[dstpf];
154 if((*buf=(unsigned char *)malloc((*w)*(*h)*dstps))==NULL)
155 _throw("loadbmp(): Memory allocation failure");
DRC2e7b76b2009-04-03 12:04:24 +0000156
DRC0fc884a2011-05-24 09:13:17 +0000157 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++)
161 {
162 unsigned char *outbuf; int row;
163 row=cinfo.next_scanline+i;
164 if(bottomup) outbuf=&(*buf)[((*h)-row-1)*(*w)*dstps];
165 else outbuf=&(*buf)[row*(*w)*dstps];
166 pixelconvert(src->buffer[i], srcpf, 0, outbuf, dstpf, bottomup, *w,
167 nlines);
168 }
169 cinfo.next_scanline+=nlines;
170 }
171
172 (*src->finish_input)(&cinfo, src);
173
174 bailout:
175 jpeg_destroy_compress(&cinfo);
176 if(file) fclose(file);
177 if(retval<0 && buf && *buf) {free(*buf); *buf=NULL;}
178 return retval;
DRC2e7b76b2009-04-03 12:04:24 +0000179}
180
DRC2e7b76b2009-04-03 12:04:24 +0000181
DRC0fc884a2011-05-24 09:13:17 +0000182int savebmp(char *filename, unsigned char *buf, int w, int h, int srcpf,
183 int bottomup)
DRC2e7b76b2009-04-03 12:04:24 +0000184{
DRC0fc884a2011-05-24 09:13:17 +0000185 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;
DRC2e7b76b2009-04-03 12:04:24 +0000191
DRC0fc884a2011-05-24 09:13:17 +0000192 if(!filename || !buf || w<1 || h<1 || srcpf<0 || srcpf>=TJ_NUMPF)
193 _throw("savebmp(): Invalid argument");
DRC2e7b76b2009-04-03 12:04:24 +0000194
DRC0fc884a2011-05-24 09:13:17 +0000195 if((file=fopen(filename, "wb"))==NULL)
196 _throwunix("savebmp(): Cannot open output file");
DRC2e7b76b2009-04-03 12:04:24 +0000197
DRC0fc884a2011-05-24 09:13:17 +0000198 dinfo.err=jpeg_std_error(&jerr.pub);
199 jerr.pub.error_exit=my_error_exit;
200 jerr.pub.output_message=my_output_message;
DRC2e7b76b2009-04-03 12:04:24 +0000201
DRC0fc884a2011-05-24 09:13:17 +0000202 if(setjmp(jerr.setjmp_buffer))
DRC2e7b76b2009-04-03 12:04:24 +0000203 {
DRC0fc884a2011-05-24 09:13:17 +0000204 /* If we get here, the JPEG code has signaled an error. */
205 retval=-1; goto bailout;
DRC2e7b76b2009-04-03 12:04:24 +0000206 }
207
DRC0fc884a2011-05-24 09:13:17 +0000208 jpeg_create_decompress(&dinfo);
209 if(srcpf==TJPF_GRAY)
DRC2e7b76b2009-04-03 12:04:24 +0000210 {
DRC0fc884a2011-05-24 09:13:17 +0000211 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");
DRC2e7b76b2009-04-03 12:04:24 +0000233 }
234
DRC0fc884a2011-05-24 09:13:17 +0000235 dst->output_file=file;
236 (*dst->start_output)(&dinfo, dst);
237 (*dinfo.mem->realize_virt_arrays)((j_common_ptr)&dinfo);
DRC2e7b76b2009-04-03 12:04:24 +0000238
DRC0fc884a2011-05-24 09:13:17 +0000239 if(srcpf==TJPF_GRAY) dstpf=srcpf;
240 else dstpf=TJPF_RGB;
241 srcps=tjPixelSize[srcpf];
DRC2e7b76b2009-04-03 12:04:24 +0000242
DRC0fc884a2011-05-24 09:13:17 +0000243 while(dinfo.output_scanline<dinfo.output_height)
244 {
245 int i, nlines=dst->buffer_height;
246 for(i=0; i<nlines; i++)
247 {
248 unsigned char *inbuf; int row;
249 row=dinfo.output_scanline+i;
250 if(bottomup) inbuf=&buf[(h-row-1)*w*srcps];
251 else inbuf=&buf[row*w*srcps];
252 pixelconvert(inbuf, srcpf, bottomup, dst->buffer[i], dstpf, 0, w,
253 nlines);
254 }
255 (*dst->put_pixel_rows)(&dinfo, dst, nlines);
256 dinfo.output_scanline+=nlines;
257 }
DRC2e7b76b2009-04-03 12:04:24 +0000258
DRC0fc884a2011-05-24 09:13:17 +0000259 (*dst->finish_output)(&dinfo, dst);
DRC2e7b76b2009-04-03 12:04:24 +0000260
DRC0fc884a2011-05-24 09:13:17 +0000261 bailout:
262 jpeg_destroy_decompress(&dinfo);
263 if(file) fclose(file);
264 return retval;
DRC2e7b76b2009-04-03 12:04:24 +0000265}
266
267const char *bmpgeterr(void)
268{
DRC0fc884a2011-05-24 09:13:17 +0000269 return errStr;
DRC2e7b76b2009-04-03 12:04:24 +0000270}