import java.awt.*;
import java.applet.*;
import java.awt.image.*; // To get the PixelGrabber
/**
* Lucio Gayosso 08/15/00. This program will show three different image priocessing
* techniques and will also make use of minimun redundancy coding.
* /
/**
* This class reads PARAM tags from its HTML host page and sets
* the color and label properties of the applet. Program execution
* begins with the init() method.
*/
public class imageprocess1 extends Applet
{
/**
* The entry point for the applet.
*/
Image m_NormalImage, m_ProcessedImage, m_ProcessedImage2, m_ProcessedImage3; // Declare the image objects to use
int m_nProcessedPixels[], m_nProcessedPixels2[], m_nProcessedPixels3[]; // Declares the pixel arrays (m_nProcessedPixels2[] for m_ProcessedImage2, m_nProcessedPixels3[] for m_ProcessedImage3)
public void init()
{
//initForm();
//usePageParams();
// TODO: Add any constructor code after initForm call.
// Call the method to load the images
loadImages();
// Call the method to pull the pixels from the image
// and place them in an integer array.
pullPixels();
// Process the image (creation of a horizontal 'wave')
horzWave();
makeLines();
makeBlurry();
// Call the method that creates a new image from
// the array of pixels
pushPixels();
}
private final String labelParam = "label";
private final String backgroundParam = "background";
private final String foregroundParam = "foreground";
/**
* Reads parameters from the applet's HTML host and sets applet
* properties.
*/
private void usePageParams()
{
final String defaultLabel = "Default label";
final String defaultBackground = "C0C0C0";
final String defaultForeground = "000000";
String labelValue;
String backgroundValue;
String foregroundValue;
/**
* Read the ,
* ,
* and tags from
* the applet's HTML host.
*/
labelValue = getParameter(labelParam);
backgroundValue = getParameter(backgroundParam);
foregroundValue = getParameter(foregroundParam);
if ((labelValue == null) || (backgroundValue == null) ||
(foregroundValue == null))
{
/**
* There was something wrong with the HTML host tags.
* Generate default values.
*/
labelValue = defaultLabel;
backgroundValue = defaultBackground;
foregroundValue = defaultForeground;
}
/**
* Set the applet's string label, background color, and
* foreground colors.
*/
label1.setText(labelValue);
label1.setBackground(stringToColor(backgroundValue));
label1.setForeground(stringToColor(foregroundValue));
this.setBackground(stringToColor(backgroundValue));
this.setForeground(stringToColor(foregroundValue));
}
/**
* Converts a string formatted as "rrggbb" to an awt.Color object
*/
private Color stringToColor(String paramValue)
{
int red;
int green;
int blue;
red = (Integer.decode("0x" + paramValue.substring(0,2))).intValue();
green = (Integer.decode("0x" + paramValue.substring(2,4))).intValue();
blue = (Integer.decode("0x" + paramValue.substring(4,6))).intValue();
return new Color(red,green,blue);
}
/**
* External interface used by design tools to show properties of an applet.
*/
public String[][] getParameterInfo()
{
String[][] info =
{
{ labelParam, "String", "Label string to be displayed" },
{ backgroundParam, "String", "Background color, format \"rrggbb\"" },
{ foregroundParam, "String", "Foreground color, format \"rrggbb\"" },
};
return info;
}
Label label1 = new Label();
/**
* Intializes values for the applet and its components
*/
void initForm()
{
this.setBackground(Color.lightGray);
this.setForeground(Color.black);
label1.setText("label1");
this.setLayout(new BorderLayout());
this.add("North",label1);
}
public void loadImages()
{
// Load the image into both Image classes
m_NormalImage = getImage(getDocumentBase(), "fotoapplet3.jpg");
m_ProcessedImage = getImage(getDocumentBase(), "fotoapplet3.jpg");
m_ProcessedImage2 = m_ProcessedImage;
m_ProcessedImage3 = m_ProcessedImage;
// Create a MediaTracker class so we can wait for
// to be completely loaded before proceeding.
MediaTracker Tracker = new MediaTracker(this);
// Add the images to the MediaTracker class ('n' images can be added to same Tracker)
Tracker.addImage(m_NormalImage,0);
Tracker.addImage(m_ProcessedImage,1);
Tracker.addImage(m_ProcessedImage2,1);
Tracker.addImage(m_ProcessedImage3,1);
try
{
// Wait for the images to completely arrive.
Tracker.waitForAll();
}
catch(InterruptedException e)
{
}
}
// ********************************************************************
public void pullPixels()
{
// Call the method that creates the pixel arrays (a pixel array is needed for each modified image)
m_nProcessedPixels = getPixels(m_ProcessedImage);
m_nProcessedPixels2 = getPixels(m_ProcessedImage2);
m_nProcessedPixels3 = getPixels(m_ProcessedImage3);
}
public int[] getPixels(Image img)
{
// Get the width and height of the image
int nWidth = img.getWidth(this);;
int nHeight = img.getHeight(this);
// Calculate the number of pixels
int nNumPixels = nWidth * nHeight;
// Create the array of integers for the pixel data.
int nRawPixels[] = new int[nNumPixels];
if(nRawPixels == null)
return(null);
// Create a PixelGrabber class
PixelGrabber Grabber = new PixelGrabber(img, 0, 0, nWidth, nHeight, nRawPixels, 0, nWidth);
try
{
// Get the pixels into the integer array
Grabber.grabPixels();
}
catch (InterruptedException e)
{
}
// Return the array
return(nRawPixels);
}
//***************************************************************************
public void pushPixels()
{
// Call the method that creates the new Image class passing m_ProcessedImage, m_nProcessedPixels the first time
// and the corresponding variables, for a second effect, on a second time and so on
m_ProcessedImage = storePixels(m_ProcessedImage, m_nProcessedPixels);
m_ProcessedImage2 = storePixels(m_ProcessedImage2, m_nProcessedPixels2);
m_ProcessedImage3 = storePixels(m_ProcessedImage3, m_nProcessedPixels3);
}
public Image storePixels(Image img, int nRawPixels[])
{
// Get the width and height of the image
int nWidth = img.getWidth(this);
int nHeight = img.getHeight(this);
// Calculate the number of pixels
int nNumPixels = nWidth * nHeight;
// Get the color model object
ColorModel cm = ColorModel.getRGBdefault();
// Create the MemoryImageSource object
MemoryImageSource ImageSource = new MemoryImageSource(nWidth, nHeight, cm, nRawPixels, 0, nWidth);
// Create the image to return
img = createImage(ImageSource);
// Wait for the image to be fully created
MediaTracker Tracker = new MediaTracker(this);
// Add the image to the MediaTracker object 'Tracker'
Tracker.addImage(img,0);
try
{
// Wait for the completion of the image
Tracker.waitForID(0);
}
catch(InterruptedException e)
{
}
return(img);
}
/** Code for images effects below.
* The methods are called after the pullPixels() and before the pushPixels() methods
*/
public void horzWave()
{
// Get the width and height of the image
int nWidth = m_ProcessedImage.getWidth(this);
int nHeight = m_ProcessedImage.getHeight(this);
// Point nPixels to the pixel data and create a new array
int nPixels[] = m_nProcessedPixels;
m_nProcessedPixels = new int[nWidth*nHeight];
// These values are our constants. Change them for different effects
double dNumWaves = 3.0;
double dOffset = 5.0;
double dPercent = 4.0;
// Calculate the wave frequency, wave offset, and radius
double dWaveFreq = (dNumWaves * Math.PI * 2.0)/nHeight;
double dWaveOffset = (dOffset * dNumWaves * Math.PI * 2.0)/100.0;
double dRadius = (nWidth*dPercent)/100.0;
int nIndex = 0;
for(int y=0; y= 0 && xOffset < nWidth)
m_nProcessedPixels[nIndex++] = nPixels[xOffset+nWidth*y];
else
m_nProcessedPixels[nIndex++] = 0xff808080;
xOffset++;
}
}
}
//***************************************************************************
public void makeLines()
{
// Get the width and height of the image
int nWidth = m_ProcessedImage2.getWidth(this);
int nHeight = m_ProcessedImage2.getHeight(this);
// Point nPixels to the pixel data and create a new array
int nPixels[] = m_nProcessedPixels2;
m_nProcessedPixels2 = new int[nWidth * nHeight];
// This intensity variable can be changed
int nIntensity =2;
// The first line is undefined; will make it gray
for(int nIndex=0; nIndex> 16;
int g1 = (c & 0x0000ff00) >> 8;
int b1 = (c & 0x000000ff);
// Get the pixel above current pixel
c = nPixels[nIndex - nWidth];
int r2 = (c & 0x00ff0000) >> 16;
int g2 = (c & 0x0000ff00) >> 8;
int b2 = (c & 0x000000ff);
// Get this pixel
c = nPixels[nIndex];
int r = (c & 0x00ff0000) >> 16;
int g = (c & 0x0000ff00) >> 8;
int b = (c & 0x000000ff);
r = Math.min((Math.abs(r2-r) + Math.abs(r1-r)) * nIntensity, 255);
g = Math.min((Math.abs(g2-g) + Math.abs(g1-g)) * nIntensity, 255);
b = Math.min((Math.abs(b2-b) + Math.abs(b1-b)) * nIntensity, 255);
m_nProcessedPixels2[nIndex] = 0xff000000 | (r<<16) | (g<<8) | b;
}
}
//***************************************************************************
public void makeBlurry()
{
// Get the width and height of the image
int nWidth = m_ProcessedImage3.getWidth(this);
int nHeight = m_ProcessedImage3.getHeight(this);
// Point nPixels to the pixel data and
// Create a new array
int nPixels[] = m_nProcessedPixels3;
m_nProcessedPixels3 = new int[nWidth * nHeight];
// This variable can be changed
int nPower = 5;
int nIndex = 0;
for(int y=0; y=0 && dx=0 && dy> 16 );
g += ((c & 0x0000ff00)>> 8);
b += (c & 0x000000ff);
nColors++;
}
}
}
r /= nColors;
g /= nColors;
b /= nColors;
m_nProcessedPixels3[nIndex++] = 0xff000000 | (r<<16) | (g<<8) | b;
}
}
}
//***************************************************************************
public void paint(Graphics g)
{
g.drawImage(m_NormalImage, 5,5,this);
g.drawImage(m_ProcessedImage, 5+m_NormalImage.getWidth(this)+2,5,this);
g.drawImage(m_ProcessedImage2, 5, 300,this);
g.drawImage(m_ProcessedImage3, 5+m_NormalImage.getWidth(this)+2,300,this);
}
}