shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * rdbmp.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
 * Modified 2009-2017 by Guido Vollbeding.
shun-iwasawa 82a8f5
 * libjpeg-turbo Modifications:
shun-iwasawa 82a8f5
 * Modified 2011 by Siarhei Siamashka.
shun-iwasawa 82a8f5
 * Copyright (C) 2015, 2017-2018, 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 read input images in Microsoft "BMP"
shun-iwasawa 82a8f5
 * format (MS Windows 3.x, OS/2 1.x, and OS/2 2.x flavors).
shun-iwasawa 82a8f5
 * Currently, only 8-bit and 24-bit images are supported, not 1-bit or
shun-iwasawa 82a8f5
 * 4-bit (feeding such low-depth images into JPEG would be silly anyway).
shun-iwasawa 82a8f5
 * Also, we don't support RLE-compressed files.
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 input from
shun-iwasawa 82a8f5
 * an ordinary stdio stream.  They further assume that reading begins
shun-iwasawa 82a8f5
 * at the start of the file; start_input may need work if the
shun-iwasawa 82a8f5
 * user interface has already read some data (e.g., to determine that
shun-iwasawa 82a8f5
 * the file is indeed BMP format).
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
shun-iwasawa 82a8f5
#ifdef BMP_SUPPORTED
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Macros to deal with unsigned chars as efficiently as compiler allows */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#ifdef HAVE_UNSIGNED_CHAR
shun-iwasawa 82a8f5
typedef unsigned char U_CHAR;
shun-iwasawa 82a8f5
#define UCH(x)  ((int)(x))
shun-iwasawa 82a8f5
#else /* !HAVE_UNSIGNED_CHAR */
shun-iwasawa 82a8f5
#ifdef __CHAR_UNSIGNED__
shun-iwasawa 82a8f5
typedef char U_CHAR;
shun-iwasawa 82a8f5
#define UCH(x)  ((int)(x))
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
typedef char U_CHAR;
shun-iwasawa 82a8f5
#define UCH(x)  ((int)(x) & 0xFF)
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
#endif /* HAVE_UNSIGNED_CHAR */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define ReadOK(file, buffer, len) \
shun-iwasawa 82a8f5
  (JFREAD(file, buffer, len) == ((size_t)(len)))
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
static int alpha_index[JPEG_NUMCS] = {
shun-iwasawa 82a8f5
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1
shun-iwasawa 82a8f5
};
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Private version of data source object */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
typedef struct _bmp_source_struct *bmp_source_ptr;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
typedef struct _bmp_source_struct {
shun-iwasawa 82a8f5
  struct cjpeg_source_struct pub; /* public fields */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  j_compress_ptr cinfo;         /* back link saves passing separate parm */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  JSAMPARRAY colormap;          /* BMP colormap (converted to my format) */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  jvirt_sarray_ptr whole_image; /* Needed to reverse row order */
shun-iwasawa 82a8f5
  JDIMENSION source_row;        /* Current source row number */
shun-iwasawa 82a8f5
  JDIMENSION row_width;         /* Physical width of scanlines in file */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  int bits_per_pixel;           /* remembers 8- or 24-bit format */
shun-iwasawa 82a8f5
  int cmap_length;              /* colormap length */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  boolean use_inversion_array;  /* TRUE = preload the whole image, which is
shun-iwasawa 82a8f5
                                   stored in bottom-up order, and feed it to
shun-iwasawa 82a8f5
                                   the calling program in top-down order
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
                                   FALSE = the calling program will maintain
shun-iwasawa 82a8f5
                                   its own image buffer and read the rows in
shun-iwasawa 82a8f5
                                   bottom-up order */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  U_CHAR *iobuffer;             /* I/O buffer (used to buffer a single row from
shun-iwasawa 82a8f5
                                   disk if use_inversion_array == FALSE) */
shun-iwasawa 82a8f5
} bmp_source_struct;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
LOCAL(int)
shun-iwasawa 82a8f5
read_byte(bmp_source_ptr sinfo)
shun-iwasawa 82a8f5
/* Read next byte from BMP file */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  register FILE *infile = sinfo->pub.input_file;
shun-iwasawa 82a8f5
  register int c;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if ((c = getc(infile)) == EOF)
shun-iwasawa 82a8f5
    ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
shun-iwasawa 82a8f5
  return c;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
