开发者

How to convert a BufferedImage to a certain colour?

Speicifically I have images that are all solid black on transparent. I want to assign an abritrary colour to the images when they are drawn so that the black areas are changed to the new colour.

I tried using a RGBImageFilter that just returned the colour I want but something's going wrong and nothing is being drawn at all. (ColourFilter extends RGBImageFilter and just returns the set colour in it's filterRGB() method.)

BufferedImage tileImage = _tiles.get( tileID );
ColourFilter cFilt = new ColourFilter();
cFilt.setColour( colour );
FilteredImageSource filteredSrc = new FilteredImageSource( tileImage.getSource(), cFilt );
Image finalImage = Toolkit.getDefaultToolkit().createImage(filteredSrc);
bufferGraph开发者_开发百科ics2D.drawImage(finalImage.....


Look at AlphaComposites, particularly DST_IN:

BufferedImage original = ... // dimensions width x height, black on transparent

// create red image
BufferedImage redVersion = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) redVersion.getGraphics();
g.setColor(Color.red);
g.fillRect(0, 0, width, height);

// paint original with composite
g.setComposite(AlphaComposite.DstIn);
g.drawImage(image, 0, 0, width, height, 0, 0, width, height, null);

// redVersion is now a red version of original


I'm not 100% sure what you're trying to do, but image filters and should be able to do what I think you are trying to do. For e.g.,

import java.awt.*;
import java.awt.image.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

public class ColorSwap {
   public static void main(String[] args) {
      final String mapUrlPath = "http://upload.wikimedia.org/"
               + "wikipedia/commons/c/c4/Maps-for-free_Sierra_Nevada.png";

      try {
         URL mapUrl = new URL(mapUrlPath);
         BufferedImage mapImage = ImageIO.read(mapUrl);
         Image newMapImage = Toolkit.getDefaultToolkit().createImage(
                           new FilteredImageSource(mapImage.getSource(),
                                    new XorFilter()));

         Image grayImage = Toolkit.getDefaultToolkit().createImage(
                  new FilteredImageSource(mapImage.getSource(),
                           new MyGrayFilter()));

         Image grayToColorImage = Toolkit.getDefaultToolkit().createImage(
                  new FilteredImageSource(grayImage.getSource(),
                           new GrayToColorFilter(Color.red)));

         ImageIcon mapIcon = new ImageIcon(mapImage);
         ImageIcon newMapIcon = new ImageIcon(newMapImage);
         ImageIcon newGrayIcon = new ImageIcon(grayImage);

         ImageIcon grayToColorIcon = new ImageIcon(grayToColorImage);

         JPanel imagePanel = new JPanel(new GridLayout(2, 2));
         imagePanel.add(new JLabel(mapIcon));
         imagePanel.add(new JLabel(newMapIcon));
         imagePanel.add(new JLabel(newGrayIcon));
         imagePanel.add(new JLabel(grayToColorIcon));

         JOptionPane.showMessageDialog(null, imagePanel, "Image Color Swap",
                  JOptionPane.PLAIN_MESSAGE);

      } catch (MalformedURLException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

class RedBlueSwapFilter extends RGBImageFilter {
   public int filterRGB(int x, int y, int rgb) {
      return ((rgb & 0xff00ff00) | ((rgb & 0xff0000) >> 16) | ((rgb & 0xff) << 16));
   }
}

class XorFilter extends RGBImageFilter {
   public int filterRGB(int x, int y, int argb) {
      return ((argb & 0xff000000) | (argb ^ 0x00ffffff));
   }
}

class MyGrayFilter extends RGBImageFilter {
   public int filterRGB(int x, int y, int argb) {
      int r = (argb & 0x00ff0000) >> 0x10;
      int g = (argb & 0x0000ff00) >> 0x08;
      int b = (argb & 0x000000ff);
      int ave = (r + g + b) / 3;

      return ((argb & 0xff000000) | (ave << 0x10 | ave << 0x08 | ave));
   }
}

class GrayToColorFilter extends RGBImageFilter {
   private Color c;

   public GrayToColorFilter(Color c) {
      this.c = c;
   }

   public int filterRGB(int x, int y, int argb) {
      return (argb | c.getRGB());
   }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