All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ajax4jsf.framework.resource.Png8Renderer Maven / Gradle / Ivy

Go to download

Ajax4jsf is an open source extension to the JavaServer Faces standard that adds AJAX capability to JSF applications without requiring the writing of any JavaScript.

The newest version!
/**
 * Licensed under the Common Development and Distribution License,
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.sun.com/cddl/
 *   
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package org.ajax4jsf.framework.resource;

import java.awt.image.BufferedImage;
import java.awt.image.IndexColorModel;
import java.awt.image.RenderedImage;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;

/**
 * @author shura (latest modification by $Author: alexsmirnov $)
 * @version $Revision: 1.3 $ $Date: 2006/11/15 14:58:28 $
 * 
 */
public class Png8Renderer extends ImageRenderer {

	/**
	 * Default web safe colors color model
	 */
	public static IndexColorModel webColorModel;

	// Build web safe 6x6x6 cube color model.
	static {
		byte[] webLevels = { 0, 51, 102, (byte) 153, (byte) 204, (byte) 255 };
		byte[] r = new byte[256];
		byte[] g = new byte[256];
		byte[] b = new byte[256];
		r[0] = g[0] = b[0] = 0;
		for (int i = 0; i < webLevels.length; i++) {
			for (int j = 0; j < webLevels.length; j++) {
				for (int k = 0; k < webLevels.length; k++) {
					int colorNum = i * webLevels.length * webLevels.length + j
							* webLevels.length + k + 1;
					r[colorNum] = webLevels[i];
					g[colorNum] = webLevels[j];
					b[colorNum] = webLevels[k];
				}
			}
		}
		webColorModel = new IndexColorModel(8, webLevels.length
				* webLevels.length * webLevels.length + 1, r, g, b, 0);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.ajax4jsf.framework.resource.ImageRenderer#sendImage(org.ajax4jsf.framework.resource.ResourceContext,
	 *      java.awt.image.RenderedImage)
	 */
	public void sendImage(ResourceContext context, RenderedImage image)
			throws IOException {
		OutputStream outputStream = context.getOutputStream();
		// PNGEncodeParam param = new PNGEncodeParam.Palette();
		// param.setBitDepth(8);
		// param.setInterlacing(true);
		// String[] encoders = ImageCodec.getEncoderNames(image,null);
		// ImageEncoder encoder =
		// ImageCodec.createImageEncoder("png",context.getOutputStream(),param);
		// encoder.encode(image);
		try {
			ImageIO.write(image, "png", outputStream);
			outputStream.flush();
			outputStream.close();

		} catch (Exception e) {
			// IE can unexpected close connection
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.ajax4jsf.framework.resource.ImageRenderer#getImageType()
	 */
	public int getImageType() {
		return BufferedImage.TYPE_BYTE_INDEXED;
		// return BufferedImage.TYPE_INT_ARGB;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.ajax4jsf.framework.resource.ImageRenderer#createImage(int, int)
	 */
	public BufferedImage createImage(int width, int height) {

		// return new BufferedImage(width,height,getImageType());//,colorModel);
		return new BufferedImage(width, height, getImageType(), webColorModel);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.ajax4jsf.framework.resource.ResourceRenderer#getContentType()
	 */
	public String getContentType() {
		// TODO Auto-generated method stub
		return "image/png";
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy