-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Screenshotting functionality. (#110)
- Loading branch information
Showing
4 changed files
with
92 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
skiko/src/jvmMain/kotlin/org/jetbrains/skiko/Convertors.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.jetbrains.skiko | ||
|
||
import org.jetbrains.skija.Bitmap | ||
import org.jetbrains.skija.ColorType | ||
import java.awt.Transparency | ||
import java.awt.color.ColorSpace | ||
import java.awt.image.BufferedImage | ||
import java.awt.image.ComponentColorModel | ||
import java.awt.image.DataBuffer | ||
import java.awt.image.Raster | ||
import java.nio.ByteBuffer | ||
|
||
private class DirectDataBuffer(val backing: ByteBuffer): DataBuffer(TYPE_BYTE, backing.limit()) { | ||
override fun getElem(bank: Int, index: Int): Int { | ||
return backing[index].toInt() | ||
} | ||
override fun setElem(bank: Int, index: Int, value: Int) { | ||
throw UnsupportedOperationException("no write access") | ||
} | ||
} | ||
|
||
fun Bitmap.toBufferedImage(): BufferedImage { | ||
val pixels = this.peekPixels() | ||
val order = when (this.colorInfo.colorType) { | ||
ColorType.RGB_888X -> intArrayOf(0, 1, 2, 3) | ||
ColorType.BGRA_8888 -> intArrayOf(2, 1, 0, 3) | ||
else -> throw UnsupportedOperationException("unsupported color type ${this.colorInfo.colorType}") | ||
} | ||
val raster = Raster.createInterleavedRaster( | ||
DirectDataBuffer(pixels!!), | ||
this.width, | ||
this.height, | ||
this.width * 4, | ||
4, | ||
order, | ||
null | ||
) | ||
val colorModel = ComponentColorModel( | ||
ColorSpace.getInstance(ColorSpace.CS_sRGB), | ||
true, | ||
false, | ||
Transparency.TRANSLUCENT, | ||
DataBuffer.TYPE_BYTE | ||
) | ||
return BufferedImage(colorModel, raster!!, false, null) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters