7 Replies - 279 Views - Last Post: 13 October 2017 - 07:06 PM Rate Topic: -----

#1 Miko55  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-October 17

Create image from binary data

Posted 13 October 2017 - 11:17 AM

I have a .IMG files.
Specification/description of files is (I translated):
- The IMG binary format consists of N x N pixels, where the size of the individual pixel is 2 syllables of the numeric data type: short (predefined).

What I'm doing now is reading data into byte array and then trying to create BitImage with RGB values.

Byte array looks like this: [0] 0 byte
[1] 248 byte
[2] 0 byte
[3] 248 byte
[4] 0 byte
...
[414317] 253
[414318] 136
[414319] 254
[414320] 102
[414321] 252
[414322] 135
...
[524287] 248 byte

public static Bitmap GetPictureFromData(int w, int h, byte[] data)
 {
     Bitmap bmp = new Bitmap(w, h);

     int i = 0;
     for (int x = 0; x < w; x++)
     {
         for (int y = 0; y < h; y++)
         {
             int a = 255;
             int r = data[i];
             int g = data[i+1];
             int b = data[i+2];

             bmp.SetPixel(x, y, Color.FromArgb(a,r, g, B)/>/>);
             i += 3;
         }

     }
     return bmp;
}
private void button1_Click(object sender, EventArgs e)
{
     byte[] file = File.ReadAllBytes("C:\\Users\\user\\Desktop\\glava\\0219.img");

     Image img = GetPictureFromData(260, 260, file);
     pictureBox1.Image = img;
}



I don't know width and height of image, that's why I have put 260. And I used 260, because if I use greater number i goes out of bounds. I'm doing something wrong, because picture doesn't represent anything. I also used jpg format to test code, but also doesn't work.

If I'm right I can get resoultion via N=Math.Sqrt(data.Length / 2) but if I pass this resolution into function (for this case 512*512) my i goes out of bounds.

Is This A Good Question/Topic? 0
  • +

Replies To: Create image from binary data

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5894
  • View blog
  • Posts: 20,114
  • Joined: 05-May 12

Re: Create image from binary data

Posted 13 October 2017 - 12:28 PM

If the .img file is for NxN where each pixel is a short, then if you read in M shorts, that means that N is the square root of M.
Was This Post Helpful? 0
  • +
  • -

#3 Miko55  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-October 17

Re: Create image from binary data

Posted 13 October 2017 - 12:32 PM

View PostSkydiver, on 13 October 2017 - 12:28 PM, said:

If the .img file is for NxN where each pixel is a short, then if you read in M shorts, that means that N is the square root of M.


Well isn't that what I wrote? data.length/2 is bassicly M, short = 2 bytes so thats why I divide by 2. And then I square root that. But problem comes in loop, cause i goes out of bounds.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5894
  • View blog
  • Posts: 20,114
  • Joined: 05-May 12

Re: Create image from binary data

Posted 13 October 2017 - 12:35 PM

In most modern languages, shorts are 16 bits, while bytes are 8 bit. That will make a difference in how you handle your file data. Is the short Big Endian or Little Endian in your file?
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5894
  • View blog
  • Posts: 20,114
  • Joined: 05-May 12

Re: Create image from binary data

Posted 13 October 2017 - 01:19 PM

I didn't realize my phone wasn't loading the full page. I didn't see the rest of your text regarding computing the sizes taking into account the byte vs short and applying the square root. My apologies.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5894
  • View blog
  • Posts: 20,114
  • Joined: 05-May 12

Re: Create image from binary data

Posted 13 October 2017 - 01:24 PM

Anyway, the reason why i is going out of bounds is because you are advancing through the array at the rate of bytes, but you read in shorts.

For example if your file is 5x5 pixels. That means you have 25 shorts x 2 bytes/short == 50 bytes. When you loop 5 rows x 5 columns x 3 bytes/row/column == 75 bytes.
Was This Post Helpful? 0
  • +
  • -

#7 Miko55  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-October 17

Re: Create image from binary data

Posted 13 October 2017 - 03:39 PM

View PostSkydiver, on 13 October 2017 - 01:24 PM, said:

Anyway, the reason why i is going out of bounds is because you are advancing through the array at the rate of bytes, but you read in shorts.

For example if your file is 5x5 pixels. That means you have 25 shorts x 2 bytes/short == 50 bytes. When you loop 5 rows x 5 columns x 3 bytes/row/column == 75 bytes.

Okay, I kinda get it. But what I don't understand is how to fix this problem now ? RGB needs 3components, I cant use only 2.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5894
  • View blog
  • Posts: 20,114
  • Joined: 05-May 12

Re: Create image from binary data

Posted 13 October 2017 - 07:06 PM

In a typical 16-bits per pixel, you would either have:
  • 4 bits each for red, green, blue, and alpha
  • 5 bits each for red, green, and blue
  • 5 bits red, 6 bits green, and 5 bits blue

You will have to ask your teacher which of these formats is being used, as well as if the data is little endian or big endian.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1