1 29 package Acme.JPM.Encoders; 30 31 import java.awt.*; 32 import java.awt.image.ColorModel ; 33 import java.awt.image.ImageConsumer ; 34 import java.awt.image.ImageProducer ; 35 import java.io.IOException ; 36 import java.io.OutputStream ; 37 import java.util.Hashtable ; 38 39 57 public abstract class ImageEncoder implements ImageConsumer { 58 59 protected OutputStream out; 60 61 private ImageProducer producer; 62 private int width = -1; 63 private int height = -1; 64 private int hintflags = 0; 65 private boolean started = false; 66 private boolean encoding; 67 private IOException iox; 68 private static final ColorModel rgbModel = ColorModel.getRGBdefault(); 69 private Hashtable props = null; 70 71 public ImageEncoder(Image img, OutputStream out) throws IOException { 75 this(img.getSource(), out); 76 } 77 78 public ImageEncoder(ImageProducer producer, OutputStream out) throws IOException { 82 this.producer = producer; 83 this.out = out; 84 } 85 86 87 89 abstract void encodeStart(int w, int h) throws IOException ; 91 92 abstract void encodePixels(int x, int y, int w, int h, int[] rgbPixels, int off, int scansize) 97 throws IOException ; 98 99 abstract void encodeDone() throws IOException ; 101 102 103 105 public synchronized void encode() throws IOException { 107 encoding = true; 108 iox = null; 109 producer.startProduction(this); 110 while (encoding) 111 try { 112 wait(); 113 } catch (InterruptedException e) {} 114 if (iox != null) 115 throw iox; 116 } 117 118 private boolean accumulate = false; 119 private int[] accumulator; 120 121 private void encodePixelsWrapper(int x, int y, int w, int h, int[] rgbPixels, int off, int scansize) 122 throws IOException { 123 if (!started) { 124 started = true; 125 encodeStart(width, height); 126 if ((hintflags & TOPDOWNLEFTRIGHT) == 0) { 127 accumulate = true; 128 accumulator = new int[width * height]; 129 } 130 } 131 if (accumulate) 132 for (int row = 0; row < h; ++row) 133 System.arraycopy(rgbPixels, row * scansize + off, 134 accumulator, (y + row) * width + x, 135 w); 136 else 137 encodePixels(x, y, w, h, rgbPixels, off, scansize); 138 } 139 140 private void encodeFinish() throws IOException { 141 if (accumulate) { 142 encodePixels(0, 0, width, height, accumulator, 0, width); 143 accumulator = null; 144 accumulate = false; 145 } 146 } 147 148 private synchronized void stop() { 149 encoding = false; 150 notifyAll(); 151 } 152 153 154 156 public void setDimensions(int width, int height) { 157 this.width = width; 158 this.height = height; 159 } 160 161 public void setProperties(Hashtable props) { 162 this.props = props; 163 } 164 165 public void setColorModel(ColorModel model) { 166 } 168 169 public void setHints(int hintflags) { 170 this.hintflags = hintflags; 171 } 172 173 public void setPixels(int x, int y, int w, int h, ColorModel model, byte[] pixels, 174 int off, int scansize) { 175 int[] rgbPixels = new int[w]; 176 for (int row = 0; row < h; ++row) { 177 int rowOff = off + row * scansize; 178 for (int col = 0; col < w; ++col) 179 rgbPixels[col] = model.getRGB(pixels[rowOff + col] & 0xff); 180 try { 181 encodePixelsWrapper(x, y + row, w, 1, rgbPixels, 0, w); 182 } catch (IOException e) { 183 iox = e; 184 stop(); 185 return; 186 } 187 } 188 } 189 190 public void setPixels(int x, int y, int w, int h, ColorModel model, int[] pixels, 191 int off, int scansize) { 192 if (model == rgbModel) { 193 try { 194 encodePixelsWrapper(x, y, w, h, pixels, off, scansize); 195 } catch (IOException e) { 196 iox = e; 197 stop(); 198 return; 199 } 200 } else { 201 int[] rgbPixels = new int[w]; 202 for (int row = 0; row < h; ++row) { 203 int rowOff = off + row * scansize; 204 for (int col = 0; col < w; ++col) 205 rgbPixels[col] = model.getRGB(pixels[rowOff + col]); 206 try { 207 encodePixelsWrapper(x, y + row, w, 1, rgbPixels, 0, w); 208 } catch (IOException e) { 209 iox = e; 210 stop(); 211 return; 212 } 213 } 214 } 215 } 216 217 public void imageComplete(int status) { 218 producer.removeConsumer(this); 219 if (status == ImageConsumer.IMAGEABORTED) 220 iox = new IOException ("image aborted"); 221 else { 222 try { 223 encodeFinish(); 224 encodeDone(); 225 } catch (IOException e) { 226 iox = e; 227 } 228 } 229 stop(); 230 } 231 232 } 233 | Popular Tags |