Skip to content

Commit

Permalink
Fix large files. Output to correct files
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownSilicon committed Sep 6, 2018
1 parent 1ac0d01 commit b4ba424
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 75 deletions.
182 changes: 110 additions & 72 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import net.lingala.zip4j.exception.ZipException;
import utility.Checksum;
import utility.FastRGB;
import utility.StringUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Objects;
import java.util.Scanner;

public class Main {
Expand All @@ -32,21 +31,13 @@ public static void main(String[] args) throws IOException, NoSuchAlgorithmExcept
if (s.equals("e")) {
File file = getFile(scan);

System.out.println("Output File: ");
String fileStr = scan.nextLine();

encode(file, fileStr);
encode(file);
} else if (s.equals("d")) {

File file = getFile(scan);

File outputFile = null;
System.out.println("Input File: ");
String inStr = scan.nextLine();

System.out.println("Output File: ");
String fileStr = scan.nextLine();
outputFile = new File(fileStr);

decode(ImageIO.read(file), outputFile);
decode(inStr);
} else {
System.out.println("Something Broke");
}
Expand All @@ -68,86 +59,131 @@ private static File getFile(Scanner scan) {
return file;
}

public static void decode(BufferedImage image, File outputFile) throws IOException, NoSuchAlgorithmException {
public static void decode(String name) throws IOException, NoSuchAlgorithmException, ZipException {
long startTime = System.nanoTime();
FastRGB fastRGB = new FastRGB(image);

int size = image.getHeight() * image.getWidth();
byte[] data = new byte[size * 3];
File dir = new File("."); // Get this directory

int dataNum = 0;
File[] files = dir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String n) {
String lower = n.toLowerCase();

for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
byte[] pixel = fastRGB.getRGB(x, y);
boolean pngEnd = lower.endsWith(".png");

data[dataNum] = pixel[0];
data[dataNum + 1] = pixel[1];
data[dataNum + 2] = pixel[2];
if (pngEnd) {
return lower.split(".png")[0].startsWith(name.toLowerCase());
}

dataNum += 3;
return false;
}
}
});

assert files != null;
for (File f : files) {

if (!f.exists()) {
break;
}

File tempOutput = new File(f.getName());

File tempInput = new File(f.getName()); // For some reason, ImageIO does not like the .\ in front

byte[] checksum;
//FileInputStream fis = new FileInputStream(f);

int firstIndex = 0;
int lastIndex = 0;
BufferedImage image = ImageIO.read(tempInput);

FastRGB fastRGB = new FastRGB(image);
//fis.close();

int currentIndex = data.length - 1;
while (lastIndex == 0) {
if (data[currentIndex--] != 0) {
lastIndex = currentIndex;
firstIndex = currentIndex - 19;
int size = image.getHeight() * image.getWidth();
byte[] data = new byte[size * 3];

int dataNum = 0;

for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
byte[] pixel = fastRGB.getRGB(x, y);

data[dataNum] = pixel[0];
data[dataNum + 1] = pixel[1];
data[dataNum + 2] = pixel[2];

dataNum += 3;
}
}

byte[] checksum;

int firstIndex = 0;
int lastIndex = 0;


int currentIndex = data.length - 1;
while (lastIndex == 0) {
if (data[currentIndex--] != 0) {
lastIndex = currentIndex;
firstIndex = currentIndex - 19;
}
}
}

/* TODO: Figure out a better way to detect checksum | See below | The current "solution" will work for all files
Except for those with a perfect square image and the checksum ends in "255 255 255" or "FF FF FF"
*/
if ((data[lastIndex - 1] & 0xFF) == 255 && (data[lastIndex] & 0xFF) == 255 && (data[lastIndex + 1] & 0xFF) == 255) {
// If the last three bytes are 255, then you are in the right place
firstIndex -= 3;
} else {
lastIndex = data.length;
firstIndex = lastIndex - 19;
}
if ((data[lastIndex - 1] & 0xFF) == 255 && (data[lastIndex] & 0xFF) == 255 && (data[lastIndex + 1] & 0xFF) == 255) {
// If the last three bytes are 255, then you are in the right place
firstIndex -= 3;
} else {
lastIndex = data.length;
firstIndex = lastIndex - 19;
}

checksum = Arrays.copyOfRange(data, firstIndex + 1, firstIndex + 21);
checksum = Arrays.copyOfRange(data, firstIndex + 1, firstIndex + 21);

