Java Code Examples for java.awt.image.RGBImageFilter

The following are top voted examples for showing how to use java.awt.image.RGBImageFilter. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: litiengine   File: ImageProcessing.java   Source Code and License 6 votes vote down vote up
/**
 * All pixels that have the specified color are rendered transparent.
 *
 * @param img
 *          the img
 * @param color
 *          the color
 * @return the image
 */
public static Image applyAlphaChannel(final Image img, final Color color) {
  if (color == null || img == null) {
    return img;
  }

  final ImageFilter filter = new RGBImageFilter() {

    // the color we are looking for... Alpha bits are set to opaque
    public final int markerRGB = color.getRGB() | 0xFF000000;

    @Override
    public final int filterRGB(final int x, final int y, final int rgb) {
      if ((rgb | 0xFF000000) == this.markerRGB) {
        // Mark the alpha bits as zero - transparent
        return 0x00FFFFFF & rgb;
      } else {
        // nothing to do
        return rgb;
      }
    }
  };

  final ImageProducer ip = new FilteredImageSource(img.getSource(), filter);
  return Toolkit.getDefaultToolkit().createImage(ip);
}
 
Example 2
Project: brModelo   File: TratadorDeImagens.java   Source Code and License 6 votes vote down vote up
public static Image makeColorTransparent(Image im, final Color color) {
    //(C)
    //Copiado da internet: 13/02/2011 - http://www.rgagnon.com/javadetails/java-0265.html e http://www.coderanch.com/t/331731/GUI/java/Resize-ImageIcon
    //

    ImageFilter filter = new RGBImageFilter() {
        // the color we are looking for... Alpha bits are set to opaque

        public int markerRGB = color.getRGB() | 0xFF000000;

        @Override
        public final int filterRGB(int x, int y, int rgb) {
            if ((rgb | 0xFF000000) == markerRGB) {
                // Mark the alpha bits as zero - transparent
                return 0x00FFFFFF & rgb;
            } else {
                // nothing to do
                return rgb;
            }
        }
    };

    ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
    return Toolkit.getDefaultToolkit().createImage(ip);
}
 
Example 3
Project: OpenRS   File: ImageUtils.java   Source Code and License 6 votes vote down vote up
public static BufferedImage makeColorTransparent(BufferedImage im, final Color color) {
	ImageFilter filter = new RGBImageFilter() {

		public int markerRGB = color.getRGB() | 0xFF000000;

		public final int filterRGB(int x, int y, int rgb) {
			if ((rgb | 0xFF000000) == markerRGB) {
				return 0x00FFFFFF & rgb;
			} else {
				return rgb;
			}
		}
	};

	return imageToBufferedImage(
			Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(im.getSource(), filter)));
}
 
Example 4
Project: SBOLDesigner   File: Images.java   Source Code and License 6 votes vote down vote up
public static Image makeColorTransparent(final BufferedImage im, final Color color) {
	final ImageFilter filter = new RGBImageFilter() {
		// the color we are looking for... Alpha bits are set to opaque
		public int markerRGB = color.getRGB() | 0xFFFFFFFF;

		public final int filterRGB(final int x, final int y, final int rgb) {
			if ((rgb | 0xFF000000) == markerRGB) {
				// Mark the alpha bits as zero - transparent
				return 0x00FFFFFF & rgb;
			} else {
				// nothing to do
				return rgb;
			}
		}
	};

	final ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
	return Toolkit.getDefaultToolkit().createImage(ip);
}
 
Example 5
Project: ComplexDataObject   File: BufferedImageTools.java   Source Code and License 6 votes vote down vote up
/**
 * 
 * @param im
 * @param color
 * @return
 */
public static Image setTransparentColor(BufferedImage im, final Color color) {
	ImageFilter filter = new RGBImageFilter() {

		// the color we are looking for... Alpha bits are set to opaque
		public int markerRGB = color.getRGB() | 0xFF000000;

		public final int filterRGB(int x, int y, int rgb) {
			if ((rgb | 0xFF000000) == markerRGB) {
				// Mark the alpha bits as zero - transparent
				return 0x00FFFFFF & rgb;
			} else {
				// nothing to do
				return rgb;
			}
		}
	};

	ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
	return Toolkit.getDefaultToolkit().createImage(ip);
}
 
