bmp.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Copyright (C)2011, 2015 D. R. Commander. All Rights Reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  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. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <setjmp.h>
  31. #include <errno.h>
  32. #include "cdjpeg.h"
  33. #include <jpeglib.h>
  34. #include <jpegint.h>
  35. #include "tjutil.h"
  36. #include "bmp.h"
  37. /* This duplicates the functionality of the VirtualGL bitmap library using
  38. the components from cjpeg and djpeg */
  39. /* Error handling (based on example in example.c) */
  40. static char errStr[JMSG_LENGTH_MAX]="No error";
  41. struct my_error_mgr
  42. {
  43. struct jpeg_error_mgr pub;
  44. jmp_buf setjmp_buffer;
  45. };
  46. typedef struct my_error_mgr *my_error_ptr;
  47. static void my_error_exit(j_common_ptr cinfo)
  48. {
  49. my_error_ptr myerr=(my_error_ptr)cinfo->err;
  50. (*cinfo->err->output_message)(cinfo);
  51. longjmp(myerr->setjmp_buffer, 1);
  52. }
  53. /* Based on output_message() in jerror.c */
  54. static void my_output_message(j_common_ptr cinfo)
  55. {
  56. (*cinfo->err->format_message)(cinfo, errStr);
  57. }
  58. #define _throw(m) {snprintf(errStr, JMSG_LENGTH_MAX, "%s", m); \
  59. retval=-1; goto bailout;}
  60. #define _throwunix(m) {snprintf(errStr, JMSG_LENGTH_MAX, "%s\n%s", m, \
  61. strerror(errno)); retval=-1; goto bailout;}
  62. static void pixelconvert(unsigned char *srcbuf, int srcpf, int srcbottomup,
  63. unsigned char *dstbuf, int dstpf, int dstbottomup, int w, int h)
  64. {
  65. unsigned char *srcrowptr=srcbuf, *srccolptr;
  66. int srcps=tjPixelSize[srcpf];
  67. int srcstride=srcbottomup? -w*srcps:w*srcps;
  68. unsigned char *dstrowptr=dstbuf, *dstcolptr;
  69. int dstps=tjPixelSize[dstpf];
  70. int dststride=dstbottomup? -w*dstps:w*dstps;
  71. int row, col;
  72. if(srcbottomup) srcrowptr=&srcbuf[w*srcps*(h-1)];
  73. if(dstbottomup) dstrowptr=&dstbuf[w*dstps*(h-1)];
  74. /* NOTE: These quick & dirty CMYK<->RGB conversion routines are for testing
  75. purposes only. Properly converting between CMYK and RGB requires a color
  76. management system. */
  77. if(dstpf==TJPF_CMYK)
  78. {
  79. for(row=0; row<h; row++, srcrowptr+=srcstride, dstrowptr+=dststride)
  80. {
  81. for(col=0, srccolptr=srcrowptr, dstcolptr=dstrowptr;
  82. col<w; col++, srccolptr+=srcps)
  83. {
  84. double c=1.0-((double)(srccolptr[tjRedOffset[srcpf]])/255.);
  85. double m=1.0-((double)(srccolptr[tjGreenOffset[srcpf]])/255.);
  86. double y=1.0-((double)(srccolptr[tjBlueOffset[srcpf]])/255.);
  87. double k=min(min(c,m),min(y,1.0));
  88. if(k==1.0) c=m=y=0.0;
  89. else
  90. {
  91. c=(c-k)/(1.0-k);
  92. m=(m-k)/(1.0-k);
  93. y=(y-k)/(1.0-k);
  94. }
  95. if(c>1.0) c=1.0;
  96. if(c<0.) c=0.;
  97. if(m>1.0) m=1.0;
  98. if(m<0.) m=0.;
  99. if(y>1.0) y=1.0;
  100. if(y<0.) y=0.;
  101. if(k>1.0) k=1.0;
  102. if(k<0.) k=0.;
  103. *dstcolptr++=(unsigned char)(255.0-c*255.0+0.5);
  104. *dstcolptr++=(unsigned char)(255.0-m*255.0+0.5);
  105. *dstcolptr++=(unsigned char)(255.0-y*255.0+0.5);
  106. *dstcolptr++=(unsigned char)(255.0-k*255.0+0.5);
  107. }
  108. }
  109. }
  110. else if(srcpf==TJPF_CMYK)
  111. {
  112. for(row=0; row<h; row++, srcrowptr+=srcstride, dstrowptr+=dststride)
  113. {
  114. for(col=0, srccolptr=srcrowptr, dstcolptr=dstrowptr;
  115. col<w; col++, dstcolptr+=dstps)
  116. {
  117. double c=(double)(*srccolptr++);
  118. double m=(double)(*srccolptr++);
  119. double y=(double)(*srccolptr++);
  120. double k=(double)(*srccolptr++);
  121. double r=c*k/255.;
  122. double g=m*k/255.;
  123. double b=y*k/255.;
  124. if(r>255.0) r=255.0;
  125. if(r<0.) r=0.;
  126. if(g>255.0) g=255.0;
  127. if(g<0.) g=0.;
  128. if(b>255.0) b=255.0;
  129. if(b<0.) b=0.;
  130. dstcolptr[tjRedOffset[dstpf]]=(unsigned char)(r+0.5);
  131. dstcolptr[tjGreenOffset[dstpf]]=(unsigned char)(g+0.5);
  132. dstcolptr[tjBlueOffset[dstpf]]=(unsigned char)(b+0.5);
  133. }
  134. }
  135. }
  136. else
  137. {
  138. for(row=0; row<h; row++, srcrowptr+=srcstride, dstrowptr+=dststride)
  139. {
  140. for(col=0, srccolptr=srcrowptr, dstcolptr=dstrowptr;
  141. col<w; col++, srccolptr+=srcps, dstcolptr+=dstps)
  142. {
  143. dstcolptr[tjRedOffset[dstpf]]=srccolptr[tjRedOffset[srcpf]];
  144. dstcolptr[tjGreenOffset[dstpf]]=srccolptr[tjGreenOffset[srcpf]];
  145. dstcolptr[tjBlueOffset[dstpf]]=srccolptr[tjBlueOffset[srcpf]];
  146. }
  147. }
  148. }
  149. }
  150. int loadbmp(char *filename, unsigned char **buf, int *w, int *h,
  151. int dstpf, int bottomup)
  152. {
  153. int retval=0, dstps, srcpf, tempc;
  154. struct jpeg_compress_struct cinfo;
  155. struct my_error_mgr jerr;
  156. cjpeg_source_ptr src;
  157. FILE *file=NULL;
  158. memset(&cinfo, 0, sizeof(struct jpeg_compress_struct));
  159. if(!filename || !buf || !w || !h || dstpf<0 || dstpf>=TJ_NUMPF)
  160. _throw("loadbmp(): Invalid argument");
  161. if((file=fopen(filename, "rb"))==NULL)
  162. _throwunix("loadbmp(): Cannot open input file");
  163. cinfo.err=jpeg_std_error(&jerr.pub);
  164. jerr.pub.error_exit=my_error_exit;
  165. jerr.pub.output_message=my_output_message;
  166. if(setjmp(jerr.setjmp_buffer))
  167. {
  168. /* If we get here, the JPEG code has signaled an error. */
  169. retval=-1; goto bailout;
  170. }
  171. jpeg_create_compress(&cinfo);
  172. if((tempc=getc(file))<0 || ungetc(tempc, file)==EOF)
  173. _throwunix("loadbmp(): Could not read input file")
  174. else if(tempc==EOF) _throw("loadbmp(): Input file contains no data");
  175. if(tempc=='B')
  176. {
  177. if((src=jinit_read_bmp(&cinfo))==NULL)
  178. _throw("loadbmp(): Could not initialize bitmap loader");
  179. }
  180. else if(tempc=='P')
  181. {
  182. if((src=jinit_read_ppm(&cinfo))==NULL)
  183. _throw("loadbmp(): Could not initialize bitmap loader");
  184. }
  185. else _throw("loadbmp(): Unsupported file type");
  186. src->input_file=file;
  187. (*src->start_input)(&cinfo, src);
  188. (*cinfo.mem->realize_virt_arrays)((j_common_ptr)&cinfo);
  189. *w=cinfo.image_width; *h=cinfo.image_height;
  190. if(cinfo.input_components==1 && cinfo.in_color_space==JCS_RGB)
  191. srcpf=TJPF_GRAY;
  192. else srcpf=TJPF_RGB;
  193. dstps=tjPixelSize[dstpf];
  194. if((*buf=(unsigned char *)malloc((*w)*(*h)*dstps))==NULL)
  195. _throw("loadbmp(): Memory allocation failure");
  196. while(cinfo.next_scanline<cinfo.image_height)
  197. {
  198. int i, nlines=(*src->get_pixel_rows)(&cinfo, src);
  199. for(i=0; i<nlines; i++)
  200. {
  201. unsigned char *outbuf; int row;
  202. row=cinfo.next_scanline+i;
  203. if(bottomup) outbuf=&(*buf)[((*h)-row-1)*(*w)*dstps];
  204. else outbuf=&(*buf)[row*(*w)*dstps];
  205. pixelconvert(src->buffer[i], srcpf, 0, outbuf, dstpf, bottomup, *w,
  206. nlines);
  207. }
  208. cinfo.next_scanline+=nlines;
  209. }
  210. (*src->finish_input)(&cinfo, src);
  211. bailout:
  212. jpeg_destroy_compress(&cinfo);
  213. if(file) fclose(file);
  214. if(retval<0 && buf && *buf) {free(*buf); *buf=NULL;}
  215. return retval;
  216. }
  217. int savebmp(char *filename, unsigned char *buf, int w, int h, int srcpf,
  218. int bottomup)
  219. {
  220. int retval=0, srcps, dstpf;
  221. struct jpeg_decompress_struct dinfo;
  222. struct my_error_mgr jerr;
  223. djpeg_dest_ptr dst;
  224. FILE *file=NULL;
  225. char *ptr=NULL;
  226. memset(&dinfo, 0, sizeof(struct jpeg_decompress_struct));
  227. if(!filename || !buf || w<1 || h<1 || srcpf<0 || srcpf>=TJ_NUMPF)
  228. _throw("savebmp(): Invalid argument");
  229. if((file=fopen(filename, "wb"))==NULL)
  230. _throwunix("savebmp(): Cannot open output file");
  231. dinfo.err=jpeg_std_error(&jerr.pub);
  232. jerr.pub.error_exit=my_error_exit;
  233. jerr.pub.output_message=my_output_message;
  234. if(setjmp(jerr.setjmp_buffer))
  235. {
  236. /* If we get here, the JPEG code has signaled an error. */
  237. retval=-1; goto bailout;
  238. }
  239. jpeg_create_decompress(&dinfo);
  240. if(srcpf==TJPF_GRAY)
  241. {
  242. dinfo.out_color_components=dinfo.output_components=1;
  243. dinfo.out_color_space=JCS_GRAYSCALE;
  244. }
  245. else
  246. {
  247. dinfo.out_color_components=dinfo.output_components=3;
  248. dinfo.out_color_space=JCS_RGB;
  249. }
  250. dinfo.image_width=w; dinfo.image_height=h;
  251. dinfo.global_state=DSTATE_READY;
  252. dinfo.scale_num=dinfo.scale_denom=1;
  253. ptr=strrchr(filename, '.');
  254. if(ptr && !strcasecmp(ptr, ".bmp"))
  255. {
  256. if((dst=jinit_write_bmp(&dinfo, 0))==NULL)
  257. _throw("savebmp(): Could not initialize bitmap writer");
  258. }
  259. else
  260. {
  261. if((dst=jinit_write_ppm(&dinfo))==NULL)
  262. _throw("savebmp(): Could not initialize PPM writer");
  263. }
  264. dst->output_file=file;
  265. (*dst->start_output)(&dinfo, dst);
  266. (*dinfo.mem->realize_virt_arrays)((j_common_ptr)&dinfo);
  267. if(srcpf==TJPF_GRAY) dstpf=srcpf;
  268. else dstpf=TJPF_RGB;
  269. srcps=tjPixelSize[srcpf];
  270. while(dinfo.output_scanline<dinfo.output_height)
  271. {
  272. int i, nlines=dst->buffer_height;
  273. for(i=0; i<nlines; i++)
  274. {
  275. unsigned char *inbuf; int row;
  276. row=dinfo.output_scanline+i;
  277. if(bottomup) inbuf=&buf[(h-row-1)*w*srcps];
  278. else inbuf=&buf[row*w*srcps];
  279. pixelconvert(inbuf, srcpf, bottomup, dst->buffer[i], dstpf, 0, w,
  280. nlines);
  281. }
  282. (*dst->put_pixel_rows)(&dinfo, dst, nlines);
  283. dinfo.output_scanline+=nlines;
  284. }
  285. (*dst->finish_output)(&dinfo, dst);
  286. bailout:
  287. jpeg_destroy_decompress(&dinfo);
  288. if(file) fclose(file);
  289. return retval;
  290. }
  291. const char *bmpgeterr(void)
  292. {
  293. return errStr;
  294. }