package GfxLib;

import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;


public class Raster 
{
  /*
   * Member variables
   */
  private Image m_image;
  private int   m_width, m_height;
  private int   m_pixel[];

  ///////////////////////// Constructors //////////////////////

  /**
   *  This constructor, which takes no arguments,
   *  allows for future extension.
   */
  public Raster()
  {
    this(0, 0);
  }

  /**
   *  This constructor creates an uninitialized
   *  Raster Object of a given size (w x h).
   */
  public Raster(int w, int h)
  {
    m_width = w;
    m_height = h;
    m_pixel = new int[w*h];
  }

  /**
   *  This constructor creates an Raster initialized
   *  with the contents of an image.
   */
  public Raster(Image img)
  {
    try 
    {
      PixelGrabber grabber = new PixelGrabber(img, 0, 0, -1, -1, true);
      if (grabber.grabPixels()) 
	{
	  m_width = grabber.getWidth();
	  m_height = grabber.getHeight();
	  m_pixel = (int []) grabber.getPixels();
	}
    } 
    catch (InterruptedException e) 
      {
      }
  }

  ////////////////////////// Methods //////////////////////////

  /**
   *  Fills a Raster with a solid color
   */
  public final void fill(Color c)
  {
    int s = size();
    int rgb = c.getRGB();
    for (int i = 0; i < s; i++)
      m_pixel[i] = rgb;
  }

  public final void fill(int c)
  {
    int s = size();
    for (int i = 0; i < s; i++)
      {
	m_pixel[i] = c;
      }
  }

  /**
   *  Gets a color from a Raster
   */
  public final Color getColor(int x, int y)
  {
    return new Color(m_pixel[y*m_width+x]);
  }

  /**
   *  Gets the height of this Raster
   */
  public final int getHeight()
  {
    return m_height;
  }

  /**
   *  Gets a pixel from a Raster
   */
  public final int getPixel(int x, int y)
  {
    return m_pixel[y*m_width+x];
  }

  /**
   *  Gets a pixel from a Raster
   */
  public final int getPixel(int offset)
  {
    return m_pixel[offset];
  }

  /**
   *  Gets the array of pixels
   */
  public final int[] getPixels()
  {
    return m_pixel;
  }

  /**
   *  Gets the width of the Raster
   */
  public final int getWidth()
  {
    return m_width;
  }
  
  /**
   *  Sets a pixel to a given color
   */
  public final boolean setColor(Color c, int x, int y)
  {
    m_pixel[y*m_width+x] = c.getRGB();
    return true;
  }

  /**
   *  Sets a pixel to a given value
   */
  public final boolean setPixel(int pix, int x, int y)
  {
    m_pixel[y*m_width+x] = pix;
    return true;
  }

  /**
   *  Sets a pixel to a given value
   */
  public final boolean setPixel(int pix, int offset)
  {
    m_pixel[pix] = offset;
    return true;
  }

  /**
   *  Returns the number of pixels in the Raster
   */
  public final int size()
  {
    return m_pixel.length;
  }

  /**
   *  Converts Rasters to Images
   */
  public final Image toImage()
  {
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    return toolkit.createImage(new MemoryImageSource(m_width, 
						     m_height,
						     m_pixel,
						     0,
						     m_width));
  }
  
}













