Skip to content

Commit

Permalink
Merge pull request #541 from JD557/avoid-more-boxing
Browse files Browse the repository at this point in the history
Avoid more boxing
  • Loading branch information
JD557 authored Nov 17, 2024
2 parents 13611ad + 1abaa1d commit 74c1ac6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ trait Plane extends Function2[Int, Int, Color] { outer =>
new Plane {
def getPixel(x: Int, y: Int): Color = {
val c1 = outer.getPixel(x, y)
if (x >= 0 && y >= 0 && x < that.width && y < that.height) {
val c2 = that.unsafeGetPixel(x, y)
f(c1, c2)
} else c1
// This plane is clipped into a surface view, so x and y will always be in bounds
val c2 = that.unsafeGetPixel(x, y)
f(c1, c2)
}
},
width = that.width,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,18 @@ object SurfaceView {
def unsafeGetPixel(x: Int, y: Int): Color = ramSurface.getPixelOrElse(cx + x, cy + y, SurfaceView.defaultColor)

override def getPixels(): Vector[Array[Color]] = {
Vector.tabulate(height) { y =>
ramSurface.dataBuffer(cy + y).slice(cx, cx + width)
val b = Vector.newBuilder[Array[Color]]
b.sizeHint(height)
var y = 0
while (y < height) {
if (width <= 0) {
b += Array.empty[Color]
} else {
b += ramSurface.dataBuffer(cy + y).slice(cx, cx + width)
}
y += 1
}
b.result()
}

def map(f: Color => Color): SurfaceView = toPlaneSurfaceView().map(f)
Expand Down

0 comments on commit 74c1ac6

Please sign in to comment.