public void testCreateBanded() {
    int[] bankIndices = new int[] {0, 1, 2};
    int[] bandOffsets = new int[] {1, 1, 1};
    ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    int dataType = DataBuffer.TYPE_BYTE;
    boolean hasAlpha = false;

    ImageTypeSpecifier typeSpecifier =
        ImageTypeSpecifier.createBanded(
            colorSpace, bankIndices, bandOffsets, dataType, hasAlpha, false);

    ColorModel colorModel = typeSpecifier.getColorModel();
    assertEquals(
        "Failed to create with the correct colorspace type",
        ColorSpace.TYPE_RGB,
        colorModel.getColorSpace().getType());
    assertEquals(
        "Failed to create with the correct transparency",
        Transparency.OPAQUE,
        colorModel.getTransparency());
    assertEquals(
        "Failed to create with the correcttransfer type",
        DataBuffer.TYPE_BYTE,
        colorModel.getTransferType());

    BandedSampleModel sampleModel = (BandedSampleModel) typeSpecifier.getSampleModel();
    assertArrayEquals(
        "Failed to create with the correct bankIndices", bankIndices, sampleModel.getBankIndices());
    assertArrayEquals(
        "Failed to create with the correct bankIndices", bandOffsets, sampleModel.getBandOffsets());
  }
  public void testCreateIndexed() {
    byte[] redLUT = new byte[] {1, 10};
    byte[] greenLUT = new byte[] {2, 20};
    byte[] blueLUT = new byte[] {3, 30};
    byte[] alphaLUT = new byte[] {4, 40};

    ImageTypeSpecifier type =
        ImageTypeSpecifier.createIndexed(
            redLUT, greenLUT, blueLUT, alphaLUT, 1, DataBuffer.TYPE_BYTE);
    ColorModel model = type.getColorModel();

    assertEquals(
        "Failed to return the colorspace", ColorSpace.TYPE_RGB, model.getColorSpace().getType());
    assertEquals(
        "Failed to return the transparency", Transparency.TRANSLUCENT, model.getTransparency());
    assertEquals(
        "Failed to return the tranfer type", DataBuffer.TYPE_BYTE, model.getTransferType());
    assertEquals("Failed to return the red color component", 1, model.getRed(0));
    assertEquals("Failed to return the red color component", 10, model.getRed(1));
    assertEquals("Failed to return the green color component", 2, model.getGreen(0));
    assertEquals("Failed to return the green color component", 20, model.getGreen(1));
    assertEquals("Failed to return the blue color component", 3, model.getBlue(0));
    assertEquals("Failed to return the blue color component", 30, model.getBlue(1));
    assertEquals("Failed to return the alpha color component", 4, model.getAlpha(0));
    assertEquals("Failed to return the alpha color component", 40, model.getAlpha(1));
  }
  public void testCreateGrayscale() {
    // create a 8-bit grayscale ImageTypeSpecifier
    ImageTypeSpecifier type = ImageTypeSpecifier.createGrayscale(8, DataBuffer.TYPE_BYTE, true);

    ColorModel model = type.getColorModel();
    assertEquals(
        "Failed to return the colorspace type",
        ColorSpace.TYPE_GRAY,
        model.getColorSpace().getType());
    assertEquals("Failed to return the transparency", Transparency.OPAQUE, model.getTransparency());
    assertEquals(
        "Failed to return the transfer type", DataBuffer.TYPE_BYTE, model.getTransferType());
    assertEquals("Failed to return the pixel size", 8, model.getPixelSize());

    // create a 16-bit grayscale AlphaPremultiplied ImageTypeSpecifier
    type = ImageTypeSpecifier.createGrayscale(16, DataBuffer.TYPE_USHORT, true, false);

    model = type.getColorModel();
    assertEquals(
        "Failed to return the colorspace type",
        ColorSpace.TYPE_GRAY,
        model.getColorSpace().getType());
    assertEquals(
        "Failed to return the transparency", Transparency.TRANSLUCENT, model.getTransparency());
    assertEquals(
        "Failed to return the transfer type", DataBuffer.TYPE_USHORT, model.getTransferType());
    assertEquals("Failed to return the pixel size", 32, model.getPixelSize());
  }
  public void testCreateInterleaved() {
    ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    int[] bandOffsets = new int[] {1, 2, 3};
    int dataType = DataBuffer.TYPE_BYTE;

    ImageTypeSpecifier type =
        ImageTypeSpecifier.createInterleaved(colorSpace, bandOffsets, dataType, false, false);
    ColorModel colorModel = type.getColorModel();
    PixelInterleavedSampleModel sampleModel = (PixelInterleavedSampleModel) type.getSampleModel();

    // validate the colorModel
    assertEquals(
        "Failed to create with the correct colorspace type",
        ColorSpace.TYPE_RGB,
        colorModel.getColorSpace().getType());
    assertEquals(
        "Failed to create with the correct transparency",
        Transparency.OPAQUE,
        colorModel.getTransparency());
    assertEquals(
        "Failed to create with the correct transfer type",
        DataBuffer.TYPE_BYTE,
        colorModel.getTransferType());

    // validate the sampleModel
    assertTrue(
        "The sampleModel and colorModel are not compatible",
        colorModel.isCompatibleSampleModel(sampleModel));
    assertArrayEquals(
        "Failed to create with the correct bandOffsets", bandOffsets, sampleModel.getBandOffsets());
    assertEquals("Failed to create with the correct pixel stride", 3, sampleModel.getPixelStride());
  }
  public void testGetBufferedImageType() {
    ImageTypeSpecifier typeSpecifier =
        ImageTypeSpecifier.createGrayscale(8, DataBuffer.TYPE_BYTE, true);

    assertEquals(
        "Failed to return the correct type",
        BufferedImage.TYPE_BYTE_GRAY,
        typeSpecifier.getBufferedImageType());
  }
  public void testCreateFromBufferedImageType() {
    for (int i = BufferedImage.TYPE_INT_RGB; i <= BufferedImage.TYPE_BYTE_INDEXED; i++) {
      BufferedImage bi = new BufferedImage(1, 1, i);
      ColorModel expected = bi.getColorModel();

      ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(i);
      ColorModel actual = typeSpecifier.getColorModel();
      String msg =
          "Failed to create correct ImageTypeSpecifier, bufferedImageType = " + Integer.toString(i);
      assertEquals(msg, expected.getClass(), actual.getClass());
      assertEquals(msg, expected.getColorSpace(), actual.getColorSpace());
      assertEquals(msg, expected.getTransferType(), actual.getTransferType());
      assertEquals(msg, expected.getTransparency(), actual.getTransparency());
    }
  }
  public void testCreateBufferedImage() {
    ImageTypeSpecifier typeSpecifier =
        ImageTypeSpecifier.createGrayscale(8, DataBuffer.TYPE_BYTE, true);

    int width = 10;
    int height = 10;
    BufferedImage image = typeSpecifier.createBufferedImage(width, height);
    assertEquals(
        "Failed to create with the correct ColorModel",
        typeSpecifier.getColorModel(),
        image.getColorModel());
    assertEquals(
        "Failed to create with the correct SampleModel",
        typeSpecifier.getSampleModel().getClass(),
        image.getSampleModel().getClass());
    assertEquals("Failed to create with the correct width", width, image.getWidth());
    assertEquals("Failed to create with the correct height", height, image.getHeight());
  }
