Add newImageNoInit and newSeqNoInit.

This commit is contained in:
treeform 2020-11-22 18:47:23 -08:00
parent 3a341dbe0b
commit f5dbe4102d
6 changed files with 26 additions and 22 deletions

View file

@ -13,6 +13,25 @@ proc newImage*(width, height: int): Image =
result.height = height
result.data = newSeq[ColorRGBA](width * height)
proc newSeqNoInit*[T](len: Natural): seq[T] =
## Creates a new sequence of type ``seq[T]`` with length ``len``.
## Skips initialization of memory to zero.
result = newSeqOfCap[T](len)
when defined(nimSeqsV2):
cast[ptr int](addr result)[] = len
else:
type GenericSeq = ref object
len, reserved: int
var s = cast[GenericSeq](result)
s.len = len
proc newImageNoInit*(width, height: int): Image =
## Creates a new image with appropriate dimensions.
result = Image()
result.width = width
result.height = height
result.data = newSeq[ColorRGBA](width * height)
proc copy*(image: Image): Image =
## Copies an image creating a new image.
result = newImage(image.width, image.height)
@ -169,7 +188,7 @@ proc hasEffect*(blendMode: BlendMode, rgba: ColorRGBA): bool =
proc drawFast1*(a: Image, b: Image, mat: Mat3): Image =
## Draws one image onto another using integer x,y offset with COPY.
result = newImage(a.width, a.height)
result = newImageNoInit(a.width, a.height)
var matInv = mat.inverse()
for y in 0 ..< a.height:
for x in 0 ..< a.width:
@ -181,11 +200,10 @@ proc drawFast1*(a: Image, b: Image, mat: Mat3): Image =
proc drawFast2*(a: Image, b: Image, mat: Mat3, blendMode: BlendMode): Image =
## Draws one image onto another using matrix with color blending.
result = newImage(a.width, a.height)
result = newImageNoInit(a.width, a.height)
var matInv = mat.inverse()
for y in 0 ..< a.height:
for x in 0 ..< a.width:
#echo x, ", ", y
var rgba = a.getRgbaUnsafe(x, y)
let srcPos = matInv * vec2(x.float32, y.float32)
if b.inside(srcPos.x.floor.int, srcPos.y.floor.int):
@ -196,7 +214,7 @@ proc drawFast2*(a: Image, b: Image, mat: Mat3, blendMode: BlendMode): Image =
proc drawFast3*(a: Image, b: Image, mat: Mat3, blendMode: BlendMode): Image =
## Draws one image onto another using matrix with color blending.
result = newImage(a.width, a.height)
result = newImageNoInit(a.width, a.height)
var matInv = mat.inverse()
for y in 0 ..< a.height:
for x in 0 ..< a.width:

View file

@ -8,7 +8,7 @@ timeIt "benchDrawFast1 COPY":
a.fill(rgba(255, 0, 0, 255))
var b = newImage(100, 100)
b.fill(rgba(0, 255, 0, 255))
c = a.drawFast1(b, x = 25, y = 25) # Copy
c = a.drawFast1(b, translate(vec2(25, 25))) # Copy
tmp += c.width * c.height
c.writeFile("tests/images/benchDrawFast1Copy.png")
echo tmp
@ -21,7 +21,7 @@ timeIt "benchDrawFast2 COPY":
a.fill(rgba(255, 0, 0, 255))
var b = newImage(100, 100)
b.fill(rgba(0, 255, 0, 255))
c = a.drawFast2(b, x = 25, y = 25, bmCopy)
c = a.drawFast2(b, translate(vec2(25, 25)), bmCopy)
tmp += c.width * c.height
c.writeFile("tests/images/benchDrawFast2Copy.png")
echo tmp
@ -47,7 +47,7 @@ timeIt "benchDrawFast2 Normal":
a.fill(rgba(255, 0, 0, 255))
var b = newImage(100, 100)
b.fill(rgba(0, 255, 0, 255))
c = a.drawFast2(b, x = 25, y = 25, bmNormal)
c = a.drawFast2(b, translate(vec2(25, 25)), bmNormal)
tmp += c.width * c.height
c.writeFile("tests/images/benchDrawFast2Normal.png")
echo tmp
@ -73,7 +73,7 @@ timeIt "benchDrawFast2 Saturation":
a.fill(rgba(255, 0, 0, 255))
var b = newImage(100, 100)
b.fill(rgba(0, 0, 0, 255))
c = a.drawFast2(b, x = 25, y = 25, bmSaturation)
c = a.drawFast2(b, translate(vec2(25, 25)), bmSaturation)
tmp += c.width * c.height
c.writeFile("tests/images/benchDrawFast2Saturation.png")
echo tmp
@ -91,20 +91,6 @@ timeIt "benchDrawFast3 Saturation":
c.writeFile("tests/images/benchDrawFast3Saturation.png")
echo tmp
timeIt "benchDrawFast4 Rotation":
var tmp = 0
var c: Image
for i in 0 ..< 1000:
var a = newImage(100, 100)
a.fill(rgba(255, 0, 0, 255))
var b = newImage(100, 100)
b.fill(rgba(0, 0, 0, 255))
c = a.drawFast4(b, translate(vec2(25, 25)) * rotationMat3(PI/2), bmNormal)
tmp += c.width * c.height
c.writeFile("tests/images/benchDrawFast2Rotation.png")
echo tmp
timeIt "benchDrawFast3 Rotation":
var tmp = 0
var c: Image

Binary file not shown.

Before

Width:  |  Height:  |  Size: 337 B

After

Width:  |  Height:  |  Size: 338 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 339 B

After

Width:  |  Height:  |  Size: 339 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 329 B

After

Width:  |  Height:  |  Size: 337 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 337 B

After

Width:  |  Height:  |  Size: 338 B