pixie/src/pixie.nim

277 lines
6.2 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/fileformats/bmp, pixie/fileformats/jpg, pixie/fileformats/png,
pixie/fileformats/svg, pixie/gradients, pixie/images, pixie/masks,
pixie/paths, vmath
2020-11-21 04:34:57 +00:00
2021-02-18 19:29:40 +00:00
export blends, bumpy, chroma, common, gradients, images, masks, paths, vmath
2020-11-20 03:44:24 +00:00
2020-11-21 04:34:57 +00:00
type
FileFormat* = enum
2020-11-21 23:45:02 +00:00
ffPng, ffBmp, ffJpg
2020-11-21 04:34:57 +00:00
2020-11-21 05:09:52 +00:00
proc decodeImage*(data: string | seq[uint8]): Image =
2020-11-21 04:34:57 +00:00
## Loads an image from a 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)
2020-12-04 16:17:03 +00:00
elif data.len > 5 and data.readStr(0, 5) == svgSignature:
decodeSvg(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
proc readImage*(filePath: string): Image =
## Loads an image from a file.
decodeImage(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()
proc writeFile*(image: Image, filePath: string, fileFormat: FileFormat) =
## Writes an image to a file.
writeFile(filePath, image.encodeImage(fileFormat))
2020-11-21 18:34:57 +00:00
proc writeFile*(image: Image, filePath: string) =
## Writes an image to a file.
let fileFormat = case splitFile(filePath).ext:
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:
2020-11-22 02:05:48 +00:00
raise newException(PixieError, "Unsupported image file extension")
2020-11-21 23:45:02 +00:00
image.writeFile(filePath, fileformat)
2021-02-18 19:29:40 +00:00
2021-02-19 18:04:27 +00:00
proc fillRect*(image: Image, rect: Rect, color: ColorRGBA) =
var path: Path
path.rect(rect)
image.fillPath(path, color)
proc fillRect*(mask: Mask, rect: Rect) =
var path: Path
path.rect(rect)
mask.fillPath(path)
proc strokeRect*(
image: Image, rect: Rect, color: ColorRGBA, strokeWidth = 1.0
2021-02-18 19:29:40 +00:00
) =
var path: Path
path.rect(rect)
2021-02-19 18:04:27 +00:00
image.strokePath(path, color, strokeWidth)
2021-02-18 19:29:40 +00:00
2021-02-19 18:04:27 +00:00
proc strokeRect*(mask: Mask, rect: Rect, strokeWidth = 1.0) =
2021-02-18 19:29:40 +00:00
var path: Path
path.rect(rect)
2021-02-19 18:04:27 +00:00
mask.strokePath(path, strokeWidth)
proc fillRoundedRect*(
image: Image,
rect: Rect,
nw, ne, se, sw: float32,
color: ColorRGBA
) =
var path: Path
path.roundedRect(rect, nw, ne, se, sw)
image.fillPath(path, color)
proc fillRoundedRect*(
image: Image,
rect: Rect,
radius: float32,
color: ColorRGBA
) =
var path: Path
path.roundedRect(rect, radius, radius, radius, radius)
image.fillPath(path, color)
proc fillRoundedRect*(mask: Mask, rect: Rect, nw, ne, se, sw: float32) =
var path: Path
path.roundedRect(rect, nw, ne, se, sw)
mask.fillPath(path)
proc fillRoundedRect*(mask: Mask, rect: Rect, radius: float32) =
var path: Path
path.roundedRect(rect, radius, radius, radius, radius)
2021-02-18 19:29:40 +00:00
mask.fillPath(path)
2021-02-18 19:55:26 +00:00
2021-02-19 18:04:27 +00:00
proc strokeRoundedRect*(
2021-02-18 22:23:54 +00:00
image: Image,
rect: Rect,
nw, ne, se, sw: float32,
color: ColorRGBA,
2021-02-19 18:04:27 +00:00
strokeWidth = 1.0
2021-02-18 22:23:54 +00:00
) =
var path: Path
path.roundedRect(rect, nw, ne, se, sw)
2021-02-19 18:04:27 +00:00
image.strokePath(path, color, strokeWidth)
2021-02-18 22:23:54 +00:00
2021-02-19 18:04:27 +00:00
proc strokeRoundedRect*(
2021-02-18 22:23:54 +00:00
image: Image,
rect: Rect,
radius: float32,
color: ColorRGBA,
2021-02-19 18:04:27 +00:00
strokeWidth = 1.0
2021-02-18 22:23:54 +00:00
) =
var path: Path
path.roundedRect(rect, radius, radius, radius, radius)
2021-02-19 18:04:27 +00:00
image.strokePath(path, color, strokeWidth)
2021-02-18 22:23:54 +00:00
2021-02-19 18:04:27 +00:00
proc strokeRoundedRect*(
mask: Mask, rect: Rect, nw, ne, se, sw: float32, strokeWidth = 1.0
) =
2021-02-18 22:23:54 +00:00
var path: Path
path.roundedRect(rect, nw, ne, se, sw)
2021-02-19 18:04:27 +00:00
mask.strokePath(path, strokeWidth)
2021-02-18 22:23:54 +00:00
2021-02-19 18:04:27 +00:00
proc strokeRoundedRect*(
mask: Mask, rect: Rect, radius: float32, strokeWidth = 1.0
) =
2021-02-18 22:23:54 +00:00
var path: Path
path.roundedRect(rect, radius, radius, radius, radius)
2021-02-19 18:04:27 +00:00
mask.strokePath(path, strokeWidth)
2021-02-18 22:23:54 +00:00
2021-02-19 18:04:27 +00:00
proc strokeSegment*(
2021-02-18 19:55:26 +00:00
image: Image,
segment: Segment,
color: ColorRGBA,
2021-02-22 22:30:28 +00:00
strokeWidth = 1.0
2021-02-18 19:55:26 +00:00
) =
var path: Path
path.moveTo(segment.at)
path.lineTo(segment.to)
2021-02-22 22:30:28 +00:00
image.strokePath(path, color, strokeWidth)
2021-02-18 19:55:26 +00:00
2021-02-19 18:04:27 +00:00
proc strokeSegment*(mask: Mask, segment: Segment, strokeWidth: float32) =
2021-02-18 19:55:26 +00:00
var path: Path
path.moveTo(segment.at)
path.lineTo(segment.to)
mask.strokePath(path, strokeWidth)
2021-02-18 22:30:47 +00:00
2021-02-19 18:04:27 +00:00
proc fillEllipse*(
2021-02-18 22:30:47 +00:00
image: Image,
center: Vec2,
rx, ry: float32,
color: ColorRGBA,
blendMode = bmNormal
) =
var path: Path
path.ellipse(center, rx, ry)
image.fillPath(path, color, wrNonZero, blendMode)
2021-02-19 18:04:27 +00:00
proc fillEllipse*(
2021-02-18 22:30:47 +00:00
mask: Mask,
center: Vec2,
rx, ry: float32
) =
var path: Path
path.ellipse(center, rx, ry)
mask.fillPath(path)
2021-02-19 18:04:27 +00:00
proc strokeEllipse*(
2021-02-18 22:30:47 +00:00
image: Image,
center: Vec2,
2021-02-19 18:04:27 +00:00
rx, ry: float32,
2021-02-18 22:30:47 +00:00
color: ColorRGBA,
2021-02-19 18:04:27 +00:00
strokeWidth = 1.0
) =
var path: Path
path.ellipse(center, rx, ry)
image.strokePath(path, color, strokeWidth)
proc strokeEllipse*(
mask: Mask,
center: Vec2,
rx, ry: float32,
strokeWidth = 1.0
) =
var path: Path
path.ellipse(center, rx, ry)
mask.strokePath(path, strokeWidth)
proc fillCircle*(
image: Image,
center: Vec2,
radius: float32,
color: ColorRGBA
2021-02-18 22:30:47 +00:00
) =
var path: Path
path.ellipse(center, radius, radius)
2021-02-19 18:04:27 +00:00
image.fillPath(path, color)
2021-02-18 22:30:47 +00:00
2021-02-19 18:04:27 +00:00
proc fillCircle*(
2021-02-18 22:30:47 +00:00
mask: Mask,
center: Vec2,
radius: float32
) =
var path: Path
path.ellipse(center, radius, radius)
mask.fillPath(path)
2021-02-18 22:40:35 +00:00
2021-02-19 18:04:27 +00:00
proc strokeCircle*(
image: Image,
center: Vec2,
radius: float32,
color: ColorRGBA,
strokeWidth = 1.0
) =
var path: Path
path.ellipse(center, radius, radius)
image.fillPath(path, color)
proc strokeCircle*(
mask: Mask,
center: Vec2,
radius: float32,
strokeWidth = 1.0
) =
var path: Path
path.ellipse(center, radius, radius)
mask.fillPath(path)
proc fillPolygon*(
2021-02-18 22:40:35 +00:00
image: Image,
pos: Vec2,
size: float32,
sides: int,
2021-02-19 18:04:27 +00:00
color: ColorRGBA
2021-02-18 22:40:35 +00:00
) =
var path: Path
path.polygon(pos, size, sides)
2021-02-19 18:04:27 +00:00
image.fillPath(path, color)
2021-02-18 22:40:35 +00:00
2021-02-23 00:39:27 +00:00
proc fillPolygon*(mask: Mask, pos: Vec2, size: float32, sides: int) =
2021-02-18 22:40:35 +00:00
var path: Path
path.polygon(pos, size, sides)
mask.fillPath(path)
2021-02-19 18:04:27 +00:00
proc strokePolygon*(
image: Image,
pos: Vec2,
size: float32,
sides: int,
color: ColorRGBA,
strokeWidth = 1.0
) =
var path: Path
path.polygon(pos, size, sides)
image.strokePath(path, color, strokeWidth)
proc strokePolygon*(
mask: Mask,
pos: Vec2,
size: float32,
sides: int,
strokeWidth = 1.0
) =
var path: Path
path.polygon(pos, size, sides)
mask.strokePath(path, strokeWidth)