Martin_Berger

Test_ifstream_read

Jul 27th, 2011
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.86 KB | None | 0 0
  1. #include <windows.h>   // include important windows stuff  
  2. #include <windowsx.h>    
  3. #include <mmsystem.h>  
  4. #include <iostream> // include important C/C++ stuff  
  5. #include <conio.h>  
  6. #include <stdlib.h>  
  7. #include <malloc.h>  
  8. #include <memory.h>  
  9. #include <cstring>  
  10. #include <stdarg.h>  
  11. #include <stdio.h>    
  12. #include <math.h>  
  13. #include <io.h>  
  14. #include <fcntl.h>  
  15. #include <fstream>
  16.  
  17. using namespace std;
  18.  
  19.  
  20.  
  21. // DEFINES ////////////////////////////////////////////////  
  22.  
  23. // defines for windows    
  24. #define WINDOW_CLASS_NAME L"WINCLASS1"  
  25.  
  26. // default screen size  
  27. #define SCREEN_WIDTH    1024  // size of screen  
  28. #define SCREEN_HEIGHT   768  
  29. #define SCREEN_BPP      16   // bits per pixel  
  30.  
  31. #define BITMAP_ID            0x4D42 // universal id for a bitmap  
  32. #define MAX_COLORS_PALETTE   256  
  33.  
  34.  
  35.  
  36. // container structure for bitmaps .BMP file  
  37. typedef struct BITMAP_FILE_TAG  
  38. {  
  39.     BITMAPFILEHEADER bitmapfileheader;  // this contains the bitmapfile header  
  40.     BITMAPINFOHEADER bitmapinfoheader;  // this is all the info including the palette  
  41.     PALETTEENTRY     palette[256];      // we will store the palette here  
  42.     UCHAR            *buffer;           // this is a pointer to the data  
  43.  
  44. } BITMAP_FILE, *BITMAP_FILE_PTR;  
  45.  
  46. BITMAP_FILE bitmap;                // holds the bitmap  
  47.  
  48.  
  49.  
  50. // FUNCTIONS ////////////////////////////////////////////////  
  51.  
  52. void printError (char * errorName)
  53. {
  54.     // print error to log.txt
  55.     ofstream errorFile = ofstream ("log.txt");
  56.     if (errorFile.is_open ())
  57.     {
  58.         errorFile << errorName << endl; //"Error: OpenFile function failure. "
  59.     }
  60.     errorFile.close ();
  61. }
  62.  
  63.  
  64. int Flip_Bitmap(UCHAR *image, int bytes_per_line, int height)  
  65. {  
  66.     // this function is used to flip bottom-up .BMP images  
  67.  
  68.     UCHAR *buffer; // used to perform the image processing  
  69.     int index;     // looping index  
  70.  
  71.     // allocate the temporary buffer  
  72.     if (!(buffer = (UCHAR *) malloc (bytes_per_line * height)))  
  73.         return(0);  
  74.  
  75.     // copy image to work area  
  76.     //memcpy(buffer, image, bytes_per_line * height);  
  77.     memcpy_s (buffer, bytes_per_line * height, image, bytes_per_line * height);
  78.  
  79.     // flip vertically  
  80.     for (index = 0; index < height; index++)  
  81.         memcpy(&image[((height - 1) - index) * bytes_per_line], &buffer[index * bytes_per_line], bytes_per_line);  
  82.  
  83.     // release the memory  
  84.     free(buffer);  
  85.  
  86.     // return success  
  87.     return(1);  
  88.  
  89. } // end Flip_Bitmap  
  90.  
  91. ///////////////////////////////////////////////////////////////
  92.  
  93.  
  94. int Load_Bitmap_File(BITMAP_FILE_PTR bitmap, char *filename)  
  95. {  
  96.     int file_handle = 0;        // the file handle
  97.     int index;                  // looping index  
  98.     UCHAR *temp_buffer = NULL; // used to convert 24 bit images to 16 bit  
  99.     streampos pos_cur;
  100.  
  101.     ifstream bitmapFile = ifstream ();
  102.     bitmapFile.open (filename, ifstream::in);
  103.     if (! bitmapFile.is_open ())
  104.     {
  105.         // print error to log.txt
  106.         ofstream errorFile = ofstream ("log.txt");
  107.         if (! errorFile.is_open ())
  108.         {
  109.             errorFile << "Error: OpenFile function failure. " << endl;
  110.         }
  111.         errorFile.close ();
  112.  
  113.         // abort
  114.         return(0);
  115.     }
  116.  
  117.  
  118.     // load the bitmap file header:  
  119.     //_lread(file_handle, &(bitmap->bitmapfileheader), sizeof(BITMAPFILEHEADER));
  120.     bitmapFile.read ((char *) &(bitmap->bitmapfileheader), sizeof (BITMAPFILEHEADER));
  121.  
  122.     // test if this is a bitmap file  
  123.     if (bitmap->bitmapfileheader.bfType != BITMAP_ID)  
  124.     {  
  125.         // close the file  
  126.         //_lclose(file_handle);  
  127.         bitmapFile.close ();
  128.  
  129.         printError ("error: wrong bitmap type");
  130.         cout << "error: wrong bitmap type" << endl;
  131.  
  132.         // return error  
  133.         return(0);  
  134.     } // end if  
  135.  
  136.     // now we know this is a bitmap, so read in all the sections.  
  137.  
  138.  
  139.     // now the bitmap infoheader:  
  140.  
  141.     //_lread(file_handle, &bitmap->bitmapinfoheader, sizeof(BITMAPINFOHEADER));  
  142.     bitmapFile.seekg (sizeof (BITMAPFILEHEADER), ios::beg);
  143.     pos_cur = bitmapFile.tellg (); // save current stream position
  144.     bitmapFile.read ((char *) &(bitmap->bitmapinfoheader), sizeof (BITMAPINFOHEADER));
  145.  
  146.     //cout << bitmap->bitmapinfoheader.biBitCount << endl;
  147.  
  148.     // now load the color palette if there is one  
  149.     if (bitmap->bitmapinfoheader.biBitCount == 8)  
  150.     {  
  151.         //_lread(file_handle, &bitmap->palette, MAX_COLORS_PALETTE * sizeof(PALETTEENTRY));  
  152.         // not tested:
  153.         bitmapFile.read ((char *) &(bitmap->palette), MAX_COLORS_PALETTE * sizeof(PALETTEENTRY));
  154.  
  155.         // now set all the flags in the palette correctly and fix the reversed    
  156.         // BGR RGBQUAD data format  
  157.         for (index = 0; index < MAX_COLORS_PALETTE; index++)  
  158.         {  
  159.             // reverse the red and green fields  
  160.             int temp_color                = bitmap->palette[index].peRed;  
  161.             bitmap->palette[index].peRed  = bitmap->palette[index].peBlue;  
  162.             bitmap->palette[index].peBlue = temp_color;  
  163.  
  164.             // always set the flags word to this  
  165.             bitmap->palette[index].peFlags = PC_NOCOLLAPSE;  
  166.         } // end for index  
  167.  
  168.     } // end if  
  169.    
  170.  
  171.     // finally the image data itself:  
  172.     //_lseek(file_handle, -((int) (bitmap->bitmapinfoheader.biSizeImage)), SEEK_END);
  173.     bitmapFile.seekg (-((int) bitmap->bitmapinfoheader.biSizeImage), ios::end);
  174.     //bitmapFile.seekg (sizeof (BITMAPINFOHEADER) + sizeof (BITMAPFILEHEADER) + MAX_COLORS_PALETTE * sizeof(PALETTEENTRY), ios::beg);
  175.  
  176.     // now read in the image, if the image is 8 or 16 bit then simply read it  
  177.     // but if its 24 bit then read it into a temporary area and then convert  
  178.     // it to a 16 bit image  
  179.  
  180.     if (bitmap->bitmapinfoheader.biBitCount == 8  ||
  181.          bitmap->bitmapinfoheader.biBitCount == 16 ||    
  182.          bitmap->bitmapinfoheader.biBitCount == 24)  
  183.     {  
  184.         // delete the last image if there was one  
  185.         if (bitmap->buffer)  
  186.             free(bitmap->buffer);  
  187.  
  188.         // allocate the memory for the image  
  189.         if (!(bitmap->buffer = (UCHAR *) malloc (bitmap->bitmapinfoheader.biSizeImage)))  
  190.         {  
  191.             // close the file  
  192.             //_lclose(file_handle);  
  193.             bitmapFile.close ();
  194.  
  195.             // return error  
  196.             return(0);  
  197.         } // end if  
  198.  
  199.         // now read it in  
  200.         //_lread(file_handle, bitmap->buffer, bitmap->bitmapinfoheader.biSizeImage);  
  201.         bitmapFile.read ((char *) (bitmap->buffer), bitmap->bitmapinfoheader.biSizeImage);
  202.  
  203.     } // end if  
  204.     else  
  205.     {  
  206.         // serious problem  
  207.         return(0);  
  208.  
  209.     } // end else  
  210.  
  211.  
  212.     // close the file  
  213.     //_lclose(file_handle);  
  214.     bitmapFile.close ();
  215.  
  216.     // flip the bitmap  
  217.     Flip_Bitmap(bitmap->buffer,    
  218.                     bitmap->bitmapinfoheader.biWidth * (bitmap->bitmapinfoheader.biBitCount / 8),    
  219.                     bitmap->bitmapinfoheader.biHeight);  
  220.  
  221.     // return success  
  222.     return(1);  
  223.  
  224. } // end Load_Bitmap_File  
  225.  
  226. ///////////////////////////////////////////////////////////  
  227.  
  228.  
  229.  
  230.  
  231. int main (int argc, char * argv[])
  232. {
  233.     if (Load_Bitmap_File (&bitmap, "bitmap24.bmp") != 1)
  234.     {
  235.         cout << "error: picture opening failure" << endl;
  236.     }
  237.  
  238.     cin.get();
  239.     return 0;
  240. }
Add Comment
Please, Sign In to add comment