Untitled
raw download clone
TEXT
views 40
,
size 1434 b
fun main(){
    val path = "src/Task06"
    val image = File("$path/orig.bmp").readBytes()
    val size = image.size
    val header = ByteArray(HEADER_LENGTH) {index -> image[index]}
    val data = ByteArray(size - HEADER_LENGTH) {index -> image[index + HEADER_LENGTH]}

    for (percent in listOf(5, 10, 15)){
        val restoredData = ByteArray(size)
        val corruptedData = ByteArray(size)

        for (i in 0 until data.size step 2){
            val codedWord = if (i < size - 1)
                encodeWord(data[i].toInt().and(0xFF) shl 8 + data[i + 1].toInt().and(0xFF))
            else
                encodeWord(data[i].toInt().and(0xFF) shl 8)

            val corruptedWord = corruptWord(codedWord, percent)
            corruptedData[i] = (removeControlBits(corruptedWord).and(0xFF00) shr 8).toByte()
            restoredData[i] = (removeControlBits(decodeWord(corruptedWord)).and(0xFF00) shr 8).toByte()

            if(i < data.size - 1){
                    corruptedData[i+1] =
                        (removeControlBits(corruptedWord) and 0x00FF).toByte()
                    restoredData[i+1] =
                        (removeControlBits(decodeWord(corruptedWord)) and 0x00FF).toByte()
                }
        }

        File("$path/corrupted_$percent.bmp").writeBytes(header + corruptedData)
        File("$path/restored_$percent.bmp").writeBytes(header + restoredData)
    }
}
close fullscreen
Login or Register to edit or fork this paste. It's free.