1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55:
56:
61: public class ImageConverter implements ImageConsumer
62: {
63:
64: public static final String IMAGE_TRANSPARENCY_PROPERTY =
65: "gnu.awt.image.transparency";
66:
67: public static final String IMAGE_PROPERTIES_PROPERTY =
68: "gnu.awt.image.properties";
69:
70: private AsyncImage image;
71: private BufferedImage bImage;
72: private Hashtable imageProperties;
73: private int width, height;
74: private ColorModel colorModel;
75: private ColorModel targetColorModel;
76:
77: public ImageConverter()
78: {
79: width = 0;
80: height = 0;
81: image = new AsyncImage();
82: }
83:
84: public void setDimensions(int w, int h)
85: {
86: width = w;
87: height = h;
88: }
89:
90: public void setProperties(Hashtable props)
91: {
92:
93: }
94:
95: public void setColorModel(ColorModel model)
96: {
97: colorModel = model;
98: }
99:
100: public void setHints(int flags)
101: {
102:
103: }
104:
105: public void setPixels(int x, int y, int w, int h, ColorModel model,
106: byte[] pixels, int offset, int scansize)
107: {
108: model = setupColorModel(model);
109:
110: if (bImage == null)
111: {
112: createImage();
113: }
114:
115: Integer t = (Integer) imageProperties.get("gnu.awt.image.transparency");
116: int transparency = t.intValue();
117:
118: if(targetColorModel.equals(model))
119: {
120: transparency = transferPixels(x, y, w, h, model, pixels, offset,
121: scansize, transparency);
122: }
123: else if (model instanceof IndexColorModel
124: && targetColorModel.equals(ColorModel.getRGBdefault()))
125: {
126: transparency = convertIndexColorModelToSRGB(x, y, w, h,
127: (IndexColorModel) model,
128: pixels, offset, scansize,
129: transparency);
130: }
131: else
132: {
133: transparency = convertPixels(x, y, w, h, model, pixels, offset,
134: scansize, transparency);
135: }
136:
137: imageProperties.put("gnu.awt.image.transparency",
138: Integer.valueOf(transparency));
139: }
140:
141: public void setPixels(int x, int y, int w, int h, ColorModel model,
142: int[] pixels, int offset, int scansize)
143: {
144: model = setupColorModel(model);
145: if (bImage == null)
146: {
147: createImage();
148: }
149:
150: Integer t = (Integer) imageProperties.get(IMAGE_TRANSPARENCY_PROPERTY);
151: int transparency= t.intValue();
152:
153: if (targetColorModel.equals(model))
154: {
155: transparency = transferPixels(x, y, w, h, model, pixels, offset,
156: scansize, transparency);
157: }
158: else if (model instanceof IndexColorModel
159: && targetColorModel.equals(ColorModel.getRGBdefault()))
160: {
161: transparency = convertIndexColorModelToSRGB(x, y, w, h,
162: (IndexColorModel) model,
163: pixels, offset, scansize,
164: transparency);
165: }
166: else
167: {
168: transparency = convertPixels(x, y, w, h, model, pixels, offset,
169: scansize, transparency);
170: }
171:
172: imageProperties.put(IMAGE_TRANSPARENCY_PROPERTY,
173: Integer.valueOf(transparency));
174:
175: }
176:
177:
186: private ColorModel setupColorModel(ColorModel model)
187: {
188:
189: if (model == null)
190: model = colorModel;
191:
192:
193: if (model == null)
194: model = ColorModel.getRGBdefault();
195:
196:
197:
198: if (targetColorModel == null)
199: targetColorModel = model;
200: targetColorModel = ColorModel.getRGBdefault();
201: return model;
202: }
203:
204:
207: private void createImage()
208: {
209: if (imageProperties == null)
210: {
211: imageProperties = new Hashtable();
212: }
213:
214: imageProperties.put(IMAGE_TRANSPARENCY_PROPERTY,
215: Integer.valueOf(Transparency.OPAQUE));
216: imageProperties.put(IMAGE_PROPERTIES_PROPERTY, imageProperties);
217:
218:
219: if (ColorModel.getRGBdefault().equals(targetColorModel))
220: {
221: bImage = GraphicsEnvironment.getLocalGraphicsEnvironment()
222: .getDefaultScreenDevice()
223: .getDefaultConfiguration()
224: .createCompatibleImage(width, height, Transparency.TRANSLUCENT);
225: }
226: else
227: {
228: WritableRaster raster =
229: targetColorModel.createCompatibleWritableRaster(width, height);
230: bImage = new BufferedImage(targetColorModel, raster, false,
231: imageProperties);
232: }
233: image.setRealImage(bImage);
234: return;
235: }
236:
237:
252: private int transferPixels(int x, int y, int w, int h, ColorModel model,
253: byte[] pixels, int offset, int scansize,
254: int transparency)
255: {
256:
257:
258: bImage.getRaster().setDataElements(x, y, w, h, pixels);
259:
260: for (int yy = 0; yy < h; yy++)
261: {
262: for (int xx = 0; xx < w; xx++)
263: {
264: int pixel = 0xFF & pixels[yy * scansize + xx + offset];
265: int alpha = model.getAlpha(pixel);
266: transparency = updateTransparency(alpha, transparency);
267: }
268: }
269: return transparency;
270: }
271:
272:
287: private int transferPixels(int x, int y, int w, int h, ColorModel model,
288: int[] pixels, int offset, int scansize,
289: int transparency)
290: {
291:
292:
293: bImage.getRaster().setDataElements(x, y, w, h, pixels);
294:
295: for (int yy = 0; yy < h; yy++)
296: {
297: for (int xx = 0; xx < w; xx++)
298: {
299: int pixel = pixels[yy * scansize + xx + offset];
300: int alpha = model.getAlpha(pixel);
301: transparency = updateTransparency(alpha, transparency);
302: }
303: }
304: return transparency;
305: }
306:
307:
323: private int convertPixels(int x, int y, int w, int h, ColorModel model,
324: byte[] pixels, int offset, int scansize,
325: int transparency)
326: {
327:
328:
329: Object dataEl = null;
330:
331: for (int yy = 0; yy < h; yy++)
332: {
333: for (int xx = 0; xx < w; xx++)
334: {
335: int pixel = 0xFF & pixels[yy * scansize + xx + offset];
336: int rgb = model.getRGB(pixel);
337: int alpha = model.getAlpha(pixel);
338: transparency = updateTransparency(alpha, transparency);
339: dataEl = targetColorModel.getDataElements(rgb, dataEl);
340: bImage.getRaster().setDataElements(x + xx, y + yy, dataEl);
341: }
342: }
343: return transparency;
344: }
345:
346:
362: private int convertPixels(int x, int y, int w, int h, ColorModel model,
363: int[] pixels, int offset, int scansize,
364: int transparency)
365: {
366:
367:
368: Object dataEl = null;
369:
370: for (int yy = 0; yy < h; yy++)
371: {
372: for (int xx = 0; xx < w; xx++)
373: {
374: int pixel = pixels[yy * scansize + xx + offset];
375: int rgb = model.getRGB(pixel);
376: int alpha = model.getAlpha(pixel);
377: transparency = updateTransparency(alpha, transparency);
378: dataEl = targetColorModel.getDataElements(rgb, dataEl);
379: bImage.getRaster().setDataElements(x + xx, y + yy, dataEl);
380: }
381: }
382: return transparency;
383: }
384:
385:
400: private int convertIndexColorModelToSRGB(int x, int y, int w, int h,
401: IndexColorModel model,
402: byte[] pixels, int offset,
403: int scansize, int transparency)
404: {
405:
406: int mapSize = model.getMapSize();
407: int[] colorMap = new int[mapSize];
408: for(int i=0; i < mapSize; i++)
409: {
410: colorMap[i] = model.getRGB(i);
411: }
412:
413: WritableRaster raster = bImage.getRaster();
414: SinglePixelPackedSampleModel sampleMode =
415: (SinglePixelPackedSampleModel) raster.getSampleModel();
416: DataBuffer dataBuffer = (DataBuffer) raster.getDataBuffer();
417:
418: int rasterOffset = sampleMode.getOffset(x,y)+dataBuffer.getOffset();
419: int rasterScanline = sampleMode.getScanlineStride();
420:
421: for (int yy = 0; yy < h; yy++)
422: {
423: int xoffset = offset;
424: for (int xx = 0; xx < w; xx++)
425: {
426: int argb = colorMap[(pixels[xoffset++] & 0xFF)];
427: dataBuffer.setElem(rasterOffset+xx, argb);
428: int alpha = (argb >>> 24);
429: transparency = updateTransparency(alpha, transparency);
430: }
431: offset += scansize;
432: rasterOffset += rasterScanline;
433: }
434:
435: return transparency;
436: }
437:
438:
453: private int convertIndexColorModelToSRGB(int x, int y, int w, int h,
454: IndexColorModel model, int[] pixels,
455: int offset, int scansize,
456: int transparency)
457: {
458: int mapSize = model.getMapSize();
459: int[] colorMap = new int[mapSize];
460: for(int i=0; i < mapSize; i++)
461: {
462: colorMap[i] = model.getRGB(i);
463: }
464:
465: WritableRaster raster = bImage.getRaster();
466: SinglePixelPackedSampleModel sampleMode =
467: (SinglePixelPackedSampleModel) raster.getSampleModel();
468: DataBuffer dataBuffer = (DataBuffer)raster.getDataBuffer();
469:
470: int rasterOffset = sampleMode.getOffset(x, y) + dataBuffer.getOffset();
471: int rasterScanline = sampleMode.getScanlineStride();
472:
473: for (int yy = 0; yy < h; yy++)
474: {
475: int xoffset = offset;
476: for (int xx = 0; xx < w; xx++)
477: {
478: int argb = colorMap[pixels[xoffset++]];
479: dataBuffer.setElem(rasterOffset + xx, argb);
480: int alpha = (argb >>> 24);
481: transparency = updateTransparency(alpha, transparency);
482: }
483: offset += scansize;
484: rasterOffset += rasterScanline;
485: }
486:
487: return transparency;
488: }
489:
490:
498: private int updateTransparency(int alpha, int transparency)
499: {
500: if (alpha != 0xFF)
501: {
502: if (alpha == 0x00 && transparency <= Transparency.BITMASK)
503: {
504: transparency = Transparency.BITMASK;
505: }
506: else if (transparency < Transparency.TRANSLUCENT)
507: {
508: transparency = Transparency.TRANSLUCENT;
509: }
510: }
511: return transparency;
512: }
513:
514: public void imageComplete(int status)
515: {
516: image.notifyObservers(ImageObserver.ALLBITS, 0, 0, width, height);
517: }
518:
519: public void setTargetColorModel(ColorModel model)
520: {
521: targetColorModel = model;
522: }
523:
524: public Image getImage()
525: {
526: return image;
527: }
528: }