home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / GRAPHICS / jpegsrc.v3.lzh / jwrppm.c < prev    next >
Text File  |  1992-07-03  |  5KB  |  192 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.    OS-9 uses CR (0x0D) for the end-of-line character instead of the
  25.    UNIX LF (0x0A).  The OS-9 cc and gcc interpret \n as 0x0D.  This
  26.    confuses the program when reading PPM files which use the UNIX
  27.    convention.  This problem is the origin of the #ifdef OSK #endif
  28.    macros in the following code.
  29. */
  30.  
  31. /*
  32.  * Haven't yet got around to making this work with text-format output,
  33.  * hence cannot handle pixels wider than 8 bits.
  34.  */
  35.  
  36. #ifndef EIGHT_BIT_SAMPLES
  37.   Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
  38. #endif
  39.  
  40.  
  41. /*
  42.  * Write the file header.
  43.  */
  44.  
  45. METHODDEF void
  46. output_init (decompress_info_ptr cinfo)
  47. {
  48.   if (cinfo->out_color_space == CS_GRAYSCALE) {
  49.     /* emit header for raw PGM format */
  50. #ifdef OSK
  51.     fprintf(cinfo->output_file, "P5\l%ld %ld\l%d\l",
  52.         cinfo->image_width, cinfo->image_height, 255);
  53. #else
  54.     fprintf(cinfo->output_file, "P5\n%ld %ld\n%d\n",
  55.         cinfo->image_width, cinfo->image_height, 255);
  56. #endif /* OSK */   
  57.   } else if (cinfo->out_color_space == CS_RGB) {
  58.     /* emit header for raw PPM format */
  59. #ifdef OSK
  60.     fprintf(cinfo->output_file, "P6\l%ld %ld\l%d\l",
  61.         cinfo->image_width, cinfo->image_height, 255);
  62. #else
  63.     fprintf(cinfo->output_file, "P6\n%ld %ld\n%d\n",
  64.         cinfo->image_width, cinfo->image_height, 255);
  65. #endif /* OSK */
  66.   } else {
  67.     ERREXIT(cinfo->emethods, "PPM output must be grayscale or RGB");
  68.   }
  69. }
  70.  
  71.  
  72. /*
  73.  * Write some pixel data.
  74.  */
  75.  
  76. METHODDEF void
  77. put_pixel_rows (decompress_info_ptr cinfo, int num_rows,
  78.         JSAMPIMAGE pixel_data)
  79. {
  80.   register FILE * outfile = cinfo->output_file;
  81.   register JSAMPROW ptr0, ptr1, ptr2;
  82.   register long col;
  83.   register long width = cinfo->image_width;
  84.   register int row;
  85.   
  86.   if (cinfo->out_color_space == CS_GRAYSCALE) {
  87.     for (row = 0; row < num_rows; row++) {
  88.       ptr0 = pixel_data[0][row];
  89.       for (col = width; col > 0; col--) {
  90.     putc(GETJSAMPLE(*ptr0), outfile);
  91.     ptr0++;
  92.       }
  93.     }
  94.   } else {
  95.     for (row = 0; row < num_rows; row++) {
  96.       ptr0 = pixel_data[0][row];
  97.       ptr1 = pixel_data[1][row];
  98.       ptr2 = pixel_data[2][row];
  99.       for (col = width; col > 0; col--) {
  100.     putc(GETJSAMPLE(*ptr0), outfile);
  101.     ptr0++;
  102.     putc(GETJSAMPLE(*ptr1), outfile);
  103.     ptr1++;
  104.     putc(GETJSAMPLE(*ptr2), outfile);
  105.     ptr2++;
  106.       }
  107.     }
  108.   }
  109. }
  110.  
  111.  
  112. /*
  113.  * Write some pixel data when color quantization is in effect.
  114.  */
  115.  
  116. METHODDEF void
  117. put_demapped_rows (decompress_info_ptr cinfo, int num_rows,
  118.            JSAMPIMAGE pixel_data)
  119. {
  120.   register FILE * outfile = cinfo->output_file;
  121.   register JSAMPARRAY color_map = cinfo->colormap;
  122.   register JSAMPROW ptr;
  123.   register long col;
  124.   long width = cinfo->image_width;
  125.   int row;
  126.   
  127.   if (cinfo->out_color_space == CS_GRAYSCALE) {
  128.     for (row = 0; row < num_rows; row++) {
  129.       ptr = pixel_data[0][row];
  130.       for (col = width; col > 0; col--) {
  131.     putc(GETJSAMPLE(color_map[0][GETJSAMPLE(*ptr)]), outfile);
  132.     ptr++;
  133.       }
  134.     }
  135.   } else {
  136.     for (row = 0; row < num_rows; row++) {
  137.       ptr = pixel_data[0][row];
  138.       for (col = width; col > 0; col--) {
  139.     register int pixval = GETJSAMPLE(*ptr);
  140.  
  141.     putc(GETJSAMPLE(color_map[0][pixval]), outfile);
  142.     putc(GETJSAMPLE(color_map[1][pixval]), outfile);
  143.     putc(GETJSAMPLE(color_map[2][pixval]), outfile);
  144.     ptr++;
  145.       }
  146.     }
  147.   }
  148. }
  149.  
  150.  
  151. /*
  152.  * Write the color map.
  153.  * For PPM output, we just demap the output data!
  154.  */
  155.  
  156. METHODDEF void
  157. put_color_map (decompress_info_ptr cinfo, int num_colors, JSAMPARRAY colormap)
  158. {
  159.   cinfo->methods->put_pixel_rows = put_demapped_rows;
  160. }
  161.  
  162.  
  163. /*
  164.  * Finish up at the end of the file.
  165.  */
  166.  
  167. METHODDEF void
  168. output_term (decompress_info_ptr cinfo)
  169. {
  170.   /* No work except to make sure we wrote the output file OK */
  171.   fflush(cinfo->output_file);
  172.   if (ferror(cinfo->output_file))
  173.     ERREXIT(cinfo->emethods, "Output file write error");
  174. }
  175.  
  176.  
  177. /*
  178.  * The method selection routine for PPM format output.
  179.  * This should be called from d_ui_method_selection if PPM output is wanted.
  180.  */
  181.  
  182. GLOBAL void
  183. jselwppm (decompress_info_ptr cinfo)
  184. {
  185.   cinfo->methods->output_init = output_init;
  186.   cinfo->methods->put_color_map = put_color_map;
  187.   cinfo->methods->put_pixel_rows = put_pixel_rows;
  188.   cinfo->methods->output_term = output_term;
  189. }
  190.  
  191. #endif /* PPM_SUPPORTED */
  192.