Archived

This topic is now archived and is closed to further replies.

jerry2

how to make a raw file

Recommended Posts

as we all know,we use raw to store the little-volume pic.but i don''t know the answer to a basic problem.i can find a tutorial about raw-loading easily ,but i still don''t how i can make a raw file.it is binary stream,but if i can get just change the filename to carry my point?or there is some way for us to make a bmp(or jpeg etc.)into raw? i know this question may be a bit stupid,but i really want to know the answer.thank u. jerry2 Programming,make a whole new world.

Share this post


Link to post
Share on other sites
Raw files are just a byte dump, so a 24bit rgb image is a list of width*hieght*3 bytes. To find any pixel in the file, it starts at x*width+y*width*3 and spans 3 bytes: 1 byte red, 1 byte green, 1 byte blue. Photoshop saves straight into raw. The easiest way to get raw data otherwise is from a 24 bit uncompressed bmp file -- just strip off the BITMAPFILEHEADER and BITMAPINFOHEADER and the rest of the data are the pixels.
Example, a 50x100 24 bit image is:
50x100x3 bytes in size.
pixel (1,1) is located at offset 1*3+1*50*3.

Loading a raw file:
  
char* buffer = new char [width * hieght *bytesPerPixel];
std::ifstream file;
file.open (filename, std::ios::binary );
file.read ( (char *)buffer, width * hieght *bytesPerPixel);
file.close();


convert bmp to raw:
  
char* buffer = new char [width * hieght *bytesPerPixel];
std::ifstream file;
file.open (filename, std::ios::binary );
//trash the headers

file.read ( (char *)buffer, sizeof(BITMAPFILEHEADER) );
file.read ( (char *)buffer, sizeof(BITMAPINFOHEADER) );
file.read ( (char *)buffer, width * hieght *bytesPerPixel);
file.close();
std::ostream output;
output.open (filename, std::ios::binary );
output.write ( (char *)buffer, width * hieght *bytesPerPixel);
output.close ();

Share this post


Link to post
Share on other sites
to David_Kay:

thanks,i have knowed the true concept of the raw.

to invective:

excuse me,u mean we can use photoshop to create raw directly?

to Anonymous Poster:

ok.thanks for yr url.



jerry2
Programming,make a whole new world.

Share this post


Link to post
Share on other sites
yes. just choose file->save as, and change the type to raw -- i suggest you do image->mode->grayscale first, so you get only 1 byte per pixel or image->mode->RGB if you want 3 bytes per pixel. Photoshop will prompt you for a header when you want to save -- just ignore it and hit ok.

Share this post


Link to post
Share on other sites
to invective:

thanks for yr help,i have understood all these completely.

to GoofProg:

do u mean raw isn''t a good file format for game?but what kind of format is more suitable?

jerry2
Programming,make a whole new world.

Share this post


Link to post
Share on other sites
If you are concerned about file size, consider PNG files (you can make these in photoshop too and they have good lossless compression). You can find code to support PNG here: http://libpng.sourceforge.net/

Share this post


Link to post
Share on other sites
OK.thank u.

then i have aother question,which format is the most popular,or the perfect(combination of size,quality and feature) one nowadays?

jerry2
Programming,make a whole new world.

Share this post


Link to post
Share on other sites
It depends what you are doing. JPG gives you the smallest files (usually), but it uses lossy compression and has no alpha support, and is complicated to encode/decode. PNG has lots of features including alpha support and lossless compression. BMP is usually the easiest to output for screen captures, since BITMAPINFOHEADER AND BITMAPFILEHEADER are defined for you as structs by windows, and the rest of the file is just a 24 bit byte dump.

It depends what is important to you: aplha channels, easy of reading/writing, file size, loading speed, etc.

In any case I vote for one of the above three formats, usually choosing PNG over TGA.

Share this post


Link to post
Share on other sites