Do a little image work.

This commit is contained in:
treeform 2020-11-20 09:13:53 -08:00
parent d3e908ba2d
commit cb50a1cb86
3 changed files with 41 additions and 17 deletions

View file

@ -1,7 +1,11 @@
# You can use this nim template to jump start your nim library or project.
# Full-featured 2d graphics library for Nim.
This template includes:
* MIT licence
* src directory and a private common.nim
* test directory
* GitHub Actions to run the tests on GitHub
Written in pure nim. Basis for the fidget UI library.
Features:
* images
* paths
* masks
Supported File formats reading and writing:
* BMP (RGBA Only)

View file

@ -71,6 +71,14 @@ proc fill*(image: Image, rgba: ColorRgba) =
for i in 0 ..< image.data.len:
image.data[i] = rgba
proc invert*(image: Image) =
## Inverts all of the colors and alpha.
for rgba in image.data.mitems:
rgba.r = 255 - rgba.r
rgba.g = 255 - rgba.g
rgba.b = 255 - rgba.b
rgba.a = 255 - rgba.a
proc subImage*(image: Image, x, y, w, h: int): Image =
## Gets a sub image of the main image.
doAssert x >= 0 and y >= 0
@ -92,6 +100,24 @@ proc minifyBy2*(image: Image): Image =
image.getRgbaUnsafe(x * 2 + 0, y * 2 + 1).color / 4.0
result.setRgbaUnsafe(x, y, color.rgba)
proc minifyBy2*(image: Image, scale2x: int): Image =
## Scales the image down by an integer scale.
result = image
for i in 1 ..< scale2x:
result = result.minifyBy2()
proc magnifyBy2*(image: Image, scale2x: int): Image =
## Scales image image up by an integer scale.
let scale = 2 ^ scale2x
result = newImage(image.width * scale, image.height * scale)
for y in 0 ..< result.height:
for x in 0 ..< result.width:
var rgba = image.getRgbaUnsafe(x div scale, y div scale)
result.setRgbaUnsafe(x, y, rgba)
proc magnifyBy2*(image: Image): Image =
image.magnifyBy2(2)
proc blitUnsafe*(destImage: Image, srcImage: Image, src, dest: Rect) =
## Blits rectangle from one image to the other image.
## * No bounds checking *
@ -260,17 +286,14 @@ proc draw*(
for x in 0 ..< srcImage.width:
let
srcRgba = srcImage.getRgbaUnsafe(x, y)
if blendMode == Mask or srcRgba.a > 0 :
if blendMode.hasEffect(srcRgba):
let
destRgba = destImage.getRgbaUnsafe(x + pos.x.int, y + pos.y.int)
rgba = blendMode.mix(destRgba, srcRgba)
# TODO: Make unsafe
destImage[x + pos.x.int, y + pos.y.int] = rgba
proc invert*(image: Image) =
## Inverts all of the colors and alpha.
for rgba in image.data.mitems:
rgba.r = 255 - rgba.r
rgba.g = 255 - rgba.g
rgba.b = 255 - rgba.b
rgba.a = 255 - rgba.a
## Thoughts
## single draw function that takes a matrix
## if matrix is simple integer translation -> fast pass
## if blend mode is copy -> even faster path

View file

@ -1,3 +0,0 @@
## Put your tests here.
import pixie