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