diff --git a/src/pixie/common.nim b/src/pixie/common.nim index 869f4b5..e090fe6 100644 --- a/src/pixie/common.nim +++ b/src/pixie/common.nim @@ -1 +1,2 @@ -## Add private variables or functions here that you don't want to export. +type + PixieError* = object of ValueError ## Raised if an operation fails. diff --git a/src/pixie/fileformats/bmp.nim b/src/pixie/fileformats/bmp.nim index bdcab8f..3a2c9d3 100644 --- a/src/pixie/fileformats/bmp.nim +++ b/src/pixie/fileformats/bmp.nim @@ -1,4 +1,4 @@ -import ../images, flatty/binny, chroma +import ../images, flatty/binny, chroma, pixie/common # See: https://en.wikipedia.org/wiki/BMP_file_format @@ -6,17 +6,26 @@ proc decodeBmp*(data: string): Image = ## Decodes bitmap data into an Image. # BMP Header - doAssert data[0..1] == "BM" - let - width = data.readInt32(0x12).int - height = data.readInt32(0x16).int - bits = data.readUint16(0x1C) - compression = data.readUint32(0x1E) - var - offset = data.readUInt32(0xA).int + if data[0..1] != "BM": + raise newException(PixieError, "Invalid BMP data") - doAssert bits in {32, 24} - doAssert compression in {0, 3} + let + width = data.readInt32(18).int + height = data.readInt32(22).int + bits = data.readUint16(28).int + compression = data.readUint32(30).int + var + offset = data.readUInt32(10).int + + if bits notin [32, 24]: + raise newException(PixieError, "Unsupported BMP data format") + + if compression notin [0, 3]: + raise newException(PixieError, "Unsupported BMP data format") + + let channels = if bits == 32: 4 else: 3 + if width * height * channels + offset > data.len: + raise newException(PixieError, "Invalid BMP data size") result = newImage(width, height) @@ -38,7 +47,7 @@ proc decodeBmp*(data: string): Image = result[x, result.height - y - 1] = rgba proc encodeBmp*(image: Image): string = - ## Encodes an image into bitmap data. + ## Encodes an image into the BMP file format. # BMP Header result.add("BM") # The header field used to identify the BMP diff --git a/tests/testbmp.nim b/tests/test_bmp.nim similarity index 94% rename from tests/testbmp.nim rename to tests/test_bmp.nim index c9768dd..d02b36c 100644 --- a/tests/testbmp.nim +++ b/tests/test_bmp.nim @@ -1,4 +1,4 @@ -import pixie, pixie/fileformats/bmp, chroma, flatty/hexPrint +import pixie, pixie/fileformats/bmp, chroma block: var image = newImage(4, 2) diff --git a/tests/testimages.nim b/tests/test_images.nim similarity index 100% rename from tests/testimages.nim rename to tests/test_images.nim