Skip to content

Commit

Permalink
Finish implementing multi-segment ODS support
Browse files Browse the repository at this point in the history
Related #13

Signed-off-by: Ethan Dye <[email protected]>
  • Loading branch information
ecdye committed Sep 30, 2024
1 parent 0ea3870 commit aa2d22d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
1 change: 1 addition & 0 deletions Sources/macSubtitleOCR/PGS/PGSError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
enum PGSError: Error {
case invalidFormat
case fileReadError
case invalidObjectDataSegment
}
19 changes: 10 additions & 9 deletions Sources/macSubtitleOCR/PGS/Parsers/ODS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class ODS {
// MARK: - Properties

private let logger = Logger(subsystem: "github.ecdye.macSubtitleOCR", category: "ODS")
private var objectDataLength: Int = 0
private var objectWidth: Int = 0
private var objectHeight: Int = 0
private var imageData: Data = .init()
Expand Down Expand Up @@ -51,8 +50,8 @@ class ODS {
// 0x17: Segment Type; already checked by the caller
// 2 bytes: Object ID (unused by us)
// 1 byte: Version number (unused by us)
// 1 byte: Sequence flag (should be 0x80 for new object, 0x00 for continuation) (unused by us)
// 3 bytes: Object data length
// 1 byte: Sequence flag (should be 0x80 for multi-segment object, 0xC0 for single segment, 0x40 for last segment)
// 3 bytes: Object data length (unused by us)
// 2 bytes: Object width
// 2 bytes: Object height
// Rest: Image data (run-length encoded, RLE)
Expand All @@ -61,16 +60,18 @@ class ODS {
let sequenceFlag = data[3]

if sequenceFlag == 0xC0 {
logger.debug("Sequence flag is 0xC0, indicating a new object fully contained in this segment")
logger.debug("Sequence flag indicates an object fully contained in this segment")
} else if sequenceFlag == 0x80 {
logger.debug("Sequence flag is 0x80, indicating a new object with more data in subsequent segments")
logger.debug("Sequence flag indicates an object contained in multiple segments")
} else if sequenceFlag == 0x40 {
logger.debug("Sequence flag indicates the last segment of a multi-segment object")
} else {
logger.warning("Unknown sequence flag: \(sequenceFlag)")
}

objectDataLength = Int(data[4]) << 16 | Int(data[5]) << 8 | Int(data[6])

// PGS includes the width and height as part of the image data length calculations
guard objectDataLength <= data.count - 7 else {
throw macSubtitleOCRError.invalidFormat
guard data.count > 7 else {
throw PGSError.invalidObjectDataSegment
}

objectWidth = Int(data[7]) << 8 | Int(data[8])
Expand Down
1 change: 0 additions & 1 deletion Sources/macSubtitleOCR/macSubtitleOCRError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

enum macSubtitleOCRError: Error {
case invalidFormat
case fileReadError
case fileCreationError
case fileWriteError
Expand Down

0 comments on commit aa2d22d

Please sign in to comment.