wrppm.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * wrppm.c
  3. *
  4. * This file was part of the Independent JPEG Group's software:
  5. * Copyright (C) 1991-1996, Thomas G. Lane.
  6. * Modified 2009 by Guido Vollbeding.
  7. * It was modified by The libjpeg-turbo Project to include only code and
  8. * information relevant to libjpeg-turbo.
  9. * For conditions of distribution and use, see the accompanying README.ijg
  10. * file.
  11. *
  12. * This file contains routines to write output images in PPM/PGM format.
  13. * The extended 2-byte-per-sample raw PPM/PGM formats are supported.
  14. * The PBMPLUS library is NOT required to compile this software
  15. * (but it is highly useful as a set of PPM image manipulation programs).
  16. *
  17. * These routines may need modification for non-Unix environments or
  18. * specialized applications. As they stand, they assume output to
  19. * an ordinary stdio stream.
  20. */
  21. #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
  22. #include "wrppm.h"
  23. #ifdef PPM_SUPPORTED
  24. /*
  25. * For 12-bit JPEG data, we either downscale the values to 8 bits
  26. * (to write standard byte-per-sample PPM/PGM files), or output
  27. * nonstandard word-per-sample PPM/PGM files. Downscaling is done
  28. * if PPM_NORAWWORD is defined (this can be done in the Makefile
  29. * or in jconfig.h).
  30. * (When the core library supports data precision reduction, a cleaner
  31. * implementation will be to ask for that instead.)
  32. */
  33. #if BITS_IN_JSAMPLE == 8
  34. #define PUTPPMSAMPLE(ptr,v) *ptr++ = (char) (v)
  35. #define BYTESPERSAMPLE 1
  36. #define PPM_MAXVAL 255
  37. #else
  38. #ifdef PPM_NORAWWORD
  39. #define PUTPPMSAMPLE(ptr,v) *ptr++ = (char) ((v) >> (BITS_IN_JSAMPLE-8))
  40. #define BYTESPERSAMPLE 1
  41. #define PPM_MAXVAL 255
  42. #else
  43. /* The word-per-sample format always puts the MSB first. */
  44. #define PUTPPMSAMPLE(ptr,v) \
  45. { register int val_ = v; \
  46. *ptr++ = (char) ((val_ >> 8) & 0xFF); \
  47. *ptr++ = (char) (val_ & 0xFF); \
  48. }
  49. #define BYTESPERSAMPLE 2
  50. #define PPM_MAXVAL ((1<<BITS_IN_JSAMPLE)-1)
  51. #endif
  52. #endif
  53. /*
  54. * When JSAMPLE is the same size as char, we can just fwrite() the
  55. * decompressed data to the PPM or PGM file.
  56. */
  57. /*
  58. * Write some pixel data.
  59. * In this module rows_supplied will always be 1.
  60. *
  61. * put_pixel_rows handles the "normal" 8-bit case where the decompressor
  62. * output buffer is physically the same as the fwrite buffer.
  63. */
  64. METHODDEF(void)
  65. put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
  66. JDIMENSION rows_supplied)
  67. {
  68. ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
  69. (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
  70. }
  71. /*
  72. * This code is used when we have to copy the data and apply a pixel
  73. * format translation. Typically this only happens in 12-bit mode.
  74. */
  75. METHODDEF(void)
  76. copy_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
  77. JDIMENSION rows_supplied)
  78. {
  79. ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
  80. register char *bufferptr;
  81. register JSAMPROW ptr;
  82. register JDIMENSION col;
  83. ptr = dest->pub.buffer[0];
  84. bufferptr = dest->iobuffer;
  85. for (col = dest->samples_per_row; col > 0; col--) {
  86. PUTPPMSAMPLE(bufferptr, GETJSAMPLE(*ptr++));
  87. }
  88. (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
  89. }
  90. /*
  91. * Write some pixel data when color quantization is in effect.
  92. * We have to demap the color index values to straight data.
  93. */
  94. METHODDEF(void)
  95. put_demapped_rgb (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
  96. JDIMENSION rows_supplied)
  97. {
  98. ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
  99. register char *bufferptr;
  100. register int pixval;
  101. register JSAMPROW ptr;
  102. register JSAMPROW color_map0 = cinfo->colormap[0];
  103. register JSAMPROW color_map1 = cinfo->colormap[1];
  104. register JSAMPROW color_map2 = cinfo->colormap[2];
  105. register JDIMENSION col;
  106. ptr = dest->pub.buffer[0];
  107. bufferptr = dest->iobuffer;
  108. for (col = cinfo->output_width; col > 0; col--) {
  109. pixval = GETJSAMPLE(*ptr++);
  110. PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map0[pixval]));
  111. PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map1[pixval]));
  112. PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map2[pixval]));
  113. }
  114. (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
  115. }
  116. METHODDEF(void)
  117. put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
  118. JDIMENSION rows_supplied)
  119. {
  120. ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
  121. register char *bufferptr;
  122. register JSAMPROW ptr;
  123. register JSAMPROW color_map = cinfo->colormap[0];
  124. register JDIMENSION col;
  125. ptr = dest->pub.buffer[0];
  126. bufferptr = dest->iobuffer;
  127. for (col = cinfo->output_width; col > 0; col--) {
  128. PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map[GETJSAMPLE(*ptr++)]));
  129. }
  130. (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
  131. }
  132. /*
  133. * Startup: write the file header.
  134. */
  135. METHODDEF(void)
  136. start_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
  137. {
  138. ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
  139. /* Emit file header */
  140. switch (cinfo->out_color_space) {
  141. case JCS_GRAYSCALE:
  142. /* emit header for raw PGM format */
  143. fprintf(dest->pub.output_file, "P5\n%ld %ld\n%d\n",
  144. (long) cinfo->output_width, (long) cinfo->output_height,
  145. PPM_MAXVAL);
  146. break;
  147. case JCS_RGB:
  148. /* emit header for raw PPM format */
  149. fprintf(dest->pub.output_file, "P6\n%ld %ld\n%d\n",
  150. (long) cinfo->output_width, (long) cinfo->output_height,
  151. PPM_MAXVAL);
  152. break;
  153. default:
  154. ERREXIT(cinfo, JERR_PPM_COLORSPACE);
  155. }
  156. }
  157. /*
  158. * Finish up at the end of the file.
  159. */
  160. METHODDEF(void)
  161. finish_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
  162. {
  163. /* Make sure we wrote the output file OK */
  164. fflush(dinfo->output_file);
  165. if (ferror(dinfo->output_file))
  166. ERREXIT(cinfo, JERR_FILE_WRITE);
  167. }
  168. /*
  169. * The module selection routine for PPM format output.
  170. */
  171. GLOBAL(djpeg_dest_ptr)
  172. jinit_write_ppm (j_decompress_ptr cinfo)
  173. {
  174. ppm_dest_ptr dest;
  175. /* Create module interface object, fill in method pointers */
  176. dest = (ppm_dest_ptr)
  177. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  178. sizeof(ppm_dest_struct));
  179. dest->pub.start_output = start_output_ppm;
  180. dest->pub.finish_output = finish_output_ppm;
  181. /* Calculate output image dimensions so we can allocate space */
  182. jpeg_calc_output_dimensions(cinfo);
  183. /* Create physical I/O buffer */
  184. dest->samples_per_row = cinfo->output_width * cinfo->out_color_components;
  185. dest->buffer_width = dest->samples_per_row * (BYTESPERSAMPLE * sizeof(char));
  186. dest->iobuffer = (char *) (*cinfo->mem->alloc_small)
  187. ((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width);
  188. if (cinfo->quantize_colors || BITS_IN_JSAMPLE != 8 ||
  189. sizeof(JSAMPLE) != sizeof(char)) {
  190. /* When quantizing, we need an output buffer for colormap indexes
  191. * that's separate from the physical I/O buffer. We also need a
  192. * separate buffer if pixel format translation must take place.
  193. */
  194. dest->pub.buffer = (*cinfo->mem->alloc_sarray)
  195. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  196. cinfo->output_width * cinfo->output_components, (JDIMENSION) 1);
  197. dest->pub.buffer_height = 1;
  198. if (! cinfo->quantize_colors)
  199. dest->pub.put_pixel_rows = copy_pixel_rows;
  200. else if (cinfo->out_color_space == JCS_GRAYSCALE)
  201. dest->pub.put_pixel_rows = put_demapped_gray;
  202. else
  203. dest->pub.put_pixel_rows = put_demapped_rgb;
  204. } else {
  205. /* We will fwrite() directly from decompressor output buffer. */
  206. /* Synthesize a JSAMPARRAY pointer structure */
  207. dest->pixrow = (JSAMPROW) dest->iobuffer;
  208. dest->pub.buffer = & dest->pixrow;
  209. dest->pub.buffer_height = 1;
  210. dest->pub.put_pixel_rows = put_pixel_rows;
  211. }
  212. return (djpeg_dest_ptr) dest;
  213. }
  214. #endif /* PPM_SUPPORTED */