Switch to miniz_oxide 🔼

This commit is contained in:
Laurenz 2020-12-11 12:37:20 +01:00
parent 1cbd5f3051
commit 0cfce1de7e
3 changed files with 29 additions and 35 deletions

View File

@ -13,10 +13,10 @@ cli = ["fs", "anyhow"]
fs = ["fontdock/fs"] fs = ["fontdock/fs"]
[dependencies] [dependencies]
deflate = { version = "0.8.6" }
fontdock = { path = "../fontdock", default-features = false } fontdock = { path = "../fontdock", default-features = false }
image = { version = "0.23", default-features = false, features = ["jpeg", "png"] } image = { version = "0.23", default-features = false, features = ["jpeg", "png"] }
itoa = "0.4" itoa = "0.4"
miniz_oxide = "0.3"
pdf-writer = { path = "../pdf-writer" } pdf-writer = { path = "../pdf-writer" }
ttf-parser = "0.8.2" ttf-parser = "0.8.2"
unicode-xid = "0.2" unicode-xid = "0.2"

View File

@ -3,12 +3,10 @@
use std::cmp::Eq; use std::cmp::Eq;
use std::collections::HashMap; use std::collections::HashMap;
use std::hash::Hash; use std::hash::Hash;
use std::io::Write;
use deflate::write::ZlibEncoder;
use deflate::Compression;
use fontdock::FaceId; use fontdock::FaceId;
use image::{DynamicImage, GenericImageView, ImageFormat, ImageResult, Luma, Rgba}; use image::{DynamicImage, GenericImageView, ImageFormat, ImageResult, Rgba};
use miniz_oxide::deflate;
use pdf_writer::{ use pdf_writer::{
CidFontType, ColorSpace, Content, Filter, FontFlags, Name, PdfWriter, Rect, Ref, Str, CidFontType, ColorSpace, Content, Filter, FontFlags, Name, PdfWriter, Rect, Ref, Str,
SystemInfo, UnicodeCmap, SystemInfo, UnicodeCmap,
@ -309,20 +307,17 @@ impl<'a> PdfExporter<'a> {
// Add a second gray-scale image containing the alpha values if // Add a second gray-scale image containing the alpha values if
// this image has an alpha channel. // this image has an alpha channel.
if img.buf.color().has_alpha() { if img.buf.color().has_alpha() {
if let Ok((alpha_data, alpha_filter)) = encode_alpha(img) { let (alpha_data, alpha_filter) = encode_alpha(img);
let mask_id = self.refs.alpha_mask(masks_seen); let mask_id = self.refs.alpha_mask(masks_seen);
image.s_mask(mask_id); image.s_mask(mask_id);
drop(image); drop(image);
let mut mask = self.writer.image(mask_id, &alpha_data); let mut mask = self.writer.image(mask_id, &alpha_data);
mask.inner().filter(alpha_filter); mask.inner().filter(alpha_filter);
mask.width(width as i32); mask.width(width as i32);
mask.height(height as i32); mask.height(height as i32);
mask.color_space(ColorSpace::DeviceGray); mask.color_space(ColorSpace::DeviceGray);
mask.bits_per_component(8); mask.bits_per_component(8);
} else {
// TODO: Warn that alpha channel could not be encoded.
}
masks_seen += 1; masks_seen += 1;
} }
@ -460,6 +455,9 @@ where
} }
} }
/// The compression level for the deflating.
const DEFLATE_LEVEL: u8 = 6;
/// Encode an image with a suitable filter. /// Encode an image with a suitable filter.
/// ///
/// Skips the alpha channel as that's encoded separately. /// Skips the alpha channel as that's encoded separately.
@ -482,21 +480,21 @@ fn encode_image(img: &ImageResource) -> ImageResult<(Vec<u8>, Filter, ColorSpace
// 8-bit gray PNG. // 8-bit gray PNG.
(ImageFormat::Png, DynamicImage::ImageLuma8(luma)) => { (ImageFormat::Png, DynamicImage::ImageLuma8(luma)) => {
let mut enc = ZlibEncoder::new(&mut data, Compression::default()); data = deflate::compress_to_vec_zlib(&luma.as_raw(), DEFLATE_LEVEL);
for &Luma([value]) in luma.pixels() {
enc.write_all(&[value])?;
}
enc.finish()?;
(Filter::FlateDecode, ColorSpace::DeviceGray) (Filter::FlateDecode, ColorSpace::DeviceGray)
} }
// Anything else (including Rgb(a) PNGs). // Anything else (including Rgb(a) PNGs).
(_, buf) => { (_, buf) => {
let mut enc = ZlibEncoder::new(&mut data, Compression::default()); let (width, height) = buf.dimensions();
let mut pixels = Vec::with_capacity(3 * width as usize * height as usize);
for (_, _, Rgba([r, g, b, _])) in buf.pixels() { for (_, _, Rgba([r, g, b, _])) in buf.pixels() {
enc.write_all(&[r, g, b])?; pixels.push(r);
pixels.push(g);
pixels.push(b);
} }
enc.finish()?;
data = deflate::compress_to_vec_zlib(&pixels, DEFLATE_LEVEL);
(Filter::FlateDecode, ColorSpace::DeviceRgb) (Filter::FlateDecode, ColorSpace::DeviceRgb)
} }
}; };
@ -504,12 +502,8 @@ fn encode_image(img: &ImageResource) -> ImageResult<(Vec<u8>, Filter, ColorSpace
} }
/// Encode an image's alpha channel if present. /// Encode an image's alpha channel if present.
fn encode_alpha(img: &ImageResource) -> ImageResult<(Vec<u8>, Filter)> { fn encode_alpha(img: &ImageResource) -> (Vec<u8>, Filter) {
let mut data = vec![]; let pixels: Vec<_> = img.buf.pixels().map(|(_, _, Rgba([_, _, _, a]))| a).collect();
let mut enc = ZlibEncoder::new(&mut data, Compression::default()); let data = deflate::compress_to_vec_zlib(&pixels, DEFLATE_LEVEL);
for (_, _, Rgba([_, _, _, a])) in img.buf.pixels() { (data, Filter::FlateDecode)
enc.write_all(&[a])?;
}
enc.finish()?;
Ok((data, Filter::FlateDecode))
} }

View File

@ -217,7 +217,7 @@ fn parse_diags(src: &str, map: &LineMap) -> SpanVec<Diag> {
fn print_diag(diag: &Spanned<Diag>, map: &LineMap) { fn print_diag(diag: &Spanned<Diag>, map: &LineMap) {
let start = map.location(diag.span.start).unwrap(); let start = map.location(diag.span.start).unwrap();
let end = map.location(diag.span.end).unwrap(); let end = map.location(diag.span.end).unwrap();
println!("{}: {}-{}: {}", diag.v.level, start, end, diag.v.message,); println!("{}: {}-{}: {}", diag.v.level, start, end, diag.v.message);
} }
fn draw(layouts: &[BoxLayout], env: &Env, pixel_per_pt: f32) -> Canvas { fn draw(layouts: &[BoxLayout], env: &Env, pixel_per_pt: f32) -> Canvas {