/******************************************************************
* Copyright (C) 2002-2005 Andrew Girow. All rights reserved. *
* ---------------------------------------------------------------*
* This software is published under the terms of the TinyLine *
* License, a copy of which has been included with this *
* distribution in the TINYLINE_LICENSE.TXT file. *
* *
* For more information on the TinyLine, *
* please see <http://www.tinyline.com/>. *
*****************************************************************/
package net.xoetrope.optional.svg;
import com.tinyline.svg.SVGImageProducer;
import com.tinyline.svg.SVGRaster;
import com.tinyline.tiny2d.TinyPixbuf;
import java.awt.image.ColorModel;
import java.awt.image.ImageConsumer;
import java.awt.image.ImageProducer;
/**
* The <tt>PPSVGImageProducer</tt> is the J2ME Personal Profile
* implementation of the SVGImageProducer interface.
* <p>
* @author (C) Andrew Girow
* @version 1.9
* <p>
*/
public class XSVGImageProducer implements SVGImageProducer, ImageProducer
{
/** The Color Model for this SVGRaster */
private ColorModel model;
/** The ImageConsumer associated with this PPSVGRaster */
private ImageConsumer theConsumer;
private SVGRaster raster;
/**
* Constructs a new <tt>PPSVGRaster</tt>.
*/
public XSVGImageProducer(SVGRaster renderer)
{
raster = renderer;
}
/**
* Sets the ImageConsumer for this renderer
* @param consumer the specified <code>ImageConsumer</code>
*/
public void setConsumer(ImageConsumer consumer)
{
theConsumer = consumer;
}
/**
* Returns true if this renderer has a consumer; otherwise
* returns false
*/
public boolean hasConsumer()
{
return (theConsumer != null);
}
/**
* Sends a rectangular region of the buffer of pixels to any
* ImageConsumers that are currently interested in the data for
* this image and notify them that a frame is complete.
*/
public void sendPixels()
{
TinyPixbuf pixbuf = raster.getPixelBuffer();
int pixelscan = pixbuf.width;
int pixeloffset = pixelscan * raster.clipRect.ymin + raster.clipRect.xmin;
theConsumer.setPixels(raster.clipRect.xmin, raster.clipRect.ymin,
raster.clipRect.xmax - raster.clipRect.xmin, raster.clipRect.ymax - raster.clipRect.ymin, model,
pixbuf.pixels32, pixeloffset, pixelscan);
}
/**
* Sends pixel data to the ImageConsumer
*/
public void imageComplete()
{
theConsumer.imageComplete(ImageConsumer.SINGLEFRAMEDONE); // 2
}
// end of SVGPlayer
/**
* Adds an ImageConsumer to the list of consumers interested in
* data for this image.
* @param imageconsumer the specified <code>ImageConsumer</code>
*/
public synchronized void addConsumer(ImageConsumer imageconsumer)
{
theConsumer = imageconsumer;
}
/**
* Determines if an ImageConsumer is on the list of consumers currently
* interested in data for this image.
* @param imageconsumer the specified <code>ImageConsumer</code>
* @return <code>true</code> if the <code>ImageConsumer</code>
* is on the list; <code>false</code> otherwise.
*/
public boolean isConsumer(ImageConsumer imageconsumer)
{
return theConsumer == imageconsumer;
}
/**
* Removes an ImageConsumer from the list of consumers interested in
* data for this image.
* @param imageconsumer the specified <code>ImageConsumer</code>
*/
public synchronized void removeConsumer(ImageConsumer imageconsumer)
{
if(theConsumer == imageconsumer)
theConsumer = null;
}
/**
* Requests that a given ImageConsumer have the image data delivered
* one more time in top-down, left-right order.
* @param imageconsumer the specified <code>ImageConsumer</code>
*/
public void requestTopDownLeftRightResend(ImageConsumer imageconsumer)
{
}
/**
* Adds an ImageConsumer to the list of consumers interested in
* data for this image and immediately starts delivery of the
* image data through the ImageConsumer interface.
* @param imageconsumer the specified <code>ImageConsumer</code>
* image data through the ImageConsumer interface.
*/
public void startProduction(ImageConsumer imageconsumer)
{
addConsumer(imageconsumer);
if ( theConsumer == null)
return;
initConsumer();
raster.invalidate();
raster.clearRect(raster.clipRect);
sendPixels();
theConsumer.imageComplete(ImageConsumer.SINGLEFRAMEDONE); // 2
}
/**
* Sets an ImageProducer object of this <tt>PPSVGRaster</tt>
* which is used to produce data for an Image object
*
* @param colormodel The ColorModel.
*
*/
public synchronized void setColorModel(ColorModel colormodel)
{
model = colormodel;
}
/** Inits the ImageConsumer */
private final void initConsumer()
{
if(theConsumer == null) return;
TinyPixbuf pixbuf = raster.getPixelBuffer();
if (( pixbuf.width > 0 ) && ( pixbuf.height > 0 ))
theConsumer.setDimensions(pixbuf.width, pixbuf.height);
theConsumer.setColorModel(model);
int hints = ImageConsumer.TOPDOWNLEFTRIGHT | ImageConsumer.SINGLEPASS;
theConsumer.setHints(hints);
}
}