/** Marvin Project <2007-2009> Initial version by: Danilo Rosetto Munoz Fabio Andrijauskas Gabriel Ambrosio Archanjo site: http://marvinproject.sourceforge.net GPL Copyright (C) <2007> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ package org.marvinproject.image.statistical.mode; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; import marvin.gui.MarvinFilterWindow; import marvin.image.MarvinImage; import marvin.image.MarvinImageMask; import marvin.performance.MarvinPerformanceMeter; import marvin.plugin.MarvinAbstractImagePlugin; import marvin.util.MarvinAttributes; /** * @author Fabio Andrijauskas */ public class Mode extends MarvinAbstractImagePlugin { MarvinAttributes attributes; MarvinPerformanceMeter performanceMeter; public void load() { attributes = getAttributes(); attributes.set("size", 3); //attributes.set("shift", 0); //attributes.set("circlesDistance", 0); performanceMeter = new MarvinPerformanceMeter(); } public void process ( MarvinImage a_imageIn, MarvinImage a_imageOut, MarvinAttributes a_attributesOut, MarvinImageMask a_mask, boolean a_previewMode ) { performanceMeter.start("Moda Filter"); int l_size = (Integer)attributes.get("size"); int mapSize = l_size * l_size; int l_totalRed = 0; int l_totalGreen = 0; int l_totalBlue = 0; int qtd = 0; int tmpx = 0; int tmpy = 0; int width = a_imageIn.getWidth(); int height = a_imageIn.getHeight(); Map mapModaR; Map mapModaG; Map mapModaB; performanceMeter.enableProgressBar("Moda Filter" ,a_imageIn.getWidth() * a_imageIn.getHeight() ); performanceMeter.startEvent("Moda Filter"); boolean[][] l_arrMask = a_mask.getMaskArray(); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { if(l_arrMask != null && !l_arrMask[x][y]){ continue; } tmpx = x - l_size; tmpy = y - l_size; mapModaR = new HashMap(mapSize); mapModaG = new HashMap(mapSize); mapModaB = new HashMap(mapSize); if(tmpx < 0) tmpx = 0; if(tmpy < 0) tmpy = 0; int finalX = x + l_size; int finalY = y + l_size; if(finalX > width) finalX = width; if(finalY > height) finalY = height; for (int xm = tmpx; xm < finalX ; xm++) { for (int ym = tmpy; ym < y + l_size ; ym++) { if(xm >= 0 && xm < width && ym >= 0 && ym < height ) { int rgb = a_imageIn.getIntColor(xm, ym); l_totalRed = (int)(rgb & 0x00FF0000) >>> 16; l_totalGreen = (int) ((rgb & 0x0000FF00) >>> 8); l_totalBlue = (int) (rgb & 0x000000FF); Integer l_val = mapModaR.get(l_totalRed); if(l_val == null) l_val = 0; mapModaR.put((Integer)l_totalRed, ++l_val); l_val = mapModaG.get(l_totalGreen); if(l_val == null) l_val = 0; mapModaG.put((Integer)l_totalGreen, ++l_val); l_val = mapModaB.get(l_totalBlue); if(l_val == null) l_val = 0; mapModaB.put((Integer)l_totalBlue, ++l_val); } } } Integer r1 = 0; for(Integer it : mapModaR.keySet()) { if( mapModaR.get(it) >= r1) { r1 = it; } } Integer g1 = 0; for(Integer it : mapModaG.keySet()) { if( mapModaG.get(it) >= g1) { g1 = it; } } Integer b1 = 0; for(Integer it : mapModaB.keySet()) { if( mapModaB.get(it) >= b1) { b1 = it; } } a_imageOut.setIntColor(x, y, r1 , g1 , b1 ); } performanceMeter.incProgressBar(height); performanceMeter.stepsFinished(height); } performanceMeter.finishEvent(); performanceMeter.finish(); } public void show() { MarvinFilterWindow l_filterWindow = new MarvinFilterWindow("Avarage Filter", 420,350, getImagePanel(), this); l_filterWindow.addLabel("lblWidth", "Size:"); l_filterWindow.addTextField("txtSize", "size", attributes); l_filterWindow.newComponentRow(); l_filterWindow.setVisible(true); } }