From 42da6582cfea698428c1a66dbfe75d582fc1e71b Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Fri, 20 Nov 2020 13:13:18 -0600 Subject: [PATCH 1/5] bmp stuff --- src/pixie/common.nim | 3 ++- src/pixie/fileformats/bmp.nim | 27 ++++++++++++++--------- tests/{testbmp.nim => test_bmp.nim} | 2 +- tests/{testimages.nim => test_images.nim} | 0 4 files changed, 19 insertions(+), 13 deletions(-) rename tests/{testbmp.nim => test_bmp.nim} (94%) rename tests/{testimages.nim => test_images.nim} (100%) 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..f532654 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,22 @@ 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, "Invalid BMP data format") + + if compression notin [0, 3]: + raise newException(PixieError, "Invalid BMP data format") result = newImage(width, height) 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 From c114ca02dd09421e940dbdde98b1ca0b558bdd71 Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Fri, 20 Nov 2020 13:51:43 -0600 Subject: [PATCH 2/5] data len check --- src/pixie/fileformats/bmp.nim | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/pixie/fileformats/bmp.nim b/src/pixie/fileformats/bmp.nim index f532654..d555960 100644 --- a/src/pixie/fileformats/bmp.nim +++ b/src/pixie/fileformats/bmp.nim @@ -23,6 +23,10 @@ proc decodeBmp*(data: string): Image = if compression notin [0, 3]: raise newException(PixieError, "Invalid 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) for y in 0 ..< result.height: From 552b167953a88fc2ce9a07a7ef7b71586aa91700 Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Fri, 20 Nov 2020 15:03:39 -0600 Subject: [PATCH 3/5] f --- src/pixie/fileformats/bmp.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pixie/fileformats/bmp.nim b/src/pixie/fileformats/bmp.nim index d555960..d63021d 100644 --- a/src/pixie/fileformats/bmp.nim +++ b/src/pixie/fileformats/bmp.nim @@ -18,7 +18,7 @@ proc decodeBmp*(data: string): Image = offset = data.readUInt32(10).int if bits notin [32, 24]: - raise newException(PixieError, "Invalid BMP data format") + raise newException(PixieError, "Unsupported BMP data format") if compression notin [0, 3]: raise newException(PixieError, "Invalid BMP data format") From e3e94cff85481b08217b660baecff4635cccb76b Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Fri, 20 Nov 2020 18:20:30 -0600 Subject: [PATCH 4/5] f --- src/pixie/fileformats/bmp.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pixie/fileformats/bmp.nim b/src/pixie/fileformats/bmp.nim index d63021d..d859584 100644 --- a/src/pixie/fileformats/bmp.nim +++ b/src/pixie/fileformats/bmp.nim @@ -47,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 From f978516b399eb0486d8ee3e099223de64dc19dc1 Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Fri, 20 Nov 2020 20:13:06 -0600 Subject: [PATCH 5/5] f --- src/pixie/fileformats/bmp.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pixie/fileformats/bmp.nim b/src/pixie/fileformats/bmp.nim index d859584..3a2c9d3 100644 --- a/src/pixie/fileformats/bmp.nim +++ b/src/pixie/fileformats/bmp.nim @@ -21,7 +21,7 @@ proc decodeBmp*(data: string): Image = raise newException(PixieError, "Unsupported BMP data format") if compression notin [0, 3]: - raise newException(PixieError, "Invalid BMP data format") + raise newException(PixieError, "Unsupported BMP data format") let channels = if bits == 32: 4 else: 3 if width * height * channels + offset > data.len: