Help converting project from scala 2.11.8 to 2.13

I’ve been working on a scala project which was created a while ago using scala 2.11.8.
I’m trying to bring it up to date to use 2.13.

The project uses sksamuel/scrimage. And I don’t understand the notation for updating the libraryDependencies in the new project.

Here is what the README.md file says. What is 2.x.x? Is it the scala version? or is it the scrimage version? Do I need to change x.x to something?

libraryDependencies += "com.sksamuel.scrimage" %% "scrimage-core" % "2.x.x"

libraryDependencies += "com.sksamuel.scrimage" %% "scrimage-io-extra" % "2.x.x"

libraryDependencies += "com.sksamuel.scrimage" %% "scrimage-filters" % "2.x.x"

On the other hand, I believe my dependence on scrimage is pretty flexible. I could refactor the project to use a different library pretty easily. I basically need some Pixel type which encodes the color of a pixel, and a way to convert an array of Pixel objects into an image file such as PNG. Which library should I use for this?

As for your original question, you can look on a website such as mvnrepository or index.scala-lang.org which versions are available for which Scala version. However, despite 2.13 support having been added in the github repository, it seems like no new version has been released in a long time. It looks like some people have released their own forks though.

What does " % 2.x.x" mean?

It’s how the author of the library tries to communicate that you can choose any version that starts with 2. Preferably the latest version, I guess.

So to be clear is it the version of scrimage, or the version of Scala?

In sbt syntax

"com.sksamuel.scrimage" %% "scrimage-core" % "2.x.x"

the first %% means, append the Scala binary version, so that in your case "scrimage-core" would become "scrimage-core_2.13". The last % specifies the library version. That you’ll have to look up, x.x is just a placeholder by the author, so they don’t need to change the README every time the version changes. For example:

https://search.maven.org/search?q=a:scrimage-core_2.13

shows that latest released version for Scala 2.13 is 2.1.10. So you could probably write

libraryDependencies += "com.malliina" %% "scrimage-core" % "2.1.10"

(this seems to be a fork off the project with different group identifier!)

2 Likes

This is translated to:

libraryDependencies += "com.sksamuel.scrimage" % s"scrimage-core_${scalaBinaryVersion}" % "2.x.x"
// or eqivalently, e.g. (if the currently selected binary Scala version is 2.12)
libraryDependencies += "com.sksamuel.scrimage" % "scrimage-core_2.12" % "2.x.x"

Note that SBT will show you the URLs it tries when searching for artifacts and you can often open them in browser, cut some suffix and browse available artifacts. Try that on some artifacts and you’ll get the intuition on which URL part corresponds to Scala binary version and which to artifact version.

Java platform has built in capabilities for that. See: https://docs.oracle.com/javase/tutorial/2d/images/saveimage.html

It also seems that scalafx supports image creation. Since I plan, anyway, on using scalafx in my project. I think I’ll investigate it. There’s a short video by Mark Lewis introducing image creation using scalafx.

1 Like

Hmm. I don’t see how to write a scalafx.scene.image.WritableImage to a file. :frowning:
It does not seem to have an output method, nor toFile etc

It seems that you need to use SwingFXUtils to convert JavaFX image to ordinary AWT image and then save it using old method.

I think I almost have it. But I can’t figure out how to write the image file.


  def output(fileName: String) = {

    import javafx.embed.swing.SwingFXUtils._
    import java.awt.image.BufferedImage._

    val bimg = new java.awt.image.BufferedImage(width,height,TYPE_INT_ARGB)
    toFXImage(bimg,wimg)
    bimg.???
  }

If you already have a java.awt.image.BufferedImage, you don’t need to go back to JavaFX. Just use

javax.imageio.ImageIO.write(bimg, "png", new java.io.File("output.png"))

IMHO, JavaFX is highly overrated ;))

1 Like

ScalaFX is what Mark Lewis uses in his book. I think he does a great job of presenting something a student can use and understand.

2 Likes

It is much harder to write images using JavaFX than Swing. The gain from this was supposed to be speed in rendering by having better support for graphics cards. Unfortunately, in my testing the 2D graphics speed of Swing is much better than JavaFX. I’m sure JavaFX is better for 3D since that wasn’t really an option with Swing.

Use 2.+ version instead. See https://github.com/sksamuel/scrimage/pull/186

Is that enough to use with Scala 2.13.x?

Can you explain what you mean by much harder ? Do you mean the API is more obscure?

It is less straightforward. Most of it is just knowing enough about the API in order to make it happen. Even if you know the API there is more code involved in making it happen. I believe that it also forces you to do it in a way that works asynchronously, though I haven’t done it in a while so I can’t be certain of that. Since it is something I want to show novice programmers, the main thing that stood out to me was that I could do it in Swing and it was straightforward for the students. The move the JavaFX/ScalaFX changed that.