Example 6
Project: torgo   File: ImageUtils.java   Source Code and License 6 votes vote down vote up
public static Image makeColorTransparent(BufferedImage im, final Color color) {
    ImageFilter filter = new RGBImageFilter() {

        // the color we are looking for... Alpha bits are set to opaque
        @Override
        public int filterRGB(int x, int y, int rgb) {
            int markerRGB = color.getRGB() | 0xFF000000;

            if ((rgb | 0xFF000000) == markerRGB) {
                // Mark the alpha bits as zero - transparent
                return 0x00FFFFFF & rgb;
            } else {
                // nothing to do
                return rgb;
            }
        }
    };

    ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
    return Toolkit.getDefaultToolkit().createImage(ip);
}
 
Example 7
Project: OS-Cache-Suite   File: ImageUtilities.java   Source Code and License 6 votes vote down vote up
public static BufferedImage makeColorTransparent(BufferedImage im, final Color color) {
    RGBImageFilter filter = new RGBImageFilter() {
        public int markerRGB = color.getRGB() | 0xFF000000;
        public final int filterRGB(int x, int y, int rgb) {
            if ((rgb | 0xFF000000) == markerRGB) {
                return 0x00FFFFFF & rgb;
            } else {
                return rgb;
            }
        }
    };
    ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
    Image image = Toolkit.getDefaultToolkit().createImage(ip);
    BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bufferedImage.createGraphics();
    g2.drawImage(image, 0, 0, null);
    g2.dispose();
    return bufferedImage;
}
 
Example 8
Project: WorldGrower   File: ImageInfoReader.java   Source Code and License 6 votes vote down vote up
private Image transformColorToTransparency(BufferedImage image, Color color)
{
  ImageFilter filter = new RGBImageFilter()
  {
    public final int filterRGB(int x, int y, int rgb)
    {
    	if (rgb == color.getRGB()) {
    		return new Color(0, 0, 0, 0).getRGB();
    	} else {
    		return rgb;
    	}
    }
  };

  ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
  return Toolkit.getDefaultToolkit().createImage(ip);
}
 
Example 9
Project: MeteoInfoLib   File: GlobalUtil.java   Source Code and License 6 votes vote down vote up
/**
 * Make a color of a image transparent
 *
 * @param im The image
 * @param color The color
 * @return Result image
 */
public static Image makeColorTransparent(BufferedImage im, final Color color) {
    ImageFilter filter = new RGBImageFilter() {
        // the color we are looking for... Alpha bits are set to opaque
        public int markerRGB = color.getRGB() | 0xFF000000;

        @Override
        public final int filterRGB(int x, int y, int rgb) {
            if ((rgb | 0xFF000000) == markerRGB) {
                // Mark the alpha bits as zero - transparent
                return 0x00FFFFFF & rgb;
            } else {
                // nothing to do
                return rgb;
            }
        }
    };

    ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
    return Toolkit.getDefaultToolkit().createImage(ip);
}
 
Example 10
Project: mobac   File: OpenSeaMap.java   Source Code and License 6 votes vote down vote up
public static Image makeColorTransparent(Image im, final Color color) {
	ImageFilter filter = new RGBImageFilter() {
		// the color we are looking for... Alpha bits are set to opaque
		public int markerRGB = color.getRGB() | 0xFF000000;

		public final int filterRGB(int x, int y, int rgb) {
			if ((rgb | 0xFF000000) == markerRGB) {
				// Mark the alpha bits as zero - transparent
				return 0x00FFFFFF & rgb;
			} else {
				// nothing to do
				return rgb;
			}
		}
	};

	ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
	return Toolkit.getDefaultToolkit().createImage(ip);
}
 
Example 11
Project: SquaresInteractive   File: Transparency.java   Source Code and License 6 votes vote down vote up
public static Image makeColorTransparent(Image im, final Color color, final int alpha) {

		final ImageFilter filter = new RGBImageFilter() {

			// the color we are looking for... Alpha bits are set to opaque
			public int markerRGB = color.getRGB() | 0xFF000000;

			public final int filterRGB(int x, int y, int rgb) {
				if ( ( rgb | 0xFF000000 ) == markerRGB ) {
					// Mark the alpha bits as zero - transparent
					return (0x00FFFFFF | (alpha << 24)) & rgb;
				}
				else {
					// nothing to do
					return rgb;
				}
			}
		}; 

		ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
		return Toolkit.getDefaultToolkit().createImage(ip);
	}
 
