Guest User

Untitled

a guest
Jul 24th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 1.09 KB | None | 0 0
  1. import javax.imageio.ImageIO
  2. import java.awt.Image
  3. import java.awt.image.BufferedImage
  4.  
  5. //def image = ImageIO.read(new File('I:/test.jpg'))
  6. def image = ImageIO.read(new File('i:/test6.png'))
  7. def (x, y) = [image.getWidth(), image.getHeight()]
  8. BufferedImage result = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB)
  9. result.createGraphics().drawImage(image, 0, 0, null)
  10.  
  11. def createPermutation(x, y) {
  12.     def RNG = new Random([x, y].hashCode())
  13.     def permutation = [*0..<x*y]
  14.     permutation.size().times() {
  15.         int randomIdx = RNG.nextInt(x*y)
  16.         int tmp = permutation[it]
  17.         permutation[it] = permutation[randomIdx]
  18.         permutation[randomIdx] = tmp
  19.     }
  20.     return permutation
  21. }
  22.  
  23. boolean direct = false
  24. def perm = createPermutation(x, y)
  25.  
  26. (0..<x).each { x0 ->
  27.     (0..<y).each { y0 ->
  28.         int destIdx = perm[x0*y + y0]
  29.         (x1, y1) = [destIdx/y, destIdx%y] as int[]
  30.         if (direct) result.setRGB(x0, y0, image.getRGB(x1, y1))
  31.         else result.setRGB(x1, y1, image.getRGB(x0, y0))
  32.     }
  33. }
  34.  
  35. ImageIO.write(result, "png", new File('i:/test7.png'));
Advertisement
Add Comment
Please, Sign In to add comment