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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
| 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(); } } 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]; 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(); } } 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 (int i = 0; i < line.length(); i++) { 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(); } }
|