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
use crate::{Handler, Rst, Scan, Frame, Dac, Dht, Dqt, App0Jfif}; pub struct TextFormat { verbose: bool, } impl TextFormat { pub fn new(verbose: bool) -> TextFormat { TextFormat { verbose } } } fn print_ascii_value(v: u8) { if v.is_ascii_graphic() || v == 0x20 { print!("{}", v as char); } else { print!("\\x{:#04X}", v); } } impl Handler for TextFormat { fn handle_app(&mut self, nr: u8, data: &[u8]) { print!("App(0x{:X}):", nr); for &v in data.iter().take(20) { print_ascii_value(v); } println!(); } fn handle_app0_jfif(&mut self, jfif: &App0Jfif) { println!("App(0x0): JFIF"); println!(" Version: {}.{:02}", jfif.major, jfif.minor); let unit = match jfif.unit { 0 => "pixel".to_owned(), 1 => "dots per inch".to_owned(), 2 => "dots per cm".to_owned(), _ => format!("Unknown unit: {}", jfif.unit), }; println!(" Density: {}x{} {}", jfif.x_density, jfif.y_density, unit); println!(" Thumbnail: {}x{}", jfif.x_thumbnail, jfif.y_thumbnail); } fn handle_dqt(&mut self, tables: &[Dqt]) { println!("DQT:"); for table in tables { print!(" {}: Precision {}", table.dest, table.precision); if self.verbose { for (i, &v) in table.values.iter().enumerate() { if i % 8 == 0 { print!("\n "); } if v < 10 { print!(" "); } if v < 100 { print!(" "); } print!("{}, ", v) } println!(); } } } fn handle_dht(&mut self, tables: &[Dht]) { println!("DHT:"); for table in tables { println!(" Table {}: Class {}", table.dest, table.class); if self.verbose { print!(" Code lengths: "); for (i, &v) in table.code_lengths.iter().enumerate() { if i > 0 { print!(", "); } print!("{}", v) } println!(); } } } fn handle_dac(&mut self, dac: &Dac) { println!("DAC:"); for param in &dac.params { println!(" Class: {} Dest: {} Value: {}", param.class, param.dest, param.value); } } fn handle_frame(&mut self, frame: &Frame) { println!("Frame: {}", frame.get_sof_name()); println!(" Precision: {}", frame.precision); println!(" Dimension: {}x{}", frame.dimension_x, frame.dimension_y); for component in &frame.components { println!(" Component({}): Sampling {}x{} Quantization: {}", component.id, component.horizontal_sampling_factor, component.vertical_sampling_factor, component.quantization_table); } } fn handle_scan(&mut self, scan: &Scan) { println!("Scan: "); for component in &scan.components { println!(" Component: {} DC:{} AC:{}", component.id, component.dc_table, component.dc_table); } println!(" Selection: {} to {}", scan.selection_start, scan.selection_end); println!(" Approximation: {} to {}", scan.approximation_low, scan.approximation_high); println!(" Data: {} bytes", scan.data.len()); } fn handle_dri(&mut self, restart: u16) { println!("DRI: {}", restart); } fn handle_rst(&mut self, restart: &Rst) { println!("RST({}): Data: {} bytes", restart.nr, restart.data.len()); } fn handle_comment(&mut self, data: &[u8]) { if let Ok(comment) = std::str::from_utf8(data) { println!("Comment: {}", comment); } else { println!("Comment: BAD STRING WITH LENGTH {}", data.len()); } } fn handle_unknown(&mut self, marker: u8, data: &[u8]) { println!("Unknown(0x{:X}):{}", marker, data.len()); } }