[pycharm] PY-79603 Debugger: fix the binarization threshold

(cherry picked from commit bb3809c53566b9cc8e07da56e53c6fe788092852)

GitOrigin-RevId: fcf2a2e1e42a3bfb3776b84526040003f8d32801
This commit is contained in:
ekaterina.itsenko
2025-03-06 17:29:15 +01:00
committed by intellij-monorepo-bot
parent 8e2e690d9b
commit 0ff65d6b69
3 changed files with 28 additions and 12 deletions

View File

@@ -93,7 +93,7 @@ image.color.mode.original.image=Original
image.color.mode.inverted.image=Inverted
image.color.mode.grayscale.image=Grayscale
image.color.mode.binarize.image=Binarized
image.binarize.dialog.title=Set Threshold
image.binarize.dialog.title=Set Binarization Threshold
image.binarize.dialog.message=Please enter a valid integer between 0 and 255.
image.binarize.dialog.invalid=Invalid Input
image.color.mode.configure.actions=Configure...

View File

@@ -2,10 +2,12 @@
package org.intellij.images.scientific
import com.intellij.openapi.components.PersistentStateComponent
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.Storage
import com.intellij.openapi.util.Key
import java.awt.image.BufferedImage
import com.intellij.openapi.components.State
import com.intellij.openapi.components.service
object ScientificUtils {
@@ -14,19 +16,33 @@ object ScientificUtils {
const val DEFAULT_IMAGE_FORMAT: String = "png"
}
@State(name = "BinarizationThresholdConfig", storages = [Storage("binarizationThresholdConfig.xml")])
object BinarizationThresholdConfig : PersistentStateComponent<BinarizationThresholdConfig> {
@Service(Service.Level.APP)
class BinarizationThresholdConfig : PersistentStateComponent<BinarizationThresholdConfig.State> {
companion object {
private const val DEFAULT_THRESHOLD = 128
private const val DEFAULT_THRESHOLD = 128
var threshold: Int = DEFAULT_THRESHOLD
override fun getState(): BinarizationThresholdConfig {
return this
@JvmStatic
fun getInstance(): BinarizationThresholdConfig {
return service()
}
}
override fun loadState(state: BinarizationThresholdConfig) {
this.threshold = state.threshold
data class State(var threshold: Int = DEFAULT_THRESHOLD)
private var state: State = State()
override fun getState(): State {
return state
}
override fun loadState(state: State) {
this.state = state
}
var threshold: Int
get() = state.threshold
set(value) {
state.threshold = value
}
}

View File

@@ -21,7 +21,7 @@ class BinarizeImageAction : AnAction() {
override fun actionPerformed(e: AnActionEvent) {
val imageFile = e.getData(CommonDataKeys.VIRTUAL_FILE) ?: return
val originalImage = imageFile.getUserData(ScientificUtils.ORIGINAL_IMAGE_KEY) ?: return
val thresholdConfig = ApplicationManager.getApplication().getService(BinarizationThresholdConfig::class.java) ?: return
val thresholdConfig = BinarizationThresholdConfig.getInstance()
val byteArrayOutputStream = ByteArrayOutputStream()
val binarizedImage = applyBinarization(originalImage, thresholdConfig.threshold)
ImageIO.write(binarizedImage, ScientificUtils.DEFAULT_IMAGE_FORMAT, byteArrayOutputStream)