1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package graphical;
29 
30 
31 // -----------------------------------------------------------------------------
32 abstract class CountPixel
33 {
34     protected int m_nCount = 0;
35     public int getCount() {return m_nCount;}
36     public abstract void count(int _nRGB);
37 }
38 
39 // -----------------------------------------------------------------------------
40 class CountNotWhite extends CountPixel
41 {
42     public CountNotWhite()
43         {
44             // System.out.println("CountWhite()");
45         }
46 
47     public void countold(final int pixel)
48         {
49             // final int alpha = (pixel >> 24) & 0xff;
50             final int red   = (pixel >> 16) & 0xff;
51             final int green = (pixel >>  8) & 0xff;
52             final int blue  = (pixel      ) & 0xff;
53 
54             // System.out.println(String.valueOf(red) + ":" + String.valueOf(green) + ":" + String.valueOf(blue));
55             if (red == 0xff && green == 0xff && blue == 0xff)
56             {
57                 return;
58             }
59             ++m_nCount;
60         }
61     public void count(final int pixel)
62         {
63             // final int alpha = (pixel >> 24) & 0xff;
64             final int blue  = (pixel      ) & 0xff;
65             if (blue != 0xff)
66             {
67                 ++m_nCount;
68                 return;
69             }
70             final int green = (pixel >>  8) & 0xff;
71             if (green != 0xff)
72             {
73                 ++m_nCount;
74                 return;
75             }
76             final int red   = (pixel >> 16) & 0xff;
77             if (red != 0xff)
78             {
79                 ++m_nCount;
80                 return;
81             }
82         }
83 }
84 
85 // -----------------------------------------------------------------------------
86 class CountNotBlack extends CountPixel
87 {
88     public CountNotBlack()
89         {
90             // System.out.println("CountBlack()");
91         }
92 
93     public void countold(final int pixel)
94         {
95             // final int alpha = (pixel >> 24) & 0xff;
96             final int red   = (pixel >> 16) & 0xff;
97             final int green = (pixel >>  8) & 0xff;
98             final int blue  = (pixel      ) & 0xff;
99 
100             if (red == 0x00 && green == 0x00 && blue == 0x00)
101             {
102                 return;
103             }
104             ++m_nCount;
105         }
106     public void count(final int pixel)
107         {
108             // final int alpha = (pixel >> 24) & 0xff;
109             final int blue  = (pixel      ) & 0xff;
110             if (blue != 0x00)
111             {
112                 ++m_nCount;
113                 return;
114             }
115             final int green = (pixel >>  8) & 0xff;
116             if (green != 0x00)
117             {
118                 ++m_nCount;
119                 return;
120             }
121             final int red   = (pixel >> 16) & 0xff;
122             if (red != 0x00)
123             {
124                 ++m_nCount;
125                 return;
126             }
127         }
128 }
129 
130 // -----------------------------------------------------------------------------
131 class graphics_stuff
132 {
133 //    public int stuff()
134 //        {
135 //// (1) decoding
136 //            int rgba = 0; // ...; // comes from PixelGrabber, BufferedImage.getRGB etc.
137 //            int red = (rgba >> 16) & 0xff;
138 //            int green = (rgba >> 8) & 0xff;
139 //            int blue = rgba & 0xff;
140 //            int alpha = (rgba >> 24) & 0xff;
141 //// (2) now modify red, green, blue and alpha as you like;
142 ////     make sure that each of the four values stays in the
143 ////     interval 0 to 255
144 ////            ...
145 //// (3) and encode back to an int, e.g. to give it to MemoryImageSource or
146 ////     BufferedImage.setRGB
147 //                rgba = (alpha << 24) | (red << 16) | (green << 8) | blue;
148 //                return 0;
149 //        }
150 
151 //    public static void handlesinglepixel(int x, int y, int pixel)
152 //        {
153 //            int alpha = (pixel >> 24) & 0xff;
154 //            int red   = (pixel >> 16) & 0xff;
155 //            int green = (pixel >>  8) & 0xff;
156 //            int blue  = (pixel      ) & 0xff;
157 //            // Deal with the pixel as necessary...
158 //        }
159 
160     public static void countPixel(ImageHelper img, int _x, int _y, int _w, int _h, CountPixel _aPixelCounter)
161         {
162             for (int y = 0; y < _h; y++) {
163                 for (int x = 0; x < _w; x++) {
164                     // handlesinglepixel(x+i, y+j, pixels[j * w + i]);
165                     _aPixelCounter.count(img.getPixel(x,y));
166                 }
167             }
168         }
169     public static int countNotWhitePixel(ImageHelper _aImage)
170         {
171             final int w = _aImage.getWidth();
172             final int h = _aImage.getHeight();
173 
174             CountPixel aCountNotWhite = new CountNotWhite();
175             countPixel(_aImage, 0, 0, w, h, aCountNotWhite);
176             return aCountNotWhite.getCount();
177         }
178 
179     public static int countNotBlackPixel(ImageHelper _aImage)
180         {
181             final int w = _aImage.getWidth();
182             final int h = _aImage.getHeight();
183 
184             CountPixel aCountNotBlack = new CountNotBlack();
185             countPixel(_aImage, 0, 0, w, h, aCountNotBlack);
186             return aCountNotBlack.getCount();
187         }
188 }
189 
190 // -----------------------------------------------------------------------------
191 
192 public class PixelCounter {
193 	// private Image m_aImage;
194     // ImageHelper m_aImage;
195 
196 
197     public int countNotWhitePixel(String _sFile)
198         throws java.io.IOException
199         {
200             ImageHelper aImage = ImageHelper.createImageHelper(_sFile);
201             final int nw = graphics_stuff.countNotWhitePixel(aImage);
202             return nw;
203         }
204 
205     public int countNotBlackPixel(String _sFile)
206         throws java.io.IOException
207         {
208             ImageHelper aImage = ImageHelper.createImageHelper(_sFile);
209             final int nw = graphics_stuff.countNotBlackPixel(aImage);
210             return nw;
211         }
212 
213     public static int countNotWhitePixelsFromImage(String _sFile)
214         throws java.io.IOException
215         {
216             PixelCounter a = new PixelCounter();
217             return a.countNotWhitePixel(_sFile);
218         }
219 
220     public static int countNotBlackPixelsFromImage(String _sFile)
221         throws java.io.IOException
222         {
223             PixelCounter a = new PixelCounter();
224             return a.countNotBlackPixel(_sFile);
225         }
226 
227     // -----------------------------------------------------------------------------
228 
229 //    public static void main(String[] args) {
230 //
231 //        String a = helper.StringHelper.createValueString(10, 4);
232 //        int dummy = 1;
233 ///*
234 // BorderRemover a = new BorderRemover();
235 //        try
236 //        {
237 //            a.createNewImageWithoutBorder(args[0], args[1]);
238 //        }
239 //        catch(java.io.IOException e)
240 //        {
241 //            System.out.println("Exception caught.");
242 //        }
243 // */
244 //    }
245 }
246 
247 
248