Commit d9c89fb4 authored by David's avatar David

Adding the method from the javafx JDK

parent 302bde05
package `is`.kow.deskscreen.utils
import javafx.scene.image.Image
import javafx.scene.image.PixelFormat
import javafx.scene.image.WritableImage
import sun.awt.image.IntegerComponentRaster
import java.awt.image.BufferedImage
import java.io.InputStream
import java.nio.file.Files
import java.nio.file.Path
......@@ -31,4 +36,79 @@ object Util {
fun md5File(path: Path): String {
return md5Stuff(Files.newInputStream(path))
}
/**
* Stolen from the JDK, because it's apparently not in the javafx on the rpi. Maybe different versions?
* Snapshots the specified [BufferedImage] and stores a copy of
* its pixels into a JavaFX [Image] object, creating a new
* object if needed.
* The returned `Image` will be a static snapshot of the state
* of the pixels in the `BufferedImage` at the time the method
* completes. Further changes to the `BufferedImage` will not
* be reflected in the `Image`.
*
*
* The optional JavaFX [WritableImage] parameter may be reused
* to store the copy of the pixels.
* A new `Image` will be created if the supplied object is null,
* is too small or of a type which the image pixels cannot be easily
* converted into.
*
* @param bimg the `BufferedImage` object to be converted
* @param wimg an optional `WritableImage` object that can be
* used to store the returned pixel data
* @return an `Image` object representing a snapshot of the
* current pixels in the `BufferedImage`.
* @since JavaFX 2.2
*/
fun toFXImage(bimg: BufferedImage, wimg: WritableImage?): WritableImage {
var bimg = bimg
var wimg = wimg
val bw = bimg.width
val bh = bimg.height
when (bimg.type) {
BufferedImage.TYPE_INT_ARGB, BufferedImage.TYPE_INT_ARGB_PRE -> {
}
else -> {
val converted = BufferedImage(bw, bh, BufferedImage.TYPE_INT_ARGB_PRE)
val g2d = converted.createGraphics()
g2d.drawImage(bimg, 0, 0, null)
g2d.dispose()
bimg = converted
}
}
// assert(bimg.getType == TYPE_INT_ARGB[_PRE]);
if (wimg != null) {
val iw = wimg.width.toInt()
val ih = wimg.height.toInt()
if (iw < bw || ih < bh) {
wimg = null
} else if (bw < iw || bh < ih) {
val empty = IntArray(iw)
val pw = wimg.pixelWriter
val pf = PixelFormat.getIntArgbPreInstance()
if (bw < iw) {
pw.setPixels(bw, 0, iw - bw, bh, pf, empty, 0, 0)
}
if (bh < ih) {
pw.setPixels(0, bh, iw, ih - bh, pf, empty, 0, 0)
}
}
}
if (wimg == null) {
wimg = WritableImage(bw, bh)
}
val pw = wimg.pixelWriter
val icr = bimg.raster as IntegerComponentRaster
val data = icr.dataStorage
val offset = icr.getDataOffset(0)
val scan = icr.scanlineStride
val pf = if (bimg.isAlphaPremultiplied)
PixelFormat.getIntArgbPreInstance()
else
PixelFormat.getIntArgbInstance()
pw.setPixels(0, 0, bw, bh, pf, data, offset, scan)
return wimg
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment