jimp 0.2.24 → 0.2.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +14 -0
- package/README.md +98 -18
- package/browser/.editorconfig +2 -0
- package/browser/README.md +45 -203
- package/browser/browserify-build.sh +6 -3
- package/browser/examples/dice.png +0 -0
- package/browser/examples/example1.html +24 -0
- package/browser/examples/example2.html +20 -0
- package/browser/examples/example3.html +35 -0
- package/browser/examples/jimp-worker.js +13 -0
- package/browser/examples/lenna.png +0 -0
- package/browser/examples/test.html +43 -0
- package/browser/lib/jimp.js +47 -3853
- package/browser/lib/jimp.min.js +2 -12
- package/browser/src/jimp-wrapper.js +45 -43
- package/index.js +583 -149
- package/jimp.d.ts +133 -0
- package/omggif.js +782 -0
- package/package.json +9 -6
- package/resize.js +120 -185
- package/resize2.js +15 -8
- package/browser/img/Landscape_1.jpg +0 -0
- package/browser/img/Landscape_2.jpg +0 -0
- package/browser/img/Landscape_3.jpg +0 -0
- package/browser/img/Landscape_4.jpg +0 -0
- package/browser/img/Portrait_1.jpg +0 -0
- package/browser/img/Portrait_2.jpg +0 -0
- package/browser/img/Portrait_3.jpg +0 -0
- package/browser/img/Portrait_4.jpg +0 -0
- package/browser/index.html +0 -151
- package/browser/jimp-worker.js +0 -71
package/browser/index.html
DELETED
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<head>
|
|
3
|
-
<title>JIMP + Web Worker Proof of Concept</title>
|
|
4
|
-
<script type="text/javascript">
|
|
5
|
-
|
|
6
|
-
// Example jimp-worker.js returns an object of this form to the main thread
|
|
7
|
-
// following a successful processing
|
|
8
|
-
// {
|
|
9
|
-
// type: "IMAGE" or "DATA_URI",
|
|
10
|
-
// data: data,
|
|
11
|
-
// width: w,
|
|
12
|
-
// height: h
|
|
13
|
-
// }
|
|
14
|
-
function workerMessageHandler(e) {
|
|
15
|
-
var returnObject = e.data;
|
|
16
|
-
|
|
17
|
-
if (returnObject.type === "IMAGE") {
|
|
18
|
-
// display the processed image by drawing in a canvas:
|
|
19
|
-
paintImageToCanvas(returnObject.data);
|
|
20
|
-
} else if (e.data.type === "DATA_URI") {
|
|
21
|
-
// display processed image by data URI:
|
|
22
|
-
paintImageFromDataUri(returnObject.data,returnObject.width,returnObject.height);
|
|
23
|
-
} else {
|
|
24
|
-
console.warn("postMessage returned unexpected data:",returnObject);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function createWorkerForImageInput(input,filename){
|
|
29
|
-
var worker = new Worker("jimp-worker.js"),
|
|
30
|
-
start = (new Date()).getTime();
|
|
31
|
-
worker.onmessage = function(e){
|
|
32
|
-
console.log(filename + " processed to "+ e.data.type +" in " + ((new Date()).getTime() - start) + "ms");
|
|
33
|
-
workerMessageHandler(e);
|
|
34
|
-
};
|
|
35
|
-
worker.onerror = function(e){
|
|
36
|
-
var message = "Could not process "+ filename + ': Line '+ e.lineno+ ' in '+ e.filename+ ': '+ e.message;
|
|
37
|
-
alert(message);
|
|
38
|
-
console.warn(message);
|
|
39
|
-
};
|
|
40
|
-
worker.postMessage(input);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function paintImageToCanvas(image){
|
|
44
|
-
var canvas = document.createElement("canvas");
|
|
45
|
-
canvas.width = image.width;
|
|
46
|
-
canvas.height = image.height;
|
|
47
|
-
document.body.appendChild(canvas);
|
|
48
|
-
|
|
49
|
-
var ctx = canvas.getContext("2d");
|
|
50
|
-
var imgData = ctx.createImageData(image.width,image.height);
|
|
51
|
-
|
|
52
|
-
var ubuf = image.data;
|
|
53
|
-
for (var i=0;i < ubuf.length; i+=4) {
|
|
54
|
-
imgData.data[i] = ubuf[i]; //red
|
|
55
|
-
imgData.data[i+1] = ubuf[i+1]; //green
|
|
56
|
-
imgData.data[i+2] = ubuf[i+2]; //blue
|
|
57
|
-
imgData.data[i+3] = ubuf[i+3]; //alpha
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
ctx.putImageData(imgData,0,0);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function paintImageFromDataUri(dataUri,w,h){
|
|
64
|
-
var imagesrc = document.createElement("img");
|
|
65
|
-
imagesrc.src = dataUri;
|
|
66
|
-
imagesrc.width = w;
|
|
67
|
-
imagesrc.height = h;
|
|
68
|
-
document.body.appendChild(imagesrc);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function newFiles(element){
|
|
72
|
-
document.getElementById("resultLabel").style.display = "block";
|
|
73
|
-
|
|
74
|
-
// Chrome allows direct transfer of element.files, but that object is not a
|
|
75
|
-
// "transferable" object in mozilla's world.
|
|
76
|
-
// To speed file transfer in a cross-browser way, readAsArrayBuffer here on the main thread
|
|
77
|
-
// then pass the arraybuffer to the worker thread.
|
|
78
|
-
for (var i=0; i<element.files.length; i++) {
|
|
79
|
-
readFileAndProcess(element.files[i]);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// Closure needed to keep all those filereader variables separate
|
|
83
|
-
function readFileAndProcess(readfile){
|
|
84
|
-
var fr = new FileReader(),
|
|
85
|
-
filename = readfile.name.toString();
|
|
86
|
-
fr.addEventListener("load",function(){
|
|
87
|
-
createWorkerForImageInput(this.result,filename);
|
|
88
|
-
});
|
|
89
|
-
fr.readAsArrayBuffer(readfile);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function loadStaticFiles(){
|
|
94
|
-
document.getElementById("resultLabel").style.display = "block";
|
|
95
|
-
var localFiles = [];
|
|
96
|
-
localFiles.push("Landscape_1.jpg");
|
|
97
|
-
localFiles.push("Landscape_2.jpg");
|
|
98
|
-
localFiles.push("Landscape_3.jpg");
|
|
99
|
-
localFiles.push("Landscape_4.jpg");
|
|
100
|
-
localFiles.push("Portrait_1.jpg");
|
|
101
|
-
localFiles.push("Portrait_2.jpg");
|
|
102
|
-
localFiles.push("Portrait_3.jpg");
|
|
103
|
-
localFiles.push("Portrait_4.jpg");
|
|
104
|
-
for (var i=0; i<localFiles.length; i++) {
|
|
105
|
-
var name = localFiles[i];
|
|
106
|
-
createWorkerForImageInput("img/"+name,name);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
</script>
|
|
111
|
-
<style>
|
|
112
|
-
body {
|
|
113
|
-
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
|
|
114
|
-
}
|
|
115
|
-
canvas,img {
|
|
116
|
-
display: inline;
|
|
117
|
-
margin: 20px;
|
|
118
|
-
vertical-align: middle;
|
|
119
|
-
}
|
|
120
|
-
h3, h4, h5, h6 {
|
|
121
|
-
margin: 0;
|
|
122
|
-
}
|
|
123
|
-
div#resultLabel {
|
|
124
|
-
margin: 30px 0 0 0;
|
|
125
|
-
}
|
|
126
|
-
</style>
|
|
127
|
-
</head>
|
|
128
|
-
<body>
|
|
129
|
-
<h3>Select images to be thumbnailed and greyscaled:</h3>
|
|
130
|
-
<input type="file" multiple="multiple" onchange="newFiles(this);">
|
|
131
|
-
<br><br>
|
|
132
|
-
or, just <a href="#" onclick="event.preventDefault(); loadStaticFiles();">load some images via xhr for the demo.</a>
|
|
133
|
-
<br>
|
|
134
|
-
<div id="resultLabel" style="display: none;">
|
|
135
|
-
<h3 >Generated <img> and <canvas> thumbnails:</h3>
|
|
136
|
-
<h5 >(Color images displayed on canvas, black and white displayed on img. See <jimp-worker class="js"></jimp-worker> to customize)</h5>
|
|
137
|
-
</div>
|
|
138
|
-
<script type="text/javascript">
|
|
139
|
-
function getParameterByName(name) {
|
|
140
|
-
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
|
|
141
|
-
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
|
|
142
|
-
results = regex.exec(location.search);
|
|
143
|
-
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
if (getParameterByName("test") === "true"){
|
|
147
|
-
loadStaticFiles();
|
|
148
|
-
}
|
|
149
|
-
</script>
|
|
150
|
-
</body>
|
|
151
|
-
</html>
|
package/browser/jimp-worker.js
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
importScripts("lib/jimp.min.js");
|
|
2
|
-
|
|
3
|
-
if (!self.Jimp && !window.Jimp) {
|
|
4
|
-
throw new Error("Could not load jimp.min.js in jimp-worker.js");
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
// Do some image processing in Jimp. Syntax exactly the same as
|
|
8
|
-
// https://github.com/oliver-moran/jimp
|
|
9
|
-
// Reading and writing functions are replaced for browser context.
|
|
10
|
-
// See readme at https://github.com/strandedcity/jimp
|
|
11
|
-
self.addEventListener('message', function(e) {
|
|
12
|
-
// Some browsers allow passing of file objects directly from inputs, which would
|
|
13
|
-
// enable doing the file I/O on the worker thread. Browser support is patchy however,
|
|
14
|
-
// so the most compatible strategy is to read the file on the main thread asynchronously,
|
|
15
|
-
// then pass the data here. File I/O is asynchronous on the main thread, and represents
|
|
16
|
-
// a generally small part of the total image-processing task.
|
|
17
|
-
//
|
|
18
|
-
// See https://developer.mozilla.org/en-US/docs/Web/API/Transferable for support of transferables
|
|
19
|
-
// Note that passing an array of Transferables makes the worker incompatible with IE10.
|
|
20
|
-
|
|
21
|
-
// Helpers to Return to the main thread
|
|
22
|
-
var RETURN = {
|
|
23
|
-
IMAGE: "IMAGE",
|
|
24
|
-
DATA_URI: "DATA_URI"
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
Jimp.read(e.data).then(function(image){
|
|
28
|
-
// EXAMPLE 1:
|
|
29
|
-
// Resize the image and return the pixel data back to the main thread
|
|
30
|
-
// <canvas> is required to paint it
|
|
31
|
-
image.contain(200, 200);
|
|
32
|
-
|
|
33
|
-
// Return resized image data to the main thread:
|
|
34
|
-
self.postMessage({
|
|
35
|
-
type: RETURN.IMAGE,
|
|
36
|
-
data: image.bitmap,
|
|
37
|
-
width: image.bitmap.width,
|
|
38
|
-
height: image.bitmap.height
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
// EXAMPLE 2:
|
|
42
|
-
// Continue processing the same image to make more versions!
|
|
43
|
-
// Return a data URI back to the main thread
|
|
44
|
-
var originalMime = image._originalMime,
|
|
45
|
-
targetMimeType = originalMime || Jimp.MIME_JPEG;
|
|
46
|
-
image.contrast(0.6) // Increase contrast
|
|
47
|
-
.brightness(0.1) // Increase brightness a little
|
|
48
|
-
.greyscale() // make it B&W
|
|
49
|
-
.quality(60) // set JPEG quality (only applies to jpegs)
|
|
50
|
-
.getBuffer(targetMimeType,function(mime,data){
|
|
51
|
-
// With access to node's Buffer objects, it's easy to get a base64 string:
|
|
52
|
-
var dataUri = "data:" + targetMimeType + ";base64," + data.toString('base64');
|
|
53
|
-
|
|
54
|
-
// Return data uri to the main thread.
|
|
55
|
-
// Data URIs can be displayed in <img> tags, without <canvas>
|
|
56
|
-
self.postMessage({
|
|
57
|
-
type: RETURN.DATA_URI,
|
|
58
|
-
data: dataUri,
|
|
59
|
-
width: image.bitmap.width,
|
|
60
|
-
height: image.bitmap.height
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
// Good idea to close the worker when you're done
|
|
64
|
-
self.close();
|
|
65
|
-
});
|
|
66
|
-
}).catch(function(err){
|
|
67
|
-
// Prevent the error from getting swallowed in the promise
|
|
68
|
-
setTimeout(function() { throw err; },0);
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
}, false);
|