This repository has been archived by the owner on Jul 8, 2022. It is now read-only.
/
Bitmap32.kt
366 lines (308 loc) · 11.3 KB
/
Bitmap32.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package com.soywiz.korim.bitmap
import com.soywiz.korim.color.ColorFormat
import com.soywiz.korim.color.RGBA
import com.soywiz.korim.vector.Bitmap32Context2d
import com.soywiz.korim.vector.Context2d
import com.soywiz.korio.typedarray.fill
import com.soywiz.korma.buffer.copyTo
import kotlin.math.abs
import kotlin.math.max
class Bitmap32(
width: Int,
height: Int,
val data: IntArray = IntArray(width * height),
premult: Boolean = false
) : Bitmap(width, height, 32, premult), Iterable<Int> {
init {
if (data.size < width * height) throw RuntimeException("Bitmap data is too short: width=$width, height=$height, data=ByteArray(${data.size}), area=${width * height}")
}
private val temp = IntArray(max(width, height))
//constructor(width: Int, height: Int, value: Int, premultiplied: Boolean = false) : this(width, height, IntArray(width * height) { value }, premultiplied)
constructor(width: Int, height: Int, value: Int, premult: Boolean) : this(width, height, premult = premult) {
data.fill(value)
}
@Deprecated("Use premultiplied constructor instead")
constructor(width: Int, height: Int, value: Int) : this(width, height, premult = false) {
data.fill(value)
}
constructor(width: Int, height: Int, premult: Boolean = false, generator: (x: Int, y: Int) -> Int) : this(width, height, premult = premult) {
setEach(generator)
}
override fun createWithThisFormat(width: Int, height: Int): Bitmap = Bitmap32(width, height, premult = premult)
override operator fun set(x: Int, y: Int, color: Int) = run { data[index(x, y)] = color }
override operator fun get(x: Int, y: Int): Int = data[index(x, y)]
override fun get32(x: Int, y: Int): Int = get(x, y)
fun setRow(y: Int, row: IntArray) {
row.copyTo(0, data, index(0, y), width)
}
fun _draw(src: Bitmap32, dx: Int, dy: Int, sleft: Int, stop: Int, sright: Int, sbottom: Int, mix: Boolean) {
val dst = this
val width = sright - sleft
val height = sbottom - stop
val dstData = dst.data
val srcData = src.data
for (y in 0 until height) {
val dstOffset = dst.index(dx, dy + y)
val srcOffset = src.index(sleft, stop + y)
if (mix) {
for (x in 0 until width) dstData[dstOffset + x] = RGBA.mix(dstData[dstOffset + x], srcData[srcOffset + x])
} else {
// System.arraycopy
srcData.copyTo(srcOffset, dstData, dstOffset, width)
//for (x in 0 until width) dstData[dstOffset + x] = srcData[srcOffset + x]
}
}
}
fun drawPixelMixed(x: Int, y: Int, c: Int) {
this[x, y] = RGBA.mix(this[x, y], c)
}
fun _drawPut(mix: Boolean, other: Bitmap32, _dx: Int = 0, _dy: Int = 0) {
var dx = _dx
var dy = _dy
var sleft = 0
var stop = 0
val sright = other.width
val sbottom = other.height
if (dx < 0) {
sleft = -dx
//sright += dx
dx = 0
}
if (dy < 0) {
stop = -dy
//sbottom += dy
dy = 0
}
_draw(other, dx, dy, sleft, stop, sright, sbottom, mix)
}
fun fill(color: Int, x: Int = 0, y: Int = 0, width: Int = this.width, height: Int = this.height) {
val x1 = clampX(x)
val x2 = clampX(x + width - 1)
val y1 = clampY(y)
val y2 = clampY(y + height - 1)
for (cy in y1..y2) this.data.fill(color, index(x1, cy), index(x2, cy) + 1)
}
fun _draw(src: BitmapSlice<Bitmap32>, dx: Int = 0, dy: Int = 0, mix: Boolean) {
val b = src.bounds
val availableWidth = width - dx
val availableHeight = height - dy
val awidth = kotlin.math.min(availableWidth, b.width)
val aheight = kotlin.math.min(availableHeight, b.height)
_draw(src.bmp, dx, dy, b.x, b.y, b.x + awidth, b.y + aheight, mix = mix)
}
fun put(src: Bitmap32, dx: Int = 0, dy: Int = 0) = _drawPut(false, src, dx, dy)
fun draw(src: Bitmap32, dx: Int = 0, dy: Int = 0) = _drawPut(true, src, dx, dy)
fun put(src: BitmapSlice<Bitmap32>, dx: Int = 0, dy: Int = 0) = _draw(src, dx, dy, mix = false)
fun draw(src: BitmapSlice<Bitmap32>, dx: Int = 0, dy: Int = 0) = _draw(src, dx, dy, mix = true)
fun copySliceWithBounds(left: Int, top: Int, right: Int, bottom: Int): Bitmap32 = copySliceWithSize(left, top, right - left, bottom - top)
fun copySliceWithSize(x: Int, y: Int, width: Int, height: Int): Bitmap32 {
val out = Bitmap32(width, height)
for (yy in 0 until height) {
//for (xx in 0 until width) out[xx, y] = this[x + xx, y + yy]
this.data.copyTo(this.index(x, y + yy), out.data, out.index(0, y), width)
}
return out
}
inline fun forEach(callback: (n: Int, x: Int, y: Int) -> Unit) {
var n = 0
for (y in 0 until height) for (x in 0 until width) callback(n++, x, y)
}
inline fun setEach(callback: (x: Int, y: Int) -> Int) {
forEach { n, x, y -> this.data[n] = callback(x, y) }
}
inline fun transformColor(callback: (rgba: Int) -> Int) {
forEach { n, x, y -> this.data[n] = callback(this.data[n]) }
}
fun writeChannel(destination: BitmapChannel, input: Bitmap32, source: BitmapChannel) {
val sourceShift = source.shift
val destShift = destination.shift
val destClear = destination.clearMask
val thisData = this.data
val inputData = input.data
for (n in 0 until area) {
val c = (inputData[n] ushr sourceShift) and 0xFF
thisData[n] = (thisData[n] and destClear) or (c shl destShift)
}
}
fun writeChannel(destination: BitmapChannel, input: Bitmap8) {
val destShift = destination.index * 8
val destClear = (0xFF shl destShift).inv()
for (n in 0 until area) {
val c = input.data[n].toInt() and 0xFF
this.data[n] = (this.data[n] and destClear) or (c shl destShift)
}
}
inline fun writeChannel(destination: BitmapChannel, gen: (x: Int, y: Int) -> Int) {
val destShift = destination.index * 8
val destClear = (0xFF shl destShift).inv()
var n = 0
for (y in 0 until height) {
for (x in 0 until width) {
val c = gen(x, y) and 0xFF
this.data[n] = (this.data[n] and destClear) or (c shl destShift)
n++
}
}
}
inline fun writeChannelN(destination: BitmapChannel, gen: (n: Int) -> Int) {
val destShift = destination.index * 8
val destClear = (0xFF shl destShift).inv()
for (n in 0 until area) {
val c = gen(n) and 0xFF
this.data[n] = (this.data[n] and destClear) or (c shl destShift)
}
}
fun extractChannel(channel: BitmapChannel): Bitmap8 {
val out = Bitmap8(width, height)
val shift = channel.shift
for (n in 0 until area) {
out.data[n] = ((data[n] ushr shift) and 0xFF).toByte()
}
return out
}
companion object {
fun copyRect(src: Bitmap32, srcX: Int, srcY: Int, dst: Bitmap32, dstX: Int, dstY: Int, width: Int, height: Int) {
for (y in 0 until height) {
val srcIndex = src.index(srcX, srcY + y)
val dstIndex = dst.index(dstX, dstY + y)
src.data.copyTo(srcIndex, dst.data, dstIndex, width)
}
}
fun createWithAlpha(color: Bitmap32, alpha: Bitmap32, alphaChannel: BitmapChannel = BitmapChannel.RED): Bitmap32 {
val out = Bitmap32(color.width, color.height)
out.put(color)
out.writeChannel(BitmapChannel.ALPHA, alpha, BitmapChannel.RED)
return out
}
// https://en.wikipedia.org/wiki/Structural_similarity
suspend fun matchesSSMI(a: Bitmap, b: Bitmap): Boolean = TODO()
suspend fun matches(a: Bitmap, b: Bitmap, threshold: Int = 32): Boolean {
val diff = diff(a, b)
//for (c in diff.data) println("%02X, %02X, %02X".format(RGBA.getR(c), RGBA.getG(c), RGBA.getB(c)))
return diff.data.all {
(RGBA.getR(it) < threshold) && (RGBA.getG(it) < threshold) && (RGBA.getB(it) < threshold) && (RGBA.getA(it) < threshold)
}
}
fun diff(a: Bitmap, b: Bitmap): Bitmap32 {
if (a.width != b.width || a.height != b.height) throw IllegalArgumentException("$a not matches $b size")
val a32 = a.toBMP32()
val b32 = b.toBMP32()
val out = Bitmap32(a.width, a.height, premult = true)
//showImageAndWait(a32)
//showImageAndWait(b32)
for (n in 0 until out.area) {
val c1 = RGBA.premultiplyFast(a32.data[n])
val c2 = RGBA.premultiplyFast(b32.data[n])
val dr = abs(RGBA.getR(c1) - RGBA.getR(c2))
val dg = abs(RGBA.getG(c1) - RGBA.getG(c2))
val db = abs(RGBA.getB(c1) - RGBA.getB(c2))
val da = abs(RGBA.getA(c1) - RGBA.getA(c2))
//val da = 0
//println("%02X, %02X, %02X".format(RGBA.getR(c1), RGBA.getR(c2), dr))
out.data[n] = RGBA.pack(dr, dg, db, da)
//println("$dr, $dg, $db, $da : ${out.data[n]}")
}
//showImageAndWait(out)
return out
}
}
fun invert() = xor(0x00FFFFFF)
fun xor(value: Int) {
for (n in 0 until area) data[n] = data[n] xor value
}
override fun toString(): String = "Bitmap32($width, $height)"
override fun swapRows(y0: Int, y1: Int) {
val s0 = index(0, y0)
val s1 = index(0, y1)
data.copyTo(s0, temp, 0, width)
data.copyTo(s1, data, s0, width)
temp.copyTo(0, data, s1, width)
}
fun writeDecoded(color: ColorFormat, data: ByteArray, offset: Int = 0, littleEndian: Boolean = true): Bitmap32 = this.apply {
color.decode(data, offset, this.data, 0, this.area, littleEndian = littleEndian)
}
override fun getContext2d(antialiasing: Boolean): Context2d = Context2d(Bitmap32Context2d(this))
fun clone() = Bitmap32(width, height, this.data.copyOf(), premult)
fun premultipliedIfRequired(): Bitmap32 = if (this.premult) this else premultiplied()
fun depremultipliedIfRequired(): Bitmap32 = if (!this.premult) this else depremultiplied()
fun premultiplied(): Bitmap32 = this.clone().apply { premultiplyInplace() }
fun depremultiplied(): Bitmap32 = this.clone().apply { depremultiplyInplace() }
fun premultiplyInplace() {
if (premult) return
premult = true
for (n in 0 until data.size) data[n] = RGBA.premultiplyFast(data[n])
}
fun depremultiplyInplace() {
if (!premult) return
premult = false
for (n in 0 until data.size) data[n] = RGBA.depremultiplyFast(data[n])
//for (n in 0 until data.size) data[n] = RGBA.depremultiplyAccurate(data[n])
}
/*
// @TODO: Optimize memory usage
private fun mipmapInplace(levels: Int): Bitmap32 {
var cwidth = width
var cheight = height
for (level in 0 until levels) {
cwidth /= 2
for (y in 0 until cheight) {
RGBA.downScaleBy2AlreadyPremultiplied(
data, index(0, y), 1,
data, index(0, y), 1,
cwidth
)
}
cheight /= 2
for (x in 0 until cwidth) {
RGBA.downScaleBy2AlreadyPremultiplied(
data, index(x, 0), width,
data, index(x, 0), width,
cheight
)
}
}
return this
}
fun mipmap(levels: Int): Bitmap32 {
val divide = Math.pow(2.0, levels.toDouble()).toInt()
//val owidth =
val temp = Bitmap32(width, height, this.data.copyOf(), this.premultiplied)
val out = Bitmap32(width / divide, height / divide, premultiplied = true)
temp.premultiplyInplace()
temp.mipmapInplace(levels)
Bitmap32.copyRect(temp, 0, 0, out, 0, 0, out.width, out.height)
out.depremultiplyInplace()
//return temp
return out
}
*/
fun mipmap(levels: Int): Bitmap32 {
val temp = this.clone()
temp.premultiplyInplace()
val dst = temp.data
var twidth = width
var theight = height
for (level in 0 until levels) {
twidth /= 2
theight /= 2
for (y in 0 until theight) {
var n = temp.index(0, y)
var m = temp.index(0, y * 2)
for (x in 0 until twidth) {
val c1 = dst[m]
val c2 = dst[m + 1]
val c3 = dst[m + width]
val c4 = dst[m + width + 1]
dst[n] = RGBA.blendRGBAFastAlreadyPremultiplied_05(c1, c2, c3, c4)
m += 2
n++
}
}
}
val out = Bitmap32(twidth, theight, premult = true)
Bitmap32.copyRect(temp, 0, 0, out, 0, 0, twidth, theight)
//out.depremultiplyInplace()
return out
}
override fun iterator(): Iterator<Int> = data.iterator()
}