wrppm.c 8.1 KB

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