1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: PixelCounter.java,v $
10 * $Revision: 1.1.2.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
34 // -----------------------------------------------------------------------------
35 abstract class CountPixel
37 protected int m_nCount
= 0;
38 public int getCount() {return m_nCount
;}
39 public abstract void count(int _nRGB
);
42 // -----------------------------------------------------------------------------
43 class CountNotWhite
extends CountPixel
45 public CountNotWhite()
47 // System.out.println("CountWhite()");
50 public void countold(final int pixel
)
52 // final int alpha = (pixel >> 24) & 0xff;
53 final int red
= (pixel
>> 16) & 0xff;
54 final int green
= (pixel
>> 8) & 0xff;
55 final int blue
= (pixel
) & 0xff;
57 // System.out.println(String.valueOf(red) + ":" + String.valueOf(green) + ":" + String.valueOf(blue));
58 if (red
== 0xff && green
== 0xff && blue
== 0xff)
64 public void count(final int pixel
)
66 // final int alpha = (pixel >> 24) & 0xff;
67 final int blue
= (pixel
) & 0xff;
73 final int green
= (pixel
>> 8) & 0xff;
79 final int red
= (pixel
>> 16) & 0xff;
88 // -----------------------------------------------------------------------------
89 class CountNotBlack
extends CountPixel
91 public CountNotBlack()
93 // System.out.println("CountBlack()");
96 public void countold(final int pixel
)
98 // final int alpha = (pixel >> 24) & 0xff;
99 final int red
= (pixel
>> 16) & 0xff;
100 final int green
= (pixel
>> 8) & 0xff;
101 final int blue
= (pixel
) & 0xff;
103 if (red
== 0x00 && green
== 0x00 && blue
== 0x00)
109 public void count(final int pixel
)
111 // final int alpha = (pixel >> 24) & 0xff;
112 final int blue
= (pixel
) & 0xff;
118 final int green
= (pixel
>> 8) & 0xff;
124 final int red
= (pixel
>> 16) & 0xff;
133 // -----------------------------------------------------------------------------
136 // public int stuff()
139 // int rgba = 0; // ...; // comes from PixelGrabber, BufferedImage.getRGB etc.
140 // int red = (rgba >> 16) & 0xff;
141 // int green = (rgba >> 8) & 0xff;
142 // int blue = rgba & 0xff;
143 // int alpha = (rgba >> 24) & 0xff;
144 //// (2) now modify red, green, blue and alpha as you like;
145 //// make sure that each of the four values stays in the
146 //// interval 0 to 255
148 //// (3) and encode back to an int, e.g. to give it to MemoryImageSource or
149 //// BufferedImage.setRGB
150 // rgba = (alpha << 24) | (red << 16) | (green << 8) | blue;
154 // public static void handlesinglepixel(int x, int y, int pixel)
156 // int alpha = (pixel >> 24) & 0xff;
157 // int red = (pixel >> 16) & 0xff;
158 // int green = (pixel >> 8) & 0xff;
159 // int blue = (pixel ) & 0xff;
160 // // Deal with the pixel as necessary...
163 public static void countPixel(ImageHelper img
, int _x
, int _y
, int _w
, int _h
, CountPixel _aPixelCounter
)
165 for (int y
= 0; y
< _h
; y
++) {
166 for (int x
= 0; x
< _w
; x
++) {
167 // handlesinglepixel(x+i, y+j, pixels[j * w + i]);
168 _aPixelCounter
.count(img
.getPixel(x
,y
));
172 public static int countNotWhitePixel(ImageHelper _aImage
)
174 final int w
= _aImage
.getWidth();
175 final int h
= _aImage
.getHeight();
177 CountPixel aCountNotWhite
= new CountNotWhite();
178 countPixel(_aImage
, 0, 0, w
, h
, aCountNotWhite
);
179 return aCountNotWhite
.getCount();
182 public static int countNotBlackPixel(ImageHelper _aImage
)
184 final int w
= _aImage
.getWidth();
185 final int h
= _aImage
.getHeight();
187 CountPixel aCountNotBlack
= new CountNotBlack();
188 countPixel(_aImage
, 0, 0, w
, h
, aCountNotBlack
);
189 return aCountNotBlack
.getCount();
193 // -----------------------------------------------------------------------------
195 public class PixelCounter
{
196 // private Image m_aImage;
197 // ImageHelper m_aImage;
200 public int countNotWhitePixel(String _sFile
)
201 throws java
.io
.IOException
203 ImageHelper aImage
= ImageHelper
.createImageHelper(_sFile
);
204 final int nw
= graphics_stuff
.countNotWhitePixel(aImage
);
208 public int countNotBlackPixel(String _sFile
)
209 throws java
.io
.IOException
211 ImageHelper aImage
= ImageHelper
.createImageHelper(_sFile
);
212 final int nw
= graphics_stuff
.countNotBlackPixel(aImage
);
216 public static int countNotWhitePixelsFromImage(String _sFile
)
217 throws java
.io
.IOException
219 PixelCounter a
= new PixelCounter();
220 return a
.countNotWhitePixel(_sFile
);
223 public static int countNotBlackPixelsFromImage(String _sFile
)
224 throws java
.io
.IOException
226 PixelCounter a
= new PixelCounter();
227 return a
.countNotBlackPixel(_sFile
);
230 // -----------------------------------------------------------------------------
232 // public static void main(String[] args) {
234 // String a = helper.StringHelper.createValueString(10, 4);
237 // BorderRemover a = new BorderRemover();
240 // a.createNewImageWithoutBorder(args[0], args[1]);
242 // catch(java.io.IOException e)
244 // System.out.println("Exception caught.");