Results 1 to 15 of 15

Thread: Generate image checksum

  1. #1
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Generate image checksum

    Here is what I'm doing to calculate an Image checksum :

    Qt Code:
    1. quint16 ZePicturePool::generateChecksum(const QPixmap & pixmap)
    2. {
    3. QImage image = pixmap.toImage();
    4. quint16 checksum = qChecksum((char *) (image.bits()), image.numBytes());
    5.  
    6. return checksum;
    7. }
    To copy to clipboard, switch view to plain text mode 

    Is it the good approach ?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Generate image checksum

    What do you need that checksum for?

  3. #3
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Generate image checksum

    To check wether a Pixmap is already in my cache or not.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Generate image checksum

    How many pixmaps are you going to operate on? Also take a look at QPixmapCache.

  5. #5
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Generate image checksum

    I just want to validate if my checksum calculation is good .

    And yes I'm using pixmap cache, in my case pixmap's path is not enough to define a pixmap.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Generate image checksum

    Quote Originally Posted by bunjee View Post
    I just want to validate if my checksum calculation is good .
    It's OK, if you won't have a lot of pixmaps.
    Last edited by jacek; 8th June 2008 at 22:21. Reason: typo

  7. The following user says thank you to jacek for this useful post:

    bunjee (23rd May 2008)

  8. #7
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Generate image checksum

    Unfortunately I get some glitches using that approach.

    Since I'm using a huge number of pixmap, sometimes it picks the same one.

    How can I refine this ?

  9. #8
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Generate image checksum

    How about a combination of file path and creation date?

  10. #9
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Generate image checksum

    Not good in my case I want to rely on QImage's data and that's all.

    I've seen there is a 32bit checksum in Zlib, has anyone ever used that ? I don't want to add one dependency just for a checksum.

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Generate image checksum

    Quote Originally Posted by bunjee View Post
    Since I'm using a huge number of pixmap, sometimes it picks the same one.

    How can I refine this ?
    You need a longer checksum, like MD5 hash. Of course it will take more time to calculate it and still there is some probability of a collision.

    Maybe there is some other way to assign an unique identifier to those pixmaps? Where do they come from?

  12. #11
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Generate image checksum

    Well I'm developping an instant messenging app.

    Basically this is all the contact avatars.

    BUT cached and displayed in various size, so I can have the same base pixmap but resized AND cached.

    I think I might use a combination of checksum and pixmap size to match them.

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Generate image checksum

    Quote Originally Posted by bunjee View Post
    Basically this is all the contact avatars.
    What is the maximum size of such pixmap?

    Quote Originally Posted by bunjee View Post
    BUT cached and displayed in various size, so I can have the same base pixmap but resized AND cached.
    Is the checksum valid only during a single session or you want to keep it after user closes the application? Is it important for you that two exactly the same pixmaps that come from two different sources have the same checksum?

  14. #13
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: Generate image checksum

    What is the maximum size of such pixmap?
    No maximum size.

    Is the checksum valid only during a single session or you want to keep it after user closes the application?
    Valid during a given session.

    Is it important for you that two exactly the same pixmaps that come from two different sources have the same checksum?
    Yes it is, it speeds up the display that I don't regenerate the pixmap.

  15. #14
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Generate image checksum

    I use md5 to check image stream or to stop connection param if the same as last..

    Qt Code:
    1. static inline QByteArray fastmd5( const QString xml )
    2. {
    3. QCryptographicHash formats( QCryptographicHash::Md5 );
    4. formats.addData(xml.toUtf8());
    5. return formats.result();
    6. }
    7.  
    8. static inline QByteArray fastmd5( const QByteArray xml )
    9. {
    10. QCryptographicHash formats( QCryptographicHash::Md5 );
    11. formats.addData(xml);
    12. return formats.result();
    13. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. /* fill params */
    2. /* take qmenu from other layer edit or same as last? */
    3. void PageWindow::PlayOn()
    4. {
    5. TextLayer *iter = CurrentView->LayerModel();
    6. const int LayID = iter->data(ObjectNameEditor).toInt();
    7. const QString editable = LayID == 0 ? "no" : "yes";
    8. bool canedit = LayID == 0 ? false : true;
    9. const QString Pmyhash = QString("%1-%2-%3-%4-%5").arg(editable) /* can edit area */
    10. .arg(Clang) /* language */
    11. .arg(wdoc->ID()) /* document nr .*/
    12. .arg(LayID) /* layer nummer*/
    13. .arg(iter->textCursor().position()); /* cursor position */
    14. emit menupage(canedit,fastmd5(Pmyhash));
    15. }
    16.  
    17.  
    18. void Gui_Main::editor( bool e , const QByteArray hashpage )
    19. {
    20. if (LastHash == hashpage) {
    21. /* not make job ist the same edit status as last! */
    22. return;
    23. } else {
    24. LastHash = hashpage;
    25. }
    26. /* continue ok*/
    To copy to clipboard, switch view to plain text mode 



    My QImage cache work in this way to compose Multiple Animated Portable Network Graphics APNG... and all frame is unique...

    http://www.qt-apps.org/content/show....?content=82221


    Qt Code:
    1. class StreamFile
    2. {
    3. public:
    4. StreamFile()
    5. :d(new QBuffer())
    6. {
    7. d->open(QIODevice::ReadWrite);
    8. start();
    9. }
    10. ~StreamFile()
    11. {
    12. d->close();
    13. }
    14. bool clear()
    15. {
    16. d->write(QByteArray());
    17. return d->bytesAvailable() == 0 ? true : false;
    18. }
    19. void start() {
    20. d->seek(0);
    21. }
    22. bool LoadFile( const QString file ) {
    23. if (clear()) {
    24. QFile f(file);
    25. if (f.exists()) {
    26. if (f.open(QFile::ReadOnly)) {
    27. d->write(f.readAll());
    28. f.close();
    29. start();
    30. return true;
    31. }
    32. }
    33. }
    34. return false;
    35. }
    36. bool PutOnFile( const QString file ) {
    37. QFile f(file);
    38. if (f.open(QFile::WriteOnly)) {
    39. uint bi = f.write(d->readAll());
    40. start();
    41. return bi > 0 ? true : false;
    42. }
    43. return false;
    44. }
    45. QBuffer *device() { return d; }
    46. bool isValid() { return img.loadFromData(stream()); }
    47. QByteArray stream() { return d->data(); }
    48. QImage img;
    49. QBuffer *d;
    50. };
    To copy to clipboard, switch view to plain text mode 

  16. The following user says thank you to patrik08 for this useful post:

    bunjee (9th June 2008)

  17. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Generate image checksum

    Quote Originally Posted by bunjee View Post
    Valid during a given session.
    Ok, so this reduces the chance of a collision.

    Quote Originally Posted by bunjee View Post
    No maximum size.
    [...]
    Yes it is, it speeds up the display that I don't regenerate the pixmap.
    But unfortunately these two make things worse.

    MD5 hashes have 128 bits, so a chance for a collision is quite low and they're of better quality than simple CRCs. The only problem is that they take more time to compute, but I don't think that it will be noticeable for the user if you compute them once per image.

Similar Threads

  1. Finding marks on scanned image for alignment
    By caduel in forum Qt Programming
    Replies: 1
    Last Post: 23rd September 2007, 02:10
  2. Explanation to Image Formats
    By sincnarf in forum Qt Programming
    Replies: 13
    Last Post: 6th July 2007, 17:02
  3. Help needed handling image data
    By toratora in forum General Programming
    Replies: 2
    Last Post: 11th May 2007, 09:24
  4. how i can add image in my toolbar
    By jyoti in forum Qt Tools
    Replies: 7
    Last Post: 19th December 2006, 14:39
  5. How and when to repaint a widget ?
    By yellowmat in forum Newbie
    Replies: 7
    Last Post: 3rd April 2006, 16:36

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.