Results 1 to 6 of 6

Thread: Trying to extract a image from a bigger file

  1. #1
    Join Date
    Jan 2012
    Location
    Canary Islands, Spain
    Posts
    86
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Trying to extract a image from a bigger file

    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.
    Qt Code:
    1. quint32 iconSize;
    2. QFile iconFile( filename );
    3. iconFile.open( QFile::ReadOnly );
    4. QDataStream iconStream( &iconFile );
    5.  
    6. iconFile.seek(0x1712);
    7. iconStream >> iconSize;
    8.  
    9. iconFile.seek(0x1716);
    10. char *iconData = new char[iconSize];
    11. iconStream.readRawData(iconData, iconSize);
    12. iconFile.close();
    13.  
    14. QFile test("test.png");
    15. test.open( QIODevice::WriteOnly );
    16. QDataStream out( &test );
    17. out.writeRawData(iconData, iconSize);
    18. delete[] iconData;
    19. test.close();
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jan 2012
    Location
    St. Petersburg, Russia
    Posts
    14
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: Trying to extract a image from a bigger file

    You can test whether an image is valid this way:

    Qt Code:
    1. if(!QImage().loadFromData(iconData, iconSize, "PNG"))
    2. {
    3. qDebug() << "image can't be read from a source";
    4. }
    To copy to clipboard, switch view to plain text mode 

    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.

  3. #3
    Join Date
    Jan 2012
    Location
    Canary Islands, Spain
    Posts
    86
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Trying to extract a image from a bigger file

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Trying to extract a image from a bigger file

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2012
    Location
    Canary Islands, Spain
    Posts
    86
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Trying to extract a image from a bigger file

    what should i use to read bineary data instead of QDataStream?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Trying to extract a image from a bigger file

    seek() and read() from QFile.

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

    Qt Code:
    1. quint32 iconSize;
    2. QFile iconFile( filename );
    3. iconFile.open( QFile::ReadOnly );
    4. iconFile.seek(0x1712);
    5. iconFile.read(4);
    6.  
    7. memcpy(&iconSize, buf.constData(), 4);
    8.  
    9. iconFile.seek(0x1716);
    10. buf = iconFile.read(iconSize);
    11. iconFile.close();
    12.  
    13. QFile test("test.png");
    14. test.open( QIODevice::WriteOnly );
    15. test.write(buf);
    16. test.close();
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Make image bigger on enterEvent, maller on leaveEvent
    By Globulus in forum Qt Programming
    Replies: 4
    Last Post: 10th August 2011, 15:38
  2. Make image bigger, when the mouse is over QLabel
    By Globulus in forum Qt Programming
    Replies: 2
    Last Post: 6th August 2011, 09:39
  3. How to extract all images from a QT3 .ui file
    By kshahar in forum Qt Programming
    Replies: 0
    Last Post: 4th December 2008, 15:27
  4. Replies: 7
    Last Post: 27th June 2007, 09:34
  5. How To Extract A File
    By deekayt in forum General Programming
    Replies: 7
    Last Post: 5th December 2006, 18:27

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.