byte[] newData = new byte[firstIndex + 1];
byte[] newData = new byte[firstIndex + 1];

for (int i = 0; i < firstIndex + 1; i++) {
newData[i] = data[i];
}
for (int i = 0; i < firstIndex + 1; i++) {
newData[i] = data[i];
}

// File should be encoded in data
// File should be encoded in data

FileOutputStream fos = new FileOutputStream(outputFile);
FileOutputStream fos = new FileOutputStream(tempOutput);

fos.write(newData);
fos.close();
fos.write(newData);
fos.close();

byte[] newChecksum = Checksum.getFileChecksum(outputFile);
byte[] newChecksum = Checksum.getFileChecksum(tempOutput);

if (Arrays.equals(checksum, newChecksum)) {
if (Arrays.equals(checksum, newChecksum)) {

System.out.println("Checksum Valid");
System.out.println("Checksum Valid");

} else {
System.out.println("Checksum invalid");
} else {
System.out.println("Checksum invalid");

System.out.println("Old Checksum: ");
for (byte b : checksum) {
System.out.print(b + " ");
}
System.out.println("Old Checksum: ");
for (byte b : checksum) {
System.out.print(b + " ");
}

System.out.println("\nNew Checksum");
for (byte b : newChecksum) {
System.out.print(b + " ");
System.out.println("\nNew Checksum");
for (byte b : newChecksum) {
System.out.print(b + " ");
}
System.out.println();
}
System.out.println();
}

Zipper zipper = new Zipper();

try {
zipper.unzip(files[0].getName());

} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("File does not exist!");
System.exit(1);
}

long endTime = System.nanoTime();
Expand All @@ -157,7 +193,7 @@ public static void decode(BufferedImage image, File outputFile) throws IOExcepti
System.out.println("Decoded in: " + deltaTime + " seconds");
}

public static void encode(File file, String outputFile) throws IOException, NoSuchAlgorithmException, ZipException {
public static void encode(File file) throws IOException, NoSuchAlgorithmException, ZipException {
long startTime = System.nanoTime();
if (!file.exists()) {
System.out.println("File does not exist!");
Expand All @@ -172,7 +208,7 @@ public static void encode(File file, String outputFile) throws IOException, NoSu

int imgNum = 0;

for (File f : directory.listFiles()) {
for (File f : Objects.requireNonNull(directory.listFiles())) {
if (f.isFile()) {

FileInputStream fis = new FileInputStream(f);
Expand Down Expand Up @@ -286,21 +322,23 @@ public static void encode(File file, String outputFile) throws IOException, NoSu
col = (pixelNum - closestSquare * row);

if (tempStorage.length == 1) {
ic.drawPixel(row, col, new Color(tempStorage[0], 255, 255));
ic.drawPixel(row, col, new Color(tempStorage[0] & 0xFF, 255, 255));
pixelNum++;
row = (pixelNum - (pixelNum % closestSquare)) / closestSquare;
col = (pixelNum - closestSquare * row);
ic.drawPixel(row, col, new Color(255, 0, 0));
} else if (tempStorage.length == 2) {
ic.drawPixel(row, col, new Color(tempStorage[0], tempStorage[1], 255));
ic.drawPixel(row, col, new Color(tempStorage[0] & 0xFF, tempStorage[1] & 0xFF, 255));
pixelNum++;
row = (pixelNum - (pixelNum % closestSquare)) / closestSquare;
col = (pixelNum - closestSquare * row);
ic.drawPixel(row, col, new Color(255, 255, 0));
}
}

ImageIO.write(ic.getImage(), "png", new File(outputFile + "-" + imgNum + ".png"));


ImageIO.write(ic.getImage(), "png", new File(f.getName() + ".png"));

imgNum++;
}
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/Zipper.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ public String zip(File source) throws ZipException, IOException {
return outputDir;
}

public void unzip(String sourceDir) throws ZipException {
ZipFile file = new ZipFile(sourceDir);
public void unzip(String source) throws ZipException {
ZipFile file = new ZipFile(source);

file.extractAll(System.getProperty("user.dir"));
String outputDir = System.getProperty("user.dir");

file.extractAll(outputDir);
}
}
13 changes: 13 additions & 0 deletions src/main/java/utility/StringUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package utility;

import java.io.File;

public class StringUtils {

public static String getFileExtension(File file) {
String fileName = file.getName();
if(fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0)
return fileName.substring(fileName.lastIndexOf(".")+1);
else return "";
}
}

0 comments on commit b4ba424

Please sign in to comment.