home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume44 / jpeg / part06 / rdbmp.c next >
C/C++ Source or Header  |  1994-09-26  |  13KB  |  422 lines

  1. /*
  2.  * rdbmp.c
  3.  *
  4.  * Copyright (C) 1994, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains routines to read input images in Microsoft "BMP"
  9.  * format (MS Windows 3.x, OS/2 1.x, and OS/2 2.x flavors).
  10.  * Currently, only 8-bit and 24-bit images are supported, not 1-bit or
  11.  * 4-bit (feeding such low-depth images into JPEG would be silly anyway).
  12.  * Also, we don't support RLE-compressed files.
  13.  *
  14.  * These routines may need modification for non-Unix environments or
  15.  * specialized applications.  As they stand, they assume input from
  16.  * an ordinary stdio stream.  They further assume that reading begins
  17.  * at the start of the file; start_input may need work if the
  18.  * user interface has already read some data (e.g., to determine that
  19.  * the file is indeed BMP format).
  20.  *
  21.  * This code contributed by James Arthur Boucher.
  22.  */
  23.  
  24. #include "cdjpeg.h"        /* Common decls for cjpeg/djpeg applications */
  25.  
  26. #ifdef BMP_SUPPORTED
  27.  
  28.  
  29. /* Macros to deal with unsigned chars as efficiently as compiler allows */
  30.  
  31. #ifdef HAVE_UNSIGNED_CHAR
  32. typedef unsigned char U_CHAR;
  33. #define UCH(x)    ((int) (x))
  34. #else /* !HAVE_UNSIGNED_CHAR */
  35. #ifdef CHAR_IS_UNSIGNED
  36. typedef char U_CHAR;
  37. #define UCH(x)    ((int) (x))
  38. #else
  39. typedef char U_CHAR;
  40. #define UCH(x)    ((int) (x) & 0xFF)
  41. #endif
  42. #endif /* HAVE_UNSIGNED_CHAR */
  43.  
  44.  
  45. #define    ReadOK(file,buffer,len)    (JFREAD(file,buffer,len) == ((size_t) (len)))
  46.  
  47.  
  48. /* Private version of data source object */
  49.  
  50. typedef struct _bmp_source_struct * bmp_source_ptr;
  51.  
  52. typedef struct _bmp_source_struct {
  53.   struct cjpeg_source_struct pub; /* public fields */
  54.  
  55.   j_compress_ptr cinfo;        /* back link saves passing separate parm */
  56.  
  57.   JSAMPARRAY colormap;        /* BMP colormap (converted to my format) */
  58.  
  59.   jvirt_sarray_ptr whole_image;    /* Needed to reverse row order */
  60.   JDIMENSION source_row;    /* Current source row number */
  61.   JDIMENSION row_width;        /* Physical width of scanlines in file */
  62.  
  63.   int bits_per_pixel;        /* remembers 8- or 24-bit format */
  64. } bmp_source_struct;
  65.  
  66.  
  67. LOCAL int
  68. read_byte (bmp_source_ptr sinfo)
  69. /* Read next byte from BMP file */
  70. {
  71.   register FILE *infile = sinfo->pub.input_file;
  72.   register int c;
  73.  
  74.   if ((c = getc(infile)) == EOF)
  75.     ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
  76.   return c;
  77. }
  78.  
  79.  
  80. LOCAL void
  81. read_colormap (bmp_source_ptr sinfo, int cmaplen, int mapentrysize)
  82. /* Read the colormap from a BMP file */
  83. {
  84.   int i;
  85.  
  86.   switch (mapentrysize) {
  87.   case 3:
  88.     /* BGR format (occurs in OS/2 files) */
  89.     for (i = 0; i < cmaplen; i++) {
  90.       sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo);
  91.       sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo);
  92.       sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo);
  93.     }
  94.     break;
  95.   case 4:
  96.     /* BGR0 format (occurs in MS Windows files) */
  97.     for (i = 0; i < cmaplen; i++) {
  98.       sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo);
  99.       sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo);
  100.       sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo);
  101.       (void) read_byte(sinfo);
  102.     }
  103.     break;
  104.   default:
  105.     ERREXIT(sinfo->cinfo, JERR_BMP_BADCMAP);
  106.     break;
  107.   }
  108. }
  109.  
  110.  
  111. /*
  112.  * Read one row of pixels.
  113.  * The image has been read into the whole_image array, but is otherwise
  114.  * unprocessed.  We must read it out in top-to-bottom row order, and if
  115.  * it is an 8-bit image, we must expand colormapped pixels to 24bit format.
  116.  */
  117.  
  118. METHODDEF JDIMENSION
  119. get_8bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  120. /* This version is for reading 8-bit colormap indexes */
  121. {
  122.   bmp_source_ptr source = (bmp_source_ptr) sinfo;
  123.   register JSAMPARRAY colormap = source->colormap;
  124.   JSAMPARRAY image_ptr;
  125.   register int t;
  126.   register JSAMPROW inptr, outptr;
  127.   register JDIMENSION col;
  128.  
  129.   /* Fetch next row from virtual array */
  130.   source->source_row--;
  131.   image_ptr = (*cinfo->mem->access_virt_sarray)
  132.     ((j_common_ptr) cinfo, source->whole_image, source->source_row, FALSE);
  133.  
  134.   /* Expand the colormap indexes to real data */
  135.   inptr = image_ptr[0];
  136.   outptr = source->pub.buffer[0];
  137.   for (col = cinfo->image_width; col > 0; col--) {
  138.     t = GETJSAMPLE(*inptr++);
  139.     *outptr++ = colormap[0][t];    /* can omit GETJSAMPLE() safely */
  140.     *outptr++ = colormap[1][t];
  141.     *outptr++ = colormap[2][t];
  142.   }
  143.  
  144.   return 1;
  145. }
  146.  
  147.  
  148. METHODDEF JDIMENSION
  149. get_24bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  150. /* This version is for reading 24-bit pixels */
  151. {
  152.   bmp_source_ptr source = (bmp_source_ptr) sinfo;
  153.   JSAMPARRAY image_ptr;
  154.   register JSAMPROW inptr, outptr;
  155.   register JDIMENSION col;
  156.  
  157.   /* Fetch next row from virtual array */
  158.   source->source_row--;
  159.   image_ptr = (*cinfo->mem->access_virt_sarray)
  160.     ((j_common_ptr) cinfo, source->whole_image, source->source_row, FALSE);
  161.  
  162.   /* Transfer data.  Note source values are in BGR order
  163.    * (even though Microsoft's own documents say the opposite).
  164.    */
  165.   inptr = image_ptr[0];
  166.   outptr = source->pub.buffer[0];
  167.   for (col = cinfo->image_width; col > 0; col--) {
  168.     outptr[2] = *inptr++;    /* can omit GETJSAMPLE() safely */
  169.     outptr[1] = *inptr++;
  170.     outptr[0] = *inptr++;
  171.     outptr += 3;
  172.   }
  173.  
  174.   return 1;
  175. }
  176.  
  177.  
  178. /*
  179.  * This method loads the image into whole_image during the first call on
  180.  * get_pixel_rows.  The get_pixel_rows pointer is then adjusted to call
  181.  * get_8bit_row or get_24bit_row on subsequent calls.
  182.  */
  183.  
  184. METHODDEF JDIMENSION
  185. preload_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  186. {
  187.   bmp_source_ptr source = (bmp_source_ptr) sinfo;
  188.   register FILE *infile = source->pub.input_file;
  189.   register int c;
  190.   register JSAMPROW out_ptr;
  191.   JSAMPARRAY image_ptr;
  192.   JDIMENSION row, col;
  193.   cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
  194.  
  195.   /* Read the data into a virtual array in input-file row order. */
  196.   for (row = 0; row < cinfo->image_height; row++) {
  197.     if (progress != NULL) {
  198.       progress->pub.pass_counter = (long) row;
  199.       progress->pub.pass_limit = (long) cinfo->image_height;
  200.       (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
  201.     }
  202.     image_ptr = (*cinfo->mem->access_virt_sarray)
  203.       ((j_common_ptr) cinfo, source->whole_image, row, TRUE);
  204.     out_ptr = image_ptr[0];
  205.     for (col = source->row_width; col > 0; col--) {
  206.       /* inline copy of read_byte() for speed */
  207.       if ((c = getc(infile)) == EOF)
  208.     ERREXIT(cinfo, JERR_INPUT_EOF);
  209.       *out_ptr++ = (JSAMPLE) c;
  210.     }
  211.   }
  212.   if (progress != NULL)
  213.     progress->completed_extra_passes++;
  214.  
  215.   /* Set up to read from the virtual array in top-to-bottom order */
  216.   switch (source->bits_per_pixel) {
  217.   case 8:
  218.     source->pub.get_pixel_rows = get_8bit_row;
  219.     break;
  220.   case 24:
  221.     source->pub.get_pixel_rows = get_24bit_row;
  222.     break;
  223.   default:
  224.     ERREXIT(cinfo, JERR_BMP_BADDEPTH);
  225.   }
  226.   source->source_row = cinfo->image_height;
  227.  
  228.   /* And read the first row */
  229.   return (*source->pub.get_pixel_rows) (cinfo, sinfo);
  230. }
  231.  
  232.  
  233. /*
  234.  * Read the file header; return image size and component count.
  235.  */
  236.  
  237. METHODDEF void
  238. start_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  239. {
  240.   bmp_source_ptr source = (bmp_source_ptr) sinfo;
  241.   U_CHAR bmpfileheader[14];
  242.   U_CHAR bmpinfoheader[64];
  243. #define GET_2B(array,offset)  ((unsigned int) UCH(array[offset]) + \
  244.                    (((unsigned int) UCH(array[offset+1])) << 8))
  245. #define GET_4B(array,offset)  ((INT32) UCH(array[offset]) + \
  246.                    (((INT32) UCH(array[offset+1])) << 8) + \
  247.                    (((INT32) UCH(array[offset+2])) << 16) + \
  248.                    (((INT32) UCH(array[offset+3])) << 24))
  249.   INT32 headerSize;
  250.   INT32 biWidth = 0;        /* initialize to avoid compiler warning */
  251.   INT32 biHeight = 0;
  252.   unsigned int biPlanes;
  253.   INT32 biCompression;
  254.   INT32 biXPelsPerMeter,biYPelsPerMeter;
  255.   INT32 biClrUsed = 0;
  256.   int mapentrysize = 0;        /* 0 indicates no colormap */
  257.   JDIMENSION row_width;
  258.  
  259.   /* Read and verify the bitmap file header */
  260.   if (! ReadOK(source->pub.input_file, bmpfileheader, 14))
  261.     ERREXIT(cinfo, JERR_INPUT_EOF);
  262.   if (GET_2B(bmpfileheader,0) != 0x4D42) /* 'BM' */
  263.     ERREXIT(cinfo, JERR_BMP_NOT);
  264.   /* We ignore the remaining fileheader fields */
  265.  
  266.   /* The infoheader might be 12 bytes (OS/2 1.x), 40 bytes (Windows),
  267.    * or 64 bytes (OS/2 2.x).  Check the first 4 bytes to find out which.
  268.    */
  269.   if (! ReadOK(source->pub.input_file, bmpinfoheader, 4))
  270.     ERREXIT(cinfo, JERR_INPUT_EOF);
  271.   headerSize = (INT32) GET_4B(bmpinfoheader,0);
  272.   if (headerSize < 12 || headerSize > 64)
  273.     ERREXIT(cinfo, JERR_BMP_BADHEADER);
  274.   if (! ReadOK(source->pub.input_file, bmpinfoheader+4, headerSize-4))
  275.     ERREXIT(cinfo, JERR_INPUT_EOF);
  276.  
  277.   switch ((int) headerSize) {
  278.   case 12:
  279.     /* Decode OS/2 1.x header (Microsoft calls this a BITMAPCOREHEADER) */
  280.     biWidth = (INT32) GET_2B(bmpinfoheader,4);
  281.     biHeight = (INT32) GET_2B(bmpinfoheader,6);
  282.     biPlanes = GET_2B(bmpinfoheader,8);
  283.     source->bits_per_pixel = (int) GET_2B(bmpinfoheader,10);
  284.  
  285.     switch (source->bits_per_pixel) {
  286.     case 8:            /* colormapped image */
  287.       mapentrysize = 3;        /* OS/2 uses RGBTRIPLE colormap */
  288.       TRACEMS2(cinfo, 1, JTRC_BMP_OS2_MAPPED, (int) biWidth, (int) biHeight);
  289.       break;
  290.     case 24:            /* RGB image */
  291.       TRACEMS2(cinfo, 1, JTRC_BMP_OS2, (int) biWidth, (int) biHeight);
  292.       break;
  293.     default:
  294.       ERREXIT(cinfo, JERR_BMP_BADDEPTH);
  295.       break;
  296.     }
  297.     if (biPlanes != 1)
  298.       ERREXIT(cinfo, JERR_BMP_BADPLANES);
  299.     break;
  300.   case 40:
  301.   case 64:
  302.     /* Decode Windows 3.x header (Microsoft calls this a BITMAPINFOHEADER) */
  303.     /* or OS/2 2.x header, which has additional fields that we ignore */
  304.     biWidth = GET_4B(bmpinfoheader,4);
  305.     biHeight = GET_4B(bmpinfoheader,8);
  306.     biPlanes = GET_2B(bmpinfoheader,12);
  307.     source->bits_per_pixel = (int) GET_2B(bmpinfoheader,14);
  308.     biCompression = GET_4B(bmpinfoheader,16);
  309.     biXPelsPerMeter = GET_4B(bmpinfoheader,24);
  310.     biYPelsPerMeter = GET_4B(bmpinfoheader,28);
  311.     biClrUsed = GET_4B(bmpinfoheader,32);
  312.     /* biSizeImage, biClrImportant fields are ignored */
  313.  
  314.     switch (source->bits_per_pixel) {
  315.     case 8:            /* colormapped image */
  316.       mapentrysize = 4;        /* Windows uses RGBQUAD colormap */
  317.       TRACEMS2(cinfo, 1, JTRC_BMP_MAPPED, (int) biWidth, (int) biHeight);
  318.       break;
  319.     case 24:            /* RGB image */
  320.       TRACEMS2(cinfo, 1, JTRC_BMP, (int) biWidth, (int) biHeight);
  321.       break;
  322.     default:
  323.       ERREXIT(cinfo, JERR_BMP_BADDEPTH);
  324.       break;
  325.     }
  326.     if (biPlanes != 1)
  327.       ERREXIT(cinfo, JERR_BMP_BADPLANES);
  328.     if (biCompression != 0)
  329.       ERREXIT(cinfo, JERR_BMP_COMPRESSED);
  330.  
  331.     if (biXPelsPerMeter > 0 && biYPelsPerMeter > 0) {
  332.       /* Set JFIF density parameters from the BMP data */
  333.       cinfo->X_density = (UINT16) (biXPelsPerMeter/100); /* 100 cm per meter */
  334.       cinfo->Y_density = (UINT16) (biYPelsPerMeter/100);
  335.       cinfo->density_unit = 2;    /* dots/cm */
  336.     }
  337.     break;
  338.   default:
  339.     ERREXIT(cinfo, JERR_BMP_BADHEADER);
  340.     break;
  341.   }
  342.  
  343.   /* Read the colormap, if any */
  344.   if (mapentrysize > 0) {
  345.     if (biClrUsed <= 0)
  346.       biClrUsed = 256;        /* assume it's 256 */
  347.     else if (biClrUsed > 256)
  348.       ERREXIT(cinfo, JERR_BMP_BADCMAP);
  349.     /* Allocate space to store the colormap */
  350.     source->colormap = (*cinfo->mem->alloc_sarray)
  351.       ((j_common_ptr) cinfo, JPOOL_IMAGE,
  352.        (JDIMENSION) biClrUsed, (JDIMENSION) 3);
  353.     /* and read it from the file */
  354.     read_colormap(source, (int) biClrUsed, mapentrysize);
  355.   }
  356.  
  357.   /* Compute row width in file, including padding to 4-byte boundary */
  358.   if (source->bits_per_pixel == 24)
  359.     row_width = (JDIMENSION) (biWidth * 3);
  360.   else
  361.     row_width = (JDIMENSION) biWidth;
  362.   while ((row_width & 3) != 0) row_width++;
  363.   source->row_width = row_width;
  364.  
  365.   /* Allocate space for inversion array, prepare for preload pass */
  366.   source->whole_image = (*cinfo->mem->request_virt_sarray)
  367.     ((j_common_ptr) cinfo, JPOOL_IMAGE,
  368.      row_width, (JDIMENSION) biHeight, (JDIMENSION) 1);
  369.   source->pub.get_pixel_rows = preload_image;
  370.   if (cinfo->progress != NULL) {
  371.     cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
  372.     progress->total_extra_passes++; /* count file input as separate pass */
  373.   }
  374.  
  375.   /* Allocate one-row buffer for returned data */
  376.   source->pub.buffer = (*cinfo->mem->alloc_sarray)
  377.     ((j_common_ptr) cinfo, JPOOL_IMAGE,
  378.      (JDIMENSION) (biWidth * 3), (JDIMENSION) 1);
  379.   source->pub.buffer_height = 1;
  380.  
  381.   cinfo->in_color_space = JCS_RGB;
  382.   cinfo->input_components = 3;
  383.   cinfo->data_precision = 8;
  384.   cinfo->image_width = (JDIMENSION) biWidth;
  385.   cinfo->image_height = (JDIMENSION) biHeight;
  386. }
  387.  
  388.  
  389. /*
  390.  * Finish up at the end of the file.
  391.  */
  392.  
  393. METHODDEF void
  394. finish_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  395. {
  396.   /* no work */
  397. }
  398.  
  399.  
  400. /*
  401.  * The module selection routine for BMP format input.
  402.  */
  403.  
  404. GLOBAL cjpeg_source_ptr
  405. jinit_read_bmp (j_compress_ptr cinfo)
  406. {
  407.   bmp_source_ptr source;
  408.  
  409.   /* Create module interface object */
  410.   source = (bmp_source_ptr)
  411.       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  412.                   SIZEOF(bmp_source_struct));
  413.   source->cinfo = cinfo;    /* make back link for subroutines */
  414.   /* Fill in method ptrs, except get_pixel_rows which start_input sets */
  415.   source->pub.start_input = start_input_bmp;
  416.   source->pub.finish_input = finish_input_bmp;
  417.  
  418.   return (cjpeg_source_ptr) source;
  419. }
  420.  
  421. #endif /* BMP_SUPPORTED */
  422.