Skip to content

Commit

Permalink
Add tabulate to backend surfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
JD557 committed Jan 5, 2025
1 parent a4da678 commit f0ddadd
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,28 @@ final class ImageDataOpaqueSurface(val data: ImageData) extends MutableSurface {

object ImageDataOpaqueSurface {

/** Produces an opaque ImageData backed surface containing values of a given function
* over ranges of integer values starting from 0.
*
* @param width the surface width
* @param height the surface height
* @param f the function computing the element values
*/
def tabulate(width: Int, height: Int)(f: (Int, Int) => Color): ImageDataOpaqueSurface = {
val surface =
ImageDataOpaqueSurface.fromImage(new Image(width, height))
var y = 0
while (y < height) {
var x = 0
while (x < width) {
surface.unsafePutPixel(x, y, f(x, y))
x += 1
}
y += 1
}
surface
}

/** Loads an ImageDataOpaqueSurface from an offscreen canvas
*
* @param canvas offscreen canvas
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,28 @@ final class ImageDataSurface(val data: ImageData) extends MutableSurface {

object ImageDataSurface {

/** Produces an ImageData backed surface containing values of a given function
* over ranges of integer values starting from 0.
*
* @param width the surface width
* @param height the surface height
* @param f the function computing the element values
*/
def tabulate(width: Int, height: Int)(f: (Int, Int) => Color): ImageDataSurface = {
val surface =
ImageDataSurface.fromImage(new Image(width, height))
var y = 0
while (y < height) {
var x = 0
while (x < width) {
surface.unsafePutPixel(x, y, f(x, y))
x += 1
}
y += 1
}
surface
}

/** Loads an ImageDataOpaqueSurface from an offscreen canvas
*
* @param canvas offscreen canvas
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,29 @@ final class BufferedImageSurface(val bufferedImage: BufferedImage) extends Mutab
super.blit(that, blendMode)(x, y, cx, cy, cw, ch)
}
}

object BufferedImageSurface {

/** Produces a BufferedImage backed surface containing values of a given function
* over ranges of integer values starting from 0.
*
* @param width the surface width
* @param height the surface height
* @param f the function computing the element values
*/
def tabulate(width: Int, height: Int)(f: (Int, Int) => Color): BufferedImageSurface = {
val surface =
new BufferedImageSurface(new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB))
var y = 0
while (y < height) {
var x = 0
while (x < width) {
surface.unsafePutPixel(x, y, f(x, y))
x += 1
}
y += 1
}
surface
}

}

0 comments on commit f0ddadd

Please sign in to comment.