Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lab-andrew-dalton/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**/node_modules/*
**/vendor/*
**/*.min.js
**/coverage/*
**/build/*
26 changes: 26 additions & 0 deletions lab-andrew-dalton/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -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" ]
}
}
92 changes: 92 additions & 0 deletions lab-andrew-dalton/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@

# Created by https://www.gitignore.io/api/node,macos

### macOS ###
*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env


# End of https://www.gitignore.io/api/node,macos
1 change: 1 addition & 0 deletions lab-andrew-dalton/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
For this project, I have defined six modules, as well as an index script which is the entry point for the CLI. The index file parses three arguments from the command line, a source file, a destination file, and a function. There are four functions on the index, each of which is similarly structured. Each reads a file, passes that data to the bitmap module which parses the file to get its metadata, passes the parsed data to the desired transformation module, and finally writes the returned altered data to the destination file. Furthermore, if the user does not supply the proper expected arguments to the command line, there are messages built in to display to the user. For instance, if either the source file or destination file do not end in .bmp, an error message is displayed to the user, and it is suggested they type 'bmptransform help' for help with using the CLI. If a user types 'bmptransform help', the index script also calls the help function from the help module. The help module takes no parameters and simply displays to the console the names of the functions (with their descriptions) and accepted input to the command line. The bitmap module contains one function which takes raw data as input and returns an object which contains the parsed data. Each of the remaining modules (addcontrast, bwtransform, flipcolors and randomcolors) takes the parsed data, performs a transform on the buffer and returns the transformed object. These functions determine whether or not a color table is present and act accordingly.
29 changes: 29 additions & 0 deletions lab-andrew-dalton/__test__/addcontrast.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const addContrast = require('../lib/addcontrast');

describe('lib/addcontrast.js', () => {

describe('testing addcontrast.transform to return values of higher contrast', () => {
test('function should return expected values for bitmap with color table', () => {
const testArr = {};
testArr.buffer = new Buffer([0, 0, 20, 0, 30, 34, 88, 0]);
testArr.colorTable = testArr.buffer.slice(0);
const newBuff = addContrast.transform(testArr);
expect(newBuff.buffer.readUInt8(2)).toBe(0);
expect(newBuff.buffer.readUInt8(6)).toBe(255);
});

test('function should return expected values for bitmap without color table', () => {
const testArr = {};
testArr.pixelTableOffset = 54;
const valArr = new Array(54);
valArr.splice(54, 0, 0, 0, 20, 30, 34, 88);
testArr.buffer = new Buffer(valArr);
const newBuff = addContrast.transform(testArr);
expect(newBuff.buffer.readUInt8(53)).toBe(0);
expect(newBuff.buffer.readUInt8(59)).toBe(252);
});
});

});
Binary file added lab-andrew-dalton/__test__/assets/bitmap.bmp
Binary file not shown.
Binary file not shown.
Binary file added lab-andrew-dalton/__test__/assets/house.bmp
Binary file not shown.
Binary file not shown.
21 changes: 21 additions & 0 deletions lab-andrew-dalton/__test__/bitmap.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const bitmap = require('../lib/bitmap');
const fs = require('fs');

describe('lib/bitmap.js', () => {

describe('testing bitmap.parseBitmap to return expected properties', () => {
test('function should return expected properties', done => {
fs.readFile(`${__dirname}/assets/house.bmp`, (error, data) => {
let parsedBitmap = bitmap.parseBitmap(data);
expect(error).toBeNull();
expect(parsedBitmap.type).toBe('BM');
expect(parsedBitmap.fileSize).toBe(66616);
expect(parsedBitmap.pixelTableOffset).toBe(1078);
expect(parsedBitmap.height).toBe(256);
done();
});
});
});
});
29 changes: 29 additions & 0 deletions lab-andrew-dalton/__test__/bwtransform.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const bwTransform = require('../lib/bwtransform');

describe('lib/bwtransform.js', () => {

describe('testing bwtransform.transform to return grayscale values', () => {
test('function should return expected values for bitmap with color table', () => {
const testArr = {};
testArr.buffer = new Buffer([0, 0, 20, 0, 30, 34, 88, 0]);
testArr.colorTable = testArr.buffer.slice(0);
const newBuff = bwTransform.transform(testArr);
expect(newBuff.buffer.readUInt8(2)).toBe(0);
expect(newBuff.buffer.readUInt8(6)).toBe(30);
});

test('function should return expected values for bitmap without color table', () => {
const testArr = {};
testArr.pixelTableOffset = 54;
const valArr = new Array(54);
valArr.splice(54, 0, 0, 0, 20, 30, 34, 88);
testArr.buffer = new Buffer(valArr);
const newBuff = bwTransform.transform(testArr);
expect(newBuff.buffer.readUInt8(53)).toBe(0);
expect(newBuff.buffer.readUInt8(58)).toBe(30);
});
});

});
29 changes: 29 additions & 0 deletions lab-andrew-dalton/__test__/flipcolors.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const flipColors = require('../lib/flipcolors');

describe('lib/flipcolors.js', () => {

describe('testing flipcolors.transform to return inverted values', () => {
test('function should return expected values for bitmap with color table', () => {
const testArr = {};
testArr.buffer = new Buffer([0, 0, 20, 0, 30, 34, 88, 0]);
testArr.colorTable = testArr.buffer.slice(0);
const newBuff = flipColors.transform(testArr);
expect(newBuff.buffer.readUInt8(2)).toBe(235);
expect(newBuff.buffer.readUInt8(6)).toBe(167);
});

test('function should return expected values for bitmap without color table', () => {
const testArr = {};
testArr.pixelTableOffset = 54;
const valArr = new Array(54);
valArr.splice(54, 0, 0, 0, 20, 30, 34, 88);
testArr.buffer = new Buffer(valArr);
const newBuff = flipColors.transform(testArr);
expect(newBuff.buffer.readUInt8(56)).toBe(235);
expect(newBuff.buffer.readUInt8(59)).toBe(167);
});
});

});
29 changes: 29 additions & 0 deletions lab-andrew-dalton/__test__/randomcolors.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const randomColors = require('../lib/randomcolors');

describe('lib/randomcolors.js', () => {

describe('testing randomcolors.transform to return random values', () => {
test('function should return expected values for bitmap with color table', () => {
const testArr = {};
testArr.buffer = new Buffer([0, 0, 20, 0, 30, 34, 88, 0]);
testArr.colorTable = testArr.buffer.slice(0);
const newBuff = randomColors.transform(testArr);
expect(newBuff.buffer.readUInt8(2)).not.toBe(30);
expect(newBuff.buffer.readUInt8(6)).not.toBe(88);
});

test('function should return expected values for bitmap without color table', () => {
const testArr = {};
testArr.pixelTableOffset = 54;
const valArr = new Array(54);
valArr.splice(54, 0, 0, 0, 20, 30, 34, 88);
testArr.buffer = new Buffer(valArr);
const newBuff = randomColors.transform(testArr);
expect(newBuff.buffer.readUInt8(56)).not.toBe(30);
expect(newBuff.buffer.readUInt8(59)).not.toBe(88);
});
});

});
114 changes: 114 additions & 0 deletions lab-andrew-dalton/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#!/usr/bin/env node
'use strict';


const index = module.exports = {};

const bitmap = require('./lib/bitmap');
const bwTransform = require('./lib/bwtransform');
const flipColors = require('./lib/flipcolors');
const addContrast = require('./lib/addcontrast');
const randomColors = require('./lib/randomcolors');
const help = require('./lib/help');
const fs = require('fs');

let file = process.argv[2];
let saveLoc = process.argv[3];
let command = process.argv[4];

index.bwTransformer = () => {
fs.readFile(file, (error, data) => {
if (error){
console.error(error);
return;
}

let parsedBitmap = bitmap.parseBitmap(data);
let bwTransformed = bwTransform.transform(parsedBitmap);
fs.writeFile(saveLoc, bwTransformed.buffer, error => {
if (error){
console.error(error);
return;
}
});
});
};

index.flipColors = () => {
fs.readFile(file, (error, data) => {
if (error){
console.error(error);
return;
}

let parsedBitmap = bitmap.parseBitmap(data);
let flippedColors = flipColors.transform(parsedBitmap);
fs.writeFile(saveLoc, flippedColors.buffer, error => {
if (error){
console.error(error);
return;
}
});
});
};

index.addContrast = () => {
fs.readFile(file, (error, data) => {
if (error){
console.error(error);
return;
}

let parsedBitmap = bitmap.parseBitmap(data);
let addedContrast = addContrast.transform(parsedBitmap);
fs.writeFile(saveLoc, addedContrast.buffer, error => {
if (error){
console.error(error);
return;
}
});
});
};

index.randomColors = () => {
fs.readFile(file, (error, data) => {
if (error){
console.error(error);
return;
}

let parsedBitmap = bitmap.parseBitmap(data);
let randomizedColors = randomColors.transform(parsedBitmap);
fs.writeFile(saveLoc, randomizedColors.buffer, error => {
if (error){
console.error(error);
return;
}
});
});
};

let errorCounter = 0;

if (process.argv.length === 3 && process.argv[2] === 'help') help.help();
if (process.argv.length > 5) console.log('invalid syntax, type \'bmptransform help\' for more info');
if (file.slice(-3) !== 'bmp' || saveLoc.slice(-3) !== 'bmp') {
errorCounter++;
console.log(file, saveLoc);
console.log('you must specify a \'.bmp\' as both the file to read and the file to save');
}

if (errorCounter < 1) {
if (command === 'addcontrast') index.addContrast();
if (command === 'bwtransform') index.bwTransformer();
if (command === 'flipcolors') index.flipColors();
if (command === 'randomcolors') index.randomColors();
}

let commandArr = ['addcontrast', 'bwtransform', 'flipcolors', 'randomcolors'];

if (commandArr.indexOf(command) === -1){
console.log('type \'bmptransform help\' for available transform types');
} else {
console.log('Transformation successful!');
}
Loading