Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / pdf / src / main / rust / src / images_part13.rs
images_part13.rs284 lines · 15.2 KiBRaw
| 1 | #[cfg(test)] |
| 2 | mod mask_tests4 { |
| 3 | use super::*; |
| 4 | use super::mask_tests::{g4_encode, half_black_g4}; |
| 5 | use super::mask_tests2::jbig2_mmr_stream; |
| 6 | /// §8.7.4.3 Table 78 confines /Background to the area OUTSIDE the shading's bounds. |
| 7 | /// Two arms used it as the shading's own in-extent colour: `eval_func` returned it |
| 8 | /// when /Function was absent (making all 256 LUT slots the background, so the whole |
| 9 | /// area painted solid), and the per-pixel lookup fell back to it when no colour |
| 10 | /// could be computed. They had to go together — with `eval_func` fixed alone, every |
| 11 | /// LUT slot is empty and the second arm reproduces the identical flood. |
| 12 | /// (a-shading's finding.) |
| 13 | #[test] |
| 14 | fn background_is_never_used_as_the_shadings_own_colour() { |
| 15 | let doc = Document::with_version("1.7"); |
| 16 | // Types 2/3 REQUIRE /Function (§8.7.4.5.3); this one has none. The axis spans |
| 17 | // only the middle fifth of the bbox, so the raster has both in-extent pixels |
| 18 | // (t in [0,1], centre) and out-of-extent ones (t < 0 / t > 1, edges). |
| 19 | let sh = Object::Dictionary(dictionary! { |
| 20 | "ShadingType" => 2, |
| 21 | "ColorSpace" => "DeviceRGB", |
| 22 | "Coords" => vec![0.4.into(), 0.into(), 0.6.into(), 0.into()], |
| 23 | "Extend" => vec![Object::Boolean(false), Object::Boolean(false)], |
| 24 | "BBox" => vec![0.into(), 0.into(), 1.into(), 1.into()], |
| 25 | "Background" => vec![1.into(), 0.into(), 0.into()], |
| 26 | }); |
| 27 | let (_, w, h, pat) = |
| 28 | rasterize_shading_as_pattern(&doc, &sh, &IDENTITY, &HashMap::new(), 32, None) |
| 29 | .expect("rasterizes"); |
| 30 | let px = |x: usize, y: usize| -> &[u8] { |
| 31 | let i = (y * w as usize + x) * 4; |
| 32 | &pat[i..i + 4] |
| 33 | }; |
| 34 | let mid_y = (h as usize) / 2; |
| 35 | // Inside the extent the shading has no colour, so the pixel stays CLEAR. |
| 36 | assert_eq!( |
| 37 | px(16, mid_y), &[0, 0, 0, 0], |
| 38 | "a point INSIDE the extent must not take /Background" |
| 39 | ); |
| 40 | // Outside it, /Background is exactly what Table 78 asks for. |
| 41 | assert_eq!(px(0, mid_y), &[255, 0, 0, 255], "left of the axis is outside -> background"); |
| 42 | assert_eq!(px(31, mid_y), &[255, 0, 0, 255], "right of the axis is outside -> background"); |
| 43 | // And the whole raster must not be background, which is the flood symptom. |
| 44 | let red = pat.chunks_exact(4).filter(|p| p[0] == 255 && p[3] == 255).count(); |
| 45 | assert!( |
| 46 | red < (w as usize) * (h as usize), |
| 47 | "the shading's own area must not be flooded with /Background" |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | /// §7.4.6 Table 11: `/Rows` defaults to 0. Every other CCITT fixture in this file |
| 52 | /// supplies it explicitly, so `images.rs`'s own `params.rows > 0` fallback at :1163 |
| 53 | /// and the `/Rows`-absent path end to end were unexercised. That is what this pins. |
| 54 | /// |
| 55 | /// It does NOT witness `filters::decode_ccitt`'s row-count derivation, and I claimed |
| 56 | /// otherwise before measuring. a-file mutated that derivation and this test came back |
| 57 | /// green; reading :1163 explains why, and the reason is structural rather than a |
| 58 | /// fixture weakness: `rows_est` is `params.rows` or `/Height`, never anything derived |
| 59 | /// from the data length, and `row_bytes` comes from `columns`. Extra rows the decoder |
| 60 | /// may produce are never read. |
| 61 | /// |
| 62 | /// Precisely: this consumer depends on `decode_ccitt` returning `Some`, NOT on which |
| 63 | /// row count it chose. Those are different, and "no dependency here" — which is what |
| 64 | /// this comment said first — was too broad. The `Option` half IS a real dependency and |
| 65 | /// is covered by `a_long_ccitt_payload_for_a_short_image_still_renders` below. |
| 66 | /// (a-file's narrowing, after their measurement corrected my first claim.) |
| 67 | #[test] |
| 68 | fn a_ccitt_image_without_an_explicit_rows_parameter_still_gets_every_row() { |
| 69 | let doc = Document::with_version("1.7"); |
| 70 | let (data, w) = half_black_g4(); |
| 71 | // /Rows deliberately ABSENT — the default per Table 11. |
| 72 | let d = dictionary! { |
| 73 | "Width" => w as i64, "Height" => 2, "BitsPerComponent" => 1, |
| 74 | "ImageMask" => true, |
| 75 | "Filter" => "CCITTFaxDecode", |
| 76 | "DecodeParms" => dictionary! { "K" => -1, "Columns" => w as i64 }, |
| 77 | }; |
| 78 | let img = extract_image_inner(&doc, &Stream::new(d, data), 0xFF00_FF00, &HashMap::new()) |
| 79 | .expect("a G4 stencil with no /Rows must still decode"); |
| 80 | assert_eq!( |
| 81 | (img.w, img.h), (8, 2), |
| 82 | "with /Rows absent the raster must still be /Height rows tall, not truncated" |
| 83 | ); |
| 84 | // Both rows must carry the fax's ink. A row count derived short leaves row 1 |
| 85 | // entirely unpainted, which renders as the bottom half of a scan going missing. |
| 86 | let alpha = |x: usize, y: usize| img.data[(y * 8 + x) * 4 + 3]; |
| 87 | assert_eq!(alpha(0, 0), 255, "row 0, x=0 is a black pel - painted"); |
| 88 | assert_eq!(alpha(4, 0), 0, "row 0, x=4 is white - transparent"); |
| 89 | assert_eq!(alpha(0, 1), 255, "row 1, x=0 must be painted too"); |
| 90 | assert_eq!(alpha(4, 1), 0, "row 1, x=4 stays transparent"); |
| 91 | } |
| 92 | |
| 93 | /// The one dependency images.rs DOES have on `filters::decode_ccitt`'s row handling: |
| 94 | /// the `Option`, not the count. `decode_ccitt` refuses over 20000 rows, and when its |
| 95 | /// row estimate came from the PAYLOAD LENGTH rather than `/Height`, a long payload |
| 96 | /// for a short image pushed the estimate past that ceiling, returned `None`, and |
| 97 | /// skipped this consumer's entire CCITT branch at images.rs:1161 — total loss of the |
| 98 | /// image through the `Option`, not a wrong raster. That is the invisible-hole symptom. |
| 99 | /// |
| 100 | /// WITNESSED: a-file re-applied the payload-derived estimate in filters.rs and this |
| 101 | /// test FAILED at the raster assertion, alongside their own |
| 102 | /// `ccitt_row_count_comes_from_the_height_not_the_compressed_length`, while the four |
| 103 | /// polarity tests above stayed green. Their own test passing under fixed code and |
| 104 | /// failing in that same invocation certifies the binary held the mutation, so the |
| 105 | /// result does not rest on marker timing or binary mtime — a failure is consistent |
| 106 | /// with exactly one compiled state. The seam is covered at this point. |
| 107 | #[test] |
| 108 | fn a_long_ccitt_payload_for_a_short_image_still_renders() { |
| 109 | let doc = Document::with_version("1.7"); |
| 110 | // 8-wide rows, encoded far past the point where payload*8/columns exceeds the |
| 111 | // decoder's 20000-row ceiling, while /Height stays 2. The phase alternates per |
| 112 | // row so G4's vertical mode cannot collapse them — identical rows compress to a |
| 113 | // couple of bits each and never reach the ceiling. |
| 114 | let rows: Vec<Vec<bool>> = (0..20000) |
| 115 | .map(|i: usize| (0..8).map(|x| (x + i) % 2 == 0).collect()) |
| 116 | .collect(); |
| 117 | let data = g4_encode(&rows, 8); |
| 118 | assert!( |
| 119 | data.len() > 20000, |
| 120 | "fixture must exceed the row ceiling under a payload-derived estimate, got {} bytes", |
| 121 | data.len() |
| 122 | ); |
| 123 | let d = dictionary! { |
| 124 | "Width" => 8, "Height" => 2, "BitsPerComponent" => 1, |
| 125 | "ImageMask" => true, |
| 126 | "Filter" => "CCITTFaxDecode", |
| 127 | "DecodeParms" => dictionary! { "K" => -1, "Columns" => 8 }, |
| 128 | }; |
| 129 | let img = extract_image_inner(&doc, &Stream::new(d, data), 0xFF00_FF00, &HashMap::new()) |
| 130 | .expect("a long payload must not make the whole image vanish"); |
| 131 | assert_eq!((img.w, img.h), (8, 2), "the raster is sized by /Height, not the payload"); |
| 132 | // The fixture alternates phase per row: row i paints x where (x + i) is even. |
| 133 | let alpha = |x: usize, y: usize| img.data[(y * 8 + x) * 4 + 3]; |
| 134 | assert_eq!(alpha(0, 0), 255, "row 0, x=0 is a black pel"); |
| 135 | assert_eq!(alpha(1, 0), 0, "row 0, x=1 is white"); |
| 136 | assert_eq!(alpha(0, 1), 0, "row 1 inverts the phase: x=0 is white"); |
| 137 | assert_eq!(alpha(1, 1), 255, "row 1, x=1 is black"); |
| 138 | } |
| 139 | |
| 140 | // --------------------------------------------------------------------- |
| 141 | // R5: defects visible only with the filter chain and the image decoder in |
| 142 | // view at once. |
| 143 | // --------------------------------------------------------------------- |
| 144 | |
| 145 | /// `objects.rs:106` returns an EMPTY buffer when every decoder has failed, |
| 146 | /// deliberately, so that still-encoded bytes are never handed on as samples. The |
| 147 | /// image layer then read every absent byte as sample 0 and `image_samples_to_rgba` |
| 148 | /// turned an all-zero DeviceRGB pixel into black at alpha 255 — so "render |
| 149 | /// nothing" arrived as an OPAQUE BLACK RECTANGLE covering whatever the image was |
| 150 | /// placed over. Neither half of that is wrong on its own, which is why it survived: |
| 151 | /// the filter side's contract and the image side's zero-fill contract only conflict |
| 152 | /// where they meet. |
| 153 | #[test] |
| 154 | fn a_contone_image_whose_filter_chain_fails_is_dropped_not_painted_black() { |
| 155 | let doc = Document::with_version("1.7"); |
| 156 | let d = dictionary! { |
| 157 | "Width" => 8, "Height" => 8, "BitsPerComponent" => 8, |
| 158 | "ColorSpace" => "DeviceRGB", |
| 159 | "Filter" => "FlateDecode", |
| 160 | }; |
| 161 | // 0xFF opens a deflate block with the reserved BTYPE, so no inflate attempt |
| 162 | // (zlib, raw, or skip-one-byte) yields a single byte. |
| 163 | let junk = vec![0xFFu8; 64]; |
| 164 | assert!( |
| 165 | crate::filters::decode_flate(&junk).is_none(), |
| 166 | "fixture must actually be undecodable, or the test proves nothing" |
| 167 | ); |
| 168 | assert!( |
| 169 | extract_image_inner(&doc, &Stream::new(d, junk), 0xFF00_0000, &HashMap::new()).is_none(), |
| 170 | "a failed Flate image must be dropped, not rendered as a black rectangle" |
| 171 | ); |
| 172 | } |
| 173 | |
| 174 | /// The same seam on the inline path, where the buffer is empty rather than absent: |
| 175 | /// `/AHx` over a lone EOD marker decodes SUCCESSFULLY to zero bytes, so the |
| 176 | /// chain-failure guard above it does not fire. Unpacked as a stencil that is |
| 177 | /// sample 0 everywhere, and sample 0 MARKS the page (§8.9.6.2) — a solid block of |
| 178 | /// the current fill colour over the content. |
| 179 | #[test] |
| 180 | fn an_inline_stencil_that_decodes_to_no_bytes_paints_nothing() { |
| 181 | let doc = Document::with_version("1.7"); |
| 182 | let d = dictionary! { |
| 183 | "W" => 8, "H" => 8, "IM" => true, "F" => "AHx", |
| 184 | }; |
| 185 | assert!( |
| 186 | extract_inline_image_inner(&doc, &Stream::new(d, b">".to_vec()), 0xFF00_FF00, &HashMap::new()) |
| 187 | .is_none(), |
| 188 | "an inline stencil with no samples must be dropped, not painted solid" |
| 189 | ); |
| 190 | } |
| 191 | |
| 192 | /// A partially-decoded inline stencil must not paint the rows the data never |
| 193 | /// reached. `unpack_samples_to_bytes` zero-fills them by design (§8.9.5.1 gives |
| 194 | /// /Width x /Height authority over the sample count), and zero is the PAINT value |
| 195 | /// for a stencil, so the two contracts combined to paint everything below the last |
| 196 | /// real scanline. The XObject stencil branch already defaulted absent bytes to the |
| 197 | /// no-paint value; the inline one went through the unpacker and lost the |
| 198 | /// distinction. |
| 199 | #[test] |
| 200 | fn a_truncated_inline_stencil_does_not_paint_the_missing_rows() { |
| 201 | let doc = Document::with_version("1.7"); |
| 202 | let d = dictionary! { "W" => 8, "H" => 4, "IM" => true }; |
| 203 | // Two rows of "all 1 bits" = nothing painted; rows 2 and 3 are simply absent. |
| 204 | let img = extract_inline_image_inner( |
| 205 | &doc, |
| 206 | &Stream::new(d, vec![0xFFu8, 0xFF]), |
| 207 | 0xFF00_FF00, |
| 208 | &HashMap::new(), |
| 209 | ) |
| 210 | .expect("two good rows must still render"); |
| 211 | assert_eq!((img.w, img.h), (8, 4)); |
| 212 | for px in 0..(8 * 4) { |
| 213 | assert_eq!( |
| 214 | img.data[px * 4 + 3], 0, |
| 215 | "pixel {px}: neither a decoded 1 bit nor an absent row may paint" |
| 216 | ); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /// §8.9.7 Table 93 lists `/CCF` among the filters an INLINE image may name, and |
| 221 | /// `decode_stream_chain` deliberately leaves every image codec encoded because only |
| 222 | /// the image layer knows /Width and /Height (filters.rs:545). The inline path never |
| 223 | /// implemented the other half of that contract, so a G4 codestream went straight |
| 224 | /// into the sample unpacker and was read as one-bit samples: noise, and for |
| 225 | /// `/IM true` a speckled block of fill colour over the page. |
| 226 | #[test] |
| 227 | fn an_inline_ccitt_stencil_is_decoded_rather_than_unpacked_as_samples() { |
| 228 | let doc = Document::with_version("1.7"); |
| 229 | let (data, w) = half_black_g4(); |
| 230 | let d = dictionary! { |
| 231 | "W" => w as i64, "H" => 2, "IM" => true, |
| 232 | "F" => "CCF", |
| 233 | "DP" => dictionary! { "K" => -1, "Columns" => w as i64, "Rows" => 2 }, |
| 234 | }; |
| 235 | let img = extract_inline_image_inner(&doc, &Stream::new(d, data), 0xFF00_FF00, &HashMap::new()) |
| 236 | .expect("an inline G4 stencil must decode"); |
| 237 | assert_eq!((img.w, img.h), (8, 2)); |
| 238 | // Same expectation as the XObject fixture: left half black (painted), right |
| 239 | // half white (transparent), on both rows. |
| 240 | let alpha = |x: usize, y: usize| img.data[(y * 8 + x) * 4 + 3]; |
| 241 | assert_eq!(alpha(0, 0), 255, "a black pel takes the fill colour"); |
| 242 | assert_eq!(alpha(3, 0), 255, "still ink at x=3"); |
| 243 | assert_eq!(alpha(4, 0), 0, "the white half is transparent"); |
| 244 | assert_eq!(alpha(0, 1), 255, "row 1 is painted the same way"); |
| 245 | assert_eq!(alpha(4, 1), 0); |
| 246 | assert_eq!(&img.data[0..3], &[0, 255, 0], "and it is the CURRENT FILL COLOUR"); |
| 247 | } |
| 248 | |
| 249 | /// A JBIG2 image that is NOT a stencil still has a `/Decode` array (§8.9.5.2), and |
| 250 | /// `jbig2.rs` returns finished RGB, so nothing downstream would ever apply it — |
| 251 | /// `decode_inverts_1bit` was computed and then used only for the stencil arm. |
| 252 | /// Pinned in both directions so a later change cannot apply it twice. |
| 253 | #[test] |
| 254 | fn a_jbig2_image_honours_its_decode_array_exactly_once() { |
| 255 | let jbig2_image = |decode_inverts: bool| -> ImageData { |
| 256 | let doc = Document::with_version("1.7"); |
| 257 | let (mmr, w) = half_black_g4(); |
| 258 | let data = jbig2_mmr_stream(w as u32, 2, &mmr); |
| 259 | let mut d = dictionary! { |
| 260 | "Width" => w as i64, "Height" => 2, "BitsPerComponent" => 1, |
| 261 | "ColorSpace" => "DeviceGray", |
| 262 | "Filter" => "JBIG2Decode", |
| 263 | }; |
| 264 | if decode_inverts { |
| 265 | d.set("Decode", vec![1.into(), 0.into()]); |
| 266 | } |
| 267 | extract_image(&doc, &Stream::new(d, data), 0xFF00_0000, &HashMap::new()) |
| 268 | .expect("an MMR generic region must decode") |
| 269 | }; |
| 270 | let plain = jbig2_image(false); |
| 271 | assert_eq!(plain.data[0], 0, "the left half is black ink"); |
| 272 | assert_eq!(plain.data[4 * 4], 255, "the right half is white paper"); |
| 273 | |
| 274 | let inverted = jbig2_image(true); |
| 275 | assert_eq!(inverted.data[0], 255, "/Decode [1 0] turns the ink white"); |
| 276 | assert_eq!(inverted.data[4 * 4], 0, "and the paper black"); |
| 277 | // Applying it twice would give back `plain`, which this catches. |
| 278 | assert_ne!(plain.data, inverted.data); |
| 279 | assert_eq!( |
| 280 | inverted.data[3], 255, |
| 281 | "inverting colour must not disturb alpha" |
| 282 | ); |
| 283 | } |
| 284 | } |