1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
mod dithering;
pub mod pixels;
mod rice;
use dithering::{N_RANDOM, RAND_VALUES};
use crate::hdu::header::extension::bintable::{BinTable, TileCompressedImage, ZQuantiz};
use crate::hdu::header::Header;
pub trait Keywords {
type T;
fn new(header: &Header<BinTable>, config: &TileCompressedImage) -> Self;
}
#[derive(Debug)]
struct TileDesc<K> {
/// Total number of pixels in the tile
pub n_pixels: u64,
/// Number of pixels remaining to return
pub remaining_pixels: u64,
/// Optional keywords relative to the parsing of floats
pub keywords: K,
}
impl<K> TileDesc<K>
where
K: Keywords,
{
fn new(header: &Header<BinTable>, config: &TileCompressedImage) -> Self {
let keywords = K::new(header, config);
Self {
keywords,
n_pixels: 0,
remaining_pixels: 0,
}
}
}
#[derive(Debug)]
pub struct F32Keywords {
/// Idx column storing z_scale values for each tile
z_scale_idx: usize,
/// Idx column storing z_zero values for each tile
z_zero_idx: usize,
/// Idx column storing z_blank values for each tile
z_blank_idx: Option<usize>,
/// Dither
z_dither_0: i64,
/// quantiz option
z_quantiz: ZQuantiz,
/// Current value of ZSCALE field (floating point case)
scale: f32,
/// Current value of ZZERO field (floating point case)
zero: f32,
/// Current value of ZBLANK (floating point case)
/// Stores the integer value that evaluates to a floating point NAN
z_blank: Option<i32>,
/// Current value of ZBLANK field (float and integer case)
/// Current quantiz state
quantiz: Quantiz,
}
#[derive(Debug)]
pub struct F64Keywords {
/// Idx column storing z_scale values for each tile
z_scale_idx: usize,
/// Idx column storing z_zero values for each tile
z_zero_idx: usize,
/// Idx column storing z_blank values for each tile
z_blank_idx: Option<usize>,
/// Dither
z_dither_0: i64,
/// quantiz option
z_quantiz: ZQuantiz,
/// Current value of ZSCALE field (floating point case)
scale: f32,
/// Current value of ZZERO field (floating point case)
zero: f32,
/// Current value of ZBLANK (floating point case)
/// Stores the integer value that evaluates to a floating point NAN
z_blank: Option<i32>,
/// Current value of ZBLANK field (float and integer case)
/// Current quantiz state
quantiz: Quantiz,
}
impl F64Keywords {
/// Unquantize the integer decoded value to the real floating point value
fn unquantize(&mut self, value: i32) -> f32 {
// map the NaN if value corresponds to BLANK
if let Some(z_blank) = self.z_blank {
if z_blank == value {
return f32::NAN;
}
}
match &mut self.quantiz {
Quantiz::NoDither => (value as f32) * self.scale + self.zero,
Quantiz::SubtractiveDither1 { i1 } => {
let ri = RAND_VALUES[*i1];
// increment i1 for the next pixel
*i1 = (*i1 + 1) % N_RANDOM;
((value as f32) - ri + 0.5) * self.scale + self.zero
}
Quantiz::SubtractiveDither2 { i1 } => {
// FIXME: i32::MIN is -2147483648 !
if value == -2147483647 {
*i1 = (*i1 + 1) % N_RANDOM;
0.0
} else {
let ri = RAND_VALUES[*i1];
// increment i1 for the next pixel
*i1 = (*i1 + 1) % N_RANDOM;
((value as f32) - ri + 0.5) * self.scale + self.zero
}
}
}
}
}
impl F32Keywords {
/// Unquantize the integer decoded value to the real floating point value
fn unquantize(&mut self, value: i32) -> f32 {
// map the NaN if value corresponds to BLANK
if let Some(z_blank) = self.z_blank {
if z_blank == value {
return f32::NAN;
}
}
match &mut self.quantiz {
Quantiz::NoDither => (value as f32) * self.scale + self.zero,
Quantiz::SubtractiveDither1 { i1 } => {
let ri = RAND_VALUES[*i1];
// increment i1 for the next pixel
*i1 = (*i1 + 1) % N_RANDOM;
((value as f32) - ri + 0.5) * self.scale + self.zero
}
Quantiz::SubtractiveDither2 { i1 } => {
// FIXME: i32::MIN is -2147483648 !
if value == -2147483647 {
*i1 = (*i1 + 1) % N_RANDOM;
0.0
} else {
let ri = RAND_VALUES[*i1];
// increment i1 for the next pixel
*i1 = (*i1 + 1) % N_RANDOM;
((value as f32) - ri + 0.5) * self.scale + self.zero
}
}
}
}
}
impl Keywords for F32Keywords {
type T = f32;
fn new(header: &Header<BinTable>, config: &TileCompressedImage) -> Self {
let TileCompressedImage {
z_dither_0,
z_quantiz,
..
} = config;
let ctx = header.get_xtension();
let z_scale_idx = ctx.find_field_by_ttype("ZSCALE").unwrap();
let z_zero_idx = ctx.find_field_by_ttype("ZZERO").unwrap();
let mut z_blank = None;
let z_blank_idx = ctx
// Check for a field named ZBLANK
.find_field_by_ttype("ZBLANK")
.or_else(|| {
z_blank = header
.get_parsed("ZBLANK")
// TODO: we should probably propagate errors from here if ZBLANK/BLANK exist but are of a wrong type.
.ok();
None
});
// If no ZBLANK colum has been found then check the header keywords (ZBLANK for float, BLANK for integer)
Self {
scale: 1.0,
zero: 0.0,
z_scale_idx,
z_zero_idx,
z_blank_idx,
z_blank,
quantiz: Quantiz::NoDither,
z_quantiz: z_quantiz.clone().unwrap_or(ZQuantiz::NoDither),
z_dither_0: z_dither_0.unwrap_or(0),
}
}
}
impl Keywords for F64Keywords {
type T = f64;
fn new(header: &Header<BinTable>, config: &TileCompressedImage) -> Self {
let TileCompressedImage {
z_dither_0,
z_quantiz,
..
} = config;
let ctx = header.get_xtension();
let z_scale_idx = ctx.find_field_by_ttype("ZSCALE").unwrap();
let z_zero_idx = ctx.find_field_by_ttype("ZZERO").unwrap();
let mut z_blank = None;
let z_blank_idx = ctx
// Check for a field named ZBLANK
.find_field_by_ttype("ZBLANK")
.or_else(|| {
z_blank = header
.get_parsed("ZBLANK")
// TODO: we should probably propagate errors from here if ZBLANK/BLANK exist but are of a wrong type.
.ok();
None
});
// If no ZBLANK colum has been found then check the header keywords (ZBLANK for float, BLANK for integer)
Self {
scale: 1.0,
zero: 0.0,
z_scale_idx,
z_zero_idx,
z_blank_idx,
z_blank,
quantiz: Quantiz::NoDither,
z_quantiz: z_quantiz.clone().unwrap_or(ZQuantiz::NoDither),
z_dither_0: z_dither_0.unwrap_or(0),
}
}
}
#[derive(Debug)]
pub struct U8Keywords {
_blank: Option<u8>,
}
impl Keywords for U8Keywords {
type T = u8;
fn new(header: &Header<BinTable>, _: &TileCompressedImage) -> Self {
let _blank = header.get_parsed::<Self::T>("BLANK").ok();
Self { _blank }
}
}
#[derive(Debug)]
pub struct I16Keywords {
_blank: Option<i16>,
}
impl Keywords for I16Keywords {
type T = i16;
fn new(header: &Header<BinTable>, _: &TileCompressedImage) -> Self {
let _blank = header.get_parsed::<Self::T>("BLANK").ok();
Self { _blank }
}
}
#[derive(Debug)]
pub struct I32Keywords {
_blank: Option<i32>,
}
impl Keywords for I32Keywords {
type T = i32;
fn new(header: &Header<BinTable>, _: &TileCompressedImage) -> Self {
let _blank = header.get_parsed::<Self::T>("BLANK").ok();
Self { _blank }
}
}
#[derive(Debug)]
enum Quantiz {
NoDither,
SubtractiveDither1 { i1: usize },
SubtractiveDither2 { i1: usize },
}
#[cfg(test)]
mod tests {
use crate::hdu::data::bintable::data::BinaryTableData;
use image::DynamicImage;
use std::io::{Cursor, Read};
use test_case::test_case;
use crate::{hdu::data::bintable::tile_compressed::pixels::Pixels, Fits, HDU};
/*
#[test]
fn test_tile_size_from_row_idx() {
use super::pixels::tile_size_from_row_idx;
let ground_truth = [
[300, 200, 150],
[300, 200, 150],
[300, 200, 150],
[100, 200, 150],
[300, 200, 150],
[300, 200, 150],
[300, 200, 150],
[100, 200, 150],
[300, 100, 150],
[300, 100, 150],
[300, 100, 150],
[100, 100, 150],
[300, 200, 150],
[300, 200, 150],
[300, 200, 150],
[100, 200, 150],
[300, 200, 150],
[300, 200, 150],
[300, 200, 150],
[100, 200, 150],
[300, 100, 150],
[300, 100, 150],
[300, 100, 150],
[100, 100, 150],
[300, 200, 50],
[300, 200, 50],
[300, 200, 50],
[100, 200, 50],
[300, 200, 50],
[300, 200, 50],
[300, 200, 50],
[100, 200, 50],
[300, 100, 50],
[300, 100, 50],
[300, 100, 50],
[100, 100, 50],
];
for (i, &ground_truth) in ground_truth.iter().enumerate() {
let tile_s = tile_size_from_row_idx(&[300, 200, 150], &[1000, 500, 350], i);
assert_eq!([tile_s[0], tile_s[1], tile_s[2]], ground_truth);
}
}*/
#[test_case("samples/fits.gsfc.nasa.gov/m13real_rice.fits", 1000.0)]
#[test_case("samples/fits.gsfc.nasa.gov/m13_rice.fits", 1000.0)]
#[test_case("samples/fits.gsfc.nasa.gov/m13_gzip.fits", 1000.0)]
fn test_fits_without_dithering(filename: &str, vmax: f32) {
use std::fs::File;
let mut f = File::open(filename).unwrap();
let mut buf = Vec::new();
f.read_to_end(&mut buf).unwrap();
let reader = Cursor::new(&buf[..]);
let mut hdu_list = Fits::from_reader(reader);
while let Some(Ok(hdu)) = hdu_list.next() {
if let HDU::XBinaryTable(hdu) = hdu {
let width = hdu.get_header().get_parsed::<u32>("ZNAXIS1").unwrap();
let height = hdu.get_header().get_parsed::<u32>("ZNAXIS2").unwrap();
let img_bytes = match hdu_list.get_data(&hdu) {
BinaryTableData::TileCompressed(Pixels::U8(pixels)) => {
pixels.collect::<Vec<_>>()
}
BinaryTableData::TileCompressed(Pixels::I16(pixels)) => pixels
.map(|v| (((v as f32) / vmax) * 255.0) as u8)
.collect::<Vec<_>>(),
BinaryTableData::TileCompressed(Pixels::I32(pixels)) => pixels
.map(|v| (((v as f32) / vmax) * 255.0) as u8)
.collect::<Vec<_>>(),
BinaryTableData::TileCompressed(Pixels::F32(pixels)) => pixels
.map(|v| ((v / vmax) * 255.0) as u8)
.collect::<Vec<_>>(),
_ => unreachable!(),
};
let imgbuf = DynamicImage::ImageLuma8(
image::ImageBuffer::from_raw(width, height, img_bytes).unwrap(),
);
imgbuf.save(format!("{filename}.jpg")).unwrap();
}
}
}
#[test_case("samples/fits.gsfc.nasa.gov/FITS RICE integer.fz")]
fn test_fits_rice_integer(filename: &str) {
use std::fs::File;
let mut f = File::open(filename).unwrap();
let mut buf = Vec::new();
f.read_to_end(&mut buf).unwrap();
let reader = Cursor::new(&buf[..]);
let mut hdu_list = Fits::from_reader(reader);
while let Some(Ok(hdu)) = hdu_list.next() {
if let HDU::XBinaryTable(hdu) = hdu {
let width = hdu.get_header().get_parsed::<u32>("ZNAXIS1").unwrap();
let height = hdu.get_header().get_parsed::<u32>("ZNAXIS2").unwrap();
let bscale = hdu.get_header().get_parsed::<f32>("BSCALE").unwrap();
let bzero = hdu.get_header().get_parsed::<f32>("BZERO").unwrap();
if let BinaryTableData::TileCompressed(Pixels::I32(pixels)) =
hdu_list.get_data(&hdu)
{
let pixels = pixels
.map(|v| ((((v as f32) * bscale + bzero) / 100.0) * 255.0) as u8)
.collect::<Vec<_>>();
let imgbuf = DynamicImage::ImageLuma8(
image::ImageBuffer::from_raw(width, height, pixels).unwrap(),
);
imgbuf.save(format!("{filename}.jpg")).unwrap();
}
}
}
}
#[test_case("samples/fits.gsfc.nasa.gov/FITS RICE_ONE.fits")]
#[test_case("samples/fits.gsfc.nasa.gov/FITS RICE DITHER2 method.fz")]
fn test_fits_f32_with_dithering(filename: &str) {
use std::fs::File;
let mut f = File::open(filename).unwrap();
let mut buf = Vec::new();
f.read_to_end(&mut buf).unwrap();
let reader = Cursor::new(&buf[..]);
let mut hdu_list = Fits::from_reader(reader);
while let Some(Ok(hdu)) = hdu_list.next() {
if let HDU::XBinaryTable(hdu) = hdu {
let width = hdu.get_header().get_parsed::<u32>("ZNAXIS1").unwrap();
let height = hdu.get_header().get_parsed::<u32>("ZNAXIS2").unwrap();
let mut buf = vec![0_u8; (width as usize) * (height as usize)];
let tile_w = 100;
let tile_h = 100;
let num_tile_per_w = width / tile_w;
if let BinaryTableData::TileCompressed(Pixels::F32(pixels)) =
hdu_list.get_data(&hdu)
{
for (i, pixel) in pixels.map(|v| (v * 255.0) as u8).enumerate() {
let tile_idx = (i as u64) / ((tile_w * tile_h) as u64);
let x_tile_idx = tile_idx % (num_tile_per_w as u64);
let y_tile_idx = tile_idx / (num_tile_per_w as u64);
let pixel_inside_tile_idx = (i as u64) % ((tile_w * tile_h) as u64);
let x_pixel_inside_tile_idx = pixel_inside_tile_idx % (tile_w as u64);
let y_pixel_inside_tile_idx = pixel_inside_tile_idx / (tile_w as u64);
let x = x_tile_idx * (tile_w as u64) + x_pixel_inside_tile_idx;
let y = y_tile_idx * (tile_h as u64) + y_pixel_inside_tile_idx;
buf[(y * (width as u64) + x) as usize] = pixel;
}
}
let imgbuf = DynamicImage::ImageLuma8(
image::ImageBuffer::from_raw(width, height, buf).unwrap(),
);
imgbuf.save(format!("{filename}.jpg")).unwrap();
}
}
}
}