From 12d1f47abd1b633e3b931168da0406405dde4b3b Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 10 Feb 2019 16:41:47 -0800 Subject: [PATCH] work on dcc decoding --- formats/dcc/direction.go | 6 ++-- formats/dcc/frame.go | 78 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 79 insertions(+), 5 deletions(-) diff --git a/formats/dcc/direction.go b/formats/dcc/direction.go index b3c40da..62cb71d 100644 --- a/formats/dcc/direction.go +++ b/formats/dcc/direction.go @@ -49,14 +49,14 @@ func readDirection(reader io.ReadSeeker, fileHead fileHeader) (*direction, error return nil, err } - frameHeads, bounds, err := readFrameHeaders(bitReader, fileHead, *dirHead) + frameHeads, dirBounds, err := readFrameHeaders(bitReader, fileHead, *dirHead) if err != nil { return nil, err } - dirData := direction{bounds: bounds} + dirData := direction{bounds: dirBounds} for _, frameHead := range frameHeads { - dirData.frames = append(dirData.frames, newFrame(frameHead, *dirHead)) + dirData.frames = append(dirData.frames, newFrame(frameHead, dirBounds)) } var entries []pixelBufferEntry diff --git a/formats/dcc/frame.go b/formats/dcc/frame.go index 305868b..f64bc06 100644 --- a/formats/dcc/frame.go +++ b/formats/dcc/frame.go @@ -95,9 +95,83 @@ func readFrameHeaders(bitReader *streaming.BitReader, fileHead fileHeader, dirHe type frame struct { header frameHeader + + nbCellsX int + nbCellsY int + dirOffsetX int + dirOffsetY int + + cellSameAsPrevious []bool + cellWidths []int + cellHeights []int + + data []byte } -func newFrame(frameHead frameHeader, dirHead directionHeader) frame { - return frame{frameHead} +func newFrame(frameHead frameHeader, dirBounds box) frame { + frameBounds := frameHead.bounds() + + frameData := frame{ + header: frameHead, + dirOffsetX: frameBounds.x1 - dirBounds.x1, + dirOffsetY: frameBounds.y1 - dirBounds.y1, + } + + widthFirstColumn := 4 - frameData.dirOffsetX%4 + frameWidth := frameBounds.x2 - frameBounds.x1 + if frameWidth-widthFirstColumn <= 1 { + frameData.nbCellsX = 1 + } else { + temp := frameWidth - widthFirstColumn - 1 + frameData.nbCellsX = 2 + temp/4 + if temp%4 == 0 { + frameData.nbCellsX-- + } + } + + heightFirstRow := 4 - frameData.dirOffsetY%4 + frameHeight := frameBounds.y2 - frameBounds.y1 + if frameHeight-heightFirstRow <= 1 { + frameData.nbCellsY = 1 + } else { + temp := frameHeight - heightFirstRow - 1 + frameData.nbCellsY = 2 + temp/4 + if temp%4 == 0 { + frameData.nbCellsY-- + } + } + + frameData.cellWidths = make([]int, frameData.nbCellsX) + for i := range frameData.cellWidths { + frameData.cellWidths[i] = 4 + } + + if frameData.nbCellsX == 1 { + frameData.cellWidths[0] = frameWidth + } else { + frameData.cellWidths[0] = widthFirstColumn + nbColumnsExcludingFirstAndLast := frameData.nbCellsX - 2 + widthExcludingFirstAndLastColumns := 4 * nbColumnsExcludingFirstAndLast + frameData.cellWidths[frameData.nbCellsX-1] = frameWidth - (widthFirstColumn + widthExcludingFirstAndLastColumns) + } + + frameData.cellHeights = make([]int, frameData.nbCellsY) + for i := range frameData.cellHeights { + frameData.cellHeights[i] = 4 + } + + if frameData.nbCellsY == 1 { + frameData.cellHeights[0] = frameHeight + } else { + frameData.cellHeights[0] = heightFirstRow + nbRowsExcludingFirstAndLast := frameData.nbCellsY - 2 + heightExcludingFirstAndLastRows := 4 * nbRowsExcludingFirstAndLast + frameData.cellHeights[frameData.nbCellsY-1] = frameHeight - (heightFirstRow + heightExcludingFirstAndLastRows) + } + + frameData.cellSameAsPrevious = make([]bool, frameData.nbCellsX*frameData.nbCellsY) + frameData.data = make([]byte, frameWidth*frameHeight) + + return frameData }