kusano 7d535a
/*
kusano 7d535a
 * wrtarga.c
kusano 7d535a
 *
shun-iwasawa 82a8f5
 * This file was part of the Independent JPEG Group's software:
kusano 7d535a
 * Copyright (C) 1991-1996, Thomas G. Lane.
shun-iwasawa 82a8f5
 * libjpeg-turbo Modifications:
shun-iwasawa 82a8f5
 * Copyright (C) 2017, D. R. Commander.
shun-iwasawa 82a8f5
 * For conditions of distribution and use, see the accompanying README.ijg
shun-iwasawa 82a8f5
 * file.
kusano 7d535a
 *
kusano 7d535a
 * This file contains routines to write output images in Targa format.
kusano 7d535a
 *
kusano 7d535a
 * These routines may need modification for non-Unix environments or
kusano 7d535a
 * specialized applications.  As they stand, they assume output to
kusano 7d535a
 * an ordinary stdio stream.
kusano 7d535a
 *
kusano 7d535a
 * Based on code contributed by Lee Daniel Crocker.
kusano 7d535a
 */
kusano 7d535a
shun-iwasawa 82a8f5
#include "cdjpeg.h"             /* Common decls for cjpeg/djpeg applications */
kusano 7d535a
kusano 7d535a
#ifdef TARGA_SUPPORTED
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * To support 12-bit JPEG data, we'd have to scale output down to 8 bits.
kusano 7d535a
 * This is not yet implemented.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#if BITS_IN_JSAMPLE != 8
kusano 7d535a
  Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Private version of data destination object */
