Opened 8 years ago

Closed 8 years ago

#12702 closed defect (fixed)

Loading of TGA image (only RGB, no RLE) fails

Reported by: stezkmil Owned by:
Priority: normal Milestone:
Component: base Version: stable-latest
Keywords: wxImage TGA RLE CUR Cc:
Blocked By: Blocking:
Patch: no


After using wxInitAllImageHandlers() , loading a TGA image using wxImage fails. The TGA image has to be RGB (not indexed) and must NOT have RLE compression.

The cause of this issue is quite a coincidence. The method bool wxImage::LoadFile( wxInputStream& stream, wxBitmapType type, int index ) scans through all image handlers and calls CanRead (as shown on the line of code below. the line is taken from the file src/common/image.cpp) method to see if it is the corresponding format.

if ( handler->CanRead(stream) && DoLoad(*handler, stream, index) )

The thing is that the header of *.CUR and *.TGA (RGB, no RLE) is the same. They both begin with bytes 00 00 02 00. The CUR file handler is found first, so its CanRead method returns true and the TGA file is misidentified as a CUR. Call to the DoLoad method follows. But the

handler.LoadFile(this, stream, true/*verbose*/, index)

call in the wxImage::DoLoad method returns false, so the image is not loaded BUT the file stream is left on the sixth byte and is not properly returned to the beginning of the file, therefore all the following handlers (including the TGA handler) start to read from a wrong position and in the end, the image is not loaded.

My proposed solution is this modification of the DoLoad method:

wxFileOffset posOld = stream.TellI();

if ( !handler.LoadFile(this, stream, true/*verbose*/, index) )
        return false;

Change History (2)

comment:1 Changed 8 years ago by vadz

  • Status changed from new to confirmed

I think ideal would be to improve the detection heuristics in CUR handler, can we distinguish between the two types of files by looking at the next few bytes by chance?

If not, we probably should indeed rewind the stream as suggested. But this would be less efficient.

comment:2 Changed 8 years ago by VZ

  • Resolution set to fixed
  • Status changed from confirmed to closed

(In [66252]) Rewind the input stream after failing to load image from it.

For seekable streams, don't change the current position when loading image
fails. This allows the subsequent image handlers to succeed during image
format auto-detection even if a previous, erroneously chosen, handler failed.

Closes #12702.

Note: See TracTickets for help on using tickets.