Skip to content

Commit

Permalink
Add support for java.awt.image.RenderedImage
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunk committed May 12, 2019
1 parent 9dc45a0 commit 550d191
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions modules/scala/jupyter-api/src/main/scala/almond/display/Image.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package almond.display

import java.io.{ByteArrayInputStream, IOException}
import java.awt.image.RenderedImage
import java.io.{ByteArrayInputStream, ByteArrayOutputStream, IOException}
import java.net.{HttpURLConnection, URL, URLConnection}
import java.util.Base64

import javax.imageio.ImageIO

import scala.util.Try

final class Image private (
val width: Option[String],
val height: Option[String],
val width: Option[Int],
val height: Option[Int],
val format: Option[Image.Format],
byteArrayOrUrl: Either[URL, Array[Byte]],
val embed: Boolean,
Expand All @@ -21,8 +24,8 @@ final class Image private (
byteArrayOrUrl.left.toOption

private def copy(
width: Option[String] = width,
height: Option[String] = height,
width: Option[Int] = width,
height: Option[Int] = height,
format: Option[Image.Format] = format,
byteArrayOrUrl: Either[URL, Array[Byte]] = byteArrayOrUrl,
embed: Boolean = embed
Expand All @@ -43,12 +46,12 @@ final class Image private (
def withUrl(url: String): Image =
copy(byteArrayOrUrl = Left(new URL(url)))
def withHeight(height: Int): Image =
copy(height = Some(height.toString))
def withHeight(heightOpt: Option[String]): Image =
copy(height = Some(height))
def withHeight(heightOpt: Option[Int]): Image =
copy(height = heightOpt)
def withWidth(width: Int): Image =
copy(width = Some(width.toString))
def withWidth(widthOpt: Option[String]): Image =
copy(width = Some(width))
def withWidth(widthOpt: Option[Int]): Image =
copy(width = widthOpt)
def withFormat(format: Image.Format): Image =
copy(format = Some(format))
Expand All @@ -61,8 +64,8 @@ final class Image private (

override def metadata(): Map[String, String] =
Map() ++
width.map("width" -> _) ++
height.map("height" -> _)
width.map("width" -> _.toString) ++
height.map("height" -> _.toString)

def data(): Map[String, String] =
byteArrayOrUrl match {
Expand Down Expand Up @@ -108,14 +111,27 @@ final class Image private (

object Image extends Display.Builder[Array[Byte], Image] {

def fromRenderedImage(image: RenderedImage, format: Format = JPG): Image = {
val output: ByteArrayOutputStream = new ByteArrayOutputStream()
ImageIO.write(image, format.toString, output)
new Image(
width = Some(image.getWidth),
height = Some(image.getHeight),
format = Some(format),
byteArrayOrUrl = Right(output.toByteArray),
embed = true,
displayId = UpdatableDisplay.generateId()
)
}

protected def build(contentOrUrl: Either[URL, Array[Byte]]): Image =
new Image(
None,
None,
None,
contentOrUrl,
width = None,
height = None,
format = None,
byteArrayOrUrl = contentOrUrl,
embed = contentOrUrl.left.exists(_.getProtocol == "file"),
UpdatableDisplay.generateId()
displayId = UpdatableDisplay.generateId()
)

sealed abstract class Format(val contentType: String) extends Product with Serializable
Expand Down

0 comments on commit 550d191

Please sign in to comment.