473,320 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

writing a bmp

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!
May 1 '08 #1
7 9616
sicarie
4,677 Expert Mod 4TB
What language are you implementing this in?
May 2 '08 #2
Savage
1,764 Expert 1GB
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?
May 2 '08 #3
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 (BITMAPFILEHEADER and BITMAPINFOHEADER) 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!
May 2 '08 #4
Savage
1,764 Expert 1GB
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 (BITMAPFILEHEADER and BITMAPINFOHEADER) 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.
May 2 '08 #5
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!
May 2 '08 #6
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:

Expand|Select|Wrap|Line Numbers
  1. HANDLE file;
  2.     BITMAPFILEHEADER fileHeader;
  3.     BITMAPINFOHEADER fileInfo;
  4.     RGBTRIPLE *image;
  5.     image = new RGBTRIPLE[512*512];
  6.  
  7.     file = CreateFile("example.bmp",GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_NEW,NULL,NULL);  //Sets up the new bmp to be written to
  8.  
  9.     fileHeader.bfType = 'BM';                                                                    //Sets our type to BM or bmp
  10.     fileHeader.bfSize = sizeof(BITMAPFILEHEADER);                                                //Sets the size equal to the size of the header struct
  11.     fileHeader.bfReserved1 = 0;                                                                    //sets the reserves to 0
  12.     fileHeader.bfReserved2 = 0;
  13.     fileHeader.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);                    //Sets offbits equal to the size of file and info header (should be 24 bits per pixel)
  14.  
  15.     fileInfo.biSize = sizeof(BITMAPINFOHEADER);
  16.     fileInfo.biWidth = 512;
  17.     fileInfo.biHeight = 512;
  18.     fileInfo.biPlanes = 1;
  19.     fileInfo.biBitCount = 24;
  20.     fileInfo.biCompression = BI_RGB;
  21.     fileInfo.biSizeImage = 0;
  22.     fileInfo.biXPelsPerMeter = 2400;
  23.     fileInfo.biYPelsPerMeter = 2400;
  24.  
  25.     WriteFile(file,&fileHeader,sizeof(fileHeader),NULL,NULL);
  26.     WriteFile(file,&fileInfo,sizeof(fileInfo),NULL,NULL);
  27.  
May 2 '08 #7
Nevermind, I finally got it! Thanks for the help guys! Heres the code (this isn't school work):

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <windows.h>
  5.  
  6. using namespace std; 
  7.  
  8.  
  9. int main()
  10. {
  11.     HANDLE file;
  12.     BITMAPFILEHEADER fileHeader;
  13.     BITMAPINFOHEADER fileInfo;
  14.     RGBTRIPLE *image;
  15.     DWORD write = 0;
  16.     image = new RGBTRIPLE[512*512*24];
  17.  
  18.     file = CreateFile("example.bmp",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);  //Sets up the new bmp to be written to
  19.  
  20.     fileHeader.bfType = 19778;                                                                    //Sets our type to BM or bmp
  21.     fileHeader.bfSize = sizeof(fileHeader.bfOffBits) + sizeof(RGBTRIPLE);                                                //Sets the size equal to the size of the header struct
  22.     fileHeader.bfReserved1 = 0;                                                                    //sets the reserves to 0
  23.     fileHeader.bfReserved2 = 0;
  24.     fileHeader.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);                    //Sets offbits equal to the size of file and info header
  25.  
  26.     fileInfo.biSize = sizeof(BITMAPINFOHEADER);
  27.     fileInfo.biWidth = 512;
  28.     fileInfo.biHeight = 512;
  29.     fileInfo.biPlanes = 1;
  30.     fileInfo.biBitCount = 24;
  31.     fileInfo.biCompression = BI_RGB;
  32.     fileInfo.biSizeImage = 512 * 512 * (24/8);
  33.     fileInfo.biXPelsPerMeter = 2400;
  34.     fileInfo.biYPelsPerMeter = 2400;
  35.     fileInfo.biClrImportant = 0;
  36.     fileInfo.biClrUsed = 0;
  37.  
  38.     WriteFile(file,&fileHeader,sizeof(fileHeader),&write,NULL);
  39.     WriteFile(file,&fileInfo,sizeof(fileInfo),&write,NULL);
  40.  
  41.     for (int i = 0; i < 512*512*24; i++)
  42.     {
  43.         image[i].rgbtBlue = 255;
  44.         image[i].rgbtGreen = 0;
  45.         image[i].rgbtRed = 0;
  46.     }
  47.  
  48.     WriteFile(file, image, fileInfo.biSizeImage, &write, NULL);
  49.  
  50.     CloseHandle(file);
  51.  
  52. }
  53.  
  54.  
It doesn't do what I want yet, but thats an easy fix. This just creates an all blue bmp.
May 3 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

48
by: Joseph | last post by:
Hi I'm writing a commercial program which must be reliable. It has to do some basic reading and writing to and from files on the hard disk, and also to a floppy. I have foreseen a potential...
6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
3
by: ishekar | last post by:
Hi, I have an application where i want to write data to a file, the data is being sent from an external source. I know the total size of the data and then i retrieve the data in small segments...
1
by: Daniel | last post by:
System.IO.StreamWriter Close or Flush method to shut down the computer in such a way that just part of the file is written? or an empty file is written? Also if the Close or Flush is to a...
5
by: Jeong-Gun Lee | last post by:
I'm writing a code of writing a value to a specific memory address. ================================================================= #include <stdio.h> int main() { long air; long...
102
by: Xah Lee | last post by:
i had the pleasure to read the PHP's manual today. http://www.php.net/manual/en/ although Pretty Home Page is another criminal hack of the unix lineage, but if we are here to judge the quality...
16
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
3
by: Barry Flynn | last post by:
Hi I am working with a VB 2005 program which has been converted from VB6. It writes data out to a flat file, with code like the following line WriteLine(riFileNo, "Hist", lsAssetID,...
89
by: Skybuck Flying | last post by:
Hello, This morning I had an idea how to write Scalable Software in general. Unfortunately with Delphi 2007 it can't be done because it does not support operating overloading for classes, or...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.