kusano 7d535a
/*
kusano 7d535a
 * wrbmp.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1994-1996, Thomas G. Lane.
kusano 7d535a
 * This file is part of the Independent JPEG Group's software.
kusano 7d535a
 * For conditions of distribution and use, see the accompanying README file.
kusano 7d535a
 *
kusano 7d535a
 * This file contains routines to write output images in Microsoft "BMP"
kusano 7d535a
 * format (MS Windows 3.x and OS/2 1.x flavors).
kusano 7d535a
 * Either 8-bit colormapped or 24-bit full-color format can be written.
kusano 7d535a
 * No compression is supported.
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
 * This code contributed by James Arthur Boucher.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#include "cdjpeg.h"		/* Common decls for cjpeg/djpeg applications */
kusano 7d535a
kusano 7d535a
#ifdef BMP_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
 * Since BMP stores scanlines bottom-to-top, we have to invert the image
kusano 7d535a
 * from JPEG's top-to-bottom order.  To do this, we save the outgoing data
kusano 7d535a
 * in a virtual array during put_pixel_row calls, then actually emit the
kusano 7d535a
 * BMP file during finish_output.  The virtual array contains one JSAMPLE per
kusano 7d535a
 * pixel if the output is grayscale or colormapped, three if it is full color.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
/* Private version of data destination object */
kusano 7d535a
kusano 7d535a
typedef struct {
kusano 7d535a
  struct djpeg_dest_struct pub;	/* public fields */
kusano 7d535a
kusano 7d535a
  boolean is_os2;		/* saves the OS2 format request flag */
kusano 7d535a
kusano 7d535a
  jvirt_sarray_ptr whole_image;	/* needed to reverse row order */
kusano 7d535a
  JDIMENSION data_width;	/* JSAMPLEs per row */
kusano 7d535a
  JDIMENSION row_width;		/* physical width of one row in the BMP file */
kusano 7d535a
  int pad_bytes;		/* number of padding bytes needed per row */
kusano 7d535a
  JDIMENSION cur_output_row;	/* next row# to write to virtual array */
kusano 7d535a
} bmp_dest_struct;
kusano 7d535a
kusano 7d535a
typedef bmp_dest_struct * bmp_dest_ptr;
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Forward declarations */
kusano 7d535a
LOCAL(void) write_colormap
kusano 7d535a
	JPP((j_decompress_ptr cinfo, bmp_dest_ptr dest,
kusano 7d535a
	     int map_colors, int map_entry_size));
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)
kusano 7d535a
put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
kusano 7d535a
		JDIMENSION rows_supplied)
