tags:

views:

304

answers:

1

I'm using the Qt library, creating QImages.

I'm able to use this constructor:

QImage image("example.jpg");

But I'm having trouble with this static function:

char buffer[sizeOfFile];
ifstream inFile("example.jpg");
inFile.read(buffer, sizeOfFile);
QImage image = QImage::fromData(buffer); // error here
// but there's nothing wrong with the buffer
ofstream outFile("bufferOut.jpg");
outFile.write(buffer, sizeOfFile);

Where Qt spits out to console:

Corrupt JPEG data: 1 extraneous bytes before marker 0xd9
JPEG datastream contains no image

The above isn't exactly what I have, but it's the only important difference. (I need to be able to read from a buffer because I'm opening images that are inside a zip archive.)

+1  A: 

Tnx to peppe from #qt on irc.freenode.net:

The solution is to explicitly include the buffer length. Ignoring a few unsigned char to char typecasting and other details, what I should have used is something akin to:

QImage image = QImage::fromData(buffer, sizeOfFile);
Kache4