Skip to content

Commit

Permalink
Merge pull request #34 from jangko/nim_1.0
Browse files Browse the repository at this point in the history
compile with Nim 1.0.0
  • Loading branch information
jangko authored Oct 4, 2019
2 parents 77cbfc3 + 149fffe commit 1cefc00
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 43 deletions.
16 changes: 8 additions & 8 deletions nimPNG.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2682,35 +2682,35 @@ proc filterScanLine(output: var DataBuf, scanLine, prevLine: DataBuf, len, byteW
of FLT_SUB:
for i in 0..byteWidth-1: output[i] = scanLine[i]
for i in byteWidth..len-1:
output[i] = chr(scanLine[i].uint - scanLine[i - byteWidth].uint)
output[i] = chr((scanLine[i].uint - scanLine[i - byteWidth].uint) and 0xFF)
of FLT_UP:
if not prevLine.isNil:
for i in 0..len-1:
output[i] = chr(scanLine[i].uint - prevLine[i].uint)
output[i] = chr((scanLine[i].uint - prevLine[i].uint) and 0xFF)
else:
for i in 0..len-1: output[i] = scanLine[i]
of FLT_AVERAGE:
if not prevLine.isNil:
for i in 0..byteWidth-1:
output[i] = chr(scanLine[i].uint - (prevLine[i].uint div 2))
output[i] = chr((scanLine[i].uint - (prevLine[i].uint div 2)) and 0xFF)
for i in byteWidth..len-1:
output[i] = chr(scanLine[i].uint - ((scanLine[i - byteWidth].uint + prevLine[i].uint) div 2))
output[i] = chr((scanLine[i].uint - ((scanLine[i - byteWidth].uint + prevLine[i].uint) div 2)) and 0xFF)
else:
for i in 0..byteWidth-1: output[i] = scanLine[i]
for i in byteWidth..len-1:
output[i] = chr(scanLine[i].uint - (scanLine[i - byteWidth].uint div 2))
output[i] = chr((scanLine[i].uint - (scanLine[i - byteWidth].uint div 2)) and 0xFF)
of FLT_PAETH:
if not prevLine.isNil:
#paethPredictor(0, prevLine[i], 0) is always prevLine[i]
for i in 0..byteWidth-1:
output[i] = chr(scanLine[i].uint - prevLine[i].uint)
output[i] = chr((scanLine[i].uint - prevLine[i].uint) and 0xFF)
for i in byteWidth..len-1:
output[i] = chr(scanLine[i].uint - paethPredictor(ord(scanLine[i - byteWidth]), ord(prevLine[i]), ord(prevLine[i - byteWidth])).uint)
output[i] = chr((scanLine[i].uint - paethPredictor(ord(scanLine[i - byteWidth]), ord(prevLine[i]), ord(prevLine[i - byteWidth])).uint) and 0xFF)
else:
for i in 0..byteWidth-1: output[i] = scanLine[i]
#paethPredictor(scanLine[i - byteWidth], 0, 0) is always scanLine[i - byteWidth]
for i in byteWidth..len-1:
output[i] = chr(scanLine[i].uint - scanLine[i - byteWidth].uint)
output[i] = chr((scanLine[i].uint - scanLine[i - byteWidth].uint) and 0xFF)

proc filterZero(output: var DataBuf, input: DataBuf, w, h, bpp: int) =
#the width of a scanline in bytes, not including the filter type
Expand Down
2 changes: 1 addition & 1 deletion nimPNG.nimble
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Package
version = "0.2.4"
version = "0.2.5"
author = "Andri Lim"
description = "PNG encoder and decoder"
license = "MIT"
Expand Down
62 changes: 31 additions & 31 deletions nimPNG/buffer.nim
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
import sequtils

type
Buffer*[T] = object ## Uses T as internal data buffer
data*: T
offset*: int
SeqBuffer*[T] = Buffer[seq[T]] ## Uses seq[T] as internal data buffer
Buffer*[T] = object ## Uses T as internal data buffer
data*: T
offset*: int
SeqBuffer*[T] = Buffer[seq[T]] ## Uses seq[T] as internal data buffer

template `[]`*[T](b: Buffer[T], i: int): auto = b.data[b.offset + i]
template `[]=`*[S, T](b: var Buffer[S], i: int, v: T) = b.data[b.offset + i] = v

proc init*[T](b: var Buffer[T], d: T) =
shallowCopy(b.data, d)
b.offset = 0
shallowCopy(b.data, d)
b.offset = 0

proc initBuffer*[T](d: T): Buffer[T] =
shallowCopy(result.data, d)
shallowCopy(result.data, d)

proc subbuffer*[T](b: Buffer[T], offset: int): Buffer[T] =
shallowCopy(result.data, b.data)
result.offset = b.offset + offset
shallowCopy(result.data, b.data)
result.offset = b.offset + offset

template isNil*[T](b: Buffer[T]): bool =
when T is (string or seq):
b.data.len == 0
else:
b.data.isNil
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) =
when defined(js):
for i in 0 ..< count: dst[i] = src[i]
else:
copyMem(addr dst[dst.offset], unsafeAddr src[src.offset], count * sizeof(dst[0]))
when defined(js):
for i in 0 ..< count: dst[i] = src[i]
else:
copyMem(addr dst[dst.offset], unsafeAddr src[src.offset], count * sizeof(dst[0]))

