detector passes

This commit is contained in:
Henry Schimke
2022-09-28 16:46:22 -05:00
parent ad9ef3fee9
commit b4c59deb01
4 changed files with 220 additions and 183 deletions

View File

@@ -146,20 +146,20 @@ impl StringUtils {
// For now, merely tries to distinguish ISO-8859-1, UTF-8 and Shift_JIS,
// which should be by far the most common encodings.
let length = bytes.len();
let mut canBeISO88591 = true;
let mut canBeShiftJIS = true;
let mut canBeUTF8 = true;
let mut utf8BytesLeft = 0;
let mut utf2BytesChars = 0;
let mut utf3BytesChars = 0;
let mut utf4BytesChars = 0;
let mut sjisBytesLeft = 0;
let mut sjisKatakanaChars = 0;
let mut sjisCurKatakanaWordLength = 0;
let mut sjisCurDoubleBytesWordLength = 0;
let mut sjisMaxKatakanaWordLength = 0;
let mut sjisMaxDoubleBytesWordLength = 0;
let mut isoHighOther = 0;
let mut can_be_iso88591 = true;
let mut can_be_shift_jis = true;
let mut can_be_utf8 = true;
let mut utf8_bytes_left = 0;
let mut utf2_bytes_chars = 0;
let mut utf3_bytes_chars = 0;
let mut utf4_bytes_chars = 0;
let mut sjis_bytes_left = 0;
let mut sjis_katakana_chars = 0;
let mut sjis_cur_katakana_word_length = 0;
let mut sjis_cur_double_bytes_word_length = 0;
let mut sjis_max_katakana_word_length = 0;
let mut sjis_max_double_bytes_word_length = 0;
let mut iso_high_other = 0;
let utf8bom = bytes.len() > 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF;
@@ -167,37 +167,37 @@ impl StringUtils {
// for (int i = 0;
// i < length && (canBeISO88591 || canBeShiftJIS || canBeUTF8);
// i++) {
if !(canBeISO88591 || canBeShiftJIS || canBeUTF8) {
if !(can_be_iso88591 || can_be_shift_jis || can_be_utf8) {
break;
}
let value = bytes[i] & 0xFF;
// UTF-8 stuff
if canBeUTF8 {
if utf8BytesLeft > 0 {
if can_be_utf8 {
if utf8_bytes_left > 0 {
if (value & 0x80) == 0 {
canBeUTF8 = false;
can_be_utf8 = false;
} else {
utf8BytesLeft -= 1;
utf8_bytes_left -= 1;
}
} else if (value & 0x80) != 0 {
if (value & 0x40) == 0 {
canBeUTF8 = false;
can_be_utf8 = false;
} else {
utf8BytesLeft += 1;
utf8_bytes_left += 1;
if (value & 0x20) == 0 {
utf2BytesChars += 1;
utf2_bytes_chars += 1;
} else {
utf8BytesLeft += 1;
utf8_bytes_left += 1;
if (value & 0x10) == 0 {
utf3BytesChars += 1;
utf3_bytes_chars += 1;
} else {
utf8BytesLeft += 1;
utf8_bytes_left += 1;
if (value & 0x08) == 0 {
utf4BytesChars += 1;
utf4_bytes_chars += 1;
} else {
canBeUTF8 = false;
can_be_utf8 = false;
}
}
}
@@ -206,63 +206,63 @@ impl StringUtils {
}
// ISO-8859-1 stuff
if canBeISO88591 {
if can_be_iso88591 {
if value > 0x7F && value < 0xA0 {
canBeISO88591 = false;
can_be_iso88591 = false;
} else if value > 0x9F && (value < 0xC0 || value == 0xD7 || value == 0xF7) {
isoHighOther += 1;
iso_high_other += 1;
}
}
// Shift_JIS stuff
if canBeShiftJIS {
if sjisBytesLeft > 0 {
if can_be_shift_jis {
if sjis_bytes_left > 0 {
if value < 0x40 || value == 0x7F || value > 0xFC {
canBeShiftJIS = false;
can_be_shift_jis = false;
} else {
sjisBytesLeft -= 1;
sjis_bytes_left -= 1;
}
} else if value == 0x80 || value == 0xA0 || value > 0xEF {
canBeShiftJIS = false;
can_be_shift_jis = false;
} else if value > 0xA0 && value < 0xE0 {
sjisKatakanaChars += 1;
sjisCurDoubleBytesWordLength = 0;
sjisCurKatakanaWordLength += 1;
if sjisCurKatakanaWordLength > sjisMaxKatakanaWordLength {
sjisMaxKatakanaWordLength = sjisCurKatakanaWordLength;
sjis_katakana_chars += 1;
sjis_cur_double_bytes_word_length = 0;
sjis_cur_katakana_word_length += 1;
if sjis_cur_katakana_word_length > sjis_max_katakana_word_length {
sjis_max_katakana_word_length = sjis_cur_katakana_word_length;
}
} else if value > 0x7F {
sjisBytesLeft += 1;
sjis_bytes_left += 1;
//sjisDoubleBytesChars++;
sjisCurKatakanaWordLength = 0;
sjisCurDoubleBytesWordLength += 1;
if sjisCurDoubleBytesWordLength > sjisMaxDoubleBytesWordLength {
sjisMaxDoubleBytesWordLength = sjisCurDoubleBytesWordLength;
sjis_cur_katakana_word_length = 0;
sjis_cur_double_bytes_word_length += 1;
if sjis_cur_double_bytes_word_length > sjis_max_double_bytes_word_length {
sjis_max_double_bytes_word_length = sjis_cur_double_bytes_word_length;
}
} else {
//sjisLowChars++;
sjisCurKatakanaWordLength = 0;
sjisCurDoubleBytesWordLength = 0;
sjis_cur_katakana_word_length = 0;
sjis_cur_double_bytes_word_length = 0;
}
}
}
if canBeUTF8 && utf8BytesLeft > 0 {
canBeUTF8 = false;
if can_be_utf8 && utf8_bytes_left > 0 {
can_be_utf8 = false;
}
if canBeShiftJIS && sjisBytesLeft > 0 {
canBeShiftJIS = false;
if can_be_shift_jis && sjis_bytes_left > 0 {
can_be_shift_jis = false;
}
// Easy -- if there is BOM or at least 1 valid not-single byte character (and no evidence it can't be UTF-8), done
if canBeUTF8 && (utf8bom || utf2BytesChars + utf3BytesChars + utf4BytesChars > 0) {
if can_be_utf8 && (utf8bom || utf2_bytes_chars + utf3_bytes_chars + utf4_bytes_chars > 0) {
return encoding::all::UTF_8;
}
// Easy -- if assuming Shift_JIS or >= 3 valid consecutive not-ascii characters (and no evidence it can't be), done
if canBeShiftJIS
if can_be_shift_jis
&& (ASSUME_SHIFT_JIS
|| sjisMaxKatakanaWordLength >= 3
|| sjisMaxDoubleBytesWordLength >= 3)
|| sjis_max_katakana_word_length >= 3
|| sjis_max_double_bytes_word_length >= 3)
{
return encoding::label::encoding_from_whatwg_label("SJIS").unwrap();
}
@@ -271,9 +271,9 @@ impl StringUtils {
// - only two consecutive katakana chars in the whole text, or
// - at least 10% of bytes that could be "upper" not-alphanumeric Latin1,
// - then we conclude Shift_JIS, else ISO-8859-1
if canBeISO88591 && canBeShiftJIS {
return if (sjisMaxKatakanaWordLength == 2 && sjisKatakanaChars == 2)
|| isoHighOther * 10 >= length
if can_be_iso88591 && can_be_shift_jis {
return if (sjis_max_katakana_word_length == 2 && sjis_katakana_chars == 2)
|| iso_high_other * 10 >= length
{
encoding::label::encoding_from_whatwg_label("SJIS").unwrap()
} else {
@@ -282,13 +282,13 @@ impl StringUtils {
}
// Otherwise, try in order ISO-8859-1, Shift JIS, UTF-8 and fall back to default platform encoding
if canBeISO88591 {
if can_be_iso88591 {
return encoding::all::ISO_8859_1;
}
if canBeShiftJIS {
if can_be_shift_jis {
return encoding::label::encoding_from_whatwg_label("SJIS").unwrap();
}
if canBeUTF8 {
if can_be_utf8 {
return encoding::all::UTF_8;
}
// Otherwise, we take a wild guess with platform encoding
@@ -361,7 +361,7 @@ impl BitArray {
return (self.size + 7) / 8;
}
fn ensureCapacity(&mut self, newSize: usize) {
fn ensure_capacity(&mut self, newSize: usize) {
if newSize > self.bits.len() * 32 {
let mut newBits = BitArray::makeArray((newSize as f32 / LOAD_FACTOR).ceil() as usize);
//System.arraycopy(bits, 0, newBits, 0, bits.length);
@@ -536,7 +536,7 @@ impl BitArray {
}
pub fn appendBit(&mut self, bit: bool) {
self.ensureCapacity(self.size + 1);
self.ensure_capacity(self.size + 1);
if bit {
self.bits[self.size / 32] |= 1 << (self.size & 0x1F);
}
@@ -563,7 +563,7 @@ impl BitArray {
}
let mut nextSize = self.size;
self.ensureCapacity(nextSize + numBits);
self.ensure_capacity(nextSize + numBits);
for numBitsLeft in (0..(numBits)).rev() {
//for (int numBitsLeft = numBits - 1; numBitsLeft >= 0; numBitsLeft--) {
if (value & (1 << numBitsLeft)) != 0 {
@@ -577,7 +577,7 @@ impl BitArray {
pub fn appendBitArray(&mut self, other: BitArray) {
let otherSize = other.size;
self.ensureCapacity(self.size + otherSize);
self.ensure_capacity(self.size + otherSize);
for i in 0..otherSize {
//for (int i = 0; i < otherSize; i++) {
self.appendBit(other.get(i));
@@ -1663,10 +1663,10 @@ impl PerspectiveTransform {
x3p: f32,
y3p: f32,
) -> Self {
let qToS = PerspectiveTransform::quadrilateralToSquare(x0, y0, x1, y1, x2, y2, x3, y3);
let sToQ =
let q_to_s = PerspectiveTransform::quadrilateralToSquare(x0, y0, x1, y1, x2, y2, x3, y3);
let s_to_q =
PerspectiveTransform::squareToQuadrilateral(x0p, y0p, x1p, y1p, x2p, y2p, x3p, y3p);
return sToQ.times(&qToS);
return s_to_q.times(&q_to_s);
}
pub fn transform_points_single(&self, points: &mut [f32]) {
@@ -2335,27 +2335,27 @@ impl GridSampler for DefaultGridSampler {
for y in 0..dimensionY {
// for (int y = 0; y < dimensionY; y++) {
let max = points.len();
let iValue = y as f32 + 0.5f32;
let i_value = y as f32 + 0.5f32;
let mut x = 0;
while x < max {
// for (int x = 0; x < max; x += 2) {
points[x] = (x as f32 / 2.0) + 0.5f32;
points[x + 1] = iValue;
points[x + 1] = i_value;
x += 2;
}
transform.transform_points_single(&mut points);
// Quick check to see if points transformed to something inside the image;
// sufficient to check the endpoints
self.checkAndNudgePoints(image, &mut points);
self.checkAndNudgePoints(image, &mut points)?;
// try {
let mut x = 0;
while x < max {
// for (int x = 0; x < max; x += 2) {
if image.get(points[x] as u32, points[x + 1] as u32) {
if image.get(points[x].floor() as u32, points[x + 1].floor() as u32) {
// Black(-ish) pixel
bits.set(x as u32 / 2, y);
x += 2;
}
x += 2;
}
// } catch (ArrayIndexOutOfBoundsException aioobe) {
// // This feels wrong, but, sometimes if the finder patterns are misidentified, the resulting