Пример #1
0
  /**
   * Testing PNG capabilities.
   *
   * @throws IOException If an error occured while writting the image.
   */
  @Test
  public void testPNGWrite() throws IOException {
    assertTrue("Assertions should be enabled.", ImageWorker.class.desiredAssertionStatus());
    // Get the image of the world with transparency.
    final ImageWorker worker = new ImageWorker(worldImage);
    show(worker, "Input file");

    // /////////////////////////////////////////////////////////////////////
    // native png filtered compression 24 bits
    // /////////////////////////////////////////////////////////////////////
    final File outFile = TestData.temp(this, "temp.png");
    worker.writePNG(outFile, "FILTERED", 0.75f, true, false);
    final ImageWorker readWorker = new ImageWorker(ImageIO.read(outFile));
    show(readWorker, "Native PNG24");

    // /////////////////////////////////////////////////////////////////////
    // native png filtered compression 8 bits
    // /////////////////////////////////////////////////////////////////////
    worker.setImage(worldImage);
    worker.writePNG(outFile, "FILTERED", 0.75f, true, true);
    readWorker.setImage(ImageIO.read(outFile));
    show(readWorker, "native PNG8");

    // /////////////////////////////////////////////////////////////////////
    // pure java png 24
    // /////////////////////////////////////////////////////////////////////
    worker.setImage(worldImage);
    worker.writePNG(outFile, "FILTERED", 0.75f, false, false);
    readWorker.setImage(ImageIO.read(outFile));
    show(readWorker, "Pure  PNG24");

    // /////////////////////////////////////////////////////////////////////
    // pure java png 8
    // /////////////////////////////////////////////////////////////////////
    worker.setImage(worldImage);
    worker.writePNG(outFile, "FILTERED", 0.75f, false, true);
    readWorker.setImage(ImageIO.read(outFile));
    show(readWorker, "Pure  PNG8");
    outFile.delete();

    // Check we are not expanding to RGB a paletted image
    worker.setImage(sstImage);
    assertTrue(sstImage.getColorModel() instanceof IndexColorModel);
    worker.writePNG(outFile, "FILTERED", 0.75f, false, false);
    readWorker.setImage(ImageIO.read(outFile));
    assertTrue(readWorker.getRenderedImage().getColorModel() instanceof IndexColorModel);
  }
Пример #2
0
  @Test
  public void test16BitPNG() 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);
      reader.dispose();
      iis.close();
      IndexColorModel icm = (IndexColorModel) bi.getColorModel();
      assertEquals(65536, icm.getMapSize());

      final File outFile = TestData.temp(this, "temp.png");
      ImageWorker worker = new ImageWorker(bi);
      worker.writePNG(outFile, "FILTERED", 0.75f, true, false);
      worker.dispose();

      // make sure we can read it
      BufferedImage back = ImageIO.read(outFile);

      // we expect a RGB one
      ComponentColorModel ccm = (ComponentColorModel) back.getColorModel();
      assertEquals(3, ccm.getNumColorComponents());

      // now ask to write paletted
      worker = new ImageWorker(bi);
      worker.writePNG(outFile, "FILTERED", 0.75f, true, true);
      worker.dispose();

      // make sure we can read it
      back = ImageIO.read(outFile);

      // we expect a RGB one
      icm = (IndexColorModel) back.getColorModel();
      assertEquals(3, icm.getNumColorComponents());
      assertTrue(icm.getMapSize() <= 256);
    } finally {
      is.close();
    }
  }
Пример #3
0
  @Test
  public void test4BitPNG() throws Exception {

    // create test image
    IndexColorModel icm =
        new IndexColorModel(
            4,
            16,
            new byte[] {(byte) 255, 0, 0, 0, 16, 32, 64, (byte) 128, 1, 2, 3, 4, 5, 6, 7, 8},
            new byte[] {0, (byte) 255, 0, 0, 16, 32, 64, (byte) 128, 1, 2, 3, 4, 5, 6, 7, 8},
            new byte[] {0, 0, (byte) 255, 0, 16, 32, 64, (byte) 128, 1, 2, 3, 4, 5, 6, 7, 8});
    assertEquals(16, icm.getMapSize());

    // create random data
    WritableRaster data =
        com.sun.media.jai.codecimpl.util.RasterFactory.createWritableRaster(
            icm.createCompatibleSampleModel(32, 32), new Point(0, 0));
    for (int x = data.getMinX(); x < data.getMinX() + data.getWidth(); x++) {
      for (int y = data.getMinY(); y < data.getMinY() + data.getHeight(); y++) {
        data.setSample(x, y, 0, (x + y) % 8);
      }
    }

    final BufferedImage bi = new BufferedImage(icm, data, false, null);
    assertEquals(16, ((IndexColorModel) bi.getColorModel()).getMapSize());
    assertEquals(4, bi.getSampleModel().getSampleSize(0));
    bi.setData(data);
    if (TestData.isInteractiveTest()) {
      ImageIOUtilities.visualize(bi, "before");
    }

    // encode as png
    ImageWorker worker = new ImageWorker(bi);
    final File outFile = TestData.temp(this, "temp4.png");
    worker.writePNG(outFile, "FILTERED", 0.75f, true, false);
    worker.dispose();

    // make sure we can read it
    BufferedImage back = ImageIO.read(outFile);

    // we expect an IndexColorMolde one matching the old one
    IndexColorModel ccm = (IndexColorModel) back.getColorModel();
    assertEquals(3, ccm.getNumColorComponents());
    assertEquals(16, ccm.getMapSize());
    assertEquals(4, ccm.getPixelSize());
    if (TestData.isInteractiveTest()) {
      ImageIOUtilities.visualize(back, "after");
    }
  }