/** * Tests capability to write GIF image. * * @throws IOException If an error occured while writting the image. */ @Test public void testGIFImageWrite() throws IOException { assertTrue("Assertions should be enabled.", ImageWorker.class.desiredAssertionStatus()); // Get the image of the world with transparency. ImageWorker worker = new ImageWorker(worldDEMImage); show(worker, "Input GIF"); RenderedImage image = worker.getRenderedImage(); ColorModel cm = image.getColorModel(); assertTrue("wrong color model", cm instanceof IndexColorModel); assertEquals("wrong transparency model", Transparency.OPAQUE, cm.getTransparency()); // Writes it out as GIF on a file using index color model with final File outFile = TestData.temp(this, "temp.gif"); worker.writeGIF(outFile, "LZW", 0.75f); // Read it back final ImageWorker readWorker = new ImageWorker(ImageIO.read(outFile)); show(readWorker, "GIF to file"); image = readWorker.getRenderedImage(); cm = image.getColorModel(); assertTrue("wrong color model", cm instanceof IndexColorModel); assertEquals("wrong transparency model", Transparency.OPAQUE, cm.getTransparency()); // Write on an output streams. final OutputStream os = new FileOutputStream(outFile); worker = new ImageWorker(worldImage); worker.forceIndexColorModelForGIF(true); worker.writeGIF(os, "LZW", 0.75f); // Read it back. readWorker.setImage(ImageIO.read(outFile)); show(readWorker, "GIF to output stream"); image = readWorker.getRenderedImage(); cm = image.getColorModel(); assertTrue("wrong color model", cm instanceof IndexColorModel); assertEquals("wrong transparency model", Transparency.BITMASK, cm.getTransparency()); assertEquals( "wrong transparent color index", 255, ((IndexColorModel) cm).getTransparentPixel()); outFile.delete(); }
@Test public void test16BitGIF() throws Exception { // the resource has been compressed since the palette is way larger than the image itself, // and the palette does not get compressed InputStream gzippedStream = ImageWorkerTest.class.getResource("test-data/sf-sfdem.tif.gz").openStream(); GZIPInputStream is = new GZIPInputStream(gzippedStream); try { ImageInputStream iis = ImageIO.createImageInputStream(is); ImageReader reader = new TIFFImageReaderSpi().createReaderInstance(iis); reader.setInput(iis); BufferedImage bi = reader.read(0); if (TestData.isInteractiveTest()) { ImageIOUtilities.visualize(bi, "before"); } reader.dispose(); iis.close(); IndexColorModel icm = (IndexColorModel) bi.getColorModel(); assertEquals(65536, icm.getMapSize()); final File outFile = TestData.temp(this, "temp.gif"); ImageWorker worker = new ImageWorker(bi); worker.writeGIF(outFile, "LZW", 0.75f); // Read it back. bi = ImageIO.read(outFile); if (TestData.isInteractiveTest()) { ImageIOUtilities.visualize(bi, "after"); } ColorModel cm = bi.getColorModel(); assertTrue("wrong color model", cm instanceof IndexColorModel); assertEquals("wrong transparency model", Transparency.OPAQUE, cm.getTransparency()); final IndexColorModel indexColorModel = (IndexColorModel) cm; assertEquals("wrong transparent color index", -1, indexColorModel.getTransparentPixel()); assertEquals("wrong component size", 8, indexColorModel.getComponentSize(0)); outFile.delete(); } finally { is.close(); } }