Giter Club home page Giter Club logo

Comments (9)

geneishouko avatar geneishouko commented on June 4, 2024

I seem to have found the issue and it looks like a Qt bug. Setting aside that it equals Quality with Compression (probably influenced by imagemagick), it has a wrong assumption: more quality == less compression, or they read the libpng API wrong.

This is what the libpng headers say about setting the compression level.:

/* Set the library compression level.  Currently, valid values range from
 * 0 - 9, corresponding directly to the zlib compression levels 0 - 9
 * (0 - no compression, 9 - "maximal" compression).  Note that tests have
 * shown that zlib compression levels 3-6 usually perform as well as level 9
 * for PNG images, and do considerably fewer caclulations.  In the future,
 * these values may not correspond directly to the zlib compression levels.
 */

This is the problem code in qpnghandler.cpp:

static bool write_png_image(const QImage &image, QIODevice *device,
                            int quality, float gamma, const QString &description)
{
    QPNGImageWriter writer(device);
    if (quality >= 0) {
        quality = qMin(quality, 100);
        quality = (100-quality) * 9 / 91; // map [0,100] -> [9,0]
    }
    writer.setGamma(gamma);
    return writer.writeImage(image, quality, description);
}

It reverses the magnitude of the specified quality/compression setting, from 0 to 9 and from 100 to 0. Thus PNG images set to be saved with the highest quality are saved with the least compression.

from qimgv.

geneishouko avatar geneishouko commented on June 4, 2024

And it's been fixed for Qt 5.12 but you must set compression option and must not set quality for saving PNG images. The behavior in Qt is to map 0 quality to the highest compression of PNG images and 100 to the least compression.

from qimgv.

geneishouko avatar geneishouko commented on June 4, 2024

This is the current dev branch code in qpnghandler.cpp:

static bool write_png_image(const QImage &image, QIODevice *device,
                            int compression, int quality, float gamma, const QString &description)
{
    // quality is used for backward compatibility, maps to compression

    QPNGImageWriter writer(device);
    if (compression >= 0)
        compression = qMin(compression, 100);
    else if (quality >= 0)                                                                                                                                                                                                                            
        compression = 100 - qMin(quality, 100);

    if (compression >= 0)
        compression = (compression * 9) / 91; // map [0,100] -> [0,9]

    writer.setGamma(gamma);
    return writer.writeImage(image, compression, description);
}

from qimgv.

geneishouko avatar geneishouko commented on June 4, 2024

So if you could detect the Qt version at runtime and the file format you could handle both Qt behaviors and fix this bug.

from qimgv.

geneishouko avatar geneishouko commented on June 4, 2024

Or just use the correct old quality behavior when saving PNG images. No Qt version detection needed.

from qimgv.

geneishouko avatar geneishouko commented on June 4, 2024

Just now I noticed there is a dev branch and it hardcodes quality to 0. Are you sure it won't save JPEG files with lower quality?

from qimgv.

easymodo avatar easymodo commented on June 4, 2024

Just now I noticed there is a dev branch and it hardcodes quality to 0. Are you sure it won't save JPEG files with lower quality?

Yeah it will corrupt jpegs. That's a leftover code from when i was testing different compression / quality values.

And it's been fixed for Qt 5.12

Could you please post a link to a bugreport? I wanna see what they did

from qimgv.

easymodo avatar easymodo commented on June 4, 2024

Also do you think i should present a compression quality option in ui (for lossy formats) or leave it hardcoded at some value?

from qimgv.

geneishouko avatar geneishouko commented on June 4, 2024

There doesn't seem to be a bug report but a direct to review patch
https://code.qt.io/cgit/qt/qtbase.git/commit/?id=1192c463db34e3f69be4d9ef976246ce265143a3

I think a sensible hardcoded value is fine for PNG but not so much for JPEG. On the other hand if quality or file size is a concern then the user should use a different tool for image editing. Considering these I think no option is needed. Gwenview doesn't have it and many other image viewers don't either.

from qimgv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.