LOCAL(void)
shun-iwasawa 82a8f5
read_colormap(bmp_source_ptr sinfo, int cmaplen, int mapentrysize)
shun-iwasawa 82a8f5
/* Read the colormap from a BMP file */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  int i, gray = 1;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  switch (mapentrysize) {
shun-iwasawa 82a8f5
  case 3:
shun-iwasawa 82a8f5
    /* BGR format (occurs in OS/2 files) */
shun-iwasawa 82a8f5
    for (i = 0; i < cmaplen; i++) {
shun-iwasawa 82a8f5
      sinfo->colormap[2][i] = (JSAMPLE)read_byte(sinfo);
shun-iwasawa 82a8f5
      sinfo->colormap[1][i] = (JSAMPLE)read_byte(sinfo);
shun-iwasawa 82a8f5
      sinfo->colormap[0][i] = (JSAMPLE)read_byte(sinfo);
shun-iwasawa 82a8f5
      if (sinfo->colormap[2][i] != sinfo->colormap[1][i] ||
shun-iwasawa 82a8f5
          sinfo->colormap[1][i] != sinfo->colormap[0][i])
shun-iwasawa 82a8f5
        gray = 0;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case 4:
shun-iwasawa 82a8f5
    /* BGR0 format (occurs in MS Windows files) */
shun-iwasawa 82a8f5
    for (i = 0; i < cmaplen; i++) {
shun-iwasawa 82a8f5
      sinfo->colormap[2][i] = (JSAMPLE)read_byte(sinfo);
shun-iwasawa 82a8f5
      sinfo->colormap[1][i] = (JSAMPLE)read_byte(sinfo);
shun-iwasawa 82a8f5
      sinfo->colormap[0][i] = (JSAMPLE)read_byte(sinfo);
shun-iwasawa 82a8f5
      (void)read_byte(sinfo);
shun-iwasawa 82a8f5
      if (sinfo->colormap[2][i] != sinfo->colormap[1][i] ||
shun-iwasawa 82a8f5
          sinfo->colormap[1][i] != sinfo->colormap[0][i])
shun-iwasawa 82a8f5
        gray = 0;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  default:
shun-iwasawa 82a8f5
    ERREXIT(sinfo->cinfo, JERR_BMP_BADCMAP);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (sinfo->cinfo->in_color_space == JCS_UNKNOWN && gray)
shun-iwasawa 82a8f5
    sinfo->cinfo->in_color_space = JCS_GRAYSCALE;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (sinfo->cinfo->in_color_space == JCS_GRAYSCALE && !gray)
shun-iwasawa 82a8f5
    ERREXIT(sinfo->cinfo, JERR_BAD_IN_COLORSPACE);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Read one row of pixels.
shun-iwasawa 82a8f5
 * The image has been read into the whole_image array, but is otherwise
shun-iwasawa 82a8f5
 * unprocessed.  We must read it out in top-to-bottom row order, and if
shun-iwasawa 82a8f5
 * it is an 8-bit image, we must expand colormapped pixels to 24bit format.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(JDIMENSION)
shun-iwasawa 82a8f5
get_8bit_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shun-iwasawa 82a8f5
/* This version is for reading 8-bit colormap indexes */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  bmp_source_ptr source = (bmp_source_ptr)sinfo;
shun-iwasawa 82a8f5
  register JSAMPARRAY colormap = source->colormap;
shun-iwasawa 82a8f5
  int cmaplen = source->cmap_length;
shun-iwasawa 82a8f5
  JSAMPARRAY image_ptr;
shun-iwasawa 82a8f5
  register int t;
shun-iwasawa 82a8f5
  register JSAMPROW inptr, outptr;
shun-iwasawa 82a8f5
  register JDIMENSION col;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (source->use_inversion_array) {
shun-iwasawa 82a8f5
    /* Fetch next row from virtual array */
shun-iwasawa 82a8f5
    source->source_row--;
shun-iwasawa 82a8f5
    image_ptr = (*cinfo->mem->access_virt_sarray)
shun-iwasawa 82a8f5
      ((j_common_ptr)cinfo, source->whole_image,
shun-iwasawa 82a8f5
       source->source_row, (JDIMENSION)1, FALSE);
shun-iwasawa 82a8f5
    inptr = image_ptr[0];
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    if (!ReadOK(source->pub.input_file, source->iobuffer, source->row_width))
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_INPUT_EOF);
shun-iwasawa 82a8f5
    inptr = source->iobuffer;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Expand the colormap indexes to real data */
shun-iwasawa 82a8f5
  outptr = source->pub.buffer[0];
shun-iwasawa 82a8f5
  if (cinfo->in_color_space == JCS_GRAYSCALE) {
shun-iwasawa 82a8f5
    for (col = cinfo->image_width; col > 0; col--) {
shun-iwasawa 82a8f5
      t = GETJSAMPLE(*inptr++);
shun-iwasawa 82a8f5
      if (t >= cmaplen)
shun-iwasawa 82a8f5
        ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
shun-iwasawa 82a8f5
      *outptr++ = colormap[0][t];
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  } else if (cinfo->in_color_space == JCS_CMYK) {
shun-iwasawa 82a8f5
    for (col = cinfo->image_width; col > 0; col--) {
shun-iwasawa 82a8f5
      t = GETJSAMPLE(*inptr++);
shun-iwasawa 82a8f5
      if (t >= cmaplen)
shun-iwasawa 82a8f5
        ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
shun-iwasawa 82a8f5
      rgb_to_cmyk(colormap[0][t], colormap[1][t], colormap[2][t], outptr,
shun-iwasawa 82a8f5
                  outptr + 1, outptr + 2, outptr + 3);
shun-iwasawa 82a8f5
      outptr += 4;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    register int rindex = rgb_red[cinfo->in_color_space];
shun-iwasawa 82a8f5
    register int gindex = rgb_green[cinfo->in_color_space];
shun-iwasawa 82a8f5
    register int bindex = rgb_blue[cinfo->in_color_space];
shun-iwasawa 82a8f5
    register int aindex = alpha_index[cinfo->in_color_space];
shun-iwasawa 82a8f5
    register int ps = rgb_pixelsize[cinfo->in_color_space];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (aindex >= 0) {
shun-iwasawa 82a8f5
      for (col = cinfo->image_width; col > 0; col--) {
shun-iwasawa 82a8f5
        t = GETJSAMPLE(*inptr++);
shun-iwasawa 82a8f5
        if (t >= cmaplen)
shun-iwasawa 82a8f5
          ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
shun-iwasawa 82a8f5
        outptr[rindex] = colormap[0][t];
shun-iwasawa 82a8f5
        outptr[gindex] = colormap[1][t];
shun-iwasawa 82a8f5
        outptr[bindex] = colormap[2][t];
shun-iwasawa 82a8f5
        outptr[aindex] = 0xFF;
shun-iwasawa 82a8f5
        outptr += ps;
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    } else {
shun-iwasawa 82a8f5
      for (col = cinfo->image_width; col > 0; col--) {
shun-iwasawa 82a8f5
        t = GETJSAMPLE(*inptr++);
shun-iwasawa 82a8f5
        if (t >= cmaplen)
shun-iwasawa 82a8f5
          ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
shun-iwasawa 82a8f5
        outptr[rindex] = colormap[0][t];
shun-iwasawa 82a8f5
        outptr[gindex] = colormap[1][t];
shun-iwasawa 82a8f5
        outptr[bindex] = colormap[2][t];
shun-iwasawa 82a8f5
        outptr += ps;
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  return 1;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(JDIMENSION)
shun-iwasawa 82a8f5
get_24bit_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shun-iwasawa 82a8f5
/* This version is for reading 24-bit pixels */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  bmp_source_ptr source = (bmp_source_ptr)sinfo;
shun-iwasawa 82a8f5
  JSAMPARRAY image_ptr;
shun-iwasawa 82a8f5
  register JSAMPROW inptr, outptr;
shun-iwasawa 82a8f5
  register JDIMENSION col;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (source->use_inversion_array) {
shun-iwasawa 82a8f5
    /* Fetch next row from virtual array */
shun-iwasawa 82a8f5
    source->source_row--;
shun-iwasawa 82a8f5
    image_ptr = (*cinfo->mem->access_virt_sarray)
shun-iwasawa 82a8f5
      ((j_common_ptr)cinfo, source->whole_image,
shun-iwasawa 82a8f5
       source->source_row, (JDIMENSION)1, FALSE);
shun-iwasawa 82a8f5
    inptr = image_ptr[0];
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    if (!ReadOK(source->pub.input_file, source->iobuffer, source->row_width))
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_INPUT_EOF);
shun-iwasawa 82a8f5
    inptr = source->iobuffer;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Transfer data.  Note source values are in BGR order
shun-iwasawa 82a8f5
   * (even though Microsoft's own documents say the opposite).
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
  outptr = source->pub.buffer[0];
shun-iwasawa 82a8f5
  if (cinfo->in_color_space == JCS_EXT_BGR) {
shun-iwasawa 82a8f5
    MEMCOPY(outptr, inptr, source->row_width);
shun-iwasawa 82a8f5
  } else if (cinfo->in_color_space == JCS_CMYK) {
shun-iwasawa 82a8f5
    for (col = cinfo->image_width; col > 0; col--) {
shun-iwasawa 82a8f5
      /* can omit GETJSAMPLE() safely */
shun-iwasawa 82a8f5
      JSAMPLE b = *inptr++, g = *inptr++, r = *inptr++;
shun-iwasawa 82a8f5
      rgb_to_cmyk(r, g, b, outptr, outptr + 1, outptr + 2, outptr + 3);
shun-iwasawa 82a8f5
      outptr += 4;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    register int rindex = rgb_red[cinfo->in_color_space];
shun-iwasawa 82a8f5
    register int gindex = rgb_green[cinfo->in_color_space];
shun-iwasawa 82a8f5
    register int bindex = rgb_blue[cinfo->in_color_space];
shun-iwasawa 82a8f5
    register int aindex = alpha_index[cinfo->in_color_space];
shun-iwasawa 82a8f5
    register int ps = rgb_pixelsize[cinfo->in_color_space];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (aindex >= 0) {
shun-iwasawa 82a8f5
      for (col = cinfo->image_width; col > 0; col--) {
shun-iwasawa 82a8f5
        outptr[bindex] = *inptr++;      /* can omit GETJSAMPLE() safely */
shun-iwasawa 82a8f5
        outptr[gindex] = *inptr++;
shun-iwasawa 82a8f5
        outptr[rindex] = *inptr++;
shun-iwasawa 82a8f5
        outptr[aindex] = 0xFF;
shun-iwasawa 82a8f5
        outptr += ps;
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    } else {
shun-iwasawa 82a8f5
      for (col = cinfo->image_width; col > 0; col--) {
shun-iwasawa 82a8f5
        outptr[bindex] = *inptr++;      /* can omit GETJSAMPLE() safely */
shun-iwasawa 82a8f5
        outptr[gindex] = *inptr++;
shun-iwasawa 82a8f5
        outptr[rindex] = *inptr++;
shun-iwasawa 82a8f5
        outptr += ps;
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  return 1;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(JDIMENSION)
shun-iwasawa 82a8f5
get_32bit_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shun-iwasawa 82a8f5
/* This version is for reading 32-bit pixels */
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  bmp_source_ptr source = (bmp_source_ptr)sinfo;
shun-iwasawa 82a8f5
  JSAMPARRAY image_ptr;
shun-iwasawa 82a8f5
  register JSAMPROW inptr, outptr;
shun-iwasawa 82a8f5
  register JDIMENSION col;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (source->use_inversion_array) {
shun-iwasawa 82a8f5
    /* Fetch next row from virtual array */
shun-iwasawa 82a8f5
    source->source_row--;
shun-iwasawa 82a8f5
    image_ptr = (*cinfo->mem->access_virt_sarray)
shun-iwasawa 82a8f5
      ((j_common_ptr)cinfo, source->whole_image,
shun-iwasawa 82a8f5
       source->source_row, (JDIMENSION)1, FALSE);
shun-iwasawa 82a8f5
    inptr = image_ptr[0];
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    if (!ReadOK(source->pub.input_file, source->iobuffer, source->row_width))
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_INPUT_EOF);
shun-iwasawa 82a8f5
    inptr = source->iobuffer;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Transfer data.  Note source values are in BGR order
shun-iwasawa 82a8f5
   * (even though Microsoft's own documents say the opposite).
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
  outptr = source->pub.buffer[0];
shun-iwasawa 82a8f5
  if (cinfo->in_color_space == JCS_EXT_BGRX ||
shun-iwasawa 82a8f5
      cinfo->in_color_space == JCS_EXT_BGRA) {
shun-iwasawa 82a8f5
    MEMCOPY(outptr, inptr, source->row_width);
shun-iwasawa 82a8f5
  } else if (cinfo->in_color_space == JCS_CMYK) {
shun-iwasawa 82a8f5
    for (col = cinfo->image_width; col > 0; col--) {
shun-iwasawa 82a8f5
      /* can omit GETJSAMPLE() safely */
shun-iwasawa 82a8f5
      JSAMPLE b = *inptr++, g = *inptr++, r = *inptr++;
shun-iwasawa 82a8f5
      rgb_to_cmyk(r, g, b, outptr, outptr + 1, outptr + 2, outptr + 3);
shun-iwasawa 82a8f5
      inptr++;                          /* skip the 4th byte (Alpha channel) */
shun-iwasawa 82a8f5
      outptr += 4;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  } else {
shun-iwasawa 82a8f5
    register int rindex = rgb_red[cinfo->in_color_space];
shun-iwasawa 82a8f5
    register int gindex = rgb_green[cinfo->in_color_space];
shun-iwasawa 82a8f5
    register int bindex = rgb_blue[cinfo->in_color_space];
shun-iwasawa 82a8f5
    register int aindex = alpha_index[cinfo->in_color_space];
shun-iwasawa 82a8f5
    register int ps = rgb_pixelsize[cinfo->in_color_space];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (aindex >= 0) {
shun-iwasawa 82a8f5
      for (col = cinfo->image_width; col > 0; col--) {
shun-iwasawa 82a8f5
        outptr[bindex] = *inptr++;      /* can omit GETJSAMPLE() safely */
shun-iwasawa 82a8f5
        outptr[gindex] = *inptr++;
shun-iwasawa 82a8f5
        outptr[rindex] = *inptr++;
shun-iwasawa 82a8f5
        outptr[aindex] = *inptr++;
shun-iwasawa 82a8f5
        outptr += ps;
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    } else {
shun-iwasawa 82a8f5
      for (col = cinfo->image_width; col > 0; col--) {
shun-iwasawa 82a8f5
        outptr[bindex] = *inptr++;      /* can omit GETJSAMPLE() safely */
shun-iwasawa 82a8f5
        outptr[gindex] = *inptr++;
shun-iwasawa 82a8f5
        outptr[rindex] = *inptr++;
shun-iwasawa 82a8f5
        inptr++;                        /* skip the 4th byte (Alpha channel) */
shun-iwasawa 82a8f5
        outptr += ps;
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  return 1;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * This method loads the image into whole_image during the first call on
shun-iwasawa 82a8f5
 * get_pixel_rows.  The get_pixel_rows pointer is then adjusted to call
shun-iwasawa 82a8f5
 * get_8bit_row, get_24bit_row, or get_32bit_row on subsequent calls.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(JDIMENSION)
shun-iwasawa 82a8f5
preload_image(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  bmp_source_ptr source = (bmp_source_ptr)sinfo;
shun-iwasawa 82a8f5
  register FILE *infile = source->pub.input_file;
shun-iwasawa 82a8f5
  register JSAMPROW out_ptr;
shun-iwasawa 82a8f5
  JSAMPARRAY image_ptr;
shun-iwasawa 82a8f5
  JDIMENSION row;
shun-iwasawa 82a8f5
  cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Read the data into a virtual array in input-file row order. */
shun-iwasawa 82a8f5
  for (row = 0; row < cinfo->image_height; row++) {
shun-iwasawa 82a8f5
    if (progress != NULL) {
shun-iwasawa 82a8f5
      progress->pub.pass_counter = (long)row;
shun-iwasawa 82a8f5
      progress->pub.pass_limit = (long)cinfo->image_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, source->whole_image, row, (JDIMENSION)1, TRUE);
shun-iwasawa 82a8f5
    out_ptr = image_ptr[0];
shun-iwasawa 82a8f5
    if (fread(out_ptr, 1, source->row_width, infile) != source->row_width) {
shun-iwasawa 82a8f5
      if (feof(infile))
shun-iwasawa 82a8f5
        ERREXIT(cinfo, JERR_INPUT_EOF);
shun-iwasawa 82a8f5
      else
shun-iwasawa 82a8f5
        ERREXIT(cinfo, JERR_FILE_READ);
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
  /* Set up to read from the virtual array in top-to-bottom order */
shun-iwasawa 82a8f5
  switch (source->bits_per_pixel) {
shun-iwasawa 82a8f5
  case 8:
shun-iwasawa 82a8f5
    source->pub.get_pixel_rows = get_8bit_row;
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case 24:
shun-iwasawa 82a8f5
    source->pub.get_pixel_rows = get_24bit_row;
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case 32:
shun-iwasawa 82a8f5
    source->pub.get_pixel_rows = get_32bit_row;
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  default:
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_BMP_BADDEPTH);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  source->source_row = cinfo->image_height;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* And read the first row */
shun-iwasawa 82a8f5
  return (*source->pub.get_pixel_rows) (cinfo, sinfo);
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Read the file header; return image size and component count.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
start_input_bmp(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  bmp_source_ptr source = (bmp_source_ptr)sinfo;
shun-iwasawa 82a8f5
  U_CHAR bmpfileheader[14];
shun-iwasawa 82a8f5
  U_CHAR bmpinfoheader[64];
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define GET_2B(array, offset) \
shun-iwasawa 82a8f5
  ((unsigned short)UCH(array[offset]) + \
shun-iwasawa 82a8f5
   (((unsigned short)UCH(array[offset + 1])) << 8))
shun-iwasawa 82a8f5
#define GET_4B(array, offset) \
shun-iwasawa 82a8f5
  ((unsigned int)UCH(array[offset]) + \
shun-iwasawa 82a8f5
   (((unsigned int)UCH(array[offset + 1])) << 8) + \
shun-iwasawa 82a8f5
   (((unsigned int)UCH(array[offset + 2])) << 16) + \
shun-iwasawa 82a8f5
   (((unsigned int)UCH(array[offset + 3])) << 24))
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  unsigned int bfOffBits;
shun-iwasawa 82a8f5
  unsigned int headerSize;
shun-iwasawa 82a8f5
  int biWidth;
shun-iwasawa 82a8f5
  int biHeight;
shun-iwasawa 82a8f5
  unsigned short biPlanes;
shun-iwasawa 82a8f5
  unsigned int biCompression;
shun-iwasawa 82a8f5
  int biXPelsPerMeter, biYPelsPerMeter;
shun-iwasawa 82a8f5
  unsigned int biClrUsed = 0;
shun-iwasawa 82a8f5
  int mapentrysize = 0;         /* 0 indicates no colormap */
shun-iwasawa 82a8f5
  int bPad;
shun-iwasawa 82a8f5
  JDIMENSION row_width = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Read and verify the bitmap file header */
shun-iwasawa 82a8f5
  if (!ReadOK(source->pub.input_file, bmpfileheader, 14))
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_INPUT_EOF);
shun-iwasawa 82a8f5
  if (GET_2B(bmpfileheader, 0) != 0x4D42) /* 'BM' */
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_BMP_NOT);
shun-iwasawa 82a8f5
  bfOffBits = GET_4B(bmpfileheader, 10);
shun-iwasawa 82a8f5
  /* We ignore the remaining fileheader fields */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* The infoheader might be 12 bytes (OS/2 1.x), 40 bytes (Windows),
shun-iwasawa 82a8f5
   * or 64 bytes (OS/2 2.x).  Check the first 4 bytes to find out which.
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
  if (!ReadOK(source->pub.input_file, bmpinfoheader, 4))
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_INPUT_EOF);
shun-iwasawa 82a8f5
  headerSize = GET_4B(bmpinfoheader, 0);
shun-iwasawa 82a8f5
  if (headerSize < 12 || headerSize > 64)
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_BMP_BADHEADER);
shun-iwasawa 82a8f5
  if (!ReadOK(source->pub.input_file, bmpinfoheader + 4, headerSize - 4))
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_INPUT_EOF);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  switch (headerSize) {
shun-iwasawa 82a8f5
  case 12:
shun-iwasawa 82a8f5
    /* Decode OS/2 1.x header (Microsoft calls this a BITMAPCOREHEADER) */
shun-iwasawa 82a8f5
    biWidth = (int)GET_2B(bmpinfoheader, 4);
shun-iwasawa 82a8f5
    biHeight = (int)GET_2B(bmpinfoheader, 6);
shun-iwasawa 82a8f5
    biPlanes = GET_2B(bmpinfoheader, 8);
shun-iwasawa 82a8f5
    source->bits_per_pixel = (int)GET_2B(bmpinfoheader, 10);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    switch (source->bits_per_pixel) {
shun-iwasawa 82a8f5
    case 8:                     /* colormapped image */
shun-iwasawa 82a8f5
      mapentrysize = 3;         /* OS/2 uses RGBTRIPLE colormap */
shun-iwasawa 82a8f5
      TRACEMS2(cinfo, 1, JTRC_BMP_OS2_MAPPED, biWidth, biHeight);
shun-iwasawa 82a8f5
      break;
shun-iwasawa 82a8f5
    case 24:                    /* RGB image */
shun-iwasawa 82a8f5
      TRACEMS2(cinfo, 1, JTRC_BMP_OS2, biWidth, biHeight);
shun-iwasawa 82a8f5
      break;
shun-iwasawa 82a8f5
    default:
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_BMP_BADDEPTH);
shun-iwasawa 82a8f5
      break;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case 40:
shun-iwasawa 82a8f5
  case 64:
shun-iwasawa 82a8f5
    /* Decode Windows 3.x header (Microsoft calls this a BITMAPINFOHEADER) */
shun-iwasawa 82a8f5
    /* or OS/2 2.x header, which has additional fields that we ignore */
shun-iwasawa 82a8f5
    biWidth = (int)GET_4B(bmpinfoheader, 4);
shun-iwasawa 82a8f5
    biHeight = (int)GET_4B(bmpinfoheader, 8);
shun-iwasawa 82a8f5
    biPlanes = GET_2B(bmpinfoheader, 12);
shun-iwasawa 82a8f5
    source->bits_per_pixel = (int)GET_2B(bmpinfoheader, 14);
shun-iwasawa 82a8f5
    biCompression = GET_4B(bmpinfoheader, 16);
shun-iwasawa 82a8f5
    biXPelsPerMeter = (int)GET_4B(bmpinfoheader, 24);
shun-iwasawa 82a8f5
    biYPelsPerMeter = (int)GET_4B(bmpinfoheader, 28);
shun-iwasawa 82a8f5
    biClrUsed = GET_4B(bmpinfoheader, 32);
shun-iwasawa 82a8f5
    /* biSizeImage, biClrImportant fields are ignored */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    switch (source->bits_per_pixel) {
shun-iwasawa 82a8f5
    case 8:                     /* colormapped image */
shun-iwasawa 82a8f5
      mapentrysize = 4;         /* Windows uses RGBQUAD colormap */
shun-iwasawa 82a8f5
      TRACEMS2(cinfo, 1, JTRC_BMP_MAPPED, biWidth, biHeight);
shun-iwasawa 82a8f5
      break;
shun-iwasawa 82a8f5
    case 24:                    /* RGB image */
shun-iwasawa 82a8f5
      TRACEMS2(cinfo, 1, JTRC_BMP, biWidth, biHeight);
shun-iwasawa 82a8f5
      break;
shun-iwasawa 82a8f5
    case 32:                    /* RGB image + Alpha channel */
shun-iwasawa 82a8f5
      TRACEMS2(cinfo, 1, JTRC_BMP, biWidth, biHeight);
shun-iwasawa 82a8f5
      break;
shun-iwasawa 82a8f5
    default:
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_BMP_BADDEPTH);
shun-iwasawa 82a8f5
      break;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    if (biCompression != 0)
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_BMP_COMPRESSED);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
    if (biXPelsPerMeter > 0 && biYPelsPerMeter > 0) {
shun-iwasawa 82a8f5
      /* Set JFIF density parameters from the BMP data */
shun-iwasawa 82a8f5
      cinfo->X_density = (UINT16)(biXPelsPerMeter / 100); /* 100 cm per meter */
shun-iwasawa 82a8f5
      cinfo->Y_density = (UINT16)(biYPelsPerMeter / 100);
shun-iwasawa 82a8f5
      cinfo->density_unit = 2;  /* dots/cm */
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  default:
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_BMP_BADHEADER);
shun-iwasawa 82a8f5
    return;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (biWidth <= 0 || biHeight <= 0)
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_BMP_EMPTY);
shun-iwasawa 82a8f5
  if (biPlanes != 1)
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_BMP_BADPLANES);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Compute distance to bitmap data --- will adjust for colormap below */
shun-iwasawa 82a8f5
  bPad = bfOffBits - (headerSize + 14);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Read the colormap, if any */
shun-iwasawa 82a8f5
  if (mapentrysize > 0) {
shun-iwasawa 82a8f5
    if (biClrUsed <= 0)
shun-iwasawa 82a8f5
      biClrUsed = 256;          /* assume it's 256 */
shun-iwasawa 82a8f5
    else if (biClrUsed > 256)
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_BMP_BADCMAP);
shun-iwasawa 82a8f5
    /* Allocate space to store the colormap */
shun-iwasawa 82a8f5
    source->colormap = (*cinfo->mem->alloc_sarray)
shun-iwasawa 82a8f5
      ((j_common_ptr)cinfo, JPOOL_IMAGE, (JDIMENSION)biClrUsed, (JDIMENSION)3);
shun-iwasawa 82a8f5
    source->cmap_length = (int)biClrUsed;
shun-iwasawa 82a8f5
    /* and read it from the file */
shun-iwasawa 82a8f5
    read_colormap(source, (int)biClrUsed, mapentrysize);
shun-iwasawa 82a8f5
    /* account for size of colormap */
shun-iwasawa 82a8f5
    bPad -= biClrUsed * mapentrysize;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Skip any remaining pad bytes */
shun-iwasawa 82a8f5
  if (bPad < 0)                 /* incorrect bfOffBits value? */
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_BMP_BADHEADER);
shun-iwasawa 82a8f5
  while (--bPad >= 0) {
shun-iwasawa 82a8f5
    (void)read_byte(source);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Compute row width in file, including padding to 4-byte boundary */
shun-iwasawa 82a8f5
  switch (source->bits_per_pixel) {
shun-iwasawa 82a8f5
  case 8:
shun-iwasawa 82a8f5
    if (cinfo->in_color_space == JCS_UNKNOWN)
shun-iwasawa 82a8f5
      cinfo->in_color_space = JCS_EXT_RGB;
shun-iwasawa 82a8f5
    if (IsExtRGB(cinfo->in_color_space))
shun-iwasawa 82a8f5
      cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
shun-iwasawa 82a8f5
    else if (cinfo->in_color_space == JCS_GRAYSCALE)
shun-iwasawa 82a8f5
      cinfo->input_components = 1;
shun-iwasawa 82a8f5
    else if (cinfo->in_color_space == JCS_CMYK)
shun-iwasawa 82a8f5
      cinfo->input_components = 4;
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
shun-iwasawa 82a8f5
    row_width = (JDIMENSION)biWidth;
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case 24:
shun-iwasawa 82a8f5
    if (cinfo->in_color_space == JCS_UNKNOWN)
shun-iwasawa 82a8f5
      cinfo->in_color_space = JCS_EXT_BGR;
shun-iwasawa 82a8f5
    if (IsExtRGB(cinfo->in_color_space))
shun-iwasawa 82a8f5
      cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
shun-iwasawa 82a8f5
    else if (cinfo->in_color_space == JCS_CMYK)
shun-iwasawa 82a8f5
      cinfo->input_components = 4;
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
shun-iwasawa 82a8f5
    row_width = (JDIMENSION)(biWidth * 3);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  case 32:
shun-iwasawa 82a8f5
    if (cinfo->in_color_space == JCS_UNKNOWN)
shun-iwasawa 82a8f5
      cinfo->in_color_space = JCS_EXT_BGRA;
shun-iwasawa 82a8f5
    if (IsExtRGB(cinfo->in_color_space))
shun-iwasawa 82a8f5
      cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
shun-iwasawa 82a8f5
    else if (cinfo->in_color_space == JCS_CMYK)
shun-iwasawa 82a8f5
      cinfo->input_components = 4;
shun-iwasawa 82a8f5
    else
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
shun-iwasawa 82a8f5
    row_width = (JDIMENSION)(biWidth * 4);
shun-iwasawa 82a8f5
    break;
shun-iwasawa 82a8f5
  default:
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_BMP_BADDEPTH);
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
  while ((row_width & 3) != 0) row_width++;
shun-iwasawa 82a8f5
  source->row_width = row_width;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (source->use_inversion_array) {
shun-iwasawa 82a8f5
    /* Allocate space for inversion array, prepare for preload pass */
shun-iwasawa 82a8f5
    source->whole_image = (*cinfo->mem->request_virt_sarray)
shun-iwasawa 82a8f5
      ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
shun-iwasawa 82a8f5
       row_width, (JDIMENSION)biHeight, (JDIMENSION)1);
shun-iwasawa 82a8f5
    source->pub.get_pixel_rows = preload_image;
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
    source->iobuffer = (U_CHAR *)
shun-iwasawa 82a8f5
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, row_width);
shun-iwasawa 82a8f5
    switch (source->bits_per_pixel) {
shun-iwasawa 82a8f5
    case 8:
shun-iwasawa 82a8f5
      source->pub.get_pixel_rows = get_8bit_row;
shun-iwasawa 82a8f5
      break;
shun-iwasawa 82a8f5
    case 24:
shun-iwasawa 82a8f5
      source->pub.get_pixel_rows = get_24bit_row;
shun-iwasawa 82a8f5
      break;
shun-iwasawa 82a8f5
    case 32:
shun-iwasawa 82a8f5
      source->pub.get_pixel_rows = get_32bit_row;
shun-iwasawa 82a8f5
      break;
shun-iwasawa 82a8f5
    default:
shun-iwasawa 82a8f5
      ERREXIT(cinfo, JERR_BMP_BADDEPTH);
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Ensure that biWidth * cinfo->input_components doesn't exceed the maximum
shun-iwasawa 82a8f5
     value of the JDIMENSION type.  This is only a danger with BMP files, since
shun-iwasawa 82a8f5
     their width and height fields are 32-bit integers. */
shun-iwasawa 82a8f5
  if ((unsigned long long)biWidth *
shun-iwasawa 82a8f5
      (unsigned long long)cinfo->input_components > 0xFFFFFFFFULL)
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
shun-iwasawa 82a8f5
  /* Allocate one-row buffer for returned data */
shun-iwasawa 82a8f5
  source->pub.buffer = (*cinfo->mem->alloc_sarray)
shun-iwasawa 82a8f5
    ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
     (JDIMENSION)(biWidth * cinfo->input_components), (JDIMENSION)1);
shun-iwasawa 82a8f5
  source->pub.buffer_height = 1;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  cinfo->data_precision = 8;
shun-iwasawa 82a8f5
  cinfo->image_width = (JDIMENSION)biWidth;
shun-iwasawa 82a8f5
  cinfo->image_height = (JDIMENSION)biHeight;
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
shun-iwasawa 82a8f5
METHODDEF(void)
shun-iwasawa 82a8f5
finish_input_bmp(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  /* no work */
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * The module selection routine for BMP format input.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
GLOBAL(cjpeg_source_ptr)
shun-iwasawa 82a8f5
jinit_read_bmp(j_compress_ptr cinfo, boolean use_inversion_array)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  bmp_source_ptr source;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Create module interface object */
shun-iwasawa 82a8f5
  source = (bmp_source_ptr)
shun-iwasawa 82a8f5
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
shun-iwasawa 82a8f5
                                sizeof(bmp_source_struct));
shun-iwasawa 82a8f5
  source->cinfo = cinfo;        /* make back link for subroutines */
shun-iwasawa 82a8f5
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
shun-iwasawa 82a8f5
  source->pub.start_input = start_input_bmp;
shun-iwasawa 82a8f5
  source->pub.finish_input = finish_input_bmp;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  source->use_inversion_array = use_inversion_array;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  return (cjpeg_source_ptr)source;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#endif /* BMP_SUPPORTED */