home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libcnv / readbmp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  22.0 KB  |  735 lines

  1. /*
  2.  * readbmp.c
  3. This file was taken from the Independent JPEG Group and modified extensively
  4.  *
  5.  * Copyright (C) 1994-1995, Thomas G. Lane.
  6.  * This file is part of the Independent JPEG Group's software.
  7.  * For conditions of distribution and use, see the accompanying README file.
  8.  *
  9. /
  10.  /* This code is for reading bmps and translating them to RGB.  There are additions that Netscape has 
  11.   * added to read more formats.
  12.  */
  13. #include "xp_core.h" //used to make library compile faster on win32 do not ifdef this or it wont work
  14.  
  15. /* readbmp : reads bmp and returns colormap (if any), CMN_IMAGEINFO,bits*/
  16. #include <memory.h>
  17. #include <stdio.h>
  18. #include <malloc.h>
  19. #include "xp_core.h"/*defines of int32 ect*/
  20. #include "xpassert.h"
  21. #include "ntypes.h" /* for MWContext to include libcnv.h*/
  22.  
  23. #include "libcnv.h"
  24. #include "readbmp.h"
  25.  
  26. typedef unsigned char U_CHAR;
  27. #define UCH(x)    ((int16) (x))
  28. typedef uint16 UINT16;
  29.  
  30. #define TRUE !FALSE
  31.  
  32. XP_Bool get_1bit_row_bmp (CONVERT_IMGCONTEXT *p_params,CONVERT_IMG_INFO *p_info,CONVERT_IMG_ROW p_outputbuffer);
  33. XP_Bool get_4bit_row_bmp (CONVERT_IMGCONTEXT *p_params,CONVERT_IMG_INFO *p_info,CONVERT_IMG_ROW p_outputbuffer);
  34. XP_Bool get_8bit_row_bmp (CONVERT_IMGCONTEXT *p_params,CONVERT_IMG_INFO *p_info,CONVERT_IMG_ROW p_outputbuffer);
  35. XP_Bool get_24bit_row_bmp (CONVERT_IMGCONTEXT *p_params,CONVERT_IMG_INFO *p_info,CONVERT_IMG_ROW p_outputbuffer);
  36. XP_Bool get_16bit_row_bmp (CONVERT_IMGCONTEXT *p_params,CONVERT_IMG_INFO *p_info,CONVERT_IMG_ROW p_outputbuffer);
  37. XP_Bool get_32bit_row_bmp (CONVERT_IMGCONTEXT *p_params,CONVERT_IMG_INFO *p_info,CONVERT_IMG_ROW p_outputbuffer);
  38.  
  39. void process_color_mask(uint32 *p_mask,char *p_shift,uint16 *p_pad);
  40.  
  41. CONVERT_IMAGERESULT read_colormap_bmp (CONVERT_IMGCONTEXT *p_inputparam, BYTE *p_colormap,int16 cmaplen, int16 mapentrysize);
  42.  
  43. ///////////////////////////////////////
  44. //////////READ BMP/////////////////////
  45. CONVERT_IMAGERESULT
  46. start_input_bmp (CONVERT_IMG_INFO *p_imageinfo, CONVERT_IMGCONTEXT *p_inputparam)
  47. {
  48.   CONVERT_IMAGERESULT t_err;
  49.   U_CHAR bmpfileheader[14];
  50.   U_CHAR bmpinfoheader[64];
  51. #define GET_2B(array,offset)  ((uint16) UCH(array[offset]) + \
  52.                    (((uint16) UCH(array[offset+1])) << 8))
  53. #define GET_4B(array,offset)  ((int32) UCH(array[offset]) + \
  54.                    (((int32) UCH(array[offset+1])) << 8) + \
  55.                    (((int32) UCH(array[offset+2])) << 16) + \
  56.                    (((int32) UCH(array[offset+3])) << 24))
  57.   int32 bfOffBits;
  58.   int32 headerSize;
  59.   int32 biWidth = 0;        /* initialize to avoid compiler warning */
  60.   int32 biHeight = 0;
  61.   uint16 biPlanes;
  62.   int32 biCompression;
  63.   int32 biXPelsPerMeter,biYPelsPerMeter;
  64.   int32 biClrUsed = 0;
  65.   int16 mapentrysize = 0;        /* 0 indicates no colormap */
  66.   DWORD t_index=0;/*index to stream*/
  67.   int32 bPad;
  68.   uint16 row_width;
  69.   if (p_inputparam->m_stream.m_type==CONVERT_FILE)
  70.   {
  71.       if (!read_param(&p_inputparam->m_stream,bmpfileheader,14))
  72.       {/*cant read fileheader*/
  73.         return CONVERR_INVALIDFILEHEADER;
  74.       }
  75.       p_imageinfo->m_image_size+=14;
  76.       /* Read and verify the bitmap file header */
  77.       if (GET_2B(bmpfileheader,0) != 0x4D42) /* 'BM' */
  78.         return CONVERR_INVALIDFILEHEADER;
  79.       bfOffBits = (int32) GET_4B(bmpfileheader,10);
  80.   }
  81.   if (! read_param(&p_inputparam->m_stream, bmpinfoheader, 4))
  82.     return CONVERR_INVALIDIMAGEHEADER;//cant read 4 bytes
  83.   p_imageinfo->m_image_size+=4;
  84.   headerSize = (int32) GET_4B(bmpinfoheader,0);
  85.   if (headerSize < 12 || headerSize > 64)
  86.     return CONVERR_INVALIDIMAGEHEADER;
  87.   if (! read_param(
  88.       &p_inputparam->m_stream, 
  89.       bmpinfoheader+4,
  90.       (int16)(headerSize-4))) /*casting here is ok beacause of check for >64 above*/
  91.     return CONVERR_INVALIDIMAGEHEADER;
  92.   p_imageinfo->m_image_size+=headerSize-4;
  93.   switch ((int16) headerSize) {
  94.   case 12:
  95.     /* Decode OS/2 1.x header (Microsoft calls this a BITMAPCOREHEADER) */
  96.     biWidth = (int32) GET_2B(bmpinfoheader,4);
  97.     biHeight = (int32) GET_2B(bmpinfoheader,6);
  98.     biPlanes = GET_2B(bmpinfoheader,8);
  99.     p_imageinfo->m_bitsperpixel = (int16) GET_2B(bmpinfoheader,10);
  100.  
  101.     switch (p_imageinfo->m_bitsperpixel) {
  102.     case 8:            /* colormapped image */
  103.       mapentrysize = 3;        /* OS/2 uses RGBTRIPLE colormap */
  104.       break;
  105.     case 16:
  106.     case 24:            /* RGB image */
  107.       break;
  108.     default:
  109.       return CONVERR_INVALIDBITDEPTH;
  110.       break;
  111.     }
  112.     if (biPlanes != 1)
  113.       return CONVERR_INVALIDBITDEPTH;
  114.     break;
  115.   case 40:
  116.   case 64:
  117.     /* Decode Windows 3.x header (Microsoft calls this a BITMAPINFOHEADER) */
  118.     /* or OS/2 2.x header, which has additional fields that we ignore */
  119.     biWidth = GET_4B(bmpinfoheader,4);
  120.     biHeight = GET_4B(bmpinfoheader,8);
  121.     biPlanes = GET_2B(bmpinfoheader,12);
  122.     p_imageinfo->m_bitsperpixel = (int16) GET_2B(bmpinfoheader,14);
  123.     biCompression = GET_4B(bmpinfoheader,16);
  124.     biXPelsPerMeter = GET_4B(bmpinfoheader,24);
  125.     biYPelsPerMeter = GET_4B(bmpinfoheader,28);
  126.     biClrUsed = GET_4B(bmpinfoheader,32);
  127.     /* biSizeImage, biClrImportant fields are ignored */
  128.     switch (p_imageinfo->m_bitsperpixel) {
  129.     case 1:
  130.         if (!biClrUsed)/* 0 denotes maximum usage of colors*/
  131.             biClrUsed=2;
  132.     case 4:
  133.         if (!biClrUsed)/* 0 denotes maximum usage of colors*/
  134.             biClrUsed=16;
  135.     case 8:            /* colormapped images */
  136.         if (!biClrUsed)/* 0 denotes maximum usage of colors*/
  137.             biClrUsed=256;
  138.         mapentrysize = 4;        /* Windows uses RGBQUAD colormap */
  139.         break;
  140.     case 24:            /* RGB image */
  141.       break;
  142.     case 16:
  143.     case 32:            /* RGB image with masks*/
  144.         if (biCompression==3) /*BI_BITFIELDS*/
  145.         {
  146.             biClrUsed=3; /*need to get masks for RGB*/
  147.             mapentrysize = 4;        /* Windows uses RGBQUAD colormap */
  148.         }
  149.       break;
  150.     default:
  151.       return CONVERR_INVALIDBITDEPTH;
  152.       break;
  153.     }
  154.  
  155.     if (biPlanes != 1)
  156.       return FALSE;
  157.     if (biCompression != 0)
  158.         if (3!=biCompression)
  159.             return CONVERR_COMPRESSED;//cant handle compressed bitmaps
  160.  
  161.     if (biXPelsPerMeter > 0 && biYPelsPerMeter > 0) {
  162.       /* Set JFIF density parameters from the BMP data */
  163.       p_imageinfo->m_X_density = (UINT16) (biXPelsPerMeter/100); /* 100 cm per meter */
  164.       p_imageinfo->m_Y_density = (UINT16) (biYPelsPerMeter/100);
  165.       p_imageinfo->m_density_unit = 2;    /* dots/cm */
  166.     }
  167.     break;
  168.   default:
  169.     return CONVERR_INVALIDIMAGEHEADER;
  170.     break;
  171.   }
  172.  
  173.   /* Compute distance to bitmap data --- will adjust for colormap below */
  174.   bPad = bfOffBits - (headerSize + 14);
  175.  
  176.   /* Read the colormap, if any */
  177.   if (mapentrysize > 0) {
  178.     if (biClrUsed <= 0)
  179.       biClrUsed = 256;        /* assume it's 256 */
  180.     else if (biClrUsed > 256)
  181.       return CONVERR_INVALIDCOLORMAP;
  182.     /* Allocate space to store the colormap */
  183.     p_imageinfo->m_colormap = (BYTE *)malloc(biClrUsed*3);
  184.     /* and read it from the file */
  185.     t_err=read_colormap_bmp(p_inputparam,p_imageinfo->m_colormap, (int16) biClrUsed, mapentrysize);
  186.     if (t_err!=CONV_OK)
  187.         return t_err;
  188.     p_imageinfo->m_image_size+=biClrUsed*mapentrysize;/*mapentrysize for windows bitmaps*/ 
  189.     /* account for size of colormap */
  190.     bPad -= biClrUsed * mapentrysize;
  191.   }
  192.  
  193.   if (CONVERT_FILE==p_inputparam->m_stream.m_type)/* Skip any remaining pad bytes */
  194.   {
  195.     if (bPad < 0)            /* incorrect bfOffBits value? */
  196.         return CONVERR_INVALIDIMAGEHEADER;
  197.     while (--bPad >= 0) 
  198.     {
  199.         (void) read_param_byte(&p_inputparam->m_stream);
  200.         p_imageinfo->m_image_size++;
  201.     }
  202.   }
  203.   /* Compute row width in file, including padding to 4-byte boundary */
  204.   switch (p_imageinfo->m_bitsperpixel)
  205.   {
  206.   case 1:
  207.       row_width = biWidth/8;
  208.       if (biWidth&7)
  209.           row_width++;
  210.       break;
  211.   case 4:
  212.       row_width = biWidth/2;
  213.       if (biWidth&1)
  214.           row_width++;
  215.       break;
  216.   case 8:
  217.     row_width = (uint16) biWidth;
  218.     break;
  219.   case 16:
  220.     row_width = (uint16) (biWidth * 2);
  221.     break;
  222.   case 24:
  223.     row_width = (uint16) (biWidth * 3);
  224.     break;
  225.   case 32:
  226.     row_width = (uint16) (biWidth * 4);
  227.     break;
  228.   default:
  229.       return CONVERR_INVALIDBITDEPTH;
  230.   }
  231.   p_imageinfo->m_stride=row_width;
  232.   while ((row_width & 3) != 0) 
  233.     row_width++;
  234.   p_imageinfo->m_row_width = row_width;
  235.   p_imageinfo->m_stride=p_imageinfo->m_row_width-p_imageinfo->m_stride;//difference
  236.  
  237.   /* p_imageinfo->m_in_color_space = JCS_RGB;*/ 
  238.   /* in color space is ignored. was only for JPEG PAL format*/
  239.   p_imageinfo->m_input_components = 3;
  240.   p_imageinfo->m_data_precision = 8;
  241.   p_imageinfo->m_image_width = (uint16) biWidth;
  242.   p_imageinfo->m_image_height = (uint16) biHeight;
  243.   p_imageinfo->m_numcolorentries=(short)biClrUsed;
  244.   return CONV_OK;
  245. }
  246. ///////////////////////////////////
  247. ///////END START READ BMP//////////
  248. ///////////////////////////////////
  249.  
  250. ///////////////////////////////////
  251. ///////COLOR MAP READ//////////////
  252. ///////////////////////////////////
  253.  
  254. CONVERT_IMAGERESULT
  255. read_colormap_bmp (CONVERT_IMGCONTEXT *p_inputparam, BYTE *p_colormap,int16 cmaplen, int16 mapentrysize)
  256. /* Read the colormap from a BMP file */
  257. {
  258.   int16 i;
  259.   
  260.   switch (mapentrysize) {
  261.   case 3:
  262.     /* BGR format (occurs in OS/2 files) */
  263.     for (i = 0; i < cmaplen; i++) {
  264.       p_colormap[i*3+2]=read_param_byte(&p_inputparam->m_stream);//file or mem doesnt matter 
  265.       p_colormap[i*3+1]=read_param_byte(&p_inputparam->m_stream);//file or mem doesnt matter 
  266.       p_colormap[i*3+0]=read_param_byte(&p_inputparam->m_stream);//file or mem doesnt matter 
  267.     }
  268.     break;
  269.   case 4:
  270.     /* BGR0 format (occurs in MS Windows files) */
  271.     for (i = 0; i < cmaplen; i++) {
  272.       p_colormap[i*3+2]=read_param_byte(&p_inputparam->m_stream);//file or mem doesnt matter 
  273.       p_colormap[i*3+1]=read_param_byte(&p_inputparam->m_stream);//file or mem doesnt matter 
  274.       p_colormap[i*3+0]=read_param_byte(&p_inputparam->m_stream);//file or mem doesnt matter 
  275.       (void) read_param_byte(&p_inputparam->m_stream);
  276.     }
  277.     break;
  278.   default:
  279.     XP_ASSERT(FALSE);
  280.     return CONVERR_INVALIDCOLORMAP;
  281.     break;
  282.   }
  283.   return CONV_OK;
  284. }
  285.  
  286. ///////////////////////////////END COLORMAP READ
  287.  
  288.  
  289.  
  290.  
  291. CONVERT_IMAGERESULT
  292. finish_input_bmp(CONVERT_IMG_INFO *p_imageinfo, CONVERT_IMGCONTEXT *p_input,CONVERT_IMG_ARRAY *p_returnarray)
  293. {
  294.   int16 i;
  295.   CONVERT_IMG_ARRAY t_returnarray;
  296.   int16 t_absheight=p_imageinfo->m_image_height;
  297.   int16 t_start,t_end;
  298.   int16 t_direction;
  299.   if (t_absheight<0) /* negative height == top down */
  300.   {
  301.     t_absheight*= -1;
  302.     t_start=0;
  303.     t_end=t_absheight;
  304.     t_direction= 1;
  305.   }
  306.   else
  307.   {
  308.     t_start =t_absheight-1;
  309.     t_end= -1;
  310.     t_direction= -1;
  311.   }
  312.   t_returnarray=(CONVERT_IMG_ARRAY)malloc(t_absheight*sizeof (CONVERT_IMG_ROW));
  313.   if (!t_returnarray) /*out of memory*/
  314.     return CONVERR_OUTOFMEMORY;
  315.   *p_returnarray=t_returnarray;
  316.   memset(t_returnarray,0,t_absheight*sizeof (CONVERT_IMG_ROW *));
  317.   /* want to initialize the memory to 0 so in case of error we can free each row.*/
  318.  
  319.   XP_ASSERT(p_imageinfo);
  320.   XP_ASSERT(p_input);
  321.   for (i=t_start;i!=t_end;i+=t_direction)
  322.   {
  323.     t_returnarray[i]=(CONVERT_IMG_ROW)malloc(p_imageinfo->m_image_width*3); /* *3 because RGB */;    /* pointer to JSAMPLE row[s] */
  324.     if (!t_returnarray[i])
  325.     {
  326.       return CONVERR_BADREAD;
  327.     }
  328.     switch (p_imageinfo->m_bitsperpixel)
  329.     {
  330.     case 1:
  331.       if (!get_1bit_row_bmp(p_input,p_imageinfo,t_returnarray[i]))/*true if succeeded*/
  332.       {
  333.         return CONVERR_BADREAD; 
  334.       }
  335.       break;
  336.     case 4:
  337.       if (!get_4bit_row_bmp(p_input,p_imageinfo,t_returnarray[i]))/*true if succeeded*/
  338.       {
  339.         return CONVERR_BADREAD; 
  340.       }
  341.       break;
  342.     case 8:
  343.       if (!get_8bit_row_bmp(p_input,p_imageinfo,t_returnarray[i]))/*true if succeeded*/
  344.       {
  345.         return CONVERR_BADREAD; 
  346.       }
  347.       break;
  348.     case 16:
  349.       if (!get_16bit_row_bmp(p_input,p_imageinfo,t_returnarray[i]))/*true if succeeded*/
  350.       {
  351.         return CONVERR_BADREAD; 
  352.       }
  353.       break;
  354.     case 24:
  355.       if (!get_24bit_row_bmp(p_input,p_imageinfo,t_returnarray[i]))/*true if succeeded*/
  356.       {
  357.         return CONVERR_BADREAD; 
  358.       }
  359.       break;
  360.     case 32:
  361.       if (!get_32bit_row_bmp(p_input,p_imageinfo,t_returnarray[i]))/*true if succeeded*/
  362.       {
  363.         return CONVERR_BADREAD; 
  364.       }
  365.       break;
  366.     default:
  367.       {
  368.         XP_ASSERT(FALSE); /* what the heck happened!*/
  369.         return CONVERR_INVALIDBITDEPTH; 
  370.       }
  371.     }  
  372.   }
  373.   return CONV_OK;
  374. }
  375.  
  376.  
  377.  
  378. XP_Bool
  379. get_8bit_row_bmp (CONVERT_IMGCONTEXT *p_params,CONVERT_IMG_INFO *p_info,CONVERT_IMG_ROW p_outputbuffer)
  380. /* This version is for reading 8-bit colormap indexes */
  381. {
  382.   CONVERT_IMG_ROW outptr = p_outputbuffer;
  383.   BYTE t_byte;
  384.   int16 col;
  385.   if (!p_info->m_colormap||!p_outputbuffer||!p_params||!p_info)
  386.   {
  387.     XP_ASSERT(FALSE);
  388.     return FALSE;
  389.   }
  390.   for (col = p_info->m_image_width; col > 0; col--) 
  391.   {
  392.     t_byte = read_param_byte(&p_params->m_stream);
  393.     p_info->m_image_size++;
  394.     if (EOF==t_byte)
  395.       return FALSE;
  396.     if (t_byte>p_info->m_numcolorentries)/*bad index*/
  397.       return FALSE;
  398.     *outptr++ = p_info->m_colormap[0+t_byte*3];    /* can omit GETJSAMPLE() safely */
  399.     if (EOF==t_byte)
  400.       return FALSE;
  401.     *outptr++ = p_info->m_colormap[1+t_byte*3];
  402.     if (EOF==t_byte)
  403.       return FALSE;
  404.     *outptr++ = p_info->m_colormap[2+t_byte*3];
  405.   }
  406.   for (col=0;col<p_info->m_stride;col++) /*read to 4byte multiple*/
  407.   {
  408.     t_byte = read_param_byte(&p_params->m_stream);
  409.     p_info->m_image_size++;
  410.     if (EOF==t_byte)
  411.       return FALSE;
  412.   }
  413.   return TRUE;
  414. }
  415.  
  416. void process_color_mask(uint32 *p_mask,char *p_shift,uint16 *p_pad)
  417. {
  418.   uint32 t_mask=*p_mask;
  419.   *p_shift=0;
  420.   *p_pad=0;
  421.   if (!p_mask||!p_shift||!p_pad)
  422.   {
  423.     XP_ASSERT(FALSE);
  424.     return;
  425.   }
  426.   while (!(t_mask&1))
  427.   {
  428.     (*p_shift)+=1;
  429.     t_mask=t_mask>>1;
  430.   }
  431.   while (t_mask&256) /*larger than 8bp color key i.e. 10-3-3*/
  432.     (*p_shift)+=1;
  433.   while (t_mask<128)/* dont know where the mask is that is why we dont put this in the same loop*/
  434.   {
  435.     (*p_shift)--;
  436.     t_mask=t_mask<<1;
  437.     (*p_pad)=(*p_pad)<<1;
  438.     (*p_pad)++;
  439.   }
  440. }
  441.  
  442.  
  443.  
  444. XP_Bool
  445. get_16bit_row_bmp (CONVERT_IMGCONTEXT *p_params,CONVERT_IMG_INFO *p_info,CONVERT_IMG_ROW p_outputbuffer)
  446. /* This version is for reading 8-bit colormap indexes */
  447. {
  448.  
  449.   CONVERT_IMG_ROW outptr = p_outputbuffer;
  450.   BYTE t_byte;
  451.   int16 col;
  452.   uint32 t_redmask=0x00007C00;
  453.   uint32 t_greenmask=0x000003E0;
  454.   uint32 t_bluemask=0x0000001F;
  455.   char t_redshift=10;
  456.   char t_greenshift=5;
  457.   char t_blueshift=0;
  458.   uint16 t_redpad=0;
  459.   uint16 t_greenpad=0;
  460.   uint16 t_bluepad=0;
  461.  
  462.   if (!p_outputbuffer||!p_params||!p_info)
  463.   {
  464.     XP_ASSERT(FALSE);
  465.     return FALSE;
  466.   }
  467.   if (p_info->m_numcolorentries==3)
  468.   {
  469.     if (!p_info->m_colormap)
  470.     {
  471.       XP_ASSERT(FALSE);
  472.       return FALSE;
  473.     }
  474.     t_redmask=(p_info->m_colormap[0]<<16)+(p_info->m_colormap[1]<<8)+p_info->m_colormap[2];
  475.     t_greenmask=(p_info->m_colormap[3]<<16)+(p_info->m_colormap[4]<<8)+p_info->m_colormap[5];
  476.     t_bluemask=(p_info->m_colormap[6]<<16)+(p_info->m_colormap[7]<<8)+p_info->m_colormap[8];
  477.     process_color_mask(&t_redmask,&t_redshift,&t_redpad);
  478.     process_color_mask(&t_greenmask,&t_greenshift,&t_greenpad);
  479.     process_color_mask(&t_bluemask,&t_blueshift,&t_bluepad);
  480.   }
  481.   for (col = p_info->m_image_width; col > 0; col--) 
  482.   {
  483.     uint16 t_pixel;/*16bits*/
  484.     uint16 t_temppixel;
  485.     if (-1==read_param(&p_params->m_stream,&t_pixel,sizeof(t_pixel)))
  486.       return FALSE;
  487.     p_info->m_image_size+=sizeof(t_pixel);
  488.     t_temppixel=(uint16)(t_redmask&t_pixel);
  489.     if (t_redshift>0)
  490.       t_temppixel=t_temppixel>>t_redshift;
  491.     else
  492.       t_temppixel=t_temppixel<<(-1 *t_redshift);
  493.     t_temppixel=t_temppixel+t_redpad;
  494.     outptr[0]=(BYTE)t_temppixel;
  495.     t_temppixel=(uint16)(t_greenmask&t_pixel);
  496.     if (t_greenshift>0)
  497.       t_temppixel=t_temppixel>>t_greenshift;
  498.     else
  499.       t_temppixel=t_temppixel<<(-1 *t_greenshift);
  500.     t_temppixel=t_temppixel+t_greenpad;
  501.     outptr[1]=(BYTE)t_temppixel;
  502.     t_temppixel=(uint16)(t_bluemask&t_pixel);
  503.     if (t_blueshift>0)
  504.       t_temppixel=t_temppixel>>t_blueshift;
  505.     else
  506.       t_temppixel=t_temppixel<<(-1 *t_blueshift);
  507.     t_temppixel=t_temppixel+t_bluepad;
  508.     outptr[2]=(BYTE)t_temppixel;
  509.     outptr+=3;
  510.   }
  511.   for (col=0;col<p_info->m_stride;col++) /*read to 4byte multiple*/
  512.   {
  513.     t_byte = read_param_byte(&p_params->m_stream);
  514.     p_info->m_image_size++;
  515.     if (EOF==t_byte)
  516.       return FALSE;
  517.   }
  518.   return TRUE;
  519. }
  520.  
  521.  
  522.  
  523. XP_Bool
  524. get_32bit_row_bmp (CONVERT_IMGCONTEXT *p_params,CONVERT_IMG_INFO *p_info,CONVERT_IMG_ROW p_outputbuffer)
  525. /* This version is for reading 8-bit colormap indexes */
  526. {
  527.  
  528.   CONVERT_IMG_ROW outptr = p_outputbuffer;
  529.   BYTE t_byte;
  530.   int16 col;
  531.   uint32 t_redmask=  0x00FF0000;/*RGB(8,8,8)*/
  532.   uint32 t_greenmask=0x0000FF00;
  533.   uint32 t_bluemask= 0x000000FF;
  534.   BYTE t_redshift=16;
  535.   BYTE t_greenshift=8;
  536.   BYTE t_blueshift=0;
  537.   uint16 t_redpad=0;
  538.   uint16 t_greenpad=0;
  539.   uint16 t_bluepad=0;
  540.   if (!p_outputbuffer||!p_params||!p_info)
  541.   {
  542.     XP_ASSERT(FALSE);
  543.     return FALSE;
  544.   }
  545.   if (p_info->m_numcolorentries==3)
  546.   {
  547.     if (!p_info->m_colormap)
  548.     {
  549.       XP_ASSERT(FALSE);
  550.       return FALSE;
  551.     }
  552.     t_redmask=(p_info->m_colormap[0]<<16)+(p_info->m_colormap[1]<<8)+p_info->m_colormap[2];
  553.     t_greenmask=(p_info->m_colormap[3]<<16)+(p_info->m_colormap[4]<<8)+p_info->m_colormap[5];
  554.     t_bluemask=(p_info->m_colormap[6]<<16)+(p_info->m_colormap[7]<<8)+p_info->m_colormap[8];
  555.     process_color_mask(&t_redmask,&t_redshift,&t_redpad);
  556.     process_color_mask(&t_greenmask,&t_greenshift,&t_greenpad);
  557.     process_color_mask(&t_bluemask,&t_blueshift,&t_bluepad);
  558.   }
  559.  
  560.   for (col = p_info->m_image_width; col > 0; col--) 
  561.   {
  562.     uint32 t_pixel;
  563.     if (-1==read_param(&p_params->m_stream,&t_pixel,sizeof(t_pixel)))
  564.       return FALSE;
  565.     p_info->m_image_size+=sizeof(t_pixel);
  566.     if (t_redshift>0)
  567.       outptr[0]=(BYTE)((t_redmask&t_pixel)>>t_redshift);
  568.     else
  569.       outptr[0]=(BYTE)((t_redmask&t_pixel)<<(-1*t_redshift));
  570.     outptr[0]+=t_redpad;
  571.     if (t_greenshift>0)
  572.       outptr[1]=(BYTE)((t_greenmask&t_pixel)>>t_greenshift);
  573.     else
  574.       outptr[1]=(BYTE)((t_greenmask&t_pixel)<<(-1*t_greenshift));
  575.     outptr[1]+=t_greenpad;
  576.     if (t_blueshift>0)
  577.       outptr[2]=(BYTE)((t_bluemask&t_pixel)>>t_blueshift);
  578.     else
  579.       outptr[2]=(BYTE)((t_bluemask&t_pixel)<<(-1*t_blueshift));
  580.     outptr[2]+=t_bluepad;
  581.     outptr+=3;
  582.   }
  583.   for (col=0;col<p_info->m_stride;col++) /*read to 4byte multiple*/
  584.   {
  585.     t_byte = read_param_byte(&p_params->m_stream);
  586.     p_info->m_image_size++;
  587.     if (EOF==t_byte)
  588.       return FALSE;
  589.   }
  590.   return TRUE;
  591. }
  592.  
  593.  
  594.  
  595. XP_Bool
  596. get_4bit_row_bmp (CONVERT_IMGCONTEXT *p_params,CONVERT_IMG_INFO *p_info,CONVERT_IMG_ROW p_outputbuffer)
  597. /* This version is for reading 8-bit colormap indexes */
  598. {
  599.   CONVERT_IMG_ROW outptr = p_outputbuffer;
  600.   BYTE t_byte;
  601.   BYTE t_value; /*4bitvalue for index into color table*/
  602.   int16 col;
  603.   if (!p_info->m_colormap||!p_outputbuffer||!p_params||!p_info)
  604.   {
  605.     XP_ASSERT(FALSE);
  606.     return FALSE;
  607.   }
  608.   for (col = 0; col <p_info->m_image_width; col++) 
  609.   {
  610.     if (0==(col%2))
  611.     {    t_byte = read_param_byte(&p_params->m_stream);
  612.     p_info->m_image_size++;
  613.     }
  614.     t_value = (t_byte>>4)& 0x0F; /*high order 4 bits, shift right, mask high order crap*/
  615.     if (EOF==t_byte)
  616.       return FALSE;
  617.     if (t_value>p_info->m_numcolorentries)/*bad index*/
  618.       return FALSE;
  619.     *outptr++ = p_info->m_colormap[0+t_value*3];    /* can omit GETJSAMPLE() safely */
  620.     if (EOF==t_byte)
  621.       return FALSE;
  622.     *outptr++ = p_info->m_colormap[1+t_value*3];
  623.     if (EOF==t_byte)
  624.       return FALSE;
  625.     *outptr++ = p_info->m_colormap[2+t_value*3];
  626.     t_byte<<=4; /*next pixel*/
  627.   }
  628.   for (col=0;col<p_info->m_stride;col++) /*read to 4byte multiple*/
  629.   {
  630.     t_byte = read_param_byte(&p_params->m_stream);
  631.     p_info->m_image_size++;
  632.     if (EOF==t_byte)
  633.       return FALSE;
  634.   }
  635.   return TRUE;
  636. }
  637.  
  638.  
  639.  
  640. XP_Bool
  641. get_1bit_row_bmp (CONVERT_IMGCONTEXT *p_params,CONVERT_IMG_INFO *p_info,CONVERT_IMG_ROW p_outputbuffer)
  642. /* This version is for reading 8-bit colormap indexes */
  643. {
  644.   CONVERT_IMG_ROW outptr = p_outputbuffer;
  645.   BYTE t_byte;
  646.   int16 col,bit;
  647.   if (!p_info->m_colormap||!p_outputbuffer||!p_params||!p_info)
  648.   {
  649.     XP_ASSERT(FALSE);
  650.     return FALSE;
  651.   }
  652.   if (!p_outputbuffer)
  653.     return FALSE;
  654.   for (bit=0;bit<p_info->m_image_width;bit++)
  655.   {
  656.     if (0==(bit%8))
  657.     {
  658.       t_byte = read_param_byte(&p_params->m_stream);
  659.       p_info->m_image_size++;
  660.     }
  661.     //start from left by & with 128 then shift one at a time
  662.     if (t_byte&128)
  663.     {
  664.       *outptr++ = p_info->m_colormap[3];
  665.       *outptr++ = p_info->m_colormap[4];
  666.       *outptr++ = p_info->m_colormap[5];
  667.     }
  668.     else
  669.     {
  670.       *outptr++ = p_info->m_colormap[0];
  671.       *outptr++ = p_info->m_colormap[1];
  672.       *outptr++ = p_info->m_colormap[2];
  673.     }
  674.     t_byte<<=1;
  675.   }
  676.   for (col=0;col<p_info->m_stride;col++) /*read to 4byte multiple*/
  677.   {
  678.     t_byte = read_param_byte(&p_params->m_stream);
  679.     p_info->m_image_size++;
  680.     if (EOF==t_byte)
  681.       return FALSE;
  682.   }
  683.   return TRUE;
  684. }
  685.  
  686.  
  687.  
  688. XP_Bool
  689. get_24bit_row_bmp (CONVERT_IMGCONTEXT *p_params,CONVERT_IMG_INFO *p_info,CONVERT_IMG_ROW p_outputbuffer)
  690. /* This version is for reading 24-bit pixels */
  691. {
  692.   CONVERT_IMG_ROW outptr = p_outputbuffer;
  693.   BYTE t_byte;
  694.   int16 col;
  695.   if (!p_outputbuffer||!p_params||!p_info)
  696.   {
  697.     XP_ASSERT(FALSE);
  698.     return FALSE;
  699.   }
  700.   for (col = p_info->m_image_width; col > 0; col--) 
  701.   {
  702.     t_byte = read_param_byte(&p_params->m_stream);
  703.     p_info->m_image_size++;
  704.     if (EOF==t_byte)
  705.       return FALSE;
  706.     outptr[2] =t_byte;
  707.     t_byte = read_param_byte(&p_params->m_stream);
  708.     p_info->m_image_size++;
  709.     if (EOF==t_byte)
  710.       return FALSE;
  711.     outptr[1] =t_byte;
  712.     t_byte = read_param_byte(&p_params->m_stream);
  713.     p_info->m_image_size++;
  714.     if (EOF==t_byte)
  715.       return FALSE;
  716.     outptr[0] =t_byte;
  717.     outptr+=3;/*next RGB*/
  718.   }
  719.   for (col=0;col<p_info->m_stride;col++) /*read to 4byte multiple*/
  720.   {
  721.     t_byte = read_param_byte(&p_params->m_stream);
  722.     p_info->m_image_size++;
  723.     if (EOF==t_byte)
  724.       return FALSE;
  725.   }
  726.   return TRUE;
  727. }
  728.  
  729.  
  730.  
  731.  
  732. ///////////////////////////////////////////////
  733. ////////////////////END READ///////////////////
  734. ///////////////////////////////////////////////
  735.