|
| 1 | +use crate::{ |
| 2 | + eof, |
| 3 | + jpeg::grammar::{ |
| 4 | + Component, |
| 5 | + EncodingProcess, |
| 6 | + HuffmanTable, |
| 7 | + Jpeg, |
| 8 | + Marker, |
| 9 | + QuantizationTable, |
| 10 | + StartOfFrame, |
| 11 | + StartOfScan, |
| 12 | + JFIF, |
| 13 | + }, |
| 14 | + read, |
| 15 | + util::read_bytes::{ |
| 16 | + U16_BYTES, |
| 17 | + U8_BYTES, |
| 18 | + }, |
| 19 | +}; |
| 20 | +use anyhow::{ |
| 21 | + anyhow, |
| 22 | + ensure, |
| 23 | + Result, |
| 24 | +}; |
| 25 | +use std::ops::{ |
| 26 | + Range, |
| 27 | + RangeInclusive, |
| 28 | +}; |
| 29 | + |
| 30 | +#[derive(Debug)] |
| 31 | +pub struct JpegDecoder<'a> { |
| 32 | + cursor: usize, |
| 33 | + data: &'a [u8], |
| 34 | +} |
| 35 | + |
| 36 | +impl<'a> JpegDecoder<'a> { |
| 37 | + pub const fn new(data: &'a [u8]) -> Self { |
| 38 | + Self { cursor: 0, data } |
| 39 | + } |
| 40 | + |
| 41 | + pub fn decode(&mut self) -> Result<Jpeg> { |
| 42 | + let _jfif = self.parse_jfif()?; |
| 43 | + |
| 44 | + todo!(); |
| 45 | + } |
| 46 | + |
| 47 | + fn parse_jfif(&mut self) -> Result<JFIF> { |
| 48 | + ensure!(self.read_marker()? == 0xFFD8); |
| 49 | + |
| 50 | + let mut quantization_tables = Vec::with_capacity(4); |
| 51 | + let mut huffman_tables = Vec::new(); |
| 52 | + let mut start_of_frame = None; |
| 53 | + let mut start_of_scan = None; |
| 54 | + let mut image_data = None; |
| 55 | + |
| 56 | + loop { |
| 57 | + match self.read_marker()? { |
| 58 | + 0xFFE0 => { |
| 59 | + self.parse_application_header()?; |
| 60 | + } |
| 61 | + 0xFFDB => { |
| 62 | + quantization_tables.push(self.parse_quantization_table()?); |
| 63 | + } |
| 64 | + 0xFFC4 => { |
| 65 | + huffman_tables.push(self.parse_huffman_table()?); |
| 66 | + } |
| 67 | + 0xFFDA => { |
| 68 | + ensure!(start_of_scan.is_none() && image_data.is_none()); |
| 69 | + start_of_scan = Some(self.parse_start_of_scan()?); |
| 70 | + image_data = Some(self.parse_image_data()?); |
| 71 | + |
| 72 | + break; |
| 73 | + } |
| 74 | + start_of_frame_marker if (start_of_frame_marker as u8 & 0xF0) == 0xC0 => { |
| 75 | + ensure!(start_of_frame.is_none()); |
| 76 | + start_of_frame = Some(self.parse_start_of_frame(start_of_frame_marker as u8)?); |
| 77 | + } |
| 78 | + foreign => unimplemented!("{:X}", foreign), |
| 79 | + }; |
| 80 | + } |
| 81 | + |
| 82 | + ensure!(self.read_marker()? == 0xFFD9); |
| 83 | + |
| 84 | + Ok(JFIF { |
| 85 | + quantization_tables, |
| 86 | + huffman_tables, |
| 87 | + start_of_frame: start_of_frame.ok_or_else(|| anyhow!("expected start of frame"))?, |
| 88 | + start_of_scan: start_of_scan.ok_or_else(|| anyhow!("expected start of scan"))?, |
| 89 | + image_data: image_data.ok_or_else(|| anyhow!("expected image data"))?, |
| 90 | + }) |
| 91 | + } |
| 92 | + |
| 93 | + fn parse_application_header(&mut self) -> Result<()> { |
| 94 | + let offset = self.cursor; |
| 95 | + let length = self.read_u16()?; |
| 96 | + |
| 97 | + ensure!(self.read_fixed::<5>()? == b"JFIF\0"); |
| 98 | + |
| 99 | + self.cursor = offset + length as usize; |
| 100 | + |
| 101 | + Ok(()) |
| 102 | + } |
| 103 | + |
| 104 | + fn parse_quantization_table(&mut self) -> Result<QuantizationTable> { |
| 105 | + let offset = self.cursor; |
| 106 | + let length = self.read_u16()? as usize; |
| 107 | + |
| 108 | + let flag = self.read_u8()?; |
| 109 | + |
| 110 | + let quantization_table = QuantizationTable { |
| 111 | + flag, |
| 112 | + element_range: Range { |
| 113 | + start: self.cursor, |
| 114 | + end: offset + length, |
| 115 | + }, |
| 116 | + }; |
| 117 | + |
| 118 | + self.cursor = offset + length; |
| 119 | + |
| 120 | + Ok(quantization_table) |
| 121 | + } |
| 122 | + |
| 123 | + fn parse_start_of_frame(&mut self, start_of_frame: u8) -> Result<StartOfFrame> { |
| 124 | + let encoding_process = EncodingProcess::try_from(start_of_frame & 0b1111)?; |
| 125 | + |
| 126 | + let offset = self.cursor; |
| 127 | + let length = self.read_u16()?; |
| 128 | + |
| 129 | + let start_of_frame = StartOfFrame { |
| 130 | + encoding_process, |
| 131 | + sample_precision: self.read_u8()?, |
| 132 | + lines: self.read_u16()?, |
| 133 | + samples_per_line: self.read_u16()?, |
| 134 | + components: { |
| 135 | + let number_of_image_components = self.read_u8()?; |
| 136 | + self.read_vec(number_of_image_components as usize, Self::parse_component)? |
| 137 | + }, |
| 138 | + }; |
| 139 | + |
| 140 | + ensure!(self.cursor == offset + length as usize); |
| 141 | + |
| 142 | + Ok(start_of_frame) |
| 143 | + } |
| 144 | + |
| 145 | + fn parse_component(&mut self) -> Result<Component> { |
| 146 | + Ok(Component { |
| 147 | + identifier: self.read_u8()?, |
| 148 | + sampling_factor: self.read_u8()?, |
| 149 | + quantization_table_destination_selector: self.read_u8()?, |
| 150 | + }) |
| 151 | + } |
| 152 | + |
| 153 | + fn parse_huffman_table(&mut self) -> Result<HuffmanTable> { |
| 154 | + let offset = self.cursor; |
| 155 | + let length = self.read_u16()? as usize; |
| 156 | + |
| 157 | + let flag = self.read_u8()?; |
| 158 | + |
| 159 | + let huffman_table = HuffmanTable { |
| 160 | + flag, |
| 161 | + element_range: Range { |
| 162 | + start: self.cursor, |
| 163 | + end: offset + length, |
| 164 | + }, |
| 165 | + }; |
| 166 | + |
| 167 | + self.cursor = offset + length; |
| 168 | + |
| 169 | + Ok(huffman_table) |
| 170 | + } |
| 171 | + |
| 172 | + fn parse_start_of_scan(&mut self) -> Result<StartOfScan> { |
| 173 | + let offset = self.cursor; |
| 174 | + let length = self.read_u16()?; |
| 175 | + |
| 176 | + let number_of_image_components = self.read_u8()?; |
| 177 | + let components = self.read_vec(number_of_image_components as usize, |this| { |
| 178 | + Ok((this.read_u8()?, this.read_u8()?)) |
| 179 | + })?; |
| 180 | + |
| 181 | + let start_of_scan = StartOfScan { |
| 182 | + components, |
| 183 | + spectral_select: RangeInclusive::new(self.read_u8()?, self.read_u8()?), |
| 184 | + approximation: self.read_u8()?, |
| 185 | + }; |
| 186 | + |
| 187 | + ensure!(self.cursor == offset + length as usize); |
| 188 | + |
| 189 | + Ok(start_of_scan) |
| 190 | + } |
| 191 | + |
| 192 | + fn parse_image_data(&mut self) -> Result<Range<usize>> { |
| 193 | + let range = Range { |
| 194 | + start: self.cursor, |
| 195 | + end: { |
| 196 | + while self.data[self.cursor..self.cursor + U16_BYTES] != [0xFF, 0xD9] { |
| 197 | + self.cursor += 1; |
| 198 | + } |
| 199 | + |
| 200 | + self.cursor |
| 201 | + }, |
| 202 | + }; |
| 203 | + |
| 204 | + Ok(range) |
| 205 | + } |
| 206 | + |
| 207 | + eof!(); |
| 208 | + read!(read_u8, u8, U8_BYTES); |
| 209 | + read!(read_u16, u16, U16_BYTES); |
| 210 | + read!(read_marker, Marker, U16_BYTES); |
| 211 | + |
| 212 | + fn read_fixed<const N: usize>(&mut self) -> Result<&'a [u8; N]> { |
| 213 | + self.eof(N)?; |
| 214 | + let bs = &self.data[self.cursor..self.cursor + N]; |
| 215 | + self.cursor += N; |
| 216 | + |
| 217 | + Ok(bs.try_into()?) |
| 218 | + } |
| 219 | + |
| 220 | + fn read_vec<T>( |
| 221 | + &mut self, |
| 222 | + capacity: usize, |
| 223 | + read_fn: impl Fn(&mut Self) -> Result<T>, |
| 224 | + ) -> Result<Vec<T>> { |
| 225 | + let mut list = Vec::with_capacity(capacity); |
| 226 | + |
| 227 | + for _ in 0..capacity { |
| 228 | + list.push(read_fn(self)?) |
| 229 | + } |
| 230 | + |
| 231 | + Ok(list) |
| 232 | + } |
| 233 | +} |
0 commit comments