Bitmap conversion

This is a discussion on Bitmap conversion within the C++ Programming forums, part of the General Programming Boards category; I was searching the 'net for 24 bit to 16 bit bitmap conversion code. I found this and modified it ...

  1. #1
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735

    Bitmap conversion

    I was searching the 'net for 24 bit to 16 bit bitmap conversion code.
    I found this and modified it so that it should output the bitmap to a new file after conversion, but it doesn't work. Can anyone help me?
    here is the code
    Code:
    #include <iostream>
    #include <fstream>
    #include <windows.h>
    
    using namespace std;
    // Macros
    struct BITMAPINFOHEADER1
    {
       DWORD biSize;      // number of bytes in this structure
       LONG  biWidth;     // width of the bitmap
       LONG  biHeight;    // height of the bitmap
       WORD  biPlanes;    // number of planes (don't worry about it)
       WORD  biBitCount;  // bits per pixel (1,4,8,16,24 or 32)
       WORD  biCompression;   // type of compression
       DWORD biSizeImage;     // size of image in Bytes
       LONG  biXPelsPerMeter; // x res of target display
       LONG  biYPelsPerMeter; // y res of target display
       DWORD biClrUsed;       // how many colors are used
       DWORD biClrImportant;  // number of important colors
    };
    
    #define RGB16(red, green, blue) ( ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3))
    
    void LoadBitmap()
    {
       BITMAPINFOHEADER1 infoheader;
       WORD             *bitmapData;
       WORD             *bitmapDone;
       FILE             *bitmapFile;
    
       BYTE             red, green, blue;
    
       bitmapFile = fopen("Grass.bmp", "rb");
       fseek(bitmapFile, sizeof(BITMAPFILEHEADER), SEEK_SET);
       fread(&infoheader, sizeof(BITMAPINFOHEADER1), 1, bitmapFile);
    
       bitmapData = new WORD[infoheader.biWidth * infoheader.biHeight];
       bitmapDone = new WORD[infoheader.biWidth * infoheader.biHeight];
    
    
       for( int y=0; y<infoheader.biHeight; ++y)
       {
          for( int x=0; x<infoheader.biWidth; ++x)
          {
             fread(&blue, sizeof(BYTE), 1, bitmapFile);
             fread(&green, sizeof(BYTE), 1, bitmapFile);
             fread(&red, sizeof(BYTE), 1, bitmapFile);
    
             bitmapData[y*infoheader.biWidth + x] = RGB16(red, green, blue);
          }
       }
       
       int heightIndex = 0;
       for( y=infoheader.biHeight-1;  y>=0; --y)
       {
          for( int x=0; x<infoheader.biWidth; ++x)
             bitmapDone[heightIndex*infoheader.biWidth + x] = bitmapData[y*infoheader.biWidth + x];
    
          ++heightIndex;
       }
       ofstream out;
       out.open("Grass2.bmp");
       int Index=0;
       while (Index < infoheader.biSizeImage)
       {
    	   out << bitmapDone[Index];
    	   Index++;
       }
       out.close();
    }
    
    int main()
    {
    	LoadBitmap();
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > out.open("Grass2.bmp");
    Use binary mode to open the file.
    Code:
       out.open("Grass2.bmp", ios::binary);
    > out << bitmapDone[Index];
    And use write() to write binary data.
    Code:
    	   out.write( reinterpret_cast<const char *>(&bitmapDone[Index]), sizeof(bitmapDone[index] );
    Or alternately you can probably write the whole array without a loop with something like:
    Code:
    out.write( reinterpret_cast<const char *>(&bitmapDone), infoheader.biSizeImage * sizeof(bitmapDone[0] );
    Last edited by swoopy; 09-16-2005 at 10:52 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bitmap conversion from 16x16 to 32x32
    By sharathyd in forum C Programming
    Replies: 6
    Last Post: 01-27-2009, 10:54 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 10:31 PM
  5. OpenGL -- Bitmaps
    By HQSneaker in forum Game Programming
    Replies: 14
    Last Post: 09-06-2004, 04:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21