1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
30 // import java.awt.Image;
31 import convwatch
.ImageHelper
;
33 // -----------------------------------------------------------------------------
34 abstract class CountPixel
37 public int getCount() {return m_nCount
;}
38 public abstract void count(int _nRGB
);
41 // -----------------------------------------------------------------------------
42 class CountNotWhite
extends CountPixel
44 public CountNotWhite()
46 // System.out.println("CountWhite()");
49 public void count(int pixel
)
51 int alpha
= (pixel
>> 24) & 0xff;
52 int red
= (pixel
>> 16) & 0xff;
53 int green
= (pixel
>> 8) & 0xff;
54 int blue
= (pixel
) & 0xff;
56 // System.out.println(String.valueOf(red) + ":" + String.valueOf(green) + ":" + String.valueOf(blue));
57 if (red
== 0xff && green
== 0xff && blue
== 0xff)
65 // -----------------------------------------------------------------------------
66 class CountNotBlack
extends CountPixel
68 public CountNotBlack()
70 // System.out.println("CountBlack()");
73 public void count(int pixel
)
75 int alpha
= (pixel
>> 24) & 0xff;
76 int red
= (pixel
>> 16) & 0xff;
77 int green
= (pixel
>> 8) & 0xff;
78 int blue
= (pixel
) & 0xff;
80 if (red
== 0x00 && green
== 0x00 && blue
== 0x00)
88 // -----------------------------------------------------------------------------
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
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
;
109 public static void handlesinglepixel(int x
, int y
, int pixel
)
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...
118 public static void countPixel(ImageHelper img
, int _x
, int _y
, int _w
, int _h
, CountPixel _aPixelCounter
)
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
));
127 public static int countNotWhitePixel(ImageHelper _aImage
)
129 int w
= _aImage
.getWidth();
130 int h
= _aImage
.getHeight();
132 CountPixel aCountNotWhite
= new CountNotWhite();
133 countPixel(_aImage
, 0, 0, w
, h
, aCountNotWhite
);
134 return aCountNotWhite
.getCount();
137 public static int countNotBlackPixel(ImageHelper _aImage
)
139 int w
= _aImage
.getWidth();
140 int h
= _aImage
.getHeight();
142 CountPixel aCountNotBlack
= new CountNotBlack();
143 countPixel(_aImage
, 0, 0, w
, h
, aCountNotBlack
);
144 return aCountNotBlack
.getCount();
148 // -----------------------------------------------------------------------------
150 public class PixelCounter
{
151 // private Image m_aImage;
152 ImageHelper m_aImage
;
155 public int countNotWhitePixel(String _sFile
)
156 throws java
.io
.IOException
158 m_aImage
= ImageHelper
.createImageHelper(_sFile
);
159 int nw
= graphics_stuff
.countNotWhitePixel(m_aImage
);
163 public int countNotBlackPixel(String _sFile
)
164 throws java
.io
.IOException
166 m_aImage
= ImageHelper
.createImageHelper(_sFile
);
167 int nw
= graphics_stuff
.countNotBlackPixel(m_aImage
);
171 public static int countNotWhitePixelsFromImage(String _sFile
)
172 throws java
.io
.IOException
174 PixelCounter a
= new PixelCounter();
175 return a
.countNotWhitePixel(_sFile
);
178 public static int countNotBlackPixelsFromImage(String _sFile
)
179 throws java
.io
.IOException
181 PixelCounter a
= new PixelCounter();
182 return a
.countNotBlackPixel(_sFile
);
185 // -----------------------------------------------------------------------------
187 // public static void main(String[] args) {
189 // String a = helper.StringHelper.createValueString(10, 4);
192 // BorderRemover a = new BorderRemover();
195 // a.createNewImageWithoutBorder(args[0], args[1]);
197 // catch(java.io.IOException e)
199 // System.out.println("Exception caught.");