1*ef39d40dSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*ef39d40dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*ef39d40dSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*ef39d40dSAndrew Rist * distributed with this work for additional information 6*ef39d40dSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*ef39d40dSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*ef39d40dSAndrew Rist * "License"); you may not use this file except in compliance 9*ef39d40dSAndrew Rist * with the License. You may obtain a copy of the License at 10*ef39d40dSAndrew Rist * 11*ef39d40dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*ef39d40dSAndrew Rist * 13*ef39d40dSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*ef39d40dSAndrew Rist * software distributed under the License is distributed on an 15*ef39d40dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*ef39d40dSAndrew Rist * KIND, either express or implied. See the License for the 17*ef39d40dSAndrew Rist * specific language governing permissions and limitations 18*ef39d40dSAndrew Rist * under the License. 19*ef39d40dSAndrew Rist * 20*ef39d40dSAndrew Rist *************************************************************/ 21*ef39d40dSAndrew Rist 22*ef39d40dSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package graphical; 25cdf0e10cSrcweir 26cdf0e10cSrcweir 27cdf0e10cSrcweir // ----------------------------------------------------------------------------- 28cdf0e10cSrcweir abstract class CountPixel 29cdf0e10cSrcweir { 30cdf0e10cSrcweir protected int m_nCount = 0; getCount()31cdf0e10cSrcweir public int getCount() {return m_nCount;} count(int _nRGB)32cdf0e10cSrcweir public abstract void count(int _nRGB); 33cdf0e10cSrcweir } 34cdf0e10cSrcweir 35cdf0e10cSrcweir // ----------------------------------------------------------------------------- 36cdf0e10cSrcweir class CountNotWhite extends CountPixel 37cdf0e10cSrcweir { CountNotWhite()38cdf0e10cSrcweir public CountNotWhite() 39cdf0e10cSrcweir { 40cdf0e10cSrcweir // System.out.println("CountWhite()"); 41cdf0e10cSrcweir } 42cdf0e10cSrcweir countold(final int pixel)43cdf0e10cSrcweir public void countold(final int pixel) 44cdf0e10cSrcweir { 45cdf0e10cSrcweir // final int alpha = (pixel >> 24) & 0xff; 46cdf0e10cSrcweir final int red = (pixel >> 16) & 0xff; 47cdf0e10cSrcweir final int green = (pixel >> 8) & 0xff; 48cdf0e10cSrcweir final int blue = (pixel ) & 0xff; 49cdf0e10cSrcweir 50cdf0e10cSrcweir // System.out.println(String.valueOf(red) + ":" + String.valueOf(green) + ":" + String.valueOf(blue)); 51cdf0e10cSrcweir if (red == 0xff && green == 0xff && blue == 0xff) 52cdf0e10cSrcweir { 53cdf0e10cSrcweir return; 54cdf0e10cSrcweir } 55cdf0e10cSrcweir ++m_nCount; 56cdf0e10cSrcweir } count(final int pixel)57cdf0e10cSrcweir public void count(final int pixel) 58cdf0e10cSrcweir { 59cdf0e10cSrcweir // final int alpha = (pixel >> 24) & 0xff; 60cdf0e10cSrcweir final int blue = (pixel ) & 0xff; 61cdf0e10cSrcweir if (blue != 0xff) 62cdf0e10cSrcweir { 63cdf0e10cSrcweir ++m_nCount; 64cdf0e10cSrcweir return; 65cdf0e10cSrcweir } 66cdf0e10cSrcweir final int green = (pixel >> 8) & 0xff; 67cdf0e10cSrcweir if (green != 0xff) 68cdf0e10cSrcweir { 69cdf0e10cSrcweir ++m_nCount; 70cdf0e10cSrcweir return; 71cdf0e10cSrcweir } 72cdf0e10cSrcweir final int red = (pixel >> 16) & 0xff; 73cdf0e10cSrcweir if (red != 0xff) 74cdf0e10cSrcweir { 75cdf0e10cSrcweir ++m_nCount; 76cdf0e10cSrcweir return; 77cdf0e10cSrcweir } 78cdf0e10cSrcweir } 79cdf0e10cSrcweir } 80cdf0e10cSrcweir 81cdf0e10cSrcweir // ----------------------------------------------------------------------------- 82cdf0e10cSrcweir class CountNotBlack extends CountPixel 83cdf0e10cSrcweir { CountNotBlack()84cdf0e10cSrcweir public CountNotBlack() 85cdf0e10cSrcweir { 86cdf0e10cSrcweir // System.out.println("CountBlack()"); 87cdf0e10cSrcweir } 88cdf0e10cSrcweir countold(final int pixel)89cdf0e10cSrcweir public void countold(final int pixel) 90cdf0e10cSrcweir { 91cdf0e10cSrcweir // final int alpha = (pixel >> 24) & 0xff; 92cdf0e10cSrcweir final int red = (pixel >> 16) & 0xff; 93cdf0e10cSrcweir final int green = (pixel >> 8) & 0xff; 94cdf0e10cSrcweir final int blue = (pixel ) & 0xff; 95cdf0e10cSrcweir 96cdf0e10cSrcweir if (red == 0x00 && green == 0x00 && blue == 0x00) 97cdf0e10cSrcweir { 98cdf0e10cSrcweir return; 99cdf0e10cSrcweir } 100cdf0e10cSrcweir ++m_nCount; 101cdf0e10cSrcweir } count(final int pixel)102cdf0e10cSrcweir public void count(final int pixel) 103cdf0e10cSrcweir { 104cdf0e10cSrcweir // final int alpha = (pixel >> 24) & 0xff; 105cdf0e10cSrcweir final int blue = (pixel ) & 0xff; 106cdf0e10cSrcweir if (blue != 0x00) 107cdf0e10cSrcweir { 108cdf0e10cSrcweir ++m_nCount; 109cdf0e10cSrcweir return; 110cdf0e10cSrcweir } 111cdf0e10cSrcweir final int green = (pixel >> 8) & 0xff; 112cdf0e10cSrcweir if (green != 0x00) 113cdf0e10cSrcweir { 114cdf0e10cSrcweir ++m_nCount; 115cdf0e10cSrcweir return; 116cdf0e10cSrcweir } 117cdf0e10cSrcweir final int red = (pixel >> 16) & 0xff; 118cdf0e10cSrcweir if (red != 0x00) 119cdf0e10cSrcweir { 120cdf0e10cSrcweir ++m_nCount; 121cdf0e10cSrcweir return; 122cdf0e10cSrcweir } 123cdf0e10cSrcweir } 124cdf0e10cSrcweir } 125cdf0e10cSrcweir 126cdf0e10cSrcweir // ----------------------------------------------------------------------------- 127cdf0e10cSrcweir class graphics_stuff 128cdf0e10cSrcweir { 129cdf0e10cSrcweir // public int stuff() 130cdf0e10cSrcweir // { 131cdf0e10cSrcweir //// (1) decoding 132cdf0e10cSrcweir // int rgba = 0; // ...; // comes from PixelGrabber, BufferedImage.getRGB etc. 133cdf0e10cSrcweir // int red = (rgba >> 16) & 0xff; 134cdf0e10cSrcweir // int green = (rgba >> 8) & 0xff; 135cdf0e10cSrcweir // int blue = rgba & 0xff; 136cdf0e10cSrcweir // int alpha = (rgba >> 24) & 0xff; 137cdf0e10cSrcweir //// (2) now modify red, green, blue and alpha as you like; 138cdf0e10cSrcweir //// make sure that each of the four values stays in the 139cdf0e10cSrcweir //// interval 0 to 255 140cdf0e10cSrcweir //// ... 141cdf0e10cSrcweir //// (3) and encode back to an int, e.g. to give it to MemoryImageSource or 142cdf0e10cSrcweir //// BufferedImage.setRGB 143cdf0e10cSrcweir // rgba = (alpha << 24) | (red << 16) | (green << 8) | blue; 144cdf0e10cSrcweir // return 0; 145cdf0e10cSrcweir // } 146cdf0e10cSrcweir 147cdf0e10cSrcweir // public static void handlesinglepixel(int x, int y, int pixel) 148cdf0e10cSrcweir // { 149cdf0e10cSrcweir // int alpha = (pixel >> 24) & 0xff; 150cdf0e10cSrcweir // int red = (pixel >> 16) & 0xff; 151cdf0e10cSrcweir // int green = (pixel >> 8) & 0xff; 152cdf0e10cSrcweir // int blue = (pixel ) & 0xff; 153cdf0e10cSrcweir // // Deal with the pixel as necessary... 154cdf0e10cSrcweir // } 155cdf0e10cSrcweir countPixel(ImageHelper img, int _x, int _y, int _w, int _h, CountPixel _aPixelCounter)156cdf0e10cSrcweir public static void countPixel(ImageHelper img, int _x, int _y, int _w, int _h, CountPixel _aPixelCounter) 157cdf0e10cSrcweir { 158cdf0e10cSrcweir for (int y = 0; y < _h; y++) { 159cdf0e10cSrcweir for (int x = 0; x < _w; x++) { 160cdf0e10cSrcweir // handlesinglepixel(x+i, y+j, pixels[j * w + i]); 161cdf0e10cSrcweir _aPixelCounter.count(img.getPixel(x,y)); 162cdf0e10cSrcweir } 163cdf0e10cSrcweir } 164cdf0e10cSrcweir } countNotWhitePixel(ImageHelper _aImage)165cdf0e10cSrcweir public static int countNotWhitePixel(ImageHelper _aImage) 166cdf0e10cSrcweir { 167cdf0e10cSrcweir final int w = _aImage.getWidth(); 168cdf0e10cSrcweir final int h = _aImage.getHeight(); 169cdf0e10cSrcweir 170cdf0e10cSrcweir CountPixel aCountNotWhite = new CountNotWhite(); 171cdf0e10cSrcweir countPixel(_aImage, 0, 0, w, h, aCountNotWhite); 172cdf0e10cSrcweir return aCountNotWhite.getCount(); 173cdf0e10cSrcweir } 174cdf0e10cSrcweir countNotBlackPixel(ImageHelper _aImage)175cdf0e10cSrcweir public static int countNotBlackPixel(ImageHelper _aImage) 176cdf0e10cSrcweir { 177cdf0e10cSrcweir final int w = _aImage.getWidth(); 178cdf0e10cSrcweir final int h = _aImage.getHeight(); 179cdf0e10cSrcweir 180cdf0e10cSrcweir CountPixel aCountNotBlack = new CountNotBlack(); 181cdf0e10cSrcweir countPixel(_aImage, 0, 0, w, h, aCountNotBlack); 182cdf0e10cSrcweir return aCountNotBlack.getCount(); 183cdf0e10cSrcweir } 184cdf0e10cSrcweir } 185cdf0e10cSrcweir 186cdf0e10cSrcweir // ----------------------------------------------------------------------------- 187cdf0e10cSrcweir 188cdf0e10cSrcweir public class PixelCounter { 189cdf0e10cSrcweir // private Image m_aImage; 190cdf0e10cSrcweir // ImageHelper m_aImage; 191cdf0e10cSrcweir 192cdf0e10cSrcweir countNotWhitePixel(String _sFile)193cdf0e10cSrcweir public int countNotWhitePixel(String _sFile) 194cdf0e10cSrcweir throws java.io.IOException 195cdf0e10cSrcweir { 196cdf0e10cSrcweir ImageHelper aImage = ImageHelper.createImageHelper(_sFile); 197cdf0e10cSrcweir final int nw = graphics_stuff.countNotWhitePixel(aImage); 198cdf0e10cSrcweir return nw; 199cdf0e10cSrcweir } 200cdf0e10cSrcweir countNotBlackPixel(String _sFile)201cdf0e10cSrcweir public int countNotBlackPixel(String _sFile) 202cdf0e10cSrcweir throws java.io.IOException 203cdf0e10cSrcweir { 204cdf0e10cSrcweir ImageHelper aImage = ImageHelper.createImageHelper(_sFile); 205cdf0e10cSrcweir final int nw = graphics_stuff.countNotBlackPixel(aImage); 206cdf0e10cSrcweir return nw; 207cdf0e10cSrcweir } 208cdf0e10cSrcweir countNotWhitePixelsFromImage(String _sFile)209cdf0e10cSrcweir public static int countNotWhitePixelsFromImage(String _sFile) 210cdf0e10cSrcweir throws java.io.IOException 211cdf0e10cSrcweir { 212cdf0e10cSrcweir PixelCounter a = new PixelCounter(); 213cdf0e10cSrcweir return a.countNotWhitePixel(_sFile); 214cdf0e10cSrcweir } 215cdf0e10cSrcweir countNotBlackPixelsFromImage(String _sFile)216cdf0e10cSrcweir public static int countNotBlackPixelsFromImage(String _sFile) 217cdf0e10cSrcweir throws java.io.IOException 218cdf0e10cSrcweir { 219cdf0e10cSrcweir PixelCounter a = new PixelCounter(); 220cdf0e10cSrcweir return a.countNotBlackPixel(_sFile); 221cdf0e10cSrcweir } 222cdf0e10cSrcweir 223cdf0e10cSrcweir // ----------------------------------------------------------------------------- 224cdf0e10cSrcweir 225cdf0e10cSrcweir // public static void main(String[] args) { 226cdf0e10cSrcweir // 227cdf0e10cSrcweir // String a = helper.StringHelper.createValueString(10, 4); 228cdf0e10cSrcweir // int dummy = 1; 229cdf0e10cSrcweir ///* 230cdf0e10cSrcweir // BorderRemover a = new BorderRemover(); 231cdf0e10cSrcweir // try 232cdf0e10cSrcweir // { 233cdf0e10cSrcweir // a.createNewImageWithoutBorder(args[0], args[1]); 234cdf0e10cSrcweir // } 235cdf0e10cSrcweir // catch(java.io.IOException e) 236cdf0e10cSrcweir // { 237cdf0e10cSrcweir // System.out.println("Exception caught."); 238cdf0e10cSrcweir // } 239cdf0e10cSrcweir // */ 240cdf0e10cSrcweir // } 241cdf0e10cSrcweir } 242cdf0e10cSrcweir 243cdf0e10cSrcweir 244