Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / qadevOOo / runner / graphical / PixelCounter.java
blobe625c2a75b12fddcfa0960391fced8a37332918a
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 ************************************************************************/
28 package graphical;
31 // -----------------------------------------------------------------------------
32 abstract class CountPixel
34 protected int m_nCount = 0;
35 public int getCount() {return m_nCount;}
36 public abstract void count(int _nRGB);
39 // -----------------------------------------------------------------------------
40 class CountNotWhite extends CountPixel
42 public CountNotWhite()
44 // System.out.println("CountWhite()");
47 public void countold(final int pixel)
49 // final int alpha = (pixel >> 24) & 0xff;
50 final int red = (pixel >> 16) & 0xff;
51 final int green = (pixel >> 8) & 0xff;
52 final int blue = (pixel ) & 0xff;
54 // System.out.println(String.valueOf(red) + ":" + String.valueOf(green) + ":" + String.valueOf(blue));
55 if (red == 0xff && green == 0xff && blue == 0xff)
57 return;
59 ++m_nCount;
61 public void count(final int pixel)
63 // final int alpha = (pixel >> 24) & 0xff;
64 final int blue = (pixel ) & 0xff;
65 if (blue != 0xff)
67 ++m_nCount;
68 return;
70 final int green = (pixel >> 8) & 0xff;
71 if (green != 0xff)
73 ++m_nCount;
74 return;
76 final int red = (pixel >> 16) & 0xff;
77 if (red != 0xff)
79 ++m_nCount;
80 return;
85 // -----------------------------------------------------------------------------
86 class CountNotBlack extends CountPixel
88 public CountNotBlack()
90 // System.out.println("CountBlack()");
93 public void countold(final int pixel)
95 // final int alpha = (pixel >> 24) & 0xff;
96 final int red = (pixel >> 16) & 0xff;
97 final int green = (pixel >> 8) & 0xff;
98 final int blue = (pixel ) & 0xff;
100 if (red == 0x00 && green == 0x00 && blue == 0x00)
102 return;
104 ++m_nCount;
106 public void count(final int pixel)
108 // final int alpha = (pixel >> 24) & 0xff;
109 final int blue = (pixel ) & 0xff;
110 if (blue != 0x00)
112 ++m_nCount;
113 return;
115 final int green = (pixel >> 8) & 0xff;
116 if (green != 0x00)
118 ++m_nCount;
119 return;
121 final int red = (pixel >> 16) & 0xff;
122 if (red != 0x00)
124 ++m_nCount;
125 return;
130 // -----------------------------------------------------------------------------
131 class graphics_stuff
133 // public int stuff()
134 // {
135 //// (1) decoding
136 // int rgba = 0; // ...; // comes from PixelGrabber, BufferedImage.getRGB etc.
137 // int red = (rgba >> 16) & 0xff;
138 // int green = (rgba >> 8) & 0xff;
139 // int blue = rgba & 0xff;
140 // int alpha = (rgba >> 24) & 0xff;
141 //// (2) now modify red, green, blue and alpha as you like;
142 //// make sure that each of the four values stays in the
143 //// interval 0 to 255
144 //// ...
145 //// (3) and encode back to an int, e.g. to give it to MemoryImageSource or
146 //// BufferedImage.setRGB
147 // rgba = (alpha << 24) | (red << 16) | (green << 8) | blue;
148 // return 0;
149 // }
151 // public static void handlesinglepixel(int x, int y, int pixel)
152 // {
153 // int alpha = (pixel >> 24) & 0xff;
154 // int red = (pixel >> 16) & 0xff;
155 // int green = (pixel >> 8) & 0xff;
156 // int blue = (pixel ) & 0xff;
157 // // Deal with the pixel as necessary...
158 // }
160 public static void countPixel(ImageHelper img, int _x, int _y, int _w, int _h, CountPixel _aPixelCounter)
162 for (int y = 0; y < _h; y++) {
163 for (int x = 0; x < _w; x++) {
164 // handlesinglepixel(x+i, y+j, pixels[j * w + i]);
165 _aPixelCounter.count(img.getPixel(x,y));
169 public static int countNotWhitePixel(ImageHelper _aImage)
171 final int w = _aImage.getWidth();
172 final int h = _aImage.getHeight();
174 CountPixel aCountNotWhite = new CountNotWhite();
175 countPixel(_aImage, 0, 0, w, h, aCountNotWhite);
176 return aCountNotWhite.getCount();
179 public static int countNotBlackPixel(ImageHelper _aImage)
181 final int w = _aImage.getWidth();
182 final int h = _aImage.getHeight();
184 CountPixel aCountNotBlack = new CountNotBlack();
185 countPixel(_aImage, 0, 0, w, h, aCountNotBlack);
186 return aCountNotBlack.getCount();
190 // -----------------------------------------------------------------------------
192 public class PixelCounter {
193 // private Image m_aImage;
194 // ImageHelper m_aImage;
197 public int countNotWhitePixel(String _sFile)
198 throws java.io.IOException
200 ImageHelper aImage = ImageHelper.createImageHelper(_sFile);
201 final int nw = graphics_stuff.countNotWhitePixel(aImage);
202 return nw;
205 public int countNotBlackPixel(String _sFile)
206 throws java.io.IOException
208 ImageHelper aImage = ImageHelper.createImageHelper(_sFile);
209 final int nw = graphics_stuff.countNotBlackPixel(aImage);
210 return nw;
213 public static int countNotWhitePixelsFromImage(String _sFile)
214 throws java.io.IOException
216 PixelCounter a = new PixelCounter();
217 return a.countNotWhitePixel(_sFile);
220 public static int countNotBlackPixelsFromImage(String _sFile)
221 throws java.io.IOException
223 PixelCounter a = new PixelCounter();
224 return a.countNotBlackPixel(_sFile);
227 // -----------------------------------------------------------------------------
229 // public static void main(String[] args) {
231 // String a = helper.StringHelper.createValueString(10, 4);
232 // int dummy = 1;
233 ///*
234 // BorderRemover a = new BorderRemover();
235 // try
236 // {
237 // a.createNewImageWithoutBorder(args[0], args[1]);
238 // }
239 // catch(java.io.IOException e)
240 // {
241 // System.out.println("Exception caught.");
242 // }
243 // */
244 // }