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