Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h> // include important windows stuff
- #include <windowsx.h>
- #include <mmsystem.h>
- #include <iostream> // include important C/C++ stuff
- #include <conio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #include <memory.h>
- #include <cstring>
- #include <stdarg.h>
- #include <stdio.h>
- #include <math.h>
- #include <io.h>
- #include <fcntl.h>
- #include <fstream>
- using namespace std;
- // DEFINES ////////////////////////////////////////////////
- // defines for windows
- #define WINDOW_CLASS_NAME L"WINCLASS1"
- // default screen size
- #define SCREEN_WIDTH 1024 // size of screen
- #define SCREEN_HEIGHT 768
- #define SCREEN_BPP 16 // bits per pixel
- #define BITMAP_ID 0x4D42 // universal id for a bitmap
- #define MAX_COLORS_PALETTE 256
- // container structure for bitmaps .BMP file
- typedef struct BITMAP_FILE_TAG
- {
- BITMAPFILEHEADER bitmapfileheader; // this contains the bitmapfile header
- BITMAPINFOHEADER bitmapinfoheader; // this is all the info including the palette
- PALETTEENTRY palette[256]; // we will store the palette here
- UCHAR *buffer; // this is a pointer to the data
- } BITMAP_FILE, *BITMAP_FILE_PTR;
- BITMAP_FILE bitmap; // holds the bitmap
- // FUNCTIONS ////////////////////////////////////////////////
- void printError (char * errorName)
- {
- // print error to log.txt
- ofstream errorFile = ofstream ("log.txt");
- if (errorFile.is_open ())
- {
- errorFile << errorName << endl; //"Error: OpenFile function failure. "
- }
- errorFile.close ();
- }
- int Flip_Bitmap(UCHAR *image, int bytes_per_line, int height)
- {
- // this function is used to flip bottom-up .BMP images
- UCHAR *buffer; // used to perform the image processing
- int index; // looping index
- // allocate the temporary buffer
- if (!(buffer = (UCHAR *) malloc (bytes_per_line * height)))
- return(0);
- // copy image to work area
- //memcpy(buffer, image, bytes_per_line * height);
- memcpy_s (buffer, bytes_per_line * height, image, bytes_per_line * height);
- // flip vertically
- for (index = 0; index < height; index++)
- memcpy(&image[((height - 1) - index) * bytes_per_line], &buffer[index * bytes_per_line], bytes_per_line);
- // release the memory
- free(buffer);
- // return success
- return(1);
- } // end Flip_Bitmap
- ///////////////////////////////////////////////////////////////
- int Load_Bitmap_File(BITMAP_FILE_PTR bitmap, char *filename)
- {
- int file_handle = 0; // the file handle
- int index; // looping index
- UCHAR *temp_buffer = NULL; // used to convert 24 bit images to 16 bit
- streampos pos_cur;
- ifstream bitmapFile = ifstream ();
- bitmapFile.open (filename, ifstream::in);
- if (! bitmapFile.is_open ())
- {
- // print error to log.txt
- ofstream errorFile = ofstream ("log.txt");
- if (! errorFile.is_open ())
- {
- errorFile << "Error: OpenFile function failure. " << endl;
- }
- errorFile.close ();
- // abort
- return(0);
- }
- // load the bitmap file header:
- //_lread(file_handle, &(bitmap->bitmapfileheader), sizeof(BITMAPFILEHEADER));
- bitmapFile.read ((char *) &(bitmap->bitmapfileheader), sizeof (BITMAPFILEHEADER));
- // test if this is a bitmap file
- if (bitmap->bitmapfileheader.bfType != BITMAP_ID)
- {
- // close the file
- //_lclose(file_handle);
- bitmapFile.close ();
- printError ("error: wrong bitmap type");
- cout << "error: wrong bitmap type" << endl;
- // return error
- return(0);
- } // end if
- // now we know this is a bitmap, so read in all the sections.
- // now the bitmap infoheader:
- //_lread(file_handle, &bitmap->bitmapinfoheader, sizeof(BITMAPINFOHEADER));
- bitmapFile.seekg (sizeof (BITMAPFILEHEADER), ios::beg);
- pos_cur = bitmapFile.tellg (); // save current stream position
- bitmapFile.read ((char *) &(bitmap->bitmapinfoheader), sizeof (BITMAPINFOHEADER));
- //cout << bitmap->bitmapinfoheader.biBitCount << endl;
- // now load the color palette if there is one
- if (bitmap->bitmapinfoheader.biBitCount == 8)
- {
- //_lread(file_handle, &bitmap->palette, MAX_COLORS_PALETTE * sizeof(PALETTEENTRY));
- // not tested:
- bitmapFile.read ((char *) &(bitmap->palette), MAX_COLORS_PALETTE * sizeof(PALETTEENTRY));
- // now set all the flags in the palette correctly and fix the reversed
- // BGR RGBQUAD data format
- for (index = 0; index < MAX_COLORS_PALETTE; index++)
- {
- // reverse the red and green fields
- int temp_color = bitmap->palette[index].peRed;
- bitmap->palette[index].peRed = bitmap->palette[index].peBlue;
- bitmap->palette[index].peBlue = temp_color;
- // always set the flags word to this
- bitmap->palette[index].peFlags = PC_NOCOLLAPSE;
- } // end for index
- } // end if
- // finally the image data itself:
- //_lseek(file_handle, -((int) (bitmap->bitmapinfoheader.biSizeImage)), SEEK_END);
- bitmapFile.seekg (-((int) bitmap->bitmapinfoheader.biSizeImage), ios::end);
- //bitmapFile.seekg (sizeof (BITMAPINFOHEADER) + sizeof (BITMAPFILEHEADER) + MAX_COLORS_PALETTE * sizeof(PALETTEENTRY), ios::beg);
- // now read in the image, if the image is 8 or 16 bit then simply read it
- // but if its 24 bit then read it into a temporary area and then convert
- // it to a 16 bit image
- if (bitmap->bitmapinfoheader.biBitCount == 8 ||
- bitmap->bitmapinfoheader.biBitCount == 16 ||
- bitmap->bitmapinfoheader.biBitCount == 24)
- {
- // delete the last image if there was one
- if (bitmap->buffer)
- free(bitmap->buffer);
- // allocate the memory for the image
- if (!(bitmap->buffer = (UCHAR *) malloc (bitmap->bitmapinfoheader.biSizeImage)))
- {
- // close the file
- //_lclose(file_handle);
- bitmapFile.close ();
- // return error
- return(0);
- } // end if
- // now read it in
- //_lread(file_handle, bitmap->buffer, bitmap->bitmapinfoheader.biSizeImage);
- bitmapFile.read ((char *) (bitmap->buffer), bitmap->bitmapinfoheader.biSizeImage);
- } // end if
- else
- {
- // serious problem
- return(0);
- } // end else
- // close the file
- //_lclose(file_handle);
- bitmapFile.close ();
- // flip the bitmap
- Flip_Bitmap(bitmap->buffer,
- bitmap->bitmapinfoheader.biWidth * (bitmap->bitmapinfoheader.biBitCount / 8),
- bitmap->bitmapinfoheader.biHeight);
- // return success
- return(1);
- } // end Load_Bitmap_File
- ///////////////////////////////////////////////////////////
- int main (int argc, char * argv[])
- {
- if (Load_Bitmap_File (&bitmap, "bitmap24.bmp") != 1)
- {
- cout << "error: picture opening failure" << endl;
- }
- cin.get();
- return 0;
- }
Add Comment
Please, Sign In to add comment