mirror of
https://github.com/status-im/nimPNG.git
synced 2025-01-28 21:34:47 +00:00
update readme.md and add savePNG
This commit is contained in:
parent
4bdd8bde6e
commit
f2c947d5e2
33
nimPNG.nim
33
nimPNG.nim
@ -2937,27 +2937,22 @@ proc writeChunks*(png: PNG, s: Stream) =
|
|||||||
s.write chunk.data
|
s.write chunk.data
|
||||||
s.writeInt32BE cast[int](chunk.crc)
|
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 =
|
proc savePNG32*(fileName, input: string, w, h: int): bool =
|
||||||
try:
|
result = savePNG(fileName, input, LCT_RGBA, 8, w, h)
|
||||||
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
|
|
||||||
|
|
||||||
proc savePNG24*(fileName, input: string, w, h: int): bool =
|
proc savePNG24*(fileName, input: string, w, h: int): bool =
|
||||||
try:
|
result = savePNG(fileName, input, LCT_RGB, 8, w, h)
|
||||||
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
|
|
||||||
|
|
||||||
proc getFilterTypesInterlaced(png: PNG): seq[string] =
|
proc getFilterTypesInterlaced(png: PNG): seq[string] =
|
||||||
var header = PNGHeader(png.getChunk(IHDR))
|
var header = PNGHeader(png.getChunk(IHDR))
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[Package]
|
[Package]
|
||||||
name = "nimPNG"
|
name = "nimPNG"
|
||||||
version = "0.1.1"
|
version = "0.1.2"
|
||||||
author = "Andri Lim"
|
author = "Andri Lim"
|
||||||
description = "PNG encoder and decoder"
|
description = "PNG encoder and decoder"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
42
readme.md
42
readme.md
@ -1,6 +1,5 @@
|
|||||||
#nimPNG
|
#nimPNG
|
||||||
Portable Network Graphics Encoder and Decoder written in Nim
|
Portable Network Graphics Encoder and Decoder written in Nim store lossless image with good compression
|
||||||
store lossless image with good compression
|
|
||||||
|
|
||||||
all PNG standard color mode are supported:
|
all PNG standard color mode are supported:
|
||||||
|
|
||||||
@ -43,5 +42,42 @@ Supported color conversions:
|
|||||||
- removing alpha channel
|
- removing alpha channel
|
||||||
- higher to smaller bitdepth, and vice versa
|
- higher to smaller bitdepth, and vice versa
|
||||||
|
|
||||||
###Feature planned:
|
###Planned Feature(s):
|
||||||
- streaming for progressive loading
|
- 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
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user