Skip to content

Commit

Permalink
fix: webp(80) and jpeg(92) use different quality
Browse files Browse the repository at this point in the history
  • Loading branch information
yisibl committed Oct 25, 2021
1 parent 04d401a commit fdefa5d
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ const MIME_PNG: &str = "image/png";
const MIME_JPEG: &str = "image/jpeg";
const MIME_AVIF: &str = "image/avif";

const DEFAULT_IMAGE_QUALITY: u8 = 80;
// Consistent with the default value of JPEG quality in Blink
// https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/platform/image-encoders/image_encoder.cc;l=85;drc=81c6f843fdfd8ef660d733289a7a32abe68e247a
const DEFAULT_JPEG_QUALITY: u8 = 92;

// Consistent with the default value of WebP quality in Blink
// https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/platform/image-encoders/image_encoder.cc;l=100;drc=81c6f843fdfd8ef660d733289a7a32abe68e247a
const DEFAULT_WEBP_QUALITY: u8 = 80;

#[module_exports]
fn init(mut exports: JsObject, env: Env) -> Result<()> {
Expand Down Expand Up @@ -164,8 +170,10 @@ fn encode(ctx: CallContext) -> Result<JsObject> {
let format_str = format.as_str()?;
let quality = if format_str != "avif" {
ctx.get::<JsNumber>(1)?.get_uint32()? as u8
} else if format_str == "webp" {
DEFAULT_WEBP_QUALITY
} else {
DEFAULT_IMAGE_QUALITY
DEFAULT_JPEG_QUALITY
};
let this = ctx.this_unchecked::<JsObject>();
let ctx_js = this.get_named_property::<JsObject>("ctx")?;
Expand Down Expand Up @@ -197,8 +205,10 @@ fn encode_sync(ctx: CallContext) -> Result<JsBuffer> {
let format_str = format.as_str()?;
let quality = if format_str != "avif" {
ctx.get::<JsNumber>(1)?.get_uint32()? as u8
} else if format_str == "webp" {
DEFAULT_WEBP_QUALITY
} else {
DEFAULT_IMAGE_QUALITY
DEFAULT_JPEG_QUALITY
};
let this = ctx.this_unchecked::<JsObject>();
let ctx_js = this.get_named_property::<JsObject>("ctx")?;
Expand Down Expand Up @@ -271,7 +281,7 @@ fn to_buffer(ctx: CallContext) -> Result<JsBuffer> {
let mime_js = ctx.get::<JsString>(0)?.into_utf8()?;
let mime = mime_js.as_str()?;
let quality = if ctx.length < 2 {
DEFAULT_IMAGE_QUALITY
DEFAULT_JPEG_QUALITY
} else {
ctx.get::<JsNumber>(1)?.get_uint32()? as u8
};
Expand Down Expand Up @@ -324,7 +334,7 @@ fn to_data_url(ctx: CallContext) -> Result<JsString> {
let mime = mime_js.as_str()?;
let quality = if ctx.length < 2 {
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL
DEFAULT_IMAGE_QUALITY
DEFAULT_JPEG_QUALITY
} else {
ctx.get::<JsNumber>(1)?.get_uint32()? as u8
};
Expand All @@ -347,7 +357,7 @@ fn to_data_url_async(ctx: CallContext) -> Result<JsObject> {
let mime = mime_js.as_str()?;
let quality = if ctx.length < 2 {
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL
DEFAULT_IMAGE_QUALITY
DEFAULT_JPEG_QUALITY
} else {
ctx.get::<JsNumber>(1)?.get_uint32()? as u8
};
Expand Down

1 comment on commit fdefa5d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: fdefa5d Previous: ae55276 Ratio
Draw house#skia-canvas 20.2 ops/sec (±1.27%) 22 ops/sec (±0.4%) 1.09
Draw house#node-canvas 21.2 ops/sec (±0.87%) 18 ops/sec (±0.37%) 0.85
Draw house#@napi-rs/skia 20.1 ops/sec (±0.45%) 20 ops/sec (±0.93%) 1.00
Draw gradient#skia-canvas 19.6 ops/sec (±0.76%) 21 ops/sec (±0.2%) 1.07
Draw gradient#node-canvas 20.3 ops/sec (±0.42%) 17 ops/sec (±0.13%) 0.84
Draw gradient#@napi-rs/skia 19.3 ops/sec (±0.44%) 19 ops/sec (±0.71%) 0.98

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.