├── README.md ├── src ├── XPChar.java ├── XPLayer.java ├── XPFile.java ├── REXReader.java └── CompressionUtils.java └── LICENSE /README.md: -------------------------------------------------------------------------------- 1 | # xpreader 2 | Rudimentary java reader for the ascii art editor REX Paints file format .xp 3 | 4 | ##REXPaint: 5 | http://www.gridsagegames.com/rexpaint/ 6 | 7 | License: 8 | http://www.wtfpl.net/ 9 | -------------------------------------------------------------------------------- /src/XPChar.java: -------------------------------------------------------------------------------- 1 | package rl.util; 2 | 3 | import java.awt.*; 4 | 5 | /** 6 | * Created by bison on 02-01-2016. 7 | */ 8 | public class XPChar 9 | { 10 | public char code; 11 | public Color fgColor; 12 | public Color bgColor; 13 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 2 | Version 2, December 2004 3 | 4 | Copyright (C) 2004 Sam Hocevar 5 | 6 | Everyone is permitted to copy and distribute verbatim or modified 7 | copies of this license document, and changing it is allowed as long 8 | as the name is changed. 9 | 10 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 11 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 12 | 13 | 0. You just DO WHAT THE FUCK YOU WANT TO. 14 | -------------------------------------------------------------------------------- /src/XPLayer.java: -------------------------------------------------------------------------------- 1 | package rl.util; 2 | 3 | import rl.util.asciiPanel.AsciiPanel; 4 | 5 | import java.awt.*; 6 | 7 | /** 8 | * Created by bison on 02-01-2016. 9 | */ 10 | public class XPLayer 11 | { 12 | public int width; 13 | public int height; 14 | public XPChar[][] data; 15 | 16 | public void draw(AsciiPanel terminal) 17 | { 18 | for(int y=0; y < height; y++) { 19 | for (int x = 0; x < width; x++) { 20 | if(data[x][y].code != 32) 21 | terminal.write(data[x][y].code, x, y, data[x][y].fgColor, data[x][y].bgColor); 22 | } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/XPFile.java: -------------------------------------------------------------------------------- 1 | package rl.util; 2 | 3 | import rl.util.asciiPanel.AsciiPanel; 4 | 5 | import java.awt.*; 6 | import java.util.ArrayList; 7 | 8 | /** 9 | * Created by bison on 02-01-2016. 10 | */ 11 | public class XPFile { 12 | int version; 13 | int noLayers; 14 | ArrayList layers; 15 | 16 | public XPFile(int version, int noLayers, ArrayList layers) { 17 | this.version = version; 18 | this.noLayers = noLayers; 19 | this.layers = layers; 20 | } 21 | 22 | public void draw(AsciiPanel terminal) 23 | { 24 | if(layers.size() < 1) 25 | return; 26 | layers.get(0).draw(terminal); 27 | } 28 | 29 | public XPLayer layer(int i) 30 | { 31 | return layers.get(i); 32 | } 33 | 34 | public int noLayers() 35 | { 36 | return noLayers; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/REXReader.java: -------------------------------------------------------------------------------- 1 | package rl.util; 2 | 3 | import java.awt.*; 4 | import java.io.File; 5 | import java.io.IOException; 6 | import java.nio.ByteBuffer; 7 | import java.nio.ByteOrder; 8 | import java.nio.file.Files; 9 | import java.util.ArrayList; 10 | import java.util.zip.DataFormatException; 11 | 12 | /** 13 | * Created by bison on 02-01-2016. 14 | */ 15 | public class REXReader { 16 | static public XPFile loadXP(String filename) throws IOException, DataFormatException { 17 | byte[] compressed = Files.readAllBytes(new File(filename).toPath()); 18 | System.out.println("Size of compressed data: " + compressed.length); 19 | byte[] decompressed = CompressionUtils.gzipDecodeByteArray(compressed); 20 | System.out.println("Size of decompressed data: " + decompressed.length); 21 | ByteBuffer bb = ByteBuffer.wrap(decompressed); 22 | bb.order(ByteOrder.LITTLE_ENDIAN); 23 | int version = bb.getInt(); 24 | int no_layers = bb.getInt(); 25 | System.out.println("version is " + version + ", layers = " + no_layers); 26 | ArrayList layers = new ArrayList<>(); 27 | 28 | // x-value with "index/height", and y-value with "index%height" 29 | 30 | for(int i=0; i < no_layers; i++) { 31 | XPLayer layer = new XPLayer(); 32 | layer.width = bb.getInt(); 33 | layer.height = bb.getInt(); 34 | layer.data = new XPChar[layer.width][layer.height]; 35 | for(int index = 0; index < (layer.width*layer.height); index++) 36 | { 37 | int x = index / layer.height; 38 | int y = index % layer.height; 39 | char ch = (char) bb.getInt(); 40 | 41 | Byte fR = bb.get(); 42 | Byte fG = bb.get(); 43 | Byte fB = bb.get(); 44 | Byte bR = bb.get(); 45 | Byte bG = bb.get(); 46 | Byte bB = bb.get(); 47 | Color fgcol = new Color(pack(fR, fG, fB, 0)); 48 | Color bgcol = new Color(pack(bR, bG, bB, 0)); 49 | //System.out.println("char=" + int_ch + ", fgcol =" + fgcol + ", bgcol =" + bgcol); 50 | XPChar xpch = new XPChar(); 51 | xpch.fgColor = fgcol; 52 | xpch.bgColor = bgcol; 53 | xpch.code = ch; 54 | layer.data[x][y] = xpch; 55 | } 56 | layers.add(layer); 57 | } 58 | XPFile xp = new XPFile(version, no_layers, layers); 59 | return xp; 60 | } 61 | 62 | public static int pack(int r, int g, int b, int a) 63 | { 64 | //return (c1 << 24) | (c2 << 16) | (c3 << 8) | (c4); 65 | int value = ((a & 0xFF) << 24) | 66 | ((r & 0xFF) << 16) | 67 | ((g & 0xFF) << 8) | 68 | ((b & 0xFF) << 0); 69 | return value; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/CompressionUtils.java: -------------------------------------------------------------------------------- 1 | package rl.util; 2 | 3 | /** 4 | * Created by bison on 02-01-2016. 5 | */ 6 | import java.io.*; 7 | import java.util.List; 8 | import java.util.Map; 9 | import java.util.logging.Logger; 10 | import java.util.zip.DataFormatException; 11 | import java.util.zip.Deflater; 12 | import java.util.zip.GZIPInputStream; 13 | import java.util.zip.Inflater; 14 | 15 | public class CompressionUtils { 16 | 17 | public static byte[] compress(byte[] data) throws IOException { 18 | Deflater deflater = new Deflater(); 19 | deflater.setInput(data); 20 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); 21 | deflater.finish(); 22 | byte[] buffer = new byte[1024]; 23 | while (!deflater.finished()) { 24 | int count = deflater.deflate(buffer); // returns the generated code... index 25 | outputStream.write(buffer, 0, count); 26 | } 27 | outputStream.close(); 28 | byte[] output = outputStream.toByteArray(); 29 | /* 30 | LOG.debug("Original: " + data.length / 1024 + " Kb"); 31 | LOG.debug("Compressed: " + output.length / 1024 + " Kb"); 32 | */ 33 | return output; 34 | } 35 | public static byte[] decompress(byte[] data) throws IOException, DataFormatException { 36 | Inflater inflater = new Inflater(); 37 | inflater.setInput(data); 38 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); 39 | byte[] buffer = new byte[1024]; 40 | while (!inflater.finished()) { 41 | int count = inflater.inflate(buffer); 42 | outputStream.write(buffer, 0, count); 43 | } 44 | outputStream.close(); 45 | byte[] output = outputStream.toByteArray(); 46 | /* 47 | LOG.debug("Original: " + data.length); 48 | LOG.debug("Compressed: " + output.length); 49 | */ 50 | return output; 51 | } 52 | 53 | public static byte[] gzipDecodeByteArray(byte[] data) { 54 | GZIPInputStream gzipInputStream = null; 55 | try { 56 | gzipInputStream = new GZIPInputStream( 57 | new ByteArrayInputStream(data)); 58 | 59 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); 60 | byte[] buffer = new byte[1024]; 61 | while (gzipInputStream.available() > 0) { 62 | int count = gzipInputStream.read(buffer, 0, 1024); 63 | if(count > 0) { 64 | //System.out.println("Read " + count + " bytes"); 65 | outputStream.write(buffer, 0, count); 66 | } 67 | } 68 | 69 | outputStream.close(); 70 | gzipInputStream.close(); 71 | return outputStream.toByteArray(); 72 | } catch (IOException e) { 73 | e.printStackTrace(); 74 | return null; 75 | } 76 | } 77 | } 78 | --------------------------------------------------------------------------------