shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * wrbmp.c
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * This file was part of the Independent JPEG Group's software:
shun-iwasawa 82a8f5
 * Copyright (C) 1994-1996, Thomas G. Lane.
shun-iwasawa 82a8f5
 * libjpeg-turbo Modifications:
shun-iwasawa 82a8f5
 * Copyright (C) 2013, Linaro Limited.
shun-iwasawa 82a8f5
 * Copyright (C) 2014-2015, 2017, 2019, D. R. Commander.
shun-iwasawa 82a8f5
 * For conditions of distribution and use, see the accompanying README.ijg
shun-iwasawa 82a8f5
 * file.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * This file contains routines to write output images in Microsoft "BMP"
shun-iwasawa 82a8f5
 * format (MS Windows 3.x and OS/2 1.x flavors).
shun-iwasawa 82a8f5
 * Either 8-bit colormapped or 24-bit full-color format can be written.
shun-iwasawa 82a8f5
 * No compression is supported.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * These routines may need modification for non-Unix environments or
shun-iwasawa 82a8f5
 * specialized applications.  As they stand, they assume output to
shun-iwasawa 82a8f5
 * an ordinary stdio stream.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * This code contributed by James Arthur Boucher.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#include "cmyk.h"
shun-iwasawa 82a8f5
#include "cdjpeg.h"             /* Common decls for cjpeg/djpeg applications */
shun-iwasawa 82a8f5
#include "jconfigint.h"
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#ifdef BMP_SUPPORTED
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * To support 12-bit JPEG data, we'd have to scale output down to 8 bits.
shun-iwasawa 82a8f5
 * This is not yet implemented.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#if BITS_IN_JSAMPLE != 8
shun-iwasawa 82a8f5
  Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Since BMP stores scanlines bottom-to-top, we have to invert the image
shun-iwasawa 82a8f5
 * from JPEG's top-to-bottom order.  To do this, we save the outgoing data
shun-iwasawa 82a8f5
 * in a virtual array during put_pixel_row calls, then actually emit the
shun-iwasawa 82a8f5
 * BMP file during finish_output.  The virtual array contains one JSAMPLE per
shun-iwasawa 82a8f5
 * pixel if the output is grayscale or colormapped, three if it is full color.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Private version of data destination object */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
typedef struct {
shun-iwasawa 82a8f5
  struct djpeg_dest_struct pub; /* public fields */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  boolean is_os2;               /* saves the OS2 format request flag */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  jvirt_sarray_ptr whole_image; /* needed to reverse row order */
shun-iwasawa 82a8f5
  JDIMENSION data_width;        /* JSAMPLEs per row */
shun-iwasawa 82a8f5
  JDIMENSION row_width;         /* physical width of one row in the BMP file */
shun-iwasawa 82a8f5
  int pad_bytes;                /* number of padding bytes needed per row */
shun-iwasawa 82a8f5
  JDIMENSION cur_output_row;    /* next row# to write to virtual array */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  boolean use_inversion_array;  /* TRUE = buffer the whole image, which is
shun-iwasawa 82a8f5
                                   stored to disk in bottom-up order, and
shun-iwasawa 82a8f5
                                   receive rows from the calling program in
shun-iwasawa 82a8f5
                                   top-down order
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
                                   FALSE = the calling program will maintain
shun-iwasawa 82a8f5
                                   its own image buffer and write the rows in
shun-iwasawa 82a8f5
                                   bottom-up order */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  JSAMPLE *iobuffer;            /* I/O buffer (used to buffer a single row to
shun-iwasawa 82a8f5
                                   disk if use_inversion_array == FALSE) */
shun-iwasawa 82a8f5
} bmp_dest_struct;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
typedef bmp_dest_struct *bmp_dest_ptr;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Forward declarations */
shun-iwasawa 82a8f5
LOCAL(void) write_colormap(j_decompress_ptr cinfo, bmp_dest_ptr dest,
shun-iwasawa 82a8f5
                           int map_colors, int map_entry_size);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static INLINE boolean is_big_endian(void)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int test_value = 1;
