forked from mihonapp/bitmap.kt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c44de3c
Showing
24 changed files
with
832 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
*.iml | ||
.gradle | ||
.idea | ||
.DS_Store | ||
build | ||
captures | ||
.externalNativeBuild | ||
.cxx | ||
local.properties | ||
xcuserdata |
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,43 @@ | ||
plugins { | ||
alias(libs.plugins.kotlinMultiplatform) | ||
alias(libs.plugins.androidLibrary) | ||
} | ||
|
||
kotlin { | ||
jvmToolchain(8) | ||
androidTarget { | ||
compilations.all { | ||
kotlinOptions { | ||
jvmTarget = "1.8" | ||
} | ||
} | ||
} | ||
jvm { | ||
compilations.all { | ||
kotlinOptions { | ||
jvmTarget = "1.8" | ||
} | ||
} | ||
} | ||
|
||
// iosX64() | ||
// iosArm64() | ||
// iosSimulatorArm64() | ||
|
||
sourceSets { | ||
commonMain.dependencies { | ||
implementation(libs.kotlinx.io) | ||
} | ||
commonTest.dependencies { | ||
implementation(libs.kotlin.test) | ||
} | ||
} | ||
} | ||
|
||
android { | ||
namespace = "app.mihon.bitmap" | ||
compileSdk = 34 | ||
defaultConfig { | ||
minSdk = 21 | ||
} | ||
} |
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,58 @@ | ||
package app.mihon.bitmap | ||
|
||
import android.annotation.TargetApi | ||
import android.os.Build | ||
import kotlinx.io.Sink | ||
import kotlinx.io.asOutputStream | ||
|
||
actual class Bitmap(val image: android.graphics.Bitmap) { | ||
actual val width: Int = image.width | ||
actual val height: Int = image.height | ||
|
||
actual enum class CompressFormat(val native: android.graphics.Bitmap.CompressFormat) { | ||
JPEG(android.graphics.Bitmap.CompressFormat.JPEG), | ||
PNG(android.graphics.Bitmap.CompressFormat.PNG), | ||
@TargetApi(Build.VERSION_CODES.R) | ||
WEBP_LOSSY(android.graphics.Bitmap.CompressFormat.WEBP_LOSSY), | ||
@TargetApi(Build.VERSION_CODES.R) | ||
WEBP_LOSSLESS(android.graphics.Bitmap.CompressFormat.WEBP_LOSSLESS) | ||
} | ||
|
||
actual enum class Config(val native: android.graphics.Bitmap.Config) { | ||
ALPHA_8(android.graphics.Bitmap.Config.ALPHA_8), | ||
RGB_565(android.graphics.Bitmap.Config.RGB_565), | ||
ARGB_8888(android.graphics.Bitmap.Config.ARGB_8888), | ||
@TargetApi(Build.VERSION_CODES.O) | ||
RGBA_F16(android.graphics.Bitmap.Config.RGBA_F16), | ||
@TargetApi(Build.VERSION_CODES.O) | ||
HARDWARE(android.graphics.Bitmap.Config.HARDWARE), | ||
@TargetApi(Build.VERSION_CODES.TIRAMISU) | ||
RGBA_1010102(android.graphics.Bitmap.Config.RGBA_1010102); | ||
} | ||
|
||
actual fun compress(format: CompressFormat, quality: Int, sink: Sink): Boolean { | ||
return image.compress(format.native, quality, sink.asOutputStream()) | ||
} | ||
|
||
actual fun getPixels( | ||
/*@ColorInt*/ pixels: IntArray, | ||
offset: Int, | ||
stride: Int, | ||
x: Int, | ||
y: Int, | ||
width: Int, | ||
height: Int | ||
) { | ||
image.getPixels(pixels, offset, stride, x, y, width, height) | ||
} | ||
|
||
actual companion object { | ||
actual fun createBitmap(width: Int, height: Int, config: Config): Bitmap { | ||
return Bitmap(android.graphics.Bitmap.createBitmap(width, height, config.native)) | ||
} | ||
|
||
actual fun createBitmap(source: Bitmap, x: Int, y: Int, width: Int, height: Int): Bitmap { | ||
return Bitmap(android.graphics.Bitmap.createBitmap(source.image, x, y, width, height)) | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
bitmap/src/androidMain/kotlin/app/mihon/bitmap/BitmapFactory.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,15 @@ | ||
package app.mihon.bitmap | ||
|
||
import android.graphics.BitmapFactory | ||
import kotlinx.io.Source | ||
import kotlinx.io.asInputStream | ||
|
||
actual object BitmapFactory { | ||
actual fun decodeStream(source: Source): Bitmap { | ||
return Bitmap(BitmapFactory.decodeStream(source.asInputStream())) | ||
} | ||
|
||
actual fun decodeByteArray(data: ByteArray, offset: Int, length: Int): Bitmap? { | ||
return BitmapFactory.decodeByteArray(data, offset, length)?.let { Bitmap(it) } | ||
} | ||
} |
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,16 @@ | ||
package app.mihon.bitmap | ||
|
||
import android.graphics.Canvas | ||
|
||
class Canvas(bitmap: Bitmap) { | ||
val canvas = Canvas(bitmap.image) | ||
|
||
fun drawBitmap(sourceBitmap: Bitmap, src: Rect, dst: Rect, paint: Paint?) { | ||
canvas.drawBitmap( | ||
sourceBitmap.image, | ||
android.graphics.Rect(src.left, src.top, src.right, src.bottom), | ||
android.graphics.Rect(dst.left, dst.top, dst.right, dst.bottom), | ||
paint?.paint | ||
) | ||
} | ||
} |
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,5 @@ | ||
package app.mihon.bitmap | ||
|
||
import android.graphics.Paint | ||
|
||
actual class Paint(val paint: Paint) |
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,43 @@ | ||
package app.mihon.bitmap | ||
|
||
import kotlinx.io.Sink | ||
|
||
expect class Bitmap { | ||
val width: Int | ||
val height: Int | ||
|
||
enum class CompressFormat { | ||
JPEG, | ||
PNG, | ||
WEBP_LOSSY, | ||
WEBP_LOSSLESS | ||
} | ||
|
||
enum class Config { | ||
ALPHA_8, | ||
RGB_565, | ||
ARGB_8888, | ||
RGBA_F16, | ||
HARDWARE, | ||
RGBA_1010102 | ||
} | ||
|
||
fun compress(format: CompressFormat, quality: Int, sink: Sink): Boolean | ||
|
||
fun getPixels( | ||
/*@ColorInt*/ pixels: IntArray, | ||
offset: Int, | ||
stride: Int, | ||
x: Int, | ||
y: Int, | ||
width: Int, | ||
height: Int | ||
) | ||
|
||
companion object { | ||
|
||
fun createBitmap(width: Int, height: Int, config: Config): Bitmap | ||
|
||
fun createBitmap(source: Bitmap, x: Int, y: Int, width: Int, height: Int): Bitmap | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
bitmap/src/commonMain/kotlin/app/mihon/bitmap/BitmapFactory.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,15 @@ | ||
package app.mihon.bitmap | ||
|
||
import kotlinx.io.Source | ||
import java.awt.image.BufferedImage | ||
import java.io.ByteArrayInputStream | ||
import java.io.IOException | ||
import java.io.InputStream | ||
import javax.imageio.ImageIO | ||
import javax.imageio.ImageReader | ||
|
||
expect object BitmapFactory { | ||
fun decodeStream(source: Source): Bitmap | ||
|
||
fun decodeByteArray(data: ByteArray, offset: Int, length: Int): Bitmap? | ||
} |
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,6 @@ | ||
package app.mihon.bitmap | ||
|
||
|
||
expect class Canvas(bitmap: Bitmap) { | ||
fun drawBitmap(sourceBitmap: Bitmap, src: Rect, dst: Rect) | ||
} |
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,6 @@ | ||
package app.mihon.bitmap | ||
|
||
/** | ||
* TODO implement | ||
*/ | ||
expect class Paint |
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,13 @@ | ||
package app.mihon.bitmap | ||
|
||
data class Rect( | ||
val left: Int = 0, | ||
val top: Int = 0, | ||
val right: Int = 0, | ||
val bottom: Int = 0, | ||
) { | ||
val width: Int | ||
get() = right - left | ||
val height: Int | ||
get() = bottom - top | ||
} |
Oops, something went wrong.