-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support native image encoding on native image formats (#1120)
* Support native image encoding on native image formats * Try to better handle CG objects
- Loading branch information
Showing
12 changed files
with
226 additions
and
76 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
5 changes: 5 additions & 0 deletions
5
korim/src/commonMain/kotlin/com/soywiz/korim/format/ImageDataContainer.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 |
---|---|---|
@@ -1,10 +1,15 @@ | ||
package com.soywiz.korim.format | ||
|
||
import com.soywiz.korim.bitmap.* | ||
|
||
open class ImageDataContainer( | ||
val imageDatas: List<ImageData> | ||
) { | ||
constructor(bitmap: Bitmap) : this(listOf(ImageData(bitmap))) | ||
|
||
val imageDatasByName = imageDatas.associateBy { it.name } | ||
val default = imageDatasByName[null] ?: imageDatas.first() | ||
val mainBitmap get() = default.mainBitmap | ||
|
||
operator fun get(name: String?): ImageData? = imageDatasByName[name] | ||
} |
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
21 changes: 21 additions & 0 deletions
21
korim/src/commonTest/kotlin/com/soywiz/korim/format/NativeEncodingTest.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,21 @@ | ||
package com.soywiz.korim.format | ||
|
||
import com.soywiz.kmem.* | ||
import com.soywiz.korim.bitmap.* | ||
import com.soywiz.korim.color.* | ||
import com.soywiz.korio.async.* | ||
import com.soywiz.korio.stream.* | ||
import com.soywiz.korma.geom.* | ||
import kotlin.test.* | ||
|
||
class NativeEncodingTest { | ||
@Test | ||
fun test() = suspendTest { | ||
if (Platform.isJsNodeJs) RegisteredImageFormats.register(PNG) | ||
val bytes = nativeImageFormatProvider.encodeSuspend(Bitmap32(10, 10, Colors.RED), ImageEncodingProps("image.png")) | ||
assertEquals(Size(10, 10), PNG.decodeHeader(bytes.openSync())!!.size) | ||
|
||
val image = nativeImageFormatProvider.decodeSuspend(bytes) | ||
assertEquals(Colors.RED, image.toBMP32()[0, 0]) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
korim/src/darwinMain/kotlin/com/soywiz/korim/format/cg/CGImageExt.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,25 @@ | ||
package com.soywiz.korim.format.cg | ||
|
||
import com.soywiz.korim.bitmap.* | ||
import kotlinx.cinterop.* | ||
import platform.CoreGraphics.* | ||
|
||
fun Bitmap32.toCGImage(): CGImageRef? { | ||
return transferBitmap32ToCGImage(this, null) | ||
} | ||
|
||
fun CGImageRef.toBitmap32(): Bitmap32 { | ||
val image = this | ||
val width = CGImageGetWidth(image).toInt() | ||
val height = CGImageGetHeight(image).toInt() | ||
val colorSpace = CGColorSpaceCreateDeviceRGB() | ||
val ctx = CGBitmapContextCreate( | ||
null, width.convert(), height.convert(), | ||
8.convert(), 0.convert(), colorSpace, | ||
CGImageAlphaInfo.kCGImageAlphaPremultipliedLast.value | ||
) | ||
CGContextDrawImage(ctx, CGRectMake(0.cg, 0.cg, width.cg, height.cg), image) | ||
val out = Bitmap32(width, height, premultiplied = true) | ||
transferBitmap32CGContext(out, ctx, toBitmap = true) | ||
return out | ||
} |
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
19 changes: 17 additions & 2 deletions
19
korim/src/darwinMobileMain/kotlin/com/soywiz/korim/format/NativeImageFormatProviderIos.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
1 change: 1 addition & 0 deletions
1
.../com/soywiz/korim/format/ui/UIImageExt.kt → .../com/soywiz/korim/format/ui/UIImageExt.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
Oops, something went wrong.