update readme.md and add savePNG

This commit is contained in:
jangko 2015-09-30 12:45:24 +07:00
parent 4bdd8bde6e
commit f2c947d5e2
3 changed files with 54 additions and 23 deletions

View File

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

View File

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

View File

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