bmp.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (C)2011 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 *srcptr=srcbuf, *srcptr2;
  66. int srcps=tjPixelSize[srcpf];
  67. int srcstride=srcbottomup? -w*srcps:w*srcps;
  68. unsigned char *dstptr=dstbuf, *dstptr2;
  69. int dstps=tjPixelSize[dstpf];
  70. int dststride=dstbottomup? -w*dstps:w*dstps;
  71. int row, col;
  72. if(srcbottomup) srcptr=&srcbuf[w*srcps*(h-1)];
  73. if(dstbottomup) dstptr=&dstbuf[w*dstps*(h-1)];
  74. for(row=0; row<h; row++, srcptr+=srcstride, dstptr+=dststride)
  75. {
  76. for(col=0, srcptr2=srcptr, dstptr2=dstptr; col<w; col++, srcptr2+=srcps,
  77. dstptr2+=dstps)
  78. {
  79. dstptr2[tjRedOffset[dstpf]]=srcptr2[tjRedOffset[srcpf]];
  80. dstptr2[tjGreenOffset[dstpf]]=srcptr2[tjGreenOffset[srcpf]];
  81. dstptr2[tjBlueOffset[dstpf]]=srcptr2[tjBlueOffset[srcpf]];
  82. }
  83. }
  84. }
  85. int loadbmp(char *filename, unsigned char **buf, int *w, int *h,
  86. int dstpf, int bottomup)
  87. {
  88. int retval=0, dstps, srcpf, tempc;
  89. struct jpeg_compress_struct cinfo;
  90. struct my_error_mgr jerr;
  91. cjpeg_source_ptr src;
  92. FILE *file=NULL;
  93. memset(&cinfo, 0, sizeof(struct jpeg_compress_struct));
  94. if(!filename || !buf || !w || !h || dstpf<0 || dstpf>=TJ_NUMPF)
  95. _throw("loadbmp(): Invalid argument");
  96. if((file=fopen(filename, "rb"))==NULL)
  97. _throwunix("loadbmp(): Cannot open input file");
  98. cinfo.err=jpeg_std_error(&jerr.pub);
  99. jerr.pub.error_exit=my_error_exit;
  100. jerr.pub.output_message=my_output_message;
  101. if(setjmp(jerr.setjmp_buffer))
  102. {
  103. /* If we get here, the JPEG code has signaled an error. */
  104. retval=-1; goto bailout;
  105. }
  106. jpeg_create_compress(&cinfo);
  107. if((tempc=getc(file))<0 || ungetc(tempc, file)==EOF)
  108. _throwunix("loadbmp(): Could not read input file")
  109. else if(tempc==EOF) _throw("loadbmp(): Input file contains no data");
  110. if(tempc=='B')
  111. {
  112. if((src=jinit_read_bmp(&cinfo))==NULL)
  113. _throw("loadbmp(): Could not initialize bitmap loader");
  114. }
  115. else if(tempc=='P')
  116. {
  117. if((src=jinit_read_ppm(&cinfo))==NULL)
  118. _throw("loadbmp(): Could not initialize bitmap loader");
  119. }
  120. else _throw("loadbmp(): Unsupported file type");
  121. src->input_file=file;
  122. (*src->start_input)(&cinfo, src);
  123. (*cinfo.mem->realize_virt_arrays)((j_common_ptr)&cinfo);
  124. *w=cinfo.image_width; *h=cinfo.image_height;
  125. if(cinfo.input_components==1 && cinfo.in_color_space==JCS_RGB)
  126. srcpf=TJPF_GRAY;
  127. else srcpf=TJPF_RGB;
  128. dstps=tjPixelSize[dstpf];
  129. if((*buf=(unsigned char *)malloc((*w)*(*h)*dstps))==NULL)
  130. _throw("loadbmp(): Memory allocation failure");
  131. while(cinfo.next_scanline<cinfo.image_height)
  132. {
  133. int i, nlines=(*src->get_pixel_rows)(&cinfo, src);
  134. for(i=0; i<nlines; i++)
  135. {
  136. unsigned char *outbuf; int row;
  137. row=cinfo.next_scanline+i;
  138. if(bottomup) outbuf=&(*buf)[((*h)-row-1)*(*w)*dstps];
  139. else outbuf=&(*buf)[row*(*w)*dstps];
  140. pixelconvert(src->buffer[i], srcpf, 0, outbuf, dstpf, bottomup, *w,
  141. nlines);
  142. }
  143. cinfo.next_scanline+=nlines;
  144. }
  145. (*src->finish_input)(&cinfo, src);
  146. bailout:
  147. jpeg_destroy_compress(&cinfo);
  148. if(file) fclose(file);
  149. if(retval<0 && buf && *buf) {free(*buf); *buf=NULL;}
  150. return retval;
  151. }
  152. int savebmp(char *filename, unsigned char *buf, int w, int h, int srcpf,
  153. int bottomup)
  154. {
  155. int retval=0, srcps, dstpf;
  156. struct jpeg_decompress_struct dinfo;
  157. struct my_error_mgr jerr;
  158. djpeg_dest_ptr dst;
  159. FILE *file=NULL;
  160. char *ptr=NULL;
  161. memset(&dinfo, 0, sizeof(struct jpeg_decompress_struct));
  162. if(!filename || !buf || w<1 || h<1 || srcpf<0 || srcpf>=TJ_NUMPF)
  163. _throw("savebmp(): Invalid argument");
  164. if((file=fopen(filename, "wb"))==NULL)
  165. _throwunix("savebmp(): Cannot open output file");
  166. dinfo.err=jpeg_std_error(&jerr.pub);
  167. jerr.pub.error_exit=my_error_exit;
  168. jerr.pub.output_message=my_output_message;
  169. if(setjmp(jerr.setjmp_buffer))
  170. {
  171. /* If we get here, the JPEG code has signaled an error. */
  172. retval=-1; goto bailout;
  173. }
  174. jpeg_create_decompress(&dinfo);
  175. if(srcpf==TJPF_GRAY)
  176. {
  177. dinfo.out_color_components=dinfo.output_components=1;
  178. dinfo.out_color_space=JCS_GRAYSCALE;
  179. }
  180. else
  181. {
  182. dinfo.out_color_components=dinfo.output_components=3;
  183. dinfo.out_color_space=JCS_RGB;
  184. }
  185. dinfo.image_width=w; dinfo.image_height=h;
  186. dinfo.global_state=DSTATE_READY;
  187. dinfo.scale_num=dinfo.scale_denom=1;
  188. ptr=strrchr(filename, '.');
  189. if(ptr && !strcasecmp(ptr, ".bmp"))
  190. {
  191. if((dst=jinit_write_bmp(&dinfo, 0))==NULL)
  192. _throw("savebmp(): Could not initialize bitmap writer");
  193. }
  194. else
  195. {
  196. if((dst=jinit_write_ppm(&dinfo))==NULL)
  197. _throw("savebmp(): Could not initialize PPM writer");
  198. }
  199. dst->output_file=file;
  200. (*dst->start_output)(&dinfo, dst);
  201. (*dinfo.mem->realize_virt_arrays)((j_common_ptr)&dinfo);
  202. if(srcpf==TJPF_GRAY) dstpf=srcpf;
  203. else dstpf=TJPF_RGB;
  204. srcps=tjPixelSize[srcpf];
  205. while(dinfo.output_scanline<dinfo.output_height)
  206. {
  207. int i, nlines=dst->buffer_height;
  208. for(i=0; i<nlines; i++)
  209. {
  210. unsigned char *inbuf; int row;
  211. row=dinfo.output_scanline+i;
  212. if(bottomup) inbuf=&buf[(h-row-1)*w*srcps];
  213. else inbuf=&buf[row*w*srcps];
  214. pixelconvert(inbuf, srcpf, bottomup, dst->buffer[i], dstpf, 0, w,
  215. nlines);
  216. }
  217. (*dst->put_pixel_rows)(&dinfo, dst, nlines);
  218. dinfo.output_scanline+=nlines;
  219. }
  220. (*dst->finish_output)(&dinfo, dst);
  221. bailout:
  222. jpeg_destroy_decompress(&dinfo);
  223. if(file) fclose(file);
  224. return retval;
  225. }
  226. const char *bmpgeterr(void)
  227. {
  228. return errStr;
  229. }