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.4.8.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 ************************************************************************/
33 // import java.awt.Image;
34 import convwatch
.ImageHelper
;
36 // -----------------------------------------------------------------------------
37 abstract class CountPixel
40 public int getCount() {return m_nCount
;}
41 public abstract void count(int _nRGB
);
44 // -----------------------------------------------------------------------------
45 class CountNotWhite
extends CountPixel
47 public CountNotWhite()
49 // System.out.println("CountWhite()");
52 public void count(int pixel
)
54 int alpha
= (pixel
>> 24) & 0xff;
55 int red
= (pixel
>> 16) & 0xff;
56 int green
= (pixel
>> 8) & 0xff;
57 int blue
= (pixel
) & 0xff;
59 // System.out.println(String.valueOf(red) + ":" + String.valueOf(green) + ":" + String.valueOf(blue));
60 if (red
== 0xff && green
== 0xff && blue
== 0xff)
68 // -----------------------------------------------------------------------------
69 class CountNotBlack
extends CountPixel
71 public CountNotBlack()
73 // System.out.println("CountBlack()");
76 public void count(int pixel
)
78 int alpha
= (pixel
>> 24) & 0xff;
79 int red
= (pixel
>> 16) & 0xff;
80 int green
= (pixel
>> 8) & 0xff;
81 int blue
= (pixel
) & 0xff;
83 if (red
== 0x00 && green
== 0x00 && blue
== 0x00)
91 // -----------------------------------------------------------------------------
97 int rgba
= 0; // ...; // comes from PixelGrabber, BufferedImage.getRGB etc.
98 int red
= (rgba
>> 16) & 0xff;
99 int green
= (rgba
>> 8) & 0xff;
100 int blue
= rgba
& 0xff;
101 int alpha
= (rgba
>> 24) & 0xff;
102 // (2) now modify red, green, blue and alpha as you like;
103 // make sure that each of the four values stays in the
106 // (3) and encode back to an int, e.g. to give it to MemoryImageSource or
107 // BufferedImage.setRGB
108 rgba
= (alpha
<< 24) | (red
<< 16) | (green
<< 8) | blue
;
112 public static void handlesinglepixel(int x
, int y
, int pixel
)
114 int alpha
= (pixel
>> 24) & 0xff;
115 int red
= (pixel
>> 16) & 0xff;
116 int green
= (pixel
>> 8) & 0xff;
117 int blue
= (pixel
) & 0xff;
118 // Deal with the pixel as necessary...
121 public static void countPixel(ImageHelper img
, int _x
, int _y
, int _w
, int _h
, CountPixel _aPixelCounter
)
123 for (int y
= 0; y
< _h
; y
++) {
124 for (int x
= 0; x
< _w
; x
++) {
125 // handlesinglepixel(x+i, y+j, pixels[j * w + i]);
126 _aPixelCounter
.count(img
.getPixel(x
,y
));
130 public static int countNotWhitePixel(ImageHelper _aImage
)
132 int w
= _aImage
.getWidth();
133 int h
= _aImage
.getHeight();
135 CountPixel aCountNotWhite
= new CountNotWhite();
136 countPixel(_aImage
, 0, 0, w
, h
, aCountNotWhite
);
137 return aCountNotWhite
.getCount();
140 public static int countNotBlackPixel(ImageHelper _aImage
)
142 int w
= _aImage
.getWidth();
143 int h
= _aImage
.getHeight();
145 CountPixel aCountNotBlack
= new CountNotBlack();
146 countPixel(_aImage
, 0, 0, w
, h
, aCountNotBlack
);
147 return aCountNotBlack
.getCount();
151 // -----------------------------------------------------------------------------
153 public class PixelCounter
{
154 // private Image m_aImage;
155 ImageHelper m_aImage
;
158 public int countNotWhitePixel(String _sFile
)
159 throws java
.io
.IOException
161 m_aImage
= ImageHelper
.createImageHelper(_sFile
);
162 int nw
= graphics_stuff
.countNotWhitePixel(m_aImage
);
166 public int countNotBlackPixel(String _sFile
)
167 throws java
.io
.IOException
169 m_aImage
= ImageHelper
.createImageHelper(_sFile
);
170 int nw
= graphics_stuff
.countNotBlackPixel(m_aImage
);
174 public static int countNotWhitePixelsFromImage(String _sFile
)
175 throws java
.io
.IOException
177 PixelCounter a
= new PixelCounter();
178 return a
.countNotWhitePixel(_sFile
);
181 public static int countNotBlackPixelsFromImage(String _sFile
)
182 throws java
.io
.IOException
184 PixelCounter a
= new PixelCounter();
185 return a
.countNotBlackPixel(_sFile
);
188 // -----------------------------------------------------------------------------
190 public static void main(String
[] args
) {
192 String a
= helper
.StringHelper
.createValueString(10, 4);
195 BorderRemover a = new BorderRemover();
198 a.createNewImageWithoutBorder(args[0], args[1]);
200 catch(java.io.IOException e)
202 System.out.println("Exception caught.");