use std::collections::HashSet;
use jpeg_encoder::{ChromaSubsamplingMethod, ColorType, Encoder, SamplingFactor};
use super::{detect_image_mime_type, memoized, Conversion};
use crate::model::image::ImageEffect;
use crate::paint::{ResolvedImageKind, ResolvedImagePayload};
use crate::renderer::render_tree::ImageNode;
pub(super) fn quality_for_palette(quality: u8, source_colors: usize) -> u8 {
if source_colors > 1024 && quality > 70 {
(f64::from(quality) * 0.7) as u8
} else {
quality
}
}
pub(super) fn requested_quality(width_points: f64, pixel_width: u32) -> u8 {
((width_points as f32 / pixel_width as f32 * 100.0) as i32).clamp(35, 75) as u8
}
pub(crate) fn print_area_sampling(
image: &ImageNode,
resolved: Option<&ResolvedImagePayload>,
) -> bool {
if (resolved.is_some_and(|payload| {
payload.kind == ResolvedImageKind::OutputDeviceWindow
&& super::png_sampling::print_separate_alpha(&payload.data)
})) || (super::print_alpha_2024::eligible(image)
&& resolved.is_some_and(|payload| super::png_sampling::print_separate_alpha(&payload.data)))
|| crate::paint::metafile_picture::is_device_picture(image, resolved)
|| super::print_picture_resample::is_device_picture(image, resolved)
{
return true;
}
let bitmap_fill = crate::paint::device_image::eligible(image);
let accepts_mime = |mime| mime == "image/jpeg" || (bitmap_fill && mime == "image/png");
image.hancom2024_print_bitmap
&& image.effect == ImageEffect::RealPic
&& image.brightness == 0
&& image.contrast == 0
&& image.opacity == 1.0
&& (image.fill_mode.is_none() || bitmap_fill)
&& !image.transform.has_transform()
&& !image
.data
.as_deref()
.is_some_and(|data| super::png_sampling::pdf_source(data))
&& image.crop.is_none_or(|(left, top, right, bottom)| {
super::image_crop_reference(image).is_some_and(|(width, height)| {
left == 0
&& top == 0
&& i64::from(right) == i64::from(width)
&& i64::from(bottom) == i64::from(height)
})
})
&& resolved.map_or_else(
|| {
image
.data
.as_deref()
.is_some_and(|data| accepts_mime(detect_image_mime_type(data)))
},
|payload| accepts_mime(payload.mime),
)
}
pub(crate) fn print_device_image_payload(
image: &ImageNode,
width_points: f64,
logical_size_points: (f64, f64),
in_shape_group: bool,
) -> Option<ResolvedImagePayload> {
if !in_shape_group {
if let Some(payload) = super::print_bitmap_window::payload(image, width_points) {
return Some(payload);
}
}
if let Some(payload) =
super::print_picture_resample::payload(image, logical_size_points, width_points)
{
return Some(payload);
}
if let Some(payload) = super::print_alpha_2024::payload(image, width_points) {
return Some(payload);
}
if crate::paint::device_image::eligible(image)
&& image
.data
.as_deref()
.is_some_and(|data| detect_image_mime_type(data) == "image/jpeg")
{
return super::print_fill_jpeg::payload(image, width_points);
}
if !image.hancom2024_print_bitmap
|| image.effect != ImageEffect::RealPic
|| image.brightness != 0
|| image.contrast != 0
|| image.opacity != 1.0
|| image.fill_mode.is_some()
|| !width_points.is_finite()
|| width_points <= 0.0
{
return None;
}
let source = image.data.as_deref()?;
if !matches!(
detect_image_mime_type(source),
"image/bmp" | "image/png" | "image/jpeg"
) || super::png_sampling::pdf_source(source)
{
return None;
}
let header = super::canvaskit_encoded_image_header(source)?;
if !header.is_within_decode_limits() {
return None;
}
let base = super::resolve_image_payload_for_print(image, in_shape_group).unwrap_or_else(|| {
ResolvedImagePayload {
data: source.to_vec(),
mime: detect_image_mime_type(source),
kind: ResolvedImageKind::FormatConverted,
suppress_effects: false,
}
});
if image.crop.is_some() && base.kind != ResolvedImageKind::OutputDeviceWindow {
let (left, top, right, bottom) = image.crop?;
let (width, height) = super::image_crop_reference(image)?;
if left != 0
|| top != 0
|| i64::from(right) != i64::from(width)
|| i64::from(bottom) != i64::from(height)
{
return None;
}
}
let output_header = super::canvaskit_encoded_image_header(&base.data)?;
if !output_header.is_within_decode_limits() {
return None;
}
let quality = requested_quality(width_points, output_header.width);
let quality = if quality > 70 {
let format = match detect_image_mime_type(source) {
"image/bmp" => image::ImageFormat::Bmp,
"image/png" => image::ImageFormat::Png,
"image/jpeg" => image::ImageFormat::Jpeg,
_ => return None,
};
let window =
super::print_window::SourceWindow::for_image(image, header.width, header.height)?;
let (visible, _) =
super::print_tone::decode_transfer_pixels(source, format, Some(window.bounds))?;
let mut colors = HashSet::with_capacity(1025);
for pixel in visible.pixels() {
colors.insert([pixel[0], pixel[1], pixel[2]]);
if colors.len() > 1024 {
break;
}
}
quality_for_palette(quality, colors.len())
} else {
quality
};
let data = memoized(
Conversion::Hancom2024PrintJpeg { quality },
&base.data,
|| encode(&base, quality),
)?;
Some(ResolvedImagePayload {
data,
mime: "image/jpeg",
kind: if base.kind == ResolvedImageKind::OutputDeviceWindow {
ResolvedImageKind::OutputDeviceWindow
} else {
ResolvedImageKind::OutputDevice
},
suppress_effects: false,
})
}
fn encode(payload: &ResolvedImagePayload, quality: u8) -> Option<Vec<u8>> {
let format = match payload.mime {
"image/png" => image::ImageFormat::Png,
"image/bmp" => image::ImageFormat::Bmp,
"image/jpeg" => image::ImageFormat::Jpeg,
_ => return None,
};
let (rgba, _) = super::print_tone::decode_transfer_pixels(&payload.data, format, None)?;
if rgba.as_raw().len() / 4 * 3 < 4000 || rgba.pixels().any(|pixel| pixel[3] != 255) {
return None;
}
encode_rgb(image::DynamicImage::ImageRgba8(rgba).to_rgb8(), quality)
}
pub(super) fn palette_len(rgb: &image::RgbImage) -> usize {
let mut colors = HashSet::with_capacity(1025);
for pixel in rgb.pixels() {
colors.insert([pixel[0], pixel[1], pixel[2]]);
if colors.len() > 1024 {
break;
}
}
colors.len()
}
pub(super) fn encode_rgb(rgb: image::RgbImage, quality: u8) -> Option<Vec<u8>> {
let (width, height) = (
u16::try_from(rgb.width()).ok()?,
u16::try_from(rgb.height()).ok()?,
);
if rgb.as_raw().len() < 4000 || palette_len(&rgb) < 32 {
return None;
}
let mut data = Vec::new();
let mut encoder = Encoder::new(&mut data, quality);
encoder.set_sampling_factor(SamplingFactor::F_2_2);
encoder.set_chroma_subsampling_method(ChromaSubsamplingMethod::Average);
encoder.set_optimized_huffman_tables(false);
encoder
.encode(rgb.as_raw(), width, height, ColorType::Rgb)
.ok()?;
Some(data)
}