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
#![doc = include_str!("../README.md")]
use bitvec::bitvec;
use bitvec::vec::BitVec;
#[cfg(feature = "image")]
use image::{DynamicImage, ImageBuffer, Luma};
#[cfg(feature = "image")]
mod convert;
mod vote;
#[cfg(feature = "image")]
use convert::ToLumaZero;
pub use vote::{Vote, Votes};
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("inconsistency between the raw image array and the width and height provided")]
InvalidRawDimensions,
#[error("failed to encode the original image to a 99% quality JPEG: {0}")]
Encoding(#[from] jpeg_encoder::EncodingError),
#[cfg(feature = "image")]
#[error("failed to decode the image: {0}")]
Decoding(#[from] image::ImageError),
}
type Result<T> = std::result::Result<T, Error>;
#[derive(Default, Clone, Copy, PartialEq, Eq, Debug)]
pub struct Grid(pub u8);
impl Grid {
const fn from_xy(x: u32, y: u32) -> Self {
Self(((x % 8) + (y % 8) * 8) as u8)
}
pub const fn x(&self) -> u8 {
self.0 % 8
}
pub const fn y(&self) -> u8 {
self.0 / 8
}
}
#[derive(Default, Clone, Debug)]
pub struct ForgedRegion {
pub start: (u32, u32),
pub end: (u32, u32),
pub grid: Grid,
pub lnfa: f64,
regions_xy: Box<[(u32, u32)]>,
}
pub struct ForeignGridAreas {
luminance: LuminanceImage,
votes: Votes,
forged_regions: Box<[ForgedRegion]>,
lnfa_grids: [f64; 64],
main_grid: Option<Grid>,
}
impl ForeignGridAreas {
pub fn votes(&self) -> &Votes {
&self.votes
}
pub fn build_forgery_mask(&self) -> ForgeryMask {
ForgeryMask::from_regions(&self.forged_regions, self.votes.width, self.votes.height)
}
pub fn forged_regions(&self) -> &[ForgedRegion] {
self.forged_regions.as_ref()
}
pub fn lnfa_grids(&self) -> [f64; 64] {
self.lnfa_grids
}
pub fn main_grid(&self) -> Option<Grid> {
self.main_grid
}
pub fn is_cropped(&self) -> bool {
self.main_grid.map_or(false, |grid| grid.0 > 0)
}
#[cfg(feature = "image")]
pub fn detect_missing_grid_areas(&self) -> Result<Option<MissingGridAreas>> {
let main_grid = if let Some(main_grid) = self.main_grid {
main_grid
} else {
return Ok(None);
};
let jpeg_99 = self.luminance.to_jpeg_99_luminance()?;
let mut jpeg_99_votes = Votes::from_luminance(&jpeg_99);
for (&vote, vote_99) in self.votes.iter().zip(jpeg_99_votes.iter_mut()) {
if vote == Vote::AlignedWith(main_grid) {
*vote_99 = Vote::Invalid;
}
}
let jpeg_99_forged_regions = jpeg_99_votes.detect_forgeries(None, Grid(0));
Ok(Some(MissingGridAreas {
votes: jpeg_99_votes,
missing_regions: jpeg_99_forged_regions,
}))
}
}
impl IntoIterator for ForeignGridAreas {
type Item = ForgedRegion;
type IntoIter = std::vec::IntoIter<ForgedRegion>;
fn into_iter(self) -> Self::IntoIter {
self.forged_regions.into_vec().into_iter()
}
}
pub struct MissingGridAreas {
votes: Votes,
missing_regions: Box<[ForgedRegion]>,
}
impl MissingGridAreas {
pub fn votes(&self) -> &Votes {
&self.votes
}
pub fn forged_regions(&self) -> &[ForgedRegion] {
self.missing_regions.as_ref()
}
pub fn build_forgery_mask(self) -> ForgeryMask {
ForgeryMask::from_regions(&self.missing_regions, self.votes.width, self.votes.height)
}
}
impl IntoIterator for MissingGridAreas {
type Item = ForgedRegion;
type IntoIter = std::vec::IntoIter<ForgedRegion>;
fn into_iter(self) -> Self::IntoIter {
self.missing_regions.into_vec().into_iter()
}
}
pub struct Zero {
luminance: LuminanceImage,
}
impl Zero {
#[cfg(feature = "image")]
pub fn from_image(image: &DynamicImage) -> Self {
let luminance = image.to_luma32f_zero();
Self { luminance }
}
pub fn from_luminance_raw(luminance: Box<[f64]>, width: u32, height: u32) -> Result<Self> {
if luminance.len() != width.saturating_mul(height) as usize {
return Err(Error::InvalidRawDimensions);
}
Ok(Self {
luminance: LuminanceImage {
image: luminance,
width,
height,
},
})
}
pub fn detect_forgeries(self) -> ForeignGridAreas {
let votes = Votes::from_luminance(&self.luminance);
let (main_grid, lnfa_grids) = votes.detect_global_grids();
let forged_regions = votes.detect_forgeries(main_grid, Grid(63));
ForeignGridAreas {
luminance: self.luminance,
votes,
forged_regions,
lnfa_grids,
main_grid,
}
}
}
#[cfg(feature = "image")]
impl IntoIterator for Zero {
type Item = ForgedRegion;
type IntoIter = Box<dyn Iterator<Item = ForgedRegion>>;
fn into_iter(self) -> Self::IntoIter {
let foreign_grid_areas = self.detect_forgeries();
let missing_grid_regions = foreign_grid_areas
.detect_missing_grid_areas()
.ok()
.flatten()
.into_iter()
.flat_map(IntoIterator::into_iter);
let forged_regions = foreign_grid_areas.into_iter().chain(missing_grid_regions);
Box::new(forged_regions)
}
}
pub struct ForgeryMask {
mask: BitVec,
width: u32,
height: u32,
}
impl ForgeryMask {
#[cfg(feature = "image")]
pub fn into_luma_image(self) -> ImageBuffer<Luma<u8>, Vec<u8>> {
ImageBuffer::from_fn(self.width, self.height, |x, y| {
let index = (x + y * self.width) as usize;
Luma([u8::from(self.mask[index]) * 255])
})
}
pub fn is_forged(&self, x: u32, y: u32) -> bool {
self.mask
.get((x + y * self.width) as usize)
.as_deref()
.copied()
.unwrap_or(false)
}
pub fn width(&self) -> u32 {
self.width
}
pub fn height(&self) -> u32 {
self.height
}
fn from_regions(regions: &[ForgedRegion], width: u32, height: u32) -> Self {
let w = 9;
let mut mask_aux = bitvec![0; width as usize * height as usize];
let mut forgery_mask = bitvec![0; width as usize * height as usize];
for forged in regions {
for &(x, y) in forged.regions_xy.iter() {
for xx in (x - w)..=(x + w) {
for yy in (y - w)..=(y + w) {
let index = (xx + yy * width) as usize;
mask_aux.set(index, true);
forgery_mask.set(index, true);
}
}
}
}
for x in w..width.saturating_sub(w) {
for y in w..height.saturating_sub(w) {
let index = (x + y * width) as usize;
if !mask_aux[index] {
for xx in (x - w)..=(x + w) {
for yy in (y - w)..=(y + w) {
let index = (xx + yy * width) as usize;
forgery_mask.set(index, false);
}
}
}
}
}
Self {
mask: forgery_mask,
width,
height,
}
}
}
pub(crate) struct LuminanceImage {
image: Box<[f64]>,
width: u32,
height: u32,
}
impl LuminanceImage {
pub(crate) fn width(&self) -> u32 {
self.width
}
pub(crate) fn height(&self) -> u32 {
self.height
}
pub(crate) fn as_raw(&self) -> &[f64] {
&self.image
}
pub(crate) unsafe fn unsafe_get_pixel(&self, x: u32, y: u32) -> &f64 {
self.image.get_unchecked((x + y * self.width) as usize)
}
}