Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.imageio.ImageIO
- import java.awt.Image
- import java.awt.image.BufferedImage
- //def image = ImageIO.read(new File('I:/test.jpg'))
- def image = ImageIO.read(new File('i:/test6.png'))
- def (x, y) = [image.getWidth(), image.getHeight()]
- BufferedImage result = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB)
- result.createGraphics().drawImage(image, 0, 0, null)
- def createPermutation(x, y) {
- def RNG = new Random([x, y].hashCode())
- def permutation = [*0..<x*y]
- permutation.size().times() {
- int randomIdx = RNG.nextInt(x*y)
- int tmp = permutation[it]
- permutation[it] = permutation[randomIdx]
- permutation[randomIdx] = tmp
- }
- return permutation
- }
- boolean direct = false
- def perm = createPermutation(x, y)
- (0..<x).each { x0 ->
- (0..<y).each { y0 ->
- int destIdx = perm[x0*y + y0]
- (x1, y1) = [destIdx/y, destIdx%y] as int[]
- if (direct) result.setRGB(x0, y0, image.getRGB(x1, y1))
- else result.setRGB(x1, y1, image.getRGB(x0, y0))
- }
- }
- ImageIO.write(result, "png", new File('i:/test7.png'));
Advertisement
Add Comment
Please, Sign In to add comment