writing a bmp

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nfollmer
    New Member
    • May 2007
    • 10

    writing a bmp

    This may seem like a newbish question (because it is) but can anyone point me in the right direction as to where I can find information on writing .bmp files? All I want to do is give the program a random number seed, and it generates a noise map from this seed. Sounds simply enough, but I don't know where to begin on actually writing the color values. I realize that bmp's color data is in BRG format and I should probably have this data stored in an array of BYTE's, but thats about all I know. Any help would be appreciated, I don't even need any code really, just some guidance (I'd actually prefer guidance over code, I learn more than way :) ).

    Thanks!
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    What language are you implementing this in?

    Comment

    • Savage
      Recognized Expert Top Contributor
      • Feb 2007
      • 1759

      #3
      Originally posted by nfollmer
      This may seem like a newbish question (because it is) but can anyone point me in the right direction as to where I can find information on writing .bmp files? All I want to do is give the program a random number seed, and it generates a noise map from this seed. Sounds simply enough, but I don't know where to begin on actually writing the color values. I realize that bmp's color data is in BRG format and I should probably have this data stored in an array of BYTE's, but thats about all I know. Any help would be appreciated, I don't even need any code really, just some guidance (I'd actually prefer guidance over code, I learn more than way :) ).

      Thanks!

      Do you know how to read in a BMP?

      Comment

      • nfollmer
        New Member
        • May 2007
        • 10

        #4
        Sorry. C++. I sort of understand how to read a bmp, and I know they are stored upside down (but that won't matter for this program, I am just generating noise). After I fill my file information (BITMAPFILEHEAD ER and BITMAPINFOHEADE R) and fill my array with my RGB and bitcount information, how do I save it to the BMP format I guess is my question? Here is the only information I have already found on reading BMP's, so this is what I have been working off of:

        ::Link removed::

        Thanks for the replies!
        Last edited by sicarie; May 2 '08, 06:16 PM. Reason: Link removed per Guidelines

        Comment

        • Savage
          Recognized Expert Top Contributor
          • Feb 2007
          • 1759

          #5
          Originally posted by nfollmer
          Sorry. C++. I sort of understand how to read a bmp, and I know they are stored upside down (but that won't matter for this program, I am just generating noise). After I fill my file information (BITMAPFILEHEAD ER and BITMAPINFOHEADE R) and fill my array with my RGB and bitcount information, how do I save it to the BMP format I guess is my question? Here is the only information I have already found on reading BMP's, so this is what I have been working off of:

          ::Link removed::

          Thanks for the replies!
          It's only the matter of changing the ReadFile() function with WriteFile() one.

          So everything is the same.

          You:

          1.Initialize bitmap file header and write it to the newly created file,
          2.Initialize bitmap info header and write it to the file,
          3.Create any data you want,
          4.Once finished write it to the file,
          5.Close the file.

          That should be it.

          Comment

          • nfollmer
            New Member
            • May 2007
            • 10

            #6
            Originally posted by Savage
            It's only the matter of changing the ReadFile() function with WriteFile() one.

            So everything is the same.

            You:

            1.Initialize bitmap file header and write it to the newly created file,
            2.Initialize bitmap info header and write it to the file,
            3.Create any data you want,
            4.Once finished write it to the file,
            5.Close the file.

            That should be it.
            Thanks, I figured it was something simple, but I have never really used image files before (besides in GUI). I'll whip something up and post it if it works. Thanks again!

            Comment

            • nfollmer
              New Member
              • May 2007
              • 10

              #7
              Originally posted by nfollmer
              Thanks, I figured it was something simple, but I have never really used image files before (besides in GUI). I'll whip something up and post it if it works. Thanks again!
              Ok, I'm stuck now. What's the difference between RGBTRIPLE and RGBQUAD? I have been looking through the MSDN documentation and they don't have any usage examples for either of these structs like they normally do.

              Here is what I have so far:

              Code:
              HANDLE file;
              	BITMAPFILEHEADER fileHeader;
              	BITMAPINFOHEADER fileInfo;
              	RGBTRIPLE *image;
              	image = new RGBTRIPLE[512*512];
              
              	file = CreateFile("example.bmp",GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_NEW,NULL,NULL);  //Sets up the new bmp to be written to
              	
              	fileHeader.bfType = 'BM';																	//Sets our type to BM or bmp
              	fileHeader.bfSize = sizeof(BITMAPFILEHEADER);												//Sets the size equal to the size of the header struct
              	fileHeader.bfReserved1 = 0;																	//sets the reserves to 0
              	fileHeader.bfReserved2 = 0;
              	fileHeader.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);					//Sets offbits equal to the size of file and info header (should be 24 bits per pixel)
              
              	fileInfo.biSize = sizeof(BITMAPINFOHEADER);
              	fileInfo.biWidth = 512;
              	fileInfo.biHeight = 512;
              	fileInfo.biPlanes = 1;
              	fileInfo.biBitCount = 24;
              	fileInfo.biCompression = BI_RGB;
              	fileInfo.biSizeImage = 0;
              	fileInfo.biXPelsPerMeter = 2400;
              	fileInfo.biYPelsPerMeter = 2400;
              
              	WriteFile(file,&fileHeader,sizeof(fileHeader),NULL,NULL);
              	WriteFile(file,&fileInfo,sizeof(fileInfo),NULL,NULL);

              Comment

              • nfollmer
                New Member
                • May 2007
                • 10

                #8
                Nevermind, I finally got it! Thanks for the help guys! Heres the code (this isn't school work):

                Code:
                #include "stdafx.h"
                #include <iostream>
                #include <fstream>
                #include <windows.h>
                
                using namespace std; 
                
                
                int main()
                {
                	HANDLE file;
                	BITMAPFILEHEADER fileHeader;
                	BITMAPINFOHEADER fileInfo;
                	RGBTRIPLE *image;
                	DWORD write = 0;
                	image = new RGBTRIPLE[512*512*24];
                
                	file = CreateFile("example.bmp",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);  //Sets up the new bmp to be written to
                	
                	fileHeader.bfType = 19778;																	//Sets our type to BM or bmp
                	fileHeader.bfSize = sizeof(fileHeader.bfOffBits) + sizeof(RGBTRIPLE);												//Sets the size equal to the size of the header struct
                	fileHeader.bfReserved1 = 0;																	//sets the reserves to 0
                	fileHeader.bfReserved2 = 0;
                	fileHeader.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);					//Sets offbits equal to the size of file and info header
                
                	fileInfo.biSize = sizeof(BITMAPINFOHEADER);
                	fileInfo.biWidth = 512;
                	fileInfo.biHeight = 512;
                	fileInfo.biPlanes = 1;
                	fileInfo.biBitCount = 24;
                	fileInfo.biCompression = BI_RGB;
                	fileInfo.biSizeImage = 512 * 512 * (24/8);
                	fileInfo.biXPelsPerMeter = 2400;
                	fileInfo.biYPelsPerMeter = 2400;
                	fileInfo.biClrImportant = 0;
                	fileInfo.biClrUsed = 0;
                
                	WriteFile(file,&fileHeader,sizeof(fileHeader),&write,NULL);
                	WriteFile(file,&fileInfo,sizeof(fileInfo),&write,NULL);
                
                	for (int i = 0; i < 512*512*24; i++)
                	{
                		image[i].rgbtBlue = 255;
                		image[i].rgbtGreen = 0;
                		image[i].rgbtRed = 0;
                	}
                
                	WriteFile(file, image, fileInfo.biSizeImage, &write, NULL);
                
                	CloseHandle(file);
                
                }
                It doesn't do what I want yet, but thats an easy fix. This just creates an all blue bmp.

                Comment

                Working...