From f2c947d5e2e2dafd16bf450ccfb761e68cb447fd Mon Sep 17 00:00:00 2001 From: jangko Date: Wed, 30 Sep 2015 12:45:24 +0700 Subject: [PATCH] update readme.md and add savePNG --- nimPNG.nim | 33 ++++++++++++++------------------- nimPNG.nimble | 2 +- readme.md | 42 +++++++++++++++++++++++++++++++++++++++--- 3 files changed, 54 insertions(+), 23 deletions(-) diff --git a/nimPNG.nim b/nimPNG.nim index b7039f5..8ff8a69 100644 --- a/nimPNG.nim +++ b/nimPNG.nim @@ -2937,27 +2937,22 @@ proc writeChunks*(png: PNG, s: Stream) = s.write chunk.data s.writeInt32BE cast[int](chunk.crc) +proc savePNG*(fileName, input: string, colorType: PNGcolorType, bitDepth, w, h: int): bool = + try: + var png = encodePNG(input, colorType, bitDepth, w, h) + var s = newFileStream(fileName, fmWrite) + png.writeChunks s + s.close() + result = true + except: + debugEcho getCurrentExceptionMsg() + result = false + proc savePNG32*(fileName, input: string, w, h: int): bool = - try: - var png = encodePNG(input, LCT_RGBA, 8, w, h) - var s = newFileStream(fileName, fmWrite) - png.writeChunks s - s.close() - result = true - except: - debugEcho getCurrentExceptionMsg() - result = false - + result = savePNG(fileName, input, LCT_RGBA, 8, w, h) + proc savePNG24*(fileName, input: string, w, h: int): bool = - try: - var png = encodePNG(input, LCT_RGB, 8, w, h) - var s = newFileStream(fileName, fmWrite) - png.writeChunks s - s.close() - result = true - except: - debugEcho getCurrentExceptionMsg() - result = false + result = savePNG(fileName, input, LCT_RGB, 8, w, h) proc getFilterTypesInterlaced(png: PNG): seq[string] = var header = PNGHeader(png.getChunk(IHDR)) diff --git a/nimPNG.nimble b/nimPNG.nimble index 72ff5d8..9c47b1d 100644 --- a/nimPNG.nimble +++ b/nimPNG.nimble @@ -1,6 +1,6 @@ [Package] name = "nimPNG" -version = "0.1.1" +version = "0.1.2" author = "Andri Lim" description = "PNG encoder and decoder" license = "MIT" diff --git a/readme.md b/readme.md index b2ffb9f..03a68e4 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,5 @@ #nimPNG -Portable Network Graphics Encoder and Decoder written in Nim -store lossless image with good compression +Portable Network Graphics Encoder and Decoder written in Nim store lossless image with good compression all PNG standard color mode are supported: @@ -43,5 +42,42 @@ Supported color conversions: - removing alpha channel - higher to smaller bitdepth, and vice versa -###Feature planned: +###Planned Feature(s): - streaming for progressive loading + +##Basic Usage +```nimrod +import nimPNG + +let png = loadPNG32("image.png") +#is equivalent to: +#let png = loadPNG("image.png", LCT_RGBA, 8) +#will produce rgba pixels: +#png.width -> width of the image +#png.height -> height of the image +#png.data -> pixels data in RGBA format +``` + +if you already have the whole file in memory +let png = decodePNG32(raw_bytes) +will do the same as above + +other variants: +loadPNG24 -> will produce pixels in RGB format 8 bpp +decodePNG24 -> load png from memory instead of file + +to create PNG: +savePNG32("output.png", rgba_pixels, width, height) +or savePNG24 +encodePNG32(rgba_pixels, width, height) +or encodePNG24 + +use **loadPNG** or **savePNG** if you need specific input/output format by supplying supported **colorType** and **bitDepth** information. Use **encodePNG** or **decodePNG** to do in memory encoding/decoding by supplying desired colorType and bitDepth information + +pixels are stored as raw bytes using Nim's string as container. +r1,g1,b1,a1,r2,g2,b2,a1,...,rn,gn,bn,an -> RGBA format 8 bit +r1,g1,b1,r2,g2,b2,...,rn,gn,bn -> RGB format 8 bit +grey1,grey2,grey3, ..., greyn -> GREY format 8 bit +grey1,alpha1,grey2,alpha2,...,greyn,alphan -> GREY ALPHA format 8 bit + +