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 convwatch; 25cdf0e10cSrcweir 26cdf0e10cSrcweir // import java.awt.Image; 27cdf0e10cSrcweir import convwatch.ImageHelper; 28cdf0e10cSrcweir 29cdf0e10cSrcweir // ----------------------------------------------------------------------------- 30cdf0e10cSrcweir abstract class CountPixel 31cdf0e10cSrcweir { 32cdf0e10cSrcweir int m_nCount = 0; getCount()33cdf0e10cSrcweir public int getCount() {return m_nCount;} count(int _nRGB)34cdf0e10cSrcweir public abstract void count(int _nRGB); 35cdf0e10cSrcweir } 36cdf0e10cSrcweir 37cdf0e10cSrcweir // ----------------------------------------------------------------------------- 38cdf0e10cSrcweir class CountNotWhite extends CountPixel 39cdf0e10cSrcweir { CountNotWhite()40cdf0e10cSrcweir public CountNotWhite() 41cdf0e10cSrcweir { 42cdf0e10cSrcweir // System.out.println("CountWhite()"); 43cdf0e10cSrcweir } 44cdf0e10cSrcweir count(int pixel)45cdf0e10cSrcweir public void count(int pixel) 46cdf0e10cSrcweir { 47cdf0e10cSrcweir int alpha = (pixel >> 24) & 0xff; 48cdf0e10cSrcweir int red = (pixel >> 16) & 0xff; 49cdf0e10cSrcweir int green = (pixel >> 8) & 0xff; 50cdf0e10cSrcweir int blue = (pixel ) & 0xff; 51cdf0e10cSrcweir 52cdf0e10cSrcweir // System.out.println(String.valueOf(red) + ":" + String.valueOf(green) + ":" + String.valueOf(blue)); 53cdf0e10cSrcweir if (red == 0xff && green == 0xff && blue == 0xff) 54cdf0e10cSrcweir { 55cdf0e10cSrcweir return; 56cdf0e10cSrcweir } 57cdf0e10cSrcweir m_nCount++; 58cdf0e10cSrcweir } 59cdf0e10cSrcweir } 60cdf0e10cSrcweir 61cdf0e10cSrcweir // ----------------------------------------------------------------------------- 62cdf0e10cSrcweir class CountNotBlack extends CountPixel 63cdf0e10cSrcweir { CountNotBlack()64cdf0e10cSrcweir public CountNotBlack() 65cdf0e10cSrcweir { 66cdf0e10cSrcweir // System.out.println("CountBlack()"); 67cdf0e10cSrcweir } 68cdf0e10cSrcweir count(int pixel)69cdf0e10cSrcweir public void count(int pixel) 70cdf0e10cSrcweir { 71cdf0e10cSrcweir int alpha = (pixel >> 24) & 0xff; 72cdf0e10cSrcweir int red = (pixel >> 16) & 0xff; 73cdf0e10cSrcweir int green = (pixel >> 8) & 0xff; 74cdf0e10cSrcweir int blue = (pixel ) & 0xff; 75cdf0e10cSrcweir 76cdf0e10cSrcweir if (red == 0x00 && green == 0x00 && blue == 0x00) 77cdf0e10cSrcweir { 78cdf0e10cSrcweir return; 79cdf0e10cSrcweir } 80cdf0e10cSrcweir m_nCount++; 81cdf0e10cSrcweir } 82cdf0e10cSrcweir } 83cdf0e10cSrcweir 84cdf0e10cSrcweir // ----------------------------------------------------------------------------- 85cdf0e10cSrcweir class graphics_stuff 86cdf0e10cSrcweir { stuff()87cdf0e10cSrcweir public int stuff() 88cdf0e10cSrcweir { 89cdf0e10cSrcweir // (1) decoding 90cdf0e10cSrcweir int rgba = 0; // ...; // comes from PixelGrabber, BufferedImage.getRGB etc. 91cdf0e10cSrcweir int red = (rgba >> 16) & 0xff; 92cdf0e10cSrcweir int green = (rgba >> 8) & 0xff; 93cdf0e10cSrcweir int blue = rgba & 0xff; 94cdf0e10cSrcweir int alpha = (rgba >> 24) & 0xff; 95cdf0e10cSrcweir // (2) now modify red, green, blue and alpha as you like; 96cdf0e10cSrcweir // make sure that each of the four values stays in the 97cdf0e10cSrcweir // interval 0 to 255 98cdf0e10cSrcweir // ... 99cdf0e10cSrcweir // (3) and encode back to an int, e.g. to give it to MemoryImageSource or 100cdf0e10cSrcweir // BufferedImage.setRGB 101cdf0e10cSrcweir rgba = (alpha << 24) | (red << 16) | (green << 8) | blue; 102cdf0e10cSrcweir return 0; 103cdf0e10cSrcweir } 104cdf0e10cSrcweir handlesinglepixel(int x, int y, int pixel)105cdf0e10cSrcweir public static void handlesinglepixel(int x, int y, int pixel) 106cdf0e10cSrcweir { 107cdf0e10cSrcweir int alpha = (pixel >> 24) & 0xff; 108cdf0e10cSrcweir int red = (pixel >> 16) & 0xff; 109cdf0e10cSrcweir int green = (pixel >> 8) & 0xff; 110cdf0e10cSrcweir int blue = (pixel ) & 0xff; 111cdf0e10cSrcweir // Deal with the pixel as necessary... 112cdf0e10cSrcweir } 113cdf0e10cSrcweir countPixel(ImageHelper img, int _x, int _y, int _w, int _h, CountPixel _aPixelCounter)114cdf0e10cSrcweir public static void countPixel(ImageHelper img, int _x, int _y, int _w, int _h, CountPixel _aPixelCounter) 115cdf0e10cSrcweir { 116cdf0e10cSrcweir for (int y = 0; y < _h; y++) { 117cdf0e10cSrcweir for (int x = 0; x < _w; x++) { 118cdf0e10cSrcweir // handlesinglepixel(x+i, y+j, pixels[j * w + i]); 119cdf0e10cSrcweir _aPixelCounter.count(img.getPixel(x,y)); 120cdf0e10cSrcweir } 121cdf0e10cSrcweir } 122cdf0e10cSrcweir } countNotWhitePixel(ImageHelper _aImage)123cdf0e10cSrcweir public static int countNotWhitePixel(ImageHelper _aImage) 124cdf0e10cSrcweir { 125cdf0e10cSrcweir int w = _aImage.getWidth(); 126cdf0e10cSrcweir int h = _aImage.getHeight(); 127cdf0e10cSrcweir 128cdf0e10cSrcweir CountPixel aCountNotWhite = new CountNotWhite(); 129cdf0e10cSrcweir countPixel(_aImage, 0, 0, w, h, aCountNotWhite); 130cdf0e10cSrcweir return aCountNotWhite.getCount(); 131cdf0e10cSrcweir } 132cdf0e10cSrcweir countNotBlackPixel(ImageHelper _aImage)133cdf0e10cSrcweir public static int countNotBlackPixel(ImageHelper _aImage) 134cdf0e10cSrcweir { 135cdf0e10cSrcweir int w = _aImage.getWidth(); 136cdf0e10cSrcweir int h = _aImage.getHeight(); 137cdf0e10cSrcweir 138cdf0e10cSrcweir CountPixel aCountNotBlack = new CountNotBlack(); 139cdf0e10cSrcweir countPixel(_aImage, 0, 0, w, h, aCountNotBlack); 140cdf0e10cSrcweir return aCountNotBlack.getCount(); 141cdf0e10cSrcweir } 142cdf0e10cSrcweir } 143cdf0e10cSrcweir 144cdf0e10cSrcweir // ----------------------------------------------------------------------------- 145cdf0e10cSrcweir 146cdf0e10cSrcweir public class PixelCounter { 147cdf0e10cSrcweir // private Image m_aImage; 148cdf0e10cSrcweir ImageHelper m_aImage; 149cdf0e10cSrcweir 150cdf0e10cSrcweir countNotWhitePixel(String _sFile)151cdf0e10cSrcweir public int countNotWhitePixel(String _sFile) 152cdf0e10cSrcweir throws java.io.IOException 153cdf0e10cSrcweir { 154cdf0e10cSrcweir m_aImage = ImageHelper.createImageHelper(_sFile); 155cdf0e10cSrcweir int nw = graphics_stuff.countNotWhitePixel(m_aImage); 156cdf0e10cSrcweir return nw; 157cdf0e10cSrcweir } 158cdf0e10cSrcweir countNotBlackPixel(String _sFile)159cdf0e10cSrcweir public int countNotBlackPixel(String _sFile) 160cdf0e10cSrcweir throws java.io.IOException 161cdf0e10cSrcweir { 162cdf0e10cSrcweir m_aImage = ImageHelper.createImageHelper(_sFile); 163cdf0e10cSrcweir int nw = graphics_stuff.countNotBlackPixel(m_aImage); 164cdf0e10cSrcweir return nw; 165cdf0e10cSrcweir } 166cdf0e10cSrcweir countNotWhitePixelsFromImage(String _sFile)167cdf0e10cSrcweir public static int countNotWhitePixelsFromImage(String _sFile) 168cdf0e10cSrcweir throws java.io.IOException 169cdf0e10cSrcweir { 170cdf0e10cSrcweir PixelCounter a = new PixelCounter(); 171cdf0e10cSrcweir return a.countNotWhitePixel(_sFile); 172cdf0e10cSrcweir } 173cdf0e10cSrcweir countNotBlackPixelsFromImage(String _sFile)174cdf0e10cSrcweir public static int countNotBlackPixelsFromImage(String _sFile) 175cdf0e10cSrcweir throws java.io.IOException 176cdf0e10cSrcweir { 177cdf0e10cSrcweir PixelCounter a = new PixelCounter(); 178cdf0e10cSrcweir return a.countNotBlackPixel(_sFile); 179cdf0e10cSrcweir } 180cdf0e10cSrcweir 181cdf0e10cSrcweir // ----------------------------------------------------------------------------- 182cdf0e10cSrcweir 183cdf0e10cSrcweir // public static void main(String[] args) { 184cdf0e10cSrcweir // 185cdf0e10cSrcweir // String a = helper.StringHelper.createValueString(10, 4); 186cdf0e10cSrcweir // int dummy = 1; 187cdf0e10cSrcweir ///* 188cdf0e10cSrcweir // BorderRemover a = new BorderRemover(); 189cdf0e10cSrcweir // try 190cdf0e10cSrcweir // { 191cdf0e10cSrcweir // a.createNewImageWithoutBorder(args[0], args[1]); 192cdf0e10cSrcweir // } 193cdf0e10cSrcweir // catch(java.io.IOException e) 194cdf0e10cSrcweir // { 195cdf0e10cSrcweir // System.out.println("Exception caught."); 196cdf0e10cSrcweir // } 197cdf0e10cSrcweir // */ 198cdf0e10cSrcweir // } 199cdf0e10cSrcweir 200cdf0e10cSrcweir } 201cdf0e10cSrcweir 202cdf0e10cSrcweir 203