Need help scrambling an image


#1

Hello, I posed a question about a code snippet a few days ago and got a quick and understandable answer. I have this piece of code that I’m trying to figure out what it does and how to reverse the action. Unfortunately I’m way out on a limb.
The code scrambles an image and I wonder if anyone can give me some help to reverse the process. This is the code:


PImage image;

long seed=6229274;

void setup() {
size(640, 320);

image = loadImage(“coordinates.jpg”);

scrambleImage(image);
image(image, 0, 0);

image.save(“scrambled.jpg”);
}

void scrambleImage(PImage image) {
int n = image.width * image.height;

image.loadPixels();

for (int i = n-1; i > 0; i–) {
int j = rand()%(i+1);
image.pixels[i]^=image.pixels[j];
image.pixels[j]^=image.pixels[i];
image.pixels[i]^=image.pixels[j];
}

image.updatePixels();
}

int rand() {
long a = 16807;
long m = 2147483647;

seed = (a * seed) % m;

return int(seed);
}


#2

If it’s code, please format it like code. Edit your post, select the code, and hit the please-format-this-as-code button, which looks like this: </>


#3

It’s hard to help with general “how do I do this” type questions. Which part of this are you stuck on?

Please read this guide:


#4

So first off, two things that kind of caught my attention:

and

int j = rand()%(i+1);

Now, that doesn’t mean that it is impossible to do what it is you want to do, but the way I would begin approaching your problem, would involve loop through the original picture, and tagging each pixel with some sort of identifier, and then having them be scrambled as you have it here, and then “de-scrambling” the image, by putting the pixels back in the original order that they were tagged in. I hope that makes sense :smiley:

Now I don’t know exactly what you mean by reversing the process. Do you want to scramble an image, then “de-scramble” it with the same piece of code? Or do you want to have this code here that you posted, where an image is scrambled, and then you want a separate piece of code to de-scramble it?

Because if THAT is the case, then an idea I would give you, (which I’m sure would be a bit data heavy) and not efficient at all, is that you get your scrambled picture, scramble it, and check if it’s the same as the original picture, and literally do that millions of times until you get it right :joy: Obviously, to do that in a time span that isn’t days would require a much smaller image, preferably one that is about 2x2 pixels :smile:

So those are my two ideas so far, but as mentioned, could you please elaborate a bit on how you would want to do this? What are you trying out and what isn’t working? Also, do you get how this code works? Or would you like some more explanation on that?

Hope you are able to get started on this project,

EnhancedLoop7


#5

It’s primarily about how I reverse this scrambling of the image.
Hans


#6

Start by breaking this into a smaller problem - lets say we have a 1x1 picture, and we put it through the scrambler. Or even simpler, lets say we took an int, between 0 and 255, and added a random number to it, like you do above

int j = rand()%(i+1);

If we took a number, for instance, 99, and made a random number with rand(), for instance, 3, we would add them together and get 102.

Now, with this “scrambled” int, how could we “unscramble” it? How would we “reverse” it?


#7

I see your point. But the question is if this code really returns a true random value.
(snippet)
long seed=6229274;

int rand() {
long a = 16807;
long m = 2147483647;

seed = (a * seed) % m;

return int(seed);


#8

It is not possible to unscramble the image created with this algorithm based on the information in the scrambled image alone. Under normal circumstances it is easy to reverse an XOR (exclusive-or) operation for instance

scrambled = original ^ n
original = scrambled ^ n

but this reversal depends on knowing n.

In the algorithm above image.pixels[j] is different in the original and the scrambled image so the scrambling operation is irreversable.

The only way to unscramble the image is to use the original image to get the original value of image.pixels[j] but that rather defeats the purpose.


#9

BTW it is not very important if the rand() method is truly random provided it gives a wide range of values. In this situation it is important that for a given seed value it produces the same sequence of numbers.