473,399 Members | 2,858 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,399 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 9622
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.