Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. import java.awt.Color
  2. import java.awt.Image
  3. import java.awt.RenderingHints
  4. import java.awt.image.BufferedImage
  5. import java.io.File
  6. import java.util.stream.IntStream
  7. import javax.imageio.ImageIO
  8. import kotlin.math.sqrt
  9. import kotlin.random.Random
  10.  
  11.  
  12. class Converter(private val inputImage: String, private val outputFormat: String, private val randomCount: Int) {
  13.  
  14. private val colors = hashMapOf<Color, String>()
  15. private val singleCache = hashMapOf<Color, String>()
  16. private val multiCache = hashMapOf<Color, ArrayList<String>>()
  17.  
  18. fun convert(): String {
  19.  
  20. loadColors()
  21.  
  22. val original = ImageIO.read(File(inputImage))
  23. val base = getBase(original)
  24.  
  25. val mosaic = BufferedImage(base.width * 100, base.height * 100, BufferedImage.TYPE_INT_RGB)
  26. val mosaicg2d = mosaic.createGraphics()
  27. mosaicg2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR)
  28. IntStream.range(1, base.width - 1).parallel().forEach { x ->
  29. IntStream.range(1, base.height - 1).parallel().forEach { y ->
  30. val baseColor = Color(base.getRGB(x, y))
  31. val imagePath = if (randomCount == 1) getSingleColor(baseColor) else getMultiColor(baseColor, randomCount)
  32. val image = ImageIO.read(File(imagePath))
  33. mosaicg2d.drawImage(image, x * 100, y * 100, x * 100 + 100, y * 100 + 100,
  34. 0, 0, 100, 100, null)
  35. }
  36. }
  37.  
  38. mosaicg2d.dispose()
  39. val outputFile = generateOutputFile(File(inputImage).parentFile.path)
  40. ImageIO.write(mosaic, outputFormat, outputFile)
  41.  
  42. return outputFile.path
  43. }
  44.  
  45. private fun loadColors() {
  46. for (s in File("D:\\clopscaled.txt").readLines()) {
  47. val split = s.split(":")
  48. val red = split[2].toInt()
  49. val green = split[3].toInt()
  50. val blue = split[4].toInt()
  51. val color = Color(red, green, blue)
  52. colors[color] = "${split[0]}:${split[1]}"
  53. }
  54. println("${colors.size} colors loaded")
  55. }
  56.  
  57. private fun getBase(image: BufferedImage): BufferedImage {
  58. return if (image.width * 100L * image.height * 100L <= 900000000L) {
  59. println("No need to resize")
  60. image
  61. } else {
  62. val scale = 300 * sqrt(1F / (image.width * image.height))
  63. println("Scale factor $scale")
  64. resizeImage(image, scale)
  65. }
  66. }
  67.  
  68. private fun resizeImage(image: BufferedImage, factor: Float): BufferedImage {
  69. val newWidth = (image.width * factor).toInt()
  70. val newHeight = (image.height * factor).toInt()
  71.  
  72. val tmp = image.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH)
  73. val resized = BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB)
  74.  
  75. val g2d = resized.createGraphics()
  76. g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR)
  77. g2d.drawImage(tmp, 0, 0, null)
  78. g2d.dispose()
  79.  
  80. return resized
  81. }
  82.  
  83. private fun getSingleColor(baseColor: Color): String {
  84. if (singleCache.containsKey(baseColor))
  85. return singleCache[baseColor]!!
  86.  
  87. val red = baseColor.red
  88. val green = baseColor.green
  89. val blue = baseColor.blue
  90. var diff = 0
  91. while (true) {
  92. for (color in colors.keys) {
  93. if (color.red <= red + diff && color.red >= red - diff
  94. && color.green <= green + diff && color.green >= green - diff
  95. && color.blue <= blue + diff && color.blue >= blue - diff) {
  96. val image = colors[color]!!
  97. singleCache[baseColor] = image
  98. return image
  99. }
  100. }
  101. diff++
  102. }
  103. }
  104.  
  105. private fun getMultiColor(baseColor: Color, amount: Int): String {
  106. if (multiCache.containsKey(baseColor)) {
  107. return multiCache[baseColor]!!.random()
  108. }
  109.  
  110. val red = baseColor.red
  111. val green = baseColor.green
  112. val blue = baseColor.blue
  113. val selection = arrayListOf<String>()
  114. var diff = 0
  115. while (true) {
  116. for (color in colors.keys) {
  117. if (red + diff >= color.red && red - diff <= color.red
  118. && green + diff >= color.green && green - diff <= color.green
  119. && blue + diff >= color.blue && blue - diff <= color.blue) {
  120. val image = colors[color]!!
  121. if (!selection.contains(image))
  122. selection.add(image)
  123. if (selection.size == amount) {
  124. multiCache[baseColor] = selection
  125. return selection.random()
  126. }
  127. }
  128. }
  129. diff++
  130. }
  131. }
  132.  
  133. fun generateOutputFile(dir: String): File {
  134. val chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  135. var filename = ""
  136. for (i in 0..10) {
  137. filename += chars[Random.nextInt(0, chars.length)]
  138. }
  139. return File("$dir\\$filename.$outputFormat")
  140. }
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement