image/imgbmp.cpp

Go to the documentation of this file.
00001 
00020 #include          "mfcpch.h"     //precompiled headers
00021 #ifdef __MSW32__
00022 #include          <io.h>
00023 #else
00024 #include          <unistd.h>
00025 #endif
00026 #include          <stdio.h>
00027 #include          "img.h"
00028 #include          "imgbmp.h"
00029 
00034 typedef struct
00035 {                                // bmfh
00036   char bfType1;                  //'B'
00037   char bfType2;                  //'M'
00038 } BMPHEADER0;
00039 
00044 typedef struct
00045 {                                // bmfh
00046   UINT32 bfSize;                 //filesize
00047   UINT16 bfReserved1;            //zero
00048   UINT16 bfReserved2;            //zero
00049   UINT32 bfOffBits;              //offset to bitmap
00050 } BMPHEADER;
00051 
00056 typedef struct
00057 {                                // bmih
00058   UINT32 biSize;                 //size of struct
00059   INT32 biWidth;                 //image width
00060   INT32 biHeight;                //image height
00061   UINT16 biPlanes;               //1
00062   UINT16 biBitCount;             //bpp
00063   UINT32 biCompression;          //0 for uncompressed
00064   UINT32 biSizeImage;            //image size
00065   INT32 biXPelsPerMeter;         //res in pp metre
00066   INT32 biYPelsPerMeter;
00067   UINT32 biClrUsed;              //0 or actual size of colour table
00068   UINT32 biClrImportant;         //usually 0
00069 } BMPHEADER2;
00070 
00075 typedef struct
00076 {                                // rgbq
00077   UINT8 rgbBlue;
00078   UINT8 rgbGreen;
00079   UINT8 rgbRed;
00080   UINT8 rgbReserved;             //0
00081 } WIN32_RGBQUAD;
00082 
00086 INT8 open_bmp_image(               //read header
00087                     int fd,        //file to read
00088                     INT32 *xsize,  //size of image
00089                     INT32 *ysize,
00090                     INT8 *bpp,     //bits per pixel
00091                     INT8 *photo,
00092                     INT32 *res     //resolution
00093                    ) {
00094   UINT32 nread;                  //current bits
00095   BMPHEADER0 head0;              //first part of header
00096   BMPHEADER head1;               //first part of header
00097   BMPHEADER2 head2;              //first part of header
00098 
00099   *photo = 1;
00100   nread = read (fd, &head0, sizeof (head0));
00101   if (nread != sizeof (head0))
00102     return -1;
00103   nread = read (fd, &head1, sizeof (head1));
00104   if (nread != sizeof (head1))
00105     return -1;
00106   nread = read (fd, &head2, sizeof (head2));
00107   if (nread != sizeof (head2))
00108     return -1;
00109 
00110   if (head0.bfType1 != 'B')
00111     return -1;
00112   if (head0.bfType2 != 'M')
00113     return -1;
00114   lseek (fd, head1.bfOffBits, SEEK_SET);
00115   *bpp = head2.biBitCount;
00116   *xsize = head2.biWidth;
00117   *ysize = head2.biHeight;
00118   *res = 300;                    //make up resolution
00119   return -2;                     //success
00120 }
00121 
00122 
00126 INT8 read_bmp_image(                //read header
00127                     int fd,         //file to read
00128                     UINT8 *pixels,  //pixels of image
00129                     INT32 xsize,    //size of image
00130                     INT32 ysize,
00131                     INT8 bpp,       //bits per pixel
00132                     INT32           //bytes per line
00133                    ) {
00134   UINT32 bpl;                    //bytes per line
00135   UINT32 wpl;                    //words per line
00136   UINT32 nread;                  //current bits
00137   INT32 index;                   //to cols
00138 
00139   bpl = (xsize * bpp + 7) / 8;   //bytes per line
00140   wpl = (bpl + 3) / 4;
00141   wpl *= 4;
00142   for (index = 0; index < ysize; index++) {
00143     nread = read (fd, pixels + bpl * (ysize - 1 - index), bpl);
00144     if (nread != bpl)
00145       return -1;
00146     if (wpl != bpl)
00147       lseek (fd, wpl - bpl, SEEK_CUR);
00148   }
00149   return 0;
00150 }
00151 
00152 
00156 INT8 write_bmp_image(                //write whole image
00157                      int fd,         //file to write on
00158                      UINT8 *pixels,  //image pixels
00159                      INT32 xsize,    //size of image
00160                      INT32 ysize,
00161                      INT8 bpp,       //bits per pixel
00162                      INT8,
00163                      INT32 res       //resolution
00164                     ) {
00165   UINT32 bpl;                    //bytes per line
00166   UINT32 wpl;                    //words per line
00167   UINT32 nread;                  //current bits
00168   INT32 cols;                    //entries in table
00169   INT32 index;                   //to cols
00170   BMPHEADER0 head0;              //first part of header
00171   BMPHEADER head1;               //first part of header
00172   BMPHEADER2 head2;              //first part of header
00173   WIN32_RGBQUAD coltab[256];     //colour table
00174 
00175   if (bpp == 24)
00176     cols = 0;
00177   else
00178     cols = 1 << bpp;             //size of colour table
00179   bpl = (xsize * bpp + 7) / 8;   //bytes per line
00180   wpl = (bpl + 3) / 4;
00181 
00182   head2.biSize = sizeof (head2); //size of struct
00183   head2.biWidth = xsize;         //image width
00184   head2.biHeight = ysize;        //image height
00185   head2.biPlanes = 1;            //1
00186   head2.biBitCount = bpp;        //bpp
00187   head2.biCompression = 0;       //0 for uncompressed
00188                                  //image size
00189   head2.biSizeImage = wpl * 4 * ysize;
00190                                  //res in pp metre
00191   head2.biXPelsPerMeter = (UINT32) (res * 39.37);
00192   head2.biYPelsPerMeter = (UINT32) (res * 39.37);
00193   head2.biClrUsed = cols;        //0 or actual size of colour table
00194   head2.biClrImportant = 0;      //usually 0
00195 
00196   head0.bfType1 = 'B';
00197   head0.bfType2 = 'M';
00198   head1.bfReserved1 = 0;         //zero
00199   head1.bfReserved2 = 0;         //zero
00200                                  //offset to bitmap
00201   head1.bfOffBits = sizeof (head0) + sizeof (head1) +
00202    sizeof (head2) + sizeof (WIN32_RGBQUAD) * cols;
00203                                  //filesize
00204   head1.bfSize = head1.bfOffBits + head2.biSizeImage;
00205 
00206   for (index = 0; index < cols; index++) {
00207     coltab[index].rgbBlue = index * 255 / (cols - 1);
00208     coltab[index].rgbGreen = coltab[index].rgbBlue;
00209     coltab[index].rgbRed = coltab[index].rgbBlue;
00210     coltab[index].rgbReserved = 0;
00211   }
00212 
00213   nread = write (fd, &head0, sizeof (head0));
00214   if (nread != sizeof (head0))
00215     return -1;
00216   nread = write (fd, &head1, sizeof (head1));
00217   if (nread != sizeof (head1))
00218     return -1;
00219   nread = write (fd, &head2, sizeof (head2));
00220   if (nread != sizeof (head2))
00221     return -1;
00222   nread = write (fd, coltab, cols * sizeof (WIN32_RGBQUAD));
00223   if (nread != cols * sizeof (WIN32_RGBQUAD))
00224     return -1;
00225   for (index = 0; index < ysize; index++) {
00226     nread = write (fd, pixels + bpl * (ysize - 1 - index), wpl * 4);
00227     if (nread != wpl * 4)
00228       return -1;
00229   }
00230   close(fd);  //done it
00231   return 0;
00232 }

Generated on Wed Feb 28 19:49:11 2007 for Tesseract by  doxygen 1.5.1