questionable patch: merge of a lot of stuff from the svg branch
[org.ibex.core.git] / src / org / ibex / graphics / PCX.java
1 package org.ibex.graphics;
2 import java.io.*;
3 import java.util.*;
4
5 public class PCX {
6
7     public static void main(String[] s) throws Exception {
8         FileOutputStream fos = new FileOutputStream("out.pcx");
9         int[] data = new int[104 * 104];
10         for(int i=0; i<104; i++) data[i*104+i] = 1;
11         for(int i=0; i<104; i++) data[i*104+(104-i)] = 1;
12         //for(int i=0; i<104*104; i++) data[i] = i%2==0?1:0;
13         dump(104, 104, data, new DataOutputStream(fos));
14     }
15
16     public static void writeLittleShort(DataOutputStream out, int i) throws IOException {
17         short s = (short)i;
18         out.writeByte(s & 0xff);
19         out.writeByte((s & 0xff00) << 8);
20     }
21
22     public static void dump(int width, int height, int[] data, DataOutputStream out) throws IOException {
23         out.writeByte(0x0A);             // PCX ID
24         out.writeByte(0);                // Version
25         out.writeByte(1);                // Encoding = RLE
26         out.writeByte(1);                // 1bpp
27         writeLittleShort(out, 0);        // XStart
28         writeLittleShort(out, 0);        // YStart
29         //writeLittleShort(out, 203);      // HDPI
30         //writeLittleShort(out, 203);      // VDPI
31         writeLittleShort(out, width-1);    // XEnd
32         writeLittleShort(out, height-1);   // YEnd
33         writeLittleShort(out, width-1);    // XEnd
34         writeLittleShort(out, height-1);   // YEnd
35         for(int i=0; i<48; i++)
36             out.writeByte(0);            // Pallette
37         out.writeByte(0);                // Reserved
38         out.writeByte(1);                // NumBitPlanes
39         writeLittleShort(out, (int)Math.ceil(((float)width)/8));  // bytes per line
40         writeLittleShort(out, 1); // PalleteType=Mono
41         writeLittleShort(out, 0); // HorizScreenSize (FIXME: maybe omit?)
42         writeLittleShort(out, 0); // VertScreenSize (FIXME: maybe omit?)
43         for(int i=0; i<54; i++)
44             out.writeByte(0);            // Reserved
45         
46         byte dat = 0;
47         int count = 0;
48         for(int y=0; y<height; y++) {
49             for(int x=0; x<width; x+=8) {
50                 int i = y * width + x;
51                 byte b = 0;
52                 for(int j=i; j<Math.min(data.length, i+8); j++)
53                     b |= (data[j]==0?0:1) << (7-(j-i));
54                 if (dat==b && count < 63) count++;
55                 else {
56                     if (count > 0) {
57                         out.writeByte(count | 0xc0);
58                         out.writeByte(dat);
59                     }
60                     dat = b;
61                     count = 1;
62                 }
63             }
64             if (count > 0) {
65                 out.writeByte(count | 0xc0);
66                 out.writeByte(dat);
67                 count = 0;
68                 dat = 0;
69             }
70         }
71         out.flush();
72         out.close();
73     }
74
75 }