template zeroMem*[T](dst: var Buffer[T]) =
when defined(js):
applyIt(dst.data, type(dst[0])(0))
else:
zeroMem(addr dst.data[dst.offset], sizeof(dst[0]) * (dst.data.len - dst.offset))
when defined(js):
applyIt(dst.data, type(dst[0])(0))
else:
zeroMem(addr dst.data[dst.offset], sizeof(dst[0]) * (dst.data.len - dst.offset))

when isMainModule:
var buf: SeqBuffer[uint8]
buf.init(newSeq[uint8](1024))
buf[1] = 5
buf.offset = 1
echo buf[0].int

var strBuf = initBuffer(newString(20))
strBuf[1] = 'A'
let subBuf = strBuf.subbuffer(1)
echo subBuf[0] # Should print A
var buf: SeqBuffer[uint8]
buf.init(newSeq[uint8](1024))
buf[1] = 5
buf.offset = 1
echo buf[0].int

var strBuf = initBuffer(newString(20))
strBuf[1] = 'A'
let subBuf = strBuf.subbuffer(1)
echo subBuf[0] # Should print A
6 changes: 3 additions & 3 deletions tester/test.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ proc toBMP(png: PNGResult, fileName: string) =
bmp.data[px + 2] = chr(0xFF)
else:
let alpha = uint(x.data[px4 + 3])
bmp.data[px] = chr(uint8(255) + uint8(((x.data[px4 + 2].uint - 255'u) * alpha) shr 8))
bmp.data[px + 1] = chr(uint8(255) + uint8(((x.data[px4 + 1].uint - 255'u) * alpha) shr 8))
bmp.data[px + 2] = chr(uint8(255) + uint8(((x.data[px4 + 0].uint - 255'u) * alpha) shr 8))
bmp.data[px] = chr(uint8(255) + uint8((((x.data[px4 + 2].uint - 255'u) * alpha) shr 8) and 0xFF))
bmp.data[px + 1] = chr(uint8(255) + uint8((((x.data[px4 + 1].uint - 255'u) * alpha) shr 8) and 0xFF))
bmp.data[px + 2] = chr(uint8(255) + uint8((((x.data[px4 + 0].uint - 255'u) * alpha) shr 8) and 0xFF))

let bmpName = fileName & "_" & $frame & ".bmp"
#var s = newFileStream(bmpName, fmWrite)
Expand Down

0 comments on commit 1cefc00

Please sign in to comment.