Example:
import src/fastpnm import std/[os, strformat] # converting an image to `.pbm` format via `convert` tool (I think it's part of `ImageMagick` app) discard execShellCmd fmt"convert -flatten -background white -alpha remove -resize 32x32 ./examples/arrow.png ./examples/play4.pbm" # parsing a .pbm file let pbm4 = parsePnm readFile "./examples/play4.pbm" # validate assert pbm4.magic == P4 assert pbm4.width == 32 assert pbm4.height == 32 assert pbm4.getBool(10, 10) assert not pbm4.getBool(0, 0)
Types
Color = tuple[r, g, b: uint8]
Pnm = object magic*: PnmMagic width*, height*, maxValue*: Natural comment*: string data*: seq[byte] filled*: Natural
PnmMagic = enum bitMapRaw = "P1", grayMapRaw = "P2", pixMapRaw = "P3", bitMapBinray = "P4", grayMapBinray = "P5", pixMapBinray = "P6"
Position = tuple[x, y: int]
Consts
bitMap = {bitMapRaw, bitMapBinray}
commonPnmExt = ".pnm"
compressed = {bitMapBinray..pixMapBinray}
grayMap = {grayMapRaw, grayMapBinray}
P1 = bitMapRaw
P2 = grayMapRaw
P3 = pixMapRaw
P4 = bitMapBinray
P5 = grayMapBinray
P6 = pixMapBinray
pixMap = {pixMapRaw, pixMapBinray}
uncompressed = {bitMapRaw..pixMapRaw}
Procs
func `$`(pnm: Pnm; dropWhiteSpaces = false; addComments = true): string {. ...raises: [], tags: [].}
- convert the .pnm, .pbm, .pgm, .ppm file to its string representation
func fileExt(magic: PnmMagic): string {....raises: [], tags: [].}
- returns file extension according to the magic
func get2d[T: bool or SomeInteger or Color](pnm: Pnm): seq[seq[T]]
- generates 2D representaion of stored data
func getBool(pnm: Pnm; x, y: int): bool {....raises: [], tags: [].}
- only applicable when pnm.magic is P1 or P4
func getColor(pnm: Pnm; x, y: int): Color {....raises: [], tags: [].}
- only applicable when pnm.magic is P3 or P6
func getGrayScale(pnm: Pnm; x, y: int): uint8 {....raises: [], tags: [].}
- only applicable when pnm.magic is P2 or P5
func parsePnm(s: string; captureComments = false): Pnm {....raises: [ValueError], tags: [].}
- parses your .pnm, .pbm, .pgm, .ppm files
func setBool(pnm: var Pnm; x, y: int; b: bool) {....raises: [], tags: [].}
- only applicable when pnm.magic is P1 or P4
func setColor(pnm: var Pnm; x, y: int; color: Color) {....raises: [], tags: [].}
- only applicable when pnm.magic is P3 or P6
func setGrayScale(pnm: var Pnm; x, y: int; g: uint8): uint8 {....raises: [], tags: [].}
- only applicable when pnm.magic is P2 or P5
func toCompressed(m: PnmMagic): PnmMagic {....raises: [], tags: [].}
func toUnCompressed(m: PnmMagic): PnmMagic {....raises: [], tags: [].}