Skip to content

Commit

Permalink
Merge pull request #38 from yurydelendik/master
Browse files Browse the repository at this point in the history
Using source code from the pdf.js; adds jbig2/jpx parsers.
  • Loading branch information
yurydelendik committed Mar 9, 2015
2 parents 8e6f346 + 554d795 commit f1d3092
Show file tree
Hide file tree
Showing 15 changed files with 4,723 additions and 984 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
build/
node_modules/
pdf.js/
66 changes: 66 additions & 0 deletions Gruntfile.js
@@ -0,0 +1,66 @@
/*
* Copyright 2015 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

module.exports = function(grunt) {

grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
uglify: {
default: {
options: {
preserveComments: 'some',
screwIE8: true,
beautify: true,
compress: false,
mangle: false
},
files: {
'jpg.js': ['src/jpg.js', 'build/pdfjs.js']
}
}
}
});

grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-uglify');

function includeFiles(src, dest) {
var fs = require('fs'), path = require('path');
function process(src) {
var content = fs.readFileSync(src).toString();
return content.replace(/\/\/#include\s+"([^"]+)[^\n]+/g, function (all, file) {
file = path.join(path.dirname(src), file);
return process(file);
});
}
fs.writeFileSync(dest, process(src));
}

grunt.registerTask('build-pdfjs', function () {
grunt.file.mkdir('build');

includeFiles('src/pdfjs.js', 'build/pdfjs.js');

// HACK patching JBIG2, see also src/pdfjs.js
var fs = require('fs');
var content = fs.readFileSync('build/pdfjs.js').toString();
fs.writeFileSync('build/pdfjs.js', content.replace('return visitor.buffer;', 'return visitor;'));
});

grunt.registerTask('default', ['build']);
grunt.registerTask('build', ['build-pdfjs', 'uglify:default']);
};

9 changes: 7 additions & 2 deletions README
@@ -1,3 +1,8 @@
Simple JPEG/DCT data decoder in JavaScript.
Simple JPEG/DCT data decoder in JavaScript. Also this project includes JPEG 2000 and JBIG2 decoders.

Example URL: http://notmasteryet.github.com/jpgjs/example.html
Example URL:
http://notmasteryet.github.com/jpgjs/example.html

The images decoders are maintained as part of the PDF.js project, please submit patches there.

https://github.com/mozilla/pdf.js/
6 changes: 3 additions & 3 deletions benchmark.html
Expand Up @@ -80,9 +80,9 @@
function go() {
var config = {
images: [
'j1.jpg',
'j2.jpg',
'j3.jpg'
'images/j1.jpg',
'images/j2.jpg',
'images/j3.jpg'
],
samples: 50
};
Expand Down
13 changes: 13 additions & 0 deletions bower.json
@@ -0,0 +1,13 @@
{
"name": "jpgjs",
"version": "1.0.0",
"keywords": [
"jpgjs",
"JPEG",
"JPG"
],
"main": [
"jpg.js"
],
"ignore": []
}
144 changes: 119 additions & 25 deletions example.html
Expand Up @@ -4,31 +4,8 @@
<html>
<head>
<script src="jpg.js"></script>
<script>

function displayImage(canvasId, url) {
var j = new JpegImage();
j.onload = function() {
var c = document.getElementById(canvasId);
c.width = j.width;
c.height = j.height;
var ctx = c.getContext("2d");
var d = ctx.getImageData(0,0,j.width,j.height);
j.copyToImageData(d);
ctx.putImageData(d, 0, 0);
};
j.load(url);
}

function loadImages() {
displayImage("c1", "j1.jpg");
displayImage("c2", "j2.jpg");
displayImage("c3", "j3.jpg");
}

</script>
</head>
<body onload="loadImages()">
<body>
<table>
<tr><th>baseline</th><th>progressive</th><th>baseline (gray)</th></tr>
<tr>
Expand All @@ -37,4 +14,121 @@
<td><canvas id="c3"></canvas></td>
</tr>
</table>
</body></html>
<script>
// Simple loading of the JPEG images
function displayImage(canvasId, url) {
var j = new JpegImage();
j.onload = function() {
var c = document.getElementById(canvasId);
c.width = j.width;
c.height = j.height;
var ctx = c.getContext("2d");
var d = ctx.getImageData(0,0,j.width,j.height);
j.copyToImageData(d);
ctx.putImageData(d, 0, 0);
};
j.load(url);
}

displayImage("c1", "images/j1.jpg");
displayImage("c2", "images/j2.jpg");
displayImage("c3", "images/j3.jpg");
</script>


<table>
<tr><th>base64</th></tr>
<tr>
<td><canvas id="c4"></canvas></td>
</tr>
</table>

<script>
// Loading image as binary data and using data protocole and base64 encoding
function loadAsUint8Array(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'arraybuffer';
xhr.onload = function () {
callback(new Uint8Array(xhr.response));
};
xhr.send();
}

function loadAsDataURL(url, callback) {
loadAsUint8Array(url, function (arr) {
var s = '';
for (var i = 0; i < arr.length; i++) {
s += String.fromCharCode(arr[i]);
}
callback('data:image/jpeg;base64,' + btoa(s));
});
}

loadAsDataURL("images/j1.jpg", function (url) {
displayImage("c4", url);
});
</script>

<table>
<tr><th>jpeg</th><th>jbig2</th><th>jpeg 2000</th></tr>
<tr>
<td><canvas id="c5"></canvas></td>
<td><canvas id="c6"></canvas></td>
<td><canvas id="c7"></canvas></td>
</tr>
</table>

<script>
// Directly using PDFjs decoders -- it is experimental and will likely change.
function decodeImage(canvasId, encoding, url) {
loadAsUint8Array(url, function (encoded) {
var numComponents, width, height, decoded, parser;
switch (encoding) {
case 'jpeg':
parser = new JpegDecoder();
parser.parse(encoded);
width = parser.width;
height = parser.height;
numComponents = parser.numComponents;
decoded = parser.getData(width, height);
break;
case 'jbig2':
parser = new Jbig2Decoder();
parser.parse(encoded);
width = parser.width;
height = parser.height;
numComponents = 1;
decoded = parser.data;
break;
case 'jpx':
parser = new JpxDecoder();
parser.parse(encoded);
width = parser.width;
height = parser.height;
numComponents = parser.componentsCount;
decoded = parser.tiles[0].items;
break;
}

var canvas = document.getElementById(canvasId);
canvas.width = width;
canvas.height = height;
var ctx = canvas.getContext('2d');
var imageData = ctx.createImageData(width, height);
var imageBytes = imageData.data;
for (var i = 0, j = 0, ii = width * height * 4; i < ii; ) {
imageBytes[i++] = decoded[j++];
imageBytes[i++] = numComponents === 3 ? decoded[j++] : decoded[j - 1];
imageBytes[i++] = numComponents === 3 ? decoded[j++] : decoded[j - 1];
imageBytes[i++] = 255;
}
ctx.putImageData(imageData, 0, 0);
});
}

decodeImage("c5", "jpeg", "images/j1.jpg");
decodeImage("c6", "jbig2", "images/j3.jb2");
decodeImage("c7", "jpx", "images/j1.jp2");
</script>
</body></html>
Binary file added images/j1.jp2
Binary file not shown.
File renamed without changes
File renamed without changes
Binary file added images/j3.jb2
Binary file not shown.
File renamed without changes

0 comments on commit f1d3092

Please sign in to comment.