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