use std::fs::File;
use std::io::BufReader;
use std::path::Path;
use arcsec_core::types::ImageBuffer;
use crate::image_io::{self, ImageFormat, MAX_IMAGE_PIXELS};
pub fn read_raster_image(path: &Path, format: ImageFormat) -> Result<ImageBuffer, String> {
match format {
ImageFormat::Jpeg => read_jpeg(path),
ImageFormat::Png => read_png(path),
ImageFormat::Tiff => read_tiff(path),
other => Err(format!("{} is not a raster format", other.name())),
}
}
pub fn read_raster_dimensions(path: &Path, format: ImageFormat) -> Option<(u32, u32)> {
let (w, h, _) = raster_header(path, format).ok()?;
Some((w, h))
}
pub fn read_raster_channels(path: &Path, format: ImageFormat) -> usize {
raster_header(path, format).map_or(1, |(_, _, c)| c)
}
fn raster_header(path: &Path, format: ImageFormat) -> Result<(u32, u32, usize), String> {
match format {
ImageFormat::Jpeg => {
let bytes = std::fs::read(path).map_err(|e| format!("{}: {e}", path.display()))?;
let mut dec =
zune_jpeg::JpegDecoder::new(zune_jpeg::zune_core::bytestream::ZCursor::new(&bytes));
dec.decode_headers()
.map_err(|e| format!("JPEG read failed: {e:?}"))?;
let info = dec.info().ok_or("JPEG read failed: no header")?;
let colour = dec
.input_colorspace()
.map_or(1, |c| if c.num_components() > 1 { 3 } else { 1 });
Ok((u32::from(info.width), u32::from(info.height), colour))
}
ImageFormat::Png => {
let reader = png_decoder(path)?
.read_info()
.map_err(|e| format!("PNG read failed: {e}"))?;
let info = reader.info();
Ok((info.width, info.height, png_colour(info.color_type).0))
}
ImageFormat::Tiff => {
let mut dec = tiff_decoder(path)?;
let (w, h) = dec.dimensions().map_err(tiff_err)?;
let ct = dec.colortype().map_err(tiff_err)?;
Ok((w, h, tiff_channels(ct).map_or(1, |(_, colour)| colour)))
}
other => Err(format!("{} is not a raster format", other.name())),
}
}
#[derive(Clone, Copy)]
enum Layout {
Chunky { stride: usize },
Planar { plane: usize },
}
impl Layout {
#[inline]
const fn index(self, i: usize, c: usize) -> usize {
match self {
Self::Chunky { stride } => i * stride + c,
Self::Planar { plane } => c * plane + i,
}
}
}
fn to_grey(
width: usize,
height: usize,
colour: usize,
layout: Layout,
sample: impl Fn(usize) -> f32,
) -> Result<ImageBuffer, String> {
let mut data = image_io::try_alloc_pixels(width * height)?;
let inv = 1.0 / colour as f32;
for (out_row, row) in data.chunks_exact_mut(width).enumerate() {
let src_row = height - 1 - out_row;
for (x, v) in row.iter_mut().enumerate() {
let i = src_row * width + x;
*v = if colour == 1 {
sample(layout.index(i, 0))
} else {
(0..colour).map(|c| sample(layout.index(i, c))).sum::<f32>() * inv
};
}
}
Ok(ImageBuffer {
data,
width,
height,
})
}
fn check_dims(width: u32, height: u32, samples: usize) -> Result<(usize, usize), String> {
let (w, h) = (width as usize, height as usize);
if w == 0 || h == 0 {
return Err(format!("invalid image dimensions {w}×{h}"));
}
image_io::checked_pixel_count(w, h, samples)?;
Ok((w, h))
}
const MAX_DECODE_BYTES: usize = MAX_IMAGE_PIXELS.saturating_mul(8);
fn read_jpeg(path: &Path) -> Result<ImageBuffer, String> {
use zune_jpeg::zune_core::bytestream::ZCursor;
use zune_jpeg::zune_core::colorspace::ColorSpace;
use zune_jpeg::zune_core::options::DecoderOptions;
let bytes = std::fs::read(path).map_err(|e| format!("{}: {e}", path.display()))?;
let options = DecoderOptions::default()
.set_max_width(1 << 16)
.set_max_height(1 << 16)
.jpeg_set_out_colorspace(ColorSpace::Luma);
let mut dec = zune_jpeg::JpegDecoder::new_with_options(ZCursor::new(&bytes), options);
let e = |err| format!("JPEG read failed: {err:?}");
dec.decode_headers().map_err(e)?;
let info = dec.info().ok_or("JPEG read failed: no header")?;
let (w, h) = check_dims(u32::from(info.width), u32::from(info.height), 1)?;
let pixels = dec.decode().map_err(e)?;
if pixels.len() < w * h {
return Err(format!(
"JPEG decoded {} samples for a {w}×{h} image",
pixels.len()
));
}
to_grey(w, h, 1, Layout::Chunky { stride: 1 }, |i| {
f32::from(pixels[i])
})
}
fn png_decoder(path: &Path) -> Result<png::Decoder<BufReader<File>>, String> {
let f = File::open(path).map_err(|e| format!("{}: {e}", path.display()))?;
let mut dec = png::Decoder::new_with_limits(
BufReader::new(f),
png::Limits {
bytes: MAX_DECODE_BYTES,
},
);
dec.set_transformations(png::Transformations::EXPAND);
dec.set_ignore_text_chunk(true);
Ok(dec)
}
const fn png_colour(ct: png::ColorType) -> (usize, usize) {
match ct {
png::ColorType::Grayscale => (1, 1),
png::ColorType::GrayscaleAlpha => (1, 2),
png::ColorType::Rgb | png::ColorType::Indexed => (3, 3),
png::ColorType::Rgba => (3, 4),
}
}
fn read_png(path: &Path) -> Result<ImageBuffer, String> {
let e = |err: png::DecodingError| format!("PNG read failed: {err}");
let mut reader = png_decoder(path)?.read_info().map_err(e)?;
let (ct, depth) = reader.output_color_type();
let (colour, stride) = png_colour(ct);
let info = reader.info();
let (w, h) = check_dims(info.width, info.height, stride)?;
let size = reader
.output_buffer_size()
.ok_or("PNG read failed: image too large")?;
let mut buf = Vec::new();
buf.try_reserve_exact(size)
.map_err(|_| format!("not enough memory for a {w}×{h} PNG"))?;
buf.resize(size, 0);
reader.next_frame(&mut buf).map_err(e)?;
let layout = Layout::Chunky { stride };
match depth {
png::BitDepth::Sixteen => to_grey(w, h, colour, layout, |i| {
f32::from(u16::from_be_bytes([buf[2 * i], buf[2 * i + 1]]))
}),
png::BitDepth::Eight => to_grey(w, h, colour, layout, |i| f32::from(buf[i])),
other => Err(format!(
"PNG read failed: {other:?}-bit samples after expansion"
)),
}
}
fn tiff_decoder(path: &Path) -> Result<tiff::decoder::Decoder<BufReader<File>>, String> {
let f = File::open(path).map_err(|e| format!("{}: {e}", path.display()))?;
let mut limits = tiff::decoder::Limits::default();
limits.decoding_buffer_size = MAX_DECODE_BYTES;
Ok(tiff::decoder::Decoder::new(BufReader::new(f))
.map_err(tiff_err)?
.with_limits(limits))
}
fn tiff_err(e: tiff::TiffError) -> String {
format!("TIFF read failed: {e}")
}
const fn tiff_channels(ct: tiff::ColorType) -> Option<(usize, usize)> {
use tiff::ColorType as C;
match ct {
C::Gray(_) => Some((1, 1)),
C::GrayA(_) => Some((2, 1)),
C::RGB(_) => Some((3, 3)),
C::RGBA(_) => Some((4, 3)),
C::Multiband { num_samples, .. } => Some((num_samples as usize, num_samples as usize)),
_ => None,
}
}
fn read_tiff(path: &Path) -> Result<ImageBuffer, String> {
use tiff::decoder::DecodingResult as D;
let mut dec = tiff_decoder(path)?;
let (w, h) = dec.dimensions().map_err(tiff_err)?;
let ct = dec.colortype().map_err(tiff_err)?;
let (stride, colour) = tiff_channels(ct).ok_or_else(|| {
format!("TIFF colour model {ct:?} is not supported for solving (use greyscale or RGB)")
})?;
if stride == 0 {
return Err("TIFF image has no samples per pixel".into());
}
let (w, h) = check_dims(w, h, stride)?;
let mut result = D::U8(Vec::new());
let layout = dec.read_image_to_buffer(&mut result).map_err(tiff_err)?;
let npix = w * h;
let layout = if layout.planes > 1 {
Layout::Planar { plane: npix }
} else {
Layout::Chunky { stride }
};
let need = npix * stride;
macro_rules! grey {
($v:expr, $conv:expr) => {{
let v = $v;
if v.len() < need {
return Err(format!(
"TIFF decoded {} samples for a {w}×{h}×{stride} image (planar \
images larger than the decoder's buffer limit are not supported)",
v.len()
));
}
to_grey(w, h, colour, layout, |i| $conv(v[i]))
}};
}
match &result {
D::U8(v) => grey!(v, f32::from),
D::U16(v) => grey!(v, f32::from),
D::U32(v) => grey!(v, |x: u32| x as f32),
D::U64(v) => grey!(v, |x: u64| x as f32),
D::I8(v) => grey!(v, f32::from),
D::I16(v) => grey!(v, f32::from),
D::I32(v) => grey!(v, |x: i32| x as f32),
D::I64(v) => grey!(v, |x: i64| x as f32),
D::F16(v) => grey!(v, f32::from),
D::F32(v) => grey!(v, |x: f32| x),
D::F64(v) => grey!(v, |x: f64| x as f32),
}
}
#[cfg(test)]
mod tests {
use super::*;
fn tmp(name: &str) -> std::path::PathBuf {
std::env::temp_dir().join(format!("arcsec_raster_{}_{name}", std::process::id()))
}
fn pattern(w: usize, h: usize) -> Vec<u16> {
(0..h)
.flat_map(|y| (0..w).map(move |x| (10 * y + x) as u16))
.collect()
}
fn assert_flipped(img: &ImageBuffer, w: usize, h: usize, scale: f32) {
assert_eq!((img.width, img.height), (w, h));
for y in 0..h {
for x in 0..w {
let want = (10 * (h - 1 - y) + x) as f32 * scale;
assert_eq!(img.data[y * w + x], want, "pixel {x},{y}");
}
}
}
fn write_png(
path: &Path,
w: u32,
h: u32,
ct: png::ColorType,
depth: png::BitDepth,
bytes: &[u8],
) {
let f = File::create(path).unwrap();
let mut enc = png::Encoder::new(std::io::BufWriter::new(f), w, h);
enc.set_color(ct);
enc.set_depth(depth);
enc.write_header().unwrap().write_image_data(bytes).unwrap();
}
#[test]
fn png_grey16_is_read_bottom_row_first() {
let p = tmp("g16.png");
let bytes: Vec<u8> = pattern(3, 2).iter().flat_map(|v| v.to_be_bytes()).collect();
write_png(
&p,
3,
2,
png::ColorType::Grayscale,
png::BitDepth::Sixteen,
&bytes,
);
assert_eq!(image_io::detect_format(&p), Ok(ImageFormat::Png));
let img = image_io::read_image(&p).unwrap();
assert_flipped(&img, 3, 2, 1.0);
assert_eq!(image_io::read_dimensions(&p), Some((3, 2)));
assert_eq!(image_io::read_channels(&p), 1);
assert_eq!(image_io::read_ra_dec(&p), None);
assert_eq!(image_io::read_pixel_scale(&p), None);
std::fs::remove_file(p).ok();
}
#[test]
fn png_rgba_averages_colour_and_drops_alpha() {
let p = tmp("rgba.png");
let bytes: Vec<u8> = pattern(3, 2)
.iter()
.flat_map(|&v| [v as u8, 2 * v as u8, 3 * v as u8, 255])
.collect();
write_png(&p, 3, 2, png::ColorType::Rgba, png::BitDepth::Eight, &bytes);
let img = image_io::read_image(&p).unwrap();
assert_flipped(&img, 3, 2, 2.0);
assert_eq!(image_io::read_channels(&p), 3);
std::fs::remove_file(p).ok();
}
#[test]
fn tiff_grey16_and_rgb_float() {
use tiff::encoder::{TiffEncoder, colortype};
let p = tmp("g16.tif");
let mut enc = TiffEncoder::new(File::create(&p).unwrap()).unwrap();
enc.write_image::<colortype::Gray16>(3, 2, &pattern(3, 2))
.unwrap();
drop(enc);
assert_eq!(image_io::detect_format(&p), Ok(ImageFormat::Tiff));
assert_flipped(&image_io::read_image(&p).unwrap(), 3, 2, 1.0);
assert_eq!(image_io::read_dimensions(&p), Some((3, 2)));
std::fs::remove_file(&p).ok();
let p = tmp("rgbf.tif");
let rgb: Vec<f32> = pattern(3, 2)
.iter()
.flat_map(|&v| {
let v = f32::from(v);
[v, 2.0 * v, 3.0 * v]
})
.collect();
let mut enc = TiffEncoder::new(File::create(&p).unwrap()).unwrap();
enc.write_image::<colortype::RGB32Float>(3, 2, &rgb)
.unwrap();
drop(enc);
assert_flipped(&image_io::read_image(&p).unwrap(), 3, 2, 2.0);
assert_eq!(image_io::read_channels(&p), 3);
std::fs::remove_file(&p).ok();
}
#[test]
fn jpeg_is_read_as_luma_bottom_row_first() {
let (w, h) = (16u16, 16u16);
let grey: Vec<u8> = (0..h)
.flat_map(|y| (0..w).map(move |_| if y < 8 { 20 } else { 230 }))
.collect();
let p = tmp("grey.jpg");
jpeg_encoder::Encoder::new_file(&p, 95)
.unwrap()
.encode(&grey, w, h, jpeg_encoder::ColorType::Luma)
.unwrap();
assert_eq!(image_io::detect_format(&p), Ok(ImageFormat::Jpeg));
let img = image_io::read_image(&p).unwrap();
assert_eq!((img.width, img.height), (16, 16));
assert_eq!(image_io::read_dimensions(&p), Some((16, 16)));
assert!(
(img.data[0] - 230.0).abs() < 4.0,
"first row: {}",
img.data[0]
);
assert!(
(img.data[15 * 16] - 20.0).abs() < 4.0,
"last row: {}",
img.data[15 * 16]
);
std::fs::remove_file(&p).ok();
let rgb: Vec<u8> = grey.iter().flat_map(|&v| [v, v, v]).collect();
jpeg_encoder::Encoder::new_file(&p, 95)
.unwrap()
.encode(&rgb, w, h, jpeg_encoder::ColorType::Rgb)
.unwrap();
let img = image_io::read_image(&p).unwrap();
assert!(
(img.data[0] - 230.0).abs() < 4.0,
"first row: {}",
img.data[0]
);
assert_eq!(image_io::read_channels(&p), 3);
std::fs::remove_file(p).ok();
}
#[test]
fn corrupt_rasters_are_errors() {
for (name, magic) in [
("bad.png", &b"\x89PNG\r\n\x1a\n\0\0\0\rIHDR"[..]),
("bad.tif", &b"II*\0\x08\0\0\0\xff\xff"[..]),
("bad.jpg", &b"\xff\xd8\xff\xe0\0\x10JFIF"[..]),
] {
let p = tmp(name);
std::fs::write(&p, magic).unwrap();
assert!(image_io::read_image(&p).is_err(), "{name}");
assert_eq!(image_io::read_dimensions(&p), None, "{name}");
std::fs::remove_file(p).ok();
}
}
}