summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/ImageConverter.java
blob: 091ba1fac1d05f3e8c09e770afa313d1e698e7d5 (plain) (blame)
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
package NET.worlds.scape;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.ColorModel;
import java.awt.image.DirectColorModel;
import java.awt.image.ImageConsumer;
import java.awt.image.IndexColorModel;
import java.util.Hashtable;

public class ImageConverter implements ImageConsumer {
   private String urlName;
   private String filename;
   private int width;
   private int height;
   private Image image;
   private boolean done;
   private boolean ok;
   private boolean debug = false;
   private ColorModel model;
   private int transparentColor = -1;
   private int hDIB;
   private int pixelPtr;

   static {
      nativeInit();
   }

   public ImageConverter(String urlName, String filename) {
      this.urlName = urlName;
      this.filename = filename;
   }

   public static native void nativeInit();

   public synchronized int convert() {
      this.image = Toolkit.getDefaultToolkit().getImage(this.filename);
      this.image.getSource().startProduction(this);

      while (!this.done) {
         try {
            this.wait();
         } catch (InterruptedException var2) {
         }
      }

      int textureID = 0;
      if (this.ok) {
         textureID = this.convertDIBToTexture();
      }

      this.cleanup();
      return textureID;
   }

   @Override
   public void setDimensions(int width, int height) {
      if (this.debug) {
         System.out.println("Set dimensions: w " + width + " h " + height);
      }

      this.width = width;
      this.height = height;
   }

   @Override
   public void setProperties(Hashtable props) {
      if (this.debug) {
         System.out.println("Set properties");
      }
   }

   @Override
   public void setColorModel(ColorModel model) {
      if (this.debug) {
         System.out.println("Set color model: " + model);
      }

      this.model = model;

      assert this.width != 0 && this.height != 0;

      int[] palette = (int[])null;
      int colors = 0;
      if (model instanceof IndexColorModel) {
         IndexColorModel im = (IndexColorModel)model;
         colors = im.getMapSize();
         this.transparentColor = im.getTransparentPixel();
         palette = new int[colors * 3];

         for (int i = 0; i < colors; i++) {
            palette[i] = im.getRGB(i);
         }
      } else {
         DirectColorModel dm = (DirectColorModel)model;

         assert dm.getBlueMask() == 255;

         assert dm.getGreenMask() == 65280;

         assert dm.getRedMask() == 16711680;
      }

      this.prepareDIB(this.width, this.height, colors, palette);
   }

   @Override
   public void setHints(int hintflags) {
      if (this.debug) {
         System.out
            .println(
               "Set hints: "
                  + ((hintflags & 1) != 0 ? " RANDOM " : "")
                  + ((hintflags & 2) != 0 ? " TOPDOWNLEFTRIGHT " : "")
                  + ((hintflags & 4) != 0 ? " COMPLETESCANS " : "")
                  + ((hintflags & 8) != 0 ? " SINGLEPASS " : "")
                  + ((hintflags & 16) != 0 ? " SINGLEFRAME " : "")
            );
      }
   }

   @Override
   public void setPixels(int x, int y, int w, int h, ColorModel model, byte[] pixels, int off, int scansize) {
      if (this.debug) {
         System.out.println("setPixels(byte): x " + x + " y " + y + " w " + w + " h " + h + " model " + model + " off " + off + " scansize " + scansize);
      }

      assert model == this.model;

      this.setDIBPixelBytes(x, y, w, h, pixels, off, scansize);
   }

   @Override
   public void setPixels(int x, int y, int w, int h, ColorModel model, int[] pixels, int off, int scansize) {
      if (this.debug) {
         System.out.println("setPixels(int): x " + x + " y " + y + " w " + w + " h " + h + " model " + model + " off " + off + " scansize " + scansize);
      }

      assert model == this.model;

      this.setDIBPixelInts(x, y, w, h, pixels, off, scansize);
   }

   @Override
   public synchronized void imageComplete(int status) {
      if (this.debug) {
         String state = null;
         switch (status) {
            case 1:
               state = "ERROR";
               break;
            case 2:
               state = "SINGLEDONE";
               break;
            case 3:
               state = "STATICDONE";
               break;
            case 4:
               state = "ABORTED";
         }

         System.out.println("Image complete: " + state);
      }

      this.image.getSource().removeConsumer(this);
      this.image.flush();
      this.ok = status != 1 && status != 4;
      this.done = true;
      this.notify();
   }

   private native void prepareDIB(int var1, int var2, int var3, int[] var4);

   private native void setDIBPixelBytes(int var1, int var2, int var3, int var4, byte[] var5, int var6, int var7);

   private native void setDIBPixelInts(int var1, int var2, int var3, int var4, int[] var5, int var6, int var7);

   private native int convertDIBToTexture();

   private native void cleanup();
}