Skip to content

Commit

Permalink
Started case study, some images are not working for b123.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinAlbs committed Nov 18, 2016
1 parent b1a91f4 commit 451565e
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions base123.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ function encode(rawData) {
stringData.push(b2);
debugLog(' Unicode character is ', b1.toString(2), b2.toString(2));
} else {
debugLog(' One byte unicode character is ', bits.toString(2));
stringData.push(bits);
}
}
Expand Down
6 changes: 6 additions & 0 deletions casestudy/base64Encode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let fs = require('fs');
if (process.argv.length != 3) {
console.log("Usage: node base64EncodeImage.js file");
process.exit(1);
}
console.log(fs.readFileSync(process.argv[2]).toString('base64'));
31 changes: 31 additions & 0 deletions casestudy/createCases.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Writes a number of images in both src, base64, and base123 format.
let fs = require('fs')
, base123 = require('../base123')
;

const kNumImages = 1000
, kImgDir = 'img/'
, kHeader = "<html><head><meta charset='utf-8'></head><body>"
, kFooter = "</body></html>"
, kBase123Script = '<script>function b123d(a){function i(a){a<<=1,f|=a>>>g,g+=7,g>=8&&(e.push(f),g-=8,f=a<<7-g&255)}for(var b=[0,10,13,34,92],d=64,e=[],f=0,g=0,h=a.charCodeAt(0),j=1;j<a.length;j++){var k=a.charCodeAt(j);if(k>127){var l=k>>>8&7;if(i(b[l]),j==a.length-1&&h&d)continue;i(127&k)}else i(k)}return e}document.querySelectorAll("[data-b123]").forEach(a=>{var b=a.dataset.b123,c=a.dataset.b123m||"image/png";inflated=new Uint8Array(b123d(b));var d=new Blob([inflated],{type:c}),e=URL.createObjectURL(d);a.src=e});</script>'
;

let outSrc = fs.createWriteStream('out.html')
, outB64 = fs.createWriteStream('out-base64.html')
, outB123 = fs.createWriteStream('out-base123.html')
, outFiles = [outSrc, outB64, outB123]
;

outFiles.forEach((file) => { file.write(kHeader); });
for (let i = 0; i < kNumImages; i++) {
outSrc.write('<img src="' + kImgDir + i + '.png" />');
let buf = fs.readFileSync(kImgDir + i + '.png');
let base64String = buf.toString('base64');
outB64.write('<img src="data:image/png;base64,'+ base64String + '" />');
let base123Data = base123.encodeFromBase64(base64String); // TODO: bad, encode from buffer.
outB123.write('<img data-b123="');
outB123.write(new Buffer(new Uint8Array(base123Data)));
outB123.write('">');
}
outB123.write(kBase123Script);
outFiles.forEach((file) => { file.end(kFooter); });
1 change: 1 addition & 0 deletions casestudy/out-base123.html

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions casestudy/out-base64.html

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions casestudy/out.html

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions casestudy/scrapeImages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
let fs = require('fs')
, http = require('http')
, currentImageIndex = 0
;

const url = 'http://unsplash.it/64?image='
, kNumImages = 1000
, kConcurrency = 8
;

function fetchImage() {
if (currentImageIndex >= kNumImages) return;
let index = currentImageIndex++;
console.log('Fetching image ' + index + '/' + kNumImages);
http.get(url + index, (res) => {
let imageData = Buffer.alloc(0);
res.on('data', (chunk) => { imageData = Buffer.concat([imageData, chunk]); });
res.on('end', () => {
fs.writeFile('img/' + index + '.png', imageData);
fetchImage();
});
});
}

// Each fetchImage will act as a separate thread fetching images.
for (let i = 0; i < kConcurrency; i++) fetchImage();
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,8 @@ Minimal path to release
transfer rate as a function of data size.). Use JSPerf
- Do a fair storage size test when gzip considered. original vs base64 vs base123
- Add a case study of an image rich page before and after.
- Refetch images to fix errors
- See what is wrong with the mis-rendered few images
- Test performance on firefox
- Test with gzip
- Finish blog post.
1 change: 1 addition & 0 deletions test/testAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ tester.addTest('realBase64Data', () => {
testBase64EncodeDecode(testData.base64.img1);
testBase64EncodeDecode(testData.base64.img2);
testBase64EncodeDecode(testData.base64.audio1);
testBase64EncodeDecode(testData.base64.imgBug);
});

tester.run();
5 changes: 5 additions & 0 deletions test/testBug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let base123 = require('../base123')
, testData = require('./testData')
;

base123.encodeFromBase64(testData.base64.imgBug);
1 change: 1 addition & 0 deletions test/testData.js

Large diffs are not rendered by default.

0 comments on commit 451565e

Please sign in to comment.