Hello, everybody!
I have a small issue with a decompression formula I have written. Basically, I’m starting by compressing my world files, which are in .png format, into text files whereby each pixel is represented by a Unicode character. I have two HashMaps keeping track of the pixel-character relationships. I am able to compress and decompress everything without any compilation errors, and all of the files are outputted and whatnot, but the problem I’m having is that my map, when reloaded after decompression, is completely skewed! It appears as if everything has been shifted to the left a bit, and it appears to get worse the lower down the map we go.
Here’s the code for the entire class I’ve created for this whole process. Thank you guys for taking a look at it! I’m not sure which method exactly is causing the issue, but I’m fairly sure it has to do with the readFile() method.
/*
* This is a test to determine whether an image file representing a level (.png) or a text file with the same
* data uses less disk space. It will use a simple coding scheme whereby the start of the level declaration will
* contain numbers (which are actually Vast color keys) that correspond to sequentially numbered shorter numbers (0
* onward) to save space in the file.
*/
package test.performance;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import vastgame.ImageHandler;
import java.io.*;
import java.util.Enumeration;
import java.util.zip.*;
public class ImageToText
{
static final int BUFFER = 2048;
private BufferedImage image;
BufferedWriter fileWriter;
Map<Integer, Character> tileMap = new HashMap<Integer, Character>();
Map<Character, Integer> tileMap2 = new HashMap<Character, Integer>();
public ImageToText()
{
int[][] imageArray;
image = ImageHandler.loadImage("resources/worlds/hugeworld0.png");
imageArray = new int[image.getWidth()][image.getHeight()];
try
{
fileWriter = new BufferedWriter(new FileWriter("worldtest.txt"));
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
/*
* The convertImage algorithm traverses an image (or array) of individual tile data and places a key starting from
* 0 for each unique tile found. It will then display the number of that tile
*/
public void convertImage()
{
char type = 0;
for (int y = 0; y < image.getHeight(); y++)
{
for (int x = 0; x < image.getWidth(); x++)
{
if (!tileMap.containsKey(image.getRGB(x, y)))
{
tileMap.put(image.getRGB(x, y), type);
tileMap2.put(type, image.getRGB(x, y));
type++;
}
try
{
fileWriter.write(tileMap.get(image.getRGB(x, y)));
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
try
{
fileWriter.newLine();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
try
{
fileWriter.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
public void condenseFiles()
{
try
{
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream("resources/leveltest.lev");
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
out.setMethod(ZipOutputStream.DEFLATED);
byte data[] = new byte[BUFFER];
// get a list of files from current directory
File f = new File("worldtest.txt");
String file = f.getName();
System.out.println("Adding: "+file);
FileInputStream fi = new FileInputStream(file);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(file);
out.putNextEntry(entry);
int count;
while((count = origin.read(data, 0,
BUFFER)) != -1)
{
out.write(data, 0, count);
}
origin.close();
out.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
/*
* This method reads in a .lev file to a 2d array, converts it to a map, then outputs it.
*/
public void readFile()
{
ZipFile zf = null;
try
{
zf = new ZipFile("resources/leveltest.lev");
}
catch (IOException ex)
{
ex.printStackTrace();
}
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
Enumeration entries = zf.entries();
ZipEntry ze = (ZipEntry)entries.nextElement();
System.out.println("Read " + ze.getName() + "?");
String inputLine = "";
try
{
inputLine = input.readLine();
}
catch (IOException ex)
{
ex.printStackTrace();
}
if (inputLine.equalsIgnoreCase("yes"))
{
long size = ze.getSize();
if (size > 0)
{
System.out.println("Length is " + size);
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(zf.getInputStream(ze)));
String line;
int[][] data = new int[image.getWidth()][image.getHeight()];
int heightIndex = 0;
int counter = 0;
while ((line = br.readLine()) != null)
{
// for every index of the line string...
for (int i = 0; i < line.length(); i++)
{
// determine whether the counter is divisible by the height
if (counter == image.getWidth())
{
counter = 0;
heightIndex++;
}
data[counter][heightIndex] = tileMap2.get(line.charAt(i));
counter++;
}
}
BufferedImage img = ImageHandler.convertMap(data);
ImageHandler.outputImage(img, "resources/level");
br.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
}
public static void main(String args[])
{
ImageToText imgText = new ImageToText();
imgText.convertImage();
imgText.condenseFiles();
imgText.readFile();
}
}