From 3c810c63deba729cae1beab3218ec6ea7af62751 Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Sat, 21 May 2022 18:27:38 -0500 Subject: [PATCH] simpler api proposal --- src/pixie.nim | 6 +++--- src/pixie/fileformats/png.nim | 6 +----- src/pixie/fileformats/qoi.nim | 6 +----- tests/test_paints.nim | 10 +++++----- tests/test_qoi.nim | 10 +++++----- 5 files changed, 15 insertions(+), 23 deletions(-) diff --git a/src/pixie.nim b/src/pixie.nim index d5abcc3..d6eb77e 100644 --- a/src/pixie.nim +++ b/src/pixie.nim @@ -21,7 +21,7 @@ converter autoPremultipliedAlpha*(c: ColorRGBA): ColorRGBX {.inline, raises: []. proc decodeImage*(data: string): Image {.raises: [PixieError].} = ## Loads an image from memory. if data.len > 8 and data.readUint64(0) == cast[uint64](pngSignature): - decodePng(data) + newImage(decodePng(data)) elif data.len > 2 and data.readUint16(0) == cast[uint16](jpegStartOfImage): decodeJpeg(data) elif data.len > 2 and data.readStr(0, 2) == bmpSignature: @@ -32,7 +32,7 @@ proc decodeImage*(data: string): Image {.raises: [PixieError].} = elif data.len > 6 and data.readStr(0, 6) in gifSignatures: decodeGif(data) elif data.len > (14+8) and data.readStr(0, 4) == qoiSignature: - decodeQoi(data) + newImage(decodeQoi(data)) elif data.len > 9 and data.readStr(0, 2) in ppmSignatures: decodePpm(data) else: @@ -41,7 +41,7 @@ proc decodeImage*(data: string): Image {.raises: [PixieError].} = proc decodeMask*(data: string): Mask {.raises: [PixieError].} = ## Loads a mask from memory. if data.len > 8 and data.readUint64(0) == cast[uint64](pngSignature): - newMask(decodePng(data)) + newMask(newImage(decodePng(data))) else: raise newException(PixieError, "Unsupported mask file format") diff --git a/src/pixie/fileformats/png.nim b/src/pixie/fileformats/png.nim index fb96177..30e3442 100644 --- a/src/pixie/fileformats/png.nim +++ b/src/pixie/fileformats/png.nim @@ -342,7 +342,7 @@ proc newImage*(png: Png): Image {.raises: [PixieError].} = copyMem(result.data[0].addr, png.data[0].addr, png.data.len * 4) result.data.toPremultipliedAlpha() -proc decodePngRaw*(data: string): Png {.raises: [PixieError].} = +proc decodePng*(data: string): Png {.raises: [PixieError].} = ## Decodes the PNG data. if data.len < (8 + (8 + 13 + 4) + 4): # Magic bytes + IHDR + IEND failInvalid() @@ -450,10 +450,6 @@ proc decodePngRaw*(data: string): Png {.raises: [PixieError].} = result.channels = 4 result.data = decodeImageData(data, header, palette, transparency, idats) -proc decodePng*(data: string): Image {.raises: [PixieError].} = - ## Decodes the PNG data into an Image. - newImage(decodePngRaw(data)) - proc encodePng*( width, height, channels: int, data: pointer, len: int ): string {.raises: [PixieError].} = diff --git a/src/pixie/fileformats/qoi.nim b/src/pixie/fileformats/qoi.nim index 5e2b02e..887191b 100644 --- a/src/pixie/fileformats/qoi.nim +++ b/src/pixie/fileformats/qoi.nim @@ -35,7 +35,7 @@ func newImage*(qoi: Qoi): Image = copyMem(result.data[0].addr, qoi.data[0].addr, qoi.data.len * 4) result.data.toPremultipliedAlpha() -proc decodeQoiRaw*(data: string): Qoi {.raises: [PixieError].} = +proc decodeQoi*(data: string): Qoi {.raises: [PixieError].} = ## Decompress QOI file format data. if data.len <= 14 or data[0 .. 3] != qoiSignature: raise newException(PixieError, "Invalid QOI header") @@ -121,10 +121,6 @@ proc decodeQoiRaw*(data: string): Qoi {.raises: [PixieError].} = raise newException(PixieError, "Invalid QOI padding") inc(p) -proc decodeQoi*(data: string): Image {.raises: [PixieError].} = - ## Decodes data in the QOI file format to an `Image`. - newImage(decodeQoiRaw(data)) - proc encodeQoi*(qoi: Qoi): string {.raises: [PixieError].} = ## Encodes raw QOI pixels to the QOI file format. diff --git a/tests/test_paints.nim b/tests/test_paints.nim index f0a800a..8a347c4 100644 --- a/tests/test_paints.nim +++ b/tests/test_paints.nim @@ -1,4 +1,4 @@ -import chroma, pixie, pixie/fileformats/png, vmath +import chroma, pixie, vmath const heartShape = """ M 10,30 @@ -18,7 +18,7 @@ block: block: let paint = newPaint(ImagePaint) - paint.image = decodePng(readFile("tests/fileformats/png/mandrill.png")) + paint.image = readImage("tests/fileformats/png/mandrill.png") paint.imageMat = scale(vec2(0.2, 0.2)) let image = newImage(100, 100) @@ -27,7 +27,7 @@ block: block: let paint = newPaint(ImagePaint) - paint.image = decodePng(readFile("tests/fileformats/png/mandrill.png")) + paint.image = readImage("tests/fileformats/png/mandrill.png") paint.imageMat = scale(vec2(0.2, 0.2)) paint.opacity = 0.5 @@ -37,7 +37,7 @@ block: block: let paint = newPaint(TiledImagePaint) - paint.image = decodePng(readFile("tests/fileformats/png/mandrill.png")) + paint.image = readImage("tests/fileformats/png/mandrill.png") paint.imageMat = scale(vec2(0.02, 0.02)) let image = newImage(100, 100) @@ -46,7 +46,7 @@ block: block: let paint = newPaint(TiledImagePaint) - paint.image = decodePng(readFile("tests/fileformats/png/mandrill.png")) + paint.image = readImage("tests/fileformats/png/mandrill.png") paint.imageMat = scale(vec2(0.02, 0.02)) paint.opacity = 0.5 diff --git a/tests/test_qoi.nim b/tests/test_qoi.nim index 88b9ff3..b8acd0d 100644 --- a/tests/test_qoi.nim +++ b/tests/test_qoi.nim @@ -1,17 +1,17 @@ -import pixie, pixie/fileformats/png, pixie/fileformats/qoi +import pixie, pixie/fileformats/qoi const tests = ["testcard", "testcard_rgba"] for name in tests: let - input = decodeQoi(readFile("tests/fileformats/qoi/" & name & ".qoi")) - control = decodePng(readFile("tests/fileformats/qoi/" & name & ".png")) + input = readImage("tests/fileformats/qoi/" & name & ".qoi") + control = readImage("tests/fileformats/qoi/" & name & ".png") doAssert input.data == control.data, "input mismatch of " & name discard encodeQoi(control) for name in tests: let - input = decodeQoiRaw(readFile("tests/fileformats/qoi/" & name & ".qoi")) - output = decodeQoiRaw(encodeQoi(input)) + input = decodeQoi(readFile("tests/fileformats/qoi/" & name & ".qoi")) + output = decodeQoi(encodeQoi(input)) doAssert output.data.len == input.data.len doAssert output.data == input.data