Flippy is a simple 2d image and drawing library.
Basic usage:
import flippy, vmath, chroma
# load an image
var image = loadImage("tests/lenna.png")
# print it out
echo image
# get a color pixel
echo image.getRgba(100, 100)
# put a color pixel
image.putRgba(10, 10, rgba(255, 0, 0, 255))
# blit a rectangular part from one place to another
blit(image, image, rect(0, 0, 100, 100), rect(100, 100, 100, 100))
# draw a line
image.line(vec2(11, 11), vec2(100, 100), rgba(0, 0, 0, 255))
# minify image by 2 or 1/2 or scale by 50%
image = image.minify(2)
# save the image to a file
image.save("tests/lenna2.png")import flippyMain image object that holds the bitmap data.
Image = ref object
filePath*: string
width*, height*, channels*, format*: int
data*: seq[uint8]Flippy = object
mipmaps*: seq[Image]Display the image path, size and channels.
proc `$`(image: Image): stringCreates a new image with appropriate dimensions.
proc newImage(width, height, channels: int): ImageCreates a new image with a path.
proc newImage(filePath: string; width, height, channels: int): ImageLoads a png image.
proc loadImage(filePath: string): Image {.raises: [IOError], tags: [ReadIOEffect, RootEffect, WriteIOEffect].}Copies an image creating a new image.
proc copy(image: Image): ImageSaves a png image.
proc save(image: Image) {.raises: [IOError], tags: [WriteIOEffect, RootEffect].}Sets image path and save the image.
proc save(image: Image; filePath: string) {.raises: [IOError], tags: [WriteIOEffect, RootEffect].}Returns true if (x, y) is inside the image.
proc inside(image: Image; x, y: int): bool {.inline.}Gets a color from (x, y) coordinates.
- No bounds checking *
Make sure that x, y are in bounds. Failure in the assumptions will case unsafe memory reads.
proc getRgbaUnsafe(image: Image; x, y: int): ColorRGBA {.inline.}Gets a pixel as (x, y) floats.
- No bounds checking *
Make sure that x, y are in bounds. Failure in the assumptions will case unsafe memory reads.
proc getRgbaUnsafe(image: Image; x, y: float64): ColorRGBA {.inline.}Gets a pixel at (x, y) or returns transparent black if outside of bounds. Slower due to bounds checking.
proc getRgba(image: Image; x, y: int): ColorRGBA {.inline.}Gets a pixel as (x, y) floats.
proc getRgbaSmooth(image: Image; x, y: float64): ColorRGBA {.inline.}Puts a ColorRGBA pixel back.
- No bounds checking *
Make sure that x, y are in bounds. Failure in the assumptions will case unsafe memory writes.
proc putRgbaUnsafe(image: Image; x, y: int; rgba: ColorRGBA) {.inline.}Puts a ColorRGBA pixel back as x, y floats (does not do blending).
- No bounds checking *
Make sure that x, y are in bounds. Failure in the assumptions will case unsafe memory writes.
proc putRgbaUnsafe(image: Image; x, y: float64; rgba: ColorRGBA) {.inline, tags: [].}Puts pixel onto the image or safely ignored if pixel is outside the image. Slower due to bounds checking.
proc putRgba(image: Image; x, y: int; rgba: ColorRGBA) {.inline.}Scales the image down by an integer scale.
proc minifyBy2(image: Image): ImageScales the image down by an integer scale.
proc minify(image: Image; scale: int): ImageScales image image up by an integer scale.
proc magnify(image: Image; scale: int): ImageBlits rectangle from one image to the other image.
- No bounds checking *
Make sure that src and dest rect are in bounds. Make sure that channels for images are the same. Failure in the assumptions will case unsafe memory writes.
proc blitUnsafe(destImage: Image; srcImage: Image; src, dest: Rect)Blits rectangle from one image to the other image.
proc blit(destImage: Image; srcImage: Image; src, dest: Rect)Blits rectangle from one image to the other image.
proc blit(destImage: Image; srcImage: Image; pos: Vec2)Blits rectangle from one image to the other image.
proc blitWithAlpha(destImage: Image; srcImage: Image; src, dest: Rect)Blits rectangle from one image to the other image with masking color.
proc blitWithMask(destImage: Image; srcImage: Image; src, dest: Rect; rgba: ColorRGBA)Gets a sub image of the main image.
proc getSubImage(image: Image; x, y, w, h: int): ImageTrims the transparent (alpha=0) border around the image.
proc trim(image: Image): ImageFlips the image around the Y axis.
proc flipHorizontal(image: Image): ImageFlips the image around the X axis.
proc flipVertical(image: Image): ImageBlits one image onto another using matrix with alpha blending.
proc blit(destImage, srcImage: Image; mat: Mat4)Blits one image onto another using matrix with alpha blending.
proc blitWithAlpha(destImage: Image; srcImage: Image; mat: Mat4)Fills the image with a solid color.
proc fill(image: Image; rgba: ColorRGBA)Draws a line from one at vec to to vec.
proc line(image: Image; at, to: Vec2; rgba: ColorRGBA)Draws a filled rectangle.
proc fillRect(image: Image; rect: Rect; rgba: ColorRGBA)Draws a rectangle borders only.
proc strokeRect(image: Image; rect: Rect; rgba: ColorRGBA)Draws a filled circle with antialiased edges.
proc fillCircle(image: Image; pos: Vec2; radius: float; rgba: ColorRGBA)Draws a border of circle with antialiased edges.
proc strokeCircle(image: Image; pos: Vec2; radius, border: float; rgba: ColorRGBA)Fills image with a rounded rectangle.
proc fillRoundedRect(image: Image; rect: Rect; radius: float; rgba: ColorRGBA)Fills image with a stroked rounded rectangle.
proc strokeRoundedRect(image: Image; rect: Rect; radius, border: float; rgba: ColorRGBA)Draws a 9-patch
proc ninePatch(image: Image; rect: Rect; radius, border: float; fill, stroke: ColorRGBA)Rotates the image clockwise.
proc rotate90Degrees(image: Image): ImageRotates the image anti-clockwise.
proc rotateNeg90Degrees(image: Image): ImageShears the image horizontally; resizes to fit.
proc shearX(image: Image; shear: float): ImageShears the image vertically; resizes to fit.
proc shearY(image: Image; shear: float): ImageRotates the image by given angle (in degrees) using the 3-shear method (Paeth method)
proc rotate(image: Image; angle: float): ImageRemoves alpha channel from the images by: Setting it to 255 everywhere.
proc removeAlpha(image: Image)PNG saves space by encoding alpha = 0 areas as black however scaling such images lets the black or gray come out. This bleeds the real colors into invisible space.
proc alphaBleed(image: Image)Blurs the image by x and y directions.
proc blur(image: Image; xBlur: int; yBlur: int): Imageproc resize(srcImage: Image; width, height: int): ImageAdds n pixel border around alpha parts of the image.
proc outlineBorder(image: Image; borderPx: int): Imagefunc width(flippy: Flippy): intfunc height(flippy: Flippy): intFlippy is a special file format that is fast to load and save with mip maps.
proc save(flippy: Flippy; filePath: string) {.raises: [Exception, IOError, OSError, SnappyException], tags: [WriteIOEffect].}proc pngToFlippy(pngPath, flippyPath: string) {.raises: [IOError, Exception, OSError, SnappyException], tags: [ReadIOEffect, RootEffect, WriteIOEffect].}Flippy is a special file format that is fast to load and save with mip maps.
proc loadFlippy(filePath: string): Flippy {.raises: [Exception, IOError, OSError, ValueError, SnappyException], tags: [WriteIOEffect, ReadIOEffect].}