Example 12
Project: momo3-backend   File: GraphicsUtil.java   Source Code and License 6 votes vote down vote up
/**
 *
 * Credits go to http://stackoverflow.com/a/665483 and
 * http://www.logikdev.com/2011/10/05/make-image-backgrounds-transparent-with-tolerance/
 *
 * @param im
 * @param colorToMakeTransparent
 * @param tolerance
 *
 * @return
 */
public static Image makeColorTransparent(final BufferedImage im, final Color colorToMakeTransparent, final int tolerance) {

	final ImageFilter transparencyfilter = new RGBImageFilter() {

		@Override
		public int filterRGB(int x, int y, int rgb) {
			final Color filterColor = new Color(rgb);

			if(colorsAreSimilar(filterColor, colorToMakeTransparent, tolerance)) {
				// Mark the alpha bits as zero - transparent
				return 0x00FFFFFF & rgb;
			} else {
				// Nothing to do
				return rgb;
			}

		}

	};

	final ImageProducer ip = new FilteredImageSource(im.getSource(), transparencyfilter);
	return Toolkit.getDefaultToolkit().createImage(ip);
}
 
Example 13
Project: vectorgraphics2d   File: GraphicsUtilsTest.java   Source Code and License 6 votes vote down vote up
@Test
public void toBufferedImageCanConvertFilteredImage() {
	Image image = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(
		new BufferedImage(
				TEST_IMAGE_WIDTH, TEST_IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB).getSource(),
		new RGBImageFilter() {
			@Override
			public int filterRGB(int x, int y, int rgb) {
				return rgb & 0xff;
			}
		}
	));

	BufferedImage result = GraphicsUtils.toBufferedImage(image);

	assertBufferedImageEquals(image, result);
}
 
Example 14
Project: shanks-wsn-module   File: ImageLoaderHelper.java   Source Code and License 6 votes vote down vote up
/**
 * Make provided image transparent wherever color matches the provided
 * color.
 * 
 * @param im
 *            BufferedImage whose color will be made transparent.
 * @param color
 *            Color in provided image which will be made transparent.
 * @return Image with transparency applied.
 */
public static Image makeColorTransparent(final BufferedImage im, final Color color) {
	final ImageFilter filter = new RGBImageFilter() {
		// the color we are looking for (white)... Alpha bits are set to
		// opaque
		public int markerRGB = color.getRGB() | 0xFFFFFFFF;

		public final int filterRGB(final int x, final int y, final int rgb) {
			if ((rgb | 0xFF000000) == markerRGB) {
				// Mark the alpha bits as zero - transparent
				return 0x00FFFFFF & rgb;
			} else {
				// nothing to do
				return rgb;
			}
		}
	};

	final ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
	return Toolkit.getDefaultToolkit().createImage(ip);
}
 
Example 15
Project: FAO_Application   File: BufferedImageRaster.java   Source Code and License 6 votes vote down vote up
public static Image makeTransparentWhite(BufferedImage im, final Color color){
    ImageFilter filter = new RGBImageFilter() {

           // the color we are looking for... Alpha bits are set to opaque
           public int markerRGB = 0x00FFFFFF;

           public final int filterRGB(int x, int y, int rgb) {
               if ((rgb & 0x00FFFFFF) == markerRGB) {
                   // Mark the alpha bits as zero - transparent
                   return color.getRGB();// & 0xFFFFFFFF;
               } else {
                   // nothing to do
                   return rgb;
               }
           }
       };
       ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
       return Toolkit.getDefaultToolkit().createImage(ip);
}
 
