Added VHS filter

This commit is contained in:
T8RIN 2025-12-26 23:06:52 +03:00
parent c3bade04fd
commit aeabcb69ce
9 changed files with 167 additions and 8 deletions

View File

@ -363,6 +363,7 @@ interface Filter<Value : Any> : VisibilityOwner {
interface RadialWeavePixelation : FloatFilter
interface BorderFrame : FloatColorModelFilter
interface GlitchVariant : TripleFloatFilter
interface VHS : PairFloatFilter
}
interface SimpleFilter : Filter<Unit>

View File

@ -371,7 +371,8 @@ sealed class UiFilter<T : Any>(
UiCropOrPerspectiveFilter(),
UiLensCorrectionFilter(),
UiSeamCarvingFilter(),
UiGlitchVariantFilter()
UiGlitchVariantFilter(),
UiVHSFilter()
),
//Dithering
listOf(

View File

@ -0,0 +1,42 @@
/*
* ImageToolbox is an image editor for android
* Copyright (c) 2025 T8RIN (Malik Mukhametzyanov)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* You should have received a copy of the Apache License
* along with this program. If not, see <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package com.t8rin.imagetoolbox.core.filters.presentation.model
import com.t8rin.imagetoolbox.core.filters.domain.model.Filter
import com.t8rin.imagetoolbox.core.filters.domain.model.FilterParam
import com.t8rin.imagetoolbox.core.resources.R
import kotlin.math.PI
class UiVHSFilter(
override val value: Pair<Float, Float> = 2f to 3f,
) : UiFilter<Pair<Float, Float>>(
title = R.string.vhs,
value = value,
paramsInfo = listOf(
FilterParam(
title = R.string.seed,
valueRange = 0f..PI.toFloat(),
roundTo = 3
),
FilterParam(
title = R.string.strength,
valueRange = 0f..10f,
roundTo = 3
),
)
), Filter.VHS

View File

@ -1898,4 +1898,5 @@
<string name="glitch_variant">Glitch Variant</string>
<string name="channel_shift">Channel Shift</string>
<string name="max_offset">Max Offset</string>
<string name="vhs">VHS</string>
</resources>

View File

@ -22,7 +22,7 @@ import com.t8rin.imagetoolbox.core.domain.model.IntegerSize
import com.t8rin.imagetoolbox.core.domain.transformation.Transformation
import com.t8rin.imagetoolbox.core.filters.domain.model.Filter
import com.t8rin.imagetoolbox.core.ksp.annotations.FilterInject
import com.t8rin.imagetoolbox.feature.filters.data.utils.glitch.Glitcher
import com.t8rin.imagetoolbox.feature.filters.data.utils.glitch.GlitchTool
@FilterInject
internal class AnaglyphFilter(
@ -35,6 +35,6 @@ internal class AnaglyphFilter(
override suspend fun transform(
input: Bitmap,
size: IntegerSize
): Bitmap = Glitcher.anaglyph(input, value.toInt())
): Bitmap = GlitchTool.anaglyph(input, value.toInt())
}

View File

@ -23,7 +23,7 @@ import com.t8rin.imagetoolbox.core.domain.model.IntegerSize
import com.t8rin.imagetoolbox.core.domain.transformation.Transformation
import com.t8rin.imagetoolbox.core.filters.domain.model.Filter
import com.t8rin.imagetoolbox.core.ksp.annotations.FilterInject
import com.t8rin.imagetoolbox.feature.filters.data.utils.glitch.Glitcher
import com.t8rin.imagetoolbox.feature.filters.data.utils.glitch.GlitchTool
import kotlinx.coroutines.coroutineScope
import java.io.ByteArrayOutputStream
@ -38,7 +38,7 @@ internal class GlitchFilter(
override suspend fun transform(
input: Bitmap,
size: IntegerSize
): Bitmap = Glitcher.glitch(
): Bitmap = GlitchTool.glitch(
bitmap = coroutineScope {
ByteArrayOutputStream().use {
input.compress(Bitmap.CompressFormat.JPEG, 100, it)

View File

@ -22,7 +22,7 @@ import com.t8rin.imagetoolbox.core.domain.model.IntegerSize
import com.t8rin.imagetoolbox.core.domain.transformation.Transformation
import com.t8rin.imagetoolbox.core.filters.domain.model.Filter
import com.t8rin.imagetoolbox.core.ksp.annotations.FilterInject
import com.t8rin.imagetoolbox.feature.filters.data.utils.glitch.Glitcher
import com.t8rin.imagetoolbox.feature.filters.data.utils.glitch.GlitchTool
import kotlin.math.roundToInt
@FilterInject
@ -36,7 +36,7 @@ internal class GlitchVariantFilter(
override suspend fun transform(
input: Bitmap,
size: IntegerSize
): Bitmap = Glitcher.glitchVariant(
): Bitmap = GlitchTool.glitchVariant(
src = input,
iterations = value.first.roundToInt(),
maxOffset = value.second,

View File

@ -0,0 +1,44 @@
/*
* ImageToolbox is an image editor for android
* Copyright (c) 2025 T8RIN (Malik Mukhametzyanov)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* You should have received a copy of the Apache License
* along with this program. If not, see <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package com.t8rin.imagetoolbox.feature.filters.data.model
import android.graphics.Bitmap
import com.t8rin.imagetoolbox.core.domain.model.IntegerSize
import com.t8rin.imagetoolbox.core.domain.transformation.Transformation
import com.t8rin.imagetoolbox.core.filters.domain.model.Filter
import com.t8rin.imagetoolbox.core.ksp.annotations.FilterInject
import com.t8rin.imagetoolbox.feature.filters.data.utils.glitch.GlitchTool
@FilterInject
internal class VHSFilter(
override val value: Pair<Float, Float> = 2f to 3f,
) : Transformation<Bitmap>, Filter.VHS {
override val cacheKey: String
get() = value.hashCode().toString()
override suspend fun transform(
input: Bitmap,
size: IntegerSize
): Bitmap = GlitchTool.vhsGlitch(
src = input,
time = value.first,
strength = value.second
)
}

View File

@ -31,9 +31,10 @@ import androidx.core.graphics.createBitmap
import kotlinx.coroutines.coroutineScope
import kotlin.math.floor
import kotlin.math.roundToInt
import kotlin.math.sin
import kotlin.random.Random
internal object Glitcher {
internal object GlitchTool {
private val leftArray = floatArrayOf(
1.0f,
@ -213,6 +214,75 @@ internal object Glitcher {
result
}
suspend fun vhsGlitch(
src: Bitmap,
time: Float = 0f,
strength: Float = 1f
): Bitmap = coroutineScope {
val w = src.width
val h = src.height
val lineJitterPx = (w * 0.015f * strength).toInt()
val rgbShiftPx = (w * 0.008f * strength).toInt()
val waveAmp = 2f * strength
val noiseAmp = (20 * strength).toInt()
val result = src.copy(Bitmap.Config.ARGB_8888, true)
val pixels = IntArray(w * h)
val out = IntArray(w * h)
result.getPixels(pixels, 0, w, 0, 0, w, h)
for (y in 0 until h) {
val wave =
sin((y * 0.06f) + time * 4f) * waveAmp
val jitter =
if (Random.nextFloat() < 0.08f * strength)
Random.nextInt(-lineJitterPx, lineJitterPx + 1)
else 0
val row = y * w
for (x in 0 until w) {
val baseX = (x + wave + jitter).toInt()
.coerceIn(0, w - 1)
val rX = (baseX + rgbShiftPx).coerceIn(0, w - 1)
val bX = (baseX - rgbShiftPx).coerceIn(0, w - 1)
val pR = pixels[row + rX]
val pG = pixels[row + baseX]
val pB = pixels[row + bX]
var r = (pR shr 16) and 0xFF
var g = (pG shr 8) and 0xFF
var b = pB and 0xFF
val a = (pG ushr 24)
val noise = Random.nextInt(-noiseAmp, noiseAmp + 1)
r = (r + noise).coerceIn(0, 255)
g = (g + noise).coerceIn(0, 255)
b = (b + noise).coerceIn(0, 255)
if ((y % 3) == 0) {
r = (r * 0.92f).toInt()
g = (g * 0.92f).toInt()
b = (b * 0.92f).toInt()
}
out[row + x] =
(a shl 24) or
(r shl 16) or
(g shl 8) or
b
}
}
result.setPixels(out, 0, w, 0, 0, w, h)
result
}
private fun glitchJpegBytes(
pos: Int,