use super::{
detect_image_mime_type, memoized, print_alpha_2024 as print_alpha, print_codec, print_tone,
Conversion,
};
use crate::model::image::ImageEffect;
use crate::paint::{ResolvedImageKind, ResolvedImagePayload};
use crate::renderer::image_header::{CANVASKIT_MAX_IMAGE_DIMENSION, CANVASKIT_MAX_IMAGE_PIXELS};
use crate::renderer::render_tree::ImageNode;
const PRINT_RESOURCE_DPI: f64 = 500.0;
fn dimensions(source: (u32, u32), points: (f64, f64)) -> Option<(u32, u32)> {
if ![points.0, points.1]
.into_iter()
.all(|n| n.is_finite() && n > 0.0)
{
return None;
}
let width = (points.0 * PRINT_RESOURCE_DPI / 72.0).round();
let height = (points.1 * PRINT_RESOURCE_DPI / 72.0).round();
if width < 1.0
|| height < 1.0
|| width > f64::from(CANVASKIT_MAX_IMAGE_DIMENSION)
|| height > f64::from(CANVASKIT_MAX_IMAGE_DIMENSION)
{
return None;
}
let size = (width as u32, height as u32);
(u64::from(size.0) * u64::from(size.1) <= CANVASKIT_MAX_IMAGE_PIXELS
&& (size.0 < source.0 || size.1 < source.1))
.then_some(size)
}
fn eligible(image: &ImageNode) -> bool {
image.hancom2024_print_bitmap
&& image.effect == ImageEffect::RealPic
&& image.brightness == 0
&& image.contrast == 0
&& image.opacity == 1.0
&& image.fill_mode.is_none()
&& image.content_inset.is_none()
&& !image.transform.has_transform()
&& image.crop.is_none_or(|(l, t, r, b)| {
super::image_crop_reference(image).is_some_and(|(w, h)| {
l == 0 && t == 0 && i64::from(r) == i64::from(w) && i64::from(b) == i64::from(h)
})
})
&& image
.data
.as_deref()
.is_some_and(|data| detect_image_mime_type(data) == "image/jpeg")
}
pub(super) fn is_device_picture(image: &ImageNode, payload: Option<&ResolvedImagePayload>) -> bool {
eligible(image)
&& payload.is_some_and(|p| {
p.kind == ResolvedImageKind::OutputDevice
&& super::png_sampling::print_separate_alpha(&p.data)
})
}
pub(super) fn payload(
image: &ImageNode,
points: (f64, f64),
width_points: f64,
) -> Option<ResolvedImagePayload> {
if !eligible(image) || !width_points.is_finite() || width_points <= 0.0 {
return None;
}
let source = image.data.as_deref()?;
let header =
super::canvaskit_encoded_image_header(source).filter(|h| h.is_within_decode_limits())?;
let (width, height) = dimensions((header.width, header.height), points)?;
let quality = print_codec::requested_quality(width_points, width);
let data = memoized(
Conversion::Hancom2024PrintResampled {
width,
height,
quality,
},
source,
|| {
let source =
print_tone::decode_print_bitmap(source, image::ImageFormat::Jpeg)?.to_rgba8();
let rgba = super::print_prefilter::resize_opaque(&source, (width, height))?;
print_alpha::encode_planes(rgba, quality, |channel, _alpha| channel)
},
)?;
Some(ResolvedImagePayload {
data,
mime: "image/png",
kind: ResolvedImageKind::OutputDevice,
suppress_effects: false,
})
}