Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.

Commit

Permalink
Code Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelerz committed Mar 29, 2015
1 parent e949bfa commit 2361282
Show file tree
Hide file tree
Showing 23 changed files with 101 additions and 54 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

[*.yml]
indent_style = space
indent_size = 2
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ CVS/
.svn
*~
.com.apple.timemachine.supported
.idea
docs/
.idea/
docs/
3 changes: 1 addition & 2 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ docs/
examples/
screwdriver/
Makefile
.idea
docs/
.idea/
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ script:

after_script:
- cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js
- CODECLIMATE_REPO_TOKEN=46b538e32a131ea7637cbbc30f08237cef088467c9b9ec25419a961962fa96b3 ./node_modules/codeclimate-test-reporter/bin/codeclimate.js < ./coverage/lcov.info

notifications:
webhooks:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ CHANGELOG
=========

v0.10.0
* General cleanup
* Add support for Node 0.12
* Add support for IO.js

Expand Down
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 Yahoo! Inc.
Copyright (c) 2014-2015 Yahoo! Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ The following third-party libraries are used by this module:

###Dev-Dependencies
* chai: http://chaijs.com
* codeclimate-test-reporter: https://github.com/codeclimate/javascript-test-reporter
* coveralls: https://github.com/cainus/node-coveralls
* istanbul: https://github.com/gotwarlost/istanbul
* mocha: https://github.com/visionmedia/mocha
Expand All @@ -190,4 +191,4 @@ The following third-party libraries are used by this module:

The MIT License

Copyright 2014 Yahoo Inc.
Copyright 2014-2015 Yahoo Inc.
Binary file modified examples/export.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/export_lightness.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/export_sepia.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions examples/loadFromMemory.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright 2014, Yahoo! Inc.
// Copyright 2014-2015, Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.

var PNGImage = require('../index'), fs = require('fs');
var PNGImage = require('../index'),
fs = require('fs');

var blob = fs.readFileSync(__dirname + '/squirrel.png');

Expand Down
53 changes: 29 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright 2014, Yahoo! Inc.
// Copyright 2014-2015, Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.

var fs = require('fs'), _ = require('underscore'),

PNG = require('pngjs').PNG,

pixel = require('./lib/pixel'), conversion = require('./lib/conversion'), filters = require('./lib/filters'),

var fs = require('fs'),
_ = require('underscore'),
PNG = require('pngjs').PNG,
pixel = require('./lib/pixel'),
conversion = require('./lib/conversion'),
filters = require('./lib/filters'),
streamBuffers = require("stream-buffers");

/**
Expand Down Expand Up @@ -69,7 +69,8 @@ PNGImage.setFilter = function (key, fn) {
*/
PNGImage.createImage = function (width, height) {
var image = new PNG({
width: width, height: height
width: width,
height: height
});
return new PNGImage(image);
};
Expand Down Expand Up @@ -98,10 +99,10 @@ PNGImage.copyImage = function (image) {
* @return {PNGImage}
*/
PNGImage.readImage = function (filename, fn) {
var image = new PNG(), resultImage = new PNGImage(image);
var image = new PNG(),
resultImage = new PNGImage(image);

fn = fn || function () {
};
fn = fn || function () {};

fs.createReadStream(filename).once('error', function(err) {
fn(err, undefined);
Expand All @@ -126,10 +127,10 @@ PNGImage.readImage = function (filename, fn) {
* @return {PNGImage}
*/
PNGImage.loadImage = function (blob, fn) {
var image = new PNG(), resultImage = new PNGImage(image);
var image = new PNG(),
resultImage = new PNGImage(image);

fn = fn || function () {
};
fn = fn || function () {};

image.once('error', function (err) {
fn(err, resultImage);
Expand Down Expand Up @@ -241,7 +242,12 @@ PNGImage.prototype = {
* @param {float} [color.opacity] Opacity of color
*/
fillRect: function (x, y, width, height, color) {
var i, iLen = x + width, j, jLen = y + height, index;

var i,
iLen = x + width,
j,
jLen = y + height,
index;

for (i = x; i < iLen; i++) {
for (j = y; j < jLen; j++) {
Expand All @@ -262,7 +268,8 @@ PNGImage.prototype = {
*/
applyFilters: function (filters, returnResult) {

var image, newFilters;
var image,
newFilters;

// Convert to array
if (_.isString(filters)) {
Expand All @@ -273,7 +280,7 @@ PNGImage.prototype = {

// Format array as needed by the function
newFilters = [];
_.each(filters, function (filter) {
(filters || []).forEach(function (filter) {

if (_.isString(filter)) {
newFilters.push({key: filter, options: {}});
Expand All @@ -286,7 +293,7 @@ PNGImage.prototype = {

// Process filters
image = this;
_.each(filters, function (filter) {
(filters || []).forEach(function (filter) {

var currentFilter = PNGImage.filters[filter.key];

Expand All @@ -299,7 +306,7 @@ PNGImage.prototype = {

image = currentFilter(this, filter.options);

}, this);
}.bind(this));

// Overwrite current image, or just returning it
if (!returnResult) {
Expand Down Expand Up @@ -332,8 +339,7 @@ PNGImage.prototype = {
*/
writeImage: function (filename, fn) {

fn = fn || function () {
};
fn = fn || function () {};

this._image.pack().pipe(fs.createWriteStream(filename)).once('close', function () {
this._image.removeListener('error', fn);
Expand All @@ -356,8 +362,7 @@ PNGImage.prototype = {
initialSize: (100 * 1024), incrementAmount: (10 * 1024)
});

fn = fn || function () {
};
fn = fn || function () {};

this._image.pack().pipe(writeBuffer).once('close', function () {
this._image.removeListener('error', fn);
Expand All @@ -377,7 +382,7 @@ _.extend(PNGImage.prototype, conversion);


// Adds all standard filters
_.each(_.keys(filters), function (key) {
_.keys(filters).forEach(function (key) {
PNGImage.setFilter(key, filters[key]);
});

Expand Down
30 changes: 25 additions & 5 deletions lib/conversion.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014 Yahoo! Inc.
// Copyright 2014-2015 Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.

/**
Expand All @@ -18,7 +18,17 @@ module.exports = {
* @return {int}
*/
getBlurPixelAtIndex: function (idx, funcName) {
var colors = 0, colorCounter = 0, fn, width = this._image.width, height = this._image.height, dim = width * height, spaceOnRight, spaceOnLeft, spaceOnTop, spaceOnBottom;

var colors = 0,
colorCounter = 0,
fn,
width = this._image.width,
height = this._image.height,
dim = width * height,
spaceOnRight,
spaceOnLeft,
spaceOnTop,
spaceOnBottom;

funcName = funcName || "getLuminosityAtIndex";

Expand Down Expand Up @@ -87,7 +97,13 @@ module.exports = {
* @return {object} YIQ-value
*/
getYIQAtIndex: function (idx) {
var r = this.getRed(idx), g = this.getGreen(idx), b = this.getBlue(idx), y, i, q;

var r = this.getRed(idx),
g = this.getGreen(idx),
b = this.getBlue(idx),
y,
i,
q;

y = this.getLumaAtIndex(idx);
i = Math.floor((0.595716 * r) - (0.274453 * g) - (0.321263 * b));
Expand Down Expand Up @@ -147,7 +163,9 @@ module.exports = {
* @return {object} Color
*/
getSepiaAtIndex: function (idx) {
var r = this.getRed(idx), g = this.getGreen(idx), b = this.getBlue(idx);
var r = this.getRed(idx),
g = this.getGreen(idx),
b = this.getBlue(idx);

r = Math.floor((0.393 * r) + (0.769 * g) + (0.189 * b));
g = Math.floor((0.349 * r) + (0.686 * g) + (0.168 * b));
Expand Down Expand Up @@ -206,7 +224,9 @@ module.exports = {
* @return {int} Lightness
*/
getLightnessAtIndex: function (idx) {
var r = this.getRed(idx), g = this.getGreen(idx), b = this.getBlue(idx);
var r = this.getRed(idx),
g = this.getGreen(idx),
b = this.getBlue(idx);

return Math.floor((Math.max(r, g, b) + Math.min(r, g, b)) / 2);
},
Expand Down
2 changes: 1 addition & 1 deletion lib/filters.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014 Yahoo! Inc.
// Copyright 2014-2015 Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.

var blur = require('./filters/blur');
Expand Down
6 changes: 4 additions & 2 deletions lib/filters/blur.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014 Yahoo! Inc.
// Copyright 2014-2015 Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.

/**
Expand All @@ -13,7 +13,9 @@
*/
module.exports = function (source, destination, options) {

var dim = source.getWidth() * source.getHeight(), idx, value;
var dim = source.getWidth() * source.getHeight(),
idx,
value;

for (idx = 0; idx < dim; idx++) {
value = source.getBlurPixelAtIndex(idx, options.funcName);
Expand Down
6 changes: 4 additions & 2 deletions lib/filters/grayScale.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014 Yahoo! Inc.
// Copyright 2014-2015 Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.

/**
Expand All @@ -12,7 +12,9 @@
*/
module.exports = function (source, destination, options) {

var dim = source.getWidth() * source.getHeight(), idx, value;
var dim = source.getWidth() * source.getHeight(),
idx,
value;

for (idx = 0; idx < dim; idx++) {
value = source.getGrayScaleAtIndex(idx);
Expand Down
6 changes: 4 additions & 2 deletions lib/filters/lightness.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014 Yahoo! Inc.
// Copyright 2014-2015 Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.

/**
Expand All @@ -12,7 +12,9 @@
*/
module.exports = function (source, destination, options) {

var dim = source.getWidth() * source.getHeight(), idx, value;
var dim = source.getWidth() * source.getHeight(),
idx,
value;

for (idx = 0; idx < dim; idx++) {
value = source.getLightnessAtIndex(idx);
Expand Down
6 changes: 4 additions & 2 deletions lib/filters/luma.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014 Yahoo! Inc.
// Copyright 2014-2015 Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.

/**
Expand All @@ -12,7 +12,9 @@
*/
module.exports = function (source, destination, options) {

var dim = source.getWidth() * source.getHeight(), idx, value;
var dim = source.getWidth() * source.getHeight(),
idx,
value;

for (idx = 0; idx < dim; idx++) {
value = source.getLumaAtIndex(idx);
Expand Down
6 changes: 4 additions & 2 deletions lib/filters/luminosity.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014 Yahoo! Inc.
// Copyright 2014-2015 Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.

/**
Expand All @@ -12,7 +12,9 @@
*/
module.exports = function (source, destination, options) {

var dim = source.getWidth() * source.getHeight(), idx, value;
var dim = source.getWidth() * source.getHeight(),
idx,
value;

for (idx = 0; idx < dim; idx++) {
value = source.getLuminosityAtIndex(idx);
Expand Down
6 changes: 4 additions & 2 deletions lib/filters/sepia.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014 Yahoo! Inc.
// Copyright 2014-2015 Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.

/**
Expand All @@ -12,7 +12,9 @@
*/
module.exports = function (source, destination, options) {

var dim = source.getWidth() * source.getHeight(), idx, value;
var dim = source.getWidth() * source.getHeight(),
idx,
value;

for (idx = 0; idx < dim; idx++) {
value = source.getSepiaAtIndex(idx);
Expand Down
2 changes: 1 addition & 1 deletion lib/pixel.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2014 Yahoo! Inc.
// Copyright 2014-2015 Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.

/**
Expand Down
Loading

0 comments on commit 2361282

Please sign in to comment.