import java.lang.*;
import java.awt.*;
import java.net.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
/**
* GraphixTools - A collection of handy graphics functions
*
* This is a collection of graphics mundanes that I had to cut and
* paste one too many times. While laziness on my part I think that
* I was doing this stuff enough to warrant packaging up these goodies
* into their own proper class. Hopefully this will evolve into something
* that's worth releasing.
*
* Feature list:
* Array returns (R,G,B).
* E-Z Image Producing.
* RGB to YUV colorspace conversion.
* YUV to RGB colorspave conversion.
* Filesize and other information
* Free & Used VM Machine Memory Info
*
* You can get the source code here.
* @author Stephen Manley (smanley@eagle.uccb.ns.ca)
* @version 0.9.7 August 20th 1996
*/
public class GrafixTools
{
/**
* Here lieth an image.
*/
public Image imageobj;
/**
* Image height.
*/
public int imageHeight;
/**
* Image width.
*/
public int imageWidth;
/**
* Gives GraphixTools a preloaded image object. Image must be
* pre-loaded with the MediaTracker or bad things will happen.
* The constructor calculates all the image parameters from this
* passed image. (Dimensions, size, etc.)
* @param image The 100% loaded image
*/
public GrafixTools(Image image)
{
imageobj = image;
imageWidth = image.getWidth(null);
imageHeight = image.getHeight(null);
}
/**
* This method returns an array[][] of 8-bit red values. The dimensions are
* the array bounds ex. (320 x 200).
* @returns An array[][] of red pixel values
*/
public int[][] getRedArray()
{
int values[] = new int[imageWidth * imageHeight];
PixelGrabber grabber = new PixelGrabber(imageobj.getSource(), 0, 0, imageWidth, imageHeight, values, 0, imageWidth);
try
{
if(grabber.grabPixels() != true)
{
try
{
throw new AWTException("Grabber returned false: " + grabber.status());
}
catch (Exception e) {};
}
}
catch (InterruptedException e) {};
int r[][] = new int[imageWidth][imageHeight];
int index = 0;
for (int y = 0; y < imageHeight; ++y)
{
for (int x = 0; x < imageWidth; ++x)
{
r[x][y] = ((values[index] & 0x00ff0000) >> 16);
index++;
}
}
return r;
}
/**
* This method returns an array[][] of 8-bit green values. The dimensions are
* the array bounds ex. (320 x 200).
* @returns An array[][] of green pixel values.
*/
public int[][] getGreenArray()
{
int values[] = new int[imageWidth * imageHeight];
PixelGrabber grabber = new PixelGrabber(imageobj.getSource(), 0, 0, imageWidth, imageHeight, values, 0, imageWidth);
try
{
if(grabber.grabPixels() != true)
{
try
{
throw new AWTException("Grabber returned false: " + grabber.status());
}
catch (Exception e) {};
}
}
catch (InterruptedException e) {};
int g[][] = new int[imageWidth][imageHeight];
int index = 0;
for (int y = 0; y < imageHeight; ++y)
{
for (int x = 0; x < imageWidth; ++x)
{
g[x][y] = ((values[index] & 0x0000ff00) >> 8);
++index;
}
}
return g;
}
/**
* This method returns an array[][] of 8-bit blue values. The dimensions are
* the array bounds, ex. (320 x 200).
* @returns An array[][] of blue pixel values.
*/
public int[][] getBlueArray()
{
int values[] = new int[imageWidth * imageHeight];
PixelGrabber grabber = new PixelGrabber(imageobj.getSource(), 0, 0, imageWidth, imageHeight, values, 0, imageWidth);
try
{
if(grabber.grabPixels() != true)
{
try
{
throw new AWTException("Grabber returned false: " + grabber.status());
}
catch (Exception e) {};
}
}
catch (InterruptedException e) {};
int b[][] = new int[imageWidth][imageHeight];
int index = 0;
for (int y = 0; y < imageHeight; ++y)
{
for (int x = 0; x < imageWidth; ++x)
{
b[x][y] = (values[index] & 0x000000ff);
++index;
}
}
return b;
}
/**
* This method converts an RGB colorspace to a YUV colorspace. (YUV is more
* technically correct when referred to a YCbCr colorspace.) YCbCr was developed
* according to recommondation ITU-R BT.601 (Formerly: CCIR 601) to develop
* a world-wide digital video standard. It is commonly used in many streaming
* video and compression routines, such as MPEG
* and H.263.
*
* This routine handles dealing with 0..255 values per pixel, the more common
* PC format. The passed arrays should all have the same bounds, corresponding
* to the image dimensions.
* @param redArray An array of 0..255 red pixel values.
* @param greenArray An array of 0..255 green pixel values.
* @param blueArray An array of 0..255 blue pixel values.
* @returns Y The Y component
*/
public int[][] convertRGBtoY(int[][] redArray, int[][] greenArray, int[][] blueArray)
{
int i;
int j;
int Y[][] = new int[imageWidth][imageHeight];
for (i=0; iMPEG
* and H.263.
*
* This routine handles dealing with 0..255 values per pixel, the more common
* PC format. The passed arrays should all have the same bounds, corresponding
* to the image dimensions.
* @param redArray An array of 0..255 red pixel values.
* @param greenArray An array of 0..255 green pixel values.
* @param blueArray An array of 0..255 blue pixel values.
* @returns Cb The Cb component.
*/
public int[][] convertRGBtoCb(int[][] redArray, int[][] greenArray, int[][] blueArray)
{
int i;
int j;
int Cb[][] = new int[imageWidth][imageHeight];
for (i=0; iMPEG
* and H.263.
*
* This routine handles dealing with 0..255 values per pixel, the more common
* PC format. The passed arrays should all have the same bounds, corresponding
* to the image dimensions.
* @param redArray An array of 0..255 red pixel values.
* @param greenArray An array of 0..255 green pixel values.
* @param blueArray An array of 0..255 blue pixel values.
* @returns Cr The Cr component.
*/
public int[][] convertRGBtoCr(int[][] redArray, int[][] greenArray, int[][] blueArray)
{
int i;
int j;
int Cr[][] = new int[imageWidth][imageHeight];
for (i=0; i
* newimage = comp.createImage(new MemoryImageSource(imageWidth,imageHeight,imageArray,0,imageWidth));
* @param R The Red Values.
* @param G The Green Values.
* @param B The Blue Values.
* @param log Enable/disable progress reporting.
* @returns array A one dimensional image array.
*/
public int[] convertRGBtoArray(int[][] R, int[][] G, int[][] B, boolean log)
{
int x = 0;
int y = 0;
int index = 0;
int array[] = new int[imageWidth * imageHeight];
for (y=0; y
* newimage = comp.createImage(new MemoryImageSource(imageWidth,imageHeight,imageArray,0,imageWidth));
* @param Array The pixel values
* @param log Enable/Disable progress reporting.
* @returns array A one dimensional image array.
*/
public int[] convertGrayToArray(int[][] R, boolean log)
{
int array[] = new int[imageWidth * imageHeight];
int x = 0;
int y = 0;
int index = 0;
int G[][] = new int[imageWidth][imageHeight];
int B[][] = new int[imageWidth][imageHeight];
G = R;
B = R;
for (y=0; y