@g9tm/lynxcontrols-native 2.6.19 → 2.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -1
- package/android/build.gradle.kts +7 -0
- package/android/src/main/java/com/g9lynx/g9native/G9NativeImages.kt +16 -0
- package/android/src/main/java/com/g9lynx/g9native/canvas/G9CanvasModel.kt +2 -2
- package/android/src/main/java/com/g9lynx/g9native/canvas/G9CanvasPaths.kt +7 -26
- package/android/src/main/java/com/g9lynx/g9native/canvas/G9CanvasView.kt +9 -4
- package/android/src/main/java/com/g9lynx/g9native/generated/G9ImageModuleSpec.java +27 -0
- package/android/src/main/java/com/g9lynx/g9native/image/G9HostTypeface.kt +49 -0
- package/android/src/main/java/com/g9lynx/g9native/image/G9ImageDraw.kt +222 -0
- package/android/src/main/java/com/g9lynx/g9native/image/G9ImageModule.kt +54 -0
- package/android/src/main/java/com/g9lynx/g9native/image/G9ImageProcessor.kt +464 -0
- package/android/src/main/java/com/g9lynx/g9native/image/core/G9HostFont.kt +28 -0
- package/android/src/main/java/com/g9lynx/g9native/image/core/G9ImageBudget.kt +54 -0
- package/android/src/main/java/com/g9lynx/g9native/image/core/G9ImageColor.kt +48 -0
- package/android/src/main/java/com/g9lynx/g9native/image/core/G9ImageError.kt +19 -0
- package/android/src/main/java/com/g9lynx/g9native/image/core/G9ImageExifTags.kt +61 -0
- package/android/src/main/java/com/g9lynx/g9native/image/core/G9ImageFiles.kt +99 -0
- package/android/src/main/java/com/g9lynx/g9native/image/core/G9ImageGeometry.kt +150 -0
- package/android/src/main/java/com/g9lynx/g9native/image/core/G9ImageJson.kt +200 -0
- package/android/src/main/java/com/g9lynx/g9native/image/core/G9ImageQuality.kt +65 -0
- package/android/src/main/java/com/g9lynx/g9native/image/core/G9ImageRedaction.kt +77 -0
- package/android/src/main/java/com/g9lynx/g9native/image/core/G9ImageService.kt +102 -0
- package/android/src/main/java/com/g9lynx/g9native/image/core/G9ImageSniff.kt +63 -0
- package/android/src/main/java/com/g9lynx/g9native/image/core/G9ImageSpec.kt +237 -0
- package/catalog/catalog.json +161 -1
- package/catalog/index.md +1 -1
- package/dist/.tsbuildinfo +1 -1
- package/dist/image.d.ts +219 -0
- package/dist/image.d.ts.map +1 -0
- package/dist/image.js +116 -0
- package/dist/image.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/protocol.d.ts +12 -0
- package/dist/protocol.d.ts.map +1 -1
- package/generated/G9ImageModule.ts +64 -0
- package/ios/Tests/G9NativeTestSuite.h +8 -0
- package/ios/Tests/G9NativeTestSuite.m +350 -0
- package/ios/Tests/G9NativeTests.m +16 -0
- package/ios/g9lynx-native-swift.podspec +1 -1
- package/ios/g9lynx-native.podspec +5 -4
- package/ios/src/G9ImageModule.h +30 -0
- package/ios/src/G9ImageModule.m +986 -0
- package/ios/src/core/G9ImageCore.h +201 -0
- package/ios/src/core/G9ImageCore.m +1039 -0
- package/ios/src/generated/G9ImageModuleSpec.h +16 -0
- package/ios/src/generated/G9ImageModuleSpec.m +2 -0
- package/ios/swift/canvas/G9LCCanvasCore.swift +3 -2
- package/ios/swift/canvas/G9LCCanvasView.swift +15 -4
- package/ios/swift/shared/G9LCFonts.swift +18 -2
- package/package.json +4 -4
- package/types/platform-native-module.d.ts +18 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
package com.g9lynx.g9native.image.core
|
|
2
|
+
|
|
3
|
+
import java.net.URI
|
|
4
|
+
|
|
5
|
+
/** Where a source is read from: a [file] path, or (Android) a `content://` [uri]. */
|
|
6
|
+
data class G9ImageSource(val file: String?, val uri: String?) {
|
|
7
|
+
companion object {
|
|
8
|
+
/** A plain path, a `file://` URI (percent-decoded) or a `content://` URI. */
|
|
9
|
+
fun parse(path: String?): G9ImageSource {
|
|
10
|
+
val p = path?.trim().orEmpty()
|
|
11
|
+
if (p.isEmpty()) throw G9ImageError.invalid("path is required.")
|
|
12
|
+
if (p.startsWith("content://")) return G9ImageSource(null, p)
|
|
13
|
+
if (p.startsWith("file:")) return G9ImageSource(filePath(p), null)
|
|
14
|
+
return G9ImageSource(p, null)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** A folder given as a plain path or a `file://` URI. */
|
|
18
|
+
fun folder(dir: String?): String? {
|
|
19
|
+
val d = dir?.trim().orEmpty()
|
|
20
|
+
if (d.isEmpty()) return null
|
|
21
|
+
return if (d.startsWith("file:")) filePath(d) else d
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
private fun filePath(uri: String): String {
|
|
25
|
+
// An unescaped URI (a space in it) is taken as written after the scheme.
|
|
26
|
+
val path = runCatching { URI(uri).path }.getOrNull() ?: uri.removePrefix("file://")
|
|
27
|
+
if (path.isEmpty()) throw G9ImageError.invalid("Not a file URI: $uri")
|
|
28
|
+
return path
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Output formats (`jpeg | png | webp`) and what follows from them. */
|
|
34
|
+
object G9ImageFormat {
|
|
35
|
+
/** The short name of a source MIME type (`jpeg`, `png`, `webp`, `heic`, `gif`, `bmp`), or null. */
|
|
36
|
+
fun fromMime(mime: String?): String? =
|
|
37
|
+
when (mime?.lowercase()) {
|
|
38
|
+
"image/jpeg", "image/jpg", "image/pjpeg" -> "jpeg"
|
|
39
|
+
"image/png" -> "png"
|
|
40
|
+
"image/webp" -> "webp"
|
|
41
|
+
"image/heic", "image/heif", "image/heic-sequence", "image/heif-sequence" -> "heic"
|
|
42
|
+
"image/gif" -> "gif"
|
|
43
|
+
"image/bmp", "image/x-ms-bmp" -> "bmp"
|
|
44
|
+
else -> null
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The format to encode: the request, or for `source` the source's own (HEIC and anything else → jpeg). */
|
|
48
|
+
fun resolve(requested: String, sourceMime: String?): String =
|
|
49
|
+
when (requested) {
|
|
50
|
+
"source" -> when (val f = fromMime(sourceMime)) {
|
|
51
|
+
"png", "webp" -> f
|
|
52
|
+
else -> "jpeg"
|
|
53
|
+
}
|
|
54
|
+
else -> requested
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
fun mime(format: String) =
|
|
58
|
+
when (format) {
|
|
59
|
+
"png" -> "image/png"
|
|
60
|
+
"webp" -> "image/webp"
|
|
61
|
+
"heic" -> "image/heic"
|
|
62
|
+
"gif" -> "image/gif"
|
|
63
|
+
"bmp" -> "image/bmp"
|
|
64
|
+
else -> "image/jpeg"
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
fun extension(format: String?) =
|
|
68
|
+
when (format) {
|
|
69
|
+
"jpeg" -> "jpg"
|
|
70
|
+
null -> "bin"
|
|
71
|
+
else -> format
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Whether the encoded file keeps transparency (PNG, WebP). */
|
|
75
|
+
fun hasAlpha(format: String) = format == "png" || format == "webp"
|
|
76
|
+
|
|
77
|
+
fun lossy(format: String) = format == "jpeg" || format == "webp"
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** The output file's base name: the caller's, or `g9photo-<epoch ms>-<4 random hex>` not yet in the folder. */
|
|
81
|
+
object G9ImageNames {
|
|
82
|
+
fun default(nowMs: Long, random: Int) = "g9photo-$nowMs-" + String.format("%04x", random and 0xffff)
|
|
83
|
+
|
|
84
|
+
/** A default name for which [exists] (base + "." + [extension]) is false. */
|
|
85
|
+
fun unique(extension: String, exists: (String) -> Boolean, now: () -> Long, random: () -> Int): String {
|
|
86
|
+
repeat(64) {
|
|
87
|
+
val base = default(now(), random())
|
|
88
|
+
if (!exists("$base.$extension")) return base
|
|
89
|
+
}
|
|
90
|
+
throw G9ImageError(G9ImageError.IO, "No free file name in the folder.")
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** The caller's base name, which must be one plain file name. */
|
|
94
|
+
fun checked(name: String): String {
|
|
95
|
+
val n = name.trim()
|
|
96
|
+
if (n.isEmpty() || n == "." || n == ".." || n.any { it == '/' || it == '\\' || it < ' ' }) throw G9ImageError.invalid("name must be a plain file name: \"$name\".")
|
|
97
|
+
return n
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
package com.g9lynx.g9native.image.core
|
|
2
|
+
|
|
3
|
+
import kotlin.math.abs
|
|
4
|
+
import kotlin.math.ceil
|
|
5
|
+
import kotlin.math.floor
|
|
6
|
+
import kotlin.math.hypot
|
|
7
|
+
import kotlin.math.max
|
|
8
|
+
import kotlin.math.min
|
|
9
|
+
import kotlin.math.roundToInt
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 2-D affine transforms in the contract's order `[a, b, c, d, e, f]`: `x' = a·x + c·y + e`, `y' = b·x + d·y + f`
|
|
13
|
+
* (Android's `Matrix.setValues` wants `[a, c, e, b, d, f, 0, 0, 1]`).
|
|
14
|
+
*/
|
|
15
|
+
object G9Affine {
|
|
16
|
+
val IDENTITY: DoubleArray get() = doubleArrayOf(1.0, 0.0, 0.0, 1.0, 0.0, 0.0)
|
|
17
|
+
|
|
18
|
+
fun scale(sx: Double, sy: Double = sx) = doubleArrayOf(sx, 0.0, 0.0, sy, 0.0, 0.0)
|
|
19
|
+
|
|
20
|
+
fun translate(tx: Double, ty: Double) = doubleArrayOf(1.0, 0.0, 0.0, 1.0, tx, ty)
|
|
21
|
+
|
|
22
|
+
fun apply(m: DoubleArray, x: Double, y: Double) = doubleArrayOf(m[0] * x + m[2] * y + m[4], m[1] * x + m[3] * y + m[5])
|
|
23
|
+
|
|
24
|
+
/** The transform that applies [first], then [second]. */
|
|
25
|
+
fun then(first: DoubleArray, second: DoubleArray): DoubleArray {
|
|
26
|
+
val (a1, b1, c1, d1) = first
|
|
27
|
+
val e1 = first[4]
|
|
28
|
+
val f1 = first[5]
|
|
29
|
+
val (a2, b2, c2, d2) = second
|
|
30
|
+
return doubleArrayOf(
|
|
31
|
+
a2 * a1 + c2 * b1,
|
|
32
|
+
b2 * a1 + d2 * b1,
|
|
33
|
+
a2 * c1 + c2 * d1,
|
|
34
|
+
b2 * c1 + d2 * d1,
|
|
35
|
+
a2 * e1 + c2 * f1 + second[4],
|
|
36
|
+
b2 * e1 + d2 * f1 + second[5],
|
|
37
|
+
)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The inverse, or null when [m] is singular or not finite. */
|
|
41
|
+
fun invert(m: DoubleArray): DoubleArray? {
|
|
42
|
+
val det = m[0] * m[3] - m[1] * m[2]
|
|
43
|
+
if (!det.isFinite() || abs(det) < 1e-12) return null
|
|
44
|
+
val ia = m[3] / det
|
|
45
|
+
val ib = -m[1] / det
|
|
46
|
+
val ic = -m[2] / det
|
|
47
|
+
val id = m[0] / det
|
|
48
|
+
return doubleArrayOf(ia, ib, ic, id, -(ia * m[4] + ic * m[5]), -(ib * m[4] + id * m[5]))
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Android `Matrix.setValues` order. */
|
|
52
|
+
fun toAndroid(m: DoubleArray) = floatArrayOf(m[0].toFloat(), m[2].toFloat(), m[4].toFloat(), m[1].toFloat(), m[3].toFloat(), m[5].toFloat(), 0f, 0f, 1f)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The EXIF orientation (1–8): how the stored (raw) pixels map to what a person sees (oriented). */
|
|
56
|
+
object G9Orientation {
|
|
57
|
+
fun normalize(orientation: Int?) = if (orientation != null && orientation in 1..8) orientation else 1
|
|
58
|
+
|
|
59
|
+
fun swapsAxes(orientation: Int) = orientation in 5..8
|
|
60
|
+
|
|
61
|
+
/** `[width, height]` after the orientation is applied. */
|
|
62
|
+
fun orientedSize(rawWidth: Int, rawHeight: Int, orientation: Int) =
|
|
63
|
+
if (swapsAxes(orientation)) intArrayOf(rawHeight, rawWidth) else intArrayOf(rawWidth, rawHeight)
|
|
64
|
+
|
|
65
|
+
/** Raw px → oriented px (pixel edges: the whole image maps onto the whole oriented image). */
|
|
66
|
+
fun rawToOriented(orientation: Int, rawWidth: Int, rawHeight: Int): DoubleArray {
|
|
67
|
+
val w = rawWidth.toDouble()
|
|
68
|
+
val h = rawHeight.toDouble()
|
|
69
|
+
return when (normalize(orientation)) {
|
|
70
|
+
2 -> doubleArrayOf(-1.0, 0.0, 0.0, 1.0, w, 0.0) // mirror horizontal
|
|
71
|
+
3 -> doubleArrayOf(-1.0, 0.0, 0.0, -1.0, w, h) // rotate 180
|
|
72
|
+
4 -> doubleArrayOf(1.0, 0.0, 0.0, -1.0, 0.0, h) // mirror vertical
|
|
73
|
+
5 -> doubleArrayOf(0.0, 1.0, 1.0, 0.0, 0.0, 0.0) // transpose
|
|
74
|
+
6 -> doubleArrayOf(0.0, 1.0, -1.0, 0.0, h, 0.0) // rotate 90 clockwise
|
|
75
|
+
7 -> doubleArrayOf(0.0, -1.0, -1.0, 0.0, h, w) // transverse
|
|
76
|
+
8 -> doubleArrayOf(0.0, -1.0, 1.0, 0.0, 0.0, w) // rotate 90 counter-clockwise
|
|
77
|
+
else -> G9Affine.IDENTITY
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
fun orientedToRaw(orientation: Int, rawWidth: Int, rawHeight: Int): DoubleArray =
|
|
82
|
+
G9Affine.invert(rawToOriented(orientation, rawWidth, rawHeight))!!
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** A pixel rectangle, right and bottom exclusive. */
|
|
86
|
+
data class G9ImageBox(val left: Int, val top: Int, val right: Int, val bottom: Int) {
|
|
87
|
+
val width get() = right - left
|
|
88
|
+
val height get() = bottom - top
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Step 2 of the pipeline: which source pixels an output needs, and at which subsample. */
|
|
92
|
+
object G9ImageRegion {
|
|
93
|
+
const val MARGIN = 2
|
|
94
|
+
const val MAX_SAMPLE = 256
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The oriented-source box the output needs: its four corners through [matrix] (output → oriented source), widened by
|
|
98
|
+
* [margin] px for the bilinear filter and clamped to the image; null when no source pixel lands in the output.
|
|
99
|
+
*/
|
|
100
|
+
fun orientedBox(matrix: DoubleArray, outWidth: Int, outHeight: Int, orientedWidth: Int, orientedHeight: Int, margin: Int = MARGIN): G9ImageBox? {
|
|
101
|
+
val w = outWidth.toDouble()
|
|
102
|
+
val h = outHeight.toDouble()
|
|
103
|
+
val corners = listOf(G9Affine.apply(matrix, 0.0, 0.0), G9Affine.apply(matrix, w, 0.0), G9Affine.apply(matrix, 0.0, h), G9Affine.apply(matrix, w, h))
|
|
104
|
+
val left = max(0, floor(corners.minOf { it[0] }).toInt() - margin)
|
|
105
|
+
val top = max(0, floor(corners.minOf { it[1] }).toInt() - margin)
|
|
106
|
+
val right = min(orientedWidth, ceil(corners.maxOf { it[0] }).toInt() + margin)
|
|
107
|
+
val bottom = min(orientedHeight, ceil(corners.maxOf { it[1] }).toInt() + margin)
|
|
108
|
+
return if (right <= left || bottom <= top) null else G9ImageBox(left, top, right, bottom)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** The same pixels in the stored (un-oriented) file. */
|
|
112
|
+
fun rawBox(oriented: G9ImageBox, orientation: Int, rawWidth: Int, rawHeight: Int): G9ImageBox {
|
|
113
|
+
val m = G9Orientation.orientedToRaw(orientation, rawWidth, rawHeight)
|
|
114
|
+
val a = G9Affine.apply(m, oriented.left.toDouble(), oriented.top.toDouble())
|
|
115
|
+
val b = G9Affine.apply(m, oriented.right.toDouble(), oriented.bottom.toDouble())
|
|
116
|
+
return G9ImageBox(
|
|
117
|
+
min(a[0], b[0]).roundToInt().coerceIn(0, rawWidth),
|
|
118
|
+
min(a[1], b[1]).roundToInt().coerceIn(0, rawHeight),
|
|
119
|
+
max(a[0], b[0]).roundToInt().coerceIn(0, rawWidth),
|
|
120
|
+
max(a[1], b[1]).roundToInt().coerceIn(0, rawHeight),
|
|
121
|
+
)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Source px per output px along the finer axis of [matrix] (output → source). */
|
|
125
|
+
fun sourcePerOutput(matrix: DoubleArray) = min(hypot(matrix[0], matrix[1]), hypot(matrix[2], matrix[3]))
|
|
126
|
+
|
|
127
|
+
/** `inSampleSize`: the largest power of two ≤ [ratio] (source px per output px), at least 1. */
|
|
128
|
+
fun sampleSize(ratio: Double): Int {
|
|
129
|
+
var s = 1
|
|
130
|
+
while (s < MAX_SAMPLE && s * 2 <= ratio) s *= 2
|
|
131
|
+
return s
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Decoded-bitmap px → output px: the bitmap covers raw [box] (decoded to [bitmapWidth]×[bitmapHeight]), then the
|
|
136
|
+
* orientation, then the inverse of the caller's output → oriented-source [matrix].
|
|
137
|
+
*/
|
|
138
|
+
fun drawMatrix(matrix: DoubleArray, orientation: Int, rawWidth: Int, rawHeight: Int, box: G9ImageBox, bitmapWidth: Int, bitmapHeight: Int): DoubleArray {
|
|
139
|
+
val toRaw = G9Affine.then(G9Affine.scale(box.width.toDouble() / bitmapWidth, box.height.toDouble() / bitmapHeight), G9Affine.translate(box.left.toDouble(), box.top.toDouble()))
|
|
140
|
+
val toOriented = G9Affine.then(toRaw, G9Orientation.rawToOriented(orientation, rawWidth, rawHeight))
|
|
141
|
+
val toOutput = G9Affine.invert(matrix) ?: throw G9ImageError.invalid("The matrix is not invertible.")
|
|
142
|
+
return G9Affine.then(toOriented, toOutput)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** The preview's size: the oriented size scaled so its longest side is at most [maxSide], never upscaled. */
|
|
146
|
+
fun previewSize(orientedWidth: Int, orientedHeight: Int, maxSide: Int): IntArray {
|
|
147
|
+
val scale = min(1.0, maxSide.toDouble() / max(orientedWidth, orientedHeight))
|
|
148
|
+
return intArrayOf(max(1, (orientedWidth * scale).roundToInt()), max(1, (orientedHeight * scale).roundToInt()))
|
|
149
|
+
}
|
|
150
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
package com.g9lynx.g9native.image.core
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The JSON of `G9ImageModule`'s one-string arguments and replies. Hand-written so the core stays platform-free (the JVM
|
|
5
|
+
* suite has no Android `org.json`) and so a `null` field survives (`JSONObject(Map)` drops it: `"quality": null`
|
|
6
|
+
* must reach JS as null). Objects → `Map<String, Any?>`, arrays → `List<Any?>`, integers → `Long`, other numbers →
|
|
7
|
+
* `Double`.
|
|
8
|
+
*/
|
|
9
|
+
object G9ImageJson {
|
|
10
|
+
fun parse(text: String?): Any? {
|
|
11
|
+
if (text == null) throw G9ImageError.invalid("The argument is not JSON.")
|
|
12
|
+
val reader = Reader(text)
|
|
13
|
+
reader.space()
|
|
14
|
+
val value = reader.value()
|
|
15
|
+
reader.space()
|
|
16
|
+
if (!reader.end()) reader.fail()
|
|
17
|
+
return value
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** [parse] that must give an object (every method's argument). */
|
|
21
|
+
fun parseObject(text: String?): Map<*, *> =
|
|
22
|
+
parse(text) as? Map<*, *> ?: throw G9ImageError.invalid("The argument must be a JSON object.")
|
|
23
|
+
|
|
24
|
+
fun stringify(value: Any?): String = StringBuilder().also { write(it, value) }.toString()
|
|
25
|
+
|
|
26
|
+
private fun write(out: StringBuilder, value: Any?) {
|
|
27
|
+
when (value) {
|
|
28
|
+
null -> out.append("null")
|
|
29
|
+
is Boolean -> out.append(value)
|
|
30
|
+
is Int, is Long, is Short, is Byte -> out.append(value)
|
|
31
|
+
is Number -> number(out, value.toDouble())
|
|
32
|
+
is String -> string(out, value)
|
|
33
|
+
is Map<*, *> -> {
|
|
34
|
+
out.append('{')
|
|
35
|
+
var first = true
|
|
36
|
+
for ((k, v) in value) {
|
|
37
|
+
if (!first) out.append(',')
|
|
38
|
+
first = false
|
|
39
|
+
string(out, k.toString())
|
|
40
|
+
out.append(':')
|
|
41
|
+
write(out, v)
|
|
42
|
+
}
|
|
43
|
+
out.append('}')
|
|
44
|
+
}
|
|
45
|
+
is List<*> -> {
|
|
46
|
+
out.append('[')
|
|
47
|
+
value.forEachIndexed { i, v ->
|
|
48
|
+
if (i > 0) out.append(',')
|
|
49
|
+
write(out, v)
|
|
50
|
+
}
|
|
51
|
+
out.append(']')
|
|
52
|
+
}
|
|
53
|
+
else -> string(out, value.toString())
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private fun number(out: StringBuilder, d: Double) {
|
|
58
|
+
when {
|
|
59
|
+
!d.isFinite() -> out.append("null")
|
|
60
|
+
d == Math.rint(d) && Math.abs(d) < 1e15 -> out.append(d.toLong())
|
|
61
|
+
else -> out.append(d)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private fun string(out: StringBuilder, s: String) {
|
|
66
|
+
out.append('"')
|
|
67
|
+
for (c in s) {
|
|
68
|
+
when (c) {
|
|
69
|
+
'"' -> out.append("\\\"")
|
|
70
|
+
'\\' -> out.append("\\\\")
|
|
71
|
+
'\n' -> out.append("\\n")
|
|
72
|
+
'\r' -> out.append("\\r")
|
|
73
|
+
'\t' -> out.append("\\t")
|
|
74
|
+
else -> if (c < ' ') out.append(String.format("\\u%04x", c.code)) else out.append(c)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
out.append('"')
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private class Reader(val s: String) {
|
|
81
|
+
var i = 0
|
|
82
|
+
|
|
83
|
+
fun end() = i >= s.length
|
|
84
|
+
|
|
85
|
+
fun fail(): Nothing = throw G9ImageError.invalid("The argument is not valid JSON (at $i).")
|
|
86
|
+
|
|
87
|
+
fun space() {
|
|
88
|
+
while (i < s.length && s[i].isWhitespace()) i++
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
fun value(): Any? {
|
|
92
|
+
if (end()) fail()
|
|
93
|
+
return when (val c = s[i]) {
|
|
94
|
+
'{' -> obj()
|
|
95
|
+
'[' -> arr()
|
|
96
|
+
'"' -> str()
|
|
97
|
+
't' -> word("true", true)
|
|
98
|
+
'f' -> word("false", false)
|
|
99
|
+
'n' -> word("null", null)
|
|
100
|
+
else -> if (c == '-' || c.isDigit()) num() else fail()
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
fun word(w: String, v: Any?): Any? {
|
|
105
|
+
if (!s.startsWith(w, i)) fail()
|
|
106
|
+
i += w.length
|
|
107
|
+
return v
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
fun obj(): Map<String, Any?> {
|
|
111
|
+
val out = LinkedHashMap<String, Any?>()
|
|
112
|
+
i++
|
|
113
|
+
space()
|
|
114
|
+
if (!end() && s[i] == '}') {
|
|
115
|
+
i++
|
|
116
|
+
return out
|
|
117
|
+
}
|
|
118
|
+
while (true) {
|
|
119
|
+
space()
|
|
120
|
+
if (end() || s[i] != '"') fail()
|
|
121
|
+
val key = str()
|
|
122
|
+
space()
|
|
123
|
+
if (end() || s[i] != ':') fail()
|
|
124
|
+
i++
|
|
125
|
+
space()
|
|
126
|
+
out[key] = value()
|
|
127
|
+
space()
|
|
128
|
+
if (end()) fail()
|
|
129
|
+
when (s[i++]) {
|
|
130
|
+
',' -> continue
|
|
131
|
+
'}' -> return out
|
|
132
|
+
else -> fail()
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
fun arr(): List<Any?> {
|
|
138
|
+
val out = ArrayList<Any?>()
|
|
139
|
+
i++
|
|
140
|
+
space()
|
|
141
|
+
if (!end() && s[i] == ']') {
|
|
142
|
+
i++
|
|
143
|
+
return out
|
|
144
|
+
}
|
|
145
|
+
while (true) {
|
|
146
|
+
space()
|
|
147
|
+
out.add(value())
|
|
148
|
+
space()
|
|
149
|
+
if (end()) fail()
|
|
150
|
+
when (s[i++]) {
|
|
151
|
+
',' -> continue
|
|
152
|
+
']' -> return out
|
|
153
|
+
else -> fail()
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
fun str(): String {
|
|
159
|
+
val out = StringBuilder()
|
|
160
|
+
i++
|
|
161
|
+
while (true) {
|
|
162
|
+
if (end()) fail()
|
|
163
|
+
val c = s[i++]
|
|
164
|
+
when (c) {
|
|
165
|
+
'"' -> return out.toString()
|
|
166
|
+
'\\' -> {
|
|
167
|
+
if (end()) fail()
|
|
168
|
+
when (s[i++]) {
|
|
169
|
+
'"' -> out.append('"')
|
|
170
|
+
'\\' -> out.append('\\')
|
|
171
|
+
'/' -> out.append('/')
|
|
172
|
+
'b' -> out.append('\b')
|
|
173
|
+
'f' -> out.append('\u000c')
|
|
174
|
+
'n' -> out.append('\n')
|
|
175
|
+
'r' -> out.append('\r')
|
|
176
|
+
't' -> out.append('\t')
|
|
177
|
+
'u' -> {
|
|
178
|
+
if (i + 4 > s.length) fail()
|
|
179
|
+
out.append(s.substring(i, i + 4).toIntOrNull(16)?.toChar() ?: fail())
|
|
180
|
+
i += 4
|
|
181
|
+
}
|
|
182
|
+
else -> fail()
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
else -> out.append(c)
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
fun num(): Any {
|
|
191
|
+
val start = i
|
|
192
|
+
if (s[i] == '-') i++
|
|
193
|
+
while (i < s.length && (s[i].isDigit() || s[i] == '.' || s[i] == 'e' || s[i] == 'E' || s[i] == '+' || s[i] == '-')) i++
|
|
194
|
+
val text = s.substring(start, i)
|
|
195
|
+
val integral = text.none { it == '.' || it == 'e' || it == 'E' }
|
|
196
|
+
if (integral) text.toLongOrNull()?.let { return it }
|
|
197
|
+
return text.toDoubleOrNull()?.takeIf { it.isFinite() } ?: fail()
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
package com.g9lynx.g9native.image.core
|
|
2
|
+
|
|
3
|
+
import kotlin.math.min
|
|
4
|
+
import kotlin.math.roundToInt
|
|
5
|
+
|
|
6
|
+
/** An encoding the `maxBytes` loop settled on: [quality] (null for a lossless format), the output [scale], [fits]. */
|
|
7
|
+
class G9ImageFit<T>(val data: T, val quality: Int?, val scale: Double, val fits: Boolean)
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The `maxBytes` loop (step 6): binary-search the highest quality in [FLOOR, quality] whose encoding fits; still too big
|
|
11
|
+
* at the floor → scale the output by [SHRINK] and search again. Gives up (fits = false, the smallest attempt) once the
|
|
12
|
+
* shorter side would fall under [MIN_SIDE] px. Encodings are assumed to shrink as the quality drops.
|
|
13
|
+
*/
|
|
14
|
+
object G9ImageQuality {
|
|
15
|
+
const val FLOOR = 30
|
|
16
|
+
const val SHRINK = 0.85
|
|
17
|
+
const val MIN_SIDE = 16
|
|
18
|
+
|
|
19
|
+
/** The highest quality in [floor, start] whose encoding is ≤ [maxBytes]; fits = false with the last try when none. */
|
|
20
|
+
fun <T> search(start: Int, maxBytes: Long, encode: (Int) -> T, size: (T) -> Long, floor: Int = FLOOR): G9ImageFit<T> {
|
|
21
|
+
val first = encode(start)
|
|
22
|
+
if (size(first) <= maxBytes) return G9ImageFit(first, start, 1.0, true)
|
|
23
|
+
if (start <= floor) return G9ImageFit(first, start, 1.0, false)
|
|
24
|
+
val atFloor = encode(floor)
|
|
25
|
+
if (size(atFloor) > maxBytes) return G9ImageFit(atFloor, floor, 1.0, false)
|
|
26
|
+
var best = atFloor
|
|
27
|
+
var bestQuality = floor
|
|
28
|
+
var lo = floor + 1
|
|
29
|
+
var hi = start - 1
|
|
30
|
+
while (lo <= hi) {
|
|
31
|
+
val mid = (lo + hi) ushr 1
|
|
32
|
+
val e = encode(mid)
|
|
33
|
+
if (size(e) <= maxBytes) {
|
|
34
|
+
best = e
|
|
35
|
+
bestQuality = mid
|
|
36
|
+
lo = mid + 1
|
|
37
|
+
} else {
|
|
38
|
+
hi = mid - 1
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return G9ImageFit(best, bestQuality, 1.0, true)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The whole loop. [lossy] false (PNG): only the scale steps. [width]×[height]: the output at scale 1.
|
|
46
|
+
* [encodeAt] encodes the output scaled by its first argument at the quality of its second.
|
|
47
|
+
*/
|
|
48
|
+
fun <T> fit(quality: Int, maxBytes: Long, lossy: Boolean, width: Int, height: Int, encodeAt: (Double, Int) -> T, size: (T) -> Long): G9ImageFit<T> {
|
|
49
|
+
var scale = 1.0
|
|
50
|
+
while (true) {
|
|
51
|
+
val s = scale
|
|
52
|
+
val r =
|
|
53
|
+
if (lossy) search(quality, maxBytes, { q -> encodeAt(s, q) }, size)
|
|
54
|
+
else encodeAt(s, quality).let { G9ImageFit(it, null, s, size(it) <= maxBytes) }
|
|
55
|
+
if (r.fits) return G9ImageFit(r.data, r.quality, s, true)
|
|
56
|
+
val next = scale * SHRINK
|
|
57
|
+
if (min(width, height) * next < MIN_SIDE) return G9ImageFit(r.data, r.quality, s, false)
|
|
58
|
+
scale = next
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The output's pixel size at [scale] (at least 1 px a side). */
|
|
63
|
+
fun scaledSize(width: Int, height: Int, scale: Double) =
|
|
64
|
+
intArrayOf(maxOf(1, (width * scale).roundToInt()), maxOf(1, (height * scale).roundToInt()))
|
|
65
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
package com.g9lynx.g9native.image.core
|
|
2
|
+
|
|
3
|
+
import kotlin.math.abs
|
|
4
|
+
import kotlin.math.ceil
|
|
5
|
+
import kotlin.math.cos
|
|
6
|
+
import kotlin.math.floor
|
|
7
|
+
import kotlin.math.max
|
|
8
|
+
import kotlin.math.min
|
|
9
|
+
import kotlin.math.sin
|
|
10
|
+
|
|
11
|
+
/** The privacy regions' sizes and pixels (step 5), platform-free. */
|
|
12
|
+
object G9ImageRedaction {
|
|
13
|
+
/** Pixelate block in output px: max(4, strength·0.08·min(W, H)). */
|
|
14
|
+
fun block(strength: Double, width: Int, height: Int) = max(4.0, strength.coerceIn(0.0, 1.0) * 0.08 * min(width, height))
|
|
15
|
+
|
|
16
|
+
/** Blur radius in output px: max(2, strength·0.05·min(W, H)). */
|
|
17
|
+
fun radius(strength: Double, width: Int, height: Int) = max(2.0, strength.coerceIn(0.0, 1.0) * 0.05 * min(width, height))
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The axis-aligned bounds of the rect ([x], [y], [w], [h]) rotated by [angle] degrees about its centre, widened by
|
|
21
|
+
* [pad] and clamped to [clampW]×[clampH]; null when nothing of it is inside.
|
|
22
|
+
*/
|
|
23
|
+
fun bounds(x: Double, y: Double, w: Double, h: Double, angle: Double, pad: Double, clampW: Int, clampH: Int): G9ImageBox? {
|
|
24
|
+
val r = Math.toRadians(angle)
|
|
25
|
+
val hw = (abs(w * cos(r)) + abs(h * sin(r))) / 2 + pad
|
|
26
|
+
val hh = (abs(w * sin(r)) + abs(h * cos(r))) / 2 + pad
|
|
27
|
+
val cx = x + w / 2
|
|
28
|
+
val cy = y + h / 2
|
|
29
|
+
val left = max(0, floor(cx - hw).toInt())
|
|
30
|
+
val top = max(0, floor(cy - hh).toInt())
|
|
31
|
+
val right = min(clampW, ceil(cx + hw).toInt())
|
|
32
|
+
val bottom = min(clampH, ceil(cy + hh).toInt())
|
|
33
|
+
return if (right <= left || bottom <= top) null else G9ImageBox(left, top, right, bottom)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* An in-place box blur of ARGB [pixels] ([w]×[h]): [passes] horizontal + vertical passes of [radius] px (three passes
|
|
38
|
+
* approach a Gaussian). Edges clamp.
|
|
39
|
+
*/
|
|
40
|
+
fun boxBlur(pixels: IntArray, w: Int, h: Int, radius: Int, passes: Int = 3) {
|
|
41
|
+
if (radius < 1 || w < 1 || h < 1) return
|
|
42
|
+
val tmp = IntArray(pixels.size)
|
|
43
|
+
repeat(passes) {
|
|
44
|
+
blurLine(pixels, tmp, w, h, radius, horizontal = true)
|
|
45
|
+
blurLine(tmp, pixels, w, h, radius, horizontal = false)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
private fun blurLine(src: IntArray, dst: IntArray, w: Int, h: Int, radius: Int, horizontal: Boolean) {
|
|
50
|
+
val lines = if (horizontal) h else w
|
|
51
|
+
val length = if (horizontal) w else h
|
|
52
|
+
val window = 2 * radius + 1
|
|
53
|
+
for (line in 0 until lines) {
|
|
54
|
+
fun at(i: Int) = src[if (horizontal) line * w + i.coerceIn(0, length - 1) else i.coerceIn(0, length - 1) * w + line]
|
|
55
|
+
var sa = 0
|
|
56
|
+
var sr = 0
|
|
57
|
+
var sg = 0
|
|
58
|
+
var sb = 0
|
|
59
|
+
for (i in -radius..radius) {
|
|
60
|
+
val p = at(i)
|
|
61
|
+
sa += p ushr 24
|
|
62
|
+
sr += (p shr 16) and 0xff
|
|
63
|
+
sg += (p shr 8) and 0xff
|
|
64
|
+
sb += p and 0xff
|
|
65
|
+
}
|
|
66
|
+
for (i in 0 until length) {
|
|
67
|
+
dst[if (horizontal) line * w + i else i * w + line] = ((sa / window) shl 24) or ((sr / window) shl 16) or ((sg / window) shl 8) or (sb / window)
|
|
68
|
+
val out = at(i - radius)
|
|
69
|
+
val inn = at(i + radius + 1)
|
|
70
|
+
sa += (inn ushr 24) - (out ushr 24)
|
|
71
|
+
sr += ((inn shr 16) and 0xff) - ((out shr 16) and 0xff)
|
|
72
|
+
sg += ((inn shr 8) and 0xff) - ((out shr 8) and 0xff)
|
|
73
|
+
sb += (inn and 0xff) - (out and 0xff)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|