In this TechNote we would like to present three ways to convert a raw (bit-stream) image into a conventional image file format. In this case we define a raw image as a series of bits. The .raw file contains only information about the image pixel values, there’s no header and no additional information included (in contrast to the camera raw files e.g. CR2, NEF, ARW, ORF, RAF etc.).

THE RAW FILE
As a sample image, we consider a 1280x800 grayscale image (8 bit). Because there is no additional information, each byte represents the gray value of one pixel. The .raw file opened in a hex-editor would look like the following.

hex full
figure 1: .raw file opend in hex editor

In the editor the file is represented by 1.024.000 (=1280*800) hexadecimal values. Because the counting starts at 0x00000, the last value 03 is at position 0xF9FFF (which is 1.023.999 in hexadecimal form). The bits are sorted line by line.

 

HOW TO CONVERT RAW IMAGES
There are many different ways to convert bit-stream images, we would like to present at least three. To be able to convert these .raw files two things must be known: the width and height of the image and the bit depth. As already mentioned, our file is a 1280x800 grayscale image (8 bit).

 

VIA PHOTOSHOP / IMAGEJ

Adobe Photoshop raw importAdobe Photoshop raw import
fig 2: Adobe Photoshop raw                                                                           fig 3: ImageJ raw import

The conversion via Adobe Photoshop and ImageJ is similar. We have to enter the image dimension and the bit depth. In Photoshop we have to enter the amount of channels in a separate edit field, whereas in ImageJ the pop-up menu “Image type” combines the bit depth and amount of channels. If the .raw files include a header you can choose an offset of several bytes to get access to the image data. After that, the images can be saved in any supported format.

 

VIA IMAGEMAGICK

ImageMagick convert via comandline
fig 4: ImageMagick  convert via comandline

ImageMagick maintains simple command-line processing for converting raw files. The executable “convert.exe” needs some flags:

-size 1280x800 (our image size)
-depth 8 (8bit/channel)
GRAY: <raw image path/filename> <converted image path/filename>


The “Explicit Image Format” GRAY identifies our image as a grayscale image, because the .raw extension does not specify a known format and has no header information. The output format can be chosen as you like, in this case we choose TIF. (type –help for more information)

 

THE CONVERTED IMAGE

converted image
fig 5: original image (left), crop (right)

The converted image will look like figure 5 left. If we take some sample values from the image e.g. row 96 and column 584 to 586, we can check if the bytes in the .raw file have the same values.

.raw file in hex editor
fig 6: .raw file in hex editor

The position of these sample values in hex code would be at 0x1E248 to 0x1E24A (<width> * <current row> + <current column> = 1280*96+584...585...586). As we can see, the values are the same:

(hex -> dec)
4E -> 78
AB -> 171
52 ->82

ck