June 13, 2002
Photoshop EPS Parser

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");

Posted at June 13, 2002 08:55 PM | Further reading
Comments

Nice work !

Posted by: Morten on June 14, 2002 04:49 PM

Could you post what program you used to make the eps, maybe an example eps ?

Posted by: morten on June 14, 2002 06:04 PM

I used Adobe Photoshop. And the pictures I used for testing were only something like 100x100 pixels. I just added a link to an example plus the flash source.

Posted by: coma2mario on June 14, 2002 06:13 PM

Hi,

How about going ohter way round? I have a bitmap in flash...and I want to save it as eps? Any clues suggestios, idead?

Cheers.

Posted by: Sharad Goel on June 4, 2003 11:57 PM

I used Adobe Photoshop,it is very useful for us to deal with pohtos.

Posted by: Jsdthcy on July 10, 2004 03:10 AM
Post a comment
Name:


Email Address:


URL:


Comments:


Remember info?



Thank you!

Most Visited Entries
Sketches, Works & Source Code
Lectures
Contact
Backlog
In Love with
Powered by
Movable Type 2.661

© Copyright Mario Klingemann

Syndicate this site:
RSS 1.0 - RSS 2.0

Quasimondo @ flickr
Quasimondo @ LinkedIn
Quasimondo @ Twitter
Quasimondo @ Facebook
Quasimondo @ MySpace
Quasimondo is a Bright
Citizen of the TRansnational Republic
My other blog in german
Impressum


My family name is written Klingemann,
not Klingelmann, Klingeman, Klingaman, Kingemann,
Kindermann, Killingaman, Klingman, Klingmann, Klingonman
Klingemman, Cleangerman, Klingerman or Kleangerman

profile for Quasimondo at Stack Overflow, Q&A for professional and enthusiast programmers