[gdal-dev] reading compressed data?

Even Rouault even.rouault at mines-paris.org
Tue Jul 10 10:45:48 PDT 2012


Le mardi 10 juillet 2012 18:53:44, Joe Lyga a écrit :
> I'm new to GDAL, and I'm wondering if there's a way I can get the buffer
> of raw compressed image data.  I have an NITF file that contains a jpeg
> image.  I've been able to use the rasterio function to get uncompressed
> image data, but what I really need is to just send along that jpeg image
> without the NITF wrapping.  Is there a funciton to give me just raw
> compressed image data?  Thanks.

Joe,

No, there's no function to do that. However, you can find some hints with 
running gdalinfo with debugging :

$ gdalinfo byte_jpg.ntf --debug on
GDAL: NITFDataset::Open() as IC=C3 (JPEG compressed)

JPG: real_filename byte_jpg.ntf, offset=907, size=519

GDAL: GDALOpen(JPEG_SUBFILE:Q0,907,519,byte_jpg.ntf, this=0x204ce00) succeeds 
as JPEG.
GDAL: GDALOpen(byte_jpg.ntf, this=0x204c770) succeeds as NITF.

[...]

This reveals that there is a JPEG stream starting at offset 907 and of length 
519 bytes.

Which can be verified with :

$ gdalinfo /vsisubfile/907_519,byte_jpg.ntf -checksum
Driver: JPEG/JPEG JFIF
Files: /vsisubfile/907_519,byte_jpg.ntf
Size is 20, 20
Coordinate System is `'
Corner Coordinates:
Upper Left  (    0.0,    0.0)
Lower Left  (    0.0,   20.0)
Upper Right (   20.0,    0.0)
Lower Right (   20.0,   20.0)
Center      (   10.0,   10.0)
Band 1 Block=20x1 Type=Byte, ColorInterp=Gray
  Checksum=4743
  Image Structure Metadata:
    COMPRESSION=JPEG

Then, using the 
http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/python/samples/gdal_cp.py 
script :

$ python swig/python/samples/gdal_cp.py /vsisubfile/907_519,byte_jpg.ntf 
jpeg_stream_extracted.jpg

or more simply if your are on Linux :

$ dd if=byte_jpg.ntf of=jpeg_stream_extracted.jpg skip=907 count=519 bs=1

Then :

$ gdalinfo jpeg_stream_extracted.jpg -checksum
Driver: JPEG/JPEG JFIF
Files: jpeg_stream_extracted.jpg
Size is 20, 20
Coordinate System is `'
Corner Coordinates:
Upper Left  (    0.0,    0.0)
Lower Left  (    0.0,   20.0)
Upper Right (   20.0,    0.0)
Lower Right (   20.0,   20.0)
Center      (   10.0,   10.0)
Band 1 Block=20x1 Type=Byte, ColorInterp=Gray
  Checksum=4743
  Image Structure Metadata:
    COMPRESSION=JPEG

The above will work with IC=C3 compressed NITF files. For IC=M3, as you have 
several tiles, it will be a bit more complicated.


More information about the gdal-dev mailing list