Skip to content

Commit

Permalink
Jpg: Add parseFrameKinds debugging function
Browse files Browse the repository at this point in the history
  • Loading branch information
nh2 committed Dec 7, 2022
1 parent 76d8cbc commit c88dab7
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/Codec/Picture/Jpg/Internal/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module Codec.Picture.Jpg.Internal.Types( MutableMacroBlock
, skipFrameMarker
, parseFrameOfKind
, parseFrames
, parseFrameKinds
, parseToFirstFrameHeader
) where

Expand Down Expand Up @@ -69,7 +70,7 @@ import qualified Data.ByteString.Char8 as BC
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Unsafe as BU

import Data.Int( Int16 )
import Data.Int( Int16, Int64 )
import Data.Word(Word8, Word16 )
import Data.Binary( Binary(..) )

Expand Down Expand Up @@ -824,6 +825,25 @@ parseFrames = do
remainingFrames <- parseFrames
return $ maybeToList mbFrame ++ remainingFrames

-- | Parse a list of `JpgFrameKind`s with their corresponding offsets and lengths
-- (not counting the segment and frame markers into the lengths).
--
-- Useful for debugging.
parseFrameKinds :: Get [(JpgFrameKind, Int64, Int64)]
parseFrameKinds = do
kindMarkerOffset :: Int64 <- bytesRead
kind <- get
case kind of
JpgEndOfImage -> pure [(JpgEndOfImage, kindMarkerOffset, 0)]
_ -> do
parserOffsetBefore <- bytesRead
_ <- parseFrameOfKind kind
parserOffsetAfter <- bytesRead
let !segmentLengthWithoutMarker = parserOffsetAfter - parserOffsetBefore
skipFrameMarker
remainingKinds <- parseFrameKinds
return $ (kind, kindMarkerOffset, segmentLengthWithoutMarker):remainingKinds

-- | Parses forward, returning the first scan header encountered.
--
-- Should be used after `skipUntilFrames`.
Expand Down

0 comments on commit c88dab7

Please sign in to comment.