Merge branch 'simplify/s13' into simplify/w2
Browse files at this commit12 changed files +310 -250
- crates/ec-ass/src/lib.rs +24 -7
- crates/ec-cc/src/extract.rs +42 -36
- crates/ec-dvbsub/src/lib.rs +52 -26
- crates/ec-image/src/bmp.rs +45 -29
- crates/ec-image/src/bytes.rs +17 -0
- crates/ec-image/src/exif.rs +5 -19
- crates/ec-image/src/lib.rs +1 -0
- crates/ec-image/src/tiff.rs +24 -37
- crates/ec-subs/src/ir.rs +3 -1
- crates/ec-subs/src/markup.rs +93 -91
- crates/ec-subs/src/srt.rs +2 -2
- crates/ec-subs/src/webvtt.rs +2 -2
crates/ec-ass/src/lib.rsmodified+24 -7
| @@ -42,7 +42,7 @@ pub fn parse(bytes: &[u8]) -> Result<SubtitleTrack> { | |||
| 42 | let mut saw_events = false; | 42 | let mut saw_events = false; |
| 43 | // `WrapStyle` is read once, when its line is seen; scanning the metadata for | 43 | // `WrapStyle` is read once, when its line is seen; scanning the metadata for |
| 44 | // every Dialogue line instead made a long script quadratic. | 44 | // every Dialogue line instead made a long script quadratic. |
| 45 | - let mut wrap_style = 0u8; | 45 | + let mut wrap_style = WrapStyle::Auto; |
| 46 | let mut wrap_seen = false; | 46 | let mut wrap_seen = false; |
| 47 | for line in text.split_inclusive('\n') { | 47 | for line in text.split_inclusive('\n') { |
| 48 | let trimmed = line.trim(); | 48 | let trimmed = line.trim(); |
| @@ -70,7 +70,7 @@ pub fn parse(bytes: &[u8]) -> Result<SubtitleTrack> { | |||
| 70 | let key = snake_case(key); | 70 | let key = snake_case(key); |
| 71 | // The first `WrapStyle` line counts, as it always did. | 71 | // The first `WrapStyle` line counts, as it always did. |
| 72 | if key == "wrap_style" && !wrap_seen { | 72 | if key == "wrap_style" && !wrap_seen { |
| 73 | - wrap_style = value.parse::<u8>().unwrap_or(0); | 73 | + wrap_style = WrapStyle::parse(value); |
| 74 | wrap_seen = true; | 74 | wrap_seen = true; |
| 75 | } | 75 | } |
| 76 | track.metadata.push((key, value.to_owned())); | 76 | track.metadata.push((key, value.to_owned())); |
| @@ -210,8 +210,10 @@ fn alignment(code: i32, v4: bool) -> TextAlign { | |||
| 210 | } | 210 | } |
| 211 | } | 211 | } |
| 212 | 212 | ||
| 213 | +type Rgba = (u8, u8, u8, u8); | ||
| 214 | + | ||
| 213 | /// `&HAABBGGRR&`: BGR with a *transparency* byte, so 0 is opaque. | 215 | /// `&HAABBGGRR&`: BGR with a *transparency* byte, so 0 is opaque. |
| 214 | -fn parse_color(value: &str) -> Option<(u8, u8, u8, u8)> { | 216 | +fn parse_color(value: &str) -> Option<Rgba> { |
| 215 | let digits: String = value | 217 | let digits: String = value |
| 216 | .trim() | 218 | .trim() |
| 217 | .trim_start_matches(['&', 'H', 'h']) | 219 | .trim_start_matches(['&', 'H', 'h']) |
| @@ -228,7 +230,7 @@ fn parse_color(value: &str) -> Option<(u8, u8, u8, u8)> { | |||
| 228 | )) | 230 | )) |
| 229 | } | 231 | } |
| 230 | 232 | ||
| 231 | -fn dialogue(format: &[String], row: &str, wrap_style: u8) -> Option<SubtitleCue> { | 233 | +fn dialogue(format: &[String], row: &str, wrap_style: WrapStyle) -> Option<SubtitleCue> { |
| 232 | const DEFAULT: &str = "Layer,Start,End,Style,Name,MarginL,MarginR,MarginV,Effect,Text"; | 234 | const DEFAULT: &str = "Layer,Start,End,Style,Name,MarginL,MarginR,MarginV,Effect,Text"; |
| 233 | let fallback = fields(DEFAULT); | 235 | let fallback = fields(DEFAULT); |
| 234 | let format = if format.is_empty() { &fallback } else { format }; | 236 | let format = if format.is_empty() { &fallback } else { format }; |
| @@ -276,7 +278,7 @@ struct Overrides { | |||
| 276 | } | 278 | } |
| 277 | 279 | ||
| 278 | /// One `Text` field into runs, plus whatever positioning its overrides state. | 280 | /// One `Text` field into runs, plus whatever positioning its overrides state. |
| 279 | -fn body(text: &str, wrap_style: u8) -> (Vec<Segment>, Option<CuePosition>) { | 281 | +fn body(text: &str, wrap_style: WrapStyle) -> (Vec<Segment>, Option<CuePosition>) { |
| 280 | let mut out: Vec<Segment> = Vec::new(); | 282 | let mut out: Vec<Segment> = Vec::new(); |
| 281 | let mut state = Overrides::default(); | 283 | let mut state = Overrides::default(); |
| 282 | let mut position = CuePosition::default(); | 284 | let mut position = CuePosition::default(); |
| @@ -445,12 +447,27 @@ fn split_tag(tag: &str) -> Option<(&str, &str)> { | |||
| 445 | } | 447 | } |
| 446 | 448 | ||
| 447 | /// Plain text between override blocks, with the three escapes ASS gives it. | 449 | /// Plain text between override blocks, with the three escapes ASS gives it. |
| 450 | +#[derive(Clone, Copy, PartialEq, Eq)] | ||
| 451 | +enum WrapStyle { | ||
| 452 | + Auto, | ||
| 453 | + Manual, | ||
| 454 | +} | ||
| 455 | + | ||
| 456 | +impl WrapStyle { | ||
| 457 | + fn parse(value: &str) -> WrapStyle { | ||
| 458 | + match value.parse::<u8>() { | ||
| 459 | + Ok(2) => WrapStyle::Manual, | ||
| 460 | + _ => WrapStyle::Auto, | ||
| 461 | + } | ||
| 462 | + } | ||
| 463 | +} | ||
| 464 | + | ||
| 448 | fn push_text( | 465 | fn push_text( |
| 449 | out: &mut Vec<Segment>, | 466 | out: &mut Vec<Segment>, |
| 450 | run: &mut String, | 467 | run: &mut String, |
| 451 | text: &str, | 468 | text: &str, |
| 452 | state: &Overrides, | 469 | state: &Overrides, |
| 453 | - wrap_style: u8, | 470 | + wrap_style: WrapStyle, |
| 454 | ) { | 471 | ) { |
| 455 | let mut chars = text.chars().peekable(); | 472 | let mut chars = text.chars().peekable(); |
| 456 | while let Some(c) = chars.next() { | 473 | while let Some(c) = chars.next() { |
| @@ -466,7 +483,7 @@ fn push_text( | |||
| 466 | flush(out, run, state); | 483 | flush(out, run, state); |
| 467 | out.push(Segment::LineBreak); | 484 | out.push(Segment::LineBreak); |
| 468 | } | 485 | } |
| 469 | - Some('n') if wrap_style == 2 => { | 486 | + Some('n') if wrap_style == WrapStyle::Manual => { |
| 470 | flush(out, run, state); | 487 | flush(out, run, state); |
| 471 | out.push(Segment::LineBreak); | 488 | out.push(Segment::LineBreak); |
| 472 | } | 489 | } |
crates/ec-cc/src/extract.rsmodified+42 -36
| @@ -24,6 +24,25 @@ struct PictureCc { | |||
| 24 | triplets: Vec<CcTriplet>, | 24 | triplets: Vec<CcTriplet>, |
| 25 | } | 25 | } |
| 26 | 26 | ||
| 27 | +/// The previous reference picture's POC MSB and LSB, for the wrap-around rule. | ||
| 28 | +#[derive(Clone, Copy, Debug, Default)] | ||
| 29 | +struct Poc { | ||
| 30 | + msb: i64, | ||
| 31 | + lsb: i64, | ||
| 32 | +} | ||
| 33 | + | ||
| 34 | +impl Poc { | ||
| 35 | + fn msb_for(&self, lsb: i64, max: i64) -> i64 { | ||
| 36 | + if lsb < self.lsb && self.lsb - lsb >= max / 2 { | ||
| 37 | + self.msb + max | ||
| 38 | + } else if lsb > self.lsb && lsb - self.lsb > max / 2 { | ||
| 39 | + self.msb - max | ||
| 40 | + } else { | ||
| 41 | + self.msb | ||
| 42 | + } | ||
| 43 | + } | ||
| 44 | +} | ||
| 45 | + | ||
| 27 | /// Captions of one presentation-order picture. | 46 | /// Captions of one presentation-order picture. |
| 28 | #[derive(Clone, Debug, PartialEq, Eq)] | 47 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 29 | pub struct TimedCc { | 48 | pub struct TimedCc { |
| @@ -131,6 +150,15 @@ fn for_each_nal(data: &[u8], fmt: NalFormat, mut f: impl FnMut(&[u8])) { | |||
| 131 | } | 150 | } |
| 132 | } | 151 | } |
| 133 | 152 | ||
| 153 | +/// A parameter set in `avcC` or `hvcC`: a big-endian u16 length, then the NAL. | ||
| 154 | +/// Returns the NAL and the offset just past it. | ||
| 155 | +fn u16_prefixed(data: &[u8], at: usize) -> Option<(&[u8], usize)> { | ||
| 156 | + let len = data.get(at..at + 2)?; | ||
| 157 | + let len = u16::from_be_bytes([len[0], len[1]]) as usize; | ||
| 158 | + let nal = data.get(at + 2..at + 2 + len)?; | ||
| 159 | + Some((nal, at + 2 + len)) | ||
| 160 | +} | ||
| 161 | + | ||
| 134 | fn unescape(p: &[u8]) -> Vec<u8> { | 162 | fn unescape(p: &[u8]) -> Vec<u8> { |
| 135 | let mut out = Vec::with_capacity(p.len()); | 163 | let mut out = Vec::with_capacity(p.len()); |
| 136 | let mut zeros = 0; | 164 | let mut zeros = 0; |
| @@ -185,8 +213,7 @@ pub struct H264Extractor { | |||
| 185 | pending: Vec<CcTriplet>, | 213 | pending: Vec<CcTriplet>, |
| 186 | stream: CcStream, | 214 | stream: CcStream, |
| 187 | epoch: u32, | 215 | epoch: u32, |
| 188 | - prev_msb: i64, | 216 | + poc: Poc, |
| 189 | - prev_lsb: i64, | ||
| 190 | counter: i64, | 217 | counter: i64, |
| 191 | last_top_field: Option<(u32, u32)>, | 218 | last_top_field: Option<(u32, u32)>, |
| 192 | } | 219 | } |
| @@ -201,8 +228,7 @@ impl H264Extractor { | |||
| 201 | pending: Vec::new(), | 228 | pending: Vec::new(), |
| 202 | stream: CcStream::default(), | 229 | stream: CcStream::default(), |
| 203 | epoch: 0, | 230 | epoch: 0, |
| 204 | - prev_msb: 0, | 231 | + poc: Poc::default(), |
| 205 | - prev_lsb: 0, | ||
| 206 | counter: 0, | 232 | counter: 0, |
| 207 | last_top_field: None, | 233 | last_top_field: None, |
| 208 | } | 234 | } |
| @@ -225,13 +251,11 @@ impl H264Extractor { | |||
| 225 | }; | 251 | }; |
| 226 | i += 1; | 252 | i += 1; |
| 227 | for _ in 0..count { | 253 | for _ in 0..count { |
| 228 | - let Some(l) = avcc.get(i..i + 2) else { return }; | 254 | + let Some((n, next)) = u16_prefixed(avcc, i) else { |
| 229 | - let l = u16::from_be_bytes([l[0], l[1]]) as usize; | ||
| 230 | - let Some(n) = avcc.get(i + 2..i + 2 + l) else { | ||
| 231 | return; | 255 | return; |
| 232 | }; | 256 | }; |
| 233 | sets.push(n); | 257 | sets.push(n); |
| 234 | - i += 2 + l; | 258 | + i = next; |
| 235 | } | 259 | } |
| 236 | } | 260 | } |
| 237 | for n in sets { | 261 | for n in sets { |
| @@ -309,23 +333,15 @@ impl H264Extractor { | |||
| 309 | }; | 333 | }; |
| 310 | if h.unit_type == H264Nal::SliceIdr { | 334 | if h.unit_type == H264Nal::SliceIdr { |
| 311 | self.epoch += 1; | 335 | self.epoch += 1; |
| 312 | - self.prev_msb = 0; | 336 | + self.poc = Poc::default(); |
| 313 | - self.prev_lsb = 0; | ||
| 314 | self.counter = 0; | 337 | self.counter = 0; |
| 315 | } | 338 | } |
| 316 | let order = if sps.pic_order_cnt_type == 0 { | 339 | let order = if sps.pic_order_cnt_type == 0 { |
| 317 | let max = 1i64 << sps.log2_max_pic_order_cnt_lsb; | 340 | let max = 1i64 << sps.log2_max_pic_order_cnt_lsb; |
| 318 | let lsb = sh.pic_order_cnt_lsb as i64; | 341 | let lsb = sh.pic_order_cnt_lsb as i64; |
| 319 | - let msb = if lsb < self.prev_lsb && self.prev_lsb - lsb >= max / 2 { | 342 | + let msb = self.poc.msb_for(lsb, max); |
| 320 | - self.prev_msb + max | ||
| 321 | - } else if lsb > self.prev_lsb && lsb - self.prev_lsb > max / 2 { | ||
| 322 | - self.prev_msb - max | ||
| 323 | - } else { | ||
| 324 | - self.prev_msb | ||
| 325 | - }; | ||
| 326 | if h.ref_idc != 0 { | 343 | if h.ref_idc != 0 { |
| 327 | - self.prev_msb = msb; | 344 | + self.poc = Poc { msb, lsb }; |
| 328 | - self.prev_lsb = lsb; | ||
| 329 | } | 345 | } |
| 330 | msb + lsb | 346 | msb + lsb |
| 331 | } else { | 347 | } else { |
| @@ -370,8 +386,7 @@ pub struct HevcExtractor { | |||
| 370 | pending: Vec<CcTriplet>, | 386 | pending: Vec<CcTriplet>, |
| 371 | stream: CcStream, | 387 | stream: CcStream, |
| 372 | epoch: u32, | 388 | epoch: u32, |
| 373 | - prev_msb: i64, | 389 | + poc: Poc, |
| 374 | - prev_lsb: i64, | ||
| 375 | } | 390 | } |
| 376 | 391 | ||
| 377 | impl HevcExtractor { | 392 | impl HevcExtractor { |
| @@ -384,8 +399,7 @@ impl HevcExtractor { | |||
| 384 | pending: Vec::new(), | 399 | pending: Vec::new(), |
| 385 | stream: CcStream::default(), | 400 | stream: CcStream::default(), |
| 386 | epoch: 0, | 401 | epoch: 0, |
| 387 | - prev_msb: 0, | 402 | + poc: Poc::default(), |
| 388 | - prev_lsb: 0, | ||
| 389 | } | 403 | } |
| 390 | } | 404 | } |
| 391 | 405 | ||
| @@ -401,13 +415,11 @@ impl HevcExtractor { | |||
| 401 | let count = u16::from_be_bytes([h[1], h[2]]) as usize; | 415 | let count = u16::from_be_bytes([h[1], h[2]]) as usize; |
| 402 | i += 3; | 416 | i += 3; |
| 403 | for _ in 0..count { | 417 | for _ in 0..count { |
| 404 | - let Some(l) = hvcc.get(i..i + 2) else { return }; | 418 | + let Some((n, next)) = u16_prefixed(hvcc, i) else { |
| 405 | - let l = u16::from_be_bytes([l[0], l[1]]) as usize; | ||
| 406 | - let Some(n) = hvcc.get(i + 2..i + 2 + l) else { | ||
| 407 | return; | 419 | return; |
| 408 | }; | 420 | }; |
| 409 | self.nal(n, None); | 421 | self.nal(n, None); |
| 410 | - i += 2 + l; | 422 | + i = next; |
| 411 | } | 423 | } |
| 412 | } | 424 | } |
| 413 | } | 425 | } |
| @@ -483,25 +495,19 @@ impl HevcExtractor { | |||
| 483 | })(); | 495 | })(); |
| 484 | if idr || bla { | 496 | if idr || bla { |
| 485 | self.epoch += 1; | 497 | self.epoch += 1; |
| 486 | - self.prev_msb = 0; | 498 | + self.poc = Poc::default(); |
| 487 | - self.prev_lsb = 0; | ||
| 488 | } | 499 | } |
| 489 | let order = match parsed { | 500 | let order = match parsed { |
| 490 | Some((lsb, bits)) => { | 501 | Some((lsb, bits)) => { |
| 491 | let max = 1i64 << bits; | 502 | let max = 1i64 << bits; |
| 492 | let msb = if idr || bla { | 503 | let msb = if idr || bla { |
| 493 | 0 | 504 | 0 |
| 494 | - } else if lsb < self.prev_lsb && self.prev_lsb - lsb >= max / 2 { | ||
| 495 | - self.prev_msb + max | ||
| 496 | - } else if lsb > self.prev_lsb && lsb - self.prev_lsb > max / 2 { | ||
| 497 | - self.prev_msb - max | ||
| 498 | } else { | 505 | } else { |
| 499 | - self.prev_msb | 506 | + self.poc.msb_for(lsb, max) |
| 500 | }; | 507 | }; |
| 501 | // Sub-layer non-reference pictures (types 0,2,4,...,14 even) do not update prevTid0. | 508 | // Sub-layer non-reference pictures (types 0,2,4,...,14 even) do not update prevTid0. |
| 502 | if ty >= 16 || ty % 2 == 1 { | 509 | if ty >= 16 || ty % 2 == 1 { |
| 503 | - self.prev_msb = msb; | 510 | + self.poc = Poc { msb, lsb }; |
| 504 | - self.prev_lsb = lsb; | ||
| 505 | } | 511 | } |
| 506 | msb + lsb | 512 | msb + lsb |
| 507 | } | 513 | } |
crates/ec-dvbsub/src/lib.rsmodified+52 -26
| @@ -43,14 +43,40 @@ pub struct DvbCue { | |||
| 43 | pub timeout_ms: u32, | 43 | pub timeout_ms: u32, |
| 44 | /// No region is shown: this set clears the screen. | 44 | /// No region is shown: this set clears the screen. |
| 45 | pub empty: bool, | 45 | pub empty: bool, |
| 46 | - /// Page state: 0 normal case, 1 acquisition point, 2 mode change. | 46 | + /// Page state: normal case, acquisition point, mode change. |
| 47 | - pub page_state: u8, | 47 | + pub page_state: PageState, |
| 48 | /// The regions shown: x, y, width, height. | 48 | /// The regions shown: x, y, width, height. |
| 49 | pub regions: Vec<(u32, u32, u32, u32)>, | 49 | pub regions: Vec<(u32, u32, u32, u32)>, |
| 50 | } | 50 | } |
| 51 | 51 | ||
| 52 | type Rgba = [u8; 4]; | 52 | type Rgba = [u8; 4]; |
| 53 | 53 | ||
| 54 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] | ||
| 55 | +pub enum PageState { | ||
| 56 | + Normal, | ||
| 57 | + Acquisition, | ||
| 58 | + ModeChange, | ||
| 59 | + Reserved, | ||
| 60 | +} | ||
| 61 | + | ||
| 62 | +impl PageState { | ||
| 63 | + fn from_bits(v: u8) -> PageState { | ||
| 64 | + match v { | ||
| 65 | + 0 => PageState::Normal, | ||
| 66 | + 1 => PageState::Acquisition, | ||
| 67 | + 2 => PageState::ModeChange, | ||
| 68 | + _ => PageState::Reserved, | ||
| 69 | + } | ||
| 70 | + } | ||
| 71 | +} | ||
| 72 | + | ||
| 73 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] | ||
| 74 | +enum Depth { | ||
| 75 | + Bits2, | ||
| 76 | + Bits4, | ||
| 77 | + Bits8, | ||
| 78 | +} | ||
| 79 | + | ||
| 54 | struct Clut { | 80 | struct Clut { |
| 55 | c2: [Rgba; 4], | 81 | c2: [Rgba; 4], |
| 56 | c4: [Rgba; 16], | 82 | c4: [Rgba; 16], |
| @@ -60,7 +86,7 @@ struct Clut { | |||
| 60 | struct Region { | 86 | struct Region { |
| 61 | width: u32, | 87 | width: u32, |
| 62 | height: u32, | 88 | height: u32, |
| 63 | - depth: u8, // bits: 2, 4, 8 | 89 | + depth: Depth, |
| 64 | clut: u8, | 90 | clut: u8, |
| 65 | pix: Vec<u8>, | 91 | pix: Vec<u8>, |
| 66 | objects: Vec<(u16, u32, u32)>, // id, x, y | 92 | objects: Vec<(u16, u32, u32)>, // id, x, y |
| @@ -73,7 +99,7 @@ pub struct DvbSubDecoder { | |||
| 73 | cluts: HashMap<u8, Clut>, | 99 | cluts: HashMap<u8, Clut>, |
| 74 | page: Vec<(u8, u32, u32)>, | 100 | page: Vec<(u8, u32, u32)>, |
| 75 | timeout: u8, | 101 | timeout: u8, |
| 76 | - page_state: u8, | 102 | + page_state: PageState, |
| 77 | display: (u32, u32), | 103 | display: (u32, u32), |
| 78 | ready: VecDeque<DvbCue>, | 104 | ready: VecDeque<DvbCue>, |
| 79 | pts: Option<Timestamp>, | 105 | pts: Option<Timestamp>, |
| @@ -180,12 +206,12 @@ impl Bits<'_> { | |||
| 180 | 206 | ||
| 181 | /// One pixel-data sub-block's code string, calling `put(count, code)` for each run. Returns the | 207 | /// One pixel-data sub-block's code string, calling `put(count, code)` for each run. Returns the |
| 182 | /// bytes the string took (through its end code and byte alignment; all of `d` if truncated). | 208 | /// bytes the string took (through its end code and byte alignment; all of `d` if truncated). |
| 183 | -fn code_string(d: &[u8], depth: u8, mut put: impl FnMut(usize, u8)) -> usize { | 209 | +fn code_string(d: &[u8], depth: Depth, mut put: impl FnMut(usize, u8)) -> usize { |
| 184 | let mut b = Bits { d, pos: 0 }; | 210 | let mut b = Bits { d, pos: 0 }; |
| 185 | loop { | 211 | loop { |
| 186 | let r = (|| -> Option<Option<(usize, u8)>> { | 212 | let r = (|| -> Option<Option<(usize, u8)>> { |
| 187 | Some(match depth { | 213 | Some(match depth { |
| 188 | - 2 => { | 214 | + Depth::Bits2 => { |
| 189 | let v = b.get(2)?; | 215 | let v = b.get(2)?; |
| 190 | if v != 0 { | 216 | if v != 0 { |
| 191 | Some((1, v as u8)) | 217 | Some((1, v as u8)) |
| @@ -209,7 +235,7 @@ fn code_string(d: &[u8], depth: u8, mut put: impl FnMut(usize, u8)) -> usize { | |||
| 209 | } | 235 | } |
| 210 | } | 236 | } |
| 211 | } | 237 | } |
| 212 | - 4 => { | 238 | + Depth::Bits4 => { |
| 213 | let v = b.get(4)?; | 239 | let v = b.get(4)?; |
| 214 | if v != 0 { | 240 | if v != 0 { |
| 215 | Some((1, v as u8)) | 241 | Some((1, v as u8)) |
| @@ -234,7 +260,7 @@ fn code_string(d: &[u8], depth: u8, mut put: impl FnMut(usize, u8)) -> usize { | |||
| 234 | } | 260 | } |
| 235 | } | 261 | } |
| 236 | } | 262 | } |
| 237 | - _ => { | 263 | + Depth::Bits8 => { |
| 238 | let v = b.get(8)?; | 264 | let v = b.get(8)?; |
| 239 | if v != 0 { | 265 | if v != 0 { |
| 240 | Some((1, v as u8)) | 266 | Some((1, v as u8)) |
| @@ -268,7 +294,7 @@ impl DvbSubDecoder { | |||
| 268 | cluts: HashMap::new(), | 294 | cluts: HashMap::new(), |
| 269 | page: Vec::new(), | 295 | page: Vec::new(), |
| 270 | timeout: 0, | 296 | timeout: 0, |
| 271 | - page_state: 0, | 297 | + page_state: PageState::Normal, |
| 272 | display: (720, 576), | 298 | display: (720, 576), |
| 273 | ready: VecDeque::new(), | 299 | ready: VecDeque::new(), |
| 274 | pts: None, | 300 | pts: None, |
| @@ -328,8 +354,8 @@ impl DvbSubDecoder { | |||
| 328 | return Err(Error::corrupt("dvbsub: short page composition")); | 354 | return Err(Error::corrupt("dvbsub: short page composition")); |
| 329 | } | 355 | } |
| 330 | self.timeout = b[0]; | 356 | self.timeout = b[0]; |
| 331 | - self.page_state = (b[1] >> 2) & 3; | 357 | + self.page_state = PageState::from_bits((b[1] >> 2) & 3); |
| 332 | - if self.page_state != 0 { | 358 | + if self.page_state != PageState::Normal { |
| 333 | // Acquisition point / mode change: nothing from before is kept. | 359 | // Acquisition point / mode change: nothing from before is kept. |
| 334 | self.regions.clear(); | 360 | self.regions.clear(); |
| 335 | self.cluts.clear(); | 361 | self.cluts.clear(); |
| @@ -357,16 +383,16 @@ impl DvbSubDecoder { | |||
| 357 | return Err(Error::corrupt("dvbsub: region size out of range")); | 383 | return Err(Error::corrupt("dvbsub: region size out of range")); |
| 358 | } | 384 | } |
| 359 | let depth = match (b[6] >> 2) & 7 { | 385 | let depth = match (b[6] >> 2) & 7 { |
| 360 | - 1 => 2, | 386 | + 1 => Depth::Bits2, |
| 361 | - 2 => 4, | 387 | + 2 => Depth::Bits4, |
| 362 | - 3 => 8, | 388 | + 3 => Depth::Bits8, |
| 363 | _ => return Err(Error::corrupt("dvbsub: bad region depth")), | 389 | _ => return Err(Error::corrupt("dvbsub: bad region depth")), |
| 364 | }; | 390 | }; |
| 365 | let clut = b[7]; | 391 | let clut = b[7]; |
| 366 | let bg = match depth { | 392 | let bg = match depth { |
| 367 | - 2 => (b[9] >> 2) & 3, | 393 | + Depth::Bits2 => (b[9] >> 2) & 3, |
| 368 | - 4 => b[9] >> 4, | 394 | + Depth::Bits4 => b[9] >> 4, |
| 369 | - _ => b[8], | 395 | + Depth::Bits8 => b[8], |
| 370 | }; | 396 | }; |
| 371 | let mut pix = vec![0u8; (w * h) as usize]; | 397 | let mut pix = vec![0u8; (w * h) as usize]; |
| 372 | if fill { | 398 | if fill { |
| @@ -486,9 +512,9 @@ impl DvbSubDecoder { | |||
| 486 | } | 512 | } |
| 487 | let code = usize::from(r.pix[(row * r.width + col) as usize]); | 513 | let code = usize::from(r.pix[(row * r.width + col) as usize]); |
| 488 | let c = match r.depth { | 514 | let c = match r.depth { |
| 489 | - 2 => clut.c2[code & 3], | 515 | + Depth::Bits2 => clut.c2[code & 3], |
| 490 | - 4 => clut.c4[code & 15], | 516 | + Depth::Bits4 => clut.c4[code & 15], |
| 491 | - _ => clut.c8[code], | 517 | + Depth::Bits8 => clut.c8[code], |
| 492 | }; | 518 | }; |
| 493 | let o = ty as usize * stride + tx as usize * 4; | 519 | let o = ty as usize * stride + tx as usize * 4; |
| 494 | px[o..o + 4].copy_from_slice(&c); | 520 | px[o..o + 4].copy_from_slice(&c); |
| @@ -528,17 +554,17 @@ fn paint_field(region: &mut Region, data: &[u8], x: u32, y: u32, field: u32) { | |||
| 528 | match kind { | 554 | match kind { |
| 529 | 0x10..=0x12 => { | 555 | 0x10..=0x12 => { |
| 530 | let src_depth = match kind { | 556 | let src_depth = match kind { |
| 531 | - 0x10 => 2, | 557 | + 0x10 => Depth::Bits2, |
| 532 | - 0x11 => 4, | 558 | + 0x11 => Depth::Bits4, |
| 533 | - _ => 8, | 559 | + _ => Depth::Bits8, |
| 534 | }; | 560 | }; |
| 535 | let depth = region.depth; | 561 | let depth = region.depth; |
| 536 | let (m24c, m28c, m48c) = (m24, m28, m48); | 562 | let (m24c, m28c, m48c) = (m24, m28, m48); |
| 537 | let mapped = |c: u8| -> u8 { | 563 | let mapped = |c: u8| -> u8 { |
| 538 | match (src_depth, depth) { | 564 | match (src_depth, depth) { |
| 539 | - (2, 4) => m24c[usize::from(c & 3)], | 565 | + (Depth::Bits2, Depth::Bits4) => m24c[usize::from(c & 3)], |
| 540 | - (2, 8) => m28c[usize::from(c & 3)], | 566 | + (Depth::Bits2, Depth::Bits8) => m28c[usize::from(c & 3)], |
| 541 | - (4, 8) => m48c[usize::from(c & 15)], | 567 | + (Depth::Bits4, Depth::Bits8) => m48c[usize::from(c & 15)], |
| 542 | _ => c, | 568 | _ => c, |
| 543 | } | 569 | } |
| 544 | }; | 570 | }; |
crates/ec-image/src/bmp.rsmodified+45 -29
| @@ -26,7 +26,7 @@ struct Header { | |||
| 26 | /// Rows are stored bottom-up unless the height was negative. | 26 | /// Rows are stored bottom-up unless the height was negative. |
| 27 | top_down: bool, | 27 | top_down: bool, |
| 28 | bits: u16, | 28 | bits: u16, |
| 29 | - compression: u32, | 29 | + compression: Compression, |
| 30 | /// Palette entries actually present in the file. | 30 | /// Palette entries actually present in the file. |
| 31 | colours: usize, | 31 | colours: usize, |
| 32 | /// Bytes per palette entry: 4 in a Windows header, 3 in an OS/2 one. | 32 | /// Bytes per palette entry: 4 in a Windows header, 3 in an OS/2 one. |
| @@ -37,13 +37,31 @@ struct Header { | |||
| 37 | masks: [u32; 4], | 37 | masks: [u32; 4], |
| 38 | } | 38 | } |
| 39 | 39 | ||
| 40 | -const BI_RGB: u32 = 0; | 40 | +#[derive(Clone, Copy, PartialEq, Eq)] |
| 41 | -const BI_RLE8: u32 = 1; | 41 | +enum Compression { |
| 42 | -const BI_RLE4: u32 = 2; | 42 | + Rgb, |
| 43 | -const BI_BITFIELDS: u32 = 3; | 43 | + Rle8, |
| 44 | -const BI_JPEG: u32 = 4; | 44 | + Rle4, |
| 45 | -const BI_PNG: u32 = 5; | 45 | + Bitfields, |
| 46 | -const BI_ALPHABITFIELDS: u32 = 6; | 46 | + Jpeg, |
| 47 | + Png, | ||
| 48 | + AlphaBitfields, | ||
| 49 | +} | ||
| 50 | + | ||
| 51 | +impl Compression { | ||
| 52 | + fn from_raw(raw: u32) -> Option<Self> { | ||
| 53 | + Some(match raw { | ||
| 54 | + 0 => Compression::Rgb, | ||
| 55 | + 1 => Compression::Rle8, | ||
| 56 | + 2 => Compression::Rle4, | ||
| 57 | + 3 => Compression::Bitfields, | ||
| 58 | + 4 => Compression::Jpeg, | ||
| 59 | + 5 => Compression::Png, | ||
| 60 | + 6 => Compression::AlphaBitfields, | ||
| 61 | + _ => return None, | ||
| 62 | + }) | ||
| 63 | + } | ||
| 64 | +} | ||
| 47 | 65 | ||
| 48 | fn u16_at(data: &[u8], at: usize) -> Result<u16> { | 66 | fn u16_at(data: &[u8], at: usize) -> Result<u16> { |
| 49 | data.get(at..at + 2) | 67 | data.get(at..at + 2) |
| @@ -72,7 +90,7 @@ fn header(data: &[u8]) -> Result<Header> { | |||
| 72 | u32::from(u16_at(data, 20)?), | 90 | u32::from(u16_at(data, 20)?), |
| 73 | false, | 91 | false, |
| 74 | u16_at(data, 24)?, | 92 | u16_at(data, 24)?, |
| 75 | - BI_RGB, | 93 | + 0, |
| 76 | 0usize, | 94 | 0usize, |
| 77 | ) | 95 | ) |
| 78 | } else if (16..40).contains(&info_size) { | 96 | } else if (16..40).contains(&info_size) { |
| @@ -84,7 +102,7 @@ fn header(data: &[u8]) -> Result<Header> { | |||
| 84 | raw_height.unsigned_abs(), | 102 | raw_height.unsigned_abs(), |
| 85 | raw_height < 0, | 103 | raw_height < 0, |
| 86 | u16_at(data, 28)?, | 104 | u16_at(data, 28)?, |
| 87 | - BI_RGB, | 105 | + 0, |
| 88 | 0usize, | 106 | 0usize, |
| 89 | ) | 107 | ) |
| 90 | } else if info_size >= 40 { | 108 | } else if info_size >= 40 { |
| @@ -106,16 +124,11 @@ fn header(data: &[u8]) -> Result<Header> { | |||
| 106 | if !matches!(bits, 1 | 2 | 4 | 8 | 16 | 24 | 32) { | 124 | if !matches!(bits, 1 | 2 | 4 | 8 | 16 | 24 | 32) { |
| 107 | return Err(Error::corrupt(format!("BMP: {bits} bits per pixel"))); | 125 | return Err(Error::corrupt(format!("BMP: {bits} bits per pixel"))); |
| 108 | } | 126 | } |
| 109 | - match compression { | 127 | + let compression = Compression::from_raw(compression) |
| 110 | - // Handled before the header is read; reaching here means the caller | 128 | + .ok_or_else(|| Error::corrupt(format!("BMP: compression {compression}")))?; |
| 111 | - // asked for the fields of a file that has none of them. | 129 | + if (compression == Compression::Rle8 && bits != 8) |
| 112 | - BI_JPEG | BI_PNG => {} | 130 | + || (compression == Compression::Rle4 && bits != 4) |
| 113 | - BI_RGB | BI_RLE8 | BI_RLE4 | BI_BITFIELDS | BI_ALPHABITFIELDS => {} | 131 | + { |
| 114 | - other => { | ||
| 115 | - return Err(Error::corrupt(format!("BMP: compression {other}"))); | ||
| 116 | - } | ||
| 117 | - } | ||
| 118 | - if (compression == BI_RLE8 && bits != 8) || (compression == BI_RLE4 && bits != 4) { | ||
| 119 | return Err(Error::corrupt(format!( | 132 | return Err(Error::corrupt(format!( |
| 120 | "BMP: run-length encoding with {bits} bits per pixel" | 133 | "BMP: run-length encoding with {bits} bits per pixel" |
| 121 | ))); | 134 | ))); |
| @@ -123,18 +136,21 @@ fn header(data: &[u8]) -> Result<Header> { | |||
| 123 | 136 | ||
| 124 | // Under BI_BITFIELDS the masks follow the 40-byte header in a v3 file and | 137 | // Under BI_BITFIELDS the masks follow the 40-byte header in a v3 file and |
| 125 | // live inside a v4/v5 one; either way they start at offset 54. | 138 | // live inside a v4/v5 one; either way they start at offset 54. |
| 126 | - let bitfields = compression == BI_BITFIELDS || compression == BI_ALPHABITFIELDS; | 139 | + let bitfields = matches!( |
| 140 | + compression, | ||
| 141 | + Compression::Bitfields | Compression::AlphaBitfields | ||
| 142 | + ); | ||
| 127 | let mut masks = [0u32; 4]; | 143 | let mut masks = [0u32; 4]; |
| 128 | let mut palette_at = 14 + info_size; | 144 | let mut palette_at = 14 + info_size; |
| 129 | if bitfields && info_size == 40 { | 145 | if bitfields && info_size == 40 { |
| 130 | for (i, mask) in masks.iter_mut().enumerate().take(3) { | 146 | for (i, mask) in masks.iter_mut().enumerate().take(3) { |
| 131 | *mask = u32_at(data, 54 + i * 4)?; | 147 | *mask = u32_at(data, 54 + i * 4)?; |
| 132 | } | 148 | } |
| 133 | - if compression == BI_ALPHABITFIELDS { | 149 | + if compression == Compression::AlphaBitfields { |
| 134 | masks[3] = u32_at(data, 66)?; | 150 | masks[3] = u32_at(data, 66)?; |
| 135 | } | 151 | } |
| 136 | palette_at += 12 | 152 | palette_at += 12 |
| 137 | - + if compression == BI_ALPHABITFIELDS { | 153 | + + if compression == Compression::AlphaBitfields { |
| 138 | 4 | 154 | 4 |
| 139 | } else { | 155 | } else { |
| 140 | 0 | 156 | 0 |
| @@ -216,9 +232,9 @@ fn embedded(data: &[u8]) -> Result<Option<(ImageFormat, &[u8])>> { | |||
| 216 | if !data.starts_with(b"BM") || u32_at(data, 14)? < 40 { | 232 | if !data.starts_with(b"BM") || u32_at(data, 14)? < 40 { |
| 217 | return Ok(None); | 233 | return Ok(None); |
| 218 | } | 234 | } |
| 219 | - let format = match u32_at(data, 30)? { | 235 | + let format = match Compression::from_raw(u32_at(data, 30)?) { |
| 220 | - BI_PNG => ImageFormat::Png, | 236 | + Some(Compression::Png) => ImageFormat::Png, |
| 221 | - BI_JPEG => ImageFormat::Jpeg, | 237 | + Some(Compression::Jpeg) => ImageFormat::Jpeg, |
| 222 | _ => return Ok(None), | 238 | _ => return Ok(None), |
| 223 | }; | 239 | }; |
| 224 | let offset = u32_at(data, 10)? as usize; | 240 | let offset = u32_at(data, 10)? as usize; |
| @@ -289,7 +305,7 @@ pub fn decode(data: &[u8], limits: Limits) -> Result<Image> { | |||
| 289 | // Indices first, then colour: every form below produces either palette | 305 | // Indices first, then colour: every form below produces either palette |
| 290 | // indices or direct samples, and the bottom-up flip is the same either way. | 306 | // indices or direct samples, and the bottom-up flip is the same either way. |
| 291 | let has_alpha = header.masks[3] != 0 | 307 | let has_alpha = header.masks[3] != 0 |
| 292 | - || (header.bits == 32 && header.compression == BI_RGB && palette.is_empty()); | 308 | + || (header.bits == 32 && header.compression == Compression::Rgb && palette.is_empty()); |
| 293 | let channels = if has_alpha { 4 } else { 3 }; | 309 | let channels = if has_alpha { 4 } else { 3 }; |
| 294 | // The output is checked before anything else sizes from the header; the RLE index buffer | 310 | // The output is checked before anything else sizes from the header; the RLE index buffer |
| 295 | // below is width * height, which this bound already covers. | 311 | // below is width * height, which this bound already covers. |
| @@ -298,7 +314,7 @@ pub fn decode(data: &[u8], limits: Limits) -> Result<Image> { | |||
| 298 | let row_of = |y: usize| if header.top_down { y } else { height - 1 - y }; | 314 | let row_of = |y: usize| if header.top_down { y } else { height - 1 - y }; |
| 299 | 315 | ||
| 300 | match header.compression { | 316 | match header.compression { |
| 301 | - BI_RLE8 | BI_RLE4 => { | 317 | + Compression::Rle8 | Compression::Rle4 => { |
| 302 | let indices = rle(body, &header)?; | 318 | let indices = rle(body, &header)?; |
| 303 | for y in 0..height { | 319 | for y in 0..height { |
| 304 | for x in 0..width { | 320 | for x in 0..width { |
| @@ -427,7 +443,7 @@ fn rle(body: &[u8], header: &Header) -> Result<Vec<u8>> { | |||
| 427 | let mut out = vec![0u8; width * height]; | 443 | let mut out = vec![0u8; width * height]; |
| 428 | let (mut x, mut y) = (0usize, 0usize); | 444 | let (mut x, mut y) = (0usize, 0usize); |
| 429 | let mut at = 0usize; | 445 | let mut at = 0usize; |
| 430 | - let four_bit = header.compression == BI_RLE4; | 446 | + let four_bit = header.compression == Compression::Rle4; |
| 431 | 447 | ||
| 432 | let put = |out: &mut Vec<u8>, x: &mut usize, y: usize, index: u8| { | 448 | let put = |out: &mut Vec<u8>, x: &mut usize, y: usize, index: u8| { |
| 433 | if *x < width && y < height { | 449 | if *x < width && y < height { |
crates/ec-image/src/bytes.rsadded+17 -0
| @@ -0,0 +1,17 @@ | |||
| 1 | +pub(crate) fn u16_at(data: &[u8], at: usize, big_endian: bool) -> Option<u16> { | ||
| 2 | + let b: [u8; 2] = data.get(at..at + 2)?.try_into().ok()?; | ||
| 3 | + Some(if big_endian { | ||
| 4 | + u16::from_be_bytes(b) | ||
| 5 | + } else { | ||
| 6 | + u16::from_le_bytes(b) | ||
| 7 | + }) | ||
| 8 | +} | ||
| 9 | + | ||
| 10 | +pub(crate) fn u32_at(data: &[u8], at: usize, big_endian: bool) -> Option<u32> { | ||
| 11 | + let b: [u8; 4] = data.get(at..at + 4)?.try_into().ok()?; | ||
| 12 | + Some(if big_endian { | ||
| 13 | + u32::from_be_bytes(b) | ||
| 14 | + } else { | ||
| 15 | + u32::from_le_bytes(b) | ||
| 16 | + }) | ||
| 17 | +} | ||
crates/ec-image/src/exif.rsmodified+5 -19
| @@ -8,27 +8,13 @@ | |||
| 8 | /// Orientation 1..=8 from a bare TIFF block, or `None` if the block is not a | 8 | /// Orientation 1..=8 from a bare TIFF block, or `None` if the block is not a |
| 9 | /// readable TIFF or carries no valid orientation entry. | 9 | /// readable TIFF or carries no valid orientation entry. |
| 10 | pub(crate) fn tiff_orientation(tiff: &[u8]) -> Option<u8> { | 10 | pub(crate) fn tiff_orientation(tiff: &[u8]) -> Option<u8> { |
| 11 | - let little = match tiff.get(..2)? { | 11 | + let big_endian = match tiff.get(..2)? { |
| 12 | - b"II" => true, | 12 | + b"II" => false, |
| 13 | - b"MM" => false, | 13 | + b"MM" => true, |
| 14 | _ => return None, | 14 | _ => return None, |
| 15 | }; | 15 | }; |
| 16 | - let u16_at = |at: usize| -> Option<u16> { | 16 | + let u16_at = |at: usize| crate::bytes::u16_at(tiff, at, big_endian); |
| 17 | - let b = tiff.get(at..at + 2)?; | 17 | + let u32_at = |at: usize| crate::bytes::u32_at(tiff, at, big_endian); |
| 18 | - Some(if little { | ||
| 19 | - u16::from_le_bytes([b[0], b[1]]) | ||
| 20 | - } else { | ||
| 21 | - u16::from_be_bytes([b[0], b[1]]) | ||
| 22 | - }) | ||
| 23 | - }; | ||
| 24 | - let u32_at = |at: usize| -> Option<u32> { | ||
| 25 | - let b = tiff.get(at..at + 4)?; | ||
| 26 | - Some(if little { | ||
| 27 | - u32::from_le_bytes([b[0], b[1], b[2], b[3]]) | ||
| 28 | - } else { | ||
| 29 | - u32::from_be_bytes([b[0], b[1], b[2], b[3]]) | ||
| 30 | - }) | ||
| 31 | - }; | ||
| 32 | if u16_at(2)? != 42 { | 18 | if u16_at(2)? != 42 { |
| 33 | return None; | 19 | return None; |
| 34 | } | 20 | } |
crates/ec-image/src/lib.rsmodified+1 -0
| @@ -42,6 +42,7 @@ pub mod png_encode; | |||
| 42 | pub mod transform; | 42 | pub mod transform; |
| 43 | pub mod webp; | 43 | pub mod webp; |
| 44 | 44 | ||
| 45 | +mod bytes; | ||
| 45 | mod exif; | 46 | mod exif; |
| 46 | mod probe; | 47 | mod probe; |
| 47 | 48 | ||
crates/ec-image/src/tiff.rsmodified+24 -37
| @@ -39,11 +39,13 @@ const TAG_TILE_COUNTS: u16 = 325; | |||
| 39 | const TAG_EXTRA_SAMPLES: u16 = 338; | 39 | const TAG_EXTRA_SAMPLES: u16 = 338; |
| 40 | const TAG_SAMPLE_FORMAT: u16 = 339; | 40 | const TAG_SAMPLE_FORMAT: u16 = 339; |
| 41 | 41 | ||
| 42 | -const COMPRESSION_NONE: u64 = 1; | 42 | +#[derive(Clone, Copy, PartialEq, Eq)] |
| 43 | -const COMPRESSION_LZW: u64 = 5; | 43 | +enum Compression { |
| 44 | -const COMPRESSION_DEFLATE: u64 = 8; | 44 | + None, |
| 45 | -const COMPRESSION_PACKBITS: u64 = 32773; | 45 | + Lzw, |
| 46 | -const COMPRESSION_DEFLATE_OLD: u64 = 32946; | 46 | + PackBits, |
| 47 | + Deflate, | ||
| 48 | +} | ||
| 47 | 49 | ||
| 48 | /// The endianness the file declared, and the bytes it declared it about. | 50 | /// The endianness the file declared, and the bytes it declared it about. |
| 49 | struct Reader<'a> { | 51 | struct Reader<'a> { |
| @@ -53,27 +55,13 @@ struct Reader<'a> { | |||
| 53 | 55 | ||
| 54 | impl<'a> Reader<'a> { | 56 | impl<'a> Reader<'a> { |
| 55 | fn u16_at(&self, at: usize) -> Result<u16> { | 57 | fn u16_at(&self, at: usize) -> Result<u16> { |
| 56 | - let bytes = self | 58 | + crate::bytes::u16_at(self.data, at, self.big_endian) |
| 57 | - .data | 59 | + .ok_or_else(|| Error::corrupt("TIFF: a field past the end of the file")) |
| 58 | - .get(at..at + 2) | ||
| 59 | - .ok_or_else(|| Error::corrupt("TIFF: a field past the end of the file"))?; | ||
| 60 | - Ok(if self.big_endian { | ||
| 61 | - u16::from_be_bytes([bytes[0], bytes[1]]) | ||
| 62 | - } else { | ||
| 63 | - u16::from_le_bytes([bytes[0], bytes[1]]) | ||
| 64 | - }) | ||
| 65 | } | 60 | } |
| 66 | 61 | ||
| 67 | fn u32_at(&self, at: usize) -> Result<u32> { | 62 | fn u32_at(&self, at: usize) -> Result<u32> { |
| 68 | - let bytes = self | 63 | + crate::bytes::u32_at(self.data, at, self.big_endian) |
| 69 | - .data | 64 | + .ok_or_else(|| Error::corrupt("TIFF: a field past the end of the file")) |
| 70 | - .get(at..at + 4) | ||
| 71 | - .ok_or_else(|| Error::corrupt("TIFF: a field past the end of the file"))?; | ||
| 72 | - Ok(if self.big_endian { | ||
| 73 | - u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]) | ||
| 74 | - } else { | ||
| 75 | - u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]) | ||
| 76 | - }) | ||
| 77 | } | 65 | } |
| 78 | } | 66 | } |
| 79 | 67 | ||
| @@ -153,7 +141,7 @@ struct Layout { | |||
| 153 | bits: Vec<u16>, | 141 | bits: Vec<u16>, |
| 154 | samples: usize, | 142 | samples: usize, |
| 155 | photometric: u64, | 143 | photometric: u64, |
| 156 | - compression: u64, | 144 | + compression: Compression, |
| 157 | planar: u64, | 145 | planar: u64, |
| 158 | predictor: u64, | 146 | predictor: u64, |
| 159 | /// Strip or tile data offsets, in the order the file lists them. | 147 | /// Strip or tile data offsets, in the order the file lists them. |
| @@ -203,16 +191,15 @@ fn layout(data: &[u8]) -> Result<Layout> { | |||
| 203 | } | 191 | } |
| 204 | let entries = directory(&reader, reader.u32_at(4)? as usize)?; | 192 | let entries = directory(&reader, reader.u32_at(4)? as usize)?; |
| 205 | 193 | ||
| 206 | - let compression = first(&entries, TAG_COMPRESSION, COMPRESSION_NONE); | 194 | + let raw = first(&entries, TAG_COMPRESSION, 1); |
| 207 | - match compression { | 195 | + let compression = match raw { |
| 208 | - COMPRESSION_NONE | 196 | + 1 => Compression::None, |
| 209 | - | COMPRESSION_LZW | 197 | + 5 => Compression::Lzw, |
| 210 | - | COMPRESSION_DEFLATE | 198 | + 32773 => Compression::PackBits, |
| 211 | - | COMPRESSION_PACKBITS | 199 | + 8 | 32946 => Compression::Deflate, |
| 212 | - | COMPRESSION_DEFLATE_OLD => {} | ||
| 213 | 2..=4 => { | 200 | 2..=4 => { |
| 214 | return Err(Error::unsupported( | 201 | return Err(Error::unsupported( |
| 215 | - format!("TIFF: CCITT compression {compression}"), | 202 | + format!("TIFF: CCITT compression {raw}"), |
| 216 | "the fax coders are a separate format with their own \ | 203 | "the fax coders are a separate format with their own \ |
| 217 | two-dimensional coding modes", | 204 | two-dimensional coding modes", |
| 218 | )); | 205 | )); |
| @@ -225,7 +212,7 @@ fn layout(data: &[u8]) -> Result<Layout> { | |||
| 225 | )); | 212 | )); |
| 226 | } | 213 | } |
| 227 | other => return Err(Error::corrupt(format!("TIFF: compression {other}"))), | 214 | other => return Err(Error::corrupt(format!("TIFF: compression {other}"))), |
| 228 | - } | 215 | + }; |
| 229 | 216 | ||
| 230 | let samples = first(&entries, TAG_SAMPLES, 1) as usize; | 217 | let samples = first(&entries, TAG_SAMPLES, 1) as usize; |
| 231 | if samples == 0 || samples > 4 { | 218 | if samples == 0 || samples > 4 { |
| @@ -482,10 +469,10 @@ fn segment(layout: &Layout, data: &[u8], index: usize, expect: usize) -> Result< | |||
| 482 | .get(at..at.saturating_add(len)) | 469 | .get(at..at.saturating_add(len)) |
| 483 | .ok_or_else(|| Error::corrupt("TIFF: a strip reaching past the end of the file"))?; | 470 | .ok_or_else(|| Error::corrupt("TIFF: a strip reaching past the end of the file"))?; |
| 484 | Ok(match layout.compression { | 471 | Ok(match layout.compression { |
| 485 | - COMPRESSION_NONE => body.to_vec(), | 472 | + Compression::None => body.to_vec(), |
| 486 | - COMPRESSION_LZW => lzw(body, expect)?, | 473 | + Compression::Lzw => lzw(body, expect)?, |
| 487 | - COMPRESSION_PACKBITS => packbits(body, expect), | 474 | + Compression::PackBits => packbits(body, expect), |
| 488 | - _ => ec_inflate::inflate_zlib(body, expect.saturating_mul(2).max(1024)) | 475 | + Compression::Deflate => ec_inflate::inflate_zlib(body, expect.saturating_mul(2).max(1024)) |
| 489 | .map_err(|e| Error::corrupt(format!("TIFF: a Deflate strip: {e}")))?, | 476 | .map_err(|e| Error::corrupt(format!("TIFF: a Deflate strip: {e}")))?, |
| 490 | }) | 477 | }) |
| 491 | } | 478 | } |
crates/ec-subs/src/ir.rsmodified+3 -1
| @@ -7,6 +7,8 @@ | |||
| 7 | //! ASS `\pos` and `\an` — rides beside the runs instead of inside them, because | 7 | //! ASS `\pos` and `\an` — rides beside the runs instead of inside them, because |
| 8 | //! it applies to the cue as a whole. | 8 | //! it applies to the cue as a whole. |
| 9 | 9 | ||
| 10 | +pub type Rgb = (u8, u8, u8); | ||
| 11 | + | ||
| 10 | /// How a karaoke syllable is highlighted, after libass. | 12 | /// How a karaoke syllable is highlighted, after libass. |
| 11 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] | 13 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 12 | pub enum KaraokeMode { | 14 | pub enum KaraokeMode { |
| @@ -38,7 +40,7 @@ pub enum Segment { | |||
| 38 | /// Children in an explicit colour (`<font color>`, ASS `\c` / `\1c`). | 40 | /// Children in an explicit colour (`<font color>`, ASS `\c` / `\1c`). |
| 39 | Color { | 41 | Color { |
| 40 | /// Text colour, one byte per channel. | 42 | /// Text colour, one byte per channel. |
| 41 | - rgb: (u8, u8, u8), | 43 | + rgb: Rgb, |
| 42 | /// What the colour applies to. | 44 | /// What the colour applies to. |
| 43 | children: Vec<Segment>, | 45 | children: Vec<Segment>, |
| 44 | }, | 46 | }, |
crates/ec-subs/src/markup.rsmodified+93 -91
| @@ -8,7 +8,7 @@ | |||
| 8 | //! with nothing open is dropped, and a `<` that begins no tag at all is the | 8 | //! with nothing open is dropped, and a `<` that begins no tag at all is the |
| 9 | //! character it looks like. | 9 | //! character it looks like. |
| 10 | 10 | ||
| 11 | -use crate::ir::Segment; | 11 | +use crate::ir::{Rgb, Segment}; |
| 12 | 12 | ||
| 13 | /// Bytes to text, the way a subtitle file has to be read: a UTF-8 or UTF-16 BOM | 13 | /// Bytes to text, the way a subtitle file has to be read: a UTF-8 or UTF-16 BOM |
| 14 | /// consumed, invalid sequences replaced rather than refused, and CRLF (or a | 14 | /// consumed, invalid sequences replaced rather than refused, and CRLF (or a |
| @@ -86,70 +86,119 @@ pub fn parse_clock(field: &str) -> Option<i64> { | |||
| 86 | /// quadratic to close or the tree too deep to walk. | 86 | /// quadratic to close or the tree too deep to walk. |
| 87 | const MAX_NEST: usize = 64; | 87 | const MAX_NEST: usize = 64; |
| 88 | 88 | ||
| 89 | -/// One cue body's markup as runs. `webvtt` turns on the two tags only WebVTT | 89 | +/// Which markup a cue body is written in. |
| 90 | +#[derive(Clone, Copy, PartialEq, Eq)] | ||
| 91 | +pub(crate) enum Dialect { | ||
| 92 | + Srt, | ||
| 93 | + WebVtt, | ||
| 94 | +} | ||
| 95 | + | ||
| 96 | +/// The cue being built: its top-level runs, and the open tags above them, innermost last. | ||
| 97 | +#[derive(Default)] | ||
| 98 | +struct Tree { | ||
| 99 | + root: Vec<Segment>, | ||
| 100 | + open: Vec<(Open, Vec<Segment>)>, | ||
| 101 | +} | ||
| 102 | + | ||
| 103 | +impl Tree { | ||
| 104 | + fn top(&mut self) -> &mut Vec<Segment> { | ||
| 105 | + match self.open.last_mut() { | ||
| 106 | + Some((_, children)) => children, | ||
| 107 | + None => &mut self.root, | ||
| 108 | + } | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | + fn close_top(&mut self) { | ||
| 112 | + let Some((open, children)) = self.open.pop() else { | ||
| 113 | + return; | ||
| 114 | + }; | ||
| 115 | + let parent = self.top(); | ||
| 116 | + match open.name.as_str() { | ||
| 117 | + "b" => parent.push(Segment::Bold(children)), | ||
| 118 | + "i" => parent.push(Segment::Italic(children)), | ||
| 119 | + "u" => parent.push(Segment::Underline(children)), | ||
| 120 | + "s" => parent.push(Segment::Strike(children)), | ||
| 121 | + "v" => parent.push(Segment::Voice { | ||
| 122 | + name: open.annotation, | ||
| 123 | + children, | ||
| 124 | + }), | ||
| 125 | + "c" => parent.push(Segment::Class { | ||
| 126 | + name: open.annotation, | ||
| 127 | + children, | ||
| 128 | + }), | ||
| 129 | + _ => { | ||
| 130 | + if let Some(rgb) = open.color { | ||
| 131 | + parent.push(Segment::Color { rgb, children }); | ||
| 132 | + } else if open.family.is_some() || open.size.is_some() { | ||
| 133 | + parent.push(Segment::Font { | ||
| 134 | + family: open.family, | ||
| 135 | + size: open.size, | ||
| 136 | + children, | ||
| 137 | + }); | ||
| 138 | + } else { | ||
| 139 | + parent.extend(children); | ||
| 140 | + } | ||
| 141 | + } | ||
| 142 | + } | ||
| 143 | + } | ||
| 144 | +} | ||
| 145 | + | ||
| 146 | +/// One cue body's markup as runs. `WebVtt` turns on the two tags only WebVTT | ||
| 90 | /// has — `<v>` and `<c>` — and the inline timestamps that go with them. | 147 | /// has — `<v>` and `<c>` — and the inline timestamps that go with them. |
| 91 | -pub(crate) fn parse_inline(body: &str, webvtt: bool) -> Vec<Segment> { | 148 | +pub(crate) fn parse_inline(body: &str, dialect: Dialect) -> Vec<Segment> { |
| 92 | - // A stack of open tags, each with the children collected inside it so far; | 149 | + let mut tree = Tree::default(); |
| 93 | - // the bottom frame is the cue itself and can never be popped. | ||
| 94 | - let mut stack: Vec<(Option<Open>, Vec<Segment>)> = vec![(None, Vec::new())]; | ||
| 95 | let mut text = String::new(); | 150 | let mut text = String::new(); |
| 96 | let mut rest = body; | 151 | let mut rest = body; |
| 97 | while let Some(at) = rest.find('<') { | 152 | while let Some(at) = rest.find('<') { |
| 98 | text.push_str(&unescape(&rest[..at])); | 153 | text.push_str(&unescape(&rest[..at])); |
| 99 | let after = &rest[at + 1..]; | 154 | let after = &rest[at + 1..]; |
| 100 | let Some(end) = after.find('>') else { | 155 | let Some(end) = after.find('>') else { |
| 101 | - // No closing bracket anywhere: the rest of the cue is text. | ||
| 102 | text.push_str(&unescape(&rest[at..])); | 156 | text.push_str(&unescape(&rest[at..])); |
| 103 | rest = ""; | 157 | rest = ""; |
| 104 | break; | 158 | break; |
| 105 | }; | 159 | }; |
| 106 | let tag = &after[..end]; | 160 | let tag = &after[..end]; |
| 107 | rest = &after[end + 1..]; | 161 | rest = &after[end + 1..]; |
| 108 | - match classify(tag, webvtt) { | 162 | + match classify(tag, dialect) { |
| 109 | Tag::Text => { | 163 | Tag::Text => { |
| 110 | text.push('<'); | 164 | text.push('<'); |
| 111 | text.push_str(&unescape(tag)); | 165 | text.push_str(&unescape(tag)); |
| 112 | text.push('>'); | 166 | text.push('>'); |
| 113 | } | 167 | } |
| 114 | Tag::Timestamp(offset_us) => { | 168 | Tag::Timestamp(offset_us) => { |
| 115 | - flush(&mut text, &mut stack); | 169 | + flush(&mut text, &mut tree); |
| 116 | - push(&mut stack, Segment::Timestamp { offset_us }); | 170 | + tree.top().push(Segment::Timestamp { offset_us }); |
| 117 | } | 171 | } |
| 118 | Tag::Raw => { | 172 | Tag::Raw => { |
| 119 | - flush(&mut text, &mut stack); | 173 | + flush(&mut text, &mut tree); |
| 120 | - push(&mut stack, Segment::Raw(format!("<{tag}>"))); | 174 | + tree.top().push(Segment::Raw(format!("<{tag}>"))); |
| 121 | } | 175 | } |
| 122 | Tag::Open(open) => { | 176 | Tag::Open(open) => { |
| 123 | - flush(&mut text, &mut stack); | 177 | + flush(&mut text, &mut tree); |
| 124 | - if stack.len() <= MAX_NEST { | 178 | + if tree.open.len() < MAX_NEST { |
| 125 | - stack.push((Some(open), Vec::new())); | 179 | + tree.open.push((open, Vec::new())); |
| 126 | } | 180 | } |
| 127 | } | 181 | } |
| 128 | Tag::Close(name) => { | 182 | Tag::Close(name) => { |
| 129 | - flush(&mut text, &mut stack); | 183 | + flush(&mut text, &mut tree); |
| 130 | // Close the innermost frame with that name, and everything | 184 | // Close the innermost frame with that name, and everything |
| 131 | // inside it: `<b><i></b>` closes both, which is what a browser | 185 | // inside it: `<b><i></b>` closes both, which is what a browser |
| 132 | // does with the same bytes. | 186 | // does with the same bytes. |
| 133 | - let found = stack | 187 | + let found = tree.open.iter().rposition(|(open, _)| open.name == name); |
| 134 | - .iter() | ||
| 135 | - .rposition(|(open, _)| open.as_ref().is_some_and(|o| o.name == name)); | ||
| 136 | if let Some(depth) = found { | 188 | if let Some(depth) = found { |
| 137 | - while stack.len() > depth { | 189 | + while tree.open.len() > depth { |
| 138 | - close_top(&mut stack); | 190 | + tree.close_top(); |
| 139 | } | 191 | } |
| 140 | } | 192 | } |
| 141 | } | 193 | } |
| 142 | } | 194 | } |
| 143 | } | 195 | } |
| 144 | text.push_str(&unescape(rest)); | 196 | text.push_str(&unescape(rest)); |
| 145 | - flush(&mut text, &mut stack); | 197 | + flush(&mut text, &mut tree); |
| 146 | - while stack.len() > 1 { | 198 | + while !tree.open.is_empty() { |
| 147 | - close_top(&mut stack); | 199 | + tree.close_top(); |
| 148 | } | 200 | } |
| 149 | - stack | 201 | + tree.root |
| 150 | - .pop() | ||
| 151 | - .map(|(_, children)| children) | ||
| 152 | - .unwrap_or_default() | ||
| 153 | } | 202 | } |
| 154 | 203 | ||
| 155 | /// An open tag: its name and the arguments the closing wrapper will need. | 204 | /// An open tag: its name and the arguments the closing wrapper will need. |
| @@ -170,7 +219,7 @@ enum Tag { | |||
| 170 | Text, | 219 | Text, |
| 171 | } | 220 | } |
| 172 | 221 | ||
| 173 | -fn classify(tag: &str, webvtt: bool) -> Tag { | 222 | +fn classify(tag: &str, dialect: Dialect) -> Tag { |
| 174 | let trimmed = tag.trim(); | 223 | let trimmed = tag.trim(); |
| 175 | if trimmed.is_empty() { | 224 | if trimmed.is_empty() { |
| 176 | return Tag::Text; | 225 | return Tag::Text; |
| @@ -181,7 +230,7 @@ fn classify(tag: &str, webvtt: bool) -> Tag { | |||
| 181 | return Tag::Close(name); | 230 | return Tag::Close(name); |
| 182 | } | 231 | } |
| 183 | // `<00:00:01.500>` is a cue timestamp, not an element. | 232 | // `<00:00:01.500>` is a cue timestamp, not an element. |
| 184 | - if webvtt && trimmed.starts_with(|c: char| c.is_ascii_digit()) { | 233 | + if dialect == Dialect::WebVtt && trimmed.starts_with(|c: char| c.is_ascii_digit()) { |
| 185 | return match parse_clock(trimmed) { | 234 | return match parse_clock(trimmed) { |
| 186 | Some(offset_us) => Tag::Timestamp(offset_us), | 235 | Some(offset_us) => Tag::Timestamp(offset_us), |
| 187 | None => Tag::Text, | 236 | None => Tag::Text, |
| @@ -211,7 +260,7 @@ fn classify(tag: &str, webvtt: bool) -> Tag { | |||
| 211 | size: attribute(attrs, "size").and_then(|v| v.trim().parse().ok()), | 260 | size: attribute(attrs, "size").and_then(|v| v.trim().parse().ok()), |
| 212 | annotation: String::new(), | 261 | annotation: String::new(), |
| 213 | }), | 262 | }), |
| 214 | - "v" | "c" if webvtt => Tag::Open(Open { | 263 | + "v" | "c" if dialect == Dialect::WebVtt => Tag::Open(Open { |
| 215 | annotation: match name.as_str() { | 264 | annotation: match name.as_str() { |
| 216 | "v" => attrs.trim().to_owned(), | 265 | "v" => attrs.trim().to_owned(), |
| 217 | _ => classes.replace('.', " "), | 266 | _ => classes.replace('.', " "), |
| @@ -252,7 +301,7 @@ fn attribute(attrs: &str, name: &str) -> Option<String> { | |||
| 252 | } | 301 | } |
| 253 | 302 | ||
| 254 | /// `#rrggbb`, `#rgb`, or one of the sixteen names HTML has always had. | 303 | /// `#rrggbb`, `#rgb`, or one of the sixteen names HTML has always had. |
| 255 | -fn parse_color(value: &str) -> Option<(u8, u8, u8)> { | 304 | +fn parse_color(value: &str) -> Option<Rgb> { |
| 256 | let value = value.trim(); | 305 | let value = value.trim(); |
| 257 | if let Some(hex) = value.strip_prefix('#') { | 306 | if let Some(hex) = value.strip_prefix('#') { |
| 258 | let digits: String = hex.chars().take_while(char::is_ascii_hexdigit).collect(); | 307 | let digits: String = hex.chars().take_while(char::is_ascii_hexdigit).collect(); |
| @@ -350,22 +399,17 @@ fn unescape(text: &str) -> String { | |||
| 350 | out | 399 | out |
| 351 | } | 400 | } |
| 352 | 401 | ||
| 353 | -fn flush(text: &mut String, stack: &mut [(Option<Open>, Vec<Segment>)]) { | 402 | +fn flush(text: &mut String, tree: &mut Tree) { |
| 354 | if !text.is_empty() { | 403 | if !text.is_empty() { |
| 355 | let taken = std::mem::take(text); | 404 | let taken = std::mem::take(text); |
| 356 | - // A newline inside a cue body is a line break, not whitespace. | ||
| 357 | let mut lines = taken.split('\n'); | 405 | let mut lines = taken.split('\n'); |
| 358 | if let Some(first) = lines.next() | 406 | if let Some(first) = lines.next() |
| 359 | && !first.is_empty() | 407 | && !first.is_empty() |
| 360 | { | 408 | { |
| 361 | - stack | 409 | + tree.top().push(Segment::Text(first.into())); |
| 362 | - .last_mut() | ||
| 363 | - .unwrap() | ||
| 364 | - .1 | ||
| 365 | - .push(Segment::Text(first.into())); | ||
| 366 | } | 410 | } |
| 367 | for line in lines { | 411 | for line in lines { |
| 368 | - let children = &mut stack.last_mut().unwrap().1; | 412 | + let children = tree.top(); |
| 369 | children.push(Segment::LineBreak); | 413 | children.push(Segment::LineBreak); |
| 370 | if !line.is_empty() { | 414 | if !line.is_empty() { |
| 371 | children.push(Segment::Text(line.into())); | 415 | children.push(Segment::Text(line.into())); |
| @@ -374,48 +418,6 @@ fn flush(text: &mut String, stack: &mut [(Option<Open>, Vec<Segment>)]) { | |||
| 374 | } | 418 | } |
| 375 | } | 419 | } |
| 376 | 420 | ||
| 377 | -fn push(stack: &mut [(Option<Open>, Vec<Segment>)], segment: Segment) { | ||
| 378 | - stack.last_mut().unwrap().1.push(segment); | ||
| 379 | -} | ||
| 380 | - | ||
| 381 | -fn close_top(stack: &mut Vec<(Option<Open>, Vec<Segment>)>) { | ||
| 382 | - let Some((open, children)) = stack.pop() else { | ||
| 383 | - return; | ||
| 384 | - }; | ||
| 385 | - let Some(open) = open else { return }; | ||
| 386 | - let parent = &mut stack.last_mut().unwrap().1; | ||
| 387 | - match open.name.as_str() { | ||
| 388 | - "b" => parent.push(Segment::Bold(children)), | ||
| 389 | - "i" => parent.push(Segment::Italic(children)), | ||
| 390 | - "u" => parent.push(Segment::Underline(children)), | ||
| 391 | - "s" => parent.push(Segment::Strike(children)), | ||
| 392 | - "v" => parent.push(Segment::Voice { | ||
| 393 | - name: open.annotation, | ||
| 394 | - children, | ||
| 395 | - }), | ||
| 396 | - "c" => parent.push(Segment::Class { | ||
| 397 | - name: open.annotation, | ||
| 398 | - children, | ||
| 399 | - }), | ||
| 400 | - // `<font>`: whichever of colour and face/size it stated. One that | ||
| 401 | - // stated neither is a frame with nothing to say, so it is unwrapped | ||
| 402 | - // rather than kept as an empty node. | ||
| 403 | - _ => { | ||
| 404 | - if let Some(rgb) = open.color { | ||
| 405 | - parent.push(Segment::Color { rgb, children }); | ||
| 406 | - } else if open.family.is_some() || open.size.is_some() { | ||
| 407 | - parent.push(Segment::Font { | ||
| 408 | - family: open.family, | ||
| 409 | - size: open.size, | ||
| 410 | - children, | ||
| 411 | - }); | ||
| 412 | - } else { | ||
| 413 | - parent.extend(children); | ||
| 414 | - } | ||
| 415 | - } | ||
| 416 | - } | ||
| 417 | -} | ||
| 418 | - | ||
| 419 | #[cfg(test)] | 421 | #[cfg(test)] |
| 420 | mod tests { | 422 | mod tests { |
| 421 | use super::*; | 423 | use super::*; |
| @@ -446,33 +448,33 @@ mod tests { | |||
| 446 | 448 | ||
| 447 | #[test] | 449 | #[test] |
| 448 | fn markup_nests_and_forgives() { | 450 | fn markup_nests_and_forgives() { |
| 449 | - let segments = parse_inline("<i>a</i> <b>b", false); | 451 | + let segments = parse_inline("<i>a</i> <b>b", Dialect::Srt); |
| 450 | assert_eq!(plain_text(&segments), "a b"); | 452 | assert_eq!(plain_text(&segments), "a b"); |
| 451 | assert!(matches!(segments[0], Segment::Italic(_))); | 453 | assert!(matches!(segments[0], Segment::Italic(_))); |
| 452 | assert!(matches!(segments[2], Segment::Bold(_))); | 454 | assert!(matches!(segments[2], Segment::Bold(_))); |
| 453 | // Mismatched close, stray close, and a `<` that is just a `<`. | 455 | // Mismatched close, stray close, and a `<` that is just a `<`. |
| 454 | - assert_eq!(plain_text(&parse_inline("<b><i>x</b>y", false)), "xy"); | 456 | + assert_eq!(plain_text(&parse_inline("<b><i>x</b>y", Dialect::Srt)), "xy"); |
| 455 | - assert_eq!(plain_text(&parse_inline("</i>plain", false)), "plain"); | 457 | + assert_eq!(plain_text(&parse_inline("</i>plain", Dialect::Srt)), "plain"); |
| 456 | - assert_eq!(plain_text(&parse_inline("a < b", false)), "a < b"); | 458 | + assert_eq!(plain_text(&parse_inline("a < b", Dialect::Srt)), "a < b"); |
| 457 | assert_eq!( | 459 | assert_eq!( |
| 458 | - plain_text(&parse_inline("2 < 3 & 4", false)), | 460 | + plain_text(&parse_inline("2 < 3 & 4", Dialect::Srt)), |
| 459 | "2 < 3 & 4" | 461 | "2 < 3 & 4" |
| 460 | ); | 462 | ); |
| 461 | } | 463 | } |
| 462 | 464 | ||
| 463 | #[test] | 465 | #[test] |
| 464 | fn webvtt_only_tags_are_off_for_srt() { | 466 | fn webvtt_only_tags_are_off_for_srt() { |
| 465 | - let vtt = parse_inline("<v Ann>hi</v>", true); | 467 | + let vtt = parse_inline("<v Ann>hi</v>", Dialect::WebVtt); |
| 466 | assert!(matches!(vtt[0], Segment::Voice { .. })); | 468 | assert!(matches!(vtt[0], Segment::Voice { .. })); |
| 467 | assert_eq!(plain_text(&vtt), "hi"); | 469 | assert_eq!(plain_text(&vtt), "hi"); |
| 468 | - let srt = parse_inline("<v Ann>hi", false); | 470 | + let srt = parse_inline("<v Ann>hi", Dialect::Srt); |
| 469 | assert_eq!(plain_text(&srt), "hi"); | 471 | assert_eq!(plain_text(&srt), "hi"); |
| 470 | assert!(matches!(srt[0], Segment::Raw(_))); | 472 | assert!(matches!(srt[0], Segment::Raw(_))); |
| 471 | } | 473 | } |
| 472 | 474 | ||
| 473 | #[test] | 475 | #[test] |
| 474 | fn font_colour_is_read_by_hex_and_by_name() { | 476 | fn font_colour_is_read_by_hex_and_by_name() { |
| 475 | - let segments = parse_inline("<font color=\"#ff0000\">red</font>", false); | 477 | + let segments = parse_inline("<font color=\"#ff0000\">red</font>", Dialect::Srt); |
| 476 | assert!(matches!( | 478 | assert!(matches!( |
| 477 | segments[0], | 479 | segments[0], |
| 478 | Segment::Color { | 480 | Segment::Color { |
| @@ -480,7 +482,7 @@ mod tests { | |||
| 480 | .. | 482 | .. |
| 481 | } | 483 | } |
| 482 | )); | 484 | )); |
| 483 | - let named = parse_inline("<font color=lime>green</font>", false); | 485 | + let named = parse_inline("<font color=lime>green</font>", Dialect::Srt); |
| 484 | assert!(matches!( | 486 | assert!(matches!( |
| 485 | named[0], | 487 | named[0], |
| 486 | Segment::Color { | 488 | Segment::Color { |
crates/ec-subs/src/srt.rsmodified+2 -2
| @@ -10,7 +10,7 @@ | |||
| 10 | use ec_core::{Error, Result}; | 10 | use ec_core::{Error, Result}; |
| 11 | 11 | ||
| 12 | use crate::ir::{SourceFormat, SubtitleCue, SubtitleTrack}; | 12 | use crate::ir::{SourceFormat, SubtitleCue, SubtitleTrack}; |
| 13 | -use crate::markup::{decode, parse_clock, parse_inline}; | 13 | +use crate::markup::{decode, parse_clock, parse_inline, Dialect}; |
| 14 | 14 | ||
| 15 | /// Parse a SubRip document. | 15 | /// Parse a SubRip document. |
| 16 | /// | 16 | /// |
| @@ -40,7 +40,7 @@ pub fn parse(bytes: &[u8]) -> Result<SubtitleTrack> { | |||
| 40 | end_us: end_us.max(start_us), | 40 | end_us: end_us.max(start_us), |
| 41 | style_ref: None, | 41 | style_ref: None, |
| 42 | positioning: None, | 42 | positioning: None, |
| 43 | - segments: parse_inline(body.trim_end(), false), | 43 | + segments: parse_inline(body.trim_end(), Dialect::Srt), |
| 44 | }); | 44 | }); |
| 45 | } | 45 | } |
| 46 | if track.cues.is_empty() && text.split_whitespace().next().is_some() { | 46 | if track.cues.is_empty() && text.split_whitespace().next().is_some() { |
crates/ec-subs/src/webvtt.rsmodified+2 -2
| @@ -9,7 +9,7 @@ | |||
| 9 | use ec_core::{Error, Result}; | 9 | use ec_core::{Error, Result}; |
| 10 | 10 | ||
| 11 | use crate::ir::{CuePosition, SourceFormat, SubtitleCue, SubtitleTrack, TextAlign}; | 11 | use crate::ir::{CuePosition, SourceFormat, SubtitleCue, SubtitleTrack, TextAlign}; |
| 12 | -use crate::markup::{decode, parse_inline}; | 12 | +use crate::markup::{decode, parse_inline, Dialect}; |
| 13 | use crate::srt::timing; | 13 | use crate::srt::timing; |
| 14 | 14 | ||
| 15 | /// Parse a WebVTT document. | 15 | /// Parse a WebVTT document. |
| @@ -57,7 +57,7 @@ pub fn parse(bytes: &[u8]) -> Result<SubtitleTrack> { | |||
| 57 | end_us: end_us.max(start_us), | 57 | end_us: end_us.max(start_us), |
| 58 | style_ref: None, | 58 | style_ref: None, |
| 59 | positioning: settings(lines[at]), | 59 | positioning: settings(lines[at]), |
| 60 | - segments: parse_inline(body.trim_end(), true), | 60 | + segments: parse_inline(body.trim_end(), Dialect::WebVtt), |
| 61 | }); | 61 | }); |
| 62 | } | 62 | } |
| 63 | if track.cues.is_empty() && text.split_whitespace().next().is_some() { | 63 | if track.cues.is_empty() && text.split_whitespace().next().is_some() { |