PDA

View Full Version : Trying to extract a image from a bigger file



aguayro
13th January 2012, 22:43
Hi, first thing, i'm a very beginner programmer so please be patient with me and sorry for offensive coding practiques :) ( sorry for my english too... )
I'm here because i've been stucked about 24hours trying to extract a png from a bigger file, i've tryed a lot of things, QByteArrays, char arrays, pointers, QBuffers, etc... but all without sucess.

here is my code now ( it's changed a lot of times.... )

filename is a QString with the filename and path, its ok, i've checked it, same with the offsets, are also checked.


quint32 iconSize;
QFile iconFile( filename );
iconFile.open( QFile::ReadOnly );
QDataStream iconStream( &iconFile );

iconFile.seek(0x1712);
iconStream >> iconSize;

iconFile.seek(0x1716);
char *iconData = new char[iconSize];
iconStream.readRawData(iconData, iconSize);
iconFile.close();

QFile test("test.png");
test.open( QIODevice::WriteOnly );
QDataStream out( &test );
out.writeRawData(iconData, iconSize);
delete[] iconData;
test.close();


basically, i try to access the file, go to size offset to get the size for the char array, so i can read the exact size of the image with readRawData, then, save it into a test file, but fails,
probbably i'm doign a lot of thigs wrong, if anybody could help me i'd be very thankful.
The test.png file size its ok ( 8.124 bytes ), but its ungrecognizable, corrupt data or something, the win7 image viewer says "Image is corrput or too big",

Thanks.

monst
14th January 2012, 09:24
You can test whether an image is valid this way:



if(!QImage().loadFromData(iconData, iconSize, "PNG"))
{
qDebug() << "image can't be read from a source";
}


Also you can try a QImageReader's functions. If any of them will fail, use QImageReader::errorString() to get more information about error.

Maybe a PNG shared library is not linked correctly to your program.
You can also QImageReader::supportedImageFormats() to get information about supported read formats.

aguayro
14th January 2012, 14:20
thanks a lot for the reply, png, jpg, etc... are all linked and working, must be something on the code, i'll try that error finder.
Thanks a lot and sorry for bothering.

wysota
14th January 2012, 15:18
Unless the big file was serialized using QDataStream, you shouldn't use QDataStream for reading. Contrary to what some people think it is not a general purpose binary stream.

aguayro
15th January 2012, 01:14
what should i use to read bineary data instead of QDataStream?

wysota
15th January 2012, 01:16
seek() and read() from QFile.

Ignoring things like byte order (and possibly some cast warnings/errors) your code should look like this:


quint32 iconSize;
QFile iconFile( filename );
iconFile.open( QFile::ReadOnly );
iconFile.seek(0x1712);
QByteArray buf;
iconFile.read(4);

memcpy(&iconSize, buf.constData(), 4);

iconFile.seek(0x1716);
buf = iconFile.read(iconSize);
iconFile.close();

QFile test("test.png");
test.open( QIODevice::WriteOnly );
test.write(buf);
test.close();