Exemplo n.º 8
0
  public static List<File> createTiffFiles(List<IIOImage> imageList, int index, int dpiX, int dpiY)
      throws IOException {
    List<File> tiffFiles = new ArrayList<File>();

    // Set up the writeParam
    TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US);
    tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED);

    // Get tif writer and set output to file
    Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(TIFF_FORMAT);
    ImageWriter writer = writers.next();

    if (writer == null) {
      throw new RuntimeException(
          "Need to install JAI Image I/O package.\nhttps://jai-imageio.dev.java.net");
    }

    // Get the stream metadata
    IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(tiffWriteParam);

    // all if index == -1; otherwise, only index-th
    for (IIOImage oimage : (index == -1 ? imageList : imageList.subList(index, index + 1))) {
      if (dpiX != 0 && dpiY != 0) {
        // Get the default image metadata.
        ImageTypeSpecifier imageType =
            ImageTypeSpecifier.createFromRenderedImage(oimage.getRenderedImage());
        IIOMetadata imageMetadata = writer.getDefaultImageMetadata(imageType, null);
        imageMetadata = setDPIViaAPI(imageMetadata, dpiX, dpiY);
        oimage.setMetadata(imageMetadata);
      }

      File tiffFile = File.createTempFile(OUTPUT_FILE_NAME, TIFF_EXT);
      ImageOutputStream ios = ImageIO.createImageOutputStream(tiffFile);
      writer.setOutput(ios);
      writer.write(streamMetadata, oimage, tiffWriteParam);
      ios.close();
      tiffFiles.add(tiffFile);
    }
    writer.dispose();

    return tiffFiles;
  }