Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Decompression Fun  (Read 381 times)
0 Members and 1 Guest are viewing this topic.
Offline coltonoscopy

Junior Member


Medals: 2



« Posted 2012-08-01 03:54:36 »

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  
/*
 * 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();
    }
}

Straight flippin.
Offline moogie

JGO Knight


Medals: 11
Projects: 5
Exp: 10 years


Java games rock!


« Reply #1 - Posted 2012-08-01 04:53:31 »

most likely you are generating illegal or special unicode when writing the text file.

see http://en.wikipedia.org/wiki/Specials_(Unicode_block)

Offline coltonoscopy

Junior Member


Medals: 2



« Reply #2 - Posted 2012-08-01 05:07:51 »

Son of a...

I love you.

Haha, thank you so much! You were right, although it didn't seem to be within the range that the page specified; rather, it was using from 0 onward, and it must have caused issues with these first Unicode characters. Didn't even occur to me! I switched it to start at 65 (A) instead for now.

Straight flippin.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (27 views)
2014-04-15 18:08:23

BurntPizza (24 views)
2014-04-15 03:46:01

UprightPath (39 views)
2014-04-14 17:39:50

UprightPath (21 views)
2014-04-14 17:35:47

Porlus (37 views)
2014-04-14 15:48:38

tom_mai78101 (61 views)
2014-04-10 04:04:31

BurntPizza (119 views)
2014-04-08 23:06:04

tom_mai78101 (219 views)
2014-04-05 13:34:39

trollwarrior1 (186 views)
2014-04-04 12:06:45

CJLetsGame (193 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!