Opened 12 years ago

Closed 11 years ago

#4702 closed defect (invalid)

Gdal.Open throws invalid file format ApplicationException for JPEG virtual file.

Reported by: panzel Owned by: warmerdam
Priority: normal Milestone:
Component: GDAL_Raster Version: svn-trunk
Severity: normal Keywords: JPEG VSI
Cc:

Description

According to "gdalinfo --format JPEG" (version GDAL 2.0dev, released 2011/12/29), the JPEG driver supports Virtual IO. When Gdal.Open is invoked against a JPEG file stored in a byte array, it throws an Application Exception, reporting that the virtual file is not in a supported format. The following C# code snippet demonstrates the problem, provided the attached JPEG file is stored in "c:\temp". The JPEG file was retrieved from Microsoft Bing, at URL http://ecn.t0.tiles.virtualearth.net/tiles/a0230123113?g=409&mkt=en-US&n=z.

byte[] jpegBuffer = File.ReadAllBytes(@"C:\temp\a0230123113.jpg");

Gdal.AllRegister();

GCHandle handle = GCHandle.Alloc(jpegBuffer.Length, GCHandleType.Pinned);
string memFilename = "/vsimem/";
try
{
  Gdal.FileFromMemBuffer(memFilename, jpegBuffer.Length, handle.AddrOfPinnedObject());

  // This throws an ApplicationException:
  //   `/vsimem/' not recognised as a supported file format.
  Dataset datasetFromBuffer = Gdal.Open(memFilename, Access.GA_ReadOnly);
}
catch (Exception ex)
{
  Console.WriteLine(ex.Message);
}
finally
{
  Gdal.Unlink(memFilename);
  handle.Free();
}

Attachments (1)

a0230123113.jpg (19.1 KB ) - added by panzel 12 years ago.
Downloaded from http://ecn.t0.tiles.virtualearth.net/tiles/a0230123113?g=409&mkt=en-US&n=z

Download all attachments as: .zip

Change History (5)

comment:1 by warmerdam, 12 years ago

Keywords: VSI added; Open ApplicationException removed

Hmm, odd. It isn't very handy for Even or I to do stuff in C#, but if I try something related it seems to work ok:

gdalinfo http://home.gdal.org/warmerda/head3.jpg --debug on
HTTP: Fetch(http://home.gdal.org/warmerda/head3.jpg)
GDAL: GDALOpen(/vsimem/http_1/head3.jpg, this=0x209a730) succeeds as JPEG.
GDAL: GDALOpen(http://home.gdal.org/warmerda/head3.jpg, this=0x209a730) succeeds as HTTP.
Driver: JPEG/JPEG JFIF
GDAL: GDALDefaultOverviews::OverviewScan()
Files: none associated
Size is 499, 537
...

Perhaps the problem is the nature of your filename? Could you try:

   string memFilename = "/vsimem/work.jpg";

comment:2 by panzel, 12 years ago

Frank, a similar error occurred: "`/vsimem/work.jpg' not recognised as a supported file format."

Here's the gdalinfo-generated description for the specific Bing tile JPEG file:

Driver: JPEG/JPEG JFIF
Files: \WssFileShare\BingTiles\02301\a0230123113.jpg
       \WssFileShare\BingTiles\02301\a0230123113.wld
Size is 256, 256
Coordinate System is `'
Origin = (-13188827.045465685000000,4031059.560675335100000)
Pixel Size = (152.874056570352990,-152.874056570352990)
Image Structure Metadata:
  COMPRESSION=JPEG
  INTERLEAVE=PIXEL
  SOURCE_COLOR_SPACE=YCbCr
Corner Coordinates:
Upper Left  (-13188827.045, 4031059.561)
Lower Left  (-13188827.045, 3991923.802)
Upper Right (-13149691.287, 4031059.561)
Lower Right (-13149691.287, 3991923.802)
Center      (-13169259.166, 4011491.681)
Band 1 Block=256x1 Type=Byte, ColorInterp=Red
  Image Structure Metadata:
    COMPRESSION=JPEG
Band 2 Block=256x1 Type=Byte, ColorInterp=Green
  Image Structure Metadata:
    COMPRESSION=JPEG
Band 3 Block=256x1 Type=Byte, ColorInterp=Blue
  Image Structure Metadata:
    COMPRESSION=JPEG

comment:3 by tamas, 11 years ago

The example above allocates the handle for wrong object so incorrect address is passed to GDAL therefore the jpeg driver receives wrong header bytes. You should modify the corresponding row to:

GCHandle handle = GCHandle.Alloc(jpegBuffer, GCHandleType.Pinned);

comment:4 by tamas, 11 years ago

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.