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