mirror of
https://github.com/storytold/designcraft.git
synced 2026-11-19 08:51:57 +01:00
cutover: use craft_jpeg::decode_reduced (#2991)
This commit is contained in:
1 parent
a73e3def53
commit
a8f6be842e
4 files changed
+59
-91
No files matched your search
Generated
+40
-26
@@ -1120,6 +1120,14 @@ dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-dds"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890#010702ee9b030844c852939095b1da0d0656a890"
|
||||
dependencies = [
|
||||
"rayon",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-designcraft-format"
|
||||
version = "0.1.0"
|
||||
@@ -1308,13 +1316,17 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-image-codecs"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813#a5ac3aa05cf3ec013961a85599ddd9e0b5c03813"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890#010702ee9b030844c852939095b1da0d0656a890"
|
||||
dependencies = [
|
||||
"craft-jpeg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813)",
|
||||
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813)",
|
||||
"craft-dds",
|
||||
"craft-jpeg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890)",
|
||||
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890)",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890)",
|
||||
"crc32fast",
|
||||
"exr",
|
||||
"flate2",
|
||||
"half",
|
||||
"hayro-jpeg2000",
|
||||
"image",
|
||||
"image-webp",
|
||||
"jpeg-encoder",
|
||||
@@ -1331,10 +1343,10 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-image-codecs"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701#bfea7d9800a9f14e496209f9df30e9ef45655701"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813#a5ac3aa05cf3ec013961a85599ddd9e0b5c03813"
|
||||
dependencies = [
|
||||
"craft-jpeg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701)",
|
||||
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701)",
|
||||
"craft-jpeg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813)",
|
||||
"craft-metadata 0.1.0 (git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813)",
|
||||
"exr",
|
||||
"flate2",
|
||||
"half",
|
||||
@@ -1403,6 +1415,14 @@ dependencies = [
|
||||
"indd",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-jpeg"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890#010702ee9b030844c852939095b1da0d0656a890"
|
||||
dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-jpeg"
|
||||
version = "0.1.0"
|
||||
@@ -1414,7 +1434,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-jpeg"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701#bfea7d9800a9f14e496209f9df30e9ef45655701"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=cc23ffd4d26f3074808e2187d332564817624f8f#cc23ffd4d26f3074808e2187d332564817624f8f"
|
||||
dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
@@ -1422,7 +1442,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-jpeg"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=cc23ffd4d26f3074808e2187d332564817624f8f#cc23ffd4d26f3074808e2187d332564817624f8f"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=e7873217287876f76a045550369c90a541693c33#e7873217287876f76a045550369c90a541693c33"
|
||||
dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
@@ -1459,11 +1479,11 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-metadata"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813#a5ac3aa05cf3ec013961a85599ddd9e0b5c03813"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890#010702ee9b030844c852939095b1da0d0656a890"
|
||||
dependencies = [
|
||||
"brotli-decompressor 6.3.1",
|
||||
"chrono",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813)",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890)",
|
||||
"quick-xml 0.41.0",
|
||||
"serde",
|
||||
]
|
||||
@@ -1471,11 +1491,11 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-metadata"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701#bfea7d9800a9f14e496209f9df30e9ef45655701"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813#a5ac3aa05cf3ec013961a85599ddd9e0b5c03813"
|
||||
dependencies = [
|
||||
"brotli-decompressor 6.3.1",
|
||||
"chrono",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701)",
|
||||
"craft-tiff 0.1.0 (git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813)",
|
||||
"quick-xml 0.41.0",
|
||||
"serde",
|
||||
]
|
||||
@@ -1693,7 +1713,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-tiff"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813#a5ac3aa05cf3ec013961a85599ddd9e0b5c03813"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890#010702ee9b030844c852939095b1da0d0656a890"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"thiserror 2.0.21",
|
||||
@@ -1702,7 +1722,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-tiff"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701#bfea7d9800a9f14e496209f9df30e9ef45655701"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=a5ac3aa05cf3ec013961a85599ddd9e0b5c03813#a5ac3aa05cf3ec013961a85599ddd9e0b5c03813"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"thiserror 2.0.21",
|
||||
@@ -2136,7 +2156,7 @@ name = "designcraft-cli"
|
||||
version = "0.21.0"
|
||||
dependencies = [
|
||||
"craft-file-formats",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701)",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890)",
|
||||
"designcraft-color",
|
||||
"designcraft-compose",
|
||||
"designcraft-doc",
|
||||
@@ -2313,7 +2333,8 @@ dependencies = [
|
||||
"craft-cms",
|
||||
"craft-eps 0.1.0 (git+https://github.com/storytold/craft-libs?rev=ba24fe2435adb3c81ad6057faf36365fbba736cf)",
|
||||
"craft-file-formats",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701)",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890)",
|
||||
"craft-jpeg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e7873217287876f76a045550369c90a541693c33)",
|
||||
"craft-ps",
|
||||
"craft-psd",
|
||||
"craft-svg 0.1.0 (git+https://github.com/storytold/craft-libs?rev=cc23ffd4d26f3074808e2187d332564817624f8f)",
|
||||
@@ -2321,7 +2342,6 @@ dependencies = [
|
||||
"designcraft-fonts",
|
||||
"flate2",
|
||||
"image",
|
||||
"jpeg-decoder",
|
||||
"log",
|
||||
"quick-xml 0.41.0",
|
||||
"skrifa 0.47.0",
|
||||
@@ -2348,7 +2368,7 @@ version = "0.21.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"craft-cms",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701)",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890)",
|
||||
"craft-pdf",
|
||||
"craft-prepress",
|
||||
"designcraft-color",
|
||||
@@ -2373,7 +2393,7 @@ name = "designcraft-pptx"
|
||||
version = "0.21.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701)",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890)",
|
||||
"craft-opc 0.1.0 (git+https://github.com/storytold/craft-libs?rev=8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551)",
|
||||
"craft-pptx",
|
||||
"designcraft-color",
|
||||
@@ -2394,7 +2414,7 @@ name = "designcraft-render"
|
||||
version = "0.21.0"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=bfea7d9800a9f14e496209f9df30e9ef45655701)",
|
||||
"craft-image-codecs 0.1.0 (git+https://github.com/storytold/craft-libs?rev=010702ee9b030844c852939095b1da0d0656a890)",
|
||||
"craft-pdf",
|
||||
"designcraft-color",
|
||||
"designcraft-compose",
|
||||
@@ -4207,12 +4227,6 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "jpeg-decoder"
|
||||
version = "0.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "00810f1d8b74be64b13dbf3db89ac67740615d6c891f0e7b6179326533011a07"
|
||||
|
||||
[[package]]
|
||||
name = "jpeg-encoder"
|
||||
version = "0.6.1"
|
||||
|
||||
+2
-1
@@ -29,7 +29,8 @@ craft-xlsx = { git = "https://github.com/storytold/craft-libs", rev = "2b9a325f5
|
||||
craft-docx = { git = "https://github.com/storytold/craft-libs", rev = "7ad4765ee55496d0ccdb7659ad7a6375c7bbb484", version = "=0.1.0" }
|
||||
craft-wordmodel = { git = "https://github.com/storytold/craft-libs", rev = "7ad4765ee55496d0ccdb7659ad7a6375c7bbb484", version = "=0.1.0" }
|
||||
craft-csv = { git = "https://github.com/storytold/craft-libs", rev = "387c64892b30fdad491b263cc3d9eac660d88dcc", version = "=0.1.0" }
|
||||
craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "bfea7d9800a9f14e496209f9df30e9ef45655701", version = "=0.1.0" }
|
||||
craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "010702ee9b030844c852939095b1da0d0656a890", version = "=0.1.0" }
|
||||
craft-jpeg = { git = "https://github.com/storytold/craft-libs", rev = "e7873217287876f76a045550369c90a541693c33", version = "=0.1.0" }
|
||||
craft-psd = { git = "https://github.com/storytold/craft-libs", rev = "d15bf1f35b17b759870f9e5111b9a5d243f64888", version = "=0.1.0" }
|
||||
craft-file-formats = { git = "https://github.com/storytold/craft-libs", rev = "eed3c28cf016723d004d6f2c79c61424c15d4558", version = "=0.1.0" }
|
||||
craft-opc = { git = "https://github.com/storytold/craft-libs", rev = "8abfa9d0f14bafbfc3449fc552ffeafb9ca6a551", version = "=0.1.0" }
|
||||
|
||||
@@ -22,8 +22,7 @@ quick-xml = { workspace = true }
|
||||
craft-image-codecs = { workspace = true }
|
||||
craft-psd = { workspace = true }
|
||||
craft-file-formats = { workspace = true }
|
||||
# Reduced-size JPEG decoding (1/2, 1/4, 1/8 through the DCT) for screen proxies.
|
||||
jpeg-decoder = { version = "0.3", default-features = false }
|
||||
craft-jpeg = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
image = { workspace = true, features = ["png", "jpeg", "webp", "gif", "tiff", "bmp"] }
|
||||
|
||||
+16
-62
@@ -1,7 +1,5 @@
|
||||
//! Reduced-size JPEG decoding (1/2, 1/4, 1/8 through the DCT) for screen proxies.
|
||||
|
||||
use jpeg_decoder::{CodingProcess, PixelFormat};
|
||||
|
||||
/// The largest reduction [`decode_reduced`] reads straight from a JPEG's 8×8 blocks: 1/8.
|
||||
pub(crate) const MAX_SHRINK: u8 = 3;
|
||||
|
||||
@@ -32,39 +30,19 @@ pub(crate) fn progressive_coefficient_bytes(bytes: &[u8]) -> usize {
|
||||
0
|
||||
}
|
||||
|
||||
fn limits() -> craft_jpeg::Limits {
|
||||
let mut limits = craft_jpeg::Limits::default();
|
||||
limits.max_alloc = MAX_BYTES as u64;
|
||||
limits
|
||||
}
|
||||
|
||||
/// Output size of a JPEG reduction without decoding pixels. `None` when the reduced decoder
|
||||
/// cannot handle the JPEG, including its full-size resource limits.
|
||||
pub(crate) fn reduced_estimate(bytes: &[u8], shrink: u8) -> Option<(u32, u32, usize)> {
|
||||
if !bytes.starts_with(&[0xFF, 0xD8]) || shrink == 0 || shrink > MAX_SHRINK {
|
||||
return None;
|
||||
}
|
||||
let mut d = jpeg_decoder::Decoder::new(bytes);
|
||||
d.set_max_decoding_buffer_size(MAX_BYTES);
|
||||
d.read_info().ok()?;
|
||||
let info = d.info()?;
|
||||
let colour = match info.pixel_format {
|
||||
PixelFormat::RGB24 => true,
|
||||
PixelFormat::L8 => false,
|
||||
PixelFormat::L16 | PixelFormat::CMYK32 => return None,
|
||||
};
|
||||
if crate::over_rgba_budget(u32::from(info.width), u32::from(info.height)) {
|
||||
return None;
|
||||
}
|
||||
let progressive_bytes = if info.coding_process == CodingProcess::DctProgressive {
|
||||
let side = |n: u16| u64::from(n).next_multiple_of(32);
|
||||
let components: u64 = if colour { 3 } else { 1 };
|
||||
let bytes = side(info.width) * side(info.height) * components * 2;
|
||||
if bytes > MAX_BYTES as u64 {
|
||||
return None;
|
||||
}
|
||||
usize::try_from(bytes).ok()?
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let reduced = |n: u16| u32::from(n).div_ceil(1 << shrink);
|
||||
let (width, height) = (reduced(info.width), reduced(info.height));
|
||||
let output_bytes = width as usize * height as usize * 8;
|
||||
Some((width, height, progressive_bytes.saturating_add(output_bytes)))
|
||||
craft_jpeg::reduced_estimate(bytes, &limits(), shrink).ok()
|
||||
}
|
||||
|
||||
/// A YCbCr, RGB or grayscale JPEG decoded at 1/2^`shrink` of its size, each side rounded up, for
|
||||
@@ -77,40 +55,16 @@ pub(crate) fn decode_reduced(bytes: &[u8], shrink: u8) -> Option<(image::RgbaIma
|
||||
if !bytes.starts_with(&[0xFF, 0xD8]) || shrink == 0 || shrink > MAX_SHRINK {
|
||||
return None;
|
||||
}
|
||||
let mut d = jpeg_decoder::Decoder::new(bytes);
|
||||
// This limits the output only, which is the reduced size.
|
||||
d.set_max_decoding_buffer_size(MAX_BYTES);
|
||||
d.read_info().ok()?;
|
||||
let info = d.info()?;
|
||||
if crate::over_rgba_budget(u32::from(info.width), u32::from(info.height)) {
|
||||
return None;
|
||||
}
|
||||
let colour = match info.pixel_format {
|
||||
PixelFormat::RGB24 => true,
|
||||
PixelFormat::L8 => false,
|
||||
PixelFormat::L16 | PixelFormat::CMYK32 => return None,
|
||||
let decoded = craft_jpeg::decode_reduced(bytes, &limits(), None, shrink).ok()?;
|
||||
let colour = match decoded.components {
|
||||
3 => true,
|
||||
1 => false,
|
||||
_ => return None,
|
||||
};
|
||||
if info.coding_process == CodingProcess::DctProgressive {
|
||||
// A progressive JPEG holds every coefficient of the full-size image (two bytes each) until
|
||||
// its last scan, whatever the output size. Each component is counted at the full size and
|
||||
// each side is rounded up to the largest MCU (32 pixels): an upper bound.
|
||||
let side = |n: u16| u64::from(n).next_multiple_of(32);
|
||||
let components: u64 = if colour { 3 } else { 1 };
|
||||
if side(info.width) * side(info.height) * components * 2 > MAX_BYTES as u64 {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
let reduced = |n: u16| u16::try_from(u32::from(n).div_ceil(1 << shrink)).ok();
|
||||
let expected = (reduced(info.width)?, reduced(info.height)?);
|
||||
// The decoder takes the smallest scale that meets either requested side, and a short side
|
||||
// rounds up to the same size at several scales, so only the long side is requested.
|
||||
let request = if info.width >= info.height { (expected.0, u16::MAX) } else { (u16::MAX, expected.1) };
|
||||
let (w, h) = d.scale(request.0, request.1).ok()?;
|
||||
if (w, h) != expected {
|
||||
let craft_jpeg::Samples::U8(px) = decoded.samples else {
|
||||
return None;
|
||||
}
|
||||
let px = d.decode().ok()?;
|
||||
let pixels = usize::from(w).checked_mul(usize::from(h))?;
|
||||
};
|
||||
let pixels = usize::try_from(decoded.width).ok()?.checked_mul(usize::try_from(decoded.height).ok()?)?;
|
||||
let rgba: Vec<u8> = if colour {
|
||||
if px.len() != pixels.checked_mul(3)? {
|
||||
return None;
|
||||
@@ -122,5 +76,5 @@ pub(crate) fn decode_reduced(bytes: &[u8], shrink: u8) -> Option<(image::RgbaIma
|
||||
}
|
||||
px.iter().flat_map(|&v| [v, v, v, 255]).collect()
|
||||
};
|
||||
Some((image::RgbaImage::from_raw(u32::from(w), u32::from(h), rgba)?, colour))
|
||||
Some((image::RgbaImage::from_raw(decoded.width, decoded.height, rgba)?, colour))
|
||||
}
|
||||
Reference in new issue
Block a user