kusano 7d535a
/* This version is for writing 24-bit pixels */
kusano 7d535a
{
kusano 7d535a
  bmp_dest_ptr dest = (bmp_dest_ptr) dinfo;
kusano 7d535a
  JSAMPARRAY image_ptr;
kusano 7d535a
  register JSAMPROW inptr, outptr;
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
  int pad;
kusano 7d535a
kusano 7d535a
  /* Access next row in virtual array */
kusano 7d535a
  image_ptr = (*cinfo->mem->access_virt_sarray)
kusano 7d535a
    ((j_common_ptr) cinfo, dest->whole_image,
kusano 7d535a
     dest->cur_output_row, (JDIMENSION) 1, TRUE);
kusano 7d535a
  dest->cur_output_row++;
kusano 7d535a
kusano 7d535a
  /* Transfer data.  Note destination values must be in BGR order
kusano 7d535a
   * (even though Microsoft's own documents say the opposite).
kusano 7d535a
   */
kusano 7d535a
  inptr = dest->pub.buffer[0];
kusano 7d535a
  outptr = image_ptr[0];
kusano 7d535a
  for (col = cinfo->output_width; col > 0; col--) {
kusano 7d535a
    outptr[2] = *inptr++;	/* can omit GETJSAMPLE() safely */
kusano 7d535a
    outptr[1] = *inptr++;
kusano 7d535a
    outptr[0] = *inptr++;
kusano 7d535a
    outptr += 3;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Zero out the pad bytes. */
kusano 7d535a
  pad = dest->pad_bytes;
kusano 7d535a
  while (--pad >= 0)
kusano 7d535a
    *outptr++ = 0;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
put_gray_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
kusano 7d535a
	       JDIMENSION rows_supplied)
kusano 7d535a
/* This version is for grayscale OR quantized color output */
kusano 7d535a
{
kusano 7d535a
  bmp_dest_ptr dest = (bmp_dest_ptr) dinfo;
kusano 7d535a
  JSAMPARRAY image_ptr;
kusano 7d535a
  register JSAMPROW inptr, outptr;
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
  int pad;
kusano 7d535a
kusano 7d535a
  /* Access next row in virtual array */
kusano 7d535a
  image_ptr = (*cinfo->mem->access_virt_sarray)
kusano 7d535a
    ((j_common_ptr) cinfo, dest->whole_image,
kusano 7d535a
     dest->cur_output_row, (JDIMENSION) 1, TRUE);
kusano 7d535a
  dest->cur_output_row++;
kusano 7d535a
kusano 7d535a
  /* Transfer data. */
kusano 7d535a
  inptr = dest->pub.buffer[0];
kusano 7d535a
  outptr = image_ptr[0];
kusano 7d535a
  for (col = cinfo->output_width; col > 0; col--) {
kusano 7d535a
    *outptr++ = *inptr++;	/* can omit GETJSAMPLE() safely */
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Zero out the pad bytes. */
kusano 7d535a
  pad = dest->pad_bytes;
kusano 7d535a
  while (--pad >= 0)
kusano 7d535a
    *outptr++ = 0;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Startup: normally writes the file header.
kusano 7d535a
 * In this module we may as well postpone everything until finish_output.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
start_output_bmp (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
kusano 7d535a
{
kusano 7d535a
  /* no work here */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Finish up at the end of the file.
kusano 7d535a
 *
kusano 7d535a
 * Here is where we really output the BMP file.
kusano 7d535a
 *
kusano 7d535a
 * First, routines to write the Windows and OS/2 variants of the file header.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
write_bmp_header (j_decompress_ptr cinfo, bmp_dest_ptr dest)
kusano 7d535a
/* Write a Windows-style BMP file header, including colormap if needed */
kusano 7d535a
{
kusano 7d535a
  char bmpfileheader[14];
kusano 7d535a
  char bmpinfoheader[40];
kusano 7d535a
#define PUT_2B(array,offset,value)  \
kusano 7d535a
	(array[offset] = (char) ((value) & 0xFF), \
kusano 7d535a
	 array[offset+1] = (char) (((value) >> 8) & 0xFF))
kusano 7d535a
#define PUT_4B(array,offset,value)  \
kusano 7d535a
	(array[offset] = (char) ((value) & 0xFF), \
kusano 7d535a
	 array[offset+1] = (char) (((value) >> 8) & 0xFF), \
kusano 7d535a
	 array[offset+2] = (char) (((value) >> 16) & 0xFF), \
kusano 7d535a
	 array[offset+3] = (char) (((value) >> 24) & 0xFF))
kusano 7d535a
  INT32 headersize, bfSize;
kusano 7d535a
  int bits_per_pixel, cmap_entries;
kusano 7d535a
kusano 7d535a
  /* Compute colormap size and total file size */
kusano 7d535a
  if (cinfo->out_color_space == JCS_RGB) {
kusano 7d535a
    if (cinfo->quantize_colors) {
kusano 7d535a
      /* Colormapped RGB */
kusano 7d535a
      bits_per_pixel = 8;
kusano 7d535a
      cmap_entries = 256;
kusano 7d535a
    } else {
kusano 7d535a
      /* Unquantized, full color RGB */
kusano 7d535a
      bits_per_pixel = 24;
kusano 7d535a
      cmap_entries = 0;
kusano 7d535a
    }
kusano 7d535a
  } else {
kusano 7d535a
    /* Grayscale output.  We need to fake a 256-entry colormap. */
kusano 7d535a
    bits_per_pixel = 8;
kusano 7d535a
    cmap_entries = 256;
kusano 7d535a
  }
kusano 7d535a
  /* File size */
kusano 7d535a
  headersize = 14 + 40 + cmap_entries * 4; /* Header and colormap */
kusano 7d535a
  bfSize = headersize + (INT32) dest->row_width * (INT32) cinfo->output_height;
kusano 7d535a
  
kusano 7d535a
  /* Set unused fields of header to 0 */
kusano 7d535a
  MEMZERO(bmpfileheader, SIZEOF(bmpfileheader));
kusano 7d535a
  MEMZERO(bmpinfoheader, SIZEOF(bmpinfoheader));
kusano 7d535a
kusano 7d535a
  /* Fill the file header */
kusano 7d535a
  bmpfileheader[0] = 0x42;	/* first 2 bytes are ASCII 'B', 'M' */
kusano 7d535a
  bmpfileheader[1] = 0x4D;
kusano 7d535a
  PUT_4B(bmpfileheader, 2, bfSize); /* bfSize */
kusano 7d535a
  /* we leave bfReserved1 & bfReserved2 = 0 */
kusano 7d535a
  PUT_4B(bmpfileheader, 10, headersize); /* bfOffBits */
kusano 7d535a
kusano 7d535a
  /* Fill the info header (Microsoft calls this a BITMAPINFOHEADER) */
kusano 7d535a
  PUT_2B(bmpinfoheader, 0, 40);	/* biSize */
kusano 7d535a
  PUT_4B(bmpinfoheader, 4, cinfo->output_width); /* biWidth */
kusano 7d535a
  PUT_4B(bmpinfoheader, 8, cinfo->output_height); /* biHeight */
kusano 7d535a
  PUT_2B(bmpinfoheader, 12, 1);	/* biPlanes - must be 1 */
kusano 7d535a
  PUT_2B(bmpinfoheader, 14, bits_per_pixel); /* biBitCount */
kusano 7d535a
  /* we leave biCompression = 0, for none */
kusano 7d535a
  /* we leave biSizeImage = 0; this is correct for uncompressed data */
kusano 7d535a
  if (cinfo->density_unit == 2) { /* if have density in dots/cm, then */
kusano 7d535a
    PUT_4B(bmpinfoheader, 24, (INT32) (cinfo->X_density*100)); /* XPels/M */
kusano 7d535a
    PUT_4B(bmpinfoheader, 28, (INT32) (cinfo->Y_density*100)); /* XPels/M */
kusano 7d535a
  }
kusano 7d535a
  PUT_2B(bmpinfoheader, 32, cmap_entries); /* biClrUsed */
kusano 7d535a
  /* we leave biClrImportant = 0 */
kusano 7d535a
kusano 7d535a
  if (JFWRITE(dest->pub.output_file, bmpfileheader, 14) != (size_t) 14)
kusano 7d535a
    ERREXIT(cinfo, JERR_FILE_WRITE);
kusano 7d535a
  if (JFWRITE(dest->pub.output_file, bmpinfoheader, 40) != (size_t) 40)
kusano 7d535a
    ERREXIT(cinfo, JERR_FILE_WRITE);
kusano 7d535a
kusano 7d535a
  if (cmap_entries > 0)
kusano 7d535a
    write_colormap(cinfo, dest, cmap_entries, 4);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
write_os2_header (j_decompress_ptr cinfo, bmp_dest_ptr dest)
kusano 7d535a
/* Write an OS2-style BMP file header, including colormap if needed */
kusano 7d535a
{
kusano 7d535a
  char bmpfileheader[14];
kusano 7d535a
  char bmpcoreheader[12];
kusano 7d535a
  INT32 headersize, bfSize;
kusano 7d535a
  int bits_per_pixel, cmap_entries;
kusano 7d535a
kusano 7d535a
  /* Compute colormap size and total file size */
kusano 7d535a
  if (cinfo->out_color_space == JCS_RGB) {
kusano 7d535a
    if (cinfo->quantize_colors) {
kusano 7d535a
      /* Colormapped RGB */
kusano 7d535a
      bits_per_pixel = 8;
kusano 7d535a
      cmap_entries = 256;
kusano 7d535a
    } else {
kusano 7d535a
      /* Unquantized, full color RGB */
kusano 7d535a
      bits_per_pixel = 24;
kusano 7d535a
      cmap_entries = 0;
kusano 7d535a
    }
kusano 7d535a
  } else {
kusano 7d535a
    /* Grayscale output.  We need to fake a 256-entry colormap. */
kusano 7d535a
    bits_per_pixel = 8;
kusano 7d535a
    cmap_entries = 256;
kusano 7d535a
  }
kusano 7d535a
  /* File size */
kusano 7d535a
  headersize = 14 + 12 + cmap_entries * 3; /* Header and colormap */
kusano 7d535a
  bfSize = headersize + (INT32) dest->row_width * (INT32) cinfo->output_height;
kusano 7d535a
  
kusano 7d535a
  /* Set unused fields of header to 0 */
kusano 7d535a
  MEMZERO(bmpfileheader, SIZEOF(bmpfileheader));
kusano 7d535a
  MEMZERO(bmpcoreheader, SIZEOF(bmpcoreheader));
kusano 7d535a
kusano 7d535a
  /* Fill the file header */
kusano 7d535a
  bmpfileheader[0] = 0x42;	/* first 2 bytes are ASCII 'B', 'M' */
kusano 7d535a
  bmpfileheader[1] = 0x4D;
kusano 7d535a
  PUT_4B(bmpfileheader, 2, bfSize); /* bfSize */
kusano 7d535a
  /* we leave bfReserved1 & bfReserved2 = 0 */
kusano 7d535a
  PUT_4B(bmpfileheader, 10, headersize); /* bfOffBits */
kusano 7d535a
kusano 7d535a
  /* Fill the info header (Microsoft calls this a BITMAPCOREHEADER) */
kusano 7d535a
  PUT_2B(bmpcoreheader, 0, 12);	/* bcSize */
kusano 7d535a
  PUT_2B(bmpcoreheader, 4, cinfo->output_width); /* bcWidth */
kusano 7d535a
  PUT_2B(bmpcoreheader, 6, cinfo->output_height); /* bcHeight */
kusano 7d535a
  PUT_2B(bmpcoreheader, 8, 1);	/* bcPlanes - must be 1 */
kusano 7d535a
  PUT_2B(bmpcoreheader, 10, bits_per_pixel); /* bcBitCount */
kusano 7d535a
kusano 7d535a
  if (JFWRITE(dest->pub.output_file, bmpfileheader, 14) != (size_t) 14)
kusano 7d535a
    ERREXIT(cinfo, JERR_FILE_WRITE);
kusano 7d535a
  if (JFWRITE(dest->pub.output_file, bmpcoreheader, 12) != (size_t) 12)
kusano 7d535a
    ERREXIT(cinfo, JERR_FILE_WRITE);
kusano 7d535a
kusano 7d535a
  if (cmap_entries > 0)
kusano 7d535a
    write_colormap(cinfo, dest, cmap_entries, 3);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Write the colormap.
kusano 7d535a
 * Windows uses BGR0 map entries; OS/2 uses BGR entries.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
LOCAL(void)
kusano 7d535a
write_colormap (j_decompress_ptr cinfo, bmp_dest_ptr dest,
kusano 7d535a
		int map_colors, int map_entry_size)
kusano 7d535a
{
kusano 7d535a
  JSAMPARRAY colormap = cinfo->colormap;
kusano 7d535a
  int num_colors = cinfo->actual_number_of_colors;
kusano 7d535a
  FILE * outfile = dest->pub.output_file;
kusano 7d535a
  int i;
kusano 7d535a
kusano 7d535a
  if (colormap != NULL) {
kusano 7d535a
    if (cinfo->out_color_components == 3) {
kusano 7d535a
      /* Normal case with RGB colormap */
kusano 7d535a
      for (i = 0; i < num_colors; i++) {
kusano 7d535a
	putc(GETJSAMPLE(colormap[2][i]), outfile);
kusano 7d535a
	putc(GETJSAMPLE(colormap[1][i]), outfile);
kusano 7d535a
	putc(GETJSAMPLE(colormap[0][i]), outfile);
kusano 7d535a
	if (map_entry_size == 4)
kusano 7d535a
	  putc(0, outfile);
kusano 7d535a
      }
kusano 7d535a
    } else {
kusano 7d535a
      /* Grayscale colormap (only happens with grayscale quantization) */
kusano 7d535a
      for (i = 0; i < num_colors; i++) {
kusano 7d535a
	putc(GETJSAMPLE(colormap[0][i]), outfile);
kusano 7d535a
	putc(GETJSAMPLE(colormap[0][i]), outfile);
kusano 7d535a
	putc(GETJSAMPLE(colormap[0][i]), outfile);
kusano 7d535a
	if (map_entry_size == 4)
kusano 7d535a
	  putc(0, outfile);
kusano 7d535a
      }
kusano 7d535a
    }
kusano 7d535a
  } else {
kusano 7d535a
    /* If no colormap, must be grayscale data.  Generate a linear "map". */
kusano 7d535a
    for (i = 0; i < 256; i++) {
kusano 7d535a
      putc(i, outfile);
kusano 7d535a
      putc(i, outfile);
kusano 7d535a
      putc(i, outfile);
kusano 7d535a
      if (map_entry_size == 4)
kusano 7d535a
	putc(0, outfile);
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
  /* Pad colormap with zeros to ensure specified number of colormap entries */ 
kusano 7d535a
  if (i > map_colors)
kusano 7d535a
    ERREXIT1(cinfo, JERR_TOO_MANY_COLORS, i);
kusano 7d535a
  for (; i < map_colors; i++) {
kusano 7d535a
    putc(0, outfile);
kusano 7d535a
    putc(0, outfile);
kusano 7d535a
    putc(0, outfile);
kusano 7d535a
    if (map_entry_size == 4)
kusano 7d535a
      putc(0, outfile);
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
METHODDEF(void)
kusano 7d535a
finish_output_bmp (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
kusano 7d535a
{
kusano 7d535a
  bmp_dest_ptr dest = (bmp_dest_ptr) dinfo;
kusano 7d535a
  register FILE * outfile = dest->pub.output_file;
kusano 7d535a
  JSAMPARRAY image_ptr;
kusano 7d535a
  register JSAMPROW data_ptr;
kusano 7d535a
  JDIMENSION row;
kusano 7d535a
  register JDIMENSION col;
kusano 7d535a
  cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
kusano 7d535a
kusano 7d535a
  /* Write the header and colormap */
kusano 7d535a
  if (dest->is_os2)
kusano 7d535a
    write_os2_header(cinfo, dest);
kusano 7d535a
  else
kusano 7d535a
    write_bmp_header(cinfo, dest);
kusano 7d535a
kusano 7d535a
  /* Write the file body from our virtual array */
kusano 7d535a
  for (row = cinfo->output_height; row > 0; row--) {
kusano 7d535a
    if (progress != NULL) {
kusano 7d535a
      progress->pub.pass_counter = (long) (cinfo->output_height - row);
kusano 7d535a
      progress->pub.pass_limit = (long) cinfo->output_height;
kusano 7d535a
      (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
kusano 7d535a
    }
kusano 7d535a
    image_ptr = (*cinfo->mem->access_virt_sarray)
kusano 7d535a
      ((j_common_ptr) cinfo, dest->whole_image, row-1, (JDIMENSION) 1, FALSE);
kusano 7d535a
    data_ptr = image_ptr[0];
kusano 7d535a
    for (col = dest->row_width; col > 0; col--) {
kusano 7d535a
      putc(GETJSAMPLE(*data_ptr), outfile);
kusano 7d535a
      data_ptr++;
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
  if (progress != NULL)
kusano 7d535a
    progress->completed_extra_passes++;
kusano 7d535a
kusano 7d535a
  /* Make sure we wrote the output file OK */
kusano 7d535a
  fflush(outfile);
kusano 7d535a
  if (ferror(outfile))
kusano 7d535a
    ERREXIT(cinfo, JERR_FILE_WRITE);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * The module selection routine for BMP format output.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(djpeg_dest_ptr)
kusano 7d535a
jinit_write_bmp (j_decompress_ptr cinfo, boolean is_os2)
kusano 7d535a
{
kusano 7d535a
  bmp_dest_ptr dest;
kusano 7d535a
  JDIMENSION row_width;
kusano 7d535a
kusano 7d535a
  /* Create module interface object, fill in method pointers */
kusano 7d535a
  dest = (bmp_dest_ptr)
kusano 7d535a
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
kusano 7d535a
				  SIZEOF(bmp_dest_struct));
kusano 7d535a
  dest->pub.start_output = start_output_bmp;
kusano 7d535a
  dest->pub.finish_output = finish_output_bmp;
kusano 7d535a
  dest->is_os2 = is_os2;
kusano 7d535a
kusano 7d535a
  if (cinfo->out_color_space == JCS_GRAYSCALE) {
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
      dest->pub.put_pixel_rows = put_gray_rows;
kusano 7d535a
    else
kusano 7d535a
      dest->pub.put_pixel_rows = put_pixel_rows;
kusano 7d535a
  } else {
kusano 7d535a
    ERREXIT(cinfo, JERR_BMP_COLORSPACE);
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Calculate output image dimensions so we can allocate space */
kusano 7d535a
  jpeg_calc_output_dimensions(cinfo);
kusano 7d535a
kusano 7d535a
  /* Determine width of rows in the BMP file (padded to 4-byte boundary). */
kusano 7d535a
  row_width = cinfo->output_width * cinfo->output_components;
kusano 7d535a
  dest->data_width = row_width;
kusano 7d535a
  while ((row_width & 3) != 0) row_width++;
kusano 7d535a
  dest->row_width = row_width;
kusano 7d535a
  dest->pad_bytes = (int) (row_width - dest->data_width);
kusano 7d535a
kusano 7d535a
  /* Allocate space for inversion array, prepare for write pass */
kusano 7d535a
  dest->whole_image = (*cinfo->mem->request_virt_sarray)
kusano 7d535a
    ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
kusano 7d535a
     row_width, cinfo->output_height, (JDIMENSION) 1);
kusano 7d535a
  dest->cur_output_row = 0;
kusano 7d535a
  if (cinfo->progress != NULL) {
kusano 7d535a
    cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
kusano 7d535a
    progress->total_extra_passes++; /* count file input as separate pass */
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Create decompressor output buffer. */
kusano 7d535a
  dest->pub.buffer = (*cinfo->mem->alloc_sarray)
kusano 7d535a
    ((j_common_ptr) cinfo, JPOOL_IMAGE, row_width, (JDIMENSION) 1);
kusano 7d535a
  dest->pub.buffer_height = 1;
kusano 7d535a
kusano 7d535a
  return (djpeg_dest_ptr) dest;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#endif /* BMP_SUPPORTED */