added PCX code (incomplete)
authoradam <adam@megacz.com>
Mon, 1 Jan 2007 03:12:59 +0000 (03:12 +0000)
committeradam <adam@megacz.com>
Mon, 1 Jan 2007 03:12:59 +0000 (03:12 +0000)
darcs-hash:20070101031259-5007d-4ed539cda5d7183c9d0bfe36a4cbc02c01b801be.gz

src/org/ibex/graphics/PCX.java [new file with mode: 0644]

diff --git a/src/org/ibex/graphics/PCX.java b/src/org/ibex/graphics/PCX.java
new file mode 100644 (file)
index 0000000..e737cae
--- /dev/null
@@ -0,0 +1,75 @@
+package org.ibex.graphics;
+import java.io.*;
+import java.util.*;
+
+public class PCX {
+
+    public static void main(String[] s) throws Exception {
+        FileOutputStream fos = new FileOutputStream("out.pcx");
+        int[] data = new int[104 * 104];
+        for(int i=0; i<104; i++) data[i*104+i] = 1;
+        for(int i=0; i<104; i++) data[i*104+(104-i)] = 1;
+        //for(int i=0; i<104*104; i++) data[i] = i%2==0?1:0;
+        dump(104, 104, data, new DataOutputStream(fos));
+    }
+
+    public static void writeLittleShort(DataOutputStream out, int i) throws IOException {
+        short s = (short)i;
+        out.writeByte(s & 0xff);
+        out.writeByte((s & 0xff00) << 8);
+    }
+
+    public static void dump(int width, int height, int[] data, DataOutputStream out) throws IOException {
+        out.writeByte(0x0A);             // PCX ID
+        out.writeByte(0);                // Version
+        out.writeByte(1);                // Encoding = RLE
+        out.writeByte(1);                // 1bpp
+        writeLittleShort(out, 0);        // XStart
+        writeLittleShort(out, 0);        // YStart
+        //writeLittleShort(out, 203);      // HDPI
+        //writeLittleShort(out, 203);      // VDPI
+        writeLittleShort(out, width-1);    // XEnd
+        writeLittleShort(out, height-1);   // YEnd
+        writeLittleShort(out, width-1);    // XEnd
+        writeLittleShort(out, height-1);   // YEnd
+        for(int i=0; i<48; i++)
+            out.writeByte(0);            // Pallette
+        out.writeByte(0);                // Reserved
+        out.writeByte(1);                // NumBitPlanes
+        writeLittleShort(out, (int)Math.ceil(((float)width)/8));  // bytes per line
+        writeLittleShort(out, 1); // PalleteType=Mono
+        writeLittleShort(out, 0); // HorizScreenSize (FIXME: maybe omit?)
+        writeLittleShort(out, 0); // VertScreenSize (FIXME: maybe omit?)
+        for(int i=0; i<54; i++)
+            out.writeByte(0);            // Reserved
+        
+        byte dat = 0;
+        int count = 0;
+        for(int y=0; y<height; y++) {
+            for(int x=0; x<width; x+=8) {
+                int i = y * width + x;
+                byte b = 0;
+                for(int j=i; j<Math.min(data.length, i+8); j++)
+                    b |= (data[j]==0?0:1) << (7-(j-i));
+                if (dat==b && count < 63) count++;
+                else {
+                    if (count > 0) {
+                        out.writeByte(count | 0xc0);
+                        out.writeByte(dat);
+                    }
+                    dat = b;
+                    count = 1;
+                }
+            }
+            if (count > 0) {
+                out.writeByte(count | 0xc0);
+                out.writeByte(dat);
+                count = 0;
+                dat = 0;
+            }
+        }
+        out.flush();
+        out.close();
+    }
+
+}