DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C)2011 D. R. Commander. All Rights Reserved. |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 3 | * |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 6 | * |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 7 | * - 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 | */ |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 28 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 29 | #include <stdio.h> |
| 30 | #include <string.h> |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 31 | #include <setjmp.h> |
| 32 | #include <errno.h> |
DRC | 296c71b | 2011-05-25 04:12:52 +0000 | [diff] [blame] | 33 | #include "cdjpeg.h" |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 34 | #include <jpeglib.h> |
| 35 | #include <jpegint.h> |
DRC | 4348464 | 2011-05-24 17:03:51 +0000 | [diff] [blame] | 36 | #include "tjutil.h" |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 37 | #include "bmp.h" |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 38 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 39 | |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 40 | /* 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 | |
| 46 | static char errStr[JMSG_LENGTH_MAX]="No error"; |
| 47 | |
| 48 | struct my_error_mgr |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 49 | { |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 50 | struct jpeg_error_mgr pub; |
| 51 | jmp_buf setjmp_buffer; |
| 52 | }; |
| 53 | typedef struct my_error_mgr *my_error_ptr; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 54 | |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 55 | static void my_error_exit(j_common_ptr cinfo) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 56 | { |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 57 | my_error_ptr myerr=(my_error_ptr)cinfo->err; |
| 58 | (*cinfo->err->output_message)(cinfo); |
| 59 | longjmp(myerr->setjmp_buffer, 1); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 60 | } |
| 61 | |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 62 | /* Based on output_message() in jerror.c */ |
| 63 | |
| 64 | static void my_output_message(j_common_ptr cinfo) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 65 | { |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 66 | (*cinfo->err->format_message)(cinfo, errStr); |
| 67 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 68 | |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 69 | #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;} |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 73 | |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 74 | |
| 75 | static 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) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 89 | { |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 90 | for(col=0, srcptr2=srcptr, dstptr2=dstptr; col<w; col++, srcptr2+=srcps, |
| 91 | dstptr2+=dstps) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 92 | { |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 93 | dstptr2[tjRedOffset[dstpf]]=srcptr2[tjRedOffset[srcpf]]; |
| 94 | dstptr2[tjGreenOffset[dstpf]]=srcptr2[tjGreenOffset[srcpf]]; |
| 95 | dstptr2[tjBlueOffset[dstpf]]=srcptr2[tjBlueOffset[srcpf]]; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | |
| 101 | int loadbmp(char *filename, unsigned char **buf, int *w, int *h, |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 102 | int dstpf, int bottomup) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 103 | { |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 104 | 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; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 109 | |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 110 | if(!filename || !buf || !w || !h || dstpf<0 || dstpf>=TJ_NUMPF) |
| 111 | _throw("loadbmp(): Invalid argument"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 112 | |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 113 | if((file=fopen(filename, "rb"))==NULL) |
| 114 | _throwunix("loadbmp(): Cannot open input file"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 115 | |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 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)) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 121 | { |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 122 | /* If we get here, the JPEG code has signaled an error. */ |
| 123 | retval=-1; goto bailout; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 124 | } |
| 125 | |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 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"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 130 | |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 131 | if(tempc=='B') |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 132 | { |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 133 | if((src=jinit_read_bmp(&cinfo))==NULL) |
| 134 | _throw("loadbmp(): Could not initialize bitmap loader"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 135 | } |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 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"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 142 | |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 143 | src->input_file=file; |
| 144 | (*src->start_input)(&cinfo, src); |
| 145 | (*cinfo.mem->realize_virt_arrays)((j_common_ptr)&cinfo); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 146 | |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 147 | *w=cinfo.image_width; *h=cinfo.image_height; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 148 | |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 149 | if(cinfo.input_components==1 && cinfo.in_color_space==JCS_RGB) |
| 150 | srcpf=TJPF_GRAY; |
| 151 | else srcpf=TJPF_RGB; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 152 | |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 153 | dstps=tjPixelSize[dstpf]; |
| 154 | if((*buf=(unsigned char *)malloc((*w)*(*h)*dstps))==NULL) |
| 155 | _throw("loadbmp(): Memory allocation failure"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 156 | |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 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++) |
| 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; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 179 | } |
| 180 | |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 181 | |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 182 | int savebmp(char *filename, unsigned char *buf, int w, int h, int srcpf, |
| 183 | int bottomup) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 184 | { |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 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; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 191 | |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 192 | if(!filename || !buf || w<1 || h<1 || srcpf<0 || srcpf>=TJ_NUMPF) |
| 193 | _throw("savebmp(): Invalid argument"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 194 | |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 195 | if((file=fopen(filename, "wb"))==NULL) |
| 196 | _throwunix("savebmp(): Cannot open output file"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 197 | |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 198 | dinfo.err=jpeg_std_error(&jerr.pub); |
| 199 | jerr.pub.error_exit=my_error_exit; |
| 200 | jerr.pub.output_message=my_output_message; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 201 | |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 202 | if(setjmp(jerr.setjmp_buffer)) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 203 | { |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 204 | /* If we get here, the JPEG code has signaled an error. */ |
| 205 | retval=-1; goto bailout; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 206 | } |
| 207 | |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 208 | jpeg_create_decompress(&dinfo); |
| 209 | if(srcpf==TJPF_GRAY) |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 210 | { |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 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"); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 233 | } |
| 234 | |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 235 | dst->output_file=file; |
| 236 | (*dst->start_output)(&dinfo, dst); |
| 237 | (*dinfo.mem->realize_virt_arrays)((j_common_ptr)&dinfo); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 238 | |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 239 | if(srcpf==TJPF_GRAY) dstpf=srcpf; |
| 240 | else dstpf=TJPF_RGB; |
| 241 | srcps=tjPixelSize[srcpf]; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 242 | |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 243 | 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 | } |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 258 | |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 259 | (*dst->finish_output)(&dinfo, dst); |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 260 | |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 261 | bailout: |
| 262 | jpeg_destroy_decompress(&dinfo); |
| 263 | if(file) fclose(file); |
| 264 | return retval; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | const char *bmpgeterr(void) |
| 268 | { |
DRC | 0fc884a | 2011-05-24 09:13:17 +0000 | [diff] [blame] | 269 | return errStr; |
DRC | 2e7b76b | 2009-04-03 12:04:24 +0000 | [diff] [blame] | 270 | } |