Skip to content

Commit

Permalink
avoid copying string
Browse files Browse the repository at this point in the history
  • Loading branch information
Hajime-san committed Sep 29, 2024
1 parent fc4064e commit fb6b87d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 73 deletions.
30 changes: 14 additions & 16 deletions ext/canvas/01_image.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,22 +256,20 @@ function createImageBitmap(
// TODO(Hajime-san): this should be real async
const processedImage = op_create_image_bitmap(
buf,
{
width,
height,
sx,
sy,
sw,
sh,
imageOrientation: options.imageOrientation ?? "from-image",
premultiplyAlpha: options.premultiplyAlpha ?? "default",
colorSpaceConversion: options.colorSpaceConversion ?? "default",
resizeWidth: options.resizeWidth,
resizeHeight: options.resizeHeight,
resizeQuality: options.resizeQuality ?? "low",
imageBitmapSource,
mimeType,
},
width,
height,
sx,
sy,
sw,
sh,
options.imageOrientation ?? "from-image",
options.premultiplyAlpha ?? "default",
options.colorSpaceConversion ?? "default",
options.resizeWidth,
options.resizeHeight,
options.resizeQuality ?? "low",
imageBitmapSource,
mimeType,
);
imageBitmap[_bitmapData] = processedImage.data;
imageBitmap[_width] = processedImage.width;
Expand Down
76 changes: 19 additions & 57 deletions ext/canvas/op_create_image_bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,6 @@ enum ResizeQuality {
High,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct OpCreateImageBitmapArgs {
width: u32,
height: u32,
sx: Option<i32>,
sy: Option<i32>,
sw: Option<i32>,
sh: Option<i32>,
image_orientation: ImageOrientation,
premultiply_alpha: PremultiplyAlpha,
color_space_conversion: ColorSpaceConversion,
resize_width: Option<u32>,
resize_height: Option<u32>,
resize_quality: ResizeQuality,
image_bitmap_source: ImageBitmapSource,
mime_type: String,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct OpCreateImageBitmapReturn {
Expand All @@ -105,7 +86,7 @@ fn decode_bitmap_data(
width: u32,
height: u32,
image_bitmap_source: &ImageBitmapSource,
mime_type: String,
mime_type: &str,
) -> Result<DecodeBitmapDataReturn, AnyError> {
let (image, width, height, icc_profile) = match image_bitmap_source {
ImageBitmapSource::Blob => {
Expand All @@ -116,7 +97,7 @@ fn decode_bitmap_data(
))
.into()
}
let (image, icc_profile) = match &*mime_type {
let (image, icc_profile) = match mime_type {
// Should we support the "image/apng" MIME type here?
"image/png" => {
let mut decoder: PngDecoder<ImageDecoderFromReaderType> =
Expand Down Expand Up @@ -284,46 +265,27 @@ fn apply_premultiply_alpha(

#[op2]
#[serde]
#[allow(clippy::too_many_arguments)]
pub(super) fn op_create_image_bitmap(
#[buffer] zero_copy: JsBuffer,
#[serde] args: OpCreateImageBitmapArgs,
#[buffer] buf: JsBuffer,
width: u32,
height: u32,
sx: Option<i32>,
sy: Option<i32>,
sw: Option<i32>,
sh: Option<i32>,
#[serde] image_orientation: ImageOrientation,
#[serde] premultiply_alpha: PremultiplyAlpha,
#[serde] color_space_conversion: ColorSpaceConversion,
resize_width: Option<u32>,
resize_height: Option<u32>,
#[serde] resize_quality: ResizeQuality,
#[serde] image_bitmap_source: ImageBitmapSource,
#[string] mime_type: &str,
) -> Result<OpCreateImageBitmapReturn, AnyError> {
let buf = &*zero_copy;
let OpCreateImageBitmapArgs {
width,
height,
sh,
sw,
sx,
sy,
image_orientation,
premultiply_alpha,
color_space_conversion,
resize_width,
resize_height,
resize_quality,
image_bitmap_source,
mime_type,
} = OpCreateImageBitmapArgs {
width: args.width,
height: args.height,
sx: args.sx,
sy: args.sy,
sw: args.sw,
sh: args.sh,
image_orientation: args.image_orientation,
premultiply_alpha: args.premultiply_alpha,
color_space_conversion: args.color_space_conversion,
resize_width: args.resize_width,
resize_height: args.resize_height,
resize_quality: args.resize_quality,
image_bitmap_source: args.image_bitmap_source,
mime_type: args.mime_type,
};

// 6. Switch on image:
let (image, width, height, icc_profile) =
decode_bitmap_data(buf, width, height, &image_bitmap_source, mime_type)?;
decode_bitmap_data(&buf, width, height, &image_bitmap_source, mime_type)?;

// crop bitmap data
// 2.
Expand Down

0 comments on commit fb6b87d

Please sign in to comment.