forked from parallax/jsPDF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jspdf.plugin.png_support.js
executable file
·517 lines (407 loc) · 13.6 KB
/
jspdf.plugin.png_support.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/**@preserve
* ====================================================================
* jsPDF PNG PlugIn
* Copyright (c) 2014 James Robb, https://github.com/jamesbrobb
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* ====================================================================
*/
(function(jsPDFAPI) {
'use strict'
/*
* @see http://www.w3.org/TR/PNG-Chunks.html
*
Color Allowed Interpretation
Type Bit Depths
0 1,2,4,8,16 Each pixel is a grayscale sample.
2 8,16 Each pixel is an R,G,B triple.
3 1,2,4,8 Each pixel is a palette index;
a PLTE chunk must appear.
4 8,16 Each pixel is a grayscale sample,
followed by an alpha sample.
6 8,16 Each pixel is an R,G,B triple,
followed by an alpha sample.
*/
/*
* PNG filter method types
*
* @see http://www.w3.org/TR/PNG-Filters.html
* @see http://www.libpng.org/pub/png/book/chapter09.html
*
* This is what the value 'Predictor' in decode params relates to
*
* 15 is "optimal prediction", which means the prediction algorithm can change from line to line.
* In that case, you actually have to read the first byte off each line for the prediction algorthim (which should be 0-4, corresponding to PDF 10-14) and select the appropriate unprediction algorithm based on that byte.
*
0 None
1 Sub
2 Up
3 Average
4 Paeth
*/
var doesNotHavePngJS = function() {
return typeof PNG !== 'function' || typeof FlateStream !== 'function';
}
, canCompress = function(value) {
return value !== jsPDFAPI.image_compression.NONE && hasCompressionJS();
}
, hasCompressionJS = function() {
var inst = typeof Deflater === 'function';
if(!inst)
throw new Error("requires deflate.js for compression")
return inst;
}
, compressBytes = function(bytes, lineLength, colorsPerPixel, compression) {
var level = 5,
filter_method = filterUp;
switch(compression) {
case jsPDFAPI.image_compression.FAST:
level = 3;
filter_method = filterSub;
break;
case jsPDFAPI.image_compression.MEDIUM:
level = 6;
filter_method = filterAverage;
break;
case jsPDFAPI.image_compression.SLOW:
level = 9;
filter_method = filterPaeth;//uses to sum to choose best filter for each line
break;
}
bytes = applyPngFilterMethod(bytes, lineLength, colorsPerPixel, filter_method);
var header = new Uint8Array(createZlibHeader(level));
var checksum = adler32(bytes);
var deflate = new Deflater(level);
var a = deflate.append(bytes);
var cBytes = deflate.flush();
var len = header.length + a.length + cBytes.length;
var cmpd = new Uint8Array(len + 4);
cmpd.set(header);
cmpd.set(a, header.length);
cmpd.set(cBytes, header.length + a.length);
cmpd[len++] = (checksum >>> 24) & 0xff;
cmpd[len++] = (checksum >>> 16) & 0xff;
cmpd[len++] = (checksum >>> 8) & 0xff;
cmpd[len++] = checksum & 0xff;
return jsPDFAPI.arrayBufferToBinaryString(cmpd);
}
, createZlibHeader = function(bytes, level){
/*
* @see http://www.ietf.org/rfc/rfc1950.txt for zlib header
*/
var cm = 8;
var cinfo = Math.LOG2E * Math.log(0x8000) - 8;
var cmf = (cinfo << 4) | cm;
var hdr = cmf << 8;
var flevel = Math.min(3, ((level - 1) & 0xff) >> 1);
hdr |= (flevel << 6);
hdr |= 0;//FDICT
hdr += 31 - (hdr % 31);
return [cmf, (hdr & 0xff) & 0xff];
}
, adler32 = function(array, param) {
var adler = 1;
var s1 = adler & 0xffff,
s2 = (adler >>> 16) & 0xffff;
var len = array.length;
var tlen;
var i = 0;
while (len > 0) {
tlen = len > param ? param : len;
len -= tlen;
do {
s1 += array[i++];
s2 += s1;
} while (--tlen);
s1 %= 65521;
s2 %= 65521;
}
return ((s2 << 16) | s1) >>> 0;
}
, applyPngFilterMethod = function(bytes, lineLength, colorsPerPixel, filter_method) {
var lines = bytes.length / lineLength,
result = new Uint8Array(bytes.length + lines),
filter_methods = getFilterMethods(),
i = 0, line, prevLine, offset;
for(; i < lines; i++) {
offset = i * lineLength;
line = bytes.subarray(offset, offset + lineLength);
if(filter_method) {
result.set(filter_method(line, colorsPerPixel, prevLine), offset + i);
}else{
var j = 0,
len = filter_methods.length,
results = [];
for(; j < len; j++)
results[j] = filter_methods[j](line, colorsPerPixel, prevLine);
var ind = getIndexOfSmallestSum(results.concat());
result.set(results[ind], offset + i);
}
prevLine = line;
}
return result;
}
, filterNone = function(line, colorsPerPixel, prevLine) {
/*var result = new Uint8Array(line.length + 1);
result[0] = 0;
result.set(line, 1);*/
var result = Array.apply([], line);
result.unshift(0);
return result;
}
, filterSub = function(line, colorsPerPixel, prevLine) {
var result = [],
i = 0,
len = line.length,
left;
result[0] = 1;
for(; i < len; i++) {
left = line[i - colorsPerPixel] || 0;
result[i + 1] = (line[i] - left + 0x0100) & 0xff;
}
return result;
}
, filterUp = function(line, colorsPerPixel, prevLine) {
var result = [],
i = 0,
len = line.length,
up;
result[0] = 2;
for(; i < len; i++) {
up = prevLine && prevLine[i] || 0;
result[i + 1] = (line[i] - up + 0x0100) & 0xff;
}
return result;
}
, filterAverage = function(line, colorsPerPixel, prevLine) {
var result = [],
i = 0,
len = line.length,
left,
up;
result[0] = 3;
for(; i < len; i++) {
left = line[i - colorsPerPixel] || 0;
up = prevLine && prevLine[i] || 0;
result[i + 1] = (line[i] + 0x0100 - ((left + up) >>> 1)) & 0xff;
}
return result;
}
, filterPaeth = function(line, colorsPerPixel, prevLine) {
var result = [],
i = 0,
len = line.length,
left,
up,
upLeft,
paeth;
result[0] = 4;
for(; i < len; i++) {
left = line[i - colorsPerPixel] || 0;
up = prevLine && prevLine[i] || 0;
upLeft = prevLine && prevLine[i - colorsPerPixel] || 0;
paeth = paethPredictor(left, up, upLeft);
result[i + 1] = (line[i] - paeth + 0x0100) & 0xff;
}
return result;
}
,paethPredictor = function(left, up, upLeft) {
var p = left + up - upLeft,
pLeft = Math.abs(p - left),
pUp = Math.abs(p - up),
pUpLeft = Math.abs(p - upLeft);
return (pLeft <= pUp && pLeft <= pUpLeft) ? left : (pUp <= pUpLeft) ? up : upLeft;
}
, getFilterMethods = function() {
return [filterNone, filterSub, filterUp, filterAverage, filterPaeth];
}
,getIndexOfSmallestSum = function(arrays) {
var i = 0,
len = arrays.length,
sum, min, ind;
while(i < len) {
sum = absSum(arrays[i].slice(1));
if(sum < min || !min) {
min = sum;
ind = i;
}
i++;
}
return ind;
}
, absSum = function(array) {
var i = 0,
len = array.length,
sum = 0;
while(i < len)
sum += Math.abs(array[i++]);
return sum;
}
, logImg = function(img) {
console.log("width: " + img.width);
console.log("height: " + img.height);
console.log("bits: " + img.bits);
console.log("colorType: " + img.colorType);
console.log("transparency:");
console.log(img.transparency);
console.log("text:");
console.log(img.text);
console.log("compressionMethod: " + img.compressionMethod);
console.log("filterMethod: " + img.filterMethod);
console.log("interlaceMethod: " + img.interlaceMethod);
console.log("imgData:");
console.log(img.imgData);
console.log("palette:");
console.log(img.palette);
console.log("colors: " + img.colors);
console.log("colorSpace: " + img.colorSpace);
console.log("pixelBitlength: " + img.pixelBitlength);
console.log("hasAlphaChannel: " + img.hasAlphaChannel);
};
jsPDFAPI.processPNG = function(imageData, imageIndex, alias, compression, dataAsBinaryString) {
'use strict'
var colorSpace = this.color_spaces.DEVICE_RGB,
decode = this.decode.FLATE_DECODE,
bpc = 8,
img, dp, trns,
colors, pal, smask;
/* if(this.isString(imageData)) {
}*/
if(this.isArrayBuffer(imageData))
imageData = new Uint8Array(imageData);
if(this.isArrayBufferView(imageData)) {
if(doesNotHavePngJS())
throw new Error("PNG support requires png.js and zlib.js");
img = new PNG(imageData);
imageData = img.imgData;
bpc = img.bits;
colorSpace = img.colorSpace;
colors = img.colors;
//logImg(img);
/*
* colorType 6 - Each pixel is an R,G,B triple, followed by an alpha sample.
*
* colorType 4 - Each pixel is a grayscale sample, followed by an alpha sample.
*
* Extract alpha to create two separate images, using the alpha as a sMask
*/
if([4,6].indexOf(img.colorType) !== -1) {
/*
* processes 8 bit RGBA and grayscale + alpha images
*/
if(img.bits === 8) {
var pixelsArrayType = window['Uint' + img.pixelBitlength + 'Array'],
pixels = new pixelsArrayType(img.decodePixels().buffer),
len = pixels.length,
imgData = new Uint8Array(len * img.colors),
alphaData = new Uint8Array(len),
pDiff = img.pixelBitlength - img.bits,
i = 0, n = 0, pixel, pbl;
for(; i < len; i++) {
pixel = pixels[i];
pbl = 0;
while(pbl < pDiff) {
imgData[n++] = ( pixel >>> pbl ) & 0xff;
pbl = pbl + img.bits;
}
alphaData[i] = ( pixel >>> pbl ) & 0xff;
}
}
/*
* processes 16 bit RGBA and grayscale + alpha images
*/
if(img.bits === 16) {
var pixels = new Uint32Array(img.decodePixels().buffer),
len = pixels.length,
imgData = new Uint8Array((len * (32 / img.pixelBitlength) ) * img.colors),
alphaData = new Uint8Array(len * (32 / img.pixelBitlength) ),
hasColors = img.colors > 1,
i = 0, n = 0, a = 0, pixel;
while(i < len) {
pixel = pixels[i++];
imgData[n++] = (pixel >>> 0) & 0xFF;
if(hasColors) {
imgData[n++] = (pixel >>> 16) & 0xFF;
pixel = pixels[i++];
imgData[n++] = (pixel >>> 0) & 0xFF;
}
alphaData[a++] = (pixel >>> 16) & 0xFF;
}
bpc = 8;
}
if(canCompress(compression)) {
imageData = compressBytes(imgData, img.width * img.colors, img.colors, compression);
smask = compressBytes(alphaData, img.width, 1, compression);
}else{
imageData = imgData;
smask = alphaData;
decode = null;
}
}
/*
* Indexed png. Each pixel is a palette index.
*/
if(img.colorType === 3) {
colorSpace = this.color_spaces.INDEXED;
pal = img.palette;
if(img.transparency.indexed) {
var trans = img.transparency.indexed;
var total = 0,
i = 0,
len = trans.length;
for(; i<len; ++i)
total += trans[i];
total = total / 255;
/*
* a single color is specified as 100% transparent (0),
* so we set trns to use a /Mask with that index
*/
if(total === len - 1 && trans.indexOf(0) !== -1) {
trns = [trans.indexOf(0)];
/*
* there's more than one colour within the palette that specifies
* a transparency value less than 255, so we unroll the pixels to create an image sMask
*/
}else if(total !== len){
var pixels = img.decodePixels(),
alphaData = new Uint8Array(pixels.length),
i = 0,
len = pixels.length;
for(; i < len; i++)
alphaData[i] = trans[pixels[i]];
smask = compressBytes(alphaData, img.width, 1);
}
}
}
if(decode === this.decode.FLATE_DECODE)
dp = '/Predictor 15 /Colors '+ colors +' /BitsPerComponent '+ bpc +' /Columns '+ img.width;
else
//remove 'Predictor' as it applies to the type of png filter applied to its IDAT - we only apply with compression
dp = '/Colors '+ colors +' /BitsPerComponent '+ bpc +' /Columns '+ img.width;
if(this.isArrayBuffer(imageData) || this.isArrayBufferView(imageData))
imageData = this.arrayBufferToBinaryString(imageData);
if(smask && this.isArrayBuffer(smask) || this.isArrayBufferView(smask))
smask = this.arrayBufferToBinaryString(smask);
return this.createImageInfo(imageData, img.width, img.height, colorSpace,
bpc, decode, imageIndex, alias, dp, trns, pal, smask);
}
throw new Error("Unsupported PNG image data, try using JPEG instead.");
}
})(jsPDF.API)