Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- typedef unsigned int GLenum;
- typedef unsigned char GLubyte;
- typedef unsigned int GLuint;
- enum IMG_ERROR
- {
- NONE = 0,
- INVALID_PATH,
- FAIL_TO_OPEN,
- INVALID_FORMAT,
- INVALID_BPP
- };
- /**
- * Converts an (H)ex (B)yte Array (t)o an Unsigned (I)nt.\n
- * Used when converting data from image files to useable base 10 integer values.
- * \n\n
- * Example:\n\n
- *
- * char data[ 3 ] = fstream.get( data, 4 );
- * unsigned dataInt = convertHBCTI( data, 3 );
- */
- unsigned int convertHBTI( char* array, unsigned length );
- /**
- * Converts an (H)ex (C)har Array (t)o an Unsigned (I)nt\n
- * \n
- * Example:\n\n
- *
- * unsigned value = convertHBTI( "A3FF" );
- */
- unsigned int convertHCTI( char* str );
- struct Image
- {
- std::string path;
- std::string name;
- std::string extension;
- unsigned int width; // Image Width
- unsigned int height; // Image Height
- unsigned int bpp; // Bits-per-Pixel (typically 24 or 32)
- unsigned int size; // Number of Elements in m_Data
- GLenum format; // Image type (GL_RGB, GL_BGR, etc.);
- IMG_ERROR error;
- GLubyte* data; // Image data starting from lower left corner
- Image( )
- {
- width = 0;
- height = 0;
- bpp = 0;
- size = 0;
- data = NULL;
- format = 0;
- error = NONE;
- }
- ~Image( )
- {
- if( data != NULL )
- delete [] data;
- data = NULL;
- }
- };
- /**
- * Loads in a 24/32-bit BMP image.\n
- * Does not create an OpenGL texture from it but reads in
- * all the information/data to do so, including the OpenGL
- * enum image type.
- *
- * \author ssell
- */
- void loadBMP( Image* image )
- {
- /**
- * All modern BMP images should be read in by this function.\n\n
- *
- * In general, a BMP file is split into three sections: Bitmap File Header, DIB Header, and the Image Data.\n\n
- *
- * From the Bitmap File Header we simply confirm that it is a valid BMP file type (BM) and then continue
- * onto the DIB information. From DIB we extract the width, height, and bpp information. We also grab
- * the length of the DIB so that we know where to seek to once ready to read in the actual image data.\n\n
- *
- * The image data starts at the lower-left corner and is in BGR/A order. If bpp == 24 (no alpha channel)
- * then padding is added to the end of each row. The padding is ( width % 4 ) to ensure 4-byte alignment.
- * If bpp == 32 then there is no padding.\n\n
- *
- * NOTE: this method is untested on BMP files that contain a color table following the DIB header. It is
- * rare for a BMP to contain such information, and I personally am unsure of what program to use to create
- * such an image so it is currently unsupported. It should be added in the future.
- */
- FILE* file = std::fopen( image->path.c_str( ), "rb" );
- if( file == NULL )
- {
- //Logger::get( )->write( 'e', "Failed to create texture from '%s'! : Failed to open!", image->path.c_str( ) );
- image->error = IMG_ERROR::FAIL_TO_OPEN;
- return;
- }
- unsigned char header[ 54 ];
- std::fread( header, sizeof( unsigned char ), 54, file );
- image->width = *( GLuint* )&header[ 18 ];
- image->height = *( GLuint* )&header[ 22 ];
- image->bpp = *( GLuint* )&header[ 28 ] / 8;
- image->size = image->width * image->height * image->bpp;
- image->data = new GLubyte[ image->size ];
- //image->format = ( image->bpp == 3 ? GL_BGR : GL_BGRA );
- if( image->bpp == 4 )
- {
- std::fread( image->data, sizeof( GLubyte ), image->size, file );
- }
- else
- {
- unsigned dataPos = 0;
- unsigned char buffer[ 4 ];
- for( int i = 0; i < image->height; i++ )
- {
- std::fread( &image->data[ dataPos ], sizeof( GLubyte ),
- image->width * 3, file );
- std::fread( buffer, sizeof( GLubyte ),
- ( image->width * 3 ) % 4, file );
- dataPos += image->width * 3;
- }
- }
- std::fclose( file );
- }
- int main( )
- {
- Image image;
- image.path = "BMP.bmp";
- loadBMP( &image );
- for( int y = 0; y < image.height; y++ )
- {
- for( int x = 0; x < image.width * 4; x++ )
- {
- std::cout << ( int )image.data[ ( y * image.height ) + x ] << " ";
- }
- std::cout << std::endl;
- }
- int a;
- std::cin >> a;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement