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: BorderRemover.java,v $
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 convwatch
.ImageHelper
;
35 import java
.awt
.image
.RenderedImage
;
36 import java
.awt
.image
.BufferedImage
;
37 import java
.lang
.reflect
.Method
;
39 // -----------------------------------------------------------------------------
47 public Rect(int _x
, int _y
, int _w
, int _h
)
54 public int getX() {return x
;}
55 public int getY() {return y
;}
56 public int getWidth() {return w
;}
57 public int getHeight() {return h
;}
64 // Helper values, filled after find Border
66 // --------------------------------- test mode ---------------------------------
68 // void pixelValue(int pixel)
70 // int alpha = (pixel >> 24) & 0xff;
71 // int red = (pixel >> 16) & 0xff;
72 // int green = (pixel >> 8) & 0xff;
73 // int blue = (pixel ) & 0xff;
78 * compares 2 colors with a given tolerance. So it's possible to check differences approximate.
81 * @param _nTolerance is a percentage value how strong the colors could be differ
84 boolean compareColorWithTolerance(int _nColor1
, int _nColor2
, int _nTolerance
)
86 // int alpha1 = (_nColor1 >> 24) & 0xff;
87 int red1
= (_nColor1
>> 16) & 0xff;
88 int green1
= (_nColor1
>> 8) & 0xff;
89 int blue1
= (_nColor1
) & 0xff;
91 // int alpha2 = (_nColor2 >> 24) & 0xff;
92 int red2
= (_nColor2
>> 16) & 0xff;
93 int green2
= (_nColor2
>> 8) & 0xff;
94 int blue2
= (_nColor2
) & 0xff;
96 if (_nTolerance
> 100)
101 // calculate tolerance halve
102 double nTolerable
= (_nTolerance
* 256 / 100);
108 // X - th < Y < X + th
109 // if ((red1 - nTolerable) < red2 && red2 < (red1 + nTolerable))
112 if (Math
.abs(red1
- red2
) < nTolerable
)
114 if (Math
.abs(green1
- green2
) < nTolerable
)
116 if (Math
.abs(blue1
- blue2
) < nTolerable
)
139 * create a new image from an exist one without it's borders
140 * open the file (_sFilenameFrom) as an image, check if it contains any borders and remove
143 public boolean createNewImageWithoutBorder(String _sFilenameFrom
, String _sFilenameTo
)
144 throws java
.io
.IOException
146 // System.out.println("load image: " + fileName);
147 m_aImage
= ImageHelper
.createImageHelper(_sFilenameFrom
);
149 // System.out.println("image width:" + String.valueOf(m_aImage.getWidth()));
150 // System.out.println("image height:" + String.valueOf(m_aImage.getHeight()));
152 // int nw = graphics_stuff.countNotWhitePixel(m_aImage);
153 // System.out.println("not white pixels:" + String.valueOf(nw));
155 // int nb = graphics_stuff.countNotBlackPixel(m_aImage);
156 // System.out.println("not black pixels:" + String.valueOf(nb));
158 int nBorderColor
= m_aImage
.getPixel(0,0);
159 Rect aInnerRect
= findBorder(m_aImage
, nBorderColor
);
161 RenderedImage aImage
= createImage(m_aImage
, aInnerRect
);
163 File aWriteFile
= new File(_sFilenameTo
);
164 // GlobalLogWriter.get().println("Hello World: File to: " + _sFilenameTo);
169 Class imageIOClass
= Class
.forName("javax.imageio.ImageIO");
170 // GlobalLogWriter.get().println("Hello World: get Class");
172 Method getWriterMIMETypesMethod
= imageIOClass
.getDeclaredMethod("getWriterMIMETypes", new Class
[]{ });
173 // GlobalLogWriter.get().println("Hello World: get Methode");
175 Object aObj
= getWriterMIMETypesMethod
.invoke(imageIOClass
, new Object
[]{ });
176 String
[] types
= (String
[])aObj
;
177 // GlobalLogWriter.get().println("Hello World: types: " + Arrays.asList(types) );
179 Method writeMethod
= imageIOClass
.getDeclaredMethod("write", new Class
[]{ java
.awt
.image
.RenderedImage
.class,
180 java
.lang
.String
.class,
181 java
.io
.File
.class});
182 // GlobalLogWriter.get().println("Hello World: get Methode");
183 writeMethod
.invoke(imageIOClass
, new Object
[]{aImage
, "image/jpeg", aWriteFile
});
185 catch(java
.lang
.ClassNotFoundException e
) {
189 catch(java
.lang
.NoSuchMethodException e
) {
193 catch(java
.lang
.IllegalAccessException e
) {
197 catch(java
.lang
.reflect
.InvocationTargetException e
) {
204 String javaVersion
= System
.getProperty("java.version");
205 throw new java
.io
.IOException(
206 "Cannot construct object with current Java version " +
207 javaVersion
+ ": " + ex
.getMessage());
209 // ImageIO.write(aImage, "jpg", aWriteFile);
216 * runs through the image, pixel by pixel
217 * as long as found pixels like the color at (0,0) this is interpreted as border.
218 * as result it fills the m_nXMin, m_nXMax, m_nYMin, m_nYMax values.
221 Rect
findBorder(ImageHelper _aImage
, int _nBorderColor
)
223 int h
= _aImage
.getHeight();
224 int w
= _aImage
.getWidth();
230 for (int y
= 0; y
< h
; y
++)
232 for (int x
= 0; x
< nXMin
; x
++)
234 // handlesinglepixel(x+i, y+j, pixels[j * w + i]);
235 int nCurrentColor
= _aImage
.getPixel(x
, y
);
236 if (! compareColorWithTolerance(nCurrentColor
, _nBorderColor
, 10))
238 // pixelValue(nCurrentColor);
239 // System.out.print("*");
240 nXMin
= java
.lang
.Math
.min(nXMin
, x
);
241 nYMin
= java
.lang
.Math
.min(nYMin
, y
);
245 // System.out.print(" ");
249 for (int y
= 0; y
< h
; y
++)
251 for (int nx
= w
- 1; nx
>= nXMax
; --nx
)
254 int nCurrentColor
= _aImage
.getPixel(nx
, ny
);
255 if (! compareColorWithTolerance(nCurrentColor
, _nBorderColor
, 10))
257 nXMax
= java
.lang
.Math
.max(nXMax
, nx
);
258 nYMax
= java
.lang
.Math
.max(nYMax
, ny
);
261 // System.out.println();
263 // System.out.println("xmin: " + String.valueOf(nXMin));
264 // System.out.println("xmax: " + String.valueOf(nXMax));
265 // System.out.println("ymin: " + String.valueOf(nYMin));
266 // System.out.println("ymax: " + String.valueOf(nYMax));
269 if (nXMin
< nXMax
&& nYMin
< nYMax
)
271 int nw
= nXMax
- nXMin
+ 1;
272 int nh
= nYMax
- nYMin
+ 1;
274 // this is the rectangle around the image content.
275 aRect
= new Rect(nXMin
, nYMin
, nw
, nh
);
279 // create the smalles possible image
280 aRect
= new Rect(0,0,1,1);
291 RenderedImage
createImage(ImageHelper _aImage
, Rect _aRect
) throws IllegalArgumentException
293 // TODO: throw if w or h < 0
294 int w
= _aRect
.getWidth();
295 int h
= _aRect
.getHeight();
297 if (w
<= 0 || h
<= 0)
299 throw new IllegalArgumentException("width or height are too small or negative.");
302 BufferedImage aBI
= new BufferedImage(w
, h
, BufferedImage
.TYPE_INT_RGB
);
304 int nXOffset
= _aRect
.getX();
305 int nYOffset
= _aRect
.getY();
308 for (int y
= 0; y
< h
; y
++)
310 for (int x
= 0; x
< w
; x
++)
312 // aPixels[y * w + x] = m_aImage.getPixel(m_nXMin + x, m_nYMin + y);
313 aBI
.setRGB(x
, y
, _aImage
.getPixel(x
+ nXOffset
, y
+ nYOffset
));
316 // java.awt.image.MemoryImageSource aSource = new java.awt.image.MemoryImageSource(w, h, aPixels, 0, w);
317 // return java.awt.Component.createImage(aSource);
318 // return java.awt.Toolkit.getDefaultToolkit().createImage(aSource);