From f7187d49903580af1bae4505a46453602c6f7e84 Mon Sep 17 00:00:00 2001 From: Ethan Dye Date: Mon, 30 Sep 2024 09:37:42 -0600 Subject: [PATCH] Fix decoding of even lines Signed-off-by: Ethan Dye --- .../Subtitles/PGS/Parsers/RLE/RLEData.swift | 40 ++++++++++++++++--- .../Subtitles/VobSub/VobSubParser.swift | 5 ++- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/Sources/macSubtitleOCR/Subtitles/PGS/Parsers/RLE/RLEData.swift b/Sources/macSubtitleOCR/Subtitles/PGS/Parsers/RLE/RLEData.swift index 1d17f64..52602eb 100644 --- a/Sources/macSubtitleOCR/Subtitles/PGS/Parsers/RLE/RLEData.swift +++ b/Sources/macSubtitleOCR/Subtitles/PGS/Parsers/RLE/RLEData.swift @@ -87,6 +87,7 @@ class RLEData { var i = 0 var y = 0 var x = 0 + var onlyHalf = false var currentNibbles: [UInt8?] = [nibbles[i], nibbles[i + 1]] i += 2 while currentNibbles[1] != nil, y < height { @@ -103,11 +104,33 @@ class RLEData { } let color = UInt8(nibble & 0x03) var run = Int(nibble >> 2) - if (image.count % width) == 0, color != 0 { - if run == 15 { + + if onlyHalf, color != 0 { + print("Got something weird, fixing it \(color) \(run)") + if 1 <= run, run <= 3 { i -= 4 + currentNibbles = [nibbles[i], nibbles[i + 1]] + i += 2 continue - } + } else if 4 <= run, run <= 15 { + i -= 5 + currentNibbles = [nibbles[i], nibbles[i + 1]] + i += 2 + continue + }// else if 16 <= run, run <= 63 { + // i -= 7 + // currentNibbles = [nibbles[i], nibbles[i + 1]] + // i += 2 + // continue + // } //else if 64 <= run, run <= 255 { + // i -= 8 + // currentNibbles = [nibbles[i], nibbles[i + 1]] + // i += 2 + // continue + // } + } + if onlyHalf { + onlyHalf = false } x += Int(run) @@ -115,16 +138,21 @@ class RLEData { if run == 0 || x >= width { run += width - x x = 0 - if y % 2 == 0 { - odd.toggle() - } + // Index is byte aligned at start of new line + // if y % 2 == 0, y > height / 2 { + // odd.toggle() + // } y += 1 + onlyHalf = true image.append(contentsOf: repeatElement(color, count: run)) image.append(contentsOf: repeatElement(0, count: width)) continue } image.append(contentsOf: repeatElement(color, count: run)) + if image.count == 181585 { + print("Image count is 181585") + } } return image } diff --git a/Sources/macSubtitleOCR/Subtitles/VobSub/VobSubParser.swift b/Sources/macSubtitleOCR/Subtitles/VobSub/VobSubParser.swift index 8652e7b..9c70344 100644 --- a/Sources/macSubtitleOCR/Subtitles/VobSub/VobSubParser.swift +++ b/Sources/macSubtitleOCR/Subtitles/VobSub/VobSubParser.swift @@ -22,7 +22,8 @@ func readSubFrame(pic: inout Subtitle, subFile: FileHandle, offset: UInt64, next var rleLengthFound = 0 pic.numberOfColors = 16 - logger.info("Reading subtitle frame at offset \(offset) with next offset \(nextOffset)") + // logger.info("Reading subtitle frame at offset \(offset) with next offset \(nextOffset)") + print("Reading subtitle frame at offset \(offset) with next offset \(nextOffset)") subFile.seek(toFileOffset: offset) repeat { let startOffset = subFile.offsetInFile @@ -224,6 +225,8 @@ func decodeImage(subtitle: Subtitle, fileBuffer _: FileHandle) throws -> Data { let rleData = RLEData(data: subtitle.imageData, width: width, height: height) bitmap = try rleData.decodeVobSub() + // Investigate what role the length of the data plays + // decode even lines // decodeLine(subtitle.imageData, rleOffset: subtitle.imageEvenOffset, rleLength: sizeEven, target: &bitmap, targetOffset: 0, width: width, maxPixels: width * (height / 2 + (height & 1)))