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