Advertisement
Guest User

Untitled

a guest
Oct 28th, 2013
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.97 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3.  
  4. typedef unsigned int GLenum;
  5. typedef unsigned char GLubyte;
  6. typedef unsigned int GLuint;
  7.  
  8. enum IMG_ERROR
  9. {
  10.         NONE = 0,
  11.         INVALID_PATH,
  12.         FAIL_TO_OPEN,
  13.         INVALID_FORMAT,
  14.         INVALID_BPP
  15. };
  16.  
  17. /**
  18.  * Converts an (H)ex (B)yte Array (t)o an Unsigned (I)nt.\n
  19.  * Used when converting data from image files to useable base 10 integer values.
  20.  * \n\n
  21.  * Example:\n\n
  22.  *
  23.  * char data[ 3 ]     = fstream.get( data, 4 );
  24.  * unsigned dataInt   = convertHBCTI( data, 3 );
  25.  */
  26. unsigned int convertHBTI( char* array, unsigned length );
  27.  
  28. /**
  29.  * Converts an (H)ex (C)har Array (t)o an Unsigned (I)nt\n
  30.  * \n
  31.  * Example:\n\n
  32.  *
  33.  * unsigned value = convertHBTI( "A3FF" );
  34.  */
  35. unsigned int convertHCTI( char* str );
  36.  
  37. struct Image
  38. {
  39.         std::string path;
  40.         std::string name;
  41.         std::string extension;
  42.  
  43.         unsigned int width;             // Image Width
  44.         unsigned int height;    // Image Height
  45.         unsigned int bpp;               // Bits-per-Pixel (typically 24 or 32)
  46.         unsigned int size;              // Number of Elements in m_Data
  47.  
  48.         GLenum    format;               // Image type (GL_RGB, GL_BGR, etc.);
  49.         IMG_ERROR error;
  50.  
  51.         GLubyte* data;                  // Image data starting from lower left corner
  52.  
  53.         Image( )
  54.         {
  55.                 width  = 0;
  56.                 height = 0;
  57.                 bpp    = 0;
  58.                 size   = 0;
  59.                 data   = NULL;
  60.                 format = 0;
  61.                 error  = NONE;
  62.         }
  63.  
  64.         ~Image( )
  65.         {
  66.                 if( data != NULL )
  67.                         delete [] data;
  68.  
  69.                 data = NULL;
  70.         }
  71. };
  72.  
  73. /**
  74. * Loads in a 24/32-bit BMP image.\n
  75. * Does not create an OpenGL texture from it but reads in
  76. * all the information/data to do so, including the OpenGL
  77. * enum image type.
  78. *
  79. * \author ssell
  80. */
  81. void loadBMP( Image* image )
  82. {
  83.         /**
  84.          *        All modern BMP images should be read in by this function.\n\n
  85.          *
  86.          *  In general, a BMP file is split into three sections: Bitmap File Header, DIB Header, and the Image Data.\n\n
  87.          *
  88.          *  From the Bitmap File Header we simply confirm that it is a valid BMP file type (BM) and then continue
  89.          *  onto the DIB information. From DIB we extract the width, height, and bpp information. We also grab
  90.          *  the length of the DIB so that we know where to seek to once ready to read in the actual image data.\n\n
  91.          *
  92.          *  The image data starts at the lower-left corner and is in BGR/A order. If bpp == 24 (no alpha channel)
  93.          *  then padding is added to the end of each row. The padding is ( width % 4 ) to ensure 4-byte alignment.
  94.          *  If bpp == 32 then there is no padding.\n\n
  95.          *
  96.          *  NOTE: this method is untested on BMP files that contain a color table following the DIB header. It is
  97.          *  rare for a BMP to contain such information, and I personally am unsure of what program to use to create
  98.          *  such an image so it is currently unsupported. It should be added in the future.
  99.          */
  100.  
  101.         FILE* file = std::fopen( image->path.c_str( ), "rb" );
  102.  
  103.         if( file == NULL )
  104.         {
  105.                 //Logger::get( )->write( 'e', "Failed to create texture from '%s'! : Failed to open!", image->path.c_str( ) );
  106.                 image->error = IMG_ERROR::FAIL_TO_OPEN;
  107.                 return;
  108.         }
  109.  
  110.         unsigned char header[ 54 ];
  111.         std::fread( header, sizeof( unsigned char ), 54, file );
  112.  
  113.         image->width  = *( GLuint* )&header[ 18 ];
  114.         image->height = *( GLuint* )&header[ 22 ];
  115.         image->bpp    = *( GLuint* )&header[ 28 ] / 8;
  116.         image->size   = image->width * image->height * image->bpp;
  117.         image->data   = new GLubyte[ image->size ];
  118.         //image->format = ( image->bpp == 3 ? GL_BGR : GL_BGRA );
  119.  
  120.         if( image->bpp == 4 )
  121.         {
  122.                 std::fread( image->data, sizeof( GLubyte ), image->size, file );
  123.         }
  124.         else
  125.         {
  126.                 unsigned dataPos = 0;
  127.                 unsigned char buffer[ 4 ];
  128.  
  129.                 for( int i = 0; i < image->height; i++ )
  130.                 {
  131.                         std::fread( &image->data[ dataPos ], sizeof( GLubyte ),
  132.                                         image->width * 3, file );
  133.                         std::fread( buffer, sizeof( GLubyte ),
  134.                                         ( image->width * 3 ) % 4, file );
  135.                         dataPos += image->width * 3;
  136.                 }
  137.         }
  138.  
  139.         std::fclose( file );
  140. }
  141.  
  142. int main( )
  143. {
  144.     Image image;
  145.     image.path = "BMP.bmp";
  146.  
  147.     loadBMP( &image );
  148.  
  149.     for( int y = 0; y < image.height; y++ )
  150.     {
  151.         for( int x = 0; x < image.width * 4; x++ )
  152.         {
  153.             std::cout << ( int )image.data[ ( y * image.height ) + x ] << " ";
  154.         }
  155.  
  156.         std::cout << std::endl;
  157.     }
  158.  
  159.     int a;
  160.     std::cin >> a;
  161.  
  162.     return 0;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement