6 Replies - 5002 Views - Last Post: 28 November 2011 - 12:32 PM Rate Topic: -----

#1 behsb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-April 10

Can't write bitmap files :(

Posted 26 November 2011 - 10:54 AM

would like some help with creating bitmap files please i can read and write them but no luck creating them this my code so far any help would be appreciated.

cout<<"please enter the name of the 1st file \n"<<endl;
    cin>>infile;
    cout<<"please enter the name of the 2ed file \n"<<endl;
    cin>>outfile;
     // Open the file
	 hfile = CreateFile(infile,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,NULL,NULL);
	 // Read the header
	 ReadFile(hfile,&bfh,sizeof(bfh),&written,NULL);
	 ReadFile(hfile,&bih,sizeof(bih),&written,NULL);
	 // Read image
	 unsigned long imagesize = bih.biWidth*bih.biHeight; // Helps you allocate memory for the image
	 image = new RGBTRIPLE[imagesize]; // Create a new image (I'm creating an array during runtime)
	 ReadFile(hfile,image,imagesize*sizeof(RGBTRIPLE),&written,NULL); // Reads it off the disk
     
    nfile = CreateFile(outfile,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,NULL,NULL);
     WriteFile(hfile,&bfh,sizeof(bfh),&written,NULL);
     WriteFile(hfile,&bih,sizeof(bih),&written,NULL);
     WriteFile(hfile,image,imagesize*sizeof(RGBTRIPLE),&written,NULL);
      // Close source file
	 CloseHandle(hfile);
	 CloseHandle(nfile);


Is This A Good Question/Topic? 0
  • +

Replies To: Can't write bitmap files :(

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1624
  • View blog
  • Posts: 3,081
  • Joined: 30-May 10

Re: Can't write bitmap files :(

Posted 26 November 2011 - 11:06 AM

> ReadFile(hfile,image,imagesize*sizeof(RGBTRIPLE),&written,NULL);
Bitmap pixel data isn't always in one single slab of contiguous bytes.
http://en.wikipedia....BMP_file_format
Each row can be padded with 1 to 3 "padding" bytes to make each row of pixels 4-byte aligned.

> ReadFile(hfile,&bfh,sizeof(bfh),&written,NULL);
You can't just read an external data format onto a struct.
http://en.wikipedia....cture_alignment
You could generally get away with it 20+ years ago when most 16-bit DOS compilers did the same thing.
But in a world of a wide variety of 32 and 64 bit compilers, the chances of this working reliably are slim.
Was This Post Helpful? 1
  • +
  • -

#3 behsb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-April 10

Re: Can't write bitmap files :(

Posted 26 November 2011 - 11:15 AM

