コード例 #1
0
  private static BufferedImage vectorGraphic(
      List<Float> data, RectilinearGrid rectGrid, ImageProducer ip) throws IOException {
    int width = ip.getPicWidth();
    int height = ip.getPicHeight();
    IndexColorModel cm = ip.getColorModel();
    int[] rgbs = new int[cm.getMapSize()];
    cm.getRGBs(rgbs);
    BufferedImage im = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED, cm);
    Graphics2D g2d = im.createGraphics();

    BoundingBox bbox = rectGrid.getExtent();

    double lonDiff = bbox.getMaxX() - bbox.getMinX();
    double latDiff = bbox.getMaxY() - bbox.getMinY();

    // This ensures that the highest value of longitude (corresponding
    // with nLon - 1) is getLonMax()
    double lonStride = lonDiff / (width - 1);
    double latStride = latDiff / (height - 1);

    // Create the transform.  We scale by the inverse of the stride length
    AffineTransform transform = new AffineTransform();
    transform.scale(1.0 / lonStride, 1.0 / latStride);
    // Then we translate by the minimum coordinate values
    transform.translate(-bbox.getMinX(), -bbox.getMinY());

    g2d.setTransform(transform);

    int xSize = rectGrid.getAxis(0).getSize();
    int ySize = rectGrid.getAxis(1).getSize();
    // Populate the BufferedImages using the information from the curvilinear grid
    // Iterate over all the cells in the grid, painting the i and j indices of the
    // cell onto the BufferedImage
    for (Cell cell : getCells(rectGrid)) {
      // Need to flip the y-axis
      int j = ySize - 1 - cell.j;
      int index = cell.i + xSize * j;
      Float val = data.get(index);
      int colourIndex = ip.getColourIndex(val);

      // Get a Path representing the boundary of the cell
      Path2D path = cell.path;
      g2d.setPaint(new Color(cm.getRGB(colourIndex)));
      g2d.fill(path);

      // We paint a second copy of the cell, shifted by 360 degrees, to handle
      // the anti-meridian
      // TODO: this bit increases runtime by 30%
      double shiftLon = cell.centre.getLongitude() > 0.0 ? -360.0 : 360.0;
      path.transform(AffineTransform.getTranslateInstance(shiftLon, 0.0));
      g2d.fill(path);
    }

    return im;
  }
コード例 #2
0
  private static ImageInfo createIndexedImageInfo(
      Product product, TIFFRenderedImage baseImage, Band band) {
    final IndexColorModel colorModel = (IndexColorModel) baseImage.getColorModel();
    final IndexCoding indexCoding = new IndexCoding("color_map");
    final int colorCount = colorModel.getMapSize();
    final ColorPaletteDef.Point[] points = new ColorPaletteDef.Point[colorCount];
    for (int j = 0; j < colorCount; j++) {
      final String name = String.format("I%3d", j);
      indexCoding.addIndex(name, j, "");
      points[j] = new ColorPaletteDef.Point(j, new Color(colorModel.getRGB(j)), name);
    }
    product.getIndexCodingGroup().add(indexCoding);
    band.setSampleCoding(indexCoding);

    return new ImageInfo(new ColorPaletteDef(points, points.length));
  }
コード例 #3
0
  /**
   * 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);
  }
コード例 #4
0
ファイル: SupXml.java プロジェクト: akjujoki/BDSup2Sub
  /* (non-Javadoc)
   * @see deadbeef.SupTools.SubtitleStream#decode(int)
   */
  @Override
  public void decode(int index) throws CoreException {
    try {
      File f = new File(subPictures.get(index).getFileName());
      if (!f.exists()) {
        throw new CoreException("file " + subPictures.get(index).getFileName() + " not found.");
      }
      BufferedImage img = ImageIO.read(f);
      int w = img.getWidth();
      int h = img.getHeight();

      this.palette = null;

      // first try to read image and palette directly from imported image
      if (img.getType() == BufferedImage.TYPE_BYTE_INDEXED) {
        IndexColorModel icm = (IndexColorModel) img.getColorModel();
        if (icm.getMapSize() < 255 || (icm.hasAlpha() && icm.getAlpha(255) == 0)) {
          // create palette
          palette = new Palette(256);
          for (int i = 0; i < icm.getMapSize(); i++) {
            int alpha = (icm.getRGB(i) >> 24) & 0xff;
            if (alpha >= configuration.getAlphaCrop()) {
              palette.setARGB(i, icm.getRGB(i));
            } else {
              palette.setARGB(i, 0);
            }
          }
          // copy pixels
          WritableRaster raster = img.getRaster();
          bitmap =
              new Bitmap(
                  img.getWidth(),
                  img.getHeight(),
                  (byte[]) raster.getDataElements(0, 0, img.getWidth(), img.getHeight(), null));
        }
      }

      // if this failed, assume RGB image and quantize palette
      if (palette == null) {
        // grab int array (ARGB)
        int[] pixels = new int[w * h];
        img.getRGB(0, 0, w, h, pixels, 0, w);
        // quantize image
        QuantizeFilter qf = new QuantizeFilter();
        bitmap = new Bitmap(img.getWidth(), img.getHeight());
        int ct[] = qf.quantize(pixels, bitmap.getInternalBuffer(), w, h, 255, false, false);
        int size = ct.length;
        if (size > 255) {
          logger.warn("Quantizer failed.\n");
          size = 255;
        }
        // create palette
        palette = new Palette(256);
        for (int i = 0; i < size; i++) {
          int alpha = (ct[i] >> 24) & 0xff;
          if (alpha >= configuration.getAlphaCrop()) {
            palette.setARGB(i, ct[i]);
          } else {
            palette.setARGB(i, 0);
          }
        }
      }
      primaryColorIndex =
          bitmap.getPrimaryColorIndex(
              palette.getAlpha(), configuration.getAlphaThreshold(), palette.getY());
      // crop
      BitmapBounds bounds =
          bitmap.getCroppingBounds(palette.getAlpha(), configuration.getAlphaCrop());
      if (bounds.yMin > 0
          || bounds.xMin > 0
          || bounds.xMax < bitmap.getWidth() - 1
          || bounds.yMax < bitmap.getHeight() - 1) {
        w = bounds.xMax - bounds.xMin + 1;
        h = bounds.yMax - bounds.yMin + 1;
        if (w < 2) {
          w = 2;
        }
        if (h < 2) {
          h = 2;
        }
        bitmap = bitmap.crop(bounds.xMin, bounds.yMin, w, h);
        // update picture
        SubPictureXml pic = subPictures.get(index);
        pic.setImageWidth(w);
        pic.setImageHeight(h);
        pic.setOfsX(pic.getOriginalXOffset() + bounds.xMin);
        pic.setOfsY(pic.getOriginalYOffset() + bounds.yMin);
      }
    } catch (IOException e) {
      throw new CoreException(e.getMessage());
    } catch (OutOfMemoryError e) {
      JOptionPane.showMessageDialog(
          null,
          "Out of heap! Use -Xmx256m to increase heap!",
          "Error!",
          JOptionPane.WARNING_MESSAGE);
      throw new CoreException("Out of heap! Use -Xmx256m to increase heap!");
    }
  }
コード例 #5
0
 Color getColor(int index) {
   IndexColorModel cm = (IndexColorModel) imp.getProcessor().getColorModel();
   // IJ.write(""+index+" "+(new Color(cm.getRGB(index))));
   return new Color(cm.getRGB(index));
 }