diff --git a/src/pixie/images.nim b/src/pixie/images.nim index 54b3543..7e28c89 100644 --- a/src/pixie/images.nim +++ b/src/pixie/images.nim @@ -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: diff --git a/tests/benchmark_draw.nim b/tests/benchmark_draw.nim index 2fa19f1..350bba6 100644 --- a/tests/benchmark_draw.nim +++ b/tests/benchmark_draw.nim @@ -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 diff --git a/tests/images/benchDrawFast2Saturation.png b/tests/images/benchDrawFast2Saturation.png index 340186d..8b795d3 100644 Binary files a/tests/images/benchDrawFast2Saturation.png and b/tests/images/benchDrawFast2Saturation.png differ diff --git a/tests/images/benchDrawFast3Copy.png b/tests/images/benchDrawFast3Copy.png index a41ca36..6654944 100644 Binary files a/tests/images/benchDrawFast3Copy.png and b/tests/images/benchDrawFast3Copy.png differ diff --git a/tests/images/benchDrawFast3Rotation.png b/tests/images/benchDrawFast3Rotation.png index acae87d..69d4c1b 100644 Binary files a/tests/images/benchDrawFast3Rotation.png and b/tests/images/benchDrawFast3Rotation.png differ diff --git a/tests/images/benchDrawFast3Saturation.png b/tests/images/benchDrawFast3Saturation.png index 340186d..8b795d3 100644 Binary files a/tests/images/benchDrawFast3Saturation.png and b/tests/images/benchDrawFast3Saturation.png differ