well ummm ok i guess but my complier doesn't complain about that, it allows me to get the info form the bitmap accordingly but i'm not so sure how to write :(
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1306
  • View blog
  • Posts: 4,496
  • Joined: 19-February 09

Re: Can't write bitmap files :(

Posted 26 November 2011 - 11:53 AM

Hi, you create nfile for output handle then use hfile.

    nfile = CreateFile(outfile, GENERIC_READ, FILE_SHARE_READ, 
                       NULL, OPEN_EXISTING,NULL,NULL);
    WriteFile(hfile,&bfh,sizeof(bfh),&written,NULL);


Was This Post Helpful? 1
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Can't write bitmap files :(

Posted 26 November 2011 - 04:52 PM

> well ummm ok i guess but my complier doesn't complain about that

Salem wasn't saying the code was syntactically incorrect. He was saying the behavior is wrong. Writing bytes directly over a struct type is not the same necessarily as assigning each member the correct value. And in modern systems, it likely isn't.

Unless you think compilation is the same as correctness, in which case software bugs don't exist, right?
Was This Post Helpful? 0
  • +
  • -

#6 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1624
  • View blog
  • Posts: 3,081
  • Joined: 30-May 10

Re: Can't write bitmap files :(

Posted 27 November 2011 - 12:46 AM

This is what padding and alignment looks like.
#include <iostream>
// see http://msdn.microsoft.com/en-us/library/dd183374%28v=vs.85%29.aspx
using namespace std;

typedef unsigned short int WORD;
typedef unsigned long int DWORD;

typedef struct {
  WORD  type;
  DWORD tize;
  WORD  res1;
  WORD  res2;
  DWORD offset;
} foo ;

#pragma pack(1)
typedef struct {
  WORD  type;
  DWORD tize;
  WORD  res1;
  WORD  res2;
  DWORD offset;
} bar ;

int main()
{
    cout << "Unpacked struct is " << sizeof(foo) << " bytes"<< endl;
    cout << "Packed struct is " << sizeof(bar) << " bytes"<< endl;
    return 0;
}



And the results are (for me - you may get different answers)
Unpacked struct is 16 bytes
Packed struct is 14 bytes

Process returned 0 (0x0)   execution time : 0.143 s
Press any key to continue.



Two structures, which for all the world seem identical, yet one is 2 bytes shorter than the other.
If you write() and read() these as solid blocks of memory, you're surely in for disappointment at some point if you write one variation and read the other.

It might not seem like it to you at the moment, but getting something to compile is exceedingly easy.
Getting it to actually work is where the real skill comes in.


Now read the BMP file format link I posted.
Note carefully what it says about BITMAPV5HEADER
Yes, thats right - there are 7 versions of this particular structure.

Well actually, there are 14 versions:
You'll have one (or more) of the 7 versions in the header files for your particular compiler.
And you'll have one of the 7 versions in any given BMP file you're trying to read.
So the first thing you MUST do is read the 4 bytes at the start of the DIBheader to work out exactly what variation of BMP file it is you're looking at, and then decide whether it is one you can cope with.

Then you read as much DIBheader as the file supports (NOT how much your header file supports) and go from there.
Was This Post Helpful? 1
  • +
  • -

#7 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 762
  • View blog
  • Posts: 2,218
  • Joined: 20-March 10

Re: Can't write bitmap files :(

Posted 28 November 2011 - 12:32 PM

To behsb

You say you can read .bmp files but I see no evidence that you are doing that properly.

A .bmp file can be split into the following sections.

The Bitmap File Header Structure BITMAPFILEHEADER
BITMAPFILEHEADER Link

The Bitmap Info Header Structure BITMAPINFOHEADER
BITMAPINFOHEADER Link

The RGB Quad Array RGBQUAD
RGBQUAD Link

and finally..

The Pixel data in BYTES

The Bitmap File Header

start position 1, size 2 bytes, name bfType, standard value 19778 or 0x4d42, these 2 bytes mark the start of the bitmap and always have the designator "BM" which is what the dec and hex values mean.

start position 3, size 4 bytes, name bfSize, standard value 'unspecified', these 4 bytes gives you the size of the file in Bytes. Useful for later on when we come to writing or reading Pixel Data.

start position 7, size 2 bytes, name bfReserved1, standard value 0, these 2 Bytes are reserved for future use and should always be 0 these values still have to be read and written.

start position 9, size 2 bytes, name bfReserved2, standard value 0, these 2 Bytes are reserved for future use and should always be 0 these values still have to be read and written.

start position 11, size 4 bytes, name bfOffBits, standard value 1078, specifies the offset from the beginning of the file to the bitmap data, the size from the start of the file to the actual RGB Pixel data.

The Bitmap Info Header

start position 15, size 4 bytes, name biSize, standard value 40, specifies the size of the BITMAPINFO header in bytes.

start position 19, size 4 bytes, name biWidth, standard value unspecified, the width of the image in Pixels, useful for later calculations.

start position 23, size 4 bytes, name biHeight, standard value unspecified, the height of the image in Pixels, useful for later calculations.

start position 27, size 2 bytes, name biPlanes, standard value 1, the number of planes for the target device must be set to 1.

start position 29, size 2 bytes, name biBitCount,no standard value but can be taken as 8, the number of bits per pixel.

start pos 31, size 4 bytes, name biCompression, usually 0 but can be other values including BI_RGB, the type of compression, used usually 0 i.e. no compression is used.

start pos 35, size 4 bytes,name biSizeImage, standard value 0, if no compression is used this can be set to 0.

start pos 39, size 4 bytes, name biXPelsPerMeter, standard value 0, Specifies the horizontal resolution, in pixels per meter, of the target device for the bitmap usually 0.

start pos 43, size 4 bytes, name biYPelsPerMeter, standard value 0, Specifies the vertical resolution, in pixels per meter, of the target device for the bitmap usually 0.

start pos 47, size 4 bytes, name biClrUsed, no standard value, specifies the number of colors used in the bitmap, if set to zero the number of colors is calculated using the biBitCount member.

start pos 51, size 4 bytes, name blClrImportant, standard value 0, Specifies the number of color indices that are considered important for displaying the bitmap usually set to 0 to indicate all colors are important.

After you have done all that you have to read the array of color indices.


So you see there is a lot more to bitmap reading and writing than appears in your code..

Best Wishes Snoopy.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1