// ----------------------------------------------------------------------------
// application: Diffuse demo
// author: Pavel Tisnovsky
// file name: Diffuse7.java
// directory: java
// date: 25.07.2004
// description: Demonstracni aplikace pro vytvoreni a zobrazeni fraktalniho
// objektu, ktery je zalozeny na simulaci difuze. Pri vytvareni
// difuze se postupne generuji body, jez se spojite pohybuji
// v zadane bitmape a tvori v ni tak cestu. Pocatek generovani
// techto bodu lezi na kruznici, ktera se postupne zvetsuje od
// stredu vznikajiciho obrazce difuze.
// ----------------------------------------------------------------------------
// Nasledujici radky jsou urceny pro spusteni appletu v programu AppletViewer:
//
//
//
// pro kompatibilitu se starsi JVM firmy Microsofti se musi provest preklad
// pomoci prikazu:
// javac -target 1.1 Diffuse7.java
// ----------------------------------------------------------------------------
// seznam importovanych balicku
// ----------------------------------------------------------------------------
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.applet.Applet;
// ----------------------------------------------------------------------------
// Trida pro aplikaci filtru na zvolenou zdrojovou pixmapu. Vysledek se uklada
// do cilove pixmapy.
// ----------------------------------------------------------------------------
class Filter {
private int imageWidth; // velikost zpracovavanych pixmap
private int imageHeight;
private int pixelSrc[]; // zdrojova pixmapa
private int pixelDest[]; // cilova pixmapa
// ------------------------------------------------------------------------
// V konstruktoru se nastavi velikost filtru a zdrojova a cilova pixmapa.
// ------------------------------------------------------------------------
public Filter(int imageWidth, int imageHeight, int pixelSrc[], int pixelDest[]) {
this.imageWidth=imageWidth; // nastavit velikost pixmapy
this.imageHeight=imageHeight;
this.pixelSrc=pixelSrc; // nastavit handle zdrojove
this.pixelDest=pixelDest; // a cilove pixmapy
}
// ------------------------------------------------------------------------
// Nastaveni nove sirky zpracovavane pixmapy.
// ------------------------------------------------------------------------
public void setImageWidth(int imageWidth) {
this.imageWidth=imageWidth;
}
// ------------------------------------------------------------------------
// Nastaveni nove vysky zpracovavane pixmapy.
// ------------------------------------------------------------------------
public void setImageHeight(int imageHeight) {
this.imageHeight=imageHeight;
}
// ------------------------------------------------------------------------
// Nastaveni zdrojove pixmapy.
// ------------------------------------------------------------------------
public void setSourceImage(int pixelSrc[]) {
this.pixelSrc=pixelSrc;
}
// ------------------------------------------------------------------------
// Nastaveni cilove pixmapy.
// ------------------------------------------------------------------------
public void setDestinationImage(int pixelDest[]) {
this.pixelDest=pixelDest;
}
// ------------------------------------------------------------------------
// Ziskani sirky zpracovavanych pixmap.
// ------------------------------------------------------------------------
public int getImageWidth() {
return imageWidth;
}
// ------------------------------------------------------------------------
// Ziskani vysky zpracovavanych pixmap.
// ------------------------------------------------------------------------
public int getImageHeight() {
return imageHeight;
}
// ------------------------------------------------------------------------
// Ziskani zdrojove pixmapy.
// ------------------------------------------------------------------------
public int[] getSourceImage() {
return pixelSrc;
}
// ------------------------------------------------------------------------
// Ziskani cilove pixmapy.
// ------------------------------------------------------------------------
public int[] getDestinationImage() {
return pixelDest;
}
// ------------------------------------------------------------------------
// Tato metoda provede filtraci pixmapy s filtrem typu:
// zadny filtr, pouze kopie zdrojove pixmapy do cilove pixmapy.
// ------------------------------------------------------------------------
public void filterNone() {
int pos=0;
for (int j=0; j>16;
g =(color & 0x0000ff00)>>8;
b =(color & 0x000000ff);
color=pixelSrc[pos+1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+imageWidth];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+imageWidth+1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
r/=4; g/=4; b/=4; // provest filtraci
pixelDest[pos]=(r<<16) | (g<<8) | b | (0xff<<24);
pos++;
}
}
}
// ------------------------------------------------------------------------
// Tato metoda provede filtraci pixmapy s filtrem typu:
// rozmazani v bloku 3x3 pixely s vyberem ve tvaru diamantu.
// ------------------------------------------------------------------------
public void filter3x3diamond() {
int pos=0; // index do zdrojove a cilove pixmapy
int color; // RGBA barvove slozky zakodovane do typu int
int r, g, b; // jednotlive rozkodovane barvove slozky
filterNone(); // nejprve pixmapy zkopirovat
pos=1+imageWidth;
for (int j=1; j>16;
g =(color & 0x0000ff00)>>8;
b =(color & 0x000000ff);
color=pixelSrc[pos-1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+imageWidth];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
r/=4; g/=4; b/=4; // provest filtraci
pixelDest[pos]=(r<<16) | (g<<8) | b | (0xff<<24);
pos++;
}
}
}
// ------------------------------------------------------------------------
// Tato metoda provede filtraci pixmapy s filtrem typu:
// rozmazani v bloku 3x3 pixely s vyberem ve tvaru osoveho krize.
// ------------------------------------------------------------------------
public void filter3x3cross() {
int pos=0; // index do zdrojove a cilove pixmapy
int color; // RGBA barvove slozky zakodovane do typu int
int r, g, b; // jednotlive rozkodovane barvove slozky
filterNone(); // nejprve pixmapy zkopirovat
pos=1+imageWidth;
for (int j=1; j>16;
g =(color & 0x0000ff00)>>8;
b =(color & 0x000000ff);
color=pixelSrc[pos-1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+imageWidth];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
r/=5; g/=5; b/=5; // provest filtraci
pixelDest[pos]=(r<<16) | (g<<8) | b | (0xff<<24);
pos++;
}
}
}
// ------------------------------------------------------------------------
// Tato metoda provede filtraci pixmapy s filtrem typu:
// rozmazani v bloku 3x3 pixelu s vyberem ve tvaru uhlopricneho krize.
// ------------------------------------------------------------------------
public void filter3x3Xcross() {
int pos=0; // index do zdrojove a cilove pixmapy
int color; // RGBA barvove slozky zakodovane do typu int
int r, g, b; // jednotlive rozkodovane barvove slozky
filterNone(); // nejprve pixmapy zkopirovat
pos=1+imageWidth;
for (int j=1; j>16;
g =(color & 0x0000ff00)>>8;
b =(color & 0x000000ff);
color=pixelSrc[pos-imageWidth+1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+imageWidth-1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos-imageWidth+1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
r/=5; g/=5; b/=5; // provest filtraci
pixelDest[pos]=(r<<16) | (g<<8) | b | (0xff<<24);
pos++;
}
}
}
// ------------------------------------------------------------------------
// Tato metoda provede filtraci pixmapy s filtrem typu:
// rozmazani v bloku 3x3 pixelu s blokovym vyberem.
// ------------------------------------------------------------------------
public void filter3x3block() {
int pos=0; // index do zdrojove a cilove pixmapy
int color; // RGBA barvove slozky zakodovane do typu int
int r, g, b; // jednotlive rozkodovane barvove slozky
filterNone(); // nejprve pixmapy zkopirovat
pos=1+imageWidth;
for (int j=1; j>16;
g =(color & 0x0000ff00)>>8;
b =(color & 0x000000ff);
color=pixelSrc[pos-imageWidth];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos-imageWidth+1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos-1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+imageWidth-1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+imageWidth];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos-imageWidth+1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
r/=9; g/=9; b/=9; // provest filtraci
pixelDest[pos]=(r<<16) | (g<<8) | b | (0xff<<24);
pos++;
}
}
}
// ------------------------------------------------------------------------
// Tato metoda provede filtraci pixmapy s filtrem typu:
// rozmazani v bloku 5x5 pixelu s vyberem ve tvaru diamantu.
// ------------------------------------------------------------------------
public void filter5x5diamond() {
int pos=0; // index do zdrojove a cilove pixmapy
int color; // RGBA barvove slozky zakodovane do typu int
int r, g, b; // jednotlive rozkodovane barvove slozky
filterNone(); // nejprve pixmapy zkopirovat
pos=2+2*imageWidth;
for (int j=2; j>16;
g =(color & 0x0000ff00)>>8;
b =(color & 0x000000ff);
color=pixelSrc[pos- imageWidth-1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos- imageWidth+1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos-2];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+2];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+ imageWidth-1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+ imageWidth+1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+2*imageWidth+0];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
r/=8; g/=8; b/=8; // provest filtraci
pixelDest[pos]=(r<<16) | (g<<8) | b | (0xff<<24);
pos++;
}
}
}
// ------------------------------------------------------------------------
// Tato metoda provede filtraci pixmapy s filtrem typu:
// rozmazani v bloku 5x5 pixelu s vyberem ve tvaru osoveho krize.
// ------------------------------------------------------------------------
public void filter5x5cross() {
int pos=0; // index do zdrojove a cilove pixmapy
int color; // RGBA barvove slozky zakodovane do typu int
int r, g, b; // jednotlive rozkodovane barvove slozky
filterNone(); // nejprve pixmapy zkopirovat
pos=2+2*imageWidth;
for (int j=2; j>16;
g =(color & 0x0000ff00)>>8;
b =(color & 0x000000ff);
color=pixelSrc[pos- imageWidth+0];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos-2];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos-1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+2];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+ imageWidth+0];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+2*imageWidth+0];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
r/=9; g/=9; b/=9; // provest filtraci
pixelDest[pos]=(r<<16) | (g<<8) | b | (0xff<<24);
pos++;
}
}
}
// ------------------------------------------------------------------------
// Tato metoda provede filtraci pixmapy s filtrem typu:
// rozmazani v bloku 5x5 pixelu s vyberem ve tvaru uhlopricneho krize.
// ------------------------------------------------------------------------
public void filter5x5Xcross() {
int pos=0; // index do zdrojove a cilove pixmapy
int color; // RGBA barvove slozky zakodovane do typu int
int r, g, b; // jednotlive rozkodovane barvove slozky
filterNone(); // nejprve pixmapy zkopirovat
pos=2+2*imageWidth;
for (int j=2; j>16;
g =(color & 0x0000ff00)>>8;
b =(color & 0x000000ff);
color=pixelSrc[pos-2*imageWidth+2];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos- imageWidth-1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos- imageWidth+1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+ imageWidth-1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+ imageWidth+1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+2*imageWidth-2];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+2*imageWidth+2];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
r/=9; g/=9; b/=9; // provest filtraci
pixelDest[pos]=(r<<16) | (g<<8) | b | (0xff<<24);
pos++;
}
}
}
// ------------------------------------------------------------------------
// Tato metoda provede filtraci pixmapy s filtrem typu:
// rozmazani v bloku 5x5 pixelu s vyberem ve tvaru bloku.
// ------------------------------------------------------------------------
public void filter5x5block() {
int pos=0; // index do zdrojove a cilove pixmapy
int color; // RGBA barvove slozky zakodovane do typu int
int r, g, b; // jednotlive rozkodovane barvove slozky
filterNone(); // nejprve pixmapy zkopirovat
pos=2+2*imageWidth;
for (int j=2; j>16;
g =(color & 0x0000ff00)>>8;
b =(color & 0x000000ff);
color=pixelSrc[pos-2*imageWidth-1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos-2*imageWidth+0];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos-2*imageWidth+1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos-2*imageWidth+2];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos- imageWidth-2];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos- imageWidth-1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos- imageWidth+0];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos- imageWidth+1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos- imageWidth+2];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos-2];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos-1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+2];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+ imageWidth-2];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+ imageWidth-1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+ imageWidth+0];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+ imageWidth+1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+ imageWidth+2];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+2*imageWidth-2];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+2*imageWidth-1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+2*imageWidth+0];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+2*imageWidth+1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+2*imageWidth+2];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
r/=25; g/=25; b/=25; // provest filtraci
pixelDest[pos]=(r<<16) | (g<<8) | b | (0xff<<24);
pos++;
}
}
}
// ------------------------------------------------------------------------
// Tato metoda provede filtraci pixmapy s filtrem typu:
// horizontalni filtr rozmazani tri pixelu.
// ------------------------------------------------------------------------
public void filter3pixelsH() {
int pos=0; // index do zdrojove a cilove pixmapy
int color; // RGBA barvove slozky zakodovane do typu int
int r, g, b; // jednotlive rozkodovane barvove slozky
filterNone(); // nejprve pixmapy zkopirovat
pos=1;
for (int j=0; j>16;
g =(color & 0x0000ff00)>>8;
b =(color & 0x000000ff);
color=pixelSrc[pos];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
r/=3; g/=3; b/=3; // provest filtraci
pixelDest[pos]=(r<<16) | (g<<8) | b | (0xff<<24);
pos++;
}
}
}
// ------------------------------------------------------------------------
// Tato metoda provede filtraci pixmapy s filtrem typu:
// vertikalni filtr rozmazani tri pixelu.
// ------------------------------------------------------------------------
public void filter3pixelsV() {
int pos=0; // index do zdrojove a cilove pixmapy
int color; // RGBA barvove slozky zakodovane do typu int
int r, g, b; // jednotlive rozkodovane barvove slozky
filterNone(); // nejprve pixmapy zkopirovat
pos=imageWidth;
for (int j=1; j>16;
g =(color & 0x0000ff00)>>8;
b =(color & 0x000000ff);
color=pixelSrc[pos];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+imageWidth];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
r/=3; g/=3; b/=3; // provest filtraci
pixelDest[pos]=(r<<16) | (g<<8) | b | (0xff<<24);
pos++;
}
}
}
// ------------------------------------------------------------------------
// Tato metoda provede filtraci pixmapy s filtrem typu:
// horizontalni filtr rozmazani peti pixelu.
// ------------------------------------------------------------------------
public void filter5pixelsH() {
int pos=0; // index do zdrojove a cilove pixmapy
int color; // RGBA barvove slozky zakodovane do typu int
int r, g, b; // jednotlive rozkodovane barvove slozky
filterNone(); // nejprve pixmapy zkopirovat
pos=2;
for (int j=0; j>16;
g =(color & 0x0000ff00)>>8;
b =(color & 0x000000ff);
color=pixelSrc[pos-1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+1];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+2];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
r/=5; g/=5; b/=5; // provest filtraci
pixelDest[pos]=(r<<16) | (g<<8) | b | (0xff<<24);
pos++;
}
}
}
// ------------------------------------------------------------------------
// Tato metoda provede filtraci pixmapy s filtrem typu:
// vertikalni filtr rozmazani peti pixelu.
// ------------------------------------------------------------------------
public void filter5pixelsV() {
int pos=0; // index do zdrojove a cilove pixmapy
int color; // RGBA barvove slozky zakodovane do typu int
int r, g, b; // jednotlive rozkodovane barvove slozky
filterNone(); // nejprve pixmapy zkopirovat
pos=2*imageWidth;
for (int j=2; j>16;
g =(color & 0x0000ff00)>>8;
b =(color & 0x000000ff);
color=pixelSrc[pos- imageWidth];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+ imageWidth];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
color=pixelSrc[pos+2*imageWidth];
r+=(color & 0x00ff0000)>>16;
g+=(color & 0x0000ff00)>>8;
b+=(color & 0x000000ff);
r/=5; g/=5; b/=5; // provest filtraci
pixelDest[pos]=(r<<16) | (g<<8) | b | (0xff<<24);
pos++;
}
}
}
}
// ----------------------------------------------------------------------------
// Konec tridy Filter.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// Trida s panelem, na kterem se zobrazuje pixmapa o predem danem rozmeru.
// ----------------------------------------------------------------------------
class ImagePanel extends Canvas {
int width; // rozmery pixmapy
int height;
public int[] pixels; // pixmapa - zdroj pixelu
MemoryImageSource imageSource; // objekt zaobalujici zdroj pixelu
Image image; // pixmapa vytvorena nad zdrojem pixelu
// ------------------------------------------------------------------------
// V konstruktoru se nastavi velikost panelu a soucasne barva jeho pozadi.
// ------------------------------------------------------------------------
public ImagePanel(int width, int height) {
pixels=new int[width*height];
for (int j=0; jwidth-2 || j>height-2) return false;
return type ?
pixels[j-1][i-1] | // osmiokoli pixelu
pixels[j-1][i] |
pixels[j-1][i+1] |
pixels[j][i-1] |
pixels[j][i+1] |
pixels[j+1][i-1] |
pixels[j+1][i] |
pixels[j+1][i+1]
:
pixels[j-1][i] | // ctyrokoli pixelu
pixels[j][i-1] |
pixels[j][i+1] |
pixels[j+1][i]
;
}
// -----------------------------------------------------------------------
// Vykresleni bodu, tj. jednoho pixelu ci nekolika pixelu, do temporalni
// bitmapy (plosne rastrove bitove mrizky) podle uzivatelem predem
// zvoleneho tvaru bodu. Tvar bodu je specifikovan v promenne pointShape.
// -----------------------------------------------------------------------
private void putPoint(int x, int y) {
if (x<3) x=3; // zabranit zapisum
if (x>=width-3) x=width-4; // mimo pole (bitmapu)
if (y<3) y=3;
if (y>=height-3) y=height-3;
if (pointShape!=PointShape.Diamond3x3 && // krome dvou specialnich tvaru
pointShape!=PointShape.Diamond5x5) {
pixels[y][x]=true; // vykreslit prostredni pixel
}
if (pointShape==PointShape.Cross3x3 ||
pointShape==PointShape.Diamond3x3 ||
pointShape==PointShape.Block3x3 ||
pointShape==PointShape.Cross5x5 ||
pointShape==PointShape.Block5x5 ||
pointShape==PointShape.Horizontal3 ||
pointShape==PointShape.Horizontal5) {
pixels[y][x-1]=true; // nastavit pixel nalevo
pixels[y][x+1]=true; // a pixel napravo
} // od prostredniho pixelu
if (pointShape==PointShape.Horizontal5 ||
pointShape==PointShape.Cross5x5 ||
pointShape==PointShape.Diamond5x5 ||
pointShape==PointShape.Block5x5) {
pixels[y][x-2]=true; // nastavit druhy pixel nalevo
pixels[y][x+2]=true; // a druhy pixel napravo
} // od prostredniho pixelu
if (pointShape==PointShape.Cross3x3 ||
pointShape==PointShape.Diamond3x3 ||
pointShape==PointShape.Block3x3 ||
pointShape==PointShape.Cross5x5 ||
pointShape==PointShape.Block5x5 ||
pointShape==PointShape.Vertical3 ||
pointShape==PointShape.Vertical5) {
pixels[y-1][x]=true; // nastavit pixel nahore
pixels[y+1][x]=true; // a pixel dole
} // od prostredniho pixelu
if (pointShape==PointShape.Vertical5 ||
pointShape==PointShape.Cross5x5 ||
pointShape==PointShape.Diamond5x5 ||
pointShape==PointShape.Block5x5) {
pixels[y-1][x]=true; // nastavit druhy pixel nahore
pixels[y+1][x]=true; // a druhy pixel dole
} // od prostredniho pixelu
if (pointShape==PointShape.XCross3x3 ||
pointShape==PointShape.Block3x3 ||
pointShape==PointShape.XCross5x5 ||
pointShape==PointShape.Diamond5x5 ||
pointShape==PointShape.Block5x5) {
pixels[y-1][x-1]=true; // nastavit ctyri diagonalni pixely
pixels[y+1][x-1]=true;
pixels[y-1][x+1]=true;
pixels[y+1][x+1]=true;
}
if (pointShape==PointShape.XCross5x5 ||
pointShape==PointShape.Block5x5) {
pixels[y-2][x-2]=true; // nastavit ctyri diagonalni pixely
pixels[y+2][x-2]=true; // ve vzdalenosti 2
pixels[y-2][x+2]=true; // od prostredniho pixelu
pixels[y+2][x+2]=true;
}
if (pointShape==PointShape.Block5x5) {
pixels[y-2][x-1]=true; // nastavit zbyvajicich osm pixelu
pixels[y+2][x-1]=true; // v bloku 5x5 pixelu
pixels[y-2][x+1]=true;
pixels[y+2][x+1]=true;
pixels[y-1][x-2]=true;
pixels[y+1][x-2]=true;
pixels[y-1][x+2]=true;
pixels[y+1][x+2]=true;
}
}
// -----------------------------------------------------------------------
// Aplikace algoritmu difuze, jez je zalozen na postupnem pohybu bodu
// v pracovni bitmape (binarni rastrove mrizce). Pocatecni pozice bodu
// lezi na kruznici, ktera se pri behu algoritmu plynule zvetsuje od
// stredu vznikajiciho obrazce difuze az do okraju vytvarene bitmapy.
// -----------------------------------------------------------------------
private void applyDiffuse() {
final int CounterThreshold=200; // pocet bodu pro provedeni vypisu
// do GUI okna appletu ci aplikace
int counter=0; // pocitadlo vytvorenych bodu
int points=0; // pocitadlo vsech bodu
int dist; // vzdalenost od stredu obrazce
float alpha; // uhel vytvoreneho bodu od x-ove osy
int x, y; // souradnice generovanych bodu
int box; // pocitadla smycek
int originX=width>>1; // souradnice stredu pracovni bitmapy
int originY=height>>1;
int radius; // maximalni povoleny polomer kruznice
boolean looking;
if (originXwidth-2) x--; // lezi uvnitr povolene
if (y<1) y++; // oblasti v bitmape
if (y>height-2) y--;
// pokud se pixel dotkl stavajiciho objektu
if (neighboor(neighboorType, x, y)){
putPoint(x, y); // je k nemu pripojen
looking=false; // hledani lze ukoncit
points++; // zvysit pocitadlo vsech pripojenych bodu
box++;
}
// po dosazeni dostatecneho poctu pripojenych pixelu
counter++; // prubezny vypis prace programu
if (counter==CounterThreshold) {
label.setText("Points: " // do GUI okna appletu ci aplikace
+String.valueOf(points)
+" dist: "
+String.valueOf(dist));
counter=0;
}
} while (looking); // interni smycku ukoncit az po pripojeni
} // pixelu k obrazci difuze
} // konec hlavni programove smycky
}
// -----------------------------------------------------------------------
// Funkce, ktera rozbehne cele vypocetni vlakno.
// -----------------------------------------------------------------------
public void run() {
prepareGUI(); // vytvoreni vsech GUI prvku
label.setText("Initializing");
initBitmap(); // inicializace bitmapy
initSeed(); // inicializace seminka
applyDiffuse(); // aplikace algoritmu difuze
label.setText("Finishing");
copyBitmapToPixmap(); // kopie pracovni bitmapy do vysledne pixmapy
finishGUI(); // fitrace a zavreni pomocneho okna
}
}
// ----------------------------------------------------------------------------
// Konec tridy Diffuser.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// finito
// ----------------------------------------------------------------------------