Permalink
Browse files

Release 0.0.1.

  • Loading branch information...
0 parents commit 0ea66cb5330abe4afdcfb92f6acee3c7d035ba12 @Munter committed Jun 27, 2012
Showing with 155 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +4 −0 README.md
  3. +101 −0 lib/index.js
  4. +26 −0 package.json
  5. +23 −0 test/histogram-test.js
  6. BIN test/images/gradient-red.png
  7. BIN test/images/gradient.png
@@ -0,0 +1 @@
+node_modules
@@ -0,0 +1,4 @@
+Histogram
+=========
+
+Provides a histogram data structure using canvas
@@ -0,0 +1,101 @@
+var Canvas = require('canvas'),
+ fs = require('fs');
+
+function getCanvasImageData(imageBuffer, callback) {
+ var canvasImage = new Canvas.Image();
+
+ canvasImage.onerror = callback;
+ canvasImage.onload = function () {
+ var context = new Canvas(canvasImage.width, canvasImage.height).getContext('2d');
+ context.drawImage(canvasImage, 0, 0);
+ callback(null, context.getImageData(0, 0, canvasImage.width, canvasImage.height).data);
+ };
+ canvasImage.src = imageBuffer;
+}
+
+function histogram(imageBuffer, callback) {
+ getCanvasImageData(imageBuffer, function (err, data) {
+ if (err) {
+ callback(err);
+ }
+
+ var hist = {
+ red: new Array(256),
+ green: new Array(256),
+ blue: new Array(256),
+ alpha: new Array(256),
+
+ colors: {
+ rgb: 0,
+ rgba: 0
+ },
+
+ pallettes: {
+ rgb: [],
+ rgba: []
+ },
+
+ greyscale: true,
+ alphachannel: false
+ },
+ red,
+ green,
+ blue,
+ alpha,
+ hexmap = {},
+ hexamap = {},
+ i;
+
+ for (i = 0; i < 256; i += 1) {
+ hist.red[i] =
+ hist.green[i] =
+ hist.blue[i] =
+ hist.alpha[i] = 0;
+ }
+
+ for (i = 0; i < data.length; i += 4) {
+ red = data[i];
+ green = data[i + 1];
+ blue = data[i + 2];
+ alpha = data[i + 3];
+
+ if (alpha < 255) {
+ hist.alphachannel = true;
+ }
+
+ if (hist.greyscale && red !== green || red !== blue) {
+ hist.greyscale = false;
+ }
+
+ hist.red[red] += 1;
+ hist.green[green] += 1;
+ hist.blue[blue] += 1;
+ hist.alpha[alpha] += 1;
+
+ var hexaString = (red * 0x1000000 + green * 0x10000 + blue * 0x100 + alpha).toString(16),
+ hexa = '#' + ('0000000'.substr(0, 8 - hexaString.length)) + hexaString,
+ hex = hexa.substr(0, 7);
+
+ if (!(hex in hexmap)) {
+ hexmap[hex] = 1;
+ hist.pallettes.rgb.push(hex);
+ hist.colors.rgb += 1;
+ } else {
+ hexmap[hex] += 1;
+ }
+
+ if (!(hexa in hexamap)) {
+ hexamap[hexa] = 1;
+ hist.pallettes.rgba.push(hexa);
+ hist.colors.rgba += 1;
+ } else {
+ hexamap[hexa] += 1;
+ }
+
+ }
+
+ callback(null, hist);
+ });
+}
+
+module.exports = histogram;
@@ -0,0 +1,26 @@
+{
+ "author": "Peter Müller <munter@fumle.dk> (http://fumle.dk)",
+ "name": "histogram",
+ "description": "Provides a histogram data structure using canvas",
+ "version": "0.0.1",
+ "homepage": "http://munter.github.com/node-histogram",
+ "repository": {
+ "url": "git://github.com/Munter/node-histogram.git"
+ },
+ "main": "lib/index.js",
+ "scripts": {
+ "test": "vows",
+ "prepublish": "npm install && npm test"
+ },
+ "dependencies": {
+ "canvas": "latest",
+ "mmmagic": "latest"
+ },
+ "devDependencies": {
+ "vows": "latest"
+ },
+ "optionalDependencies": {},
+ "engines": {
+ "node": "*"
+ }
+}
@@ -0,0 +1,23 @@
+var vows = require('vows'),
+ assert = require('assert'),
+ histogram = require('../lib/index.js');
+
+vows.describe('histogram').addBatch({
+ 'Histogram of gradient.png': {
+ topic: function () {
+ histogram(__dirname + '/images/gradient.png', this.callback);
+ },
+ 'Should be greyscale': function (histogram) {
+ assert.ok(histogram.greyscale);
+ },
+ 'Should have an alpha channel': function (histogram) {
+ assert.ok(histogram.alphachannel);
+ },
+ 'Should have 256 alpha channel colors': function (histogram) {
+ assert.equal(histogram.colors.rgba, 256);
+ },
+ 'Should have 1 rgba color': function (histogram) {
+ assert.equal(histogram.colors.rgb, 1);
+ }
+ }
+})['export'](module);
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0ea66cb

Please sign in to comment.