Skip to content

Commit

Permalink
Hack new decoder, but kinda works
Browse files Browse the repository at this point in the history
Signed-off-by: Ethan Dye <mrtops03@gmail.com>
  • Loading branch information
ecdye committed Sep 30, 2024
1 parent c1b8c3e commit 46177bd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
55 changes: 41 additions & 14 deletions Sources/macSubtitleOCR/Subtitles/PGS/Parsers/RLE/RLEData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,36 +84,63 @@ class RLEData {
throw RLEDataError.invalidData
}

var i = nibbles.makeIterator()
var i = 0
var y = 0
var x = 0
while var nibble = i.next(), y < height {
if odd {
nibble = i.next()!
odd.toggle()
}
var currentNibbles: [UInt8?] = [nibbles[i], nibbles[i + 1]]
i += 2
while currentNibbles[1] != nil, y < height {
var nibble = getNibble(currentNibbles: &currentNibbles, nibbles: nibbles, i: &i, odd: &odd)

if nibble < 0x04 {
if nibble == 0x00 {
nibble = nibble << 4 | i.next()!
nibble = nibble < 0x04 ? nibble << 4 | i.next()! : nibble
nibble = nibble << 4 | getNibble(currentNibbles: &currentNibbles, nibbles: nibbles, i: &i, odd: &odd)
if nibble < 0x04 {
nibble = nibble << 4 | getNibble(currentNibbles: &currentNibbles, nibbles: nibbles, i: &i, odd: &odd)
}
}
nibble = nibble << 4 | getNibble(currentNibbles: &currentNibbles, nibbles: nibbles, i: &i, odd: &odd)
}
let color = UInt8(nibble & 0x03)
var run = Int(nibble >> 2)
if (image.count % width) == 0, color != 0 {
if run == 15 {
i -= 4
continue
}
nibble = nibble << 4 | i.next()!
}
let color = 3 - (nibble & 0x03)
var run: Int = Int(nibble >> 2)
x += Int(run)

x += Int(run)

if run == 0 || x >= width {
run += width - x
x = 0
if y % 2 == 0 {
odd.toggle()
}
y += 1
odd.toggle()
image.append(contentsOf: repeatElement(color, count: run))
image.append(contentsOf: repeatElement(0, count: width))
continue
}

image.append(contentsOf: repeatElement(color, count: run))
}
return image
}

func getNibble(currentNibbles: inout [UInt8?], nibbles: Data, i: inout Int, odd: inout Bool) -> UInt16 {
if odd {
// _ = currentNibbles.removeFirst()
_ = currentNibbles.removeFirst()
odd.toggle()
// currentNibbles.append(i.next())
currentNibbles.append(nibbles[i])
i += 1
}
return image // Placeholder
let nibble = UInt16(currentNibbles.removeFirst()!)
currentNibbles.append(nibbles[i])
i += 1
return nibble
}
}
4 changes: 2 additions & 2 deletions Sources/macSubtitleOCR/Subtitles/VobSub/VobSubParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func decodeImage(subtitle: Subtitle, fileBuffer _: FileHandle) throws -> Data {
// decodeLine(subtitle.imageData, rleOffset: subtitle.imageEvenOffset, rleLength: sizeEven, target: &bitmap, targetOffset: 0, width: width, maxPixels: width * (height / 2 + (height & 1)))

// decode odd lines
//decodeLine(subtitle.imageData, rleOffset: subtitle.imageOddOffset, rleLength: sizeOdd, target: &bitmap, targetOffset: subtitle.stride, width: subtitle.stride, maxPixels: (height / 2) * subtitle.stride)
// decodeLine(subtitle.imageData, rleOffset: subtitle.imageOddOffset, rleLength: sizeOdd, target: &bitmap, targetOffset: subtitle.stride, width: subtitle.stride, maxPixels: (height / 2) * subtitle.stride)

return bitmap
}
Expand All @@ -247,7 +247,7 @@ private func decodeLine(_ rleData: Data, rleOffset: Int, rleLength: Int, target:

var sumPixels = 0
var x = 0
var trgOfs = targetOffset
var trgOfs = targetOffset > 0 ? width : 0

while var b = nibIterator.next(), sumPixels < maxPixels {
var len: Int
Expand Down

0 comments on commit 46177bd

Please sign in to comment.