0

I have a simple encoding/ decoding application using Windows Imaging Component API. The issue I'm having is that when I use either the JPEGXR or BMP formats, everything works fine. However, when I use the JPEG codec - the encoder works fine and I can visually verify the generated JPEG image, but when I try to decode that stream, I get a WINCODEC_ERR_BADHEADER (0x88982f61)

Here's the line that fails:

    hr = m_pFactory->CreateDecoderFromStream(
                                    pInputStream, 
                                    NULL,
                                    WICDecodeMetadataCacheOnDemand,
                                    &pDecoder);

Here pInputStream is an IStream created from a byte array (output of the encoder - a black box which outputs a byte vector).

Please help! This is driving me nuts!

Sau
  • 326
  • 2
  • 15
  • So maybe it is bad stream as it suggests? – Roman R. Oct 17 '12 at 05:36
  • What makes a "bad" stream? When I write out the encoded bytes to a file, it renders perfectly fine. And I'm using the exact same code for JPEGXR and BMP (only the encoding GUID is changed) – Sau Oct 17 '12 at 05:48
  • 1
    Maybe you are passing `IStream` with current position at the end of the stream, or in the middle, and not at the beginning of the JPEG file. – Roman R. Oct 17 '12 at 05:49
  • Dang I feel stupid...that was it. I added a seek statement and now it's fine! It's strange how it sailed through the other codecs though. Thanks! – Sau Oct 17 '12 at 06:03

1 Answers1

2

When passing stream as an argument, make sure to pre-seek it to proper initial position (esp. seek it back to the beginning if you just wrote data into it and expect further retrieval). APIs are typically not expected to seek, because this way they let you provide data in the middle of a bigger stream.

Roman R.
  • 68,205
  • 6
  • 94
  • 158