pixie/src/pixie.nim

91 lines
3 KiB
Nim
Raw Normal View History

2021-02-18 19:29:40 +00:00
import bumpy, chroma, flatty/binny, os, pixie/blends, pixie/common,
pixie/contexts, pixie/fileformats/bmp, pixie/fileformats/gif,
2021-05-19 05:04:39 +00:00
pixie/fileformats/jpg, pixie/fileformats/png, pixie/fileformats/svg,
2021-05-24 02:04:58 +00:00
pixie/fonts, pixie/images, pixie/masks, pixie/paints, pixie/paths, strutils, vmath
2020-11-21 04:34:57 +00:00
export blends, bumpy, chroma, common, contexts, fonts, images, masks, paints,
2021-05-19 05:04:39 +00:00
paths, vmath
2020-11-20 03:44:24 +00:00
2020-11-21 04:34:57 +00:00
type
FileFormat* = enum
2021-03-01 16:53:15 +00:00
ffPng, ffBmp, ffJpg, ffGif
2020-11-21 04:34:57 +00:00
2021-03-03 18:37:15 +00:00
converter autoStraightAlpha*(c: ColorRGBX): ColorRGBA {.inline.} =
## Convert a paremultiplied alpha RGBA to a straight alpha RGBA.
c.rgba()
converter autoPremultipliedAlpha*(c: ColorRGBA): ColorRGBX {.inline.} =
## Convert a straight alpha RGBA to a premultiplied alpha RGBA.
c.rgbx()
2020-11-21 05:09:52 +00:00
proc decodeImage*(data: string | seq[uint8]): Image =
2021-06-25 00:09:54 +00:00
## Loads an image from memory.
2020-11-21 23:45:02 +00:00
if data.len > 8 and data.readUint64(0) == cast[uint64](pngSignature):
decodePng(data)
elif data.len > 2 and data.readUint16(0) == cast[uint16](jpgStartOfImage):
decodeJpg(data)
2020-12-04 16:17:03 +00:00
elif data.len > 2 and data.readStr(0, 2) == bmpSignature:
2020-11-21 23:45:02 +00:00
decodeBmp(data)
2021-02-23 04:35:08 +00:00
elif data.len > 5 and
(data.readStr(0, 5) == xmlSignature or data.readStr(0, 4) == svgSignature):
decodeSvg(data)
2021-03-01 16:53:15 +00:00
elif data.len > 6 and data.readStr(0, 6) in gifSignatures:
decodeGif(data)
2020-11-21 23:45:02 +00:00
else:
raise newException(PixieError, "Unsupported image file format")
2020-11-21 04:34:57 +00:00
2021-06-25 00:09:54 +00:00
proc decodeMask*(data: string | seq[uint8]): Mask =
## Loads a mask from memory.
if data.len > 8 and data.readUint64(0) == cast[uint64](pngSignature):
newMask(decodePng(data))
else:
raise newException(PixieError, "Unsupported mask file format")
2020-11-21 04:34:57 +00:00
proc readImage*(filePath: string): Image =
## Loads an image from a file.
decodeImage(readFile(filePath))
2021-06-25 00:09:54 +00:00
proc readMask*(filePath: string): Mask =
## Loads a mask from a file.
decodeMask(readFile(filePath))
2020-11-21 05:09:52 +00:00
proc encodeImage*(image: Image, fileFormat: FileFormat): string =
2020-11-21 23:45:02 +00:00
## Encodes an image into memory.
2020-11-21 04:34:57 +00:00
case fileFormat:
of ffPng:
image.encodePng()
2020-11-21 23:45:02 +00:00
of ffJpg:
image.encodeJpg()
2020-11-21 04:34:57 +00:00
of ffBmp:
image.encodeBmp()
2021-03-01 16:53:15 +00:00
of ffGif:
2021-06-25 00:09:54 +00:00
raise newException(PixieError, "Unsupported file format")
proc encodeMask*(mask: Mask, fileFormat: FileFormat): string =
## Encodes a mask into memory.
case fileFormat:
of ffPng:
mask.encodePng()
else:
raise newException(PixieError, "Unsupported file format")
2020-11-21 04:34:57 +00:00
2020-11-21 18:34:57 +00:00
proc writeFile*(image: Image, filePath: string) =
## Writes an image to a file.
2021-04-27 06:52:05 +00:00
let fileFormat = case splitFile(filePath).ext.toLowerAscii():
2020-11-22 02:02:57 +00:00
of ".png": ffPng
of ".bmp": ffBmp
2021-01-27 08:46:00 +00:00
of ".jpg", ".jpeg": ffJpg
2020-11-21 23:45:02 +00:00
else:
2021-06-25 00:09:54 +00:00
raise newException(PixieError, "Unsupported file extension")
writeFile(filePath, image.encodeImage(fileFormat))
2021-06-25 00:09:54 +00:00
proc writeFile*(mask: Mask, filePath: string) =
## Writes a mask to a file.
let fileFormat = case splitFile(filePath).ext.toLowerAscii():
of ".png": ffPng
of ".bmp": ffBmp
of ".jpg", ".jpeg": ffJpg
else:
raise newException(PixieError, "Unsupported file extension")
writeFile(filePath, mask.encodeMask(fileFormat))