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

  1. /*
  2.  * jwrtarga.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 Targa format.
  9.  *
  10.  * These routines may need modification for non-Unix environments or
  11.  * specialized applications.  As they stand, they assume output to
  12.  * an ordinary stdio stream.
  13.  *
  14.  * These routines are invoked via the methods put_pixel_rows, put_color_map,
  15.  * and output_init/term.
  16.  *
  17.  * Based on code contributed by Lee Daniel Crocker.
  18.  */
  19.  
  20. #include "jinclude.h"
  21.  
  22. #ifdef TARGA_SUPPORTED
  23.  
  24.  
  25. /*
  26.  * To support 12-bit JPEG data, we'd have to scale output down to 8 bits.
  27.  * This is not yet implemented.
  28.  */
  29.  
  30. #ifndef EIGHT_BIT_SAMPLES
  31.   Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
  32. #endif
  33.  
  34.  
  35. LOCAL void
  36. write_header (decompress_info_ptr cinfo, int num_colors)
  37. /* Create and write a Targa header */
  38. {
  39.   char targaheader[18];
  40.  
  41.   /* Set unused fields of header to 0 */
  42.   MEMZERO((void *) targaheader, SIZEOF(targaheader));
  43.  
  44.   if (num_colors > 0) {
  45.     targaheader[1] = 1;        /* color map type 1 */
  46.     targaheader[5] = (char) (num_colors & 0xFF);
  47.     targaheader[6] = (char) (num_colors >> 8);
  48.     targaheader[7] = 24;    /* 24 bits per cmap entry */
  49.   }
  50.  
  51.   targaheader[12] = (char) (cinfo->image_width & 0xFF);
  52.   targaheader[13] = (char) (cinfo->image_width >> 8);
  53.   targaheader[14] = (char) (cinfo->image_height & 0xFF);
  54.   targaheader[15] = (char) (cinfo->image_height >> 8);
  55.   targaheader[17] = 0x20;    /* Top-down, non-interlaced */
  56.  
  57.   if (cinfo->out_color_space == CS_GRAYSCALE) {
  58.     targaheader[2] = 3;        /* image type = uncompressed gray-scale */
  59.     targaheader[16] = 8;    /* bits per pixel */
  60.   } else {            /* must be RGB */
  61.     if (num_colors > 0) {
  62.       targaheader[2] = 1;    /* image type = colormapped RGB */
  63.       targaheader[16] = 8;
  64.     } else {
  65.       targaheader[2] = 2;    /* image type = uncompressed RGB */
  66.       targaheader[16] = 24;
  67.     }
  68.   }
  69.  
  70.   if (JFWRITE(cinfo->output_file, targaheader, 18) != (size_t) 18)
  71.     ERREXIT(cinfo->emethods, "Could not write Targa header");
  72. }
  73.  
  74.  
  75. /*
  76.  * Write the file header.
  77.  */
  78.  
  79. METHODDEF void
  80. output_init (decompress_info_ptr cinfo)
  81. {
  82.   if (cinfo->out_color_space == CS_GRAYSCALE) {
  83.     /* Targa doesn't have a mapped grayscale format, so we will */
  84.     /* demap quantized gray output.  Never emit a colormap. */
  85.     write_header(cinfo, 0);
  86.   } else if (cinfo->out_color_space == CS_RGB) {
  87.     /* For quantized output, defer writing header until put_color_map time. */
  88.     if (! cinfo->quantize_colors)
  89.       write_header(cinfo, 0);
  90.   } else {
  91.     ERREXIT(cinfo->emethods, "Targa output must be grayscale or RGB");
  92.   }
  93. }
  94.  
  95.  
  96. /*
  97.  * Write some pixel data.
  98.  */
  99.  
  100. METHODDEF void
  101. put_pixel_rows (decompress_info_ptr cinfo, int num_rows,
  102.         JSAMPIMAGE pixel_data)
  103. {
  104.   register FILE * outfile = cinfo->output_file;
  105.   register JSAMPROW ptr0, ptr1, ptr2;
  106.   register long col;
  107.   register long width = cinfo->image_width;
  108.   register int row;
  109.   
  110.   if (cinfo->final_out_comps == 1) {
  111.     /* here for grayscale or quantized color output */
  112.     for (row = 0; row < num_rows; row++) {
  113.       ptr0 = pixel_data[0][row];
  114.       for (col = width; col > 0; col--) {
  115.     putc(GETJSAMPLE(*ptr0), outfile);
  116.     ptr0++;
  117.       }
  118.     }
  119.   } else {
  120.     /* here for unquantized color output */
  121.     for (row = 0; row < num_rows; row++) {
  122.       ptr0 = pixel_data[0][row];
  123.       ptr1 = pixel_data[1][row];
  124.       ptr2 = pixel_data[2][row];
  125.       for (col = width; col > 0; col--) {
  126.     putc(GETJSAMPLE(*ptr2), outfile); /* write in BGR order */
  127.     ptr2++;
  128.     putc(GETJSAMPLE(*ptr1), outfile);
  129.     ptr1++;
  130.     putc(GETJSAMPLE(*ptr0), outfile);
  131.     ptr0++;
  132.       }
  133.     }
  134.   }
  135. }
  136.  
  137.  
  138. /*
  139.  * Write some demapped pixel data when color quantization is in effect.
  140.  * For Targa, this is only applied to grayscale data.
  141.  */
  142.  
  143. METHODDEF void
  144. put_demapped_rows (decompress_info_ptr cinfo, int num_rows,
  145.            JSAMPIMAGE pixel_data)
  146. {
  147.   register FILE * outfile = cinfo->output_file;
  148.   register JSAMPARRAY color_map = cinfo->colormap;
  149.   register JSAMPROW ptr;
  150.   register long col;
  151.   long width = cinfo->image_width;
  152.   int row;
  153.   
  154.   for (row = 0; row < num_rows; row++) {
  155.     ptr = pixel_data[0][row];
  156.     for (col = width; col > 0; col--) {
  157.       putc(GETJSAMPLE(color_map[0][GETJSAMPLE(*ptr)]), outfile);
  158.       ptr++;
  159.     }
  160.   }
  161. }
  162.  
  163.  
  164. /*
  165.  * Write the color map.
  166.  */
  167.  
  168. METHODDEF void
  169. put_color_map (decompress_info_ptr cinfo, int num_colors, JSAMPARRAY colormap)
  170. {
  171.   register FILE * outfile = cinfo->output_file;
  172.   int i;
  173.  
  174.   if (cinfo->out_color_space == CS_RGB) {
  175.     /* We only support 8-bit colormap indexes, so only 256 colors */
  176.     if (num_colors > 256)
  177.       ERREXIT(cinfo->emethods, "Too many colors for Targa output");
  178.     /* Time to write the header */
  179.     write_header(cinfo, num_colors);
  180.     /* Write the colormap.  Note Targa uses BGR byte order */
  181.     for (i = 0; i < num_colors; i++) {
  182.       putc(GETJSAMPLE(colormap[2][i]), outfile);
  183.       putc(GETJSAMPLE(colormap[1][i]), outfile);
  184.       putc(GETJSAMPLE(colormap[0][i]), outfile);
  185.     }
  186.   } else {
  187.     cinfo->methods->put_pixel_rows = put_demapped_rows;
  188.   }
  189. }
  190.  
  191.  
  192. /*
  193.  * Finish up at the end of the file.
  194.  */
  195.  
  196. METHODDEF void
  197. output_term (decompress_info_ptr cinfo)
  198. {
  199.   /* No work except to make sure we wrote the output file OK */
  200.   fflush(cinfo->output_file);
  201.   if (ferror(cinfo->output_file))
  202.     ERREXIT(cinfo->emethods, "Output file write error");
  203. }
  204.  
  205.  
  206. /*
  207.  * The method selection routine for Targa format output.
  208.  * This should be called from d_ui_method_selection if Targa output is wanted.
  209.  */
  210.  
  211. GLOBAL void
  212. jselwtarga (decompress_info_ptr cinfo)
  213. {
  214.   cinfo->methods->output_init = output_init;
  215.   cinfo->methods->put_color_map = put_color_map;
  216.   cinfo->methods->put_pixel_rows = put_pixel_rows;
  217.   cinfo->methods->output_term = output_term;
  218. }
  219.  
  220. #endif /* TARGA_SUPPORTED */
  221.