shun-iwasawa 82a8f5
  if (*(char *)&test_value != 1)
shun-iwasawa 82a8f5
    return TRUE;
shun-iwasawa 82a8f5
  return FALSE;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Write some pixel data.
shun-iwasawa 82a8f5
 * In this module rows_supplied will always be 1.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
put_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
shun-iwasawa 82a8f5
               JDIMENSION rows_supplied)
shun-iwasawa 82a8f5
/* This version is for writing 24-bit pixels */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  bmp_dest_ptr dest = (bmp_dest_ptr)dinfo;
shun-iwasawa 82a8f5
  JSAMPARRAY image_ptr;
shun-iwasawa 82a8f5
  register JSAMPROW inptr, outptr;
shun-iwasawa 82a8f5
  register JDIMENSION col;
shun-iwasawa 82a8f5
  int pad;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (dest->use_inversion_array) {
shun-iwasawa 82a8f5
    /* Access next row in virtual array */
shun-iwasawa 82a8f5
    image_ptr = (*cinfo->mem->access_virt_sarray)
shun-iwasawa 82a8f5
      ((j_common_ptr)cinfo, dest->whole_image,
shun-iwasawa 82a8f5
       dest->cur_output_row, (JDIMENSION)1, TRUE);
shun-iwasawa 82a8f5
    dest->cur_output_row++;
shun-iwasawa 82a8f5
    outptr = image_ptr[0];
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    outptr = dest->iobuffer;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Transfer data.  Note destination values must be in BGR order
shun-iwasawa 82a8f5
   * (even though Microsoft's own documents say the opposite).
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
  inptr = dest->pub.buffer[0];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (cinfo->out_color_space == JCS_EXT_BGR) {
shun-iwasawa 82a8f5
    MEMCOPY(outptr, inptr, dest->row_width);
shun-iwasawa 82a8f5
    outptr += cinfo->output_width * 3;
shun-iwasawa 82a8f5
  } else if (cinfo->out_color_space == JCS_RGB565) {
shun-iwasawa 82a8f5
    boolean big_endian = is_big_endian();
shun-iwasawa 82a8f5
    unsigned short *inptr2 = (unsigned short *)inptr;
shun-iwasawa 82a8f5
    for (col = cinfo->output_width; col > 0; col--) {
shun-iwasawa 82a8f5
      if (big_endian) {
shun-iwasawa 82a8f5
        outptr[0] = (*inptr2 >> 5) & 0xF8;
shun-iwasawa 82a8f5
        outptr[1] = ((*inptr2 << 5) & 0xE0) | ((*inptr2 >> 11) & 0x1C);
shun-iwasawa 82a8f5
        outptr[2] = *inptr2 & 0xF8;
shun-iwasawa 82a8f5
      } else {
shun-iwasawa 82a8f5
        outptr[0] = (*inptr2 << 3) & 0xF8;
shun-iwasawa 82a8f5
        outptr[1] = (*inptr2 >> 3) & 0xFC;
shun-iwasawa 82a8f5
        outptr[2] = (*inptr2 >> 8) & 0xF8;
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
      outptr += 3;
shun-iwasawa 82a8f5
      inptr2++;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  } else if (cinfo->out_color_space == JCS_CMYK) {
shun-iwasawa 82a8f5
    for (col = cinfo->output_width; col > 0; col--) {
shun-iwasawa 82a8f5
      /* can omit GETJSAMPLE() safely */
shun-iwasawa 82a8f5
      JSAMPLE c = *inptr++, m = *inptr++, y = *inptr++, k = *inptr++;
shun-iwasawa 82a8f5
      cmyk_to_rgb(c, m, y, k, outptr + 2, outptr + 1, outptr);
shun-iwasawa 82a8f5
      outptr += 3;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    register int rindex = rgb_red[cinfo->out_color_space];
shun-iwasawa 82a8f5
    register int gindex = rgb_green[cinfo->out_color_space];
shun-iwasawa 82a8f5
    register int bindex = rgb_blue[cinfo->out_color_space];
shun-iwasawa 82a8f5
    register int ps = rgb_pixelsize[cinfo->out_color_space];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    for (col = cinfo->output_width; col > 0; col--) {
shun-iwasawa 82a8f5
      /* can omit GETJSAMPLE() safely */
shun-iwasawa 82a8f5
      outptr[0] = inptr[bindex];
shun-iwasawa 82a8f5
      outptr[1] = inptr[gindex];
shun-iwasawa 82a8f5
      outptr[2] = inptr[rindex];
shun-iwasawa 82a8f5
      outptr += 3;  inptr += ps;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Zero out the pad bytes. */
shun-iwasawa 82a8f5
  pad = dest->pad_bytes;
shun-iwasawa 82a8f5
  while (--pad >= 0)
shun-iwasawa 82a8f5
    *outptr++ = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!dest->use_inversion_array)
shun-iwasawa 82a8f5
    (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->row_width);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
put_gray_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
shun-iwasawa 82a8f5
              JDIMENSION rows_supplied)
shun-iwasawa 82a8f5
/* This version is for grayscale OR quantized color output */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  bmp_dest_ptr dest = (bmp_dest_ptr)dinfo;
shun-iwasawa 82a8f5
  JSAMPARRAY image_ptr;
shun-iwasawa 82a8f5
  register JSAMPROW inptr, outptr;
shun-iwasawa 82a8f5
  int pad;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (dest->use_inversion_array) {
shun-iwasawa 82a8f5
    /* Access next row in virtual array */
shun-iwasawa 82a8f5
    image_ptr = (*cinfo->mem->access_virt_sarray)
shun-iwasawa 82a8f5
      ((j_common_ptr)cinfo, dest->whole_image,
shun-iwasawa 82a8f5
       dest->cur_output_row, (JDIMENSION)1, TRUE);
shun-iwasawa 82a8f5
    dest->cur_output_row++;
shun-iwasawa 82a8f5
    outptr = image_ptr[0];
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    outptr = dest->iobuffer;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Transfer data. */
shun-iwasawa 82a8f5
  inptr = dest->pub.buffer[0];
shun-iwasawa 82a8f5
  MEMCOPY(outptr, inptr, cinfo->output_width);
shun-iwasawa 82a8f5
  outptr += cinfo->output_width;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Zero out the pad bytes. */
shun-iwasawa 82a8f5
  pad = dest->pad_bytes;
shun-iwasawa 82a8f5
  while (--pad >= 0)
shun-iwasawa 82a8f5
    *outptr++ = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!dest->use_inversion_array)
shun-iwasawa 82a8f5
    (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->row_width);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Finish up at the end of the file.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * Here is where we really output the BMP file.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * First, routines to write the Windows and OS/2 variants of the file header.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
LOCAL(void)
shun-iwasawa 82a8f5
write_bmp_header(j_decompress_ptr cinfo, bmp_dest_ptr dest)
shun-iwasawa 82a8f5
/* Write a Windows-style BMP file header, including colormap if needed */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  char bmpfileheader[14];
shun-iwasawa 82a8f5
  char bmpinfoheader[40];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define PUT_2B(array, offset, value) \
shun-iwasawa 82a8f5
  (array[offset] = (char)((value) & 0xFF), \
shun-iwasawa 82a8f5
   array[offset + 1] = (char)(((value) >> 8) & 0xFF))
shun-iwasawa 82a8f5
#define PUT_4B(array, offset, value) \
shun-iwasawa 82a8f5
  (array[offset] = (char)((value) & 0xFF), \
shun-iwasawa 82a8f5
   array[offset + 1] = (char)(((value) >> 8) & 0xFF), \
shun-iwasawa 82a8f5
   array[offset + 2] = (char)(((value) >> 16) & 0xFF), \
shun-iwasawa 82a8f5
   array[offset + 3] = (char)(((value) >> 24) & 0xFF))
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  long headersize, bfSize;
shun-iwasawa 82a8f5
  int bits_per_pixel, cmap_entries;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Compute colormap size and total file size */
shun-iwasawa 82a8f5
  if (IsExtRGB(cinfo->out_color_space)) {
shun-iwasawa 82a8f5
    if (cinfo->quantize_colors) {
shun-iwasawa 82a8f5
      /* Colormapped RGB */
shun-iwasawa 82a8f5
      bits_per_pixel = 8;
shun-iwasawa 82a8f5
      cmap_entries = 256;
shun-iwasawa 82a8f5
    } else {
shun-iwasawa 82a8f5
      /* Unquantized, full color RGB */
shun-iwasawa 82a8f5
      bits_per_pixel = 24;
shun-iwasawa 82a8f5
      cmap_entries = 0;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  } else if (cinfo->out_color_space == JCS_RGB565 ||
shun-iwasawa 82a8f5
             cinfo->out_color_space == JCS_CMYK) {
shun-iwasawa 82a8f5
    bits_per_pixel = 24;
shun-iwasawa 82a8f5
    cmap_entries   = 0;
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    /* Grayscale output.  We need to fake a 256-entry colormap. */
shun-iwasawa 82a8f5
    bits_per_pixel = 8;
shun-iwasawa 82a8f5
    cmap_entries = 256;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  /* File size */
shun-iwasawa 82a8f5
  headersize = 14 + 40 + cmap_entries * 4; /* Header and colormap */
shun-iwasawa 82a8f5
  bfSize = headersize + (long)dest->row_width * (long)cinfo->output_height;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Set unused fields of header to 0 */
shun-iwasawa 82a8f5
  MEMZERO(bmpfileheader, sizeof(bmpfileheader));
shun-iwasawa 82a8f5
  MEMZERO(bmpinfoheader, sizeof(bmpinfoheader));
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Fill the file header */
shun-iwasawa 82a8f5
  bmpfileheader[0] = 0x42;      /* first 2 bytes are ASCII 'B', 'M' */
shun-iwasawa 82a8f5
  bmpfileheader[1] = 0x4D;
shun-iwasawa 82a8f5
  PUT_4B(bmpfileheader, 2, bfSize); /* bfSize */
shun-iwasawa 82a8f5
  /* we leave bfReserved1 & bfReserved2 = 0 */
shun-iwasawa 82a8f5
  PUT_4B(bmpfileheader, 10, headersize); /* bfOffBits */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Fill the info header (Microsoft calls this a BITMAPINFOHEADER) */
shun-iwasawa 82a8f5
  PUT_2B(bmpinfoheader, 0, 40); /* biSize */
shun-iwasawa 82a8f5
  PUT_4B(bmpinfoheader, 4, cinfo->output_width); /* biWidth */
shun-iwasawa 82a8f5
  PUT_4B(bmpinfoheader, 8, cinfo->output_height); /* biHeight */
shun-iwasawa 82a8f5
  PUT_2B(bmpinfoheader, 12, 1); /* biPlanes - must be 1 */
shun-iwasawa 82a8f5
  PUT_2B(bmpinfoheader, 14, bits_per_pixel); /* biBitCount */
shun-iwasawa 82a8f5
  /* we leave biCompression = 0, for none */
shun-iwasawa 82a8f5
  /* we leave biSizeImage = 0; this is correct for uncompressed data */
shun-iwasawa 82a8f5
  if (cinfo->density_unit == 2) { /* if have density in dots/cm, then */
shun-iwasawa 82a8f5
    PUT_4B(bmpinfoheader, 24, (long)(cinfo->X_density * 100)); /* XPels/M */
shun-iwasawa 82a8f5
    PUT_4B(bmpinfoheader, 28, (long)(cinfo->Y_density * 100)); /* XPels/M */
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  PUT_2B(bmpinfoheader, 32, cmap_entries); /* biClrUsed */
shun-iwasawa 82a8f5
  /* we leave biClrImportant = 0 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (JFWRITE(dest->pub.output_file, bmpfileheader, 14) != (size_t)14)
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_FILE_WRITE);
shun-iwasawa 82a8f5
  if (JFWRITE(dest->pub.output_file, bmpinfoheader, 40) != (size_t)40)
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_FILE_WRITE);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (cmap_entries > 0)
shun-iwasawa 82a8f5
    write_colormap(cinfo, dest, cmap_entries, 4);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
LOCAL(void)
shun-iwasawa 82a8f5
write_os2_header(j_decompress_ptr cinfo, bmp_dest_ptr dest)
shun-iwasawa 82a8f5
/* Write an OS2-style BMP file header, including colormap if needed */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  char bmpfileheader[14];
shun-iwasawa 82a8f5
  char bmpcoreheader[12];
shun-iwasawa 82a8f5
  long headersize, bfSize;
shun-iwasawa 82a8f5
  int bits_per_pixel, cmap_entries;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Compute colormap size and total file size */
shun-iwasawa 82a8f5
  if (IsExtRGB(cinfo->out_color_space)) {
shun-iwasawa 82a8f5
    if (cinfo->quantize_colors) {
shun-iwasawa 82a8f5
      /* Colormapped RGB */
shun-iwasawa 82a8f5
      bits_per_pixel = 8;
shun-iwasawa 82a8f5
      cmap_entries = 256;
shun-iwasawa 82a8f5
    } else {
shun-iwasawa 82a8f5
      /* Unquantized, full color RGB */
shun-iwasawa 82a8f5
      bits_per_pixel = 24;
shun-iwasawa 82a8f5
      cmap_entries = 0;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  } else if (cinfo->out_color_space == JCS_RGB565 ||
shun-iwasawa 82a8f5
             cinfo->out_color_space == JCS_CMYK) {
shun-iwasawa 82a8f5
    bits_per_pixel = 24;
shun-iwasawa 82a8f5
    cmap_entries   = 0;
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    /* Grayscale output.  We need to fake a 256-entry colormap. */
shun-iwasawa 82a8f5
    bits_per_pixel = 8;
shun-iwasawa 82a8f5
    cmap_entries = 256;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  /* File size */
shun-iwasawa 82a8f5
  headersize = 14 + 12 + cmap_entries * 3; /* Header and colormap */
shun-iwasawa 82a8f5
  bfSize = headersize + (long)dest->row_width * (long)cinfo->output_height;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Set unused fields of header to 0 */
shun-iwasawa 82a8f5
  MEMZERO(bmpfileheader, sizeof(bmpfileheader));
shun-iwasawa 82a8f5
  MEMZERO(bmpcoreheader, sizeof(bmpcoreheader));
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Fill the file header */
shun-iwasawa 82a8f5
  bmpfileheader[0] = 0x42;      /* first 2 bytes are ASCII 'B', 'M' */
shun-iwasawa 82a8f5
  bmpfileheader[1] = 0x4D;
shun-iwasawa 82a8f5
  PUT_4B(bmpfileheader, 2, bfSize); /* bfSize */
shun-iwasawa 82a8f5
  /* we leave bfReserved1 & bfReserved2 = 0 */
shun-iwasawa 82a8f5
  PUT_4B(bmpfileheader, 10, headersize); /* bfOffBits */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Fill the info header (Microsoft calls this a BITMAPCOREHEADER) */
shun-iwasawa 82a8f5
  PUT_2B(bmpcoreheader, 0, 12); /* bcSize */
shun-iwasawa 82a8f5
  PUT_2B(bmpcoreheader, 4, cinfo->output_width); /* bcWidth */
shun-iwasawa 82a8f5
  PUT_2B(bmpcoreheader, 6, cinfo->output_height); /* bcHeight */
shun-iwasawa 82a8f5
  PUT_2B(bmpcoreheader, 8, 1);  /* bcPlanes - must be 1 */
shun-iwasawa 82a8f5
  PUT_2B(bmpcoreheader, 10, bits_per_pixel); /* bcBitCount */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (JFWRITE(dest->pub.output_file, bmpfileheader, 14) != (size_t)14)
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_FILE_WRITE);
shun-iwasawa 82a8f5
  if (JFWRITE(dest->pub.output_file, bmpcoreheader, 12) != (size_t)12)
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_FILE_WRITE);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (cmap_entries > 0)
shun-iwasawa 82a8f5
    write_colormap(cinfo, dest, cmap_entries, 3);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Write the colormap.
shun-iwasawa 82a8f5
 * Windows uses BGR0 map entries; OS/2 uses BGR entries.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
LOCAL(void)
shun-iwasawa 82a8f5
write_colormap(j_decompress_ptr cinfo, bmp_dest_ptr dest, int map_colors,
shun-iwasawa 82a8f5
               int map_entry_size)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  JSAMPARRAY colormap = cinfo->colormap;
shun-iwasawa 82a8f5
  int num_colors = cinfo->actual_number_of_colors;
shun-iwasawa 82a8f5
  FILE *outfile = dest->pub.output_file;
shun-iwasawa 82a8f5
  int i;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (colormap != NULL) {
shun-iwasawa 82a8f5
    if (cinfo->out_color_components == 3) {
shun-iwasawa 82a8f5
      /* Normal case with RGB colormap */
shun-iwasawa 82a8f5
      for (i = 0; i < num_colors; i++) {
shun-iwasawa 82a8f5
        putc(GETJSAMPLE(colormap[2][i]), outfile);
shun-iwasawa 82a8f5
        putc(GETJSAMPLE(colormap[1][i]), outfile);
shun-iwasawa 82a8f5
        putc(GETJSAMPLE(colormap[0][i]), outfile);
shun-iwasawa 82a8f5
        if (map_entry_size == 4)
shun-iwasawa 82a8f5
          putc(0, outfile);
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    } else {
shun-iwasawa 82a8f5
      /* Grayscale colormap (only happens with grayscale quantization) */
shun-iwasawa 82a8f5
      for (i = 0; i < num_colors; i++) {
shun-iwasawa 82a8f5
        putc(GETJSAMPLE(colormap[0][i]), outfile);
shun-iwasawa 82a8f5
        putc(GETJSAMPLE(colormap[0][i]), outfile);
shun-iwasawa 82a8f5
        putc(GETJSAMPLE(colormap[0][i]), outfile);
shun-iwasawa 82a8f5
        if (map_entry_size == 4)
shun-iwasawa 82a8f5
          putc(0, outfile);
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    /* If no colormap, must be grayscale data.  Generate a linear "map". */
shun-iwasawa 82a8f5
    for (i = 0; i < 256; i++) {
shun-iwasawa 82a8f5
      putc(i, outfile);
shun-iwasawa 82a8f5
      putc(i, outfile);
shun-iwasawa 82a8f5
      putc(i, outfile);
shun-iwasawa 82a8f5
      if (map_entry_size == 4)
shun-iwasawa 82a8f5
        putc(0, outfile);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  /* Pad colormap with zeros to ensure specified number of colormap entries */
shun-iwasawa 82a8f5
  if (i > map_colors)
shun-iwasawa 82a8f5
    ERREXIT1(cinfo, JERR_TOO_MANY_COLORS, i);
shun-iwasawa 82a8f5
  for (; i < map_colors; i++) {
shun-iwasawa 82a8f5
    putc(0, outfile);
shun-iwasawa 82a8f5
    putc(0, outfile);
shun-iwasawa 82a8f5
    putc(0, outfile);
shun-iwasawa 82a8f5
    if (map_entry_size == 4)
shun-iwasawa 82a8f5
      putc(0, outfile);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Startup: write the file header unless the inversion array is being used.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
start_output_bmp(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  bmp_dest_ptr dest = (bmp_dest_ptr)dinfo;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (!dest->use_inversion_array) {
shun-iwasawa 82a8f5
    /* Write the header and colormap */
shun-iwasawa 82a8f5
    if (dest->is_os2)
shun-iwasawa 82a8f5
      write_os2_header(cinfo, dest);
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      write_bmp_header(cinfo, dest);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
finish_output_bmp(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  bmp_dest_ptr dest = (bmp_dest_ptr)dinfo;
shun-iwasawa 82a8f5
  register FILE *outfile = dest->pub.output_file;
shun-iwasawa 82a8f5
  JSAMPARRAY image_ptr;
shun-iwasawa 82a8f5
  register JSAMPROW data_ptr;
shun-iwasawa 82a8f5
  JDIMENSION row;
shun-iwasawa 82a8f5
  register JDIMENSION col;
shun-iwasawa 82a8f5
  cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (dest->use_inversion_array) {
shun-iwasawa 82a8f5
    /* Write the header and colormap */
shun-iwasawa 82a8f5
    if (dest->is_os2)
shun-iwasawa 82a8f5
      write_os2_header(cinfo, dest);
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      write_bmp_header(cinfo, dest);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    /* Write the file body from our virtual array */
shun-iwasawa 82a8f5
    for (row = cinfo->output_height; row > 0; row--) {
shun-iwasawa 82a8f5
      if (progress != NULL) {
shun-iwasawa 82a8f5
        progress->pub.pass_counter = (long)(cinfo->output_height - row);
shun-iwasawa 82a8f5
        progress->pub.pass_limit = (long)cinfo->output_height;
shun-iwasawa 82a8f5
        (*progress->pub.progress_monitor) ((j_common_ptr)cinfo);
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
      image_ptr = (*cinfo->mem->access_virt_sarray)
shun-iwasawa 82a8f5
        ((j_common_ptr)cinfo, dest->whole_image, row - 1, (JDIMENSION)1,
shun-iwasawa 82a8f5
         FALSE);
shun-iwasawa 82a8f5
      data_ptr = image_ptr[0];
shun-iwasawa 82a8f5
      for (col = dest->row_width; col > 0; col--) {
shun-iwasawa 82a8f5
        putc(GETJSAMPLE(*data_ptr), outfile);
shun-iwasawa 82a8f5
        data_ptr++;
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    if (progress != NULL)
shun-iwasawa 82a8f5
      progress->completed_extra_passes++;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Make sure we wrote the output file OK */
shun-iwasawa 82a8f5
  fflush(outfile);
shun-iwasawa 82a8f5
  if (ferror(outfile))
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_FILE_WRITE);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * The module selection routine for BMP format output.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
GLOBAL(djpeg_dest_ptr)
shun-iwasawa 82a8f5
jinit_write_bmp(j_decompress_ptr cinfo, boolean is_os2,
shun-iwasawa 82a8f5
                boolean use_inversion_array)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  bmp_dest_ptr dest;
shun-iwasawa 82a8f5
  JDIMENSION row_width;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Create module interface object, fill in method pointers */
shun-iwasawa 82a8f5
  dest = (bmp_dest_ptr)
shun-iwasawa 82a8f5
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
                                sizeof(bmp_dest_struct));
shun-iwasawa 82a8f5
  dest->pub.start_output = start_output_bmp;
shun-iwasawa 82a8f5
  dest->pub.finish_output = finish_output_bmp;
shun-iwasawa 82a8f5
  dest->pub.calc_buffer_dimensions = NULL;
shun-iwasawa 82a8f5
  dest->is_os2 = is_os2;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (cinfo->out_color_space == JCS_GRAYSCALE) {
shun-iwasawa 82a8f5
    dest->pub.put_pixel_rows = put_gray_rows;
shun-iwasawa 82a8f5
  } else if (IsExtRGB(cinfo->out_color_space)) {
shun-iwasawa 82a8f5
    if (cinfo->quantize_colors)
shun-iwasawa 82a8f5
      dest->pub.put_pixel_rows = put_gray_rows;
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      dest->pub.put_pixel_rows = put_pixel_rows;
shun-iwasawa 82a8f5
  } else if (!cinfo->quantize_colors &&
shun-iwasawa 82a8f5
             (cinfo->out_color_space == JCS_RGB565 ||
shun-iwasawa 82a8f5
              cinfo->out_color_space == JCS_CMYK)) {
shun-iwasawa 82a8f5
    dest->pub.put_pixel_rows = put_pixel_rows;
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_BMP_COLORSPACE);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Calculate output image dimensions so we can allocate space */
shun-iwasawa 82a8f5
  jpeg_calc_output_dimensions(cinfo);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Determine width of rows in the BMP file (padded to 4-byte boundary). */
shun-iwasawa 82a8f5
  if (cinfo->out_color_space == JCS_RGB565) {
shun-iwasawa 82a8f5
    row_width = cinfo->output_width * 2;
shun-iwasawa 82a8f5
    dest->row_width = dest->data_width = cinfo->output_width * 3;
shun-iwasawa 82a8f5
    while ((row_width & 3) != 0) row_width++;
shun-iwasawa 82a8f5
  } else if (!cinfo->quantize_colors &&
shun-iwasawa 82a8f5
             (IsExtRGB(cinfo->out_color_space) ||
shun-iwasawa 82a8f5
              cinfo->out_color_space == JCS_CMYK)) {
shun-iwasawa 82a8f5
    row_width = cinfo->output_width * cinfo->output_components;
shun-iwasawa 82a8f5
    dest->row_width = dest->data_width = cinfo->output_width * 3;
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    row_width = cinfo->output_width * cinfo->output_components;
shun-iwasawa 82a8f5
    dest->row_width = dest->data_width = row_width;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  while ((dest->row_width & 3) != 0) dest->row_width++;
shun-iwasawa 82a8f5
  dest->pad_bytes = (int)(dest->row_width - dest->data_width);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (use_inversion_array) {
shun-iwasawa 82a8f5
    /* Allocate space for inversion array, prepare for write pass */
shun-iwasawa 82a8f5
    dest->whole_image = (*cinfo->mem->request_virt_sarray)
shun-iwasawa 82a8f5
      ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
shun-iwasawa 82a8f5
       dest->row_width, cinfo->output_height, (JDIMENSION)1);
shun-iwasawa 82a8f5
    dest->cur_output_row = 0;
shun-iwasawa 82a8f5
    if (cinfo->progress != NULL) {
shun-iwasawa 82a8f5
      cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress;
shun-iwasawa 82a8f5
      progress->total_extra_passes++; /* count file input as separate pass */
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    dest->iobuffer = (JSAMPLE *)(*cinfo->mem->alloc_small)
shun-iwasawa 82a8f5
      ((j_common_ptr)cinfo, JPOOL_IMAGE, dest->row_width);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  dest->use_inversion_array = use_inversion_array;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Create decompressor output buffer. */
shun-iwasawa 82a8f5
  dest->pub.buffer = (*cinfo->mem->alloc_sarray)
shun-iwasawa 82a8f5
    ((j_common_ptr)cinfo, JPOOL_IMAGE, row_width, (JDIMENSION)1);
shun-iwasawa 82a8f5
  dest->pub.buffer_height = 1;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  return (djpeg_dest_ptr)dest;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#endif /* BMP_SUPPORTED */