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 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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 doodle.core.format.Format
import java.awt.image.BufferedImage

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

class BufferedImageConverterIOOpsHelper[Fmt <: Format](
picture: Picture[Alg, A]
) {
def apply[Frame](frame: Frame)(implicit
w: BufferedImageConverter[Alg, Frame, Fmt],
r: IORuntime
): IO[(A, BufferedImage)] =
w.bufferedImage(frame, picture)
}

def bufferedImage[Fmt <: Format] =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the Fmt type is needed. This type specifies a file format (like PNG or JPEG), which is not relevant for saving to an in-memory bitmap.

If the Fmt types goes the helper classes are no longer necessary.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just added the format becuase the renderBufferedImage() requires a makeImage() function which defined for each Java2d[Format]Writer object
image
Should I just create a separate object for the BufferedImageConverter with its own makeImage() function and make this the only implicit BufferedImageConverter?

new BufferedImageConverterOpsHelper[Fmt](picture)

def bufferedImageToIO[Fmt <: Format] =
new BufferedImageConverterIOOpsHelper[Fmt](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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this compile on the Javascript platform? E.g. does coreJS / fastOptJS succeed?

I suspect the BufferedImage type doesn't exist on JS, and hence it won't compile. This mean either:

  1. this type needs to become generic in the bitmap type or
  2. this type needs to migrate to live only on the JVM platform (and possibly only on the Java2D backend.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are probably right. I was just following what Base64 does. I will move it to the Java2D specific directory.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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
import doodle.core.format.Format

trait BufferedImageConverter[+Alg <: Algebra, Frame, Fmt <: Format] {
def bufferedImage[A](
description: Frame,
picture: Picture[Alg, A]
): IO[(A, BufferedImage)]
}
16 changes: 15 additions & 1 deletion java2d/src/main/scala/doodle/java2d/effect/Java2dWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ import javax.imageio.ImageIO

trait Java2dWriter[Fmt <: Format]
extends Writer[doodle.java2d.Algebra, Frame, Fmt]
with Base64[doodle.java2d.Algebra, Frame, Fmt] {
with Base64[doodle.java2d.Algebra, Frame, Fmt]
with BufferedImageConverter[doodle.java2d.Algebra, Frame, Fmt] {
def format: String

// Allows formats to control the encoding of the buffered image. Not all
Expand Down Expand Up @@ -79,6 +80,19 @@ trait Java2dWriter[Fmt <: Format]
def base64[A](image: Picture[A]): IO[(A, B64[Fmt])] =
base64(Frame.default.withSizedToPicture(20), image)

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)

private def writeToOutput[A](
output: OutputStream,
frame: Frame,
Expand Down
21 changes: 13 additions & 8 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 @@ -53,17 +54,21 @@ package object java2d extends Java2dToPicture {
implicit val java2dRenderer
: DefaultRenderer[Algebra, doodle.java2d.effect.Frame, Canvas] =
doodle.java2d.effect.Java2dRenderer
implicit val java2dGifWriter
: Writer[Algebra, Frame, Gif] with Base64[Algebra, Frame, Gif] =
implicit val java2dGifWriter: Writer[Algebra, Frame, Gif]
with Base64[Algebra, Frame, Gif]
with BufferedImageConverter[Algebra, Frame, Gif] =
doodle.java2d.effect.Java2dGifWriter
implicit val java2dPngWriter
: Writer[Algebra, Frame, Png] with Base64[Algebra, Frame, Png] =
implicit val java2dPngWriter: Writer[Algebra, Frame, Png]
with Base64[Algebra, Frame, Png]
with BufferedImageConverter[Algebra, Frame, Png] =
doodle.java2d.effect.Java2dPngWriter
implicit val java2dJpgWriter
: Writer[Algebra, Frame, Jpg] with Base64[Algebra, Frame, Jpg] =
implicit val java2dJpgWriter: Writer[Algebra, Frame, Jpg]
with Base64[Algebra, Frame, Jpg]
with BufferedImageConverter[Algebra, Frame, Jpg] =
doodle.java2d.effect.Java2dJpgWriter
implicit val java2dPdfWriter
: Writer[Algebra, Frame, Pdf] with Base64[Algebra, Frame, Pdf] =
implicit val java2dPdfWriter: Writer[Algebra, Frame, Pdf]
with Base64[Algebra, Frame, Pdf]
with BufferedImageConverter[Algebra, Frame, Pdf] =
doodle.java2d.effect.Java2dPdfWriter

val Frame = doodle.java2d.effect.Frame
Expand Down
Loading