diff --git a/apidoc.md b/apidoc.md index ea3266a..88b1ce8 100644 --- a/apidoc.md +++ b/apidoc.md @@ -1,35 +1,35 @@ ## Legacy API ```Nim -proc encodePNG*(input: string, colorType: PNGColorType, bitDepth, w, h: int, settings = PNGEncoder(nil)): PNG -proc encodePNG32*(input: string, w, h: int): PNG -proc encodePNG24*(input: string, w, h: int): PNG +encodePNG(input: string, colorType: PNGColorType, bitDepth, w, h: int, settings = PNGEncoder(nil)): PNG +encodePNG32(input: string, w, h: int): PNG +encodePNG24(input: string, w, h: int): PNG when not defined(js): - proc savePNG*(fileName, input: string, colorType: PNGColorType, bitDepth, w, h: int): bool - proc savePNG32*(fileName, input: string, w, h: int): bool - proc savePNG24*(fileName, input: string, w, h: int): bool + savePNG(fileName, input: string, colorType: PNGColorType, bitDepth, w, h: int): bool + savePNG32(fileName, input: string, w, h: int): bool + savePNG24(fileName, input: string, w, h: int): bool -proc prepareAPNG*(colorType: PNGColorType, bitDepth, numPlays: int, settings = PNGEncoder(nil)): PNG -proc prepareAPNG24*(numPlays = 0): PNG -proc prepareAPNG32*(numPlays = 0): PNG -proc addDefaultImage*(png: PNG, input: string, width, height: int, ctl = APNGFrameControl(nil)): bool -proc addFrame*(png: PNG, frame: string, ctl: APNGFrameControl): bool -proc encodeAPNG*(png: PNG): string +prepareAPNG(colorType: PNGColorType, bitDepth, numPlays: int, settings = PNGEncoder(nil)): PNG +prepareAPNG24(numPlays = 0): PNG +prepareAPNG32(numPlays = 0): PNG +addDefaultImage(png: PNG, input: string, width, height: int, ctl = APNGFrameControl(nil)): bool +addFrame(png: PNG, frame: string, ctl: APNGFrameControl): bool +encodeAPNG(png: PNG): string when not defined(js): - proc saveAPNG*(png: PNG, fileName: string): bool + saveAPNG(png: PNG, fileName: string): bool -proc decodePNG*(s: Stream, colorType: PNGColorType, bitDepth: int, settings = PNGDecoder(nil)): PNGResult -proc decodePNG*(s: Stream, settings = PNGDecoder(nil)): PNG +decodePNG(s: Stream, colorType: PNGColorType, bitDepth: int, settings = PNGDecoder(nil)): PNGResult +decodePNG(s: Stream, settings = PNGDecoder(nil)): PNG when not defined(js): - proc loadPNG*(fileName: string, colorType: PNGColorType, bitDepth: int, settings: PNGDecoder = nil): PNGResult - proc loadPNG32*(fileName: string, settings = PNGDecoder(nil)): PNGResult - proc loadPNG24*(fileName: string, settings = PNGDecoder(nil)): PNGResult + loadPNG(fileName: string, colorType: PNGColorType, bitDepth: int, settings: PNGDecoder = nil): PNGResult + loadPNG32(fileName: string, settings = PNGDecoder(nil)): PNGResult + loadPNG24(fileName: string, settings = PNGDecoder(nil)): PNGResult -proc decodePNG32*(input: string, settings = PNGDecoder(nil)): PNGResult -proc decodePNG24*(input: string, settings = PNGDecoder(nil)): PNGResult +decodePNG32(input: string, settings = PNGDecoder(nil)): PNGResult +decodePNG24(input: string, settings = PNGDecoder(nil)): PNGResult ``` @@ -37,19 +37,46 @@ proc decodePNG24*(input: string, settings = PNGDecoder(nil)): PNGResult ```Nim -proc decodePNG*(T: type, s: Stream, colorType: PNGColorType, bitDepth: int, settings = PNGDecoder(nil)): PNGResult[T] -proc decodePNG*(T: type, s: Stream, settings = PNGDecoder(nil)): PNG +# generic version accept T = `string`, `seq[uint8]` +encodePNG(input: T, w, h: int, settings = PNGEncoder(nil)): PNG[T] +encodePNG(input: T, colorType: PNGColorType, bitDepth, w, h: int, settings = PNGEncoder(nil)): PNG[T] +encodePNG32(input: T, w, h: int): PNG[T] +encodePNG24(input: T, w, h: int): PNG[T] +writeChunks(png: PNG[T], s: Stream) + +type + PNGStatus* = Result[void, string] + PNGBytes*[T] = Result[T, string] + +prepareAPNG(T: type, colorType: PNGColorType, bitDepth, numPlays: int, settings = PNGEncoder(nil)): PNG[T] +prepareAPNG24(T: type, numPlays = 0): PNG[T] +prepareAPNG32(T: type, numPlays = 0): PNG[T] +addDefaultImage(png: PNG[T], input: T, width, height: int, ctl = APNGFrameControl(nil)): bool +addFrame(png: PNG[T], frame: T, ctl: APNGFrameControl): bool + +when not defined(js): + savePNG(fileName: string, input: T, colorType: PNGColorType, bitDepth, w, h: int): PNGStatus + savePNG32(fileName: string, input: T, w, h: int): PNGStatus + savePNG24(fileName: string, input: T, w, h: int): PNGStatus + +encodeAPNG(png: PNG[T]): PNGBytes[T] + +when not defined(js): + saveAPNG(png: PNG[T], fileName: string): PNGStatus + +decodePNG(T: type, s: Stream, colorType: PNGColorType, bitDepth: int, settings = PNGDecoder(nil)): PNGResult[T] +decodePNG(T: type, s: Stream, settings = PNGDecoder(nil)): PNG type PNGRes*[T] = Result[PNGResult[T], string] when not defined(js): - proc loadPNG*(T: type, fileName: string, colorType: PNGColorType, bitDepth: int, settings: PNGDecoder = nil): PNGRes[T] - proc loadPNG32*(T: type, fileName: string, settings = PNGDecoder(nil)): PNGRes[T] - proc loadPNG24*(T: type, fileName: string, settings = PNGDecoder(nil)): PNGRes[T] + loadPNG(T: type, fileName: string, colorType: PNGColorType, bitDepth: int, settings: PNGDecoder = nil): PNGRes[T] + loadPNG32(T: type, fileName: string, settings = PNGDecoder(nil)): PNGRes[T] + loadPNG24(T: type, fileName: string, settings = PNGDecoder(nil)): PNGRes[T] -proc decodePNG32*(T: type, input: T, settings = PNGDecoder(nil)): PNGRes[T] -proc decodePNG24*(T: type, input: T, settings = PNGDecoder(nil)): PNGRes[T] +decodePNG32(input: T, settings = PNGDecoder(nil)): PNGRes[T] +decodePNG24(input: T, settings = PNGDecoder(nil)): PNGRes[T] ``` ## How to use PNGRes? diff --git a/nimPNG.nim b/nimPNG.nim index 5a598f8..0976958 100644 --- a/nimPNG.nim +++ b/nimPNG.nim @@ -1871,7 +1871,7 @@ when not defined(js): proc loadPNG24*(T: type, fileName: string, settings = PNGDecoder(nil)): PNGRes[T] = loadPNG(T, fileName, LCT_RGB, 8, settings) -proc decodePNG32*(T: type, input: T, settings = PNGDecoder(nil)): PNGRes[T] = +proc decodePNG32Impl*[T](input: T, settings = PNGDecoder(nil)): PNGRes[T] = try: when T is string: var s = newStringStream(input) @@ -1884,7 +1884,7 @@ proc decodePNG32*(T: type, input: T, settings = PNGDecoder(nil)): PNGRes[T] = except PNGError, IOError, NZError: result.err(getCurrentExceptionMsg()) -proc decodePNG24*(T: type, input: T, settings = PNGDecoder(nil)): PNGRes[T] = +proc decodePNG24Impl*[T](input: T, settings = PNGDecoder(nil)): PNGRes[T] = try: when T is string: var s = newStringStream(input) @@ -1920,16 +1920,28 @@ when not defined(js): if res.isOk: result = res.get() else: debugEcho res.error() -proc decodePNG32*(input: string, settings = PNGDecoder(nil)): PNGResult[string] = - let res = decodePNG32(string, input, settings) +proc decodePNG32Legacy*(input: string, settings = PNGDecoder(nil)): PNGResult[string] = + let res = decodePNG32Impl(input, settings) if res.isOk: result = res.get() else: debugEcho res.error() -proc decodePNG24*(input: string, settings = PNGDecoder(nil)): PNGResult[string] = - let res = decodePNG24(string, input, settings) +proc decodePNG24Legacy*(input: string, settings = PNGDecoder(nil)): PNGResult[string] = + let res = decodePNG24Impl(input, settings) if res.isOk: result = res.get() else: debugEcho res.error() +template decodePNG32*[T](input: T, settings = PNGDecoder(nil)): untyped = + when T is string: + decodePNG32Legacy(input, settings) + else: + decodePNG32Impl(input, settings) + +template decodePNG24*[T](input: T, settings = PNGDecoder(nil)): untyped = + when T is string: + decodePNG24Legacy(input, settings) + else: + decodePNG24Impl(input, settings) + #Encoder/Decoder demarcation line----------------------------- type