1#![allow(
7 clippy::too_many_arguments,
8 clippy::type_complexity,
9 clippy::needless_range_loop
10)]
11
12mod hcompress;
13mod plio;
14mod quantize;
15mod rice;
16
17#[cfg(feature = "gzip")]
18mod gzip_tile;
19
20use crate::error::{FitsError, Result};
21use crate::file::FitsFile;
22use crate::header::Header;
23use crate::status::{BAD_DATATYPE, BAD_DIMEN, DATA_COMPRESSION_ERR, DATA_DECOMPRESSION_ERR};
24use crate::types::{
25 GZIP_1, GZIP_2, HCOMPRESS_1, ImageType, MAX_COMPRESS_DIM, NO_DITHER, NO_QUANTIZE, NOCOMPRESS,
26 PLIO_1, RICE_1, SUBTRACTIVE_DITHER_1, SUBTRACTIVE_DITHER_2,
27};
28
29pub use quantize::{N_RANDOM, dither_table};
30
31#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
33pub enum CompressionType {
34 None,
36 Rice1,
38 Gzip1,
40 Gzip2,
42 Plio1,
44 Hcompress1,
46}
47
48impl CompressionType {
49 #[must_use]
51 pub const fn code(self) -> i32 {
52 match self {
53 Self::None => NOCOMPRESS,
54 Self::Rice1 => RICE_1,
55 Self::Gzip1 => GZIP_1,
56 Self::Gzip2 => GZIP_2,
57 Self::Plio1 => PLIO_1,
58 Self::Hcompress1 => HCOMPRESS_1,
59 }
60 }
61
62 #[must_use]
64 pub const fn from_code(code: i32) -> Option<Self> {
65 match code {
66 0 | NOCOMPRESS => Some(Self::None),
67 RICE_1 => Some(Self::Rice1),
68 GZIP_1 => Some(Self::Gzip1),
69 GZIP_2 => Some(Self::Gzip2),
70 PLIO_1 => Some(Self::Plio1),
71 HCOMPRESS_1 => Some(Self::Hcompress1),
72 _ => None,
73 }
74 }
75
76 #[must_use]
78 pub const fn zcmptype(self) -> &'static str {
79 match self {
80 Self::None => "NOCOMPRESS",
81 Self::Rice1 => "RICE_1",
82 Self::Gzip1 => "GZIP_1",
83 Self::Gzip2 => "GZIP_2",
84 Self::Plio1 => "PLIO_1",
85 Self::Hcompress1 => "HCOMPRESS_1",
86 }
87 }
88
89 fn from_zcmptype(s: &str) -> Result<Self> {
90 match s.trim() {
91 "RICE_1" | "RICE_ONE" => Ok(Self::Rice1),
92 "GZIP_1" => Ok(Self::Gzip1),
93 "GZIP_2" => Ok(Self::Gzip2),
94 "PLIO_1" => Ok(Self::Plio1),
95 "HCOMPRESS_1" => Ok(Self::Hcompress1),
96 "NOCOMPRESS" => Ok(Self::None),
97 other => Err(FitsError::with_message(
98 DATA_DECOMPRESSION_ERR,
99 format!("Unknown image compression type: {other}"),
100 )),
101 }
102 }
103}
104
105#[derive(Debug, Clone)]
107pub struct CompressionRequest {
108 pub kind: Option<CompressionType>,
110 pub tilesize: [i64; MAX_COMPRESS_DIM],
112 pub quantize_level: f32,
114 pub quantize_method: i32,
116 pub dither_seed: i32,
118 pub hcomp_scale: f32,
120 pub hcomp_smooth: i32,
122}
123
124impl Default for CompressionRequest {
125 fn default() -> Self {
126 Self {
127 kind: None,
128 tilesize: [0; MAX_COMPRESS_DIM],
129 quantize_level: 0.0,
130 quantize_method: 0,
131 dither_seed: 1,
132 hcomp_scale: 0.0,
133 hcomp_smooth: 0,
134 }
135 }
136}
137
138#[derive(Debug, Clone)]
140pub struct ZImageInfo {
141 pub kind: CompressionType,
143 pub zbitpix: i32,
145 pub znaxis: Vec<i64>,
147 pub tilesize: Vec<i64>,
149 pub rice_blocksize: i32,
151 pub rice_bytepix: i32,
153 pub hcomp_scale: f32,
155 pub hcomp_smooth: i32,
157 pub quantize_method: i32,
159 pub no_quantize: bool,
161 pub dither_seed: i32,
163 pub bscale: f64,
165 pub bzero: f64,
167}
168
169impl ZImageInfo {
170 #[must_use]
172 pub fn npix(&self) -> i64 {
173 self.znaxis
174 .iter()
175 .copied()
176 .filter(|&n| n > 0)
177 .product::<i64>()
178 .max(0)
179 }
180
181 pub fn image_type(&self) -> Result<ImageType> {
183 let z = self.bzero;
184 Ok(match self.zbitpix {
185 8 if (z + 128.0).abs() < 0.5 => ImageType::I8,
186 8 => ImageType::U8,
187 16 if (z - 32768.0).abs() < 0.5 => ImageType::U16,
188 16 => ImageType::I16,
189 32 if (z - 2_147_483_648.0).abs() < 0.5 => ImageType::U32,
190 32 => ImageType::I32,
191 64 if z > 1e18 => ImageType::U64,
192 64 => ImageType::I64,
193 -32 => ImageType::F32,
194 -64 => ImageType::F64,
195 _ => return Err(FitsError::new(crate::status::BAD_BITPIX)),
196 })
197 }
198
199 #[must_use]
201 pub fn bytes_per_pixel(&self) -> usize {
202 match self.zbitpix.unsigned_abs() {
203 8 => 1,
204 16 => 2,
205 32 => 4,
206 64 => 8,
207 _ => 1,
208 }
209 }
210}
211
212impl Header {
213 #[must_use]
215 pub fn is_compressed_image(&self) -> bool {
216 self.get_logical("ZIMAGE").map(|(v, _)| v).unwrap_or(false)
217 }
218
219 pub fn zimage_info(&self, nrows: i64) -> Result<ZImageInfo> {
221 let zcmp = self.get_string("ZCMPTYPE").map(|(v, _)| v).map_err(|_| {
222 FitsError::with_message(
223 DATA_DECOMPRESSION_ERR,
224 "required ZCMPTYPE compression keyword not found",
225 )
226 })?;
227 let kind = CompressionType::from_zcmptype(&zcmp)?;
228 let zbitpix = self.get_i64("ZBITPIX").map_err(|_| {
229 FitsError::with_message(
230 DATA_DECOMPRESSION_ERR,
231 "required ZBITPIX compression keyword not found",
232 )
233 })? as i32;
234 let zndim = self.get_i64("ZNAXIS").map_err(|_| {
235 FitsError::with_message(
236 DATA_DECOMPRESSION_ERR,
237 "required ZNAXIS compression keyword not found",
238 )
239 })?;
240 if zndim < 1 || zndim as usize > MAX_COMPRESS_DIM {
241 return Err(FitsError::new(crate::status::BAD_NAXIS));
242 }
243 let mut znaxis = Vec::with_capacity(zndim as usize);
244 let mut tilesize = Vec::with_capacity(zndim as usize);
245 let mut expect_nrows = 1i64;
246 for i in 1..=zndim {
247 let n = self.get_i64(&format!("ZNAXIS{i}")).map_err(|_| {
248 FitsError::with_message(
249 DATA_DECOMPRESSION_ERR,
250 "required ZNAXISn compression keyword not found",
251 )
252 })?;
253 znaxis.push(n);
254 let default = if i == 1 { n } else { 1 };
255 let t = self.get_i64(&format!("ZTILE{i}")).unwrap_or(default);
256 if t == 0 {
257 return Err(FitsError::with_message(
258 DATA_DECOMPRESSION_ERR,
259 "invalid ZTILE value = 0 in compressed image",
260 ));
261 }
262 tilesize.push(t);
263 expect_nrows *= (n - 1) / t + 1;
264 }
265 if expect_nrows != nrows {
266 return Err(FitsError::with_message(
267 DATA_DECOMPRESSION_ERR,
268 "number of table rows != the number of tiles in compressed image",
269 ));
270 }
271 let has_zscale = self.card_by_name("ZSCALE").is_some()
272 || (1..=self.get_i64("TFIELDS").unwrap_or(0)).any(|c| {
273 self.get_string(&format!("TTYPE{c}"))
274 .map(|(v, _)| v.eq_ignore_ascii_case("ZSCALE"))
275 .unwrap_or(false)
276 });
277 let zquant = self.get_string("ZQUANTIZ").ok().map(|(v, _)| v);
278 let (no_quantize, quantize_method) = if zbitpix < 0 && !has_zscale {
279 (true, NO_DITHER)
280 } else {
281 match zquant.as_deref() {
282 Some(s) if s.trim() == "NONE" => (true, NO_DITHER),
283 Some(s) if s.trim() == "SUBTRACTIVE_DITHER_1" => (false, SUBTRACTIVE_DITHER_1),
284 Some(s) if s.trim() == "SUBTRACTIVE_DITHER_2" => (false, SUBTRACTIVE_DITHER_2),
285 Some(s) if s.trim() == "NO_DITHER" => (false, NO_DITHER),
286 Some(_) => {
287 return Err(FitsError::with_message(
288 DATA_DECOMPRESSION_ERR,
289 "Unknown quantization type",
290 ));
291 }
292 None => (false, NO_DITHER),
293 }
294 };
295 let mut rice_blocksize = 32;
296 let mut rice_bytepix = 4;
297 let mut hcomp_scale = 0.0f32;
298 let mut hcomp_smooth = 0i32;
299 if kind == CompressionType::Rice1 {
300 rice_blocksize = self.get_i64("ZVAL1").unwrap_or(32) as i32;
301 rice_bytepix = self.get_i64("ZVAL2").unwrap_or(4) as i32;
302 if rice_blocksize < 16 && rice_bytepix > 8 {
303 std::mem::swap(&mut rice_blocksize, &mut rice_bytepix);
304 }
305 if rice_blocksize == 0 {
306 return Err(FitsError::new(DATA_DECOMPRESSION_ERR));
307 }
308 } else if kind == CompressionType::Hcompress1 {
309 hcomp_scale = self.get_f64("ZVAL1").unwrap_or(0.0) as f32;
310 hcomp_smooth = self.get_i64("ZVAL2").unwrap_or(0) as i32;
311 }
312 Ok(ZImageInfo {
313 kind,
314 zbitpix,
315 znaxis,
316 tilesize,
317 rice_blocksize,
318 rice_bytepix,
319 hcomp_scale,
320 hcomp_smooth,
321 quantize_method,
322 no_quantize,
323 dither_seed: self.get_i64("ZDITHER0").unwrap_or(1) as i32,
324 bscale: self.get_f64("BSCALE").unwrap_or(1.0),
325 bzero: self.get_f64("BZERO").unwrap_or(0.0),
326 })
327 }
328}
329
330impl FitsFile {
331 pub fn set_compression_type(&mut self, ctype: i32) -> Result<()> {
333 let kind = CompressionType::from_code(ctype).ok_or_else(|| {
334 FitsError::with_message(
335 DATA_COMPRESSION_ERR,
336 "unknown compression algorithm (fits_set_compression_type)",
337 )
338 })?;
339 let inner = self.inner_mut()?;
340 inner.compression.kind = if ctype == 0 || ctype == NOCOMPRESS {
341 None
342 } else {
343 Some(kind)
344 };
345 Ok(())
346 }
347
348 pub fn get_compression_type(&self) -> Result<i32> {
350 Ok(self
351 .inner()?
352 .compression
353 .kind
354 .map_or(0, CompressionType::code))
355 }
356
357 pub fn set_tile_dim(&mut self, dims: &[i64]) -> Result<()> {
359 if dims.len() > MAX_COMPRESS_DIM {
360 return Err(FitsError::new(BAD_DIMEN));
361 }
362 let inner = self.inner_mut()?;
363 for (i, &d) in dims.iter().enumerate() {
364 inner.compression.tilesize[i] = d;
365 }
366 Ok(())
367 }
368
369 pub fn get_tile_dim(&self, ndim: usize) -> Result<Vec<i64>> {
371 let c = &self.inner()?.compression;
372 Ok(c.tilesize[..ndim.min(MAX_COMPRESS_DIM)].to_vec())
373 }
374
375 pub fn set_quantize_level(&mut self, qlevel: f32) -> Result<()> {
377 self.inner_mut()?.compression.quantize_level =
378 if qlevel == 0.0 { NO_QUANTIZE } else { qlevel };
379 Ok(())
380 }
381
382 pub fn get_quantize_level(&self) -> Result<f32> {
384 let q = self.inner()?.compression.quantize_level;
385 Ok(if q == NO_QUANTIZE { 0.0 } else { q })
386 }
387
388 pub fn set_quantize_method(&mut self, method: i32) -> Result<()> {
390 self.inner_mut()?.compression.quantize_method = method;
391 Ok(())
392 }
393
394 pub fn set_hcomp_scale(&mut self, scale: f32) -> Result<()> {
396 self.inner_mut()?.compression.hcomp_scale = scale;
397 Ok(())
398 }
399
400 pub fn get_hcomp_scale(&self) -> Result<f32> {
402 Ok(self.inner()?.compression.hcomp_scale)
403 }
404
405 pub fn set_hcomp_smooth(&mut self, smooth: i32) -> Result<()> {
407 self.inner_mut()?.compression.hcomp_smooth = smooth;
408 Ok(())
409 }
410
411 pub fn is_compressed_image(&self) -> Result<bool> {
413 Ok(self.inner()?.hdus[self.inner()?.current]
414 .header
415 .is_compressed_image())
416 }
417
418 pub(crate) fn compression_requested(&self) -> bool {
419 self.inner()
420 .ok()
421 .and_then(|i| i.compression.kind)
422 .is_some_and(|k| k != CompressionType::None)
423 }
424
425 pub(crate) fn create_compressed_image(&mut self, ty: ImageType, naxes: &[i64]) -> Result<()> {
427 if matches!(ty, ImageType::I64 | ImageType::U64) {
428 return Err(FitsError::new(BAD_DATATYPE));
429 }
430 let req = self.inner()?.compression.clone();
431 let kind = req.kind.unwrap_or(CompressionType::Rice1);
432 if ty.bitpix() < 0
433 && req.quantize_level == NO_QUANTIZE
434 && !matches!(kind, CompressionType::Gzip1 | CompressionType::Gzip2)
435 {
436 return Err(FitsError::with_message(
437 DATA_COMPRESSION_ERR,
438 "Lossless compression of floating point images must use GZIP (imcomp_init_table)",
439 ));
440 }
441 let tiles = actual_tilesize(kind, naxes, &req.tilesize)?;
442 let nrows = tile_count(naxes, &tiles);
443 let bitpix = ty.bitpix();
444 let quantize = bitpix < 0 && req.quantize_level != NO_QUANTIZE;
445 let tform0 = if kind == CompressionType::Plio1 {
446 "1PI"
447 } else {
448 "1PB"
449 };
450 let (ttype, tform): (Vec<&str>, Vec<&str>) = if quantize {
451 (
452 vec!["COMPRESSED_DATA", "ZSCALE", "ZZERO"],
453 vec![tform0, "1D", "1D"],
454 )
455 } else {
456 (vec!["COMPRESSED_DATA"], vec![tform0])
457 };
458 self.create_binary_table(nrows, &ttype, &tform, &[], None)?;
459 {
460 let h = self.header_mut()?;
461 h.write_logical("ZIMAGE", true, Some("extension contains compressed image"))?;
462 h.write_long(
463 "ZBITPIX",
464 i64::from(bitpix),
465 Some("data type of original image"),
466 )?;
467 h.write_long(
468 "ZNAXIS",
469 naxes.len() as i64,
470 Some("dimension of original image"),
471 )?;
472 for (i, &n) in naxes.iter().enumerate() {
473 h.write_long(
474 &format!("ZNAXIS{}", i + 1),
475 n,
476 Some("length of original image axis"),
477 )?;
478 }
479 for (i, &t) in tiles.iter().enumerate() {
480 h.write_long(
481 &format!("ZTILE{}", i + 1),
482 t,
483 Some("size of tiles to be compressed"),
484 )?;
485 }
486 if bitpix < 0 {
487 if req.quantize_level == NO_QUANTIZE {
488 h.write_string(
489 "ZQUANTIZ",
490 "NONE",
491 Some("Lossless compression without quantization"),
492 )?;
493 } else {
494 let method = if req.quantize_method == 0 {
495 SUBTRACTIVE_DITHER_1
496 } else {
497 req.quantize_method
498 };
499 let method =
500 if kind == CompressionType::Hcompress1 && method == SUBTRACTIVE_DITHER_2 {
501 SUBTRACTIVE_DITHER_1
502 } else {
503 method
504 };
505 let name = match method {
506 SUBTRACTIVE_DITHER_2 => "SUBTRACTIVE_DITHER_2",
507 NO_DITHER => "NO_DITHER",
508 _ => "SUBTRACTIVE_DITHER_1",
509 };
510 h.write_string("ZQUANTIZ", name, Some("Pixel Quantization Algorithm"))?;
511 if method != NO_DITHER {
512 h.write_long(
513 "ZDITHER0",
514 i64::from(req.dither_seed.max(1)),
515 Some("dithering offset when quantizing floats"),
516 )?;
517 }
518 }
519 }
520 h.write_string("ZCMPTYPE", kind.zcmptype(), Some("compression algorithm"))?;
521 if kind == CompressionType::Rice1 {
522 h.write_string("ZNAME1", "BLOCKSIZE", Some("compression block size"))?;
523 h.write_long("ZVAL1", 32, Some("pixels per block"))?;
524 h.write_string("ZNAME2", "BYTEPIX", Some("bytes per pixel (1, 2, 4, or 8)"))?;
525 let bp = match bitpix {
526 8 => 1,
527 16 => 2,
528 _ => 4,
529 };
530 h.write_long("ZVAL2", bp, Some("bytes per pixel (1, 2, 4, or 8)"))?;
531 } else if kind == CompressionType::Hcompress1 {
532 h.write_string("ZNAME1", "SCALE", Some("HCOMPRESS scale factor"))?;
533 h.write_float_exp(
534 "ZVAL1",
535 f64::from(req.hcomp_scale),
536 7,
537 Some("HCOMPRESS scale factor"),
538 )?;
539 h.write_string("ZNAME2", "SMOOTH", Some("HCOMPRESS smooth option"))?;
540 h.write_long(
541 "ZVAL2",
542 i64::from(req.hcomp_smooth),
543 Some("HCOMPRESS smooth option"),
544 )?;
545 }
546 match ty {
547 ImageType::U16 => {
548 h.write_fixed_double(
549 "BZERO",
550 32768.0,
551 0,
552 Some("offset data range to that of unsigned short"),
553 )?;
554 h.write_fixed_double("BSCALE", 1.0, 0, Some("default scaling factor"))?;
555 }
556 ImageType::I8 => {
557 h.write_fixed_double(
558 "BZERO",
559 -128.0,
560 0,
561 Some("offset data range to that of signed byte"),
562 )?;
563 h.write_fixed_double("BSCALE", 1.0, 0, Some("default scaling factor"))?;
564 }
565 ImageType::U32 => {
566 h.write_fixed_double(
567 "BZERO",
568 2_147_483_648.0,
569 0,
570 Some("offset data range to that of unsigned long"),
571 )?;
572 h.write_fixed_double("BSCALE", 1.0, 0, Some("default scaling factor"))?;
573 }
574 _ => {}
575 }
576 }
577 self.flush()?;
578 Ok(())
579 }
580
581 pub(crate) fn read_compressed_pixels<T: crate::image::Pixel>(
582 &mut self,
583 firstelem: i64,
584 nelem: usize,
585 ) -> Result<Vec<T>> {
586 let info = {
587 let inner = self.inner()?;
588 let hdu = &inner.hdus[inner.current];
589 let nrows = hdu.header.get_i64("NAXIS2").unwrap_or(0);
590 hdu.header.zimage_info(nrows)?
591 };
592 let npix = info.npix();
593 if firstelem < 1 || firstelem - 1 + nelem as i64 > npix {
594 return Err(FitsError::new(crate::status::BAD_PIX_NUM));
595 }
596 let all = self.decompress_all(&info)?;
597 let start = (firstelem - 1) as usize;
598 let mut out = Vec::with_capacity(nelem);
599 for chunk in all[start * info.bytes_per_pixel()..]
600 .chunks_exact(info.bytes_per_pixel())
601 .take(nelem)
602 {
603 out.push(crate::image::decode_pixel(
604 chunk,
605 info.image_type()?,
606 info.bscale,
607 info.bzero,
608 )?);
609 }
610 Ok(out)
611 }
612
613 pub(crate) fn write_compressed_pixels<T: crate::image::Pixel>(
614 &mut self,
615 firstelem: i64,
616 data: &[T],
617 ) -> Result<()> {
618 if data.is_empty() {
619 return Ok(());
620 }
621 let info = {
622 let inner = self.inner()?;
623 let hdu = &inner.hdus[inner.current];
624 let nrows = hdu.header.get_i64("NAXIS2").unwrap_or(0);
625 hdu.header.zimage_info(nrows)?
626 };
627 let npix = info.npix() as usize;
628 let bpp = info.bytes_per_pixel();
629 let ty = info.image_type()?;
630 if firstelem < 1 || firstelem as usize - 1 + data.len() > npix {
631 return Err(FitsError::new(crate::status::BAD_PIX_NUM));
632 }
633 let mut raw = if firstelem == 1 && data.len() == npix {
634 vec![0u8; npix * bpp]
635 } else {
636 self.decompress_all(&info)?
637 };
638 if raw.len() < npix * bpp {
639 raw.resize(npix * bpp, 0);
640 }
641 let start = (firstelem - 1) as usize;
642 for (i, &px) in data.iter().enumerate() {
643 let enc = crate::image::encode_pixel(px, ty, info.bscale, info.bzero)?;
644 let off = (start + i) * bpp;
645 raw[off..off + bpp].copy_from_slice(&enc);
646 }
647 self.compress_all(&info, &raw)?;
648 Ok(())
649 }
650
651 fn decompress_all(&mut self, info: &ZImageInfo) -> Result<Vec<u8>> {
652 let npix = info.npix() as usize;
653 let bpp = info.bytes_per_pixel();
654 let mut out = vec![0u8; npix * bpp];
655 let col = self
656 .get_colnum(false, "COMPRESSED_DATA")
657 .map_err(|_| FitsError::new(DATA_DECOMPRESSION_ERR))?;
658 let zscale_col = self.get_colnum(false, "ZSCALE").ok();
659 let zzero_col = self.get_colnum(false, "ZZERO").ok();
660 let gzip_col = self.get_colnum(false, "GZIP_COMPRESSED_DATA").ok();
661 let tiles = enumerate_tiles(&info.znaxis, &info.tilesize);
662 let rand = if !info.no_quantize && info.zbitpix < 0 {
663 Some(dither_table())
664 } else {
665 None
666 };
667 for (row, origin, shape) in tiles {
668 let tile_npix: usize = shape.iter().map(|&d| d as usize).product();
669 let bytes = self.read_vla_bytes(col, row)?;
670 let (len, _) = self.read_descriptor(col, row)?;
671 let tile = if len == 0 {
672 if let Some(gc) = gzip_col {
673 let gz = self.read_vla_bytes(gc, row)?;
674 if gz.is_empty() {
675 vec![0u8; tile_npix * bpp]
676 } else {
677 decompress_gzip_tile(&gz, tile_npix, bpp, false)?
678 }
679 } else {
680 vec![0u8; tile_npix * bpp]
681 }
682 } else {
683 self.decompress_tile(
684 info,
685 row,
686 tile_npix,
687 &bytes,
688 &shape,
689 zscale_col,
690 zzero_col,
691 rand.as_deref(),
692 )?
693 };
694 scatter_tile(&mut out, &info.znaxis, &origin, &shape, &tile, bpp);
695 }
696 Ok(out)
697 }
698
699 fn decompress_tile(
700 &mut self,
701 info: &ZImageInfo,
702 row: i64,
703 tile_npix: usize,
704 bytes: &[u8],
705 shape: &[i64],
706 zscale_col: Option<i32>,
707 zzero_col: Option<i32>,
708 rand: Option<&[f32]>,
709 ) -> Result<Vec<u8>> {
710 let bpp = info.bytes_per_pixel();
711 let mut scale = 1.0f64;
712 let mut zero = 0.0f64;
713 if let (Some(sc), Some(zc)) = (zscale_col, zzero_col) {
714 scale = self
715 .read_bin_col_f64(sc, row, 1, None)
716 .ok()
717 .and_then(|(v, _)| v.first().copied())
718 .unwrap_or(1.0);
719 zero = self
720 .read_bin_col_f64(zc, row, 1, None)
721 .ok()
722 .and_then(|(v, _)| v.first().copied())
723 .unwrap_or(0.0);
724 }
725 match info.kind {
726 CompressionType::Rice1 => {
727 let ints =
728 rice::decompress(bytes, tile_npix, info.rice_bytepix, info.rice_blocksize)?;
729 ints_to_stored(&ints, info, scale, zero, row, rand)
730 }
731 CompressionType::Plio1 => {
732 let words = be_i16_words(bytes);
733 let ints = plio::decompress(&words, tile_npix)?;
734 ints_to_stored(&ints, info, scale, zero, row, rand)
735 }
736 CompressionType::Hcompress1 => {
737 let ny = shape.first().copied().unwrap_or(1) as i32;
738 let nx = if shape.len() > 1 { shape[1] as i32 } else { 1 };
739 let smooth = info.hcomp_smooth != 0;
740 let (ints, _, _, _) = if info.zbitpix == 8 || info.zbitpix == 16 {
741 hcompress::decompress(bytes, tile_npix.max((nx * ny) as usize), smooth)?
742 } else {
743 hcompress::decompress64(bytes, tile_npix.max((nx * ny) as usize), smooth)?
744 };
745 let take = ints.into_iter().take(tile_npix).collect::<Vec<_>>();
746 ints_to_stored(&take, info, scale, zero, row, rand)
747 }
748 CompressionType::Gzip1 | CompressionType::Gzip2 => {
749 let shuffle = info.kind == CompressionType::Gzip2;
750 let raw = decompress_gzip_tile(bytes, tile_npix, bpp, shuffle)?;
751 if info.zbitpix < 0 && !info.no_quantize {
752 let ints = be_i32_from_bytes(&raw);
753 ints_to_stored(&ints, info, scale, zero, row, rand)
754 } else {
755 Ok(raw)
756 }
757 }
758 CompressionType::None => Ok(bytes.to_vec()),
759 }
760 }
761
762 fn compress_all(&mut self, info: &ZImageInfo, raw: &[u8]) -> Result<()> {
763 let col = self.get_colnum(false, "COMPRESSED_DATA")?;
764 let zscale_col = self.get_colnum(false, "ZSCALE").ok();
765 let zzero_col = self.get_colnum(false, "ZZERO").ok();
766 let bpp = info.bytes_per_pixel();
767 let rand = if info.zbitpix < 0 && !info.no_quantize {
768 Some(dither_table())
769 } else {
770 None
771 };
772 let qlevel = {
773 let q = self.inner()?.compression.quantize_level;
774 if q == 0.0 { 4.0 } else { q }
775 };
776 for (row, origin, shape) in enumerate_tiles(&info.znaxis, &info.tilesize) {
777 let tile = gather_tile(raw, &info.znaxis, &origin, &shape, bpp);
778 let tile_npix = tile.len() / bpp.max(1);
779 let (payload, nelem, maybe_scale) =
780 self.compress_tile(info, row, &tile, tile_npix, &shape, qlevel, rand.as_deref())?;
781 if info.kind == CompressionType::Plio1 {
782 self.write_vla_bytes(col, row, &payload, nelem)?;
783 } else {
784 self.write_vla_bytes(col, row, &payload, payload.len() as i64)?;
785 }
786 if let (Some(sc), Some(zc), Some((bscale, bzero))) =
787 (zscale_col, zzero_col, maybe_scale)
788 {
789 self.write_bin_col_f64(sc, row, &[bscale])?;
790 self.write_bin_col_f64(zc, row, &[bzero])?;
791 }
792 }
793 Ok(())
794 }
795
796 fn compress_tile(
797 &mut self,
798 info: &ZImageInfo,
799 row: i64,
800 tile: &[u8],
801 tile_npix: usize,
802 shape: &[i64],
803 qlevel: f32,
804 rand: Option<&[f32]>,
805 ) -> Result<(Vec<u8>, i64, Option<(f64, f64)>)> {
806 let bpp = info.bytes_per_pixel();
807 if info.zbitpix < 0 && info.no_quantize {
808 let shuffle = info.kind == CompressionType::Gzip2;
809 let payload = compress_gzip_tile(tile, bpp, shuffle)?;
810 return Ok((payload, 0, None));
811 }
812 if info.zbitpix < 0 {
813 let floats = match info.zbitpix {
814 -32 => tile
815 .chunks_exact(4)
816 .map(|c| f64::from(f32::from_be_bytes(c.try_into().unwrap())))
817 .collect::<Vec<_>>(),
818 _ => tile
819 .chunks_exact(8)
820 .map(|c| f64::from_be_bytes(c.try_into().unwrap()))
821 .collect::<Vec<_>>(),
822 };
823 let nx = shape.first().copied().unwrap_or(tile_npix as i64) as usize;
824 let ny = tile_npix / nx.max(1);
825 let dither_row = if info.quantize_method == NO_DITHER {
826 0
827 } else {
828 row + i64::from(info.dither_seed.saturating_sub(1))
829 };
830 let q = quantize::quantize(
831 &floats,
832 nx,
833 ny,
834 qlevel,
835 info.quantize_method,
836 dither_row,
837 rand.unwrap_or(&[]),
838 );
839 if let Some(q) = q {
840 let payload = compress_ints(info, &q.idata, shape)?;
841 let nelem = if info.kind == CompressionType::Plio1 {
842 (payload.len() / 2) as i64
843 } else {
844 payload.len() as i64
845 };
846 return Ok((payload, nelem, Some((q.bscale, q.bzero))));
847 }
848 let payload = compress_gzip_tile(tile, bpp, info.kind == CompressionType::Gzip2)?;
849 let n = payload.len() as i64;
850 return Ok((payload, n, None));
851 }
852 let ints = stored_to_ints(tile, info.zbitpix);
853 let payload = compress_ints(info, &ints, shape)?;
854 let nelem = if info.kind == CompressionType::Plio1 {
855 (payload.len() / 2) as i64
856 } else {
857 payload.len() as i64
858 };
859 Ok((payload, nelem, None))
860 }
861}
862
863fn compress_ints(info: &ZImageInfo, ints: &[i32], shape: &[i64]) -> Result<Vec<u8>> {
864 match info.kind {
865 CompressionType::Rice1 => rice::compress(ints, info.rice_bytepix, info.rice_blocksize),
866 CompressionType::Plio1 => {
867 let words = plio::compress(ints)?;
868 Ok(words.iter().flat_map(|w| w.to_be_bytes()).collect())
869 }
870 CompressionType::Hcompress1 => {
871 let ny = shape.first().copied().unwrap_or(1) as i32;
872 let nx = if shape.len() > 1 { shape[1] as i32 } else { 1 };
873 let scale = if info.hcomp_scale < 0.0 {
874 (-info.hcomp_scale + 0.5) as i32
875 } else {
876 0
877 };
878 if info.zbitpix == 8 || info.zbitpix == 16 {
879 let mut a = ints.to_vec();
880 a.resize((nx as usize) * (ny as usize).max(1), 0);
881 hcompress::compress(&mut a, ny, nx, scale)
882 } else {
883 let mut a: Vec<i64> = ints.iter().copied().map(i64::from).collect();
884 a.resize((nx as usize) * (ny as usize).max(1), 0);
885 hcompress::compress64(&mut a, ny, nx, scale)
886 }
887 }
888 CompressionType::Gzip1 | CompressionType::Gzip2 => {
889 let bpp = info.bytes_per_pixel().min(4).max(if info.zbitpix == 8 {
890 1
891 } else if info.zbitpix == 16 {
892 2
893 } else {
894 4
895 });
896 let bytes = ints_to_be(ints, bpp);
897 compress_gzip_tile(&bytes, bpp, info.kind == CompressionType::Gzip2)
898 }
899 CompressionType::None => Ok(ints_to_be(ints, info.bytes_per_pixel())),
900 }
901}
902
903fn ints_to_stored(
904 ints: &[i32],
905 info: &ZImageInfo,
906 scale: f64,
907 zero: f64,
908 row: i64,
909 rand: Option<&[f32]>,
910) -> Result<Vec<u8>> {
911 if info.zbitpix < 0 && (scale != 1.0 || zero != 0.0 || rand.is_some()) {
912 let dither_row = if info.quantize_method == NO_DITHER {
913 0
914 } else {
915 row + i64::from(info.dither_seed.saturating_sub(1))
916 };
917 let floats = quantize::dequantize(
918 ints,
919 scale,
920 zero,
921 info.quantize_method,
922 dither_row,
923 rand.unwrap_or(&[]),
924 );
925 let mut out = Vec::with_capacity(floats.len() * info.bytes_per_pixel());
926 for f in floats {
927 if info.zbitpix == -32 {
928 out.extend_from_slice(&(f as f32).to_be_bytes());
929 } else {
930 out.extend_from_slice(&f.to_be_bytes());
931 }
932 }
933 return Ok(out);
934 }
935 Ok(ints_to_be(ints, info.bytes_per_pixel()))
936}
937
938fn ints_to_be(ints: &[i32], bpp: usize) -> Vec<u8> {
939 let mut out = Vec::with_capacity(ints.len() * bpp);
940 for &v in ints {
941 match bpp {
942 1 => out.push(v as u8),
943 2 => out.extend_from_slice(&(v as i16).to_be_bytes()),
944 8 => out.extend_from_slice(&i64::from(v).to_be_bytes()),
945 _ => out.extend_from_slice(&v.to_be_bytes()),
946 }
947 }
948 out
949}
950
951fn stored_to_ints(tile: &[u8], zbitpix: i32) -> Vec<i32> {
952 match zbitpix {
953 8 => tile.iter().map(|&b| i32::from(b)).collect(),
954 16 => tile
955 .chunks_exact(2)
956 .map(|c| i32::from(i16::from_be_bytes(c.try_into().unwrap())))
957 .collect(),
958 _ => tile
959 .chunks_exact(4)
960 .map(|c| i32::from_be_bytes(c.try_into().unwrap()))
961 .collect(),
962 }
963}
964
965fn be_i16_words(bytes: &[u8]) -> Vec<i16> {
966 bytes
967 .chunks_exact(2)
968 .map(|c| i16::from_be_bytes(c.try_into().unwrap()))
969 .collect()
970}
971
972fn be_i32_from_bytes(bytes: &[u8]) -> Vec<i32> {
973 bytes
974 .chunks_exact(4)
975 .map(|c| i32::from_be_bytes(c.try_into().unwrap()))
976 .collect()
977}
978
979fn compress_gzip_tile(tile: &[u8], width: usize, shuffle: bool) -> Result<Vec<u8>> {
980 #[cfg(feature = "gzip")]
981 {
982 gzip_tile::compress_tile(tile, width, shuffle)
983 }
984 #[cfg(not(feature = "gzip"))]
985 {
986 let _ = (tile, width, shuffle);
987 Err(FitsError::new(DATA_COMPRESSION_ERR))
988 }
989}
990
991fn decompress_gzip_tile(bytes: &[u8], npix: usize, width: usize, shuffle: bool) -> Result<Vec<u8>> {
992 #[cfg(feature = "gzip")]
993 {
994 gzip_tile::decompress_tile(bytes, npix, width, shuffle)
995 }
996 #[cfg(not(feature = "gzip"))]
997 {
998 let _ = (bytes, npix, width, shuffle);
999 Err(FitsError::new(DATA_DECOMPRESSION_ERR))
1000 }
1001}
1002
1003fn actual_tilesize(
1004 kind: CompressionType,
1005 naxes: &[i64],
1006 request: &[i64; MAX_COMPRESS_DIM],
1007) -> Result<Vec<i64>> {
1008 let naxis = naxes.len();
1009 if kind == CompressionType::Hcompress1 {
1010 if naxis < 2 {
1011 return Err(FitsError::with_message(
1012 DATA_COMPRESSION_ERR,
1013 "Hcompress cannot be used with 1-dimensional images (imcomp_init_table)",
1014 ));
1015 }
1016 let mut t = vec![0i64; naxis];
1017 t[0] = if request[0] <= 0 {
1018 naxes[0]
1019 } else {
1020 request[0]
1021 };
1022 if naxes[1] <= 30 {
1023 t[1] = naxes[1];
1024 } else {
1025 t[1] = 16;
1026 }
1027 for i in 2..naxis {
1028 t[i] = 1;
1029 }
1030 if t[0] < 4 || t[1] < 4 {
1031 t[0] = naxes[0].max(4);
1032 t[1] = naxes[1].max(4);
1033 }
1034 return Ok(t);
1035 }
1036 let mut t = Vec::with_capacity(naxis);
1037 for i in 0..naxis {
1038 let r = request.get(i).copied().unwrap_or(0);
1039 if i == 0 {
1040 t.push(if r <= 0 { naxes[i] } else { r });
1041 } else if r < 0 {
1042 t.push(naxes[i]);
1043 } else if r == 0 {
1044 t.push(1);
1045 } else {
1046 t.push(r);
1047 }
1048 }
1049 Ok(t)
1050}
1051
1052fn tile_count(naxes: &[i64], tiles: &[i64]) -> i64 {
1053 naxes
1054 .iter()
1055 .zip(tiles)
1056 .map(|(&n, &t)| (n - 1) / t + 1)
1057 .product()
1058}
1059
1060fn enumerate_tiles(naxes: &[i64], tiles: &[i64]) -> Vec<(i64, Vec<i64>, Vec<i64>)> {
1062 let naxis = naxes.len();
1063 let ntiles: Vec<i64> = naxes
1064 .iter()
1065 .zip(tiles)
1066 .map(|(&n, &t)| (n - 1) / t + 1)
1067 .collect();
1068 let total: i64 = ntiles.iter().product();
1069 let mut out = Vec::with_capacity(total as usize);
1070 for row0 in 0..total {
1071 let mut rest = row0;
1072 let mut idx = vec![0i64; naxis];
1073 for i in 0..naxis {
1075 idx[i] = rest % ntiles[i];
1076 rest /= ntiles[i];
1077 }
1078 let mut origin = vec![0i64; naxis];
1079 let mut shape = vec![0i64; naxis];
1080 for i in 0..naxis {
1081 origin[i] = idx[i] * tiles[i];
1082 shape[i] = (origin[i] + tiles[i]).min(naxes[i]) - origin[i];
1083 }
1084 out.push((row0 + 1, origin, shape));
1085 }
1086 out
1087}
1088
1089fn scatter_tile(
1090 dest: &mut [u8],
1091 naxes: &[i64],
1092 origin: &[i64],
1093 shape: &[i64],
1094 tile: &[u8],
1095 bpp: usize,
1096) {
1097 if naxes.len() == 1 {
1098 let off = origin[0] as usize * bpp;
1099 dest[off..off + tile.len()].copy_from_slice(tile);
1100 return;
1101 }
1102 let nx = naxes[0] as usize;
1103 let tx = shape[0] as usize;
1104 let n_rest: usize = shape.iter().skip(1).map(|&d| d as usize).product();
1105 for r in 0..n_rest {
1106 let mut coords = vec![0i64; naxes.len()];
1107 let mut rem = r as i64;
1108 for i in 1..naxes.len() {
1109 coords[i] = rem % shape[i];
1110 rem /= shape[i];
1111 }
1112 let mut dest_off = origin[0] as usize;
1113 let mut stride = nx;
1114 dest_off += (origin[1] as usize + coords[1] as usize) * stride;
1115 for i in 2..naxes.len() {
1116 stride *= naxes[i - 1] as usize;
1117 dest_off += (origin[i] as usize + coords[i] as usize) * stride;
1118 }
1119 dest_off *= bpp;
1120 let src_off = r * tx * bpp;
1121 dest[dest_off..dest_off + tx * bpp].copy_from_slice(&tile[src_off..src_off + tx * bpp]);
1122 }
1123}
1124
1125pub fn fits_set_compression_type(f: &mut FitsFile, ctype: i32) -> Result<()> {
1127 f.set_compression_type(ctype)
1128}
1129
1130pub fn fits_is_compressed_image(f: &FitsFile) -> Result<bool> {
1132 f.is_compressed_image()
1133}
1134
1135fn gather_tile(src: &[u8], naxes: &[i64], origin: &[i64], shape: &[i64], bpp: usize) -> Vec<u8> {
1136 let tile_npix: usize = shape.iter().map(|&d| d as usize).product();
1137 let mut tile = vec![0u8; tile_npix * bpp];
1138 if naxes.len() == 1 {
1139 let off = origin[0] as usize * bpp;
1140 let n = tile.len();
1141 tile.copy_from_slice(&src[off..off + n]);
1142 return tile;
1143 }
1144 let nx = naxes[0] as usize;
1145 let tx = shape[0] as usize;
1146 let n_rest: usize = shape.iter().skip(1).map(|&d| d as usize).product();
1147 for r in 0..n_rest {
1148 let mut coords = vec![0i64; naxes.len()];
1149 let mut rem = r as i64;
1150 for i in 1..naxes.len() {
1151 coords[i] = rem % shape[i];
1152 rem /= shape[i];
1153 }
1154 let mut src_off = origin[0] as usize;
1155 let mut stride = nx;
1156 src_off += (origin[1] as usize + coords[1] as usize) * stride;
1157 for i in 2..naxes.len() {
1158 stride *= naxes[i - 1] as usize;
1159 src_off += (origin[i] as usize + coords[i] as usize) * stride;
1160 }
1161 src_off *= bpp;
1162 let dest_off = r * tx * bpp;
1163 tile[dest_off..dest_off + tx * bpp].copy_from_slice(&src[src_off..src_off + tx * bpp]);
1164 }
1165 tile
1166}