mirror of https://github.com/status-im/nimPNG.git
commit
8f50614b0c
|
@ -45,4 +45,8 @@ Temporary Items
|
||||||
*.exe
|
*.exe
|
||||||
nimcache
|
nimcache
|
||||||
bug
|
bug
|
||||||
tester/temp.png
|
tester/temp.png
|
||||||
|
tester/rainbow.png
|
||||||
|
tester/test
|
||||||
|
tester/testCodec
|
||||||
|
tester/testSuite
|
20
nimPNG.nim
20
nimPNG.nim
|
@ -26,7 +26,7 @@
|
||||||
#-------------------------------------
|
#-------------------------------------
|
||||||
|
|
||||||
import streams, endians, tables, hashes, math
|
import streams, endians, tables, hashes, math
|
||||||
import private.buffer, private.nimz
|
import private/buffer, private/nimz
|
||||||
|
|
||||||
const
|
const
|
||||||
NIM_PNG_VERSION = "0.2.1"
|
NIM_PNG_VERSION = "0.2.1"
|
||||||
|
@ -330,16 +330,14 @@ proc copyTo*(src, dest: PNGColorMode) =
|
||||||
dest.colorType = src.colorType
|
dest.colorType = src.colorType
|
||||||
dest.bitDepth = src.bitDepth
|
dest.bitDepth = src.bitDepth
|
||||||
dest.paletteSize = src.paletteSize
|
dest.paletteSize = src.paletteSize
|
||||||
if src.palette != nil:
|
newSeq(dest.palette, src.paletteSize)
|
||||||
newSeq(dest.palette, src.paletteSize)
|
for i in 0..src.palette.len-1: dest.palette[i] = src.palette[i]
|
||||||
for i in 0..src.palette.len-1: dest.palette[i] = src.palette[i]
|
|
||||||
|
|
||||||
proc newColorMode*(mode: PNGColorMode): PNGColorMode =
|
proc newColorMode*(mode: PNGColorMode): PNGColorMode =
|
||||||
new(result)
|
new(result)
|
||||||
mode.copyTo(result)
|
mode.copyTo(result)
|
||||||
|
|
||||||
proc addPalette*(mode: PNGColorMode, r, g, b, a: int) =
|
proc addPalette*(mode: PNGColorMode, r, g, b, a: int) =
|
||||||
if mode.palette == nil: mode.palette = @[]
|
|
||||||
mode.palette.add RGBA8(r: chr(r), g: chr(g), b: chr(b), a: chr(a))
|
mode.palette.add RGBA8(r: chr(r), g: chr(g), b: chr(b), a: chr(a))
|
||||||
mode.paletteSize = mode.palette.len
|
mode.paletteSize = mode.palette.len
|
||||||
|
|
||||||
|
@ -1938,7 +1936,7 @@ proc processingAPNG(apng: APNG, colorType: PNGcolorType, bitDepth: int) =
|
||||||
if apng.png.firstFrameIsDefaultImage:
|
if apng.png.firstFrameIsDefaultImage:
|
||||||
start = 1
|
start = 1
|
||||||
# IDAT already processed, so we add a dummy here
|
# IDAT already processed, so we add a dummy here
|
||||||
frameData.add string(nil)
|
frameData.add ""
|
||||||
|
|
||||||
for x in apng.png.apngChunks:
|
for x in apng.png.apngChunks:
|
||||||
if x.chunkType == fcTL:
|
if x.chunkType == fcTL:
|
||||||
|
@ -2155,7 +2153,7 @@ proc makePNGEncoder*(): PNGEncoder =
|
||||||
s.modeIn = newColorMode()
|
s.modeIn = newColorMode()
|
||||||
s.modeOut = newColorMode()
|
s.modeOut = newColorMode()
|
||||||
s.forcePalette = false
|
s.forcePalette = false
|
||||||
s.predefinedFilters = nil
|
s.predefinedFilters = ""
|
||||||
s.addID = false
|
s.addID = false
|
||||||
s.textCompression = true
|
s.textCompression = true
|
||||||
s.interlaceMethod = IM_NONE
|
s.interlaceMethod = IM_NONE
|
||||||
|
@ -3113,7 +3111,7 @@ proc addChunkacTL(png: PNG, numFrames, numPlays: int) =
|
||||||
|
|
||||||
proc addChunkfcTL(png: PNG, chunk: APNGFrameControl, sequenceNumber: int) =
|
proc addChunkfcTL(png: PNG, chunk: APNGFrameControl, sequenceNumber: int) =
|
||||||
chunk.chunkType = fcTL
|
chunk.chunkType = fcTL
|
||||||
if chunk.data.isNil:
|
if chunk.data == "":
|
||||||
chunk.data = newStringOfCap(26)
|
chunk.data = newStringOfCap(26)
|
||||||
chunk.sequenceNumber = sequenceNumber
|
chunk.sequenceNumber = sequenceNumber
|
||||||
png.chunks.add chunk
|
png.chunks.add chunk
|
||||||
|
@ -3319,7 +3317,7 @@ proc prepareAPNG*(colorType: PNGcolorType, bitDepth, numPlays: int, settings = P
|
||||||
png.isAPNG = true
|
png.isAPNG = true
|
||||||
png.apngChunks = @[]
|
png.apngChunks = @[]
|
||||||
png.apngPixels = @[]
|
png.apngPixels = @[]
|
||||||
png.pixels = nil
|
png.pixels = ""
|
||||||
png.firstFrameIsDefaultImage = false
|
png.firstFrameIsDefaultImage = false
|
||||||
png.width = 0
|
png.width = 0
|
||||||
png.height = 0
|
png.height = 0
|
||||||
|
@ -3337,7 +3335,7 @@ proc addDefaultImage*(png: PNG, input: string, width, height: int, ctl = APNGFra
|
||||||
png.firstFrameIsDefaultImage = ctl != nil
|
png.firstFrameIsDefaultImage = ctl != nil
|
||||||
if ctl != nil:
|
if ctl != nil:
|
||||||
png.apngChunks.add ctl
|
png.apngChunks.add ctl
|
||||||
png.apngPixels.add nil # add dummy
|
png.apngPixels.add "" # add dummy
|
||||||
result = result and (ctl.xOffset == 0)
|
result = result and (ctl.xOffset == 0)
|
||||||
result = result and (ctl.yOffset == 0)
|
result = result and (ctl.yOffset == 0)
|
||||||
result = result and (ctl.width == width)
|
result = result and (ctl.width == width)
|
||||||
|
@ -3375,7 +3373,7 @@ proc encodeAPNG*(png: PNG): string =
|
||||||
result = s.data
|
result = s.data
|
||||||
except:
|
except:
|
||||||
debugEcho getCurrentExceptionMsg()
|
debugEcho getCurrentExceptionMsg()
|
||||||
result = nil
|
result = ""
|
||||||
|
|
||||||
when not defined(js):
|
when not defined(js):
|
||||||
proc saveAPNG*(png: PNG, fileName: string): bool =
|
proc saveAPNG*(png: PNG, fileName: string): bool =
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
# Package
|
# Package
|
||||||
version = "0.2.2"
|
version = "0.2.3"
|
||||||
author = "Andri Lim"
|
author = "Andri Lim"
|
||||||
description = "PNG encoder and decoder"
|
description = "PNG encoder and decoder"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
|
@ -20,7 +20,11 @@ proc subbuffer*[T](b: Buffer[T], offset: int): Buffer[T] =
|
||||||
shallowCopy(result.data, b.data)
|
shallowCopy(result.data, b.data)
|
||||||
result.offset = b.offset + offset
|
result.offset = b.offset + offset
|
||||||
|
|
||||||
template isNil*[T](b: Buffer[T]): bool = b.data.isNil
|
template isNil*[T](b: Buffer[T]): bool =
|
||||||
|
when T is (string or seq):
|
||||||
|
b.data.len == 0
|
||||||
|
else:
|
||||||
|
b.data.isNil
|
||||||
|
|
||||||
template copyElements*[T](dst: var Buffer[T], src: Buffer[T], count: int) =
|
template copyElements*[T](dst: var Buffer[T], src: Buffer[T], count: int) =
|
||||||
when defined(js):
|
when defined(js):
|
||||||
|
|
|
@ -19,7 +19,7 @@ proc newBMP*(w, h: int): BMP =
|
||||||
result.height = h
|
result.height = h
|
||||||
result.data = newString(w * h * 3)
|
result.data = newString(w * h * 3)
|
||||||
|
|
||||||
proc write*(s: Stream, bmp: BMP) =
|
proc writeBMP*(s: Stream, bmp: BMP) =
|
||||||
let stride = 4 * ((bmp.width * 24 + 31) div 32)
|
let stride = 4 * ((bmp.width * 24 + 31) div 32)
|
||||||
let imageData = stride * bmp.height
|
let imageData = stride * bmp.height
|
||||||
let offset = 54
|
let offset = 54
|
||||||
|
@ -43,7 +43,7 @@ proc write*(s: Stream, bmp: BMP) =
|
||||||
|
|
||||||
let bytesPerRow = bmp.width * 3
|
let bytesPerRow = bmp.width * 3
|
||||||
let paddingLen = stride - bytesPerRow
|
let paddingLen = stride - bytesPerRow
|
||||||
let padding = if paddingLen > 0: newString(paddingLen) else: nil
|
let padding = if paddingLen > 0: newString(paddingLen) else: ""
|
||||||
|
|
||||||
for i in 0..bmp.height-1:
|
for i in 0..bmp.height-1:
|
||||||
s.writeData(addr(bmp.data[i * bytesPerRow]), bytesPerRow)
|
s.writeData(addr(bmp.data[i * bytesPerRow]), bytesPerRow)
|
||||||
|
|
|
@ -2,11 +2,11 @@ import nimPNG, streams, minibmp, os, strutils
|
||||||
|
|
||||||
proc write(bmp: BMP): string =
|
proc write(bmp: BMP): string =
|
||||||
var s = newStringStream()
|
var s = newStringStream()
|
||||||
s.write(bmp)
|
s.writeBMP(bmp)
|
||||||
result = s.data
|
result = s.data
|
||||||
|
|
||||||
proc toBMP(png: PNGResult, fileName: string) =
|
proc toBMP(png: PNGResult, fileName: string) =
|
||||||
if png.frames != nil:
|
if png.frames != @[]:
|
||||||
var frame = 0
|
var frame = 0
|
||||||
for x in png.frames:
|
for x in png.frames:
|
||||||
var bmp = newBMP(x.ctl.width, x.ctl.height)
|
var bmp = newBMP(x.ctl.width, x.ctl.height)
|
||||||
|
|
|
@ -15,7 +15,7 @@ proc loadPNG(fileName: string): BMP =
|
||||||
|
|
||||||
proc write(bmp: BMP): string =
|
proc write(bmp: BMP): string =
|
||||||
var s = newStringStream()
|
var s = newStringStream()
|
||||||
s.write(bmp)
|
s.writeBMP(bmp)
|
||||||
result = s.data
|
result = s.data
|
||||||
|
|
||||||
proc convert(dir: string) =
|
proc convert(dir: string) =
|
||||||
|
|
Loading…
Reference in New Issue