Skip to content

Commit

Permalink
Add Surface.copyFrom helpers to concrete surface companion objects
Browse files Browse the repository at this point in the history
  • Loading branch information
JD557 committed Jan 5, 2025
1 parent f0ddadd commit bff4f83
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import org.scalajs.dom.{
window
}

import eu.joaocosta.minart.graphics.{Color, MutableSurface}
import eu.joaocosta.minart.graphics.{Color, MutableSurface, Surface}

/** A mutable surface backed by an ImageData that drops the alpha channel when puting pixels.
*
Expand Down Expand Up @@ -80,6 +80,13 @@ final class ImageDataOpaqueSurface(val data: ImageData) extends MutableSurface {

object ImageDataOpaqueSurface {

/** Copies a surface into an opaque ImageData backed surface.
*
* @param surface surface to copy from
*/
def copyFrom(surface: Surface): ImageDataOpaqueSurface =
ImageDataOpaqueSurface.tabulate(surface.width, surface.height)(surface.unsafeGetPixel)

/** Produces an opaque ImageData backed surface containing values of a given function
* over ranges of integer values starting from 0.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import org.scalajs.dom.{
window
}

import eu.joaocosta.minart.graphics.{Color, MutableSurface}
import eu.joaocosta.minart.graphics.{Color, MutableSurface, Surface}

/** A mutable surface backed by an ImageData.
*
Expand Down Expand Up @@ -78,6 +78,13 @@ final class ImageDataSurface(val data: ImageData) extends MutableSurface {

object ImageDataSurface {

/** Copies a surface into an ImageData backed surface.
*
* @param surface surface to copy from
*/
def copyFrom(surface: Surface): ImageDataSurface =
ImageDataSurface.tabulate(surface.width, surface.height)(surface.unsafeGetPixel)

/** Produces an ImageData backed surface containing values of a given function
* over ranges of integer values starting from 0.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ final class BufferedImageSurface(val bufferedImage: BufferedImage) extends Mutab

object BufferedImageSurface {

/** Copies a surface into an BufferedImage backed surface.
*
* @param surface surface to copy from
*/
def copyFrom(surface: Surface): BufferedImageSurface =
BufferedImageSurface.tabulate(surface.width, surface.height)(surface.unsafeGetPixel)

/** Produces a BufferedImage backed surface containing values of a given function
* over ranges of integer values starting from 0.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,21 @@ final class RamSurface(val dataBuffer: Vector[Array[Color]]) extends MutableSurf

object RamSurface {

/** Copies a surface into a RAM Surface surface.
*
* This is just an alias to Suface#toRamSurface.
*
* @param surface surface to copy from
*/
def copyFrom(surface: Surface): RamSurface =
surface.toRamSurface()

/** Produces a RAM surface containing values of a given function
* over ranges of integer values starting from 0.
* 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
* @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): RamSurface = {
val b = Vector.newBuilder[Array[Color]]
Expand Down

0 comments on commit bff4f83

Please sign in to comment.