Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,131 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,776 people online right now. Registration is fast and FREE... Join Now!




Read and write bitmap

 
Reply to this topicStart new topic

Read and write bitmap

hug_hes
12 Apr, 2007 - 04:35 AM
Post #1

New D.I.C Head
*

Joined: 12 Apr, 2007
Posts: 4


My Contributions
I am trying to write a simple program to read a bitmap image and then write it back to a bitmap file so i can later edit the image. I have done a lot of reading and understand the bitmap file format and have looked at several code snippets to write bitmaps from scratch. Any help would be appreciated!! smile.gif
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Read And Write Bitmap
12 Apr, 2007 - 06:10 AM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
well, if you understand the file format, and you have looked at code snippits... what more help do you need? We really are not here to program for you, though we can help you with your code. So if you have some code that you are working on, post it and we would be glad to help you get it working.
User is offlineProfile CardPM
+Quote Post

hug_hes
RE: Read And Write Bitmap
17 Apr, 2007 - 05:26 AM
Post #3

New D.I.C Head
*

Joined: 12 Apr, 2007
Posts: 4


My Contributions
QUOTE(NickDMax @ 12 Apr, 2007 - 07:10 AM) *

well, if you understand the file format, and you have looked at code snippits... what more help do you need? We really are not here to program for you, though we can help you with your code. So if you have some code that you are working on, post it and we would be glad to help you get it working.


I have a piece of code here which i am trying to edit...i am not sure how to move on to use the data to write back to a file!!

CODE
using namespace std;

typedef struct bitmapFileHeader{
   unsigned short int bfType;                
   unsigned int bfSize;                      
   unsigned short int bfReserved1, bfReserved2;
   unsigned int bfOffBits;                    
} BITMAPFILEHEADER;

typedef struct bitmapInfoHeader{
   unsigned int biSize;              
   int biWidth, biHeight;                
   unsigned short int biPlanes;      
   unsigned short int biBitCount;        
   unsigned int biCompression;        
   unsigned int biSizeImage;        
   int biXPelsPerMeter, biYPelsPerMeter;    
   unsigned int biClrUsed;          
   unsigned int biClrImportant;  
} BITMAPINFOHEADER;

typedef struct colourIndex{
   unsigned char r,g,b,junk;
} COLOURINDEX;


unsigned char *LoadBitmapFile(char *argv[], BITMAPINFOHEADER *bitmapInfoHeader)
{
FILE *file1;
BITMAPFILEHEADER bitmapFileHeader;
unsigned char *bitmapImage;
int imageIdx=0;
unsigned char tempRGB;


file1 = fopen(argv[1], "rb");
if (file1 == NULL)
return NULL;


fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, file1);


if (bitmapFileHeader.bfType !=0x4D42)
{
fclose(file1);
return NULL;
}

fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER),1,file1);


fseek(file1, bitmapFileHeader.bfOffBits, SEEK_SET);


bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);


if (!bitmapImage)
{
free(bitmapImage);
fclose(file1);
return NULL;
}


fread(bitmapImage,bitmapInfoHeader->biSizeImage,1,file1);


if (bitmapImage == NULL)
{
fclose(file1);
return NULL;
}


fclose(file1);
return bitmapImage;


}

User is offlineProfile CardPM
+Quote Post

WolfCoder
RE: Read And Write Bitmap
17 Apr, 2007 - 06:03 AM
Post #4

ギュウ~
Group Icon

Joined: 5 May, 2005
Posts: 3,610



Thanked: 7 times
Dream Kudos: 1450
My Contributions
I've done this many many times before. However, it might help you if you use the CreateFile method for handling files on the disk.

For a standard 24-bit bitmap image, try using the following settings:

CODE

typedef struct bitmapFileHeader
{
   unsigned short int bfType;  // I think 0x4d42 goes here (otherwise known as the string 'BM')              
   unsigned int bfSize;  // put sizeof(BITMAPFILEHEADER) here                    
   unsigned short int bfReserved1, bfReserved2; // These are 0
   unsigned int bfOffBits; // Return the size of the headers. This tells whatever reader where the image data starts            
} BITMAPFILEHEADER;

