Bug 1750475. Allow quality parameter to be passed through the webp encoder from canvas data url encoding functions. r=lsalzman

The necessary code was already there, just needed to allow it through.

Differential Revision: https://phabricator.services.mozilla.com/D136092
This commit is contained in:
Timothy Nikkel 2022-01-19 09:50:33 +00:00
Родитель 417e3d731c
Коммит 32bad1b68a
3 изменённых файлов: 50 добавлений и 33 удалений

Просмотреть файл

@ -261,8 +261,9 @@ nsresult CanvasRenderingContextHelper::UpdateContext(
nsresult CanvasRenderingContextHelper::ParseParams(
JSContext* aCx, const nsAString& aType, const JS::Value& aEncoderOptions,
nsAString& outParams, bool* const outUsingCustomParseOptions) {
// Quality parameter is only valid for the image/jpeg MIME type
if (aType.EqualsLiteral("image/jpeg")) {
// Quality parameter is only valid for the image/jpeg and image/webp MIME
// types.
if (aType.EqualsLiteral("image/jpeg") || aType.EqualsLiteral("image/webp")) {
if (aEncoderOptions.isNumber()) {
double quality = aEncoderOptions.toNumber();
// Quality must be between 0.0 and 1.0, inclusive

Просмотреть файл

@ -4,13 +4,13 @@
<link rel="stylesheet" href="/tests/SimpleTest/test.css">
<body>
<p>
This test covers the JPEG quality parameter. If (when) the HTML5 spec changes the
This test covers the JPEG and webp quality parameter. If (when) the HTML5 spec changes the
allowed parameters for ToDataURL, new tests should go here.
</p>
<canvas id="c" width="100" height="100"><p class="fallback">FAIL (fallback content)</p></canvas>
<script>
var canvas = document.getElementById('c');
var ctx = canvas.getContext("2d");
let canvas = document.getElementById('c');
let ctx = canvas.getContext("2d");
ctx.strokeStyle = '#FF0000';
ctx.fillStyle = '#00FF00';
@ -20,35 +20,44 @@ ctx.moveTo(10, 10);
ctx.lineTo(90, 90);
ctx.stroke();
var pngData = canvas.toDataURL('image/png');
var pngQuality = canvas.toDataURL('image/png', 0.1);
let pngData = canvas.toDataURL('image/png');
let pngQuality = canvas.toDataURL('image/png', 0.1);
is(pngQuality, pngData, "Quality is not supported for PNG images");
var data = canvas.toDataURL('image/jpeg');
if (data.match(/^data:image\/jpeg[;,]/)) {
// Test the JPEG quality parameter
var fullQuality = canvas.toDataURL('image/jpeg', 1.0);
var lowQuality = canvas.toDataURL('image/jpeg', 0.1);
isnot(lowQuality, fullQuality, "A low quality (0.1) should differ from high quality (1.0)");
var medQuality = canvas.toDataURL('image/jpeg', 0.5);
isnot(medQuality, fullQuality, "A medium quality (0.5) should differ from high (1.0)");
isnot(medQuality, lowQuality, "A medium quality (0.5) should differ from low (0.5)");
var tooHigh = canvas.toDataURL('image/jpeg', 2.0);
is(tooHigh, data, "Quality above 1.0 is treated as unspecified");
var tooLow = canvas.toDataURL('image/jpeg', -1.0);
is(tooLow, data, "Quality below 0.0 is treated as unspecified");
var lowQualityExtra = canvas.toDataURL('image/jpeg', 0.1, 'foo', 'bar', null);
is(lowQualityExtra, lowQuality, "Quality applies even if extra arguments are present");
var lowQualityUppercase = canvas.toDataURL('IMAGE/JPEG', 0.1);
is(lowQualityUppercase, lowQuality, "Quality applies to image/jpeg regardless of case");
var lowQualityString = canvas.toDataURL('image/jpeg', '0.1');
isnot(lowQualityString, lowQuality, "Quality must be a number (should not be a string)");
function checkType(imagetype) {
let imageTypeString = 'image/' + imagetype;
let imageTypeUpperString = 'IMAGE/' + imagetype.toUpperCase();
let data = canvas.toDataURL(imageTypeString);
if (data.match(new RegExp('^data:image\/' + imagetype + '[;,]'))) {
// Test the JPEG/wepb quality parameter
let fullQuality = canvas.toDataURL(imageTypeString, 1.0);
let lowQuality = canvas.toDataURL(imageTypeString, 0.1);
isnot(lowQuality, fullQuality, "A low quality (0.1) should differ from high quality (1.0) " + imageTypeString);
let medQuality = canvas.toDataURL(imageTypeString, 0.5);
isnot(medQuality, fullQuality, "A medium quality (0.5) should differ from high (1.0) " + imageTypeString);
isnot(medQuality, lowQuality, "A medium quality (0.5) should differ from low (0.5) " + imageTypeString);
let tooHigh = canvas.toDataURL(imageTypeString, 2.0);
is(tooHigh, data, "Quality above 1.0 is treated as unspecified " + imageTypeString);
let tooLow = canvas.toDataURL(imageTypeString, -1.0);
is(tooLow, data, "Quality below 0.0 is treated as unspecified " + imageTypeString);
let lowQualityExtra = canvas.toDataURL(imageTypeString, 0.1, 'foo', 'bar', null);
is(lowQualityExtra, lowQuality, "Quality applies even if extra arguments are present " + imageTypeString);
let lowQualityUppercase = canvas.toDataURL(imageTypeUpperString, 0.1);
is(lowQualityUppercase, lowQuality, "Quality applies to " + imageTypeString + " regardless of case");
let lowQualityString = canvas.toDataURL(imageTypeString, '0.1');
isnot(lowQualityString, lowQuality, "Quality must be a number (should not be a string) " + imageTypeString);
} else {
ok(false, "should get a data url " + imageTypeString);
}
}
checkType('jpeg');
checkType('webp');
</script>

Просмотреть файл

@ -64,6 +64,13 @@ interface imgIEncoder : nsIAsyncInputStream
// quality=# -- default: "92"
// Quality of compression, 0-100 (worst-best).
// Quality >= 90 prevents down-sampling of the color channels.
//
//
// WEBP:
// -----
//
// quality=# -- default: "92"
// Quality of compression, 0-100 (worst-best).
// Possible values for input format (note that not all image formats