```package seamcarving import java.awt.Color impor...
# codeforces
b
Copy code
package seamcarving
import java.awt.Color
import java.awt.Graphics2D
import java.awt.image.BufferedImage
import java.io.File
import javax.imageio.ImageIO
import kotlin.math.pow
import kotlin.math.sqrt
fun main(args: Array<String>) {
    val bufferedImage = ImageIO.read(File("/Users/name/Downloads/images/blue.png"))
    val outImage = ImageIO.read(File("/Users/name/Downloads/images/blue.png"))
    val height = bufferedImage.height
    val width = bufferedImage.width
    var minX = 0
//    (minSeamX, 0) would be the coordinate of the minimum seam
    var minSeamX = 0
    var minSeam = Double.MAX_VALUE
    //Starting from top left find the sum of pixel energies for all possible seams(#width number of possible seams) 
    for (column in 0 until width) {
        var totalSeam = 0.0
        var xHigh = column
        var xLow = column
        var min = Double.MAX_VALUE
        for (y in 0 until height) {
            for (x in xLow..xHigh) {
                if (x < 0 || x > width - 1) continue
                val energy = calculateEnergy(x, y, bufferedImage)
//                println("Energy $x $y $energy")
                if (energy < min) {
                    min = energy
                    minX = x
                }
            }
            totalSeam += min
            min = Double.MAX_VALUE
            xLow = minX - 1
            xHigh = minX + 1
        }
        if (totalSeam < minSeam) {
            minSeamX = column
            minSeam = totalSeam
        }
        println("total:$totalSeam")
    }
    var xLow = minSeamX
    var xHigh = minSeamX
    var min = Double.MAX_VALUE
    for (y in 0 until height) {
        for (x in xLow..xHigh) {
            val energy = calculateEnergy(x, y, bufferedImage)
            if (energy < min) {
                min = energy
                minX = x
            }
        }
        val createGraphics = applyColor(outImage, minX, y)
        min = Double.MAX_VALUE
        xLow = minX - 1
        xHigh = minX + 1
    }
//    for (x in 0 until width) {
//        for (y in 0 until height) {
//            val intensity = ((255.0 * array[x][y]) / max).toInt()
//            val color = Color(intensity, intensity, intensity)
////            outputImage.setRGB(x, y, intensity)
//            createGraphics.paint = color
//            createGraphics.fillRect(x, y, 1, 1)
//        }
//    }
    ImageIO.write(outImage, "png", File("out.png"))
//    ImageIO.write(bufferedImage, "png", File("${args[3]}"))
}
private fun applyColor(outputImage: BufferedImage, maxX: Int, maxY: Int): Graphics2D? {
    val createGraphics = outputImage.createGraphics()
    val color = Color(255, 0, 0)
    createGraphics.paint = color
    createGraphics.fillRect(maxX, maxY, 1, 1)
    return createGraphics
}
private fun calculateEnergy(x: Int, y: Int, bufferedImage: BufferedImage): Double {
    return sqrt(getXGradient(x, y, bufferedImage) + getYGradient(x, y, bufferedImage))
}
fun getXGradient(x: Int, y: Int, inImage: BufferedImage): Double {
    val width = inImage.width
    var xx = x
    var yy = y
    if (x == 0) xx = 1
    if (x == width - 1) xx = x - 1
    val lc = Color(inImage.getRGB(xx - 1, yy))
    val rc = Color(inImage.getRGB(xx + 1, yy))
    return (lc.red - rc.red).toDouble().pow(2.0) + (lc.green - rc.green).toDouble().pow(2.0) + (lc.blue - rc.blue).toDouble().pow(2.0)
}
fun getYGradient(x: Int, y: Int, inImage: BufferedImage): Double {
    val height = inImage.height
    var xx = x
    var yy = y
    if (y == 0) yy = 1
    if (y == height - 1) yy = y - 1
    val lc = Color(inImage.getRGB(xx, yy - 1))
    val rc = Color(inImage.getRGB(xx, yy + 1))
    return (lc.red - rc.red).toDouble().pow(2.0) + (lc.green - rc.green).toDouble().pow(2.0) + (lc.blue - rc.blue).toDouble().pow(2.0)
}