home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / compresn / jpegv3sr / jwrppm.c < prev    next >
C/C++ Source or Header  |  1992-01-17  |  4KB  |  175 lines

  1. /*
  2.  * jwrppm.c
  3.  *
  4.  * Copyright (C) 1991, 1992, 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 format.
  9.  * The PBMPLUS library is required (well, it will be in the real version).
  10.  *
  11.  * These routines may need modification for non-Unix environments or
  12.  * specialized applications.  As they stand, they assume output to
  13.  * an ordinary stdio stream.
  14.  *
  15.  * These routines are invoked via the methods put_pixel_rows, put_color_map,
  16.  * and output_init/term.
  17.  */
  18.  
  19. #include "jinclude.h"
  20.  
  21. #ifdef PPM_SUPPORTED
  22.  
  23.  
  24. /*
  25.  * Haven't yet got around to making this work with text-format output,
  26.  * hence cannot handle pixels wider than 8 bits.
  27.  */
  28.  
  29. #ifndef EIGHT_BIT_SAMPLES
  30.   Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
  31. #endif
  32.  
  33.  
  34. /*
  35.  * Write the file header.
  36.  */
  37.  
  38. METHODDEF void
  39. output_init (decompress_info_ptr cinfo)
  40. {
  41.   if (cinfo->out_color_space == CS_GRAYSCALE) {
  42.     /* emit header for raw PGM format */
  43.     fprintf(cinfo->output_file, "P5\n%ld %ld\n%d\n",
  44.         cinfo->image_width, cinfo->image_height, 255);
  45.   } else if (cinfo->out_color_space == CS_RGB) {
  46.     /* emit header for raw PPM format */
  47.     fprintf(cinfo->output_file, "P6\n%ld %ld\n%d\n",
  48.         cinfo->image_width, cinfo->image_height, 255);
  49.   } else {
  50.     ERREXIT(cinfo->emethods, "PPM output must be grayscale or RGB");
  51.   }
  52. }
  53.  
  54.  
  55. /*
  56.  * Write some pixel data.
  57.  */
  58.  
  59. METHODDEF void
  60. put_pixel_rows (decompress_info_ptr cinfo, int num_rows,
  61.         JSAMPIMAGE pixel_data)
  62. {
  63.   register FILE * outfile = cinfo->output_file;
  64.   register JSAMPROW ptr0, ptr1, ptr2;
  65.   register long col;
  66.   register long width = cinfo->image_width;
  67.   register int row;
  68.   
  69.   if (cinfo->out_color_space == CS_GRAYSCALE) {
  70.     for (row = 0; row < num_rows; row++) {
  71.       ptr0 = pixel_data[0][row];
  72.       for (col = width; col > 0; col--) {
  73.     putc(GETJSAMPLE(*ptr0), outfile);
  74.     ptr0++;
  75.       }
  76.     }
  77.   } else {
  78.     for (row = 0; row < num_rows; row++) {
  79.       ptr0 = pixel_data[0][row];
  80.       ptr1 = pixel_data[1][row];
  81.       ptr2 = pixel_data[2][row];
  82.       for (col = width; col > 0; col--) {
  83.     putc(GETJSAMPLE(*ptr0), outfile);
  84.     ptr0++;
  85.     putc(GETJSAMPLE(*ptr1), outfile);
  86.     ptr1++;
  87.     putc(GETJSAMPLE(*ptr2), outfile);
  88.     ptr2++;
  89.       }
  90.     }
  91.   }
  92. }
  93.  
  94.  
  95. /*
  96.  * Write some pixel data when color quantization is in effect.
  97.  */
  98.  
  99. METHODDEF void
  100. put_demapped_rows (decompress_info_ptr cinfo, int num_rows,
  101.            JSAMPIMAGE pixel_data)
  102. {
  103.   register FILE * outfile = cinfo->output_file;
  104.   register JSAMPARRAY color_map = cinfo->colormap;
  105.   register JSAMPROW ptr;
  106.   register long col;
  107.   long width = cinfo->image_width;
  108.   int row;
  109.   
  110.   if (cinfo->out_color_space == CS_GRAYSCALE) {
  111.     for (row = 0; row < num_rows; row++) {
  112.       ptr = pixel_data[0][row];
  113.       for (col = width; col > 0; col--) {
  114.     putc(GETJSAMPLE(color_map[0][GETJSAMPLE(*ptr)]), outfile);
  115.     ptr++;
  116.       }
  117.     }
  118.   } else {
  119.     for (row = 0; row < num_rows; row++) {
  120.       ptr = pixel_data[0][row];
  121.       for (col = width; col > 0; col--) {
  122.     register int pixval = GETJSAMPLE(*ptr);
  123.  
  124.     putc(GETJSAMPLE(color_map[0][pixval]), outfile);
  125.     putc(GETJSAMPLE(color_map[1][pixval]), outfile);
  126.     putc(GETJSAMPLE(color_map[2][pixval]), outfile);
  127.     ptr++;
  128.       }
  129.     }
  130.   }
  131. }
  132.  
  133.  
  134. /*
  135.  * Write the color map.
  136.  * For PPM output, we just demap the output data!
  137.  */
  138.  
  139. METHODDEF void
  140. put_color_map (decompress_info_ptr cinfo, int num_colors, JSAMPARRAY colormap)
  141. {
  142.   cinfo->methods->put_pixel_rows = put_demapped_rows;
  143. }
  144.  
  145.  
  146. /*
  147.  * Finish up at the end of the file.
  148.  */
  149.  
  150. METHODDEF void
  151. output_term (decompress_info_ptr cinfo)
  152. {
  153.   /* No work except to make sure we wrote the output file OK */
  154.   fflush(cinfo->output_file);
  155.   if (ferror(cinfo->output_file))
  156.     ERREXIT(cinfo->emethods, "Output file write error");
  157. }
  158.  
  159.  
  160. /*
  161.  * The method selection routine for PPM format output.
  162.  * This should be called from d_ui_method_selection if PPM output is wanted.
  163.  */
  164.  
  165. GLOBAL void
  166. jselwppm (decompress_info_ptr cinfo)
  167. {
  168.   cinfo->methods->output_init = output_init;
  169.   cinfo->methods->put_color_map = put_color_map;
  170.   cinfo->methods->put_pixel_rows = put_pixel_rows;
  171.   cinfo->methods->output_term = output_term;
  172. }
  173.  
  174. #endif /* PPM_SUPPORTED */
  175.