@Test public void testBitmask() { assertTrue("Assertions should be enabled.", ImageWorker.class.desiredAssertionStatus()); ImageWorker worker = new ImageWorker(sstImage); worker.forceBitmaskIndexColorModel(); assertEquals(1, worker.getNumBands()); assertEquals(-1, worker.getTransparentPixel()); assertTrue(worker.isBytes()); assertFalse(worker.isBinary()); assertTrue(worker.isIndexed()); assertTrue(worker.isColorSpaceRGB()); assertFalse(worker.isColorSpaceGRAYScale()); assertFalse(worker.isTranslucent()); final BufferedImage directRGB = getSyntheticRGB(true); worker = new ImageWorker(directRGB); worker.forceBitmaskIndexColorModel(); assertEquals(1, worker.getNumBands()); assertEquals(-1, worker.getTransparentPixel()); assertTrue(worker.isBytes()); assertFalse(worker.isBinary()); assertTrue(worker.isIndexed()); assertTrue(worker.isColorSpaceRGB()); assertFalse(worker.isColorSpaceGRAYScale()); assertFalse(worker.isTranslucent()); final BufferedImage componentRGB = getSyntheticRGB(false); worker = new ImageWorker(componentRGB); worker.forceBitmaskIndexColorModel(); assertEquals(1, worker.getNumBands()); assertEquals(-1, worker.getTransparentPixel()); assertTrue(worker.isBytes()); assertFalse(worker.isBinary()); assertTrue(worker.isIndexed()); assertTrue(worker.isColorSpaceRGB()); assertFalse(worker.isColorSpaceGRAYScale()); assertFalse(worker.isTranslucent()); final BufferedImage translucentIndexed = getSyntheticTranslucentIndexed(); worker = new ImageWorker(translucentIndexed); assertTrue(worker.isBytes()); assertFalse(worker.isBinary()); assertTrue(worker.isIndexed()); assertTrue(worker.isColorSpaceRGB()); assertTrue(worker.isTranslucent()); worker.forceIndexColorModelForGIF(true); assertEquals(1, worker.getNumBands()); assertEquals(0, worker.getTransparentPixel()); assertTrue(worker.isBytes()); assertFalse(worker.isBinary()); assertTrue(worker.isIndexed()); assertTrue(worker.isColorSpaceRGB()); assertFalse(worker.isTranslucent()); }
/** Tests the {@link ImageWorker#tile()} methods. Some trivial tests are performed before. */ @Test public void testReTile() { assertTrue("Assertions should be enabled.", ImageWorker.class.desiredAssertionStatus()); ImageWorker worker = new ImageWorker(worldImage); assertSame(worldImage, worker.getRenderedImage()); assertEquals(4, worker.getNumBands()); assertEquals(-1, worker.getTransparentPixel()); assertTrue(worker.isBytes()); assertFalse(worker.isBinary()); assertFalse(worker.isIndexed()); assertTrue(worker.isColorSpaceRGB()); assertFalse(worker.isColorSpaceGRAYScale()); assertTrue(worker.isTranslucent()); assertSame("Expected no operation.", worldImage, worker.rescaleToBytes().getRenderedImage()); assertSame( "Expected no operation.", worldImage, worker.forceComponentColorModel().getRenderedImage()); assertSame( "Expected no operation.", worldImage, worker.forceColorSpaceRGB().getRenderedImage()); assertSame("Expected no operation.", worldImage, worker.retainBands(4).getRenderedImage()); // Following will change image, so we need to test after the above assertions. worker.setRenderingHint( JAI.KEY_IMAGE_LAYOUT, new ImageLayout() .setTileGridXOffset(0) .setTileGridYOffset(0) .setTileHeight(64) .setTileWidth(64)); worker.tile(); assertSame("Expected 64.", 64, worker.getRenderedImage().getTileWidth()); assertSame("Expected 64.", 64, worker.getRenderedImage().getTileHeight()); }
/** * Tests the {@link ImageWorker#makeColorTransparent} methods. Some trivial tests are performed * before. * * @throws IOException * @throws FileNotFoundException * @throws IllegalStateException */ @Test public void testMakeColorTransparent() throws IllegalStateException, FileNotFoundException, IOException { assertTrue("Assertions should be enabled.", ImageWorker.class.desiredAssertionStatus()); ImageWorker worker = new ImageWorker(sstImage); assertSame(sstImage, worker.getRenderedImage()); assertEquals(1, worker.getNumBands()); assertEquals(-1, worker.getTransparentPixel()); assertTrue(worker.isBytes()); assertFalse(worker.isBinary()); assertTrue(worker.isIndexed()); assertTrue(worker.isColorSpaceRGB()); assertFalse(worker.isColorSpaceGRAYScale()); assertFalse(worker.isTranslucent()); assertSame( "Expected no operation.", sstImage, worker.forceIndexColorModel(false).getRenderedImage()); assertSame( "Expected no operation.", sstImage, worker.forceIndexColorModel(true).getRenderedImage()); assertSame("Expected no operation.", sstImage, worker.forceColorSpaceRGB().getRenderedImage()); assertSame("Expected no operation.", sstImage, worker.retainFirstBand().getRenderedImage()); assertSame("Expected no operation.", sstImage, worker.retainLastBand().getRenderedImage()); // Following will change image, so we need to test after the above assertions. assertEquals(0, worker.getMinimums()[0], 0); assertEquals(255, worker.getMaximums()[0], 0); assertNotSame(sstImage, worker.getRenderedImage()); assertSame( "Expected same databuffer, i.e. pixels should not be duplicated.", sstImage.getTile(0, 0).getDataBuffer(), worker.getRenderedImage().getTile(0, 0).getDataBuffer()); assertSame(worker, worker.makeColorTransparent(Color.WHITE)); assertEquals(255, worker.getTransparentPixel()); assertFalse(worker.isTranslucent()); assertSame( "Expected same databuffer, i.e. pixels should not be duplicated.", sstImage.getTile(0, 0).getDataBuffer(), worker.getRenderedImage().getTile(0, 0).getDataBuffer()); // INDEX TO INDEX-ALPHA worker = new ImageWorker(chlImage).makeColorTransparent(Color.black); show(worker, "CHL01195.png"); assertEquals(1, worker.getNumBands()); assertEquals(0, worker.getTransparentPixel()); assertTrue(worker.isBytes()); assertTrue(worker.isIndexed()); assertTrue(worker.isColorSpaceRGB()); assertFalse(worker.isColorSpaceGRAYScale()); assertFalse(worker.isTranslucent()); RenderedImage image = worker.getRenderedImage(); assertTrue(image.getColorModel() instanceof IndexColorModel); IndexColorModel iColorModel = (IndexColorModel) image.getColorModel(); int transparentColor = iColorModel.getRGB(worker.getTransparentPixel()) & 0x00ffffff; assertTrue(transparentColor == 0); // INDEX TO INDEX-ALPHA worker = new ImageWorker(bathy).makeColorTransparent(Color.WHITE); show(worker, "BATHY.png"); assertEquals(1, worker.getNumBands()); assertEquals(206, worker.getTransparentPixel()); assertTrue(worker.isBytes()); assertTrue(worker.isIndexed()); assertTrue(worker.isColorSpaceRGB()); assertFalse(worker.isColorSpaceGRAYScale()); assertFalse(worker.isTranslucent()); image = worker.getRenderedImage(); assertTrue(image.getColorModel() instanceof IndexColorModel); iColorModel = (IndexColorModel) image.getColorModel(); transparentColor = iColorModel.getRGB(worker.getTransparentPixel()) & 0x00ffffff; assertTrue(transparentColor == (Color.WHITE.getRGB() & 0x00ffffff)); // RGB TO RGBA worker = new ImageWorker(smallWorld).makeColorTransparent(new Color(11, 10, 50)); show(worker, "small_world.png"); assertEquals(4, worker.getNumBands()); assertEquals(-1, worker.getTransparentPixel()); assertTrue(worker.isBytes()); assertFalse(worker.isIndexed()); assertTrue(worker.isColorSpaceRGB()); assertFalse(worker.isColorSpaceGRAYScale()); assertTrue(worker.isTranslucent()); image = worker.getRenderedImage(); assertTrue(image.getColorModel() instanceof ComponentColorModel); // RGBA to RGBA worker = new ImageWorker(worldImage).makeColorTransparent(Color.white); show(worker, "world.png"); assertEquals(4, worker.getNumBands()); assertEquals(-1, worker.getTransparentPixel()); assertTrue(worker.isBytes()); assertFalse(worker.isIndexed()); assertTrue(worker.isColorSpaceRGB()); assertFalse(worker.isColorSpaceGRAYScale()); assertTrue(worker.isTranslucent()); image = worker.getRenderedImage(); assertTrue(image.getColorModel() instanceof ComponentColorModel); // GRAY TO GRAY-ALPHA worker = new ImageWorker(gray).makeColorTransparent(Color.black); show(worker, "gray.png"); assertEquals(2, worker.getNumBands()); assertEquals(-1, worker.getTransparentPixel()); assertTrue(worker.isBytes()); assertFalse(worker.isIndexed()); assertFalse(worker.isColorSpaceRGB()); assertTrue(worker.isColorSpaceGRAYScale()); assertTrue(worker.isTranslucent()); image = worker.getRenderedImage(); assertTrue(image.getColorModel() instanceof ComponentColorModel); // GRAY-ALPHA TO GRAY-ALPHA. worker = new ImageWorker(grayAlpha).makeColorTransparent(Color.black); show(worker, "gray-alpha.png"); assertEquals(2, worker.getNumBands()); assertEquals(-1, worker.getTransparentPixel()); assertTrue(worker.isBytes()); assertFalse(worker.isIndexed()); assertFalse(worker.isColorSpaceRGB()); assertTrue(worker.isColorSpaceGRAYScale()); assertTrue(worker.isTranslucent()); image = worker.getRenderedImage(); assertTrue(image.getColorModel() instanceof ComponentColorModel); }