allow js to set jpeg quality

This commit is contained in:
Lily Tsuru 2024-07-24 04:53:08 -04:00
parent b127e37323
commit ce6f37e229
4 changed files with 13 additions and 7 deletions

View file

@ -14,6 +14,7 @@ export class JPEGEncoder {
width: rect.width, width: rect.width,
height: rect.height, height: rect.height,
stride: displaySize.width, stride: displaySize.width,
quality: gJpegQuality,
buffer: canvas.subarray(offset) buffer: canvas.subarray(offset)
}); });
} }

10
jpeg-rs/index.d.ts vendored
View file

@ -1,10 +1,11 @@
// //
interface JpegInputArgs { interface JpegInputArgs {
width: number, width: number;
height: number, height: number;
stride: number, // The width of your input framebuffer OR your image width (if encoding a full image) stride: number; // The width of your input framebuffer OR your image width (if encoding a full image)
buffer: Buffer quality: number | undefined;
buffer: Buffer;
// TODO: Allow different formats, or export a boxed ffi object which can store a format // TODO: Allow different formats, or export a boxed ffi object which can store a format
// (i.e: new JpegEncoder(FORMAT_xxx)). // (i.e: new JpegEncoder(FORMAT_xxx)).
@ -12,4 +13,3 @@ interface JpegInputArgs {
/// Performs JPEG encoding. /// Performs JPEG encoding.
export function jpegEncode(input: JpegInputArgs): Promise<Buffer>; export function jpegEncode(input: JpegInputArgs): Promise<Buffer>;

Binary file not shown.

View file

@ -31,6 +31,11 @@ fn jpeg_encode_impl<'a>(cx: &mut FunctionContext<'a>) -> JsResult<'a, JsPromise>
let width: u64 = input.get::<JsNumber, _, _>(cx, "width")?.value(cx) as u64; let width: u64 = input.get::<JsNumber, _, _>(cx, "width")?.value(cx) as u64;
let height: u64 = input.get::<JsNumber, _, _>(cx, "height")?.value(cx) as u64; let height: u64 = input.get::<JsNumber, _, _>(cx, "height")?.value(cx) as u64;
let stride: u64 = input.get::<JsNumber, _, _>(cx, "stride")?.value(cx) as u64; let stride: u64 = input.get::<JsNumber, _, _>(cx, "stride")?.value(cx) as u64;
let quality : u64 = if let Ok(val) = input.get::<JsNumber, _, _>(cx, "quality") {
val.value(cx) as u64
} else {
35u64
};
let buffer: Handle<JsBuffer> = input.get(cx, "buffer")?; let buffer: Handle<JsBuffer> = input.get(cx, "buffer")?;
let (deferred, promise) = cx.promise(); let (deferred, promise) = cx.promise();
@ -65,7 +70,7 @@ fn jpeg_encode_impl<'a>(cx: &mut FunctionContext<'a>) -> JsResult<'a, JsPromise>
let vec = COMPRESSOR.with(|lazy| { let vec = COMPRESSOR.with(|lazy| {
let mut b = lazy.borrow_mut(); let mut b = lazy.borrow_mut();
b.set_quality(35); b.set_quality(quality as u32);
b.set_subsamp(turbojpeg_sys::TJSAMP_TJSAMP_420); b.set_subsamp(turbojpeg_sys::TJSAMP_TJSAMP_420);
b.compress_buffer(&image) b.compress_buffer(&image)
}); });