2

I need to calculate the image width and height from the actual image file, so I'm reading the image with open file. so I have bunch of characters and numbers and everything that seems meaningless and they are presenting rgb information probably. I just want to calculate the size of the image with the raw file information I am programming in Erlang language but the code in any language will help as we are working with raw file as long as we don't use built-in libraries.

Thank you all in advance for help

Khashayar
  • 2,014
  • 3
  • 22
  • 31

4 Answers4

3

I found the answer by going to details of each format, So it works like this

JPG : you can find the width and height after the bytes "255,192,0,17,8" after that its the information for size

PNG : you can find it after "IHDR"

GIF : you can find it after "GIF89a"

there are information for more but this is the most common image types on internet

Thank you all for your time

Khashayar
  • 2,014
  • 3
  • 22
  • 31
1

I assume when you say 'raw' you mean you only have the pixel values.
In this case there isn't always a way to know the width and height.

Say you read 400 pixels. In this case a valid image side may be any whole factorization of 400, e.g. 1x400, 2x200, 4x100, 8x50, 20x20 etc. and transposed as well.

Not to mention the fact that many image formats include some padding for pixel rows that are not multiples of 4, 8 or 16...

Adi Shavit
  • 16,743
  • 5
  • 67
  • 137
  • so how do actual programs figure the size of the image? like if you go on properties it has the width and height!! how do they calculate this! – Khashayar Nov 21 '12 at 13:23
  • 1
    They don't calculate it. It is written in the image header along with other meta-data like number of channels, pixel depth, compression type etc. – Adi Shavit Nov 21 '12 at 13:36
  • How can I read this header through the program and get the properties as you said? where are they saved? – Khashayar Nov 21 '12 at 13:47
  • That depends on the image format. It is different for each format. The simplest formats are probably of the PPM, PGM, and PBM family, which are like raw with simple text headers. – Adi Shavit Nov 21 '12 at 14:02
  • so can u point me in a direction that I can find all the format specification?! – Khashayar Nov 21 '12 at 14:55
  • 2
    @KhashayarNapster, you are the only one who knows what format the images are in. If you were to name the format we might be able to point you to a spec. – Mark Ransom Nov 21 '12 at 17:58
0

The way it is coded in the image file depend on the image type, which hopefully is also coded in the image file. you can have a look at the question Getting Image size of JPEG from its binary for an example with JPEG coding.

Community
  • 1
  • 1
Pascal
  • 13,977
  • 2
  • 24
  • 32
0

If your data is unknown, use Octave and load the image. Then take a look at this page:

http://www.gnu.org/software/octave/doc/interpreter/Displaying-Images.html

for commands to display images. Hopefully with some manipulation it will work. This works for raw images, though there are specific decoders. Once you understand how the image is, you can write the equivalent C code.

Gustavo Litovsky
  • 2,457
  • 1
  • 29
  • 41