Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

implemented the color-list->image function

  • Loading branch information...
commit cff671c429d7b7dd0b8ec08163a09a9fbe92c2d0 1 parent 9f28d39
Danny Yoo authored
39 image/image.js
View
@@ -566,7 +566,7 @@ EXPORTS['image->color-list'] =
1,
false, false,
function(img) {
- check(img, isImage, 'image-height', 'image', 1);
+ check(img, isImage, 'image->color-list', 'image', 1);
var width = img.getWidth(),
height = img.getHeight(),
canvas = world.Kernel.makeCanvas(width, height),
@@ -591,11 +591,32 @@ EXPORTS['image->color-list'] =
});
-// EXPORTS['color-list->image'] =
-// new PrimProc('color-list->image',
-// 1,
-// false, false,
-// function(colors) {
-// checkListOf(colors, isImage, 'image-height', 'image', 1);
-// return types.VOID;
-// });
+EXPORTS['color-list->image'] =
+ new PrimProc('color-list->image',
+ 5,
+ false, false,
+ function(listOfColors, width, height, pinholeX, pinholeY) {
+ checkListOf(listOfColors, isColor, 'color-list->image', 'image', 1);
+ check(width, isNatural, 'color-list->image', 'natural', 2);
+ check(height, isNatural, 'color-list->image', 'natural', 3);
+ check(pinholeX, isNatural, 'color-list->image', 'natural', 4);
+ check(pinholeY, isNatural, 'color-list->image', 'natural', 5);
+ var canvas = world.Kernel.makeCanvas(jsnums.toFixnum(width),
+ jsnums.toFixnum(height)),
+ ctx = canvas.getContext("2d"),
+ imageData = ctx.createImageData(jsnums.toFixnum(width),
+ jsnums.toFixnum(height)),
+ data = imageData.data,
+ aColor, i = 0;
+ while (listOfColors !== types.EMPTY) {
+ aColor = listOfColors.first;
+ data[i] = jsnums.toFixnum(types.colorRed(aColor));
+ data[i+1] = jsnums.toFixnum(types.colorGreen(aColor));
+ data[i+2] = jsnums.toFixnum(types.colorBlue(aColor));
+ data[i+3] = 255; // alpha?
+
+ i += 4;
+ listOfColors = listOfColors.rest;
+ };
+ return world.Kernel.imageDataImage(imageData);
+ });
3  image/image.rkt
View
@@ -31,4 +31,5 @@
image-width
image-height
- image->color-list)
+ image->color-list
+ color-list->image)
2  tests/moby-programs/images.rkt
View
@@ -74,4 +74,4 @@
(let ([circle-color-list (image->color-list (circle 20 'solid 'green))])
;; fixme: add tests for number of colors
- (void))
+ (color-list->image circle-color-list 40 40 0 0))
37 world/kernel.js
View
@@ -752,6 +752,40 @@ TextImage.prototype.isEqual = function(other, aUnionFind) {
this.font == other.font);
};
+//////////////////////////////////////////////////////////////////////
+
+
+var ImageDataImage = function(imageData) {
+ BaseImage.call(this, 0, 0);
+ this.imageData = imageData;
+ this.width = imageData.width;
+ this.height = imageData.height;
+};
+
+ImageDataImage.prototype = heir(BaseImage.prototype);
+
+ImageDataImage.prototype.render = function(ctx, x, y) {
+ ctx.putImageData(this.imageData, x, y);
+};
+
+ImageDataImage.prototype.getWidth = function() {
+ return this.width;
+};
+
+
+ImageDataImage.prototype.getHeight = function() {
+ return this.height;
+};
+
+ImageDataImage.prototype.isEqual = function(other, aUnionFind) {
+ return (other instanceof ImageDataImage &&
+ this.pinholeX == other.pinholeX &&
+ this.pinholeY == other.pinholeY);
+ // FIXME
+};
+
+
+
//////////////////////////////////////////////////////////////////////
@@ -1372,6 +1406,9 @@ world.Kernel.scaleImage = function(xFactor, yFactor, img) {
world.Kernel.textImage = function(msg, size, color) {
return new TextImage(msg, size, color);
};
+world.Kernel.imageDataImage = function(imageData) {
+ return new ImageDataImage(imageData);
+};
world.Kernel.fileImage = function(path, rawImage) {
return FileImage.makeInstance(path, rawImage);
};
Please sign in to comment.
Something went wrong with that request. Please try again.