Hello I am trying to make an image encryptor/decryptor for a hobby project of mine and I’ve gotten it to work but it has a weird bug.
Here are the images (code further down)
Original:
Encrypted file(Slightly downscaled version just to be able to be uploaded):
Decrypted file:
As you can see the program struggles with darker shades and blacks
And I am wondering how I can fix this issue as it can be quite annoying on some images.
I suspect that the issue is how I round the pixel data around the 255/0 points but I cannot seem to make it work
Here is the code that I am currently using:
import interfascia.*;
PImage inputIMG, inputIMGSmall, outputIMG, outputIMGSmall;
GUIController gui;
IFTextField userinputfile, password, useroutputfile;
IFLabel labelinput, labeloutput, passlabel, progresslabel;
IFButton startbutton, startencoding, startdecoding;
String inid, outid, passid;
Long pass;
Float progressfloat = 0.0;
int progressint = 0;
ArrayList<Integer> changes = new ArrayList();
PFont UIF;
void setup() {
size(360, 420);
surface.setTitle("ProjectEgami - Idle");
UIF = createFont("Arial", 20, true);
textFont(UIF, 20);
gui = new GUIController(this);
labelinput = new IFLabel("Input File", 10, 15);
userinputfile = new IFTextField("FileIn", 10, 30, 120);
labeloutput = new IFLabel("Output File", 10, 65);
useroutputfile = new IFTextField("FileOut", 10, 80, 120);
passlabel = new IFLabel("Passcode", 10, 115);
password = new IFTextField("FileOut", 10, 130, 120);
startbutton = new IFButton("Load Files", 10, 160, 120, 17);
startencoding = new IFButton("Start Encoding", 10, 180, 120, 17);
startdecoding = new IFButton("Start Decoding", 10, 200, 120, 17);
userinputfile.addActionListener(this);
useroutputfile.addActionListener(this);
password.addActionListener(this);
startbutton.addActionListener(this);
startencoding.addActionListener(this);
startdecoding.addActionListener(this);
gui.add(labelinput);
gui.add(labeloutput);
gui.add(userinputfile);
gui.add(useroutputfile);
gui.add(passlabel);
gui.add(password);
gui.add(startbutton);
}
void draw() {
background(230);
if (inputIMGSmall != null) {
image(inputIMGSmall, 150, 10);
}
if (outputIMG != null && outputIMGSmall == null) {
outputIMGSmall = outputIMG.copy();
outputIMGSmall.resize(0, 200);
image(outputIMGSmall, 150, 210);
} else if (outputIMGSmall != null) {
image(outputIMGSmall, 150, 210);
}
}
void LoadImagery() {
inputIMG = loadImage("data/input/" + inid);
inputIMGSmall = inputIMG.copy();
inputIMGSmall.resize(0, 200);
gui.add(startencoding);
gui.add(startdecoding);
}
void StartEncoding() {
float r, g, b;
float randomVal;
pass = Long.parseLong(passid);
outputIMG = inputIMG.copy();
outputIMG.loadPixels();
println(pass);
for (int i = 0; i < outputIMG.pixels.length; i++) {
progressfloat = i/((float)outputIMG.pixels.length);
randomSeed(pass);
randomVal = random(255);
r = red(outputIMG.pixels[i]);
g = green(outputIMG.pixels[i]);
b = blue(outputIMG.pixels[i]);
r -= randomVal;
if (r < 0) {
r += 255;
}
g += randomVal;
if (g > 255) {
g -= 255;
}
b -= randomVal;
if (b < 0) {
b += 255;
}
outputIMG.pixels[i] = color(r, g, b);
pass *= pass/2;
pass += 5;
if (progressint < (int) (progressfloat * 100)) {
surface.setTitle("ProjectEgami - " + (int) (progressfloat * 100) + "%");
progressint = (int) (progressfloat * 100);
}
println(progressfloat);
}
outputIMG.updatePixels();
outputIMG.save("data/output/" + outid);
surface.setTitle("ProjectEgami - Finish");
println(outputIMG.pixels.length + " " + changes.size());
}
void StartDecoding() {
float r, g, b;
float randomVal;
pass = Long.parseLong(passid);
outputIMG = inputIMG.copy();
outputIMG.loadPixels();
println(pass);
for (int i = 0; i < outputIMG.pixels.length; i++) {
progressfloat = i/((float)outputIMG.pixels.length);
randomSeed(pass);
randomVal = random(255);
r = red(outputIMG.pixels[i]);
g = green(outputIMG.pixels[i]);
b = blue(outputIMG.pixels[i]);
r += randomVal;
if (r > 255) {
r -= 255;
}
g -= randomVal;
if (g < 0) {
g += 255;
}
b += randomVal;
if (b > 255) {
b -= 255;
}
outputIMG.pixels[i] = color(r, g, b);
pass *= pass/2;
pass += 5;
if (progressint < (int) (progressfloat * 100)) {
surface.setTitle("ProjectEgami - " + (int) (progressfloat * 100) + "%");
progressint = (int) (progressfloat * 100);
}
println(progressfloat);
}
outputIMG.updatePixels();
outputIMG.save("data/output/" + outid);
surface.setTitle("ProjectEgami - Finish");
println(outputIMG.pixels.length + " " + changes.size());
}
void actionPerformed(GUIEvent e) {
if (e.getMessage().equals("Modified")) {
if (e.getSource() == userinputfile) {
inid = userinputfile.getValue();
} else if (e.getSource() == useroutputfile) {
outid = useroutputfile.getValue();
} else if (e.getSource() == password) {
passid = password.getValue();
}
}
if (e.getSource() == startbutton) {
println("Input: " + inid + " Output: " + outid + " Password: " + passid);
randomSeed(Long.parseLong(passid));
LoadImagery();
} else if (e.getSource() == startencoding) {
println("Starting Encoding");
StartEncoding();
} else if (e.getSource() == startdecoding) {
println("Starting Decoding");
StartDecoding();
}
}
Any help or tips would be greately appriciated
Best regards
cheetah