Inspired by a question on Flashcoders if it is possible to get the color values of bitmaps into flash I gave this piece a try. It is able to load bitmaps into flash, render them and retrieve the pixel color information. The bitmaps have to be saved as Adobe Photoshop EPS without preview image in ASCII format. This format was the only one I found that is pure text and thus can be read with the XML object.
Of course this does not run at rocket speed, but it can even load greyscale or CMYK images.
Download the source and an example bitmap here
Movieclip.prototype.getTop = function() {
var i;
var m = -1;
for (i in this) {
if (typeof (this[i]) == "movieclip") {
m = Math.max(this[i].getDepth(), m);
}
}
return (m+1);
};
function EPS() {
this.canvas = _root.createEmptyMovieClip("cv", _root.getTop());
//this.canvas._xscale = this.canvas._yscale=500;
this.bitsPerChannel = 0;
this.channels = 0;
this.paintHandle = 0;
this.raw = new XML();
this.raw.parent = this;
this.raw.onData = function(src) {
this.parent.success = this.parent.parseEPS(src);
this.parent.onLoad();
};
}
EPS.prototype.load = function(filename) {
this.d = [];
this.w = 0;
this.h = 0;
this.bitsPerChannel = 0;
this.channels = 0;
this.raw.load(filename);
};
EPS.prototype.drawPixel = function(x, y, c) {
this.canvas.lineStyle();
this.canvas.beginFill(c, 100);
this.canvas.moveTo(x, y);
this.canvas.lineTo(x+1, y);
this.canvas.lineTo(x+1, y+1);
this.canvas.lineTo(x, y+1);
this.canvas.lineTo(x, y);
this.canvas.endFill();
};
EPS.prototype.getColorRGB = function(x, y) {
var r = (y*this.w*4+x)*2;
var g = r+this.w*6;
var b = r+this.w*4;
var ro = (r >> 6);
var go = (g >> 6);
var bo = (b >> 6);
//return {r:parseInt("0x" + this.d[ro].substr(r%64, 2)),g:parseInt("0x" + this.d[go].substr(g%64, 2)),b:parseInt("0x" + this.d[bo].substr(b%64, 2))}
return parseInt("0x" add this.d[ro].substr(r%64, 2) add this.d[go].substr(g%64, 2) add this.d[bo].substr(b%64, 2));
};
EPS.prototype.getColorGrey = function(x, y) {
var g = (y*this.w+x)*2;
var go = (g >> 6);
g = this.d[go].substr(g%64, 2);
return parseInt("0x" add g add g add g);
};
EPS.prototype.getColorCMYK = function(x, y) {
var r = (y*this.w*5+x)*2;
var g = r+this.w*8;
var b = r+this.w*4;
var ro = (r >> 6);
var bo = (b >> 6);
var go = (g >> 6);
r = (parseInt("0x"+this.d[ro].substr(r%64, 2)) ^ 255) << 16;
g = (parseInt("0x"+this.d[go].substr(g%64, 2))) << 8;
b = (parseInt("0x"+this.d[bo].substr(b%64, 2)) ^ 255);
return r | g | b;
};
EPS.prototype.refresh = function() {
this.currentY = 0;
clearInterval(this.paintHandle);
this.paintHandle = setInterval(this, "paint", 1);
};
EPS.prototype.onLoad = function() {
this.refresh();
};
EPS.prototype.paint = function() {
clearInterval(this.paintHandle);
var y = 2;
while (y--) {
var x = this.w;
while (x--) {
this.drawPixel(x, this.currentY, this.getColor(x, this.currentY));
}
this.currentY++;
if (this.currentY == this.h) {
y = 0;
return;
}
}
this.paintHandle = setInterval(this, "paint", 1);
};
EPS.prototype.parseEPS = function(eps) {
var a = eps.indexOf("%ImageData: ")+12;
var imageData = eps.substring(a, eps.indexOf("\n", a)-1).split(" ");
a = eps.indexOf("\n"+"beginimage")+13;
this.d = eps.substring(a, eps.indexOf("%%EndBinary", a)-1).split("\n");
this.w = parseInt(imageData[0]);
this.h = parseInt(imageData[1]);
this.bitsPerChannel = parseInt(imageData[2]);
this.channels = parseInt(imageData[3]);
if (this.channels == 1) {
this.getColor = this.getColorGrey;
}
if (this.channels == 3) {
this.getColor = this.getColorRGB;
}
if (this.channels == 4) {
this.getColor = this.getColorCMYK;
}
return true;
};
test = new EPS();
test.load("test.eps");



