Java examples for 2D Graphics:BufferedImage Pixel
Indicates whether a zero bit indicates a black/dark pixel for a monochrome image RenderedImage.
/*/*from w w w.ja va2 s . c o m*/ * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ //package com.java2s; import java.awt.image.ColorModel; import java.awt.image.IndexColorModel; import java.awt.image.RenderedImage; public class Main { /** * Indicates whether a zero bit indicates a black/dark pixel for a * monochrome image. * * @param img * the image (must be 1 bit monochrome) * @return true if a zero bit indicates a black/dark pixel, false for a * white/bright pixel */ public static boolean isZeroBlack(final RenderedImage img) { if (!isMonochromeImage(img)) { throw new IllegalArgumentException( "Image is not a monochrome image!"); } final IndexColorModel icm = (IndexColorModel) img.getColorModel(); final int gray0 = convertToGray(icm.getRGB(0)); final int gray1 = convertToGray(icm.getRGB(1)); return gray0 < gray1; } /** * Indicates whether an image is a monochrome (1 bit black and white) image. * * @param img * the image * @return true if it's a monochrome image */ public static boolean isMonochromeImage(final RenderedImage img) { return getColorIndexSize(img) == 2; } /** * Convert an RGB color value to a grayscale from 0 to 100. * * @param r * the red component * @param g * the green component * @param b * the blue component * @return the gray value */ public static int convertToGray(final int r, final int g, final int b) { return (r * 30 + g * 59 + b * 11) / 100; } /** * Convert an RGB color value to a grayscale from 0 to 100. * * @param rgb * the RGB value * @return the gray value */ public static int convertToGray(final int rgb) { final int r = (rgb & 0xFF0000) >> 16; final int g = (rgb & 0xFF00) >> 8; final int b = rgb & 0xFF; return convertToGray(r, g, b); } /** * Returns the size of the color index if the given image has one. * * @param img * the image * @return the size of the color index or 0 if there's no color index */ public static int getColorIndexSize(final RenderedImage img) { final ColorModel cm = img.getColorModel(); if (cm instanceof IndexColorModel) { final IndexColorModel icm = (IndexColorModel) cm; return icm.getMapSize(); } else { return 0; } } }