nimPNG/tests/test_suite.nim

40 lines
1.1 KiB
Nim
Raw Normal View History

2020-04-10 05:54:40 +00:00
import streams, os, strutils, ../nimPNG, ./minibmp
2015-08-28 15:56:55 +00:00
2018-10-09 03:50:02 +00:00
proc loadPNG2BMP(fileName: string): BMP =
2015-09-03 04:56:22 +00:00
var settings = makePNGDecoder()
2015-08-28 15:56:55 +00:00
settings.readTextChunks = true
var png = loadPNG24(fileName, settings)
if png == nil: return nil
let size = png.width * png.height
result = newBMP(png.width, png.height)
for i in 0..size-1:
let px = i * 3
result.data[px] = png.data[px]
result.data[px + 1] = png.data[px + 1]
result.data[px + 2] = png.data[px + 2]
2018-10-09 03:50:02 +00:00
proc toString(bmp: BMP): string =
2015-08-28 15:56:55 +00:00
var s = newStringStream()
2018-08-30 15:16:32 +00:00
s.writeBMP(bmp)
2015-08-28 15:56:55 +00:00
result = s.data
proc convert(dir: string) =
for fileName in walkDirRec(dir, {pcFile}):
let path = splitFile(fileName)
if path.ext.len() == 0: continue
2016-08-24 02:56:11 +00:00
let ext = toLowerAscii(path.ext)
2015-08-28 15:56:55 +00:00
if ext != ".png": continue
if path.name[0] == 'x': continue
2017-11-24 03:42:14 +00:00
2020-04-10 05:54:40 +00:00
let bmpName = path.dir / path.name & ExtSep & "bmp"
2017-11-24 03:42:14 +00:00
2015-08-28 15:56:55 +00:00
echo fileName, " vs. ", bmpName
2018-10-09 03:50:02 +00:00
var bmp = loadPNG2BMP(fileName)
2015-08-28 15:56:55 +00:00
if bmp != nil:
2018-10-09 03:50:02 +00:00
let data1 = bmp.toString()
2015-08-28 15:56:55 +00:00
let data2 = readFile(bmpName)
assert data1 == data2
2020-04-10 05:54:40 +00:00
convert("tests" / "suite")