Example 16
Project: FAO_Application   File: BufferedImageRaster.java   Source Code and License 6 votes vote down vote up
public static Image makeColorTransparent(BufferedImage im, final Color color) {
	ImageFilter filter = new RGBImageFilter() {

		// the color we are looking for... Alpha bits are set to opaque
		public int markerRGB = color.getRGB() | 0xFF000000;

		public final int filterRGB(int x, int y, int rgb) {
			if ((rgb | 0xFF000000) == markerRGB) {
				// Mark the alpha bits as zero - transparent
				return 0x00FFFFFF & rgb;
			} else {
				// nothing to do
				return rgb;
			}
		}
	};
	ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
	return Toolkit.getDefaultToolkit().createImage(ip);
}
 
Example 17
Project: hortonmachine   File: ImageUtilities.java   Source Code and License 6 votes vote down vote up
/**
 * Make a color of the image transparent.
 * 
 * @param bufferedImageToProcess the image to extract the color from.
 * @param colorToMakeTransparent the color to make transparent.
 * @return the new image.
 */
public static BufferedImage makeColorTransparent( BufferedImage bufferedImageToProcess, final Color colorToMakeTransparent ) {
    ImageFilter filter = new RGBImageFilter(){
        public int markerRGB = colorToMakeTransparent.getRGB() | 0xFF000000;
        public final int filterRGB( int x, int y, int rgb ) {
            if ((rgb | 0xFF000000) == markerRGB) {
                // Mark the alpha bits as zero - transparent
                return 0x00FFFFFF & rgb;
            } else {
                return rgb;
            }
        }
    };
    ImageProducer ip = new FilteredImageSource(bufferedImageToProcess.getSource(), filter);
    Image image = Toolkit.getDefaultToolkit().createImage(ip);
    BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bufferedImage.createGraphics();
    g2.drawImage(image, 0, 0, null);
    g2.dispose();
    return bufferedImage;
}
 
Example 18
Project: darkedenkit   File: PkSpriteExtractor.java   Source Code and License 6 votes vote down vote up
private BufferedImage filterWhiteToTransparent(Image img)
{		
	ImageFilter filter = new RGBImageFilter() {
		@Override
		public int filterRGB(int x, int y, int rgb)
		{
			return (rgb & 0x00FFFFFF) == 0x00FFFFFF ? 0x00FFFFFF : rgb;
		}
	};
	
	Image transparentImage = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(img.getSource(), filter));

	BufferedImage canvas = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
	Graphics2D g = (Graphics2D)canvas.getGraphics();
	g.drawImage(transparentImage, 0, 0, null);
	g.dispose();

	return canvas;
}
 
Example 19
Project: com.opendoorlogistics   File: CachedGeomImageRenderer.java   Source Code and License 6 votes vote down vote up
private BufferedImage createColouredImage(Image bwImage, final Color renderCol) {
	// create coloured image
	RGBImageFilter rgbFilter = new RGBImageFilter() {

		@Override
		public int filterRGB(int x, int y, int rgb) {
			Color current = new Color(rgb);
			int red = current.getRed();
			if (current.getAlpha() == 0 || red == 0) {
				return 0;
			}
			double factor = red / 255.0;
			int newRed = Colours.to0To255Int(renderCol.getRed() * factor);
			int newBlue = Colours.to0To255Int(renderCol.getBlue() * factor);
			int newGreen = Colours.to0To255Int(renderCol.getGreen() * factor);
			Color newCol = new Color(newRed, newGreen, newBlue, renderCol.getAlpha());
			return newCol.getRGB();
		}
	};
	
	// transform to colour and save in colour
	ImageProducer ip = new FilteredImageSource(bwImage.getSource(), rgbFilter);
	Image coloured = Toolkit.getDefaultToolkit().createImage(ip);
	BufferedImage colourImage = ImageUtils.toBufferedImage(coloured);
	return colourImage;
}
 
Example 20
Project: XPressOnTechnologies   File: ImageUtils.java   Source Code and License 6 votes vote down vote up
public static Image makeColorTransparent(Image im, final Color color) {
	ImageFilter filter = new RGBImageFilter() {
		// the color we are looking for... Alpha bits are set to opaque
		public int markerRGB = color.getRGB() | 0xFF000000;

		public final int filterRGB(int x, int y, int rgb) {
			if ((rgb | 0xFF000000) == markerRGB) {
				// Mark the alpha bits as zero - transparent
				return 0x00FFFFFF & rgb;
			} else {
				// nothing to do
				return rgb;
			}
		}
	};

	ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
	return Toolkit.getDefaultToolkit().createImage(ip);
}
 
