Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
71901dd
initial commit
ronbarrantes Sep 14, 2017
dde2c1e
add the lib and __test__ directories
ronbarrantes Sep 14, 2017
2e0c959
test pulls all data needed for further functions.
Mackoyokcam Sep 15, 2017
8e5a29a
created parser and bitmapimage object. Parser is tested and passes.
Mackoyokcam Sep 15, 2017
233ab9c
BitMapImage.js passed the tests.
Mackoyokcam Sep 15, 2017
fe955ca
added some scaffolding files for transform and writer.
Mackoyokcam Sep 15, 2017
a1accf9
Merge pull request #1 from ronbarrantes/mark-reader-test
Penssake Sep 15, 2017
2ad265c
deleted redundant code. now works for simple transformations
Mackoyokcam Sep 15, 2017
4de7b09
Merge pull request #2 from ronbarrantes/mark-reader-test
AMKRobinson Sep 15, 2017
702e143
added transforms: noise, blue, red, green, rainbow, mirror, flipup
Mackoyokcam Sep 16, 2017
68c9588
got invert to work
AMKRobinson Sep 16, 2017
266c698
Merge pull request #3 from ronbarrantes/mark-reader-test
AMKRobinson Sep 16, 2017
937dc29
Merge branch 'master' into Anthony-invert
Mackoyokcam Sep 16, 2017
51ed268
Merge pull request #4 from ronbarrantes/Anthony-invert
Mackoyokcam Sep 16, 2017
9855ce1
Update transform.js
Mackoyokcam Sep 16, 2017
76b4c22
fixed some issues
Mackoyokcam Sep 16, 2017
6c4da82
Merge pull request #5 from ronbarrantes/mark-reader-test
Penssake Sep 16, 2017
164a7ec
Added readme, updated testing, and cleaned up some corpse code.
Mackoyokcam Sep 18, 2017
62a31d1
Merge pull request #6 from ronbarrantes/mark-home
Penssake Sep 18, 2017
6cbff6b
added cyan magenta and yellow transformation
ronbarrantes Sep 18, 2017
27fb2f4
Merge pull request #7 from ronbarrantes/mkcolors-ron
ronbarrantes Sep 18, 2017
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-closure-group/.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-closure-group/.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" ]
}
}
70 changes: 70 additions & 0 deletions lab-closure-group/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# 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

package-lock.json
21 changes: 21 additions & 0 deletions lab-closure-group/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
![CF](https://camo.githubusercontent.com/70edab54bba80edb7493cad3135e9606781cbb6b/687474703a2f2f692e696d6775722e636f6d2f377635415363382e706e67) Code-401-Javascript Group Lab-4: Bitmap Transformer
===
This is the day 4 lab with Code Fellows. The purpose of the lab is to teach students how to manipulated binary data in order to transform/alter bitmap files with various specific effects.
# Execution
Go to the lab-closure-group directory. On a terminal, run the command `node index.js <input file> <output file> <option>`. This will mutate the input file depending on the option specified, and store it into the output file in the data directory.
# Modules
Description of exported values of each module defined in lib/ directory, along with airity and expected input/return values.
### BitMapImage.js
Exports a single constructor function with an arity of one, that takes data from a file system read occuring in index.js and creates a bitmap image object to be used for transformations.
### transform.js
Exports a function with an arity of two, that takes in a bitmap image object (contstructed by BitMapImage.js) as well as a string to determine what transformation will be performed.
* # Transformations
* grayscale - Converts colors to only different shades of gray.
* invert - Inverts the rgb values.
* rainbow - Randomizes all the colors.
* noise - Creates a static screen effect.
* red - Changes the image to red, unless pixel is white.
* blue - Changes the image to blue, unless pixel is white.
* green - Changes the image to green, unless pixel is white.
* flipUp - Flips the image vertically.
* mirror - Creates a vertical mirror effect.
47 changes: 47 additions & 0 deletions lab-closure-group/__test__/BitMapImage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

const fs = require('fs');
const BitMapImage = require('../lib/BitMapImage.js');

describe('BitMapImage', () => {

test('BitMapImage should return a BitMapImage object with correct data for house.bmp.', (done) => {
fs.readFile(`${__dirname}/../../asset/house.bmp`, (err, data) => {
if(err)
throw new Error('Usage: node index.js <input filename> <output filename> <transformation>');
let result = new BitMapImage(data);
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) => {
fs.readFile(`${__dirname}/../../asset/bitmap.bmp`, (err, data) => {
if(err)
throw new Error('Usage: node index.js <input filename> <output filename> <transformation>');
let result = new BitMapImage(data);
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();
});
});
});
Binary file added lab-closure-group/data/fingerBlue.bmp
Binary file not shown.
Binary file added lab-closure-group/data/fingerFlipUp.bmp
Binary file not shown.
Binary file added lab-closure-group/data/fingerGreen.bmp
Binary file not shown.
Binary file added lab-closure-group/data/fingerMirror.bmp
Binary file not shown.
Binary file added lab-closure-group/data/fingerNoise.bmp
Binary file not shown.
Binary file added lab-closure-group/data/fingerRainbow.bmp
Binary file not shown.
Binary file added lab-closure-group/data/fingerRed.bmp
Binary file not shown.
Binary file added lab-closure-group/data/newbitmap.bmp
Binary file not shown.
22 changes: 22 additions & 0 deletions lab-closure-group/index.js
Original file line number Diff line number Diff line change
@@ -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 <input filename> <output filename> <transformation>');
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 <input filename> <output filename> <transformation>');
console.log('Bitmap created!');
});
});
18 changes: 18 additions & 0 deletions lab-closure-group/lib/BitMapImage.js
Original file line number Diff line number Diff line change
@@ -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);
};
61 changes: 61 additions & 0 deletions lab-closure-group/lib/transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

// Require each transform modules here...
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 cyan = require('./transformations/cyan.js');
const magenta = require('./transformations/magenta.js');
const yellow = require('./transformations/yellow.js');
const flipUp = require('./transformations/flipUp.js');
const mirror = require('./transformations/mirror.js');
const invert = require('./transformations/invert.js');

module.exports = (object, option) => {
// Parse the transform flags/options and call their respective modules for transformation of the object.

switch(option) {
case 'grayscale':
grayscale(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 'cyan':
cyan(object);
break;
case 'magenta':
magenta(object);
break;
case 'yellow':
yellow(object);
break;
case 'flipUp':
flipUp(object);
break;
case 'mirror':
mirror(object);
break;
default:
break;
}

};
12 changes: 12 additions & 0 deletions lab-closure-group/lib/transformations/blue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

module.exports = (object) => {
for(let i = 0; i < object.colorTable.length; i+=4) {
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);
}
};
11 changes: 11 additions & 0 deletions lab-closure-group/lib/transformations/cyan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

module.exports = (object) => {
for(let i = 0; i < object.colorTable.length; i+=4) {
let red = object.colorTable.readUInt8(i+2);

object.colorTable.writeUInt8(255, i);
object.colorTable.writeUInt8(255, i+1);
object.colorTable.writeUInt8(red, i+2);
}
};
11 changes: 11 additions & 0 deletions lab-closure-group/lib/transformations/flipUp.js
Original file line number Diff line number Diff line change
@@ -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);
}
};
15 changes: 15 additions & 0 deletions lab-closure-group/lib/transformations/grayscale.js
Original file line number Diff line number Diff line change
@@ -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);
}
};
12 changes: 12 additions & 0 deletions lab-closure-group/lib/transformations/green.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

module.exports = (object) => {
for(let i = 0; i < object.colorTable.length; i+=4) {
let blue = object.colorTable.readUInt8(i);
let red = object.colorTable.readUInt8(i+2);

object.colorTable.writeUInt8(blue, i);
object.colorTable.writeUInt8(255, i+1);
object.colorTable.writeUInt8(red, i+2);
}
};
7 changes: 7 additions & 0 deletions lab-closure-group/lib/transformations/invert.js
Original file line number Diff line number Diff line change
@@ -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];
}
};
12 changes: 12 additions & 0 deletions lab-closure-group/lib/transformations/magenta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

module.exports = (object) => {
for(let i = 0; i < object.colorTable.length; i+=4) {

let green = object.colorTable.readUInt8(i+1);

object.colorTable.writeUInt8(255, i);
object.colorTable.writeUInt8(green, i+1);
object.colorTable.writeUInt8(255, i+2);
}
};
9 changes: 9 additions & 0 deletions lab-closure-group/lib/transformations/mirror.js
Original file line number Diff line number Diff line change
@@ -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);
}
};
11 changes: 11 additions & 0 deletions lab-closure-group/lib/transformations/noise.js
Original file line number Diff line number Diff line change
@@ -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);
}
};
Loading