0

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 y = 0; y < h; y++)
     {
         for (int x = 0; x < w; x++)
         {
             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.

Miko
  • 363
  • 1
  • 8
  • 22
  • 1
    Each pixel is represented by two shorts, so most probably 4 bytes - you are currently reading bytes in groups of three. Please give us more information on the input format. – Bernd Fischer Oct 13 '17 at 09:50
  • if it's N x N, and each pixel is represented by ARGB, then `(file.length / 4) ^ 2` should give you N, no? – Icepickle Oct 13 '17 at 09:50
  • Picture resolution is `N x N`, where `N` is `Math.Sqrt(data.Length / 2)` (`short` = 2 bytes). From `524288` bytes it's `512x512`. And to read short from byte array you can either convert individual bytes (low byte + high byte * 256) or [convert byte array to short array](https://stackoverflow.com/q/1104599/1997232). – Sinatr Oct 13 '17 at 09:57
  • Oh okay, that helps for later, when I need to show image in dimension user defines (N). But now if I set dimension to 512X512 my i goes out of bounds. So I guess I'm not doing something right with RGB. @BerndFischer I can't give anything else, image is about CT scan and thats pretty much else. – Miko Oct 13 '17 at 10:19
  • I'm sorry: Without a better specification of the input format you're quite lost. If you have a lot of those images and/or know what they're supposed to look like, you may be able to find out more about the format. – Bernd Fischer Oct 13 '17 at 19:03
  • Actually this should already help you Miko, as you don't have to guess the height and width anymore, you can do the calculation and use that value to read the file – Icepickle Oct 13 '17 at 22:29
  • I suggest messing around with a tool like TiledGGD to see what the image format is... but yea, you need a complete header to decode the image. – Nyerguds Oct 17 '17 at 09:10

0 Answers0