3

I have a raw binary file containing color (RGB, 1byte per channel) with band interleaved by pixel (BIP) layout that I'd like to translate to a PNG. I could probably use some other non-GIS specific tool for this (recommendations welcome), but I'd prefer to use gdal_translate since it's already in my pipeline and has done a good job of translating other formats for me.

Is it possible to use raw files as input to gdal_translate?

  • What do you mean by "raw file"? If you are talking about camera raw images, there's not one but many raw formats, some similar, some very different. en.m.wikipedia.org/wiki/Raw_image_format – user2856 Feb 12 '17 at 3:26
  • Or do you mean you just have the samples in R, G, B, R, G, B.... order (binary or text)? Something like ARG? – BradHards Feb 12 '17 at 4:24
  • I mean raw in the since like R, G, B, R, G, B, etc.... I guess binary may be a better term for this. – user8709 Feb 12 '17 at 5:00
  • So it's a 3 band, 8 bit binary raster. Do you know the interleaving (BIL, BIP, BSQ)? Do you have a header file? Do you know the georeferencing? Do you know the coordinate reference system? If you can answer at least the first question (interleaving), then yes GDAL can read it. If you have a header that GDAL understands then it's easier, if not, you can make a VRT file to allow GDAL to read it. – user2856 Feb 12 '17 at 6:51
  • It's interleaved RGB values (BIP). Not sure how easy it would be to georeference. I at least know the bounds of the data, but the data itself is in a quadrilateralized spherical cube like projection. I'm not interested in warping it to a different projection though, just converting it to PNG. Maybe resizing it. – user8709 Feb 12 '17 at 7:43
3

You have an 8 bit 3 band binary file, interleaved by pixel. Assuming you don't already have a header file that GDAL understands and you know the number of columns and rows, you can create (in code or by hand) a VRT file that describes the layout of the binary file so that GDAL can read it. Write something like the following to a file (can be called anything you like)

<VRTDataset rasterXSize="400" rasterYSize="300">
  <VRTRasterBand dataType="Byte" band="1" subClass="VRTRawRasterBand">
    <SourceFilename relativetoVRT="0">/path/to/file.raw</SourceFilename>
    <ImageOffset>0</ImageOffset>
    <PixelOffset>3</PixelOffset>
    <LineOffset>1200</LineOffset>
  </VRTRasterBand>
  <VRTRasterBand dataType="Byte" band="2" subClass="VRTRawRasterBand">
    <SourceFilename relativetoVRT="0">/path/to/file.raw</SourceFilename>
    <ImageOffset>1</ImageOffset>
    <PixelOffset>3</PixelOffset>
    <LineOffset>1200</LineOffset>
  </VRTRasterBand>
  <VRTRasterBand dataType="Byte" band="3" subClass="VRTRawRasterBand">
    <SourceFilename relativetoVRT="0">/path/to/file.raw</SourceFilename>
    <ImageOffset>0</ImageOffset>
    <PixelOffset>3</PixelOffset>
    <LineOffset>1200</LineOffset>
  </VRTRasterBand>
</VRTDataset>

From the GDAL VRT tutorial:

  • SourceFilename: The name of the raw file containing the data for this band. The relativeToVRT attribute can be used to indicate if the SourceFilename is relative to the .vrt file (1) or not (0).

  • ImageOffset: The offset in bytes to the beginning of the first pixel of data of this image band. Defaults to zero.

  • PixelOffset: The offset in bytes from the beginning of one pixel and the next on the same line. In packed single band data this will be the size of the dataType in bytes.

  • LineOffset: The offset in bytes from the beginning of one scanline of data and the next scanline of data. In packed single band data this will be PixelOffset * rasterXSize.

So the only things you'll need to change in the example are rasterXSize, rasterYSize, SourceFilename and LineOffset. LineOffset will be 3 x rasterXSize.

You can then use gdal_translate to convert the VRT to PNG:

gdal_translate {appropriate options if required} /path/to/file.vrt /path/to/file.png

2

If you are trying to convert a 16-bit image to 8-bit then you should use the -scale_* parameter of gdal_translate to map a data range in the 16-bit space to a data range in the 8-bit space (docs).

gdal_translate input.tif output.png \
  -of PNG -ot Byte \
  -scale_1 5000 15000 -scale_2 6000 12000 -scale_3 8000 12000

The 16-bit scales are mapping to 0-255 by default, but you can specify a different range with two additional values -scale_1 5000 15000 64 128.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.