Answer the question
In order to leave comments, you need to log in
How to implement color shift when decoding an image?
I stumbled upon pikabu for a fullstack developer job.
One of the test questions went like this:
Secret agent Peekaboo transmitted an encrypted image. You need to decode the image and display it on the page using JS (without third-party libraries). The decryption algorithm is known:
the pixels are moved from left to right for each line;
for each pixel, the parameter s += x + y * 80 is calculated (initially s = 0 and for each next pixel the value increases by x + y * 80), where x is a pixel column, y is a row;
for the red and blue channel, you need to add the s parameter;
for the green channel, you need to subtract the s parameter as follows: green = (green - s) & 0xff;
Encrypted image (to avoid problems with CORS, it is better to download the image to your local server):
Answer the question
In order to leave comments, you need to log in
Can anyone understand what they want?
With this algorithm, the value of R and B will quickly go beyond 255, and G will become less than 0. Or they meant that if more than 255, then the color is the remainder of dividing by 255 - 1. For example, when R becomes 256, then this means, what R = 0? Can anyone understand what they want?Anyone who has worked with bytes at least once will understand)
I decided to try to implement on js. But nothing happened =( Tell me where to dig? Am I adding the channels correctly?
let s = 0
for (var x = 0; x < imgW; x++) {
for (var y = 0; y < imgH; y++) {
s += x + y * 80;
var c = ImDat.getPixel(x, y);
var r = (c.R + s) % 256;
var g = (c.G - s) & 0xff;
var b = (c.B + s) % 256;
ImDat.setPixel(x, y, {R: r, G: g, B: b, A: c.A});
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question