Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read in bitmaps as binary data

Status
Not open for further replies.

cmsamcfe

Programmer
Dec 18, 2002
31
0
0
GB
I need to read in bitmaps and png files as binary data, change the header data by overwriting the first 20 bytes, then write out the data to a new filename. I have written code to do this in matlab (shown below) but for some reason I am having a complete block on how to do this in C++

Any help or suggestions appreciated

Thanks

Matlab code
--------------------------

fid=fopen(filename,'w');
fwrite(fid,vid_header,'ubit8');
fwrite(fid,reshape(img_data:),:,1).',512*512,1),'ubit8');
fclose(fid);

----------------------------
 
It's pretty much the same thing.

unsigned short int Buffer[10];
unsigned short int RemainingBitmap[SizeOfBitmap];

int FilePtr = FileOpen("Filename", fmOpenRead);
for (int x=0; x < 10; x++)
{
FileRead(FilePtr, Buffer, 20);
}
for (int x=0; x < (SizeOfBitmap - 20); x++)
{
FileRead(FilePtr, RemainingBitmap, SizeOfBitmap - 20);
}
FileClose(FilePtr);

This grabs your first 20 bytes. Modify them word at a time. i.e. Buffer[0] = 2 new bytes; Then it stores the rest of the bytes. If you don't know your bitmap size at design-time, you will have to use the new keyword and create new segments of memory to store the bitmap in. You may also opt to use an unsigned char instead of unsigned short int. The unsigned char will be one byte and allow you to access byte at a time, instead of word at a time.

Then...
FilePtr = FileCreate(&quot;NewFileName&quot;);
FileWrite(FilePtr, Buffer, 20);
FileWrite(FilePtr, RemainingBitmap, SizeOfBitmap - 20);
FileClose(FilePtr);

Good luck,
Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top