diff --git a/lab-closure-group/.eslintignore b/lab-closure-group/.eslintignore new file mode 100644 index 0000000..05b1cf3 --- /dev/null +++ b/lab-closure-group/.eslintignore @@ -0,0 +1,5 @@ +**/node_modules/* +**/vendor/* +**/*.min.js +**/coverage/* +**/build/* diff --git a/lab-closure-group/.eslintrc.json b/lab-closure-group/.eslintrc.json new file mode 100644 index 0000000..840d336 --- /dev/null +++ b/lab-closure-group/.eslintrc.json @@ -0,0 +1,26 @@ +{ + "env": { + "browser": true, + "node": true, + "commonjs": true, + "jest": true, + "es6": true + }, + "globals": { + "err": true, + "req": true, + "res": true, + "next": true + }, + "extends": "eslint:recommended", + "parserOptions": { + "sourceType": "module" + }, + "rules": { + "no-console": "off", + "indent": [ "error", 2 ], + "quotes": ["error", "single", { "allowTemplateLiterals": true }], + "comma-dangle": ["error", "always-multiline"], + "semi": [ "error", "always" ] + } +} diff --git a/lab-closure-group/.gitignore b/lab-closure-group/.gitignore new file mode 100644 index 0000000..3583e8a --- /dev/null +++ b/lab-closure-group/.gitignore @@ -0,0 +1,68 @@ +# 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 +*~ + +# auto-generated tag files +tags + +###### Personal +.tern-project diff --git a/lab-closure-group/README.md b/lab-closure-group/README.md new file mode 100644 index 0000000..e69de29 diff --git a/lab-closure-group/__test__/BitMapImage.test.js b/lab-closure-group/__test__/BitMapImage.test.js new file mode 100644 index 0000000..554c10d --- /dev/null +++ b/lab-closure-group/__test__/BitMapImage.test.js @@ -0,0 +1,46 @@ +'use strict'; + +const reader = require('../lib/reader.js'); +const parser = require('../lib/parser.js'); +const BitMapImage = require('../lib/BitMapImage.js'); + +describe('BitMapImage', () => { + + test('BitMapImage should return a BitMapImage object with correct data for house.bmp.', (done) => { + reader(`${__dirname}/../../asset/house.bmp`, (err, data) => { + let parsedData = parser(data); + let result = new BitMapImage(parsedData); + expect(err).toBeNull(); + expect(result.header).toEqual('BM'); + expect(result.size).toEqual(66616); + expect(result.imageWidth).toEqual(256); + expect(result.imageHeight).toEqual(256); + expect(result.bitsPerPix).toEqual(8); + expect(result.sizeOfHeader).toEqual(40); + expect(result.pixelArrayOffset).toEqual(1078); + expect(result.pixelArraySize).toEqual(65536); + expect(result.colorTableSize).toEqual(1024); + expect(result.colorTableOffset).toEqual(54); + done(); + }); + }); + + test('BitMapImage should return a BitMapImage object with correct data for bitmap.bmp.', (done) => { + reader(`${__dirname}/../../asset/bitmap.bmp`, (err, data) => { + let parsedData = parser(data); + let result = new BitMapImage(parsedData); + expect(err).toBeNull(); + expect(result.header).toEqual('BM'); + expect(result.size).toEqual(11078); + expect(result.imageWidth).toEqual(100); + expect(result.imageHeight).toEqual(100); + expect(result.bitsPerPix).toEqual(8); + expect(result.sizeOfHeader).toEqual(40); + expect(result.pixelArrayOffset).toEqual(1078); + expect(result.pixelArraySize).toEqual(10000); + expect(result.colorTableSize).toEqual(1024); + expect(result.colorTableOffset).toEqual(54); + done(); + }); + }); +}); diff --git a/lab-closure-group/__test__/transform.test.js b/lab-closure-group/__test__/transform.test.js new file mode 100644 index 0000000..3ed0e0d --- /dev/null +++ b/lab-closure-group/__test__/transform.test.js @@ -0,0 +1,5 @@ +'use strict'; + +const transform = require('../lib/transform.js'); + +// Test each transformation... either in here, or separate test.js???? IDK again... lol diff --git a/lab-closure-group/data/fingerBlue.bmp b/lab-closure-group/data/fingerBlue.bmp new file mode 100644 index 0000000..1bc4170 Binary files /dev/null and b/lab-closure-group/data/fingerBlue.bmp differ diff --git a/lab-closure-group/data/fingerFlipUp.bmp b/lab-closure-group/data/fingerFlipUp.bmp new file mode 100644 index 0000000..b7cd025 Binary files /dev/null and b/lab-closure-group/data/fingerFlipUp.bmp differ diff --git a/lab-closure-group/data/fingerGreen.bmp b/lab-closure-group/data/fingerGreen.bmp new file mode 100644 index 0000000..6067f38 Binary files /dev/null and b/lab-closure-group/data/fingerGreen.bmp differ diff --git a/lab-closure-group/data/fingerMirror.bmp b/lab-closure-group/data/fingerMirror.bmp new file mode 100644 index 0000000..5db1492 Binary files /dev/null and b/lab-closure-group/data/fingerMirror.bmp differ diff --git a/lab-closure-group/data/fingerNoise.bmp b/lab-closure-group/data/fingerNoise.bmp new file mode 100644 index 0000000..7d1642a Binary files /dev/null and b/lab-closure-group/data/fingerNoise.bmp differ diff --git a/lab-closure-group/data/fingerRainbow.bmp b/lab-closure-group/data/fingerRainbow.bmp new file mode 100644 index 0000000..972c3a7 Binary files /dev/null and b/lab-closure-group/data/fingerRainbow.bmp differ diff --git a/lab-closure-group/data/fingerRed.bmp b/lab-closure-group/data/fingerRed.bmp new file mode 100644 index 0000000..c2e6596 Binary files /dev/null and b/lab-closure-group/data/fingerRed.bmp differ diff --git a/lab-closure-group/data/newbitmap.bmp b/lab-closure-group/data/newbitmap.bmp new file mode 100644 index 0000000..0803bd1 Binary files /dev/null and b/lab-closure-group/data/newbitmap.bmp differ diff --git a/lab-closure-group/index.js b/lab-closure-group/index.js new file mode 100644 index 0000000..774c5b1 --- /dev/null +++ b/lab-closure-group/index.js @@ -0,0 +1,22 @@ +'use strict'; + +const fs = require('fs'); +const BitMapImage = require('./lib/BitMapImage.js'); +const transform = require('./lib/transform.js'); + +const inputFile = process.argv[2]; +const outputFile = process.argv[3]; +const option = process.argv[4]; + + +fs.readFile(`${__dirname}/../asset/${inputFile}`, (err, data) => { + if(err) + throw new Error('Usage: node index.js '); + let bitMapObj = new BitMapImage(data); + transform(bitMapObj, option); + fs.writeFile(`${__dirname}/data/${outputFile}`, bitMapObj.buffer, (err) => { + if(err) + throw new Error('Usage: node index.js '); + console.log('Bitmap created!'); + }); +}); diff --git a/lab-closure-group/lib/BitMapImage.js b/lab-closure-group/lib/BitMapImage.js new file mode 100644 index 0000000..12e1b1c --- /dev/null +++ b/lab-closure-group/lib/BitMapImage.js @@ -0,0 +1,18 @@ +'use strict'; + +// BitMapImage constructor +module.exports = function BitMapImage(data) { + this.buffer = Buffer.from(data); + this.header = this.buffer.toString('utf8', 0, 2); + this.size = this.buffer.readUInt32LE(2); + this.imageWidth = this.buffer.readUInt32LE(18); + this.imageHeight = this.buffer.readUInt32LE(22); + this.bitsPerPix = this.buffer.readUInt16LE(28); + this.sizeOfHeader = this.buffer.readUInt32LE(14); + this.colorTableOffset = 54; // Header(14) + DIB(40) size is the offset + this.colorTableSize = 1024; + this.pixelArrayOffset = this.buffer.readUInt32LE(10); + this.pixelArraySize = this.imageWidth * this.imageHeight; + this.colorTable = this.buffer.slice(54, 1024+54); + this.pixelArray = this.buffer.slice(this.pixelArrayOffset, this.pixelArrayOffset + this.pixelArraySize); +}; diff --git a/lab-closure-group/lib/transform.js b/lab-closure-group/lib/transform.js new file mode 100644 index 0000000..0abd471 --- /dev/null +++ b/lab-closure-group/lib/transform.js @@ -0,0 +1,52 @@ +'use strict'; + +// Require each transform modules here... +const invert = require(`./transformations/invert.js`); +const grayscale = require(`./transformations/grayscale.js`); +const rainbow = require(`./transformations/rainbow.js`); +const noise = require('./transformations/noise.js'); +const red = require('./transformations/red.js'); +const blue = require('./transformations/blue.js'); +const green = require('./transformations/green.js'); +const flipUp = require('./transformations/flipUp.js'); +const mirror = require('./transformations/mirror.js'); + +module.exports = (object, option) => { + // Parse the transform flags/options and call their respective modules for transformation of the object. + // Maybe a switch statement? IDK.. + + switch(option) + case 'invert': + invert(object); + break; + case 'invert': + invert(object); + break; + case 'rainbow': + rainbow(object); + break; + case 'noise': + noise(object); + break; + case 'red': + red(object); + break; + case 'blue': + blue(object); + break; + case 'green': + green(object); + break; + case 'flipUp': + flipUp(object); + break; + case 'mirror': + mirror(object); + break; + default: + break; + } + + // object.colorTable.fill(150); + +}; diff --git a/lab-closure-group/lib/transformations/blue.js b/lab-closure-group/lib/transformations/blue.js new file mode 100644 index 0000000..6b86d0e --- /dev/null +++ b/lab-closure-group/lib/transformations/blue.js @@ -0,0 +1,13 @@ +'use strict'; + +module.exports = (object) => { + for(let i = 0; i < object.colorTable.length; i+=4) { + let blue = object.colorTable.readUInt8(i); + let green = object.colorTable.readUInt8(i+1); + let red = object.colorTable.readUInt8(i+2); + + object.colorTable.writeUInt8(255, i); + object.colorTable.writeUInt8(green, i+1); + object.colorTable.writeUInt8(red, i+2); + } +}; diff --git a/lab-closure-group/lib/transformations/flipUp.js b/lab-closure-group/lib/transformations/flipUp.js new file mode 100644 index 0000000..bbef3f4 --- /dev/null +++ b/lab-closure-group/lib/transformations/flipUp.js @@ -0,0 +1,11 @@ +'use strict'; + +module.exports = (object) => { + let length = object.pixelArray.length/2; + for(let i = 0; i < length; i++) { + let change = object.pixelArray.readUInt8(object.pixelArray.length-1-i); + let temp = object.pixelArray.readUInt8(i); + object.pixelArray.writeUInt8(change, i); + object.pixelArray.writeUInt8(temp, object.pixelArray.length-1-i); + } +}; diff --git a/lab-closure-group/lib/transformations/grayscale.js b/lab-closure-group/lib/transformations/grayscale.js new file mode 100644 index 0000000..7adf61a --- /dev/null +++ b/lab-closure-group/lib/transformations/grayscale.js @@ -0,0 +1,15 @@ +'use strict'; + +module.exports = (object) => { + for(let i = 0; i < object.colorTable.length; i+=4) { + let blue = object.colorTable.readUInt8(i); + let green = object.colorTable.readUInt8(i+1); + let red = object.colorTable.readUInt8(i+2); + + let average = (blue + green + red) / 3; + + object.colorTable.writeUInt8(average, i); + object.colorTable.writeUInt8(average, i+1); + object.colorTable.writeUInt8(average, i+2); + } +}; diff --git a/lab-closure-group/lib/transformations/green.js b/lab-closure-group/lib/transformations/green.js new file mode 100644 index 0000000..61c01d6 --- /dev/null +++ b/lab-closure-group/lib/transformations/green.js @@ -0,0 +1,13 @@ +'use strict'; + +module.exports = (object) => { + for(let i = 0; i < object.colorTable.length; i+=4) { + let blue = object.colorTable.readUInt8(i); + let green = object.colorTable.readUInt8(i+1); + let red = object.colorTable.readUInt8(i+2); + + object.colorTable.writeUInt8(blue, i); + object.colorTable.writeUInt8(255, i+1); + object.colorTable.writeUInt8(red, i+2); + } +}; diff --git a/lab-closure-group/lib/transformations/invert.js b/lab-closure-group/lib/transformations/invert.js new file mode 100644 index 0000000..3118c80 --- /dev/null +++ b/lab-closure-group/lib/transformations/invert.js @@ -0,0 +1,7 @@ +'use strict' + +module.exports = (object) => { + for (var i = 0; i < object.pixelArray.length; i++) { + object.pixelArray[i] = 255 - object.pixelArray[i]; + } +}; diff --git a/lab-closure-group/lib/transformations/mirror.js b/lab-closure-group/lib/transformations/mirror.js new file mode 100644 index 0000000..e870469 --- /dev/null +++ b/lab-closure-group/lib/transformations/mirror.js @@ -0,0 +1,9 @@ +'use strict'; + +module.exports = (object) => { + let length = object.pixelArray.length; + for(let i = 0; i < length; i++) { + let change = object.pixelArray.readUInt8(object.pixelArray.length-1-i); + object.pixelArray.writeUInt8(change, i); + } +}; diff --git a/lab-closure-group/lib/transformations/noise.js b/lab-closure-group/lib/transformations/noise.js new file mode 100644 index 0000000..8b14a05 --- /dev/null +++ b/lab-closure-group/lib/transformations/noise.js @@ -0,0 +1,11 @@ +'use strict'; + +module.exports = (object) => { + for(let i = 0; i < object.pixelArray.length; i++) { + let min = Math.ceil(0); + let max = Math.floor(256); + let random = Math.floor(Math.random() * (max - min)) + min; + + object.pixelArray.writeUInt8(random, i); + } +}; diff --git a/lab-closure-group/lib/transformations/rainbow.js b/lab-closure-group/lib/transformations/rainbow.js new file mode 100644 index 0000000..fe2eab1 --- /dev/null +++ b/lab-closure-group/lib/transformations/rainbow.js @@ -0,0 +1,26 @@ +'use strict'; + +module.exports = (object) => { + for(let i = 0; i < object.colorTable.length; i+=4) { + let min = Math.ceil(0); + let max = Math.floor(256); + let random1 = Math.floor(Math.random() * (max - min)) + min; + let random2 = Math.floor(Math.random() * (max - min)) + min; + let random3 = Math.floor(Math.random() * (max - min)) + min; + let random4 = Math.floor(Math.random() * (max - min)) + min; + + object.colorTable.writeUInt8(random1, i); + object.colorTable.writeUInt8(random2, i+1); + object.colorTable.writeUInt8(random3, i+2); + object.colorTable.writeUInt8(random4, i+3); + } + + for(let i = 0; i < object.pixelArray.length; i++) { + let min = Math.ceil(0); + let max = Math.floor(256); + let random = Math.floor(Math.random() * (max - min)) + min; + + object.pixelArray.writeUInt8(random, i); + } + +}; diff --git a/lab-closure-group/lib/transformations/red.js b/lab-closure-group/lib/transformations/red.js new file mode 100644 index 0000000..52bb23f --- /dev/null +++ b/lab-closure-group/lib/transformations/red.js @@ -0,0 +1,13 @@ +'use strict'; + +module.exports = (object) => { + for(let i = 0; i < object.colorTable.length; i+=4) { + let blue = object.colorTable.readUInt8(i); + let green = object.colorTable.readUInt8(i+1); + let red = object.colorTable.readUInt8(i+2); + + object.colorTable.writeUInt8(blue, i); + object.colorTable.writeUInt8(green, i+1); + object.colorTable.writeUInt8(255, i+2); + } +}; diff --git a/lab-closure-group/package.json b/lab-closure-group/package.json new file mode 100644 index 0000000..b60f203 --- /dev/null +++ b/lab-closure-group/package.json @@ -0,0 +1,19 @@ +{ + "name": "lab-closure-group", + "version": "0.0.1", + "description": "Lab 4 of 401, Bitmap Transformer. Mark, Christina, Ron, Anthony", + "main": "index.js", + "dependencies": {}, + "devDependencies": { + "eslint": "^4.6.1", + "jest": "^21.0.2" + }, + "scripts": { + "lint": "eslint .", + "test": "jest --coverage -i", + "test-watch": "jest --watch -i" + }, + "keywords": [], + "author": "", + "license": "MIT" +}