kusano 7d535a
kusano 7d535a
typedef struct {
shun-iwasawa 82a8f5
  struct djpeg_dest_struct pub; /* public fields */
kusano 7d535a
shun-iwasawa 82a8f5
  char *iobuffer;               /* physical I/O buffer */
shun-iwasawa 82a8f5
  JDIMENSION buffer_width;      /* width of one row */
kusano 7d535a
} tga_dest_struct;
kusano 7d535a
shun-iwasawa 82a8f5
typedef tga_dest_struct *tga_dest_ptr;
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
shun-iwasawa 82a8f5
write_header(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, int num_colors)
kusano 7d535a
/* Create and write a Targa header */
kusano 7d535a
{
kusano 7d535a
  char targaheader[18];
kusano 7d535a
kusano 7d535a
  /* Set unused fields of header to 0 */
shun-iwasawa 82a8f5
  MEMZERO(targaheader, sizeof(targaheader));
kusano 7d535a
kusano 7d535a
  if (num_colors > 0) {
shun-iwasawa 82a8f5
    targaheader[1] = 1;         /* color map type 1 */
shun-iwasawa 82a8f5
    targaheader[5] = (char)(num_colors & 0xFF);
shun-iwasawa 82a8f5
    targaheader[6] = (char)(num_colors >> 8);
shun-iwasawa 82a8f5
    targaheader[7] = 24;        /* 24 bits per cmap entry */
kusano 7d535a
  }
kusano 7d535a
shun-iwasawa 82a8f5
  targaheader[12] = (char)(cinfo->output_width & 0xFF);
shun-iwasawa 82a8f5
  targaheader[13] = (char)(cinfo->output_width >> 8);
shun-iwasawa 82a8f5
  targaheader[14] = (char)(cinfo->output_height & 0xFF);
shun-iwasawa 82a8f5
  targaheader[15] = (char)(cinfo->output_height >> 8);
shun-iwasawa 82a8f5
  targaheader[17] = 0x20;       /* Top-down, non-interlaced */
kusano 7d535a
kusano 7d535a
  if (cinfo->out_color_space == JCS_GRAYSCALE) {
shun-iwasawa 82a8f5
    targaheader[2] = 3;         /* image type = uncompressed grayscale */
shun-iwasawa 82a8f5
    targaheader[16] = 8;        /* bits per pixel */
shun-iwasawa 82a8f5
  } else {                      /* must be RGB */
kusano 7d535a
    if (num_colors > 0) {
shun-iwasawa 82a8f5
      targaheader[2] = 1;       /* image type = colormapped RGB */
kusano 7d535a
      targaheader[16] = 8;
kusano 7d535a
    } else {
shun-iwasawa 82a8f5
      targaheader[2] = 2;       /* image type = uncompressed RGB */
kusano 7d535a
      targaheader[16] = 24;
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
shun-iwasawa 82a8f5
  if (JFWRITE(dinfo->output_file, targaheader, 18) != (size_t)18)
kusano 7d535a
    ERREXIT(cinfo, JERR_FILE_WRITE);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Write some pixel data.
kusano 7d535a
 * In this module rows_supplied will always be 1.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
shun-iwasawa 82a8f5
put_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
shun-iwasawa 82a8f5
               JDIMENSION rows_supplied)
kusano 7d535a
/* used for unquantized full-color output */
kusano 7d535a
{
shun-iwasawa 82a8f5
  tga_dest_ptr dest = (tga_dest_ptr)dinfo;
kusano 7d535a
  register JSAMPROW inptr;
shun-iwasawa 82a8f5
  register char *outptr;
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
kusano 7d535a
  inptr = dest->pub.buffer[0];
kusano 7d535a
  outptr = dest->iobuffer;
kusano 7d535a
  for (col = cinfo->output_width; col > 0; col--) {
shun-iwasawa 82a8f5
    outptr[0] = (char)GETJSAMPLE(inptr[2]); /* RGB to BGR order */
shun-iwasawa 82a8f5
    outptr[1] = (char)GETJSAMPLE(inptr[1]);
shun-iwasawa 82a8f5
    outptr[2] = (char)GETJSAMPLE(inptr[0]);
kusano 7d535a
    inptr += 3, outptr += 3;
kusano 7d535a
  }
shun-iwasawa 82a8f5
  (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
METHODDEF(void)
shun-iwasawa 82a8f5
put_gray_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
shun-iwasawa 82a8f5
              JDIMENSION rows_supplied)
kusano 7d535a
/* used for grayscale OR quantized color output */
kusano 7d535a
{
shun-iwasawa 82a8f5
  tga_dest_ptr dest = (tga_dest_ptr)dinfo;
kusano 7d535a
  register JSAMPROW inptr;
shun-iwasawa 82a8f5
  register char *outptr;
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
kusano 7d535a
  inptr = dest->pub.buffer[0];
kusano 7d535a
  outptr = dest->iobuffer;
kusano 7d535a
  for (col = cinfo->output_width; col > 0; col--) {
shun-iwasawa 82a8f5
    *outptr++ = (char)GETJSAMPLE(*inptr++);
kusano 7d535a
  }
shun-iwasawa 82a8f5
  (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Write some demapped pixel data when color quantization is in effect.
kusano 7d535a
 * For Targa, this is only applied to grayscale data.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
shun-iwasawa 82a8f5
put_demapped_gray(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
shun-iwasawa 82a8f5
                  JDIMENSION rows_supplied)
kusano 7d535a
{
shun-iwasawa 82a8f5
  tga_dest_ptr dest = (tga_dest_ptr)dinfo;
kusano 7d535a
  register JSAMPROW inptr;
shun-iwasawa 82a8f5
  register char *outptr;
kusano 7d535a
  register JSAMPROW color_map0 = cinfo->colormap[0];
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
kusano 7d535a
  inptr = dest->pub.buffer[0];
kusano 7d535a
  outptr = dest->iobuffer;
kusano 7d535a
  for (col = cinfo->output_width; col > 0; col--) {
shun-iwasawa 82a8f5
    *outptr++ = (char)GETJSAMPLE(color_map0[GETJSAMPLE(*inptr++)]);
kusano 7d535a
  }
shun-iwasawa 82a8f5
  (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Startup: write the file header.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
shun-iwasawa 82a8f5
start_output_tga(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
kusano 7d535a
{
shun-iwasawa 82a8f5
  tga_dest_ptr dest = (tga_dest_ptr)dinfo;
kusano 7d535a
  int num_colors, i;
kusano 7d535a
  FILE *outfile;
kusano 7d535a
kusano 7d535a
  if (cinfo->out_color_space == JCS_GRAYSCALE) {
kusano 7d535a
    /* Targa doesn't have a mapped grayscale format, so we will */
kusano 7d535a
    /* demap quantized gray output.  Never emit a colormap. */
kusano 7d535a
    write_header(cinfo, dinfo, 0);
kusano 7d535a
    if (cinfo->quantize_colors)
kusano 7d535a
      dest->pub.put_pixel_rows = put_demapped_gray;
kusano 7d535a
    else
kusano 7d535a
      dest->pub.put_pixel_rows = put_gray_rows;
kusano 7d535a
  } else if (cinfo->out_color_space == JCS_RGB) {
kusano 7d535a
    if (cinfo->quantize_colors) {
kusano 7d535a
      /* We only support 8-bit colormap indexes, so only 256 colors */
kusano 7d535a
      num_colors = cinfo->actual_number_of_colors;
kusano 7d535a
      if (num_colors > 256)
shun-iwasawa 82a8f5
        ERREXIT1(cinfo, JERR_TOO_MANY_COLORS, num_colors);
kusano 7d535a
      write_header(cinfo, dinfo, num_colors);
kusano 7d535a
      /* Write the colormap.  Note Targa uses BGR byte order */
kusano 7d535a
      outfile = dest->pub.output_file;
kusano 7d535a
      for (i = 0; i < num_colors; i++) {
shun-iwasawa 82a8f5
        putc(GETJSAMPLE(cinfo->colormap[2][i]), outfile);
shun-iwasawa 82a8f5
        putc(GETJSAMPLE(cinfo->colormap[1][i]), outfile);
shun-iwasawa 82a8f5
        putc(GETJSAMPLE(cinfo->colormap[0][i]), outfile);
kusano 7d535a
      }
kusano 7d535a
      dest->pub.put_pixel_rows = put_gray_rows;
kusano 7d535a
    } else {
kusano 7d535a
      write_header(cinfo, dinfo, 0);
kusano 7d535a
      dest->pub.put_pixel_rows = put_pixel_rows;
kusano 7d535a
    }
kusano 7d535a
  } else {
kusano 7d535a
    ERREXIT(cinfo, JERR_TGA_COLORSPACE);
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Finish up at the end of the file.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
shun-iwasawa 82a8f5
finish_output_tga(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
kusano 7d535a
{
kusano 7d535a
  /* Make sure we wrote the output file OK */
kusano 7d535a
  fflush(dinfo->output_file);
kusano 7d535a
  if (ferror(dinfo->output_file))
kusano 7d535a
    ERREXIT(cinfo, JERR_FILE_WRITE);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
shun-iwasawa 82a8f5
 * Re-calculate buffer dimensions based on output dimensions.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
calc_buffer_dimensions_tga(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  tga_dest_ptr dest = (tga_dest_ptr)dinfo;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  dest->buffer_width = cinfo->output_width * cinfo->output_components;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
kusano 7d535a
 * The module selection routine for Targa format output.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(djpeg_dest_ptr)
shun-iwasawa 82a8f5
jinit_write_targa(j_decompress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  tga_dest_ptr dest;
kusano 7d535a
kusano 7d535a
  /* Create module interface object, fill in method pointers */
kusano 7d535a
  dest = (tga_dest_ptr)
shun-iwasawa 82a8f5
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
                                sizeof(tga_dest_struct));
kusano 7d535a
  dest->pub.start_output = start_output_tga;
kusano 7d535a
  dest->pub.finish_output = finish_output_tga;
shun-iwasawa 82a8f5
  dest->pub.calc_buffer_dimensions = calc_buffer_dimensions_tga;
kusano 7d535a
kusano 7d535a
  /* Calculate output image dimensions so we can allocate space */
kusano 7d535a
  jpeg_calc_output_dimensions(cinfo);
kusano 7d535a
shun-iwasawa 82a8f5
  /* Create I/O buffer. */
shun-iwasawa 82a8f5
  dest->pub.calc_buffer_dimensions(cinfo, (djpeg_dest_ptr)dest);
kusano 7d535a
  dest->iobuffer = (char *)
shun-iwasawa 82a8f5
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
                                (size_t)(dest->buffer_width * sizeof(char)));
kusano 7d535a
kusano 7d535a
  /* Create decompressor output buffer. */
kusano 7d535a
  dest->pub.buffer = (*cinfo->mem->alloc_sarray)
shun-iwasawa 82a8f5
    ((j_common_ptr)cinfo, JPOOL_IMAGE, dest->buffer_width, (JDIMENSION)1);
kusano 7d535a
  dest->pub.buffer_height = 1;
kusano 7d535a
shun-iwasawa 82a8f5
  return (djpeg_dest_ptr)dest;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#endif /* TARGA_SUPPORTED */