wrppm.c 8.1 KB

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