typedef struct bitmapInfoHeader
{
   unsigned int biSize; // sizeof(BITMAPINFOHEADER)
   int biWidth, biHeight; // Actual dimensions of the image            
   unsigned short int biPlanes; // 1
   unsigned short int biBitCount; // 24  
   unsigned int biCompression; // = BI_RGB
   unsigned int biSizeImage; // put the actual byte size of the image here      
   int biXPelsPerMeter, biYPelsPerMeter; // Put anything here    
   unsigned int biClrUsed; // For 24-bit, ignore these    
   unsigned int biClrImportant; // Same
} BITMAPINFOHEADER;


The image in this case is made up of an array of RGBTRIPLES found in <windows.h>. Keep in mind the image is upside down.

I hope you aren't re-defining the structs. They're already defined for you correctly in <windows.h>.

This post has been edited by WolfCoder: 17 Apr, 2007 - 06:06 AM
User is offlineProfile CardPM
+Quote Post

hug_hes
RE: Read And Write Bitmap
17 Apr, 2007 - 06:48 AM
Post #5

New D.I.C Head
*

Joined: 12 Apr, 2007
Posts: 4


My Contributions
does this mean i require something like this:

CODE
// Fill the bitmap file header structure
    bitmapFileHeader.bfType = 0x4d42
    bitmapFileHeader.bfSize = sizeof(BITMAPFILEHEADER);
    bitmapFileHeader.bfReserved1 = 0;
    bitmapFileHeader.bfReserved2 = 0;
    bitmapFileHeader.bfOffBits = //?? not sure what goes here


    // Fill the bitmap info structure
    bitmapInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
    bitmapInfoHeader.biWidth = 480;
    bitmapInfoHeader.biHeight = 480;
    bitmapInfoHeader.biPlanes = 1;
    bitmapInfoHeader.biBitCount = 16;            // 16 - bit bitmap
    bitmapInfoHeader.biCompression = BI_RGB;     //
    bitmapInfoHeader.biSizeImage = 115318;     // does this need padding?
    bitmapInfoHeader.biXPelsPerMeter = 0;
    bitmapInfoHeader.biYPelsPerMeter = 0;
    bitmapInfoHeader.biClrUsed = 0;
    bitmapInfoHeader.biClrImportant = 0;

User is offlineProfile CardPM
+Quote Post

WolfCoder
RE: Read And Write Bitmap
17 Apr, 2007 - 06:50 AM
Post #6

ギュウ~
Group Icon

Joined: 5 May, 2005
Posts: 3,610



Thanked: 7 times
Dream Kudos: 1450
My Contributions
as far as filling out the structs, yes. However, 16-bit images are not supported under BMPs as far as I have tried.

This post has been edited by WolfCoder: 17 Apr, 2007 - 06:51 AM
User is offlineProfile CardPM
+Quote Post

hug_hes
RE: Read And Write Bitmap
17 Apr, 2007 - 06:54 AM
Post #7

New D.I.C Head
*

Joined: 12 Apr, 2007
Posts: 4


My Contributions
QUOTE(WolfCoder @ 17 Apr, 2007 - 07:50 AM) *

as far as filling out the structs, yes. However, 16-bit images are not supported under BMPs as far as I have tried.


so i can now try to write this to a file?
User is offlineProfile CardPM
+Quote Post

WolfCoder
RE: Read And Write Bitmap
17 Apr, 2007 - 07:34 AM
Post #8

ギュウ~
Group Icon

Joined: 5 May, 2005
Posts: 3,610



Thanked: 7 times
Dream Kudos: 1450
My Contributions
First, you must write the bitmapfileheader, then the bitmapinfoheader, and then the array of RGBTRIPLEs. To ensure the correct byte-order, use the already-defined structs located in <windows.h> instead of defining them yourself. Write them in exactly that order.

If you are having problems, try changing the method of file input/output. I use the Windows API call CreateFile.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:22PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month