- Home /
Import 8bit .raw file, and 16bit explanation.
Hello, this is my first question here, so please forgive me if I make any mistake. Im trying to create a script that imports .raw heightmaps to a desired terrain (like in the included "Import Heightmap" option. I Googled a lot, and I came with this:
System.IO.FileInfo fi = new System.IO.FileInfo(path);
int size = m_heightMapRes*m_heightMapRes*2;
System.IO.FileStream fs = fi.OpenRead();
byte[] data = new byte[size];
fs.Read(data, 0, size);
fs.Close();
bool bigendian = (m_rawByteOrder == BYTE_ORDER.Mac) ? true : false;
int i=0;
for(int x = 0 ; x < m_heightMapRes; x++)
{
for(int y = 0; y < m_heightMapRes; y++)
{
//Extract 16 bit data and normalize.
float ht = (bigendian) ? (data[i++]*256.0f + data[i++]) : (data[i++] + data[i++]*256.0f);
htmap[m_heightMapRes-x-1,y] = ht / 65535.0f;
}
};
This works for 16bit raw files. What I don't understand: Why "size = m_heightMapRes*m_heightMapRes*2" is multplied by 2? An explanation for the "Extract 16 bit data part"? (I understand the littleendian and bigendian part)
I want to understand it, because I want to convert it to support 8bit files. Yes, im a noob in this. Thanks in advance.
Ok so searhing more y discovered that the size is given by: yRes*xRes*BitDepth (where 2 is 2bytes = 16 bits hehe). So I supose if the file is 8bit it will be size=xRes*yRes ? Still haven't figured out the extract part. Please help :D
Ok, now I understand the normalize part I think. for unsigned integers 16bit range is [0,65535] and 8bit range is [0,255]. It scales the data. So for a 8 bit image will it be like this?
float ht = (bigendian) ? (data[i++]*256.0f + data[i++]) : (data[i++] + data[i++]*256.0f);
htmap[m_height$$anonymous$$apRes-x-1,y] = ht / 256.0f;
Please help.
Answer by soliddanii · Feb 01, 2014 at 01:49 PM
In case anyone is wondering, this is the solution I achieved:
For 8 bit heightmaps this changes
size = heightMapRes*heightMapRes;
And,
int i = 0;
for(int x = 0 ; x < heightMapRes; x++) {
for(int y = 0; y < heightMapRes; y++) {
//Extract 8 bit data and normalize.
htmap[heightMapRes-x-1,y] = data[i++]/255.0f;
}
};
Obviously you get much worst results with 8 bit heightmaps, but thats not the point here. Thanks for the help guys.
Answer by UDN_237f197f-1c0d-4823-8d3d-3c7de090485b · May 16, 2017 at 10:00 AM
Hi, You are working with 16 bit raw file, 16 bit for each height value. File consist of bytes, which is 8 bit. So, you have to multiply map resolution by 2 to get the byte array size.
Your answer
Follow this Question
Related Questions
How can I import terrain from DXF? 1 Answer
How are the bytes in a 16bit heightmap organized? 3 Answers
How to read raw files for custom terrain generation system? 0 Answers
Is there a definitive explanation for generating and importing raw heightmaps? 1 Answer
Understanding heightmap RAW imports 1 Answer