pixie/tests/test_images.nim

229 lines
5.3 KiB
Nim
Raw Normal View History

2022-07-09 18:08:34 +00:00
import chroma, pixie, pixie/internal, vmath, xrays
2020-11-22 20:12:53 +00:00
2020-11-20 06:12:44 +00:00
block:
2021-01-23 20:17:28 +00:00
let image = newImage(10, 10)
2020-11-20 06:12:44 +00:00
image[0, 0] = rgba(255, 255, 255, 255)
doAssert image[0, 0] == rgba(255, 255, 255, 255)
block:
2021-01-23 20:17:28 +00:00
let image = newImage(10, 10)
2020-11-28 00:54:10 +00:00
image.fill(rgba(255, 0, 0, 255))
2020-11-20 06:12:44 +00:00
doAssert image[0, 0] == rgba(255, 0, 0, 255)
2020-11-21 18:34:57 +00:00
2021-01-20 00:50:51 +00:00
block:
2021-01-23 20:17:28 +00:00
let
image = newImage(256, 256)
subImage = image.subImage(0, 0, 128, 128)
doAssert subImage.width == 128 and subImage.height == 128
block:
let image = newImage(10, 10)
2021-01-20 00:50:51 +00:00
image.fill(rgba(255, 0, 0, 128))
2021-02-25 22:11:36 +00:00
image.data.toPremultipliedAlpha()
2021-01-20 00:50:51 +00:00
doAssert image[9, 9] == rgba(128, 0, 0, 128)
block:
2022-05-30 02:07:15 +00:00
var data = newSeq[ColorRGBX](100)
fillUnsafe(data, rgbx(100, 0, 0, 128), 0, data.len)
data.toStraightAlpha()
doAssert data[10] == rgbx(199, 0, 0, 128)
2021-01-20 00:50:51 +00:00
block:
let image = newImage(100, 100)
2021-02-25 23:56:52 +00:00
image.fill(rgbx(200, 200, 200, 200))
image.applyOpacity(0.5)
2021-02-25 23:56:52 +00:00
doAssert image[0, 0] == rgbx(100, 100, 100, 100)
doAssert image[88, 88] == rgbx(100, 100, 100, 100)
2021-01-23 05:33:05 +00:00
block:
let
a = newImage(101, 101)
b = newImage(50, 50)
a.fill(rgba(255, 0, 0, 255))
b.fill(rgba(0, 255, 0, 255))
2021-10-03 20:19:33 +00:00
a.draw(b)
2021-01-23 05:33:05 +00:00
2022-07-09 18:08:34 +00:00
a.xray("tests/images/flipped1.png")
2021-01-23 05:33:05 +00:00
a.flipVertical()
2022-07-09 18:08:34 +00:00
a.xray("tests/images/flipped2.png")
2021-01-23 05:33:05 +00:00
a.flipHorizontal()
2022-07-09 18:08:34 +00:00
a.xray("tests/images/flipped3.png")
2021-01-31 01:44:28 +00:00
block:
let
a = readImage("tests/images/flipped1.png")
b = a.superImage(-10, 0, 20, 20)
2022-07-09 18:08:34 +00:00
b.xray("tests/images/superimage1.png")
2021-01-31 01:44:28 +00:00
block:
let
a = readImage("tests/images/flipped1.png")
b = a.superImage(-10, -10, 20, 20)
2022-07-09 18:08:34 +00:00
b.xray("tests/images/superimage2.png")
2021-01-31 01:44:28 +00:00
block:
let
a = readImage("tests/images/flipped1.png")
b = a.superImage(90, 0, 120, 120)
2022-07-09 18:08:34 +00:00
b.xray("tests/images/superimage3.png")
2021-01-31 01:44:28 +00:00
block:
let
a = readImage("tests/images/flipped1.png")
b = a.superImage(90, 90, 120, 120)
2022-07-09 18:08:34 +00:00
b.xray("tests/images/superimage4.png")
2021-01-31 01:44:28 +00:00
block:
let
a = readImage("tests/images/flipped1.png")
b = a.superImage(-10, -10, 120, 120)
2022-07-09 18:08:34 +00:00
b.xray("tests/images/superimage5.png")
2021-01-31 01:44:28 +00:00
block:
let
a = readImage("tests/images/flipped1.png")
b = a.superImage(45, 45, 20, 20)
2022-07-09 18:08:34 +00:00
b.xray("tests/images/superimage6.png")
2021-02-08 02:39:46 +00:00
block:
let
a = readImage("tests/images/flipped1.png")
b = a.minifyBy2()
2022-07-09 18:08:34 +00:00
b.xray("tests/images/minifiedBy2.png")
2021-02-08 03:15:02 +00:00
2021-06-17 22:34:48 +00:00
block:
let
a = readImage("tests/images/minifiedBy2.png")
b = a.magnifyBy2()
2022-07-09 18:08:34 +00:00
b.xray("tests/images/magnifiedBy2.png")
2021-06-17 22:34:48 +00:00
2021-02-08 03:15:02 +00:00
block:
let
a = readImage("tests/images/flipped1.png")
b = a.minifyBy2(2)
2022-07-09 18:08:34 +00:00
b.xray("tests/images/minifiedBy4.png")
2021-02-09 04:26:51 +00:00
2021-06-17 22:34:48 +00:00
block:
let
a = readImage("tests/images/minifiedBy4.png")
b = a.magnifyBy2(2)
2022-07-09 18:08:34 +00:00
b.xray("tests/images/magnifiedBy4.png")
2021-06-17 22:34:48 +00:00
2021-06-18 05:47:26 +00:00
block:
let
a = readImage("tests/fileformats/png/mandrill.png")
2021-06-18 05:47:26 +00:00
b = a.minifyBy2()
2022-07-09 18:08:34 +00:00
b.xray("tests/images/minifiedMandrill.png")
2021-06-18 05:47:26 +00:00
2021-02-09 04:26:51 +00:00
block:
let a = newImage(100, 100)
2021-02-25 23:56:52 +00:00
a.fill(rgbx(50, 100, 150, 200))
2021-02-09 04:26:51 +00:00
a.invert()
2021-02-25 23:56:52 +00:00
doAssert a[0, 0] == rgbx(44, 33, 22, 55)
2021-02-27 00:42:40 +00:00
block:
let ctx = newContext(100, 100)
ctx.fillStyle = rgba(255, 255, 255, 255)
ctx.image.fill(rgba(0, 0, 0, 255))
ctx.fillRect(rect(25, 25, 50, 50), )
ctx.image.blur(20)
2022-07-09 18:08:34 +00:00
ctx.image.xray("tests/images/imageblur20.png")
block:
let ctx = newContext(100, 100)
ctx.fillStyle = rgba(255, 255, 255, 255)
ctx.image.fill(rgba(0, 0, 0, 255))
ctx.fillRect(rect(25, 25, 50, 50))
ctx.image.blur(20, rgba(0, 0, 0, 255))
2022-07-09 18:08:34 +00:00
ctx.image.xray("tests/images/imageblur20oob.png")
2021-06-25 00:09:54 +00:00
block: # Test conversion between image and mask
let
originalImage = newImage(100, 100)
originalMask = newMask(100, 100)
2021-08-06 19:38:03 +00:00
let p = newPath()
2021-11-28 09:08:25 +00:00
p.circle(50, 50, 25)
2021-06-25 00:09:54 +00:00
originalImage.fillPath(p, rgba(255, 0, 0, 255))
originalMask.fillPath(p)
2022-06-25 07:07:44 +00:00
# # Converting an image to a mask == a mask of the same fill
# doAssert newMask(originalImage).data == originalMask.data
2021-06-25 00:09:54 +00:00
2022-06-25 07:07:44 +00:00
# # Converting a mask to an image == converting an image to a mask as an image
# doAssert newImage(newMask(originalImage)).data == newImage(originalMask).data
2021-06-25 00:09:54 +00:00
block:
2021-08-06 19:38:03 +00:00
let p = newPath()
2021-06-25 00:09:54 +00:00
p.roundedRect(10, 10, 80, 80, 10, 10, 10, 10)
let image = newImage(100, 100)
image.fillPath(p, rgba(255, 0, 0, 255))
2022-07-09 18:08:34 +00:00
newImage(newMask(image)).xray("tests/images/mask2image.png")
2021-08-17 21:14:31 +00:00
block:
let image = newImage(100, 100)
doAssert image.isOneColor()
block:
let image = newImage(100, 100)
image.fill(rgba(255, 255, 255, 255))
doAssert image.isOneColor()
block:
let image = newImage(100, 100)
image.fill(rgba(1, 2, 3, 4))
doAssert image.isOneColor()
block:
let image = newImage(100, 100)
image[99, 99] = rgba(255, 255, 255, 255)
doAssert not image.isOneColor()
block:
let image = newImage(100, 100)
doAssert image.isTransparent()
block:
let image = newImage(100, 100)
image.fill(rgba(255, 255, 255, 0))
doAssert image.isTransparent()
block:
let image = newImage(100, 100)
image[99, 99] = rgba(255, 255, 255, 255)
doAssert not image.isTransparent()
2021-08-18 03:25:36 +00:00
block:
let image = newImage(100, 100)
image.fill(rgba(255, 255, 255, 255))
doAssert not image.isTransparent()
2022-06-08 23:31:03 +00:00
block:
let image = newImage(100, 100)
image.fill(rgba(255, 255, 255, 255))
doAssert image.isOpaque()
block:
let image = newImage(100, 100)
image.fill(rgba(255, 255, 255, 255))
image[9, 13] = rgbx(250, 250, 250, 250)
doAssert not image.isOpaque()
block:
let a = newImage(400, 400)
let b = newImage(156, 434)
b.fill(rgba(255, 0, 0, 255))
a.draw(
b,
mat3(
-0.5, -4.371138828673793e-008, 0.0,
-4.371138828673793e-008, 0.5, 0.0,
292.0, 45.0, 1.0
)
)