bump product version to 4.1.6.2
[LibreOffice.git] / qadevOOo / runner / convwatch / BorderRemover.java
blobc3290fc47713d71ee393f60d2364274cfa389d77
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package convwatch;
21 import convwatch.ImageHelper;
22 import java.io.File;
23 import java.awt.image.RenderedImage;
24 import java.awt.image.BufferedImage;
25 import java.lang.reflect.Method;
27 // -----------------------------------------------------------------------------
28 class Rect
30 int x;
31 int y;
32 int w;
33 int h;
35 public Rect(int _x, int _y, int _w, int _h)
37 x = _x;
38 y = _y;
39 w = _w;
40 h = _h;
42 public int getX() {return x;}
43 public int getY() {return y;}
44 public int getWidth() {return w;}
45 public int getHeight() {return h;}
48 class BorderRemover
50 ImageHelper m_aImage;
52 // Helper values, filled after find Border
54 // --------------------------------- test mode ---------------------------------
56 // void pixelValue(int pixel)
57 // {
58 // int alpha = (pixel >> 24) & 0xff;
59 // int red = (pixel >> 16) & 0xff;
60 // int green = (pixel >> 8) & 0xff;
61 // int blue = (pixel ) & 0xff;
62 // int dummy = 0;
63 // }
66 * compares 2 colors with a given tolerance. So it's possible to check differences approximate.
67 * @param _nColor1
68 * @param _nColor2
69 * @param _nTolerance is a percentage value how strong the colors could be differ
72 boolean compareColorWithTolerance(int _nColor1, int _nColor2, int _nTolerance)
74 // int alpha1 = (_nColor1 >> 24) & 0xff;
75 int red1 = (_nColor1 >> 16) & 0xff;
76 int green1 = (_nColor1 >> 8) & 0xff;
77 int blue1 = (_nColor1 ) & 0xff;
79 // int alpha2 = (_nColor2 >> 24) & 0xff;
80 int red2 = (_nColor2 >> 16) & 0xff;
81 int green2 = (_nColor2 >> 8) & 0xff;
82 int blue2 = (_nColor2 ) & 0xff;
84 if (_nTolerance > 100)
86 _nTolerance = 100;
89 // calculate tolerance halve
90 double nTolerable = (_nTolerance * 256 / 100);
91 if (nTolerable < 0)
93 nTolerable = 0;
96 // X - th < Y < X + th
97 // if ((red1 - nTolerable) < red2 && red2 < (red1 + nTolerable))
98 // is the same
99 // abs (X - Y) < th
100 if (Math.abs(red1 - red2) < nTolerable)
102 if (Math.abs(green1 - green2) < nTolerable)
104 if (Math.abs(blue1 - blue2) < nTolerable)
106 return true;
108 else
110 // blue differ
113 else
115 // green differ
118 else
120 // red differ
123 return false;
127 * create a new image from an exist one without it's borders
128 * open the file (_sFilenameFrom) as an image, check if it contains any borders and remove
129 * the borders.
131 public boolean createNewImageWithoutBorder(String _sFilenameFrom, String _sFilenameTo)
132 throws java.io.IOException
134 // System.out.println("load image: " + fileName);
135 m_aImage = ImageHelper.createImageHelper(_sFilenameFrom);
137 // System.out.println("image width:" + String.valueOf(m_aImage.getWidth()));
138 // System.out.println("image height:" + String.valueOf(m_aImage.getHeight()));
140 // int nw = graphics_stuff.countNotWhitePixel(m_aImage);
141 // System.out.println("not white pixels:" + String.valueOf(nw));
143 // int nb = graphics_stuff.countNotBlackPixel(m_aImage);
144 // System.out.println("not black pixels:" + String.valueOf(nb));
146 int nBorderColor = m_aImage.getPixel(0,0);
147 Rect aInnerRect = findBorder(m_aImage, nBorderColor);
149 RenderedImage aImage = createImage(m_aImage, aInnerRect);
151 File aWriteFile = new File(_sFilenameTo);
152 // GlobalLogWriter.get().println("Hello World: File to: " + _sFilenameTo);
154 Exception ex = null;
157 Class<?> imageIOClass = Class.forName("javax.imageio.ImageIO");
158 // GlobalLogWriter.get().println("Hello World: get Class");
160 Method getWriterMIMETypesMethod = imageIOClass.getDeclaredMethod("getWriterMIMETypes", new Class[]{ });
161 // GlobalLogWriter.get().println("Hello World: get Methode");
163 getWriterMIMETypesMethod.invoke(imageIOClass, new Object[]{ });
164 Method writeMethod = imageIOClass.getDeclaredMethod("write", new Class[]{ java.awt.image.RenderedImage.class,
165 java.lang.String.class,
166 java.io.File.class});
167 // GlobalLogWriter.get().println("Hello World: get Methode");
168 writeMethod.invoke(imageIOClass, new Object[]{aImage, "image/jpeg", aWriteFile});
170 catch(java.lang.ClassNotFoundException e) {
171 e.printStackTrace();
172 ex = e;
174 catch(java.lang.NoSuchMethodException e) {
175 e.printStackTrace();
176 ex = e;
178 catch(java.lang.IllegalAccessException e) {
179 e.printStackTrace();
180 ex = e;
182 catch(java.lang.reflect.InvocationTargetException e) {
183 e.printStackTrace();
184 ex = e;
187 if (ex != null) {
188 // get Java version:
189 String javaVersion = System.getProperty("java.version");
190 throw new java.io.IOException(
191 "Cannot construct object with current Java version " +
192 javaVersion + ": " + ex.getMessage());
194 // ImageIO.write(aImage, "jpg", aWriteFile);
196 return true;
201 * runs through the image, pixel by pixel
202 * as long as found pixels like the color at (0,0) this is interpreted as border.
203 * as result it fills the m_nXMin, m_nXMax, m_nYMin, m_nYMax values.
206 Rect findBorder(ImageHelper _aImage, int _nBorderColor)
208 int h = _aImage.getHeight();
209 int w = _aImage.getWidth();
210 int nXMin = w;
211 int nXMax = 0;
212 int nYMin = h;
213 int nYMax = 0;
215 for (int y = 0; y < h; y++)
217 for (int x = 0; x < nXMin; x++)
219 // handlesinglepixel(x+i, y+j, pixels[j * w + i]);
220 int nCurrentColor = _aImage.getPixel(x, y);
221 if (! compareColorWithTolerance(nCurrentColor, _nBorderColor, 10))
223 // pixelValue(nCurrentColor);
224 // System.out.print("*");
225 nXMin = java.lang.Math.min(nXMin, x);
226 nYMin = java.lang.Math.min(nYMin, y);
228 // else
229 // {
230 // System.out.print(" ");
231 // }
234 for (int y = 0; y < h; y++)
236 for (int nx = w - 1; nx >= nXMax; --nx)
238 int ny = h - y - 1;
239 int nCurrentColor = _aImage.getPixel(nx, ny);
240 if (! compareColorWithTolerance(nCurrentColor, _nBorderColor, 10))
242 nXMax = java.lang.Math.max(nXMax, nx);
243 nYMax = java.lang.Math.max(nYMax, ny);
246 // System.out.println();
248 // System.out.println("xmin: " + String.valueOf(nXMin));
249 // System.out.println("xmax: " + String.valueOf(nXMax));
250 // System.out.println("ymin: " + String.valueOf(nYMin));
251 // System.out.println("ymax: " + String.valueOf(nYMax));
253 Rect aRect;
254 if (nXMin < nXMax && nYMin < nYMax)
256 int nw = nXMax - nXMin + 1;
257 int nh = nYMax - nYMin + 1;
259 // this is the rectangle around the image content.
260 aRect = new Rect(nXMin, nYMin, nw, nh );
262 else
264 // create the smalles possible image
265 aRect = new Rect(0,0,1,1);
269 // m_nXMin = nXMin;
270 // m_nXMax = nXMax;
271 // m_nYMin = nYMin;
272 // m_nYMax = nYMax;
273 return aRect;
276 RenderedImage createImage(ImageHelper _aImage, Rect _aRect) throws IllegalArgumentException
278 // TODO: throw if w or h < 0
279 int w = _aRect.getWidth();
280 int h = _aRect.getHeight();
282 if (w <= 0 || h <= 0)
284 throw new IllegalArgumentException("width or height are too small or negative.");
287 BufferedImage aBI = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
289 int nXOffset = _aRect.getX();
290 int nYOffset = _aRect.getY();
292 // Memory Block move
293 for (int y = 0; y < h; y++)
295 for (int x = 0; x < w; x++)
297 // aPixels[y * w + x] = m_aImage.getPixel(m_nXMin + x, m_nYMin + y);
298 aBI.setRGB(x, y, _aImage.getPixel(x + nXOffset, y + nYOffset));
301 // java.awt.image.MemoryImageSource aSource = new java.awt.image.MemoryImageSource(w, h, aPixels, 0, w);
302 // return java.awt.Component.createImage(aSource);
303 // return java.awt.Toolkit.getDefaultToolkit().createImage(aSource);
304 return aBI;