Example 21
Project: incubator-netbeans   File: MetalLFCustoms.java   Source Code and License 5 votes vote down vote up
private static Image filterImage( Image image ) {
    if( null == image )
        return image;
    Object obj = UIManager.get("nb.imageicon.filter");
    if( obj instanceof RGBImageFilter && null != image ) {
        RGBImageFilter imageIconFilter = ( RGBImageFilter ) obj;
        image = Toolkit.getDefaultToolkit().createImage( new FilteredImageSource( image.getSource(), imageIconFilter ) );
    }
    return image;
}
 
Example 22
Project: JDigitalSimulator   File: Guitilities.java   Source Code and License 5 votes vote down vote up
public static Image makeColorTransparent(Image image, final Color color) {
	ImageFilter filter = new RGBImageFilter() {
		public int markerRGB = color.getRGB()|0xFF000000;
		@Override
		public final int filterRGB(int x, int y, int rgb) {
			if((rgb|0xFF000000)==markerRGB)
				return 0x00FFFFFF & rgb;
			else return rgb;
		}
	};
	return Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), filter));
}
 
Example 23
Project: openjdk-jdk10   File: HeadlessRGBImageFilter.java   Source Code and License 5 votes vote down vote up
public static void main(String args[]) {
    new RGBImageFilter() {
        public int filterRGB(int x, int y, int rgb) {
            return 0;
        }
    }.clone();
}
 
Example 24
Project: openjdk9   File: HeadlessRGBImageFilter.java   Source Code and License 5 votes vote down vote up
public static void main(String args[]) {
    new RGBImageFilter() {
        public int filterRGB(int x, int y, int rgb) {
            return 0;
        }
    }.clone();
}
 
Example 25
Project: TuxGuitar-1.3.1-fork   File: TGImageImpl.java   Source Code and License 5 votes vote down vote up
public void applyTransparency( final TGColor background ){
	ImageFilter filter = new RGBImageFilter() {
		public int markerRGB = (((TGColorImpl)background).getHandle().getRGB() | 0xFF000000);
		
		public final int filterRGB(int x, int y, int rgb) {
			if ( ( rgb | 0xFF000000 ) == markerRGB ) {
				return 0x00FFFFFF & rgb;
			}
			return rgb;
		}
	};
	this.handle = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(this.handle.getSource(), filter));
}
 
Example 26
Project: pathvisio   File: ColorByExpression.java   Source Code and License 5 votes vote down vote up
Image doReplaceColor(Image img, final Color oldColor, final Color newColor, final int tol) {
	RGBImageFilter f = new RGBImageFilter() {
		public int filterRGB(int x, int y, int rgb) {
			Color thisColor = new Color(rgb);
			if(compareColor(oldColor, thisColor, tol)) {
				return newColor.getRGB();
			}
			return rgb;
		}
	};
	ImageProducer pr = new FilteredImageSource(img.getSource(), f);
	return Toolkit.getDefaultToolkit().createImage(pr);
}
 
Example 27
Project: intellij-ce-playground   File: ColorBlindnessTest.java   Source Code and License 5 votes vote down vote up
private static void compareFilters(ImageFilter one, ImageFilter two) {
  RGBImageFilter rgb1 = (RGBImageFilter)one;
  RGBImageFilter rgb2 = (RGBImageFilter)two;
  for (int i = 0; i < 0x01000000; i++) {
    assertEquals(rgb1.filterRGB(0, 0, i), rgb2.filterRGB(0, 0, i));
  }
}
 
Example 28
Project: NBModeler   File: SVGImageUtilities.java   Source Code and License 5 votes vote down vote up
/**
 * Loads an icon based on resource path. Similar to
 * {@link #loadImage(String, boolean)}, returns ImageIcon instead of Image.
 *
 * @param resource resource path of the icon (no initial slash)
 * @param localized localized resource should be used
 * @return ImageIcon or null, if the icon cannot be loaded.
 * @since 7.22
 */
public static final ImageIcon loadImageIcon(String resource, boolean localized) {
    Image image = getIcon(resource, localized);
    if (image == null) {
        return null;
    }
    RGBImageFilter imageFilter = getImageIconFilter();
    if (null != imageFilter) {
        image = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), imageFilter));
    }
    return (ImageIcon) image2Icon(image);
}
 
Example 29
Project: AppWoksUtils   File: IconIO.java   Source Code and License 5 votes vote down vote up
public static BufferedImage colorRangeToTransparency(final BufferedImage image, final Color c1, final Color c2) {

        final int r1 = c1.getRed();
        final int g1 = c1.getGreen();
        final int b1 = c1.getBlue();
        final int r2 = c2.getRed();
        final int g2 = c2.getGreen();
        final int b2 = c2.getBlue();
        final ImageFilter filter = new RGBImageFilter() {
            @Override
            public final int filterRGB(final int x, final int y, final int rgb) {

                final int r = (rgb & 0xFF0000) >> 16;
                final int g = (rgb & 0xFF00) >> 8;
                final int b = rgb & 0xFF;
                if (r >= r1 && r <= r2 && g >= g1 && g <= g2 && b >= b1 && b <= b2) {
                    // Set fully transparent but keep color
                    // calculate a alpha value based on the distance between the
                    // range borders and the pixel color
                    final int dist = (Math.abs(r - (r1 + r2) / 2) + Math.abs(g - (g1 + g2) / 2) + Math.abs(b - (b1 + b2) / 2)) * 2;

                    return new Color(r, g, b, Math.min(255, dist)).getRGB();
                }

                return rgb;
            }
        };

        final ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
        final Image img = Toolkit.getDefaultToolkit().createImage(ip);
        return IconIO.toBufferedImage(img);
    }
 
Example 30
Project: Android-to-PC-Presentation   File: UtilPC.java   Source Code and License 5 votes vote down vote up
/** RGB icersinde belirlenmis bir rengi transparan yapar */
public static Image makeColorTransparent(BufferedImage im, final Color color) {
	ImageFilter filter = new RGBImageFilter() {
		/* ARGB formatinda A = 255 ve RGB olarak ekleniyor */
		public final int markerRGB = color.getRGB() | 0xFF000000;
		public final int filterRGB(int x, int y, int rgb) {
			if ((rgb | 0xFF000000) == markerRGB)
				return 0x00FFFFFF & rgb; // eger renk belirlenen renk ise transparan yapar
			else
				return rgb; // degilse aynen birakir
		}
	};
	ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
	return Toolkit.getDefaultToolkit().createImage(ip);
}
 
Example 31
Project: java-image-processing-survival-guide   File: InvertingImageComparator.java   Source Code and License 5 votes vote down vote up
/**
 * Make provided image transparent wherever color matches the provided color.
 *
 * @param im BufferedImage whose color will be made transparent.
 * @param color Color in provided image which will be made transparent.
 * @return Image with transparency applied.
 */
public static BufferedImage createTransparentImage(final BufferedImage im, final Color color) {

    final ImageFilter filter = new RGBImageFilter() {
        // the color we are looking for (white)... Alpha bits are set to opaque
        public int markerRGB = color.getRGB() | 0xFFFFFFFF;

        public final int filterRGB(final int x, final int y, final int rgb) {
            if ((rgb | 0xFF000000) == markerRGB) {
                // Mark the alpha bits as zero - transparent
                return 0x00FFFFFF & rgb;
            } else {
                // nothing to do
                return rgb;
            }
        }
    };

    final ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
    final Image image = Toolkit.getDefaultToolkit().createImage(ip);

    final BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    final Graphics2D g2 = bufferedImage.createGraphics();
    g2.drawImage(image, 0, 0, null);
    g2.dispose();
    return bufferedImage;
}
 
Example 32
Project: com.opendoorlogistics   File: Functions.java   Source Code and License 5 votes vote down vote up
@Override
protected ImageFilter createFilter(Object[] executed) {
	final Color filterCol = Colours.toColour(executed[1]);
	if (filterCol == null) {
		return null;
	}

	final Double lerpValue = Numbers.toDouble(executed[2]);
	if (lerpValue == null) {
		return null;
	}

	ImageFilter filter = new RGBImageFilter() {

		@Override
		public int filterRGB(int x, int y, int rgb) {
			// pass directly if completely transparent
			Color pixelCol = new Color(rgb, true);
			if (pixelCol.getAlpha() == 0) {
				return rgb;
			}

			// lerp between the two according to the lerp value
			Color lerped = Colours.lerp(pixelCol, filterCol, lerpValue);
			return lerped.getRGB();
		}
	};
	return filter;
}
 
Example 33
Project: com.opendoorlogistics   File: NOVLPolyLayerTile.java   Source Code and License 5 votes vote down vote up
/**
 * @param colours
 * @return
 */
private RGBImageFilter createRGBFilter(final TIntObjectHashMap<Color> colours,final boolean isBorderRendering) {
	return new RGBImageFilter() {
		int lastInputRGB;
		int lastOutputRGB;
		
		@Override
		public int filterRGB(int x, int y, int rgb) {
			// remove alpha channel as can't get it to work right.... may give max 16 million objects
			rgb = rgb & 0x00FFFFFF;
			
			if(rgb!=0){
				
				// Do a speedup to stop to many hashtable queries. 
				// Often we will call this method for the same object twice in a row
				if(rgb == lastInputRGB){
					return lastOutputRGB;
				}		

				// Get colour from input map using rgb as id
				Color col = colours.get(rgb);
				if(col!=null){
					
					if(isBorderRendering){
						col = DatastoreRenderer.getDefaultPolygonBorderColour(col);
					}
					
					lastInputRGB = rgb;
					rgb = col.getRGB();
					lastOutputRGB = rgb;
				}
				else{
					// Hide it as not visible
					rgb = 0;
				}
			}
			return rgb;
		}
	};
}
 
Example 34
Project: ivp2java   File: IconButtonUI.java   Source Code and License 5 votes vote down vote up
/**
 * Change the brightness of an image
 */
private Image changeBrightness(Image img, final boolean brighten, final int percent) {
    ImageFilter filter = new RGBImageFilter() {
        public int filterRGB(int x, int y, int rgb) {
            return (rgb & 0xff000000) | (filter(rgb >> 16) << 16) | (filter(rgb >> 8) << 8) | (filter(rgb));
        }
        
        /**
         * Brighens or darkens a single r/g/b value.
         */
        private int filter(int color) {
            color = color & 0xff;
            if (brighten) {
                color = (255 - ((255 - color) * (100 - percent) / 100));
            } else {
                color = (color * (100 - percent) / 100);
            }
            if (color < 0)
                color = 0;
            if (color > 255)
                color = 255;
            return color;
        }
    };
    ImageProducer prod = new FilteredImageSource(img.getSource(), filter);
    return Toolkit.getDefaultToolkit().createImage(prod);
}
 
Example 35
Project: consulo   File: BalloonImpl.java   Source Code and License 5 votes vote down vote up
private Image makeColorTransparent(Image image, Color color) {
  final int markerRGB = color.getRGB() | 0xFF000000;
  ImageFilter filter = new RGBImageFilter() {
    @Override
    public int filterRGB(int x, int y, int rgb) {
      if ((rgb | 0xFF000000) == markerRGB) {
        return 0x00FFFFFF & rgb; // set alpha to 0
      }
      else {
        return rgb;
      }
    }
  };
  return ImageUtil.filter(image, filter);
}
 
Example 36
Project: incubator-netbeans   File: ImageUtilities.java   Source Code and License 4 votes vote down vote up
/**
 * Loads an image based on resource path.
 * Exactly like {@link #loadImage(String)} but may do a localized search.
 * For example, requesting <samp>org/netbeans/modules/foo/resources/foo.gif</samp>
 * might actually find <samp>org/netbeans/modules/foo/resources/foo_ja.gif</samp>
 * or <samp>org/netbeans/modules/foo/resources/foo_mybranding.gif</samp>.
 * 
 * <p>Caching of loaded images can be used internally to improve performance.
 * <p> Since version 8.12 the returned image object responds to call
 * <code>image.getProperty("url", null)</code> by returning the internal
 * {@link URL} of the found and loaded <code>resource</code>.
 * 
 * <p>If the current look and feel is 'dark' (<code>UIManager.getBoolean("nb.dark.theme")</code>)
 * then the method first attempts to load image <i>&lt;original file name&gt;<b>_dark</b>.&lt;original extension&gt;</i>.
 * If such file doesn't exist the default one is loaded instead.
 * </p>
 * 
 * @param resource resource path of the image (no initial slash)
 * @param localized true for localized search
 * @return icon's Image or null if the icon cannot be loaded
 */
public static final Image loadImage(String resource, boolean localized) {
    Image image = null;
    if( isDarkLaF() ) {
        image = getIcon(addDarkSuffix(resource), localized);
        // found an image with _dark-suffix, so there no need to apply an
        // image filter to make it look nice using dark themes
    }
    if (null == image) {
        image = getIcon(resource, localized);
        // only non _dark images need filtering
        RGBImageFilter imageFilter = getImageIconFilter();
        if (null != image && null != imageFilter) {
            image = Toolkit.getDefaultToolkit()
                    .createImage(new FilteredImageSource(image.getSource(), imageFilter));
        }
    }
    return image;
}
 
Example 37
Project: SweetHome3D   File: SwingTools.java   Source Code and License 4 votes vote down vote up
/**
 * Returns the image matching a given pattern.
 */
public static BufferedImage getPatternImage(TextureImage pattern, Color backgroundColor, Color foregroundColor)
{
	if (patternImages == null)
	{
		patternImages = new HashMap<TextureImage, BufferedImage>();
	}
	BufferedImage image = new BufferedImage((int) pattern.getWidth(), (int) pattern.getHeight(),
			BufferedImage.TYPE_INT_RGB);
	Graphics2D imageGraphics = (Graphics2D) image.getGraphics();
	imageGraphics.setColor(backgroundColor);
	imageGraphics.fillRect(0, 0, image.getWidth(), image.getHeight());
	// Get pattern image from cache
	BufferedImage patternImage = patternImages.get(pattern);
	if (patternImage == null)
	{
		try
		{
			InputStream imageInput = pattern.getImage().openStream();
			patternImage = ImageIO.read(imageInput);
			imageInput.close();
			patternImages.put(pattern, patternImage);
		}
		catch (IOException ex)
		{
			throw new IllegalArgumentException("Can't read pattern image " + pattern.getName());
		}
	}
	// Draw the pattern image with foreground color
	final int foregroundColorRgb = foregroundColor.getRGB() & 0xFFFFFF;
	imageGraphics.drawImage(Toolkit.getDefaultToolkit()
			.createImage(new FilteredImageSource(patternImage.getSource(), new RGBImageFilter()
			{
				{
					this.canFilterIndexColorModel = true;
				}
				
				@Override
				public int filterRGB(int x, int y, int argb)
				{
					// Always use foreground color and alpha
					return (argb & 0xFF000000) | foregroundColorRgb;
				}
			})), 0, 0, null);
	imageGraphics.dispose();
	return image;
}
 
Example 38
Project: intellij-ce-playground   File: ColorBlindnessTest.java   Source Code and License 4 votes vote down vote up
private static void testZeroFilter(ImageFilter filter) {
  RGBImageFilter rgb = (RGBImageFilter)filter;
  for (int i = 0; i < 0x01000000; i++) {
    assertEquals(i, rgb.filterRGB(0, 0, i));
  }
}
 
Example 39
Project: terminal-angel-disease   File: TileSet.java   Source Code and License 4 votes vote down vote up
private Surface trans(Surface origin, RGBImageFilter filter) {
    Image image = ImageTool.createFiltered(origin.image(), filter);
    return new Surface(image, true);
}
 
Example 40
Project: com.opendoorlogistics   File: NOVLPolyLayerTile.java   Source Code and License 4 votes vote down vote up
private static BufferedImage applyColourFiltering(Image uncompressed, RGBImageFilter rgbFilter) {
	ImageProducer ip = new FilteredImageSource(uncompressed.getSource(), rgbFilter);
	Image coloured = Toolkit.getDefaultToolkit().createImage(ip);
	return ImageUtils.toBufferedImage(coloured);
}