diff --git a/.eslintrc b/.eslintrc index 7577a09..86e3575 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,35 +1,19 @@ { "rules": { - "no-console": 0, - "indent": [ - 2, - 2 - ], - "quotes": [ - 2, - "single" - ], - "linebreak-style": [ - 2, - "unix" - ], - "semi": [ - 2, - "always" - ] + "comma-dangle": ["error", "always-multiline"], + "no-console": "off", + "indent": [ "error", 2 ], + "quotes": [ "error", "single" ], + "semi": ["error", "always"], + "linebreak-style": [ "error", "unix" ] }, "env": { "es6": true, "node": true, - "browser": true + "mocha": true, + "jasmine": true }, "globals": { - "describe": false, - "it": false, - "beforeEach": false, - "afterEach": false, - "before": false, - "after": false }, "ecmaFeatures": { "modules": true, diff --git a/.gitignore b/.gitignore index b02a2a7..41ae928 100644 --- a/.gitignore +++ b/.gitignore @@ -63,3 +63,6 @@ Session.vim # auto-generated tag files tags + +# new bitmaps +new*.bmp diff --git a/assets/marbles.bmp b/assets/marbles.bmp new file mode 100644 index 0000000..4e11052 Binary files /dev/null and b/assets/marbles.bmp differ diff --git a/assets/test.txt b/assets/test.txt new file mode 100644 index 0000000..58c9bdf --- /dev/null +++ b/assets/test.txt @@ -0,0 +1 @@ +111 diff --git a/assets/testimage-actuallyjpg.bmp b/assets/testimage-actuallyjpg.bmp new file mode 100644 index 0000000..37adaad Binary files /dev/null and b/assets/testimage-actuallyjpg.bmp differ diff --git a/bitmap-nassarah/.eslintrc b/bitmap-nassarah/.eslintrc new file mode 100644 index 0000000..86e3575 --- /dev/null +++ b/bitmap-nassarah/.eslintrc @@ -0,0 +1,24 @@ +{ + "rules": { + "comma-dangle": ["error", "always-multiline"], + "no-console": "off", + "indent": [ "error", 2 ], + "quotes": [ "error", "single" ], + "semi": ["error", "always"], + "linebreak-style": [ "error", "unix" ] + }, + "env": { + "es6": true, + "node": true, + "mocha": true, + "jasmine": true + }, + "globals": { + }, + "ecmaFeatures": { + "modules": true, + "experimentalObjectRestSpread": true, + "impliedStrict": true + }, + "extends": "eslint:recommended" +} diff --git a/bitmap-nassarah/.gitignore b/bitmap-nassarah/.gitignore new file mode 100644 index 0000000..ff330c9 --- /dev/null +++ b/bitmap-nassarah/.gitignore @@ -0,0 +1,73 @@ +# Created by https://www.gitignore.io/api/osx,linux,node,vim + +### OSX ### +.DS_Store +.AppleDouble +.LSOverride + +### Linux ### +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + + +### Node ### +# Logs +logs +*.log +npm-debug.log* + +# Runtime data +pids +*.pid +*.seed + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules +jspm_packages + +# Optional npm cache directory +.npm + +# Optional REPL history +.node_repl_history + +### Vim ### +# swap +[._]*.s[a-w][a-z] +[._]s[a-w][a-z] +# session +Session.vim +# temporary +.netrwhist +*~ + +### Existential Dread ### +inevitable_death +ever_present_cowardice +pain_of_wakefulness + +# auto-generated tag files +tags + +# new bitmaps +new*.bmp diff --git a/bitmap-nassarah/README.md b/bitmap-nassarah/README.md new file mode 100644 index 0000000..52c744e --- /dev/null +++ b/bitmap-nassarah/README.md @@ -0,0 +1,15 @@ +# Sarah and Nassir's Bitmap Lab + +## Reads a bitmap file, performs 3 transforms, and outputs a new transformed bitmap + +### 3 Transforms: + 1. Invert Transform + 2. Gray Transform + 3. RGB Transform + +### Using our program: + 1. Run 'node index.js [bitmap filepath] [transform-method]' from main project directory + 2. Transform method flags: + a. invert + b. gray + c. rgb diff --git a/bitmap-nassarah/gulpfile.js b/bitmap-nassarah/gulpfile.js new file mode 100644 index 0000000..347b567 --- /dev/null +++ b/bitmap-nassarah/gulpfile.js @@ -0,0 +1,29 @@ +'use strict'; + +const gulp = require('gulp'); +const eslint = require('gulp-eslint'); +const mocha = require('gulp-mocha'); + +gulp.task('hello', function(){ + console.log('hello'); +}); + +gulp.watch('***/*.js', ['hello']); + +gulp.task('lint', function () { + return gulp.src(['**/*.js','!node_modules/**']) + .pipe(eslint()) + .pipe(eslint.format()) + .pipe(eslint.failAfterError()); +}); + +gulp.task('test', function(){ + gulp.src('./test/*-test.js', {read: false}) + .pipe(mocha({reporter: 'nyan'})); +}); + +gulp.task('dev', function(){ + gulp.watch(['**/*.js','!node_modules/**'], ['test', 'lint']); +}); + +gulp.task('default', ['dev']); diff --git a/bitmap-nassarah/index.js b/bitmap-nassarah/index.js new file mode 100644 index 0000000..c4869a1 --- /dev/null +++ b/bitmap-nassarah/index.js @@ -0,0 +1,28 @@ +'use strict'; + +const bitmapReader = require('./lib/bitmap-read'); +const bitmapConstructor = require('./model/bitmap-constructor'); +const bitmapWriter = require('./lib/bitmap-write'); +const programSelect = process.argv[3]; +const fileSelect = process.argv[2]; + + +function runProgram(){ + if (!programSelect || !fileSelect) { + console.error('Error!! Program requires a filename and transform\nUse form \'node index.js filename.bmp transform\' \nThree transforms are available: invert, gray, rgb'); + } else { + bitmapReader(fileSelect, function(err, buffer){ + if (err) throw console.error('Error! Not a valid file.\nFiles much be in form filename.bmp, and must be in the assets directory.'); + bitmapConstructor(buffer, fileSelect, function(err, object){ + if (err) throw console.error('Error! bitmapConstructor is not receiving a valid buffer object.'); + var fileHeaders = ['BM', 'BA', 'CI', 'CP', 'IC', 'PT']; + if (fileHeaders.indexOf(object.isBitmap) === -1) throw console.error('Error! File does not appear to be a valid bitmap file.'); + bitmapWriter(object, programSelect, function(err, newFilePath){ + console.log('New file saved as ' + newFilePath + ' in assets.'); + }); + }); + }); + } +} + +runProgram(); diff --git a/bitmap-nassarah/lib/bitmap-read.js b/bitmap-nassarah/lib/bitmap-read.js new file mode 100644 index 0000000..99a2c91 --- /dev/null +++ b/bitmap-nassarah/lib/bitmap-read.js @@ -0,0 +1,10 @@ +'use strict'; + +const fs = require('fs'); + +module.exports = function bitmapRead(pathName, callback) { + fs.readFile(`${__dirname}/../../assets/${pathName}`, function(err, buffer) { + if (err) return callback(err); + callback(null, buffer); + }); +}; diff --git a/bitmap-nassarah/lib/bitmap-write.js b/bitmap-nassarah/lib/bitmap-write.js new file mode 100644 index 0000000..0a10a31 --- /dev/null +++ b/bitmap-nassarah/lib/bitmap-write.js @@ -0,0 +1,41 @@ +'use strict'; + +const fs = require('fs'); +const EE = require('events'); +const myEE = new EE(); + +const invertTransform = require('./invert-transform.js'); +const grayTransform = require('./gray-transform.js'); +const rgbTransform = require('./rgb-transform.js'); + +module.exports = function bitmapWriter(object, programSelect, callback) { + var colorArray = object.colorArray; + var pathName = object.pathName; + var buffer = object.wholeBuffer; + + myEE.on('first', function(){ + if (colorArray[0] === undefined) colorArray = object.pixelArray; + myEE.emit('second'); + }); + + myEE.on('second', function(){ + if (programSelect === 'invert') { + invertTransform(colorArray, function(){}); + } else if (programSelect === 'gray') { + grayTransform(colorArray, function(){}); + } else if (programSelect === 'rgb') { + rgbTransform(colorArray, function(){}); + } else { + console.error('Error! Transform if/else statement running to end'); + } + myEE.emit('third'); + }); + + myEE.on('third', function(){ + var newPathName = `new${pathName}`; + fs.writeFile(`../assets/new${pathName}`, buffer, function() { + callback(null, newPathName); + }); + }); + myEE.emit('first'); +}; diff --git a/bitmap-nassarah/lib/gray-transform.js b/bitmap-nassarah/lib/gray-transform.js new file mode 100644 index 0000000..cec510a --- /dev/null +++ b/bitmap-nassarah/lib/gray-transform.js @@ -0,0 +1,13 @@ +'use strict'; + +module.exports = function grayscaleBitmap(colorArray, callback) { + for (var i = 0; i < colorArray.length; i += 4) { + var arrayChunk = colorArray.slice([i], [i+4]); + var avg = (arrayChunk[0] + arrayChunk[1] + arrayChunk[2] + arrayChunk[3])/4; + arrayChunk[0] = avg; //blue + arrayChunk[1] = avg; //green + arrayChunk[2] = avg; //red + arrayChunk[3] = avg; //alpha + } + callback(arrayChunk); +}; diff --git a/bitmap-nassarah/lib/invert-transform.js b/bitmap-nassarah/lib/invert-transform.js new file mode 100644 index 0000000..647917f --- /dev/null +++ b/bitmap-nassarah/lib/invert-transform.js @@ -0,0 +1,13 @@ +'use strict'; + + +module.exports = function invertBitmap(colorArray, callback) { + for (var i = 0; i < colorArray.length; i += 4) { + var arrayChunk = colorArray.slice([i], [i+4]); + arrayChunk[0] = 255 - arrayChunk[0]; //blue + arrayChunk[1] = 255 - arrayChunk[1]; //green + arrayChunk[2] = 255 - arrayChunk[2]; //red + arrayChunk[3] = 255 - arrayChunk[3]; //alpha + } + callback(arrayChunk); +}; diff --git a/bitmap-nassarah/lib/rgb-transform.js b/bitmap-nassarah/lib/rgb-transform.js new file mode 100644 index 0000000..27f02d6 --- /dev/null +++ b/bitmap-nassarah/lib/rgb-transform.js @@ -0,0 +1,13 @@ +'use strict'; + + +module.exports = function rgbBitmap(colorArray, callback) { + for (var i = 0; i < colorArray.length; i += 4) { + var arrayChunk = colorArray.slice([i], [i+4]); + arrayChunk[0] = arrayChunk[0]; //blue + arrayChunk[1] = 255 * arrayChunk[1]; //green + arrayChunk[2] = arrayChunk[2]; //red + arrayChunk[3] = arrayChunk[3]; //alpha + } + callback(arrayChunk); +}; diff --git a/bitmap-nassarah/model/bitmap-constructor.js b/bitmap-nassarah/model/bitmap-constructor.js new file mode 100644 index 0000000..1a40a2c --- /dev/null +++ b/bitmap-nassarah/model/bitmap-constructor.js @@ -0,0 +1,31 @@ +'use strict'; + +const os = require('os'); + +module.exports = function buildObject(bufferIn, pathName, callback) { + var bitmapObject = new Bitmap(bufferIn, pathName); + callback(null, bitmapObject); +}; + +function Bitmap(buffer, pathName) { + this.pathName = pathName; + this.isBitmap = buffer.toString('utf8', 0, 2); + this.wholeBuffer = buffer; + this.header = buffer.toString('utf8', 0, 15); + this.headerLength = checkEndian(buffer, 14, 18) + 14; + this.endianness = os.endianness(); + this.fileSize = checkEndian(buffer, 2, 6); + this.offset = checkEndian(buffer, 10, 14); + this.colorArray = buffer.slice(this.headerLength, this.offset); + this.pixelArray = buffer.slice(this.offset); +} + +function checkEndian(buffer, start, finish) { + if (os.endianness() === 'LE') { + return buffer.readUInt32LE(start, finish); + } else if (os.endianness() === 'BE') { + return buffer.readUInt32BE(start, finish); + } else { + console.error('Error!! checkEndian is not functioning'); + } +} diff --git a/bitmap-nassarah/package.json b/bitmap-nassarah/package.json new file mode 100644 index 0000000..1233cca --- /dev/null +++ b/bitmap-nassarah/package.json @@ -0,0 +1,22 @@ +{ + "name": "bitmap-nassarah", + "version": "1.0.0", + "description": "", + "main": "index.js", + "directories": { + "test": "test" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "chai": "^3.5.0", + "gulp": "^3.9.1", + "gulp-eslint": "^3.0.1", + "gulp-mocha": "^3.0.1", + "mocha": "^3.0.2" + } +} diff --git a/bitmap-nassarah/test/bitmap-constructor-error-test.js b/bitmap-nassarah/test/bitmap-constructor-error-test.js new file mode 100644 index 0000000..a64142f --- /dev/null +++ b/bitmap-nassarah/test/bitmap-constructor-error-test.js @@ -0,0 +1,17 @@ +'use strict'; + +const assert = require('assert'); +const bitmapReader = require('../lib/bitmap-read'); +const bitmapConstructor = require('../model/bitmap-constructor'); + +describe('This tests whether bitmap-constructor is outputting a valid bitmap object', function() { + it('bitmap-constructor should return an object with a property .isBitmap with a value of \'BM\'', function(done){ + bitmapReader('testimage-actuallyjpg.bmp', function(err, buffer){ + bitmapConstructor(buffer, 'testimage-actuallyjpg.bmp', function(err, object){ + var testThis = object.isBitmap; + assert.ok(testThis !== 'BM', 'This property should not read BM'); + }); + done(); + }); + }); +}); diff --git a/bitmap-nassarah/test/bitmap-read-test.js b/bitmap-nassarah/test/bitmap-read-test.js new file mode 100644 index 0000000..a0674ab --- /dev/null +++ b/bitmap-nassarah/test/bitmap-read-test.js @@ -0,0 +1,16 @@ +'use strict'; + +const assert = require('assert'); + +const bitmapRead = require('../lib/bitmap-read'); + +describe('testing whether we can read buffer of any file', function() { + it('should return a buffer', function(done) { + bitmapRead('test.txt', function(err, result){ + console.log(err); + var resultString = result.toString(); + assert.equal(resultString, '111\n', 'was not 111'); + done(); + }); + }); +}); diff --git a/bitmap-nassarah/test/transform-test.js b/bitmap-nassarah/test/transform-test.js new file mode 100644 index 0000000..c2c23c9 --- /dev/null +++ b/bitmap-nassarah/test/transform-test.js @@ -0,0 +1,34 @@ +'use strict'; + +const assert = require('assert'); +const invertTransform = require('../lib/invert-transform.js'); +const grayTransform = require('../lib/gray-transform.js'); +const rgbTransform = require('../lib/rgb-transform.js'); +const testArray = [55, 40, 25, 5]; + +describe('testing all invert, gray, and rgb transforms', function() { + describe('testing invert transform', function() { + it('should return an inverted array', function() { + invertTransform(testArray, function(data) { + console.log('data from invertTransform', data); + assert.equal(data[0], '200', 'was not correct array, returned this:' + data); + }); + }); + }); + + describe('testing gray transform', function() { + it('should return an array of an average of all indices', function() { + grayTransform(testArray, function(data){ + assert.equal(data[0], '31.25', 'was not correct array, returned this:' + data); + }); + }); + }); + + describe('testing rgb transform', function() { + it('should return an array with one index multiplied by a constant', function() { + rgbTransform(testArray, function(data) { + assert.equal(data[1], '10200', 'was not correct array, returned this:' + data); + }); + }); + }); +}); diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..347b567 --- /dev/null +++ b/gulpfile.js @@ -0,0 +1,29 @@ +'use strict'; + +const gulp = require('gulp'); +const eslint = require('gulp-eslint'); +const mocha = require('gulp-mocha'); + +gulp.task('hello', function(){ + console.log('hello'); +}); + +gulp.watch('***/*.js', ['hello']); + +gulp.task('lint', function () { + return gulp.src(['**/*.js','!node_modules/**']) + .pipe(eslint()) + .pipe(eslint.format()) + .pipe(eslint.failAfterError()); +}); + +gulp.task('test', function(){ + gulp.src('./test/*-test.js', {read: false}) + .pipe(mocha({reporter: 'nyan'})); +}); + +gulp.task('dev', function(){ + gulp.watch(['**/*.js','!node_modules/**'], ['test', 'lint']); +}); + +gulp.task('default', ['dev']);