What's default image quality in toDataURL?

感情迁移 提交于 2019-12-24 16:27:53

问题


Here is doc for toDataURL:

Syntax

canvas.toDataURL(type, encoderOptions);

Parameters

type Optional
    A DOMString indicating the image format. The default type is image/png.

encoderOptions Optional
    A Number between 0 and 1 indicating image quality if the requested type is image/jpeg or image/webp.
    If this argument is anything else, the default value for image quality is used. Other arguments are ignored. 

If this argument is anything else, the default value for image quality is used. Other arguments are ignored.. What's the default value for image quality?


回答1:


Blink: 0.92 for JPEG and 0.80 for WEBP.

Gecko: 0.92 for JPEG (no webp support)

How I found it

I searched for "toDataURL" using https://code.google.com/p/chromium/codesearch

Relevant code https://code.google.com/p/chromium/codesearch#chromium/src/third_party/WebKit/Source/core/html/HTMLCanvasElement.cpp&q=toDataURL&sq=package:chromium&l=517

Looks like it sets the quality like so double quality = UndefinedQualityValue; where UndefinedQualityValue; is a constant of -1.0.

Following the call chain further down we get to https://code.google.com/p/chromium/codesearch#chromium/src/third_party/WebKit/Source/platform/graphics/ImageBuffer.cpp&sq=package:chromium&l=348&rcl=1448382621

bool ImageDataBuffer::encodeImage(const String& mimeType, const double& quality, Vector<unsigned char>* encodedImage) const
{
    if (mimeType == "image/jpeg") {
        int compressionQuality = JPEGImageEncoder::DefaultCompressionQuality;
        if (quality >= 0.0 && quality <= 1.0)
            compressionQuality = static_cast<int>(quality * 100 + 0.5);
        if (!JPEGImageEncoder::encode(*this, compressionQuality, encodedImage))
            return false;
    } else if (mimeType == "image/webp") {
        int compressionQuality = WEBPImageEncoder::DefaultCompressionQuality;
        if (quality >= 0.0 && quality <= 1.0)
            compressionQuality = static_cast<int>(quality * 100 + 0.5);
        if (!WEBPImageEncoder::encode(*this, compressionQuality, encodedImage))
            return false;
    } else {
        if (!PNGImageEncoder::encode(*this, encodedImage))
            return false;
        ASSERT(mimeType == "image/png");
    }

    return true;
}

Where the values are as follows:

  • JPEGImageEncoder::DefaultCompressionQuality = 92 source
  • WEBPImageEncoder::DefaultCompressionQuality = 80 source

Gecko

We can follow a similar process as the above with mozilla code search, or just google "gecko image encoder" which takes you to the following value: JPEG: 0.92 source



来源:https://stackoverflow.com/questions/33904144/whats-default-image-quality-in-todataurl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!