Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement API for converting Picture to BufferedImage #144

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import laika.config.LinkConfig
import laika.config.ApiLinks
import laika.theme.Theme

ThisBuild / tlBaseVersion := "0.21" // your current series x.y
ThisBuild / tlBaseVersion := "0.22" // your current series x.y

Global / onChangedBuildSource := ReloadOnSourceChanges

Expand Down
30 changes: 30 additions & 0 deletions core/jvm/src/main/scala/doodle/effect/BufferedImageConverter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2015 Creative Scala
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package doodle
package effect

import cats.effect.IO
import doodle.algebra.Algebra
import doodle.algebra.Picture
import java.awt.image.BufferedImage

trait BufferedImageConverter[+Alg <: Algebra, Frame] {
def bufferedImage[A](
description: Frame,
picture: Picture[Alg, A]
): IO[(A, BufferedImage)]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2015 Creative Scala
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package doodle
package syntax

import cats.effect.IO
import cats.effect.unsafe.IORuntime
import doodle.algebra.Algebra
import doodle.algebra.Picture
import doodle.effect.BufferedImageConverter
import java.awt.image.BufferedImage

trait BufferedImageConverterSyntax {
implicit class BufferedImageConverterOps[Alg <: Algebra, A](
picture: Picture[Alg, A]
) {
def bufferedImage[Frame](frame: Frame)(implicit
w: BufferedImageConverter[Alg, Frame],
r: IORuntime
): (A, BufferedImage) =
w.bufferedImage(frame, picture).unsafeRunSync()

def bufferedImageToIO[Frame](frame: Frame)(implicit
w: BufferedImageConverter[Alg, Frame]
): IO[(A, BufferedImage)] =
w.bufferedImage(frame, picture)
}
}
2 changes: 2 additions & 0 deletions core/jvm/src/main/scala/doodle/syntax/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package object syntax {
with Base64Syntax
with BitmapSyntax
with BlendSyntax
with BufferedImageConverterSyntax
with DebugSyntax
with LayoutSyntax
with NormalizedSyntax
Expand All @@ -40,6 +41,7 @@ package object syntax {
object base64 extends Base64Syntax
object bitmap extends BitmapSyntax
object blend extends BlendSyntax
object bufferedImageConverter extends BufferedImageConverterSyntax
object debug extends DebugSyntax
object layout extends LayoutSyntax
object normalized extends NormalizedSyntax
Expand Down
19 changes: 19 additions & 0 deletions java2d/src/main/scala/doodle/java2d/effect/Java2dWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,22 @@ object Java2dPdfWriter extends Java2dWriter[Pdf] {
}
} yield value
}

object Java2dBufferedImageWriter
extends BufferedImageConverter[doodle.java2d.Algebra, Frame] {
def makeImage(width: Int, height: Int): BufferedImage =
new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB)

def bufferedImage[A](
frame: Frame,
picture: Picture[A]
): IO[(A, BufferedImage)] = for {
result <- Java2dWriter.renderBufferedImage(
frame.size,
frame.center,
frame.background,
picture
)(makeImage _)
(bi, a) = result
} yield (a, bi)
}
4 changes: 4 additions & 0 deletions java2d/src/main/scala/doodle/java2d/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import doodle.interact.effect.AnimationRenderer
import doodle.interact.effect.AnimationWriter
import doodle.java2d.algebra.reified.Reification
import doodle.language.Basic
import doodle.effect.BufferedImageConverter

package object java2d extends Java2dToPicture {
type Algebra =
Expand Down Expand Up @@ -65,6 +66,9 @@ package object java2d extends Java2dToPicture {
implicit val java2dPdfWriter
: Writer[Algebra, Frame, Pdf] with Base64[Algebra, Frame, Pdf] =
doodle.java2d.effect.Java2dPdfWriter
implicit val java2dBufferedImageWriter
: BufferedImageConverter[doodle.java2d.Algebra, Frame] =
doodle.java2d.effect.Java2dBufferedImageWriter

val Frame = doodle.java2d.effect.Frame

Expand Down
Loading