import java.awt.Color; import java.util.Vector; import processing.core.PApplet; import processing.core.PImage; /* * Created on 21-Jul-2005 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ /** * @author Christian * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class Word { String name; Vector colors; String colorName; String[] related_tags; Vector relatedVect; int[] sharedColors; int average_color; float av_hue; float av_sat; float av_bri; Vector photos; Vector maincolors; Vector finalColors; int gshift; int rshift; float segmentationRate; float _x; float _y; float _xf; float _yf; float _xi; float _yi; int[] wPixels; int _width; int _height; boolean _visible; float wScale; PImage image_1; boolean isSelected; Word(String $name, String $colorname, String $photoId, String[] $related_tags) { name=$name; colors=new Vector(); photos=new Vector(); colors.addElement(new Colore($colorname)); photos.addElement($photoId); _visible=true; wScale=.25f; related_tags=$related_tags; gshift = 255<<8; rshift = 255<<16; isSelected=false; } // void addImage(String $colorname, String $photoId) { photos.addElement($photoId); addColor($colorname); } // // void addColor(String $colorname){ int $len=colors.size(); Colore $col; for(int i=0; i<$len; i++){ $col=(Colore)colors.get(i); if($col.name.equals($colorname)){ $col.increaseCount(); return; } } colors.addElement(new Colore($colorname)); } // // int getNumberImages(){ return photos.size(); } // // int getNumberColors(){ return colors.size(); } // // int getNumberColors(int soglia){ int $res=0; int $len1=colors.size(); for(int i=0; i<$len1; i++){ if(((Colore)colors.get(i)).count>=soglia) $res++; } return $res; } // // void createMainColors() { maincolors=new Vector(); int $len=colors.size(); int $tollerance=50; } // // void orderColors(){ finalColors=new Vector(); int $len=colors.size(); Colore $col; Colore $finalcol; for(int i=0; i<$len; i++){ int $len2=finalColors.size(); $col=(Colore) colors.get(i); if($len2<1){ finalColors.addElement($col); } else{ for(int j=0; j<$len2; j++){ $finalcol=(Colore) finalColors.get(j); if($finalcol.hue>$col.hue){ finalColors.insertElementAt($col,j); //finalColors.add(j,$col); j=$len2; } } if(finalColors.size()==$len2){ finalColors.add($col); } } } calculateAverage2(); if($len>1){ calculateSegmentationRate2(); } else{ segmentationRate=0.0f; } //checkErrors(); } // // void checkErrors(){ int $colorsLen=0; int $len=colors.size(); Colore $col; for(int i=0; i<$len; i++){ $col=(Colore) colors.get(i); $colorsLen+=$col.count+1; } if($colorsLen!=photos.size()){ System.out.println("Error in "+name); } } // // void calculateAverage(){ float $hueSum=0f; float $satSum=0f; float $briSum=0f; int $len=colors.size(); Colore $col; for(int i=0; i<$len; i++){ $col=(Colore) colors.get(i); $hueSum+=$col.hue*($col.count+1); if(name.equals("red")) System.out.print($col.hue*($col.count+1)+" "); $satSum+=$col.sat*($col.count+1); $briSum+=$col.bri*($col.count+1); } float fact=photos.size()*1.0f; av_hue=$hueSum/fact; av_sat=$satSum/fact; av_bri=$briSum/fact; average_color=Color.HSBtoRGB(av_hue,av_sat,av_bri); if(name.equals("red")){ System.out.println(" ------- "); System.out.println("average hue: "+av_hue); System.out.println("average sat: "+av_sat); System.out.println("average bri: "+av_bri); } } // // void calculateAverage2(){ float $bSum=0f; float $gSum=0f; float $rSum=0f; int $len=colors.size(); Colore $col; int $val; for(int i=0; i<$len; i++){ $col=(Colore) colors.get(i); $val=$col.value; $bSum+=($val & 255)*($col.count+1); $gSum+=(($val & gshift) >> 8)*($col.count+1); $rSum+=(($val & rshift) >> 16)*($col.count+1); } float fact=photos.size()*1.0f; int $b=(int) ($bSum/fact); int $g=(int) ($gSum/fact); int $r=(int) ($rSum/fact); average_color=0xff000000 | ($r<<16) | ($g<<8) | $b; int c=average_color; float hsb[]=Color.RGBtoHSB( c>>16&0xff, c>>8&0xff, c&0xff, null ); av_hue=hsb[0]; av_sat=hsb[1]; av_bri=hsb[2]; } // // void calculateSegmentationRate2(){ Vector $hueDifferenceVect=new Vector(); Vector $satDifferenceVect=new Vector(); Vector $briDifferenceVect=new Vector(); int $len=finalColors.size(); Colore $col; float[] res; for(int i=0; i<$len; i++){ $col=(Colore) finalColors.get(i); res=getDifference2($col); $hueDifferenceVect.addElement(new Float(res[0])); $satDifferenceVect.addElement(new Float(res[1])); $briDifferenceVect.addElement(new Float(res[2])); } float $hueSum=0f; float $satSum=0f; float $briSum=0f; float $size=$hueDifferenceVect.size()*1.0f; for(int i=0; i<$size; i++){ $hueSum+=((Float)$hueDifferenceVect.get(i)).floatValue(); $satSum+=((Float)$satDifferenceVect.get(i)).floatValue(); $briSum+=((Float)$briDifferenceVect.get(i)).floatValue(); } float fact=colors.size()*1.0f; //segmentationRate=($hueSum/fact+$satSum/fact+$briSum/fact)/3.0f; segmentationRate=$hueSum/fact; //System.out.println("seg rate: "+segmentationRate); } // // float[] getDifference2(Colore $col){ float[] $res=new float[3]; $res[0]=Math.abs($col.hue-av_hue); $res[1]=Math.abs($col.sat-av_sat); $res[2]=Math.abs($col.bri-av_bri); return $res; } // // void calculateSegmentationRate(){ Vector $hueDifferenceVect=new Vector(); Vector $satDifferenceVect=new Vector(); Vector $briDifferenceVect=new Vector(); int $len=finalColors.size(); Colore $col_1; Colore $col_2; float[] res; for(int i=0; i<$len; i++){ $col_1=(Colore) finalColors.get(i); for(int j=i+1; j<$len; j++){ if(j!=i){ $col_2=(Colore) finalColors.get(j); res=getDifference($col_1,$col_2); $hueDifferenceVect.addElement(new Float(res[0])); $satDifferenceVect.addElement(new Float(res[0])); $briDifferenceVect.addElement(new Float(res[0])); } } } float $hueSum=0f; float $satSum=0f; float $briSum=0f; float $size=$hueDifferenceVect.size()*1.0f; for(int i=0; i<$size; i++){ $hueSum+=((Float)$hueDifferenceVect.get(i)).floatValue(); $satSum+=((Float)$satDifferenceVect.get(i)).floatValue(); $briSum+=((Float)$briDifferenceVect.get(i)).floatValue(); } segmentationRate=($hueSum/photos.size()+$satSum/photos.size()+$briSum/photos.size())/3.0f; //segmentationRate=$hueSum/photos.size(); //System.out.println("seg rate: "+segmentationRate); } // // float[] getDifference(Colore $col_1, Colore $col_2){ float[] $res=new float[3]; $res[0]=new Double(Math.sqrt(Math.abs($col_1.hue-$col_2.hue)*Math.abs($col_1.hue-$col_2.hue))).floatValue(); $res[1]=new Double(Math.sqrt(Math.abs($col_1.sat-$col_2.sat)*Math.abs($col_1.sat-$col_2.sat))).floatValue(); $res[2]=new Double(Math.sqrt(Math.abs($col_1.bri-$col_2.bri)*Math.abs($col_1.bri-$col_2.bri))).floatValue(); /*int b1=$col_1 & 255; int b2=$col_2 & 255; int g1=($col_1 & gshift) >> 8; int g2=($col_2 & gshift) >> 8; int r1=($col_1 & rshift) >> 16; int r2=($col_2 & rshift) >> 16; return(Math.abs(b1-b2)+Math.abs(g1-g2)+Math.abs(r1-r2))/3f;*/ return $res; } // // void initMe(PApplet pa,int x,int y, int w, int h) { _width=w*4; _height=h*4; _xi=_xf=_x=x*1f+w*.5f; _yi=_yf=_y=y*1f+h*.5f; float $fact=photos.size()*1.0f; float wFact=_width/$fact; float laX=0f; int $len=finalColors.size(); Colore $col; pa.noStroke(); for(int i=0; i<$len; i++){ $col=(Colore) finalColors.get(i); pa.fill(0xff000000|Integer.parseInt($col.name,16)); pa.rect(laX,0,wFact*($col.count+1),_height); laX+=wFact*($col.count+1); } /*pa.stroke(255,100); pa.noFill(); pa.rect(x,y,w-1,h-1);*/ pa.fill(255); String t_name=name.toUpperCase(); if(pa.textWidth(t_name)>_width){ int $lenStr=t_name.length(); while(pa.textWidth(t_name)>_width){ $lenStr--; t_name=t_name.substring(0,$lenStr)+"..."; } } pa.text(t_name, _width*.5, _height*.75); wPixels=new int[_width*_height]; pa.loadPixels(); int posY,posY1; for(int $y=0; $y<_height; $y++){ posY=$y*pa.width; posY1=$y*_width; for(int $x=0; $x<_width; $x++){ wPixels[posY1+$x]=pa.pixels[posY+$x]; } } image_1=new PImage(_width, _height); System.arraycopy(wPixels,0,image_1.pixels,0,_width*_height); isSelected=true; /*pa.fill(0xff000000|average_color); pa.rect(10,y+h*.5f,780,h*.5f); System.out.println("Segmentation: "+segmentationRate);*/ } // // void drawMe(PApplet pa){ if(!_visible) return; int $x=(int) _x; int $y=(int) _y; pa.image(image_1,0,0); /*pa.loadPixels(); int yPos,yRootPos; for(int y=0; y<_height; y++){ yPos=y*_width; if($y+y=0){ yRootPos=($y+y)*pa.width; for(int x=0; x<_width; x++){ if(x+$x=0){ //if(_visible){ pa.pixels[yRootPos+x+$x]=wPixels[yPos+x]; //}else{ //pa.pixels[yRootPos+x+$x]=0x88000000 | wPixels[yPos+x]; //} } } } } pa.updatePixels();*/ } // // void update(){ float speed=0.2f; _x+=(_xf-_x)*speed; _y+=(_yf-_y)*speed; } // // void arrangeBy(PApplet pa,int criteria,float nPhotosFactW, float nPhotosFactH, int minPhotos, int maxPhotos, float segmentationFactW, float segmentationFactH,float minSeg,float maxSeg){ if(criteria==0){ _xf=_xi; _yf=_yi; wScale=.25f; } else if(criteria==1){ //y:seg x:pop wScale=.25f; _xf=((photos.size()-minPhotos)*nPhotosFactW)+_width*.5*wScale; _yf=(segmentationRate-minSeg)*segmentationFactH+_height*.5*wScale; } else if(criteria==2){ wScale=.25f; _yf=pa.height-(_height*wScale*.5)-(photos.size()-minPhotos)*nPhotosFactH; _xf=(segmentationRate-minSeg)*segmentationFactW+(_width*.5*wScale); } else if(criteria==3){ float $r=(av_hue*360f); float $rad=Math.abs(maxSeg-(segmentationRate-minSeg))*segmentationFactH*.5f; //float $rad=(maxPhotos-photos.size())*nPhotosFactH*0.5f; _xf = (float) (pa.width*.5 + Math.cos ($r * Math.PI / 180) * $rad); _yf = (float) (pa.height*.5f + Math.sin ($r * Math.PI / 180f) * $rad)-_height*.25; wScale=(float) (Math.sqrt(Math.sqrt((double)(photos.size()-minPhotos)))*.15); } } // // void checkString(String str){ int $len=str.length(); if($len>0){ if($len<=name.length()){ if(str.substring(0,$len).equals(name.substring(0,$len))){ _visible=true; } else{ //System.out.print("."); _visible=false; } } else{ _visible=false; } } else{ _visible=true; } } /** * * */ void findSharedColors() { // TODO Auto-generated method stub int $rLen=relatedVect.size(); sharedColors=new int[$rLen]; if($rLen>0){ for(int i=0; i<$rLen; i++){ sharedColors[i]=findSharedColor((Word) relatedVect.get(i)); } } } int findSharedColor(Word $rWord){ int $closestCol=0xffffffff; float $mindist=1000f; int $len1=colors.size(); int $len2=$rWord.colors.size(); Colore $col_1; Colore $col_2; float t_dists[]; float t_dist; for(int i=0; i<$len1; i++){ $col_1=(Colore) colors.get(i); for(int j=0; j<$len2; j++){ $col_2=(Colore) $rWord.colors.get(j); t_dists=getDifference($col_1,$col_2); t_dist=(t_dists[0]+t_dists[1]+t_dists[2])/3f; if(t_dist<$mindist){ $mindist=t_dist; $closestCol=0xff000000 | $col_2.value; } } } return $closestCol; } }