I am studying java for the first time and I am using a program called Eclipse to
ID: 3863345 • Letter: I
Question
I am studying java for the first time and I am using a program called Eclipse to build my programs. I am working with arrays and I am tasked with the following:
Create a method that restores an image in which each pixel has been scrambled by exhanging the lower 2 bits with the upper 2 bits. To do this requires that your code do the same exchange to restore the original pixel. Don't modify the middle four bits.
I understand all the concepts regarding two-dimensional arrays and such, but I am unsure how to go about this...
Explanation / Answer
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ImageDemo{
public static void main(String[] args) {
String in = "image.png";
String out = in;
int passes = 0;
//Check Length
if (args.length < 1) {
System.out.println("1 pass, reading and saving image.png");
System.out.println("Usage: pass a number. Negative - n passes of decryption, positive - n passes of enion, 0 - do nothing");
} else {
passes = Integer.parseInt(args[0]);
if (args.length > 1) {
in = args[1];
}
if(args.length > 2){
out = args[2];
}
}
boolean en = passes > 0;
passes = Math.abs(passes);
for (int a = 0; a < passes; a++) {
BufferedImage img = null;
try {
img = ImageIO.read(new File(a == 0 ? in : out));
} catch (IOException e) {
e.printStackTrace();
return;
}
int pixels[][] = new int[img.getWidth()][img.getHeight()];
int[][] newPixels = new int[img.getWidth()][img.getHeight()];
for (int x = 0; x < pixels.length; x++) {
for (int y = 0; y < pixels[x].length; y++) {
pixels[x][y] = img.getRGB(x, y);
}
}
int amt = img.getWidth() * img.getHeight();
int[] list = new int[amt];
for (int i = 0; i < amt; i++) {
list[i] = i;
}
int[] mapping = new int[amt];
for (int i = amt - 1; i >= 0; i--) {
int num = (Math.abs((int) (Math.sin(i) * amt))) % (i + 1);
mapping[i] = list[num];
list[num] = list[i];
}
for (int xz = 0; xz < amt; xz++) {
int x = xz % img.getWidth();
int z = xz / img.getWidth();
int xzMap = mapping[xz];
int newX = xzMap % img.getWidth();
int newZ = xzMap / img.getWidth();
if (en) {
newPixels[x][z] = pixels[newX][newZ];
} else {
newPixels[newX][newZ] = pixels[x][z];
}
}
//Buffer Image Processing
BufferedImage newImg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < pixels.length; x++) {
for (int y = 0; y < pixels[x].length; y++) {
newImg.setRGB(x, y, newPixels[x][y]);
}
}
try {
String[] s = out.split("\.");
ImageIO.write(newImg, s[s.length - 1],
new File(out));
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
}
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ImageDemo{
public static void main(String[] args) {
String in = "image.png";
String out = in;
int passes = 0;
//Check Length
if (args.length < 1) {
System.out.println("1 pass, reading and saving image.png");
System.out.println("Usage: pass a number. Negative - n passes of decryption, positive - n passes of enion, 0 - do nothing");
} else {
passes = Integer.parseInt(args[0]);
if (args.length > 1) {
in = args[1];
}
if(args.length > 2){
out = args[2];
}
}
boolean en = passes > 0;
passes = Math.abs(passes);
for (int a = 0; a < passes; a++) {
BufferedImage img = null;
try {
img = ImageIO.read(new File(a == 0 ? in : out));
} catch (IOException e) {
e.printStackTrace();
return;
}
int pixels[][] = new int[img.getWidth()][img.getHeight()];
int[][] newPixels = new int[img.getWidth()][img.getHeight()];
for (int x = 0; x < pixels.length; x++) {
for (int y = 0; y < pixels[x].length; y++) {
pixels[x][y] = img.getRGB(x, y);
}
}
int amt = img.getWidth() * img.getHeight();
int[] list = new int[amt];
for (int i = 0; i < amt; i++) {
list[i] = i;
}
int[] mapping = new int[amt];
for (int i = amt - 1; i >= 0; i--) {
int num = (Math.abs((int) (Math.sin(i) * amt))) % (i + 1);
mapping[i] = list[num];
list[num] = list[i];
}
for (int xz = 0; xz < amt; xz++) {
int x = xz % img.getWidth();
int z = xz / img.getWidth();
int xzMap = mapping[xz];
int newX = xzMap % img.getWidth();
int newZ = xzMap / img.getWidth();
if (en) {
newPixels[x][z] = pixels[newX][newZ];
} else {
newPixels[newX][newZ] = pixels[x][z];
}
}
//Buffer Image Processing
BufferedImage newImg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < pixels.length; x++) {
for (int y = 0; y < pixels[x].length; y++) {
newImg.setRGB(x, y, newPixels[x][y]);
}
}
try {
String[] s = out.split("\.");
ImageIO.write(newImg, s[s.length - 1],
new File(out));
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.