simpler api proposal

This commit is contained in:
Ryan Oldenburg 2022-05-21 18:27:38 -05:00
parent b79a31fabf
commit 3c810c63de
5 changed files with 15 additions and 23 deletions

View file

@ -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")

View file

@ -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].} =

View file

@ -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.

View file

@ -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

View file

@ -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