add ptr+len decode dimens

This commit is contained in:
Ryan Oldenburg 2023-05-28 21:58:20 -05:00
parent c32c18dd8b
commit dc69535191
2 changed files with 25 additions and 6 deletions

View file

@ -228,19 +228,27 @@ proc decodeBmp*(data: string): Image {.raises: [PixieError].} =
decodeDib(data[14].unsafeAddr, data.len - 14)
proc decodeBmpDimensions*(
data: string
data: pointer, len: int
): ImageDimensions {.raises: [PixieError].} =
## Decodes the BMP dimensions.
if data.len < 26:
if len < 26:
failInvalid()
let data = cast[ptr UncheckedArray[uint8]](data)
# BMP Header
if data[0 .. 1] != "BM":
if data[0].char != 'B' or data[1].char != 'M': # Must start with BM
failInvalid()
result.width = data.readInt32(18).int
result.height = abs(data.readInt32(22)).int
proc decodeBmpDimensions*(
data: string
): ImageDimensions {.raises: [PixieError].} =
## Decodes the BMP dimensions.
decodeBmpDimensions(data.cstring, data.len)
proc encodeDib*(image: Image): string {.raises: [].} =
## Encodes an image into a DIB.

View file

@ -379,18 +379,29 @@ proc decodeGif*(data: string): Gif {.raises: [PixieError].} =
result.duration += interval
proc decodeGifDimensions*(
data: string
data: pointer, len: int
): ImageDimensions {.raises: [PixieError].} =
## Decodes the GIF dimensions.
if data.len < 10:
if len < 10:
failInvalid()
if data[0 .. 5] notin gifSignatures:
let data = cast[ptr UncheckedArray[uint8]](data)
let startsWithSignature =
equalMem(data, gifSignatures[0].cstring, 6) or
equalMem(data, gifSignatures[1].cstring, 6)
if not startsWithSignature:
raise newException(PixieError, "Invalid GIF file signature")
result.width = data.readInt16(6).int
result.height = data.readInt16(8).int
proc decodeGifDimensions*(
data: string
): ImageDimensions {.raises: [PixieError].} =
decodeGifDimensions(data.cstring, data.len)
proc newImage*(gif: Gif): Image {.raises: [].} =
gif.frames[0].copy()