/*********************************************************************************************/
/*                                                                                           */
/*  Java Image Tutorials - Tut x.x                                                           */
/*  Auth: bkenwright@xbdev.net                                                               */
/*                                                                                           */
/*  Loading an image and getting at its pixels with java 1.1 and later!                      */
/*  We simple load in an image and extract the pixel data - the data is rendered to the      */
/*  screen to show its actually done what it was suppose to.                                 */
/*  We use a very simple pixel plotting code (e.g. fillRect) to draw the image pixels - even */
/*  though there are more efficent ways.
/*                                                                                           */
/*********************************************************************************************/

import java.awt.image.*;
import java.awt.*;
import java.applet.*;



/*********************************************************************************************/
/*                                                                                           */
/* class Texture                                                                             */
/* The class takes a filename, and loads the image -either jpg or a gif.  Once loaded it     */
/* extracts the pixel information into an array, so that you can access and use the pixel    */
/* information as you need - to create those special effects or demo's that require pixel    */
/* operations.                                                                               */
/*                                                                                           */
/* The class had to extend Applet so that we could use MediaTracker - also, we needed to     */
/* pass the pointer to the parent applet so that we could load the images.  As it woudln't   */
/* cooperate with 'getImage' or 'getDocumentBase()' unless we used the main applet...hence   */
/* the reason for passing the parent pointer.                                                */
/*                                                                                           */
/*********************************************************************************************/

class Texture extends Applet
{
   public int   Width;
	 public int   Height;
   public int[] Pixels;
   
   public int Create(Applet parent, String szFileName)
   {
      // Load Image
      Image image=parent.getImage( parent.getDocumentBase(), szFileName);
      MediaTracker mediatracker = new MediaTracker(this);
      mediatracker.addImage(image, 0);
      try { mediatracker.waitForID(0); }
      catch(InterruptedException ex) {}
      // Init sprite pixels array
      Width = image.getWidth(null);
      Height = image.getHeight(null);
      Pixels = new int[Width*Height];
      // Grab pixels
      PixelGrabber pg= new PixelGrabber(image,0,0,Width,Height,Pixels,0,Width);
      try { pg.grabPixels(); }
      catch (InterruptedException e) {}
      //image=null; mediatracker=null;
      return 1;
   }// End Create(..)
   
}//End class texture

/*********************************************************************************************/
/*                                                                                           */
/*  Entry Class Point                                                                        */
/*  This is the class that is initialised and starts our applet                              */
/*                                                                                           */
/*********************************************************************************************/

public class imagepixels extends Applet
{
  Texture tex;

  public void init() 
  {
     tex = new Texture();
     tex.Create(this, "test.jpg");
  }// End of init(..)
  

  public void paint(Graphics g) 
  {
      int[] pixels = tex.Pixels;
	    PaintTexture(g, pixels, tex.Width, tex.Height );

  }// End of paint(..)

  void PaintTexture(Graphics g, int pix[], int w, int h)
  {
     for(int y=0; y<h; y++)
        for(int x=0; x<w; x++)
        {
           int col = pix[x + y*w];
           setPixel(g, x, y, col);
        }//End loops
  }//End PaintTexture(..)
  
  
  public void setPixel(Graphics g, int x, int y, int c ) 
  {
  	Color color = new Color(  (c>>16)&0xFF,
  														(c>>8)&0xFF,
  														(c)&0xFF );
    g.setColor( color );
    g.fillRect( x, y, 1, 1 );
  }// End of setPixel(..) int
	
}// End of Applet



