I have tried to change the red channel value of an image and then reverse it to the original value. I simply add 100 to the red channel value and after that subtract to 100 to get the original image. But it does not work
Below is my code:
static void encrypt() throws IOException {
BufferedImage oimage = ImageIO.read(new File("inputjpg.jpg"));
for (int x = 0; x < oimage.getWidth(); x++) {
for (int y = 0; y < oimage.getHeight(); y++) {
int orgb = oimage.getRGB(x, y);
int nr = red(orgb) + 100;
if (nr < 255) {
oimage.setRGB(x, y, new Color(nr, green(orgb), blue(orgb)).getRGB());
}
}
}
writeImage(oimage, "encrypt.jpg");
}
static void decrypt() throws IOException {
BufferedImage oimage = ImageIO.read(new File("encrypt.jpg"));
for (int x = 0; x < oimage.getWidth(); x++) {
for (int y = 0; y < oimage.getHeight(); y++) {
int orgb = oimage.getRGB(x, y);
if (red(orgb) >= 100) {
int nr = red(orgb) - 100;
oimage.setRGB(x, y, new Color(nr, green(orgb), blue(orgb)).getRGB());
}
}
}
writeImage(oimage, "origin.jpg");
}
static void writeImage(BufferedImage img, String filePath) {
String format = filePath.substring(filePath.indexOf('.') + 1);
// Get Picture Format
System.out.println(format);
try {
ImageIO.write(img, format, new File(filePath));
} catch (IOException e) {
e.printStackTrace();
}
}
static int maskAlpha(int rgb, int alpha) {
// strip alpha from original color
int color = (0x00ffffff & rgb);
return color + ((alpha) << 24);
}
static int alpha(int rgb) {
return (0xff & (rgb >> 24));
}
static int red(int rgb) {
return (0xff & rgb);
}
static int green(int rgb) {
return (0xff & (rgb >> 8));
}
static int blue(int rgb) {
return (0xff & (rgb >> 16));
}
And this is images I used
inputjpg.jpg
encrypt.jpg
origin.jpg
Could you help me to solve it? Thank you!