Skip to content

Commit

Permalink
Fix decoding of even lines
Browse files Browse the repository at this point in the history
Signed-off-by: Ethan Dye <mrtops03@gmail.com>
  • Loading branch information
ecdye committed Oct 1, 2024
1 parent 46177bd commit f7187d4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
40 changes: 34 additions & 6 deletions Sources/macSubtitleOCR/Subtitles/PGS/Parsers/RLE/RLEData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -103,28 +104,55 @@ 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 {

Check warning on line 110 in Sources/macSubtitleOCR/Subtitles/PGS/Parsers/RLE/RLEData.swift

View workflow job for this annotation

GitHub Actions / Lint

Prefer constant values to be on the right-hand-side of expressions. (yodaConditions)

Check warning on line 110 in Sources/macSubtitleOCR/Subtitles/PGS/Parsers/RLE/RLEData.swift

View workflow job for this annotation

GitHub Actions / Lint

Prefer constant values to be on the right-hand-side of expressions. (yodaConditions)
i -= 4
currentNibbles = [nibbles[i], nibbles[i + 1]]
i += 2
continue
}
} else if 4 <= run, run <= 15 {

Check warning on line 115 in Sources/macSubtitleOCR/Subtitles/PGS/Parsers/RLE/RLEData.swift

View workflow job for this annotation

GitHub Actions / Lint

Replace consecutive spaces with a single space. (consecutiveSpaces)

Check warning on line 115 in Sources/macSubtitleOCR/Subtitles/PGS/Parsers/RLE/RLEData.swift

View workflow job for this annotation

GitHub Actions / Lint

Prefer constant values to be on the right-hand-side of expressions. (yodaConditions)

Check warning on line 115 in Sources/macSubtitleOCR/Subtitles/PGS/Parsers/RLE/RLEData.swift

View workflow job for this annotation

GitHub Actions / Lint

Replace consecutive spaces with a single space. (consecutiveSpaces)

Check warning on line 115 in Sources/macSubtitleOCR/Subtitles/PGS/Parsers/RLE/RLEData.swift

View workflow job for this annotation

GitHub Actions / Lint

Prefer constant values to be on the right-hand-side of expressions. (yodaConditions)
i -= 5
currentNibbles = [nibbles[i], nibbles[i + 1]]
i += 2
continue
}// else if 16 <= run, run <= 63 {

Check warning on line 120 in Sources/macSubtitleOCR/Subtitles/PGS/Parsers/RLE/RLEData.swift

View workflow job for this annotation

GitHub Actions / Lint

Add space before and/or after comments. (spaceAroundComments)

Check warning on line 120 in Sources/macSubtitleOCR/Subtitles/PGS/Parsers/RLE/RLEData.swift

View workflow job for this annotation

GitHub Actions / Lint

Add space before and/or after comments. (spaceAroundComments)
// 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)

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
}
Expand Down
5 changes: 4 additions & 1 deletion Sources/macSubtitleOCR/Subtitles/VobSub/VobSubParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)))

Expand Down

0 comments on commit f7187d4

Please sign in to comment.