From 4e04eff041ef50b878805da4a6747523afde3c4f Mon Sep 17 00:00:00 2001 From: Zach Bjornson Date: Sat, 21 Apr 2018 12:24:27 -0700 Subject: [PATCH] disableChromaSubsampling: false -> chromaSubsampling: true --- Readme.md | 2 +- lib/canvas.js | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Readme.md b/Readme.md index ff92a8548..8a9d87e59 100644 --- a/Readme.md +++ b/Readme.md @@ -171,7 +171,7 @@ var stream = canvas.jpegStream({ bufsize: 4096 // output buffer size in bytes, default: 4096 , quality: 75 // JPEG quality (0-100) default: 75 , progressive: false // true for progressive compression, default: false - , disableChromaSubsampling: false // true to disable 2x2 subsampling of the chroma components, default: false + , chromaSubsampling: true // false to disable 2x2 subsampling of the chroma components, default: true }); ``` diff --git a/lib/canvas.js b/lib/canvas.js index 8f2d74a42..a589a2e56 100644 --- a/lib/canvas.js +++ b/lib/canvas.js @@ -90,12 +90,21 @@ Canvas.prototype.createJPEGStream = function(options){ // Don't allow the buffer size to exceed the size of the canvas (#674) var maxBufSize = this.width * this.height * 4; var clampedBufSize = Math.min(options.bufsize || 4096, maxBufSize); + var chromaFactor; + if (typeof options.chromaSubsampling === "number") { + // libjpeg-turbo seems to complain about values above 2, but hopefully this + // can be supported in the future. For now 1 and 2 are valid. + // https://github.com/Automattic/node-canvas/pull/1092#issuecomment-366558028 + chromaFactor = options.chromaSubsampling; + } else { + chromaFactor = options.chromaSubsampling === false ? 1 : 2; + } return new JPEGStream(this, { bufsize: clampedBufSize , quality: options.quality || 75 , progressive: options.progressive || false - , chromaHSampFactor: options.disableChromaSubsampling ? 1 : 2 - , chromaVSampFactor: options.disableChromaSubsampling ? 1 : 2 + , chromaHSampFactor: chromaFactor + , chromaVSampFactor: chromaFactor }); };