Rename color filter to filter to be more generic

This commit is contained in:
Ming Ming 2022-09-06 13:39:42 +08:00
parent 839ef8bb03
commit a4f2fb0bce
13 changed files with 35 additions and 35 deletions

View file

@ -189,7 +189,7 @@ class _ImageEditorState extends State<ImageEditor> {
} }
Future<void> _onSavePressed(BuildContext context) async { Future<void> _onSavePressed(BuildContext context) async {
await ImageProcessor.colorFilter( await ImageProcessor.filter(
"${widget.account.url}/${widget.file.path}", "${widget.account.url}/${widget.file.path}",
widget.file.filename, widget.file.filename,
4096, 4096,

View file

@ -88,9 +88,9 @@ class ImageProcessorChannelHandler(context: Context) :
} }
} }
"colorFilter" -> { "filter" -> {
try { try {
colorFilter( filter(
call.argument("fileUrl")!!, call.argument("fileUrl")!!,
call.argument("headers"), call.argument("headers"),
call.argument("filename")!!, call.argument("filename")!!,
@ -174,7 +174,7 @@ class ImageProcessorChannelHandler(context: Context) :
} }
) )
private fun colorFilter( private fun filter(
fileUrl: String, headers: Map<String, String>?, filename: String, fileUrl: String, headers: Map<String, String>?, filename: String,
maxWidth: Int, maxHeight: Int, filters: List<Map<String, Any>>, maxWidth: Int, maxHeight: Int, filters: List<Map<String, Any>>,
result: MethodChannel.Result result: MethodChannel.Result
@ -184,7 +184,7 @@ class ImageProcessorChannelHandler(context: Context) :
filters.mapTo(l, { HashMap(it) }) filters.mapTo(l, { HashMap(it) })
method( method(
fileUrl, headers, filename, maxWidth, maxHeight, fileUrl, headers, filename, maxWidth, maxHeight,
ImageProcessorService.METHOD_COLOR_FILTER, result, ImageProcessorService.METHOD_FILTER, result,
onIntent = { onIntent = {
it.putExtra(ImageProcessorService.EXTRA_FILTERS, l) it.putExtra(ImageProcessorService.EXTRA_FILTERS, l)
} }
@ -196,7 +196,7 @@ class ImageProcessorChannelHandler(context: Context) :
result: MethodChannel.Result result: MethodChannel.Result
) { ) {
var img = Rgba8Image.fromJson(rgba8) var img = Rgba8Image.fromJson(rgba8)
for (f in filters.map(ColorFilter::fromJson)) { for (f in filters.map(ImageFilter::fromJson)) {
img = f.apply(img) img = f.apply(img)
} }
result.success(img.toJson()) result.success(img.toJson())
@ -226,9 +226,9 @@ class ImageProcessorChannelHandler(context: Context) :
private var eventSink: EventChannel.EventSink? = null private var eventSink: EventChannel.EventSink? = null
} }
interface ColorFilter { interface ImageFilter {
companion object { companion object {
fun fromJson(json: Map<String, Any>): ColorFilter { fun fromJson(json: Map<String, Any>): ImageFilter {
return when (json["type"]) { return when (json["type"]) {
"brightness" -> Brightness((json["weight"] as Double).toFloat()) "brightness" -> Brightness((json["weight"] as Double).toFloat())
"contrast" -> Contrast((json["weight"] as Double).toFloat()) "contrast" -> Contrast((json["weight"] as Double).toFloat())

View file

@ -30,7 +30,7 @@ class ImageProcessorService : Service() {
const val METHOD_DEEP_LAP_PORTRAIT = "DeepLab3Portrait" const val METHOD_DEEP_LAP_PORTRAIT = "DeepLab3Portrait"
const val METHOD_ESRGAN = "Esrgan" const val METHOD_ESRGAN = "Esrgan"
const val METHOD_ARBITRARY_STYLE_TRANSFER = "ArbitraryStyleTransfer" const val METHOD_ARBITRARY_STYLE_TRANSFER = "ArbitraryStyleTransfer"
const val METHOD_COLOR_FILTER = "ColorFilter" const val METHOD_FILTER = "Filter"
const val EXTRA_FILE_URL = "fileUrl" const val EXTRA_FILE_URL = "fileUrl"
const val EXTRA_HEADERS = "headers" const val EXTRA_HEADERS = "headers"
const val EXTRA_FILENAME = "filename" const val EXTRA_FILENAME = "filename"
@ -49,7 +49,7 @@ class ImageProcessorService : Service() {
METHOD_ARBITRARY_STYLE_TRANSFER, METHOD_ARBITRARY_STYLE_TRANSFER,
) )
val EDIT_METHODS = listOf( val EDIT_METHODS = listOf(
METHOD_COLOR_FILTER, METHOD_FILTER,
) )
private const val ACTION_CANCEL = "cancel" private const val ACTION_CANCEL = "cancel"
@ -126,7 +126,7 @@ class ImageProcessorService : Service() {
METHOD_ARBITRARY_STYLE_TRANSFER -> onArbitraryStyleTransfer( METHOD_ARBITRARY_STYLE_TRANSFER -> onArbitraryStyleTransfer(
startId, intent.extras!! startId, intent.extras!!
) )
METHOD_COLOR_FILTER -> onColorFilter(startId, intent.extras!!) METHOD_FILTER -> onFilter(startId, intent.extras!!)
else -> { else -> {
logE(TAG, "Unknown method: $method") logE(TAG, "Unknown method: $method")
// we can't call stopSelf here as it'll stop the service even if // we can't call stopSelf here as it'll stop the service even if
@ -170,12 +170,12 @@ class ImageProcessorService : Service() {
) )
} }
private fun onColorFilter(startId: Int, extras: Bundle) { private fun onFilter(startId: Int, extras: Bundle) {
val filters = extras.getSerializable(EXTRA_FILTERS)!! val filters = extras.getSerializable(EXTRA_FILTERS)!!
.asType<ArrayList<Serializable>>() .asType<ArrayList<Serializable>>()
.map { it.asType<HashMap<String, Any>>() } .map { it.asType<HashMap<String, Any>>() }
return onMethod( return onMethod(
startId, extras, METHOD_COLOR_FILTER, startId, extras, METHOD_FILTER,
args = mapOf( args = mapOf(
"filters" to filters, "filters" to filters,
) )

View file

@ -1,8 +1,8 @@
package com.nkming.nc_photos.plugin.image_processor package com.nkming.nc_photos.plugin.image_processor
import com.nkming.nc_photos.plugin.ColorFilter import com.nkming.nc_photos.plugin.ImageFilter
class BlackPoint(val weight: Float) : ColorFilter { class BlackPoint(val weight: Float) : ImageFilter {
override fun apply(rgba8: Rgba8Image) = Rgba8Image( override fun apply(rgba8: Rgba8Image) = Rgba8Image(
applyNative(rgba8.pixel, rgba8.width, rgba8.height, weight), applyNative(rgba8.pixel, rgba8.width, rgba8.height, weight),
rgba8.width, rgba8.height rgba8.width, rgba8.height

View file

@ -1,8 +1,8 @@
package com.nkming.nc_photos.plugin.image_processor package com.nkming.nc_photos.plugin.image_processor
import com.nkming.nc_photos.plugin.ColorFilter import com.nkming.nc_photos.plugin.ImageFilter
class Brightness(val weight: Float) : ColorFilter { class Brightness(val weight: Float) : ImageFilter {
override fun apply(rgba8: Rgba8Image) = Rgba8Image( override fun apply(rgba8: Rgba8Image) = Rgba8Image(
applyNative(rgba8.pixel, rgba8.width, rgba8.height, weight), applyNative(rgba8.pixel, rgba8.width, rgba8.height, weight),
rgba8.width, rgba8.height rgba8.width, rgba8.height

View file

@ -1,8 +1,8 @@
package com.nkming.nc_photos.plugin.image_processor package com.nkming.nc_photos.plugin.image_processor
import com.nkming.nc_photos.plugin.ColorFilter import com.nkming.nc_photos.plugin.ImageFilter
class Contrast(val weight: Float) : ColorFilter { class Contrast(val weight: Float) : ImageFilter {
override fun apply(rgba8: Rgba8Image) = Rgba8Image( override fun apply(rgba8: Rgba8Image) = Rgba8Image(
applyNative(rgba8.pixel, rgba8.width, rgba8.height, weight), applyNative(rgba8.pixel, rgba8.width, rgba8.height, weight),
rgba8.width, rgba8.height rgba8.width, rgba8.height

View file

@ -1,8 +1,8 @@
package com.nkming.nc_photos.plugin.image_processor package com.nkming.nc_photos.plugin.image_processor
import com.nkming.nc_photos.plugin.ColorFilter import com.nkming.nc_photos.plugin.ImageFilter
class Cool(val weight: Float) : ColorFilter { class Cool(val weight: Float) : ImageFilter {
override fun apply(rgba8: Rgba8Image) = Rgba8Image( override fun apply(rgba8: Rgba8Image) = Rgba8Image(
applyNative(rgba8.pixel, rgba8.width, rgba8.height, weight), applyNative(rgba8.pixel, rgba8.width, rgba8.height, weight),
rgba8.width, rgba8.height rgba8.width, rgba8.height

View file

@ -5,15 +5,15 @@ import android.graphics.Bitmap
import android.net.Uri import android.net.Uri
import com.nkming.nc_photos.plugin.BitmapResizeMethod import com.nkming.nc_photos.plugin.BitmapResizeMethod
import com.nkming.nc_photos.plugin.BitmapUtil import com.nkming.nc_photos.plugin.BitmapUtil
import com.nkming.nc_photos.plugin.ColorFilter import com.nkming.nc_photos.plugin.ImageFilter
import com.nkming.nc_photos.plugin.use import com.nkming.nc_photos.plugin.use
class ColorFilterProcessor( class ImageFilterProcessor(
context: Context, maxWidth: Int, maxHeight: Int, context: Context, maxWidth: Int, maxHeight: Int,
filters: List<Map<String, Any>> filters: List<Map<String, Any>>
) { ) {
companion object { companion object {
const val TAG = "ColorFilterProcessor" const val TAG = "ImageFilterProcessor"
} }
fun apply(imageUri: Uri): Bitmap { fun apply(imageUri: Uri): Bitmap {
@ -24,7 +24,7 @@ class ColorFilterProcessor(
Rgba8Image(TfLiteHelper.bitmapToRgba8Array(it), it.width, it.height) Rgba8Image(TfLiteHelper.bitmapToRgba8Array(it), it.width, it.height)
} }
for (f in filters.map(ColorFilter::fromJson)) { for (f in filters.map(ImageFilter::fromJson)) {
img = f.apply(img) img = f.apply(img)
} }
return img.toBitmap() return img.toBitmap()

View file

@ -1,8 +1,8 @@
package com.nkming.nc_photos.plugin.image_processor package com.nkming.nc_photos.plugin.image_processor
import com.nkming.nc_photos.plugin.ColorFilter import com.nkming.nc_photos.plugin.ImageFilter
class Saturation(val weight: Float) : ColorFilter { class Saturation(val weight: Float) : ImageFilter {
override fun apply(rgba8: Rgba8Image) = Rgba8Image( override fun apply(rgba8: Rgba8Image) = Rgba8Image(
applyNative(rgba8.pixel, rgba8.width, rgba8.height, weight), applyNative(rgba8.pixel, rgba8.width, rgba8.height, weight),
rgba8.width, rgba8.height rgba8.width, rgba8.height

View file

@ -1,8 +1,8 @@
package com.nkming.nc_photos.plugin.image_processor package com.nkming.nc_photos.plugin.image_processor
import com.nkming.nc_photos.plugin.ColorFilter import com.nkming.nc_photos.plugin.ImageFilter
class Tint(val weight: Float) : ColorFilter { class Tint(val weight: Float) : ImageFilter {
override fun apply(rgba8: Rgba8Image) = Rgba8Image( override fun apply(rgba8: Rgba8Image) = Rgba8Image(
applyNative(rgba8.pixel, rgba8.width, rgba8.height, weight), applyNative(rgba8.pixel, rgba8.width, rgba8.height, weight),
rgba8.width, rgba8.height rgba8.width, rgba8.height

View file

@ -1,8 +1,8 @@
package com.nkming.nc_photos.plugin.image_processor package com.nkming.nc_photos.plugin.image_processor
import com.nkming.nc_photos.plugin.ColorFilter import com.nkming.nc_photos.plugin.ImageFilter
class Warmth(val weight: Float) : ColorFilter { class Warmth(val weight: Float) : ImageFilter {
override fun apply(rgba8: Rgba8Image) = Rgba8Image( override fun apply(rgba8: Rgba8Image) = Rgba8Image(
applyNative(rgba8.pixel, rgba8.width, rgba8.height, weight), applyNative(rgba8.pixel, rgba8.width, rgba8.height, weight),
rgba8.width, rgba8.height rgba8.width, rgba8.height

View file

@ -1,8 +1,8 @@
package com.nkming.nc_photos.plugin.image_processor package com.nkming.nc_photos.plugin.image_processor
import com.nkming.nc_photos.plugin.ColorFilter import com.nkming.nc_photos.plugin.ImageFilter
class WhitePoint(val weight: Float) : ColorFilter { class WhitePoint(val weight: Float) : ImageFilter {
override fun apply(rgba8: Rgba8Image) = Rgba8Image( override fun apply(rgba8: Rgba8Image) = Rgba8Image(
applyNative(rgba8.pixel, rgba8.width, rgba8.height, weight), applyNative(rgba8.pixel, rgba8.width, rgba8.height, weight),
rgba8.width, rgba8.height rgba8.width, rgba8.height

View file

@ -113,7 +113,7 @@ class ImageProcessor {
"weight": weight, "weight": weight,
}); });
static Future<void> colorFilter( static Future<void> filter(
String fileUrl, String fileUrl,
String filename, String filename,
int maxWidth, int maxWidth,
@ -121,7 +121,7 @@ class ImageProcessor {
List<ImageFilter> filters, { List<ImageFilter> filters, {
Map<String, String>? headers, Map<String, String>? headers,
}) => }) =>
_methodChannel.invokeMethod("colorFilter", <String, dynamic>{ _methodChannel.invokeMethod("filter", <String, dynamic>{
"fileUrl": fileUrl, "fileUrl": fileUrl,
"headers": headers, "headers": headers,
"filename": filename, "filename": filename,