Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

269 lines
8.2 KiB

  1. /*
  2. * wrppm.c
  3. *
  4. * Copyright (C) 1991-1996, Thomas G. Lane.
  5. * Modified 2009 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->pub.buffer[0];
  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->pub.buffer[0];
  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_map = cinfo->colormap[0];
  135. register JDIMENSION col;
  136. ptr = dest->pub.buffer[0];
  137. bufferptr = dest->iobuffer;
  138. for (col = cinfo->output_width; col > 0; col--) {
  139. PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map[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. ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
  150. /* Emit file header */
  151. switch (cinfo->out_color_space) {
  152. case JCS_GRAYSCALE:
  153. /* emit header for raw PGM format */
  154. fprintf(dest->pub.output_file, "P5\n%ld %ld\n%d\n",
  155. (long) cinfo->output_width, (long) cinfo->output_height,
  156. PPM_MAXVAL);
  157. break;
  158. case JCS_RGB:
  159. /* emit header for raw PPM format */
  160. fprintf(dest->pub.output_file, "P6\n%ld %ld\n%d\n",
  161. (long) cinfo->output_width, (long) cinfo->output_height,
  162. PPM_MAXVAL);
  163. break;
  164. default:
  165. ERREXIT(cinfo, JERR_PPM_COLORSPACE);
  166. }
  167. }
  168. /*
  169. * Finish up at the end of the file.
  170. */
  171. METHODDEF(void)
  172. finish_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
  173. {
  174. /* Make sure we wrote the output file OK */
  175. fflush(dinfo->output_file);
  176. if (ferror(dinfo->output_file))
  177. ERREXIT(cinfo, JERR_FILE_WRITE);
  178. }
  179. /*
  180. * The module selection routine for PPM format output.
  181. */
  182. GLOBAL(djpeg_dest_ptr)
  183. jinit_write_ppm (j_decompress_ptr cinfo)
  184. {
  185. ppm_dest_ptr dest;
  186. /* Create module interface object, fill in method pointers */
  187. dest = (ppm_dest_ptr)
  188. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  189. SIZEOF(ppm_dest_struct));
  190. dest->pub.start_output = start_output_ppm;
  191. dest->pub.finish_output = finish_output_ppm;
  192. /* Calculate output image dimensions so we can allocate space */
  193. jpeg_calc_output_dimensions(cinfo);
  194. /* Create physical I/O buffer. Note we make this near on a PC. */
  195. dest->samples_per_row = cinfo->output_width * cinfo->out_color_components;
  196. dest->buffer_width = dest->samples_per_row * (BYTESPERSAMPLE * SIZEOF(char));
  197. dest->iobuffer = (char *) (*cinfo->mem->alloc_small)
  198. ((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width);
  199. if (cinfo->quantize_colors || BITS_IN_JSAMPLE != 8 ||
  200. SIZEOF(JSAMPLE) != SIZEOF(char)) {
  201. /* When quantizing, we need an output buffer for colormap indexes
  202. * that's separate from the physical I/O buffer. We also need a
  203. * separate buffer if pixel format translation must take place.
  204. */
  205. dest->pub.buffer = (*cinfo->mem->alloc_sarray)
  206. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  207. cinfo->output_width * cinfo->output_components, (JDIMENSION) 1);
  208. dest->pub.buffer_height = 1;
  209. if (! cinfo->quantize_colors)
  210. dest->pub.put_pixel_rows = copy_pixel_rows;
  211. else if (cinfo->out_color_space == JCS_GRAYSCALE)
  212. dest->pub.put_pixel_rows = put_demapped_gray;
  213. else
  214. dest->pub.put_pixel_rows = put_demapped_rgb;
  215. } else {
  216. /* We will fwrite() directly from decompressor output buffer. */
  217. /* Synthesize a JSAMPARRAY pointer structure */
  218. /* Cast here implies near->far pointer conversion on PCs */
  219. dest->pixrow = (JSAMPROW) dest->iobuffer;
  220. dest->pub.buffer = & dest->pixrow;
  221. dest->pub.buffer_height = 1;
  222. dest->pub.put_pixel_rows = put_pixel_rows;
  223. }
  224. return (djpeg_dest_ptr) dest;
  225. }
  226. #endif /* PPM_SUPPORTED */