Skip to content

Commit

Permalink
add missing openArray API
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Jun 11, 2020
1 parent e91cfff commit 0bd2060
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
20 changes: 16 additions & 4 deletions nimPNG.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2962,11 +2962,17 @@ proc encodePNG*[T](input: T, colorType: PNGColorType, bitDepth, w, h: int, setti
state.modeIn.bitDepth = bitDepth
result = encodePNG(input, w, h, state)

proc encodePNG32*[T](input: T, w, h: int): PNG[T] =
result = encodePNG(input, LCT_RGBA, 8, w, h)
template encodePNG32*[T](input: T, w, h: int): auto =
when T is openArray:
encodePNG(@(input), LCT_RGBA, 8, w, h)
else:
encodePNG(input, LCT_RGBA, 8, w, h)

proc encodePNG24*[T](input: T, w, h: int): PNG[T] =
result = encodePNG(input, LCT_RGB, 8, w, h)
template encodePNG24*[T](input: T, w, h: int): auto =
when T is openArray:
encodePNG(@(input), LCT_RGB, 8, w, h)
else:
encodePNG(input, LCT_RGB, 8, w, h)

proc writeChunks*[T](png: PNG[T], s: Stream) =
s.write PNGSignature
Expand Down Expand Up @@ -3115,18 +3121,24 @@ when not defined(js):
template savePNG*[T](fileName: string, input: T, colorType: PNGColorType, bitDepth, w, h: int): untyped =
when T is string:
savePNGLegacy(fileName, input, colorType, bitDepth, w , h)
elif T is openArray:
savePNGImpl(fileName, @(input), colorType, bitDepth, w , h)
else:
savePNGImpl(fileName, input, colorType, bitDepth, w , h)

template savePNG32*[T](fileName: string, input: T, w, h: int): untyped =
when T is string:
savePNG32Legacy(fileName, input, w, h)
elif T is openArray:
savePNG32Impl(fileName, @(input), w, h)
else:
savePNG32Impl(fileName, input, w, h)

template savePNG24*[T](fileName: string, input: T, w, h: int): untyped =
when T is string:
savePNG24Legacy(fileName, input, w, h)
elif T is openArray:
savePNG24Impl(fileName, @(input), w, h)
else:
savePNG24Impl(fileName, input, w, h)

Expand Down
13 changes: 11 additions & 2 deletions tests/test_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,17 @@ proc main() =
check savePNG32(subject32, png2.data, png2.width, png2.height).isOk() == true

test "decodePNG openArray[uint8]":
let png1 = decodePNG24(data.toOpenArrayByte(0, data.len-1))
let png2 = decodePNG32(data.toOpenArrayByte(0, data.len-1))
let res1 = decodePNG24(data.toOpenArrayByte(0, data.len-1))
let res2 = decodePNG32(data.toOpenArrayByte(0, data.len-1))

check res1.isOk() == true
check res2.isOk() == true

let png1 = res1.get()
let png2 = res2.get()

let im1 = encodePNG24(png1.data.toOpenArray(0, png1.data.len-1), png1.width, png1.height)
let im2 = encodePNG32(png2.data.toOpenArray(0, png2.data.len-1), png2.width, png2.height)

test "loadPNG string":
let png1 = loadPNG32(string, subject)
Expand Down

0 comments on commit 0bd2060

Please sign in to comment.