001: /**
002: *
003: */package org.geotools.coverage.image;
004:
005: import java.awt.HeadlessException;
006: import java.awt.image.BufferedImage;
007: import java.awt.image.RenderedImage;
008: import java.io.File;
009: import java.io.FileOutputStream;
010: import java.io.IOException;
011: import java.io.OutputStream;
012:
013: import javax.imageio.ImageIO;
014: import javax.media.jai.widget.ScrollingImagePanel;
015: import javax.swing.JFrame;
016:
017: import junit.framework.Test;
018: import junit.framework.TestCase;
019: import junit.framework.TestSuite;
020: import junit.textui.TestRunner;
021:
022: import org.geotools.image.ImageWorker;
023: import org.geotools.test.TestData;
024:
025: ;
026: /**
027: * Testing {@link ImageWorker} capabilities.
028: *
029: * @author Simone Giannecchini
030: * @since 2.3.x
031: *
032: */
033: public class ImageWorkerTest extends TestCase {
034:
035: private BufferedImage worldImage;
036:
037: /**
038: * Returns the test suite.
039: */
040: public static Test suite() {
041: return new TestSuite(ImageWorkerTest.class);
042: }
043:
044: /**
045: * @param arg0
046: */
047: public ImageWorkerTest(String arg0) {
048: super (arg0);
049: }
050:
051: /*
052: * (non-Javadoc)
053: *
054: * @see junit.framework.TestCase#setUp()
055: */
056: protected void setUp() throws Exception {
057: super .setUp();
058: File fileImage = TestData.file(this , "world.PNG");
059: assertNotNull(
060: "Unable to get the test file world.PNG, test data returned null",
061: fileImage);
062: assertTrue("Unable to get the test file world.PNG", fileImage
063: .exists());
064: this .worldImage = ImageIO.read(fileImage);
065: assertNotNull("Returned image was null", worldImage);
066:
067: }
068:
069: /*
070: * (non-Javadoc)
071: *
072: * @see junit.framework.TestCase#tearDown()
073: */
074: protected void tearDown() throws Exception {
075: this .worldImage.flush();
076: this .worldImage = null;
077: super .tearDown();
078: }
079:
080: public static void main(String[] args) {
081: TestRunner.run(ImageWorkerTest.class);
082: }
083:
084: /**
085: * Testing capability to write GIF image.
086: *
087: * @throws IOException
088: *
089: */
090: public void testGIFImageWrite() throws IOException {
091: // get the image of the world with transparency
092: ImageWorker worker = new ImageWorker(worldImage);
093:
094: // visualize it
095: if (TestData.isInteractiveTest()) {
096: visualize(worker.getRenderedImage(), "Input GIF");
097:
098: } else
099: worker.getPlanarImage().getAsBufferedImage();
100: // assertions
101: // assertTrue("wrong color model", worker.getRenderedImage()
102: // .getColorModel() instanceof IndexColorModel);
103: // assertTrue("wrong transparency model", worker.getRenderedImage()
104: // .getColorModel().getTransparency() == Transparency.BITMASK);
105: // assertTrue("wrong transparency index",
106: // ((IndexColorModel) worker.getRenderedImage().getColorModel())
107: // .getTransparentPixel() == 255);
108:
109: // write it out as GIF on a file
110: final File outFile = TestData.temp(this , "temp.gif");
111: // go to index color model with floyd stenberg alg.
112: worker.forceIndexColorModelForGIF(true);
113: worker.writeGIF(outFile, "LZW", 0.75f);
114:
115: // read it back
116: final ImageWorker worker2 = new ImageWorker(ImageIO
117: .read(outFile));
118:
119: // re visualize it
120: if (TestData.isInteractiveTest()) {
121: visualize(worker2.getRenderedImage(), "GIF to file");
122: } else
123: worker.getPlanarImage().getAsBufferedImage();
124: // // assertions
125: // assertTrue("wrong color model", worker2.getRenderedImage()
126: // .getColorModel() instanceof IndexColorModel);
127: // assertTrue("wrong transparency model", worker2.getRenderedImage()
128: // .getColorModel().getTransparency() == Transparency.BITMASK);
129: // assertTrue("wrong transparency index",
130: // ((IndexColorModel) worker2.getRenderedImage().getColorModel())
131: // .getTransparentPixel() == 255);
132:
133: // write on an output streams
134: ImageIO.setUseCache(true);
135: final OutputStream os = new FileOutputStream(outFile);
136: worker = new ImageWorker(worldImage);
137: worker.forceIndexColorModelForGIF(true);
138: worker.writeGIF(os, "LZW", 0.75f);
139:
140: // read it back
141: worker2.setImage(ImageIO.read(outFile));
142:
143: // re visualize it
144: if (TestData.isInteractiveTest()) {
145: visualize(worker2.getRenderedImage(),
146: "GIF to output stream");
147: } else
148: worker.getPlanarImage().getAsBufferedImage();
149: // // assertions
150: // assertTrue("wrong color model", worker2.getRenderedImage()
151: // .getColorModel() instanceof IndexColorModel);
152: // assertTrue("wrong transparency model", worker2.getRenderedImage()
153: // .getColorModel().getTransparency() == Transparency.BITMASK);
154: // assertTrue("wrong transparency index",
155: // ((IndexColorModel) worker2.getRenderedImage().getColorModel())
156: // .getTransparentPixel() == 255);
157:
158: }
159:
160: /**
161: * @param image
162: * @param string
163: * @throws HeadlessException
164: */
165: private void visualize(final RenderedImage image, String string)
166: throws HeadlessException {
167: final JFrame f = new JFrame(string);
168: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
169: f.getContentPane()
170: .add(new ScrollingImagePanel(image, 400, 400));
171: f.pack();
172: f.setVisible(true);
173: }
174:
175: /**
176: * Testing JPEG capabilities
177: *
178: * @throws IOException
179: */
180: public void testJPEGWrite() throws IOException {
181: // get the image of the world with transparency
182: final ImageWorker worker = new ImageWorker(worldImage);
183:
184: // visualize it
185: if (TestData.isInteractiveTest()) {
186: visualize(worker.getRenderedImage(), "Input JPEG");
187:
188: }
189:
190: // /////////////////////////////////////////////////////////////////////
191: // nativeJPEG with compression JPEG-LS
192: // TODO: Disabled for now, because Continuum fails in this case.
193: // /////////////////////////////////////////////////////////////////////
194: final File outFile = TestData.temp(this , "temp.jpeg");
195: // worker.writeJPEG(outFile, "JPEG-LS", 0.75f, true);
196: //
197: // // read it back
198: // final ImageWorker worker2 = new ImageWorker(ImageIO.read(outFile));
199: //
200: // // re visualize it
201: // if (TestData.isInteractiveTest()) {
202: // visualize(worker2.getRenderedImage(), "Native JPEG LS");
203: // } else
204: // worker.getPlanarImage().getAsBufferedImage();
205:
206: // /////////////////////////////////////////////////////////////////////
207: // native JPEG compression
208: // /////////////////////////////////////////////////////////////////////
209: worker.setImage(worldImage);
210: worker.writeJPEG(outFile, "JPEG", 0.75f, true);
211:
212: // read it back
213: final ImageWorker worker2 = new ImageWorker(ImageIO
214: .read(outFile));
215:
216: // re visualize it
217: if (TestData.isInteractiveTest()) {
218: visualize(worker2.getRenderedImage(), "native JPEG");
219: } else
220: worker.getPlanarImage().getAsBufferedImage();
221:
222: // /////////////////////////////////////////////////////////////////////
223: // pure java JPEG compression
224: // /////////////////////////////////////////////////////////////////////
225: worker.setImage(worldImage);
226: worker.writeJPEG(outFile, "JPEG", 0.75f, false);
227:
228: // read it back
229: worker2.setImage(ImageIO.read(outFile));
230:
231: // re visualize it
232: if (TestData.isInteractiveTest()) {
233: visualize(worker2.getRenderedImage(), "Pure Java JPEG");
234: } else
235: worker.getPlanarImage().getAsBufferedImage();
236:
237: }
238:
239: /**
240: * Testing PNG capabilities
241: *
242: * @throws IOException
243: */
244: public void testPNGWrite() throws IOException {
245: // get the image of the world with transparency
246: final ImageWorker worker = new ImageWorker(worldImage);
247:
248: // visualize it
249: if (TestData.isInteractiveTest()) {
250: visualize(worker.getRenderedImage(), "Input file");
251:
252: }
253:
254: // /////////////////////////////////////////////////////////////////////
255: // native png filtered compression 24 bits
256: // /////////////////////////////////////////////////////////////////////
257: final File outFile = TestData.temp(this , "temp.png");
258: worker.writePNG(outFile, "FILTERED", 0.75f, true, false);
259:
260: // read it back
261: final ImageWorker worker2 = new ImageWorker(ImageIO
262: .read(outFile));
263:
264: // re visualize it
265: if (TestData.isInteractiveTest()) {
266: visualize(worker2.getRenderedImage(), "Native PNG24");
267: } else
268: worker.getPlanarImage().getAsBufferedImage();
269:
270: // /////////////////////////////////////////////////////////////////////
271: // native png filtered compression 8 bits
272: // /////////////////////////////////////////////////////////////////////
273: worker.setImage(worldImage);
274: worker.writePNG(outFile, "FILTERED", 0.75f, true, true);
275:
276: // read it back
277: worker2.setImage(ImageIO.read(outFile));
278:
279: // re visualize it
280: if (TestData.isInteractiveTest()) {
281: visualize(worker2.getRenderedImage(), "native PNG8");
282: } else
283: worker.getPlanarImage().getAsBufferedImage();
284:
285: // /////////////////////////////////////////////////////////////////////
286: // pure java png 24
287: // /////////////////////////////////////////////////////////////////////
288: worker.setImage(worldImage);
289: worker.writePNG(outFile, "FILTERED", 0.75f, false, false);
290:
291: // read it back
292: worker2.setImage(ImageIO.read(outFile));
293:
294: // re visualize it
295: if (TestData.isInteractiveTest()) {
296: visualize(worker2.getRenderedImage(), "Pure PNG24");
297: } else
298: worker.getPlanarImage().getAsBufferedImage();
299:
300: // /////////////////////////////////////////////////////////////////////
301: // pure java png 8
302: // /////////////////////////////////////////////////////////////////////
303: worker.setImage(worldImage);
304: worker.writePNG(outFile, "FILTERED", 0.75f, false, true);
305:
306: // read it back
307: worker2.setImage(ImageIO.read(outFile));
308:
309: // re visualize it
310: if (TestData.isInteractiveTest()) {
311: visualize(worker2.getRenderedImage(), "Pure PNG8");
312: } else
313: worker.getPlanarImage().getAsBufferedImage();
314: }
315:
316: public void testRGB2Palette() {
317: //creating a worker
318: final ImageWorker worker = new ImageWorker(worldImage);
319:
320: // visualize it
321: if (TestData.isInteractiveTest()) {
322: visualize(worker.getRenderedImage(), "Input file");
323:
324: }
325: worker.forceIndexColorModelForGIF(true);
326:
327: //converto to index color bitmask
328: // assertions
329: // assertTrue("wrong color model", worker.getRenderedImage()
330: // .getColorModel() instanceof IndexColorModel);
331: // assertTrue("wrong transparency model", worker.getRenderedImage()
332: // .getColorModel().getTransparency() == Transparency.BITMASK);
333: // assertTrue("wrong transparency index",
334: // ((IndexColorModel) worker.getRenderedImage().getColorModel())
335: // .getTransparentPixel() == 255);
336: // re visualize it
337: if (TestData.isInteractiveTest()) {
338: visualize(worker.getRenderedImage(), "Paletted bitmask");
339: } else
340: worker.getPlanarImage().getAsBufferedImage();
341:
342: //go back to rgb
343: worker.forceComponentColorModel();
344:
345: // // assertions
346: // assertTrue("wrong color model", worker.getRenderedImage()
347: // .getColorModel() instanceof ComponentColorModel);
348: // assertTrue("wrong bands number", worker.getRenderedImage()
349: // .getColorModel().getNumComponents()==4);
350: // assertTrue("wrong transparency model", worker.getRenderedImage()
351: // .getColorModel().getTransparency() == Transparency.TRANSLUCENT);
352:
353: // re visualize it
354: if (TestData.isInteractiveTest()) {
355: visualize(worker.getRenderedImage(), "RGB translucent");
356: } else
357: worker.getPlanarImage().getAsBufferedImage();
358:
359: }
360: }
|