Rails Against The Machine

Just a mind dump. Why are you even reading this?

Thursday, 19 June 2008

 

Cleaner pixel level drawing with java

Need to change alpha transparency independently of colour so changing from index color to direct.
More bitmask fun

package services;

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.awt.image.DirectColorModel;
import java.awt.image.SampleModel;
import java.awt.image.WritableRaster;

public class ImageFactory
{

private static int width = 255;
private static int height = 255;

public static BufferedImage Image()
{
// This creates an RGBA colormodel... I think.
DirectColorModel colormodel = new DirectColorModel(32, 0xff000000, 0xff0000, 0xff00, 0xff);
int[] pixels = generatePixels(width, height);
SampleModel sample = colormodel.createCompatibleSampleModel(width, height);
DataBufferInt data = new DataBufferInt(pixels, width * height);
WritableRaster raster = WritableRaster.createWritableRaster(sample, data, new Point(0, 0));
return new BufferedImage(colormodel, raster, false, null);
}

private static int[] generatePixels(int w, int h)
{
int[] pixData = new int[width * height];
int i = 0;
for (int y = 0; y < height; y++) {
int red = (y * 255) / (height - 1);
for (int x = 0; x < width; x++) {
int green = (x * 255) / (width - 1);
int blue = 128;
int alpha = Math.max(0,x-y);
pixData[i++] = (alpha << 32) | (red << 24) |
(green << 16) | (blue << 8);
}
}
return pixData;
}
}

Comments: Post a Comment

Subscribe to Post Comments [Atom]





<< Home

Archives

July 2007   August 2007   September 2007   December 2007   January 2008   February 2008   March 2008   April 2008   June 2008   July 2008   August 2008   October 2008   November 2008   January 2009  

This page is powered by Blogger. Isn't yours?

Subscribe to Comments [Atom]