@aboutgeo/pdiiif 0.0.0-stage → 0.2.7

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.
Files changed (69) hide show
  1. package/README.md +169 -2
  2. package/dist/__tests__/parser.test.d.ts +1 -0
  3. package/dist/__tests__/pdf.spec.d.ts +1 -0
  4. package/dist/__tests__/region.test.d.ts +1 -0
  5. package/dist/__tests__/setupJest.d.ts +0 -0
  6. package/dist/assets/optimization.worker-C8u4PFfM.js +886 -0
  7. package/dist/assets/optimization.worker-C8u4PFfM.js.map +1 -0
  8. package/dist/convert.d.ts +223 -0
  9. package/dist/download.d.ts +115 -0
  10. package/dist/iiif.d.ts +110 -0
  11. package/dist/index.cjs +65 -0
  12. package/dist/index.cjs.map +1 -0
  13. package/dist/index.d.ts +7 -0
  14. package/dist/index.mjs +11137 -0
  15. package/dist/index.mjs.map +1 -0
  16. package/dist/io.d.ts +75 -0
  17. package/dist/log.d.ts +21 -0
  18. package/dist/metrics.d.ts +10 -0
  19. package/dist/ocr.d.ts +36 -0
  20. package/dist/optimization.d.ts +48 -0
  21. package/dist/optimization.worker.d.ts +1 -0
  22. package/dist/pdf/annos-text.d.ts +19 -0
  23. package/dist/pdf/annos.d.ts +4 -0
  24. package/dist/pdf/common.d.ts +48 -0
  25. package/dist/pdf/generator.d.ts +197 -0
  26. package/dist/pdf/image.d.ts +34 -0
  27. package/dist/pdf/parser.d.ts +131 -0
  28. package/dist/pdf/pkzip.d.ts +23 -0
  29. package/dist/pdf/png.d.ts +34 -0
  30. package/dist/pdf/util.d.ts +18 -0
  31. package/dist/res/licenses.d.ts +7 -0
  32. package/dist/res/srgbColorspace.d.ts +2 -0
  33. package/dist/util.d.ts +8 -0
  34. package/dist/version.d.ts +2 -0
  35. package/package.json +149 -4
  36. package/src/__tests__/fixtures/deep_toc.json +2953 -0
  37. package/src/__tests__/fixtures/flat_toc.json +2917 -0
  38. package/src/__tests__/fixtures/physdim.json +294 -0
  39. package/src/__tests__/fixtures/region_target.json +58 -0
  40. package/src/__tests__/fixtures/wunder.jpg +0 -0
  41. package/src/__tests__/parser.test.ts +42 -0
  42. package/src/__tests__/pdf.spec.ts +216 -0
  43. package/src/__tests__/region.test.ts +194 -0
  44. package/src/__tests__/setupJest.ts +2 -0
  45. package/src/convert.ts +1098 -0
  46. package/src/download.ts +703 -0
  47. package/src/global.d.ts +10 -0
  48. package/src/iiif.ts +605 -0
  49. package/src/index.ts +7 -0
  50. package/src/io.ts +243 -0
  51. package/src/log.ts +51 -0
  52. package/src/metrics.ts +47 -0
  53. package/src/ocr.ts +176 -0
  54. package/src/optimization.ts +201 -0
  55. package/src/optimization.worker.ts +118 -0
  56. package/src/pdf/annos-text.ts +382 -0
  57. package/src/pdf/annos.ts +240 -0
  58. package/src/pdf/common.ts +158 -0
  59. package/src/pdf/generator.ts +1665 -0
  60. package/src/pdf/image.ts +340 -0
  61. package/src/pdf/parser.ts +811 -0
  62. package/src/pdf/pkzip.ts +228 -0
  63. package/src/pdf/png.ts +397 -0
  64. package/src/pdf/util.ts +111 -0
  65. package/src/res/licenses.ts +266 -0
  66. package/src/res/srgbColorspace.ts +405 -0
  67. package/src/types.d.ts +6 -0
  68. package/src/util.ts +55 -0
  69. package/src/version.ts +1 -0
@@ -0,0 +1,340 @@
1
+ /* Based on the `images` modules in `pdfkit` by Devon Govett, licensed under MIT.
2
+ *
3
+ * Ported to TypeScript and modified to better fit a web use case, by using Uint8Array
4
+ * instead of Buffer.
5
+ *
6
+ * https://github.com/foliojs/pdfkit/blob/master/lib/image/jpeg.js
7
+ * https://github.com/foliojs/pdfkit/blob/master/lib/image/png.js
8
+ *
9
+ * MIT LICENSE
10
+ * Copyright (c) 2011 Devon Govett
11
+ *
12
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this
13
+ * software and associated documentation files (the "Software"), to deal in the Software
14
+ * without restriction, including without limitation the rights to use, copy, modify, merge,
15
+ * publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
16
+ * to whom the Software is furnished to do so, subject to the following conditions:
17
+ *
18
+ * The above copyright notice and this permission notice shall be included in all copies or
19
+ * substantial portions of the Software.
20
+ *
21
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
22
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
24
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+ */
27
+ import { zlibSync } from 'fflate';
28
+ import { PdfDictionary, serialize, PdfObject, makeRef } from './common.js';
29
+ import PNG from './png.js';
30
+ import { IS_BIG_ENDIAN } from './util.js';
31
+
32
+ function readUint16BE(buf: Uint8Array, pos = 0): number {
33
+ const val = new Uint16Array(buf.slice(pos, pos + 2).buffer)[0];
34
+ if (IS_BIG_ENDIAN) {
35
+ return val;
36
+ } else {
37
+ // system is little endian, swap bytes in value from buffer
38
+ return ((val & 0xff) << 8) | ((val >> 8) & 0xff);
39
+ }
40
+ }
41
+
42
+ abstract class PdfImage {
43
+ static open(data: Uint8Array): PdfImage {
44
+ if (data.length > 2 && data[0] === 0xff && data[1] === 0xd8) {
45
+ return new JPEGImage(data);
46
+ } else if (
47
+ data.length > 8 &&
48
+ data[0] === 0x89 &&
49
+ data[1] === 0x50 &&
50
+ data[2] === 0x4e &&
51
+ data[3] === 0x47 &&
52
+ data[4] === 0x0d &&
53
+ data[5] === 0x0a &&
54
+ data[6] === 0x1a &&
55
+ data[7] === 0x0a
56
+ ) {
57
+ return new PNGImage(data);
58
+ } else {
59
+ throw new Error('Unknown image format.');
60
+ }
61
+ }
62
+
63
+ abstract toObjects(
64
+ startNum: number,
65
+ isOptional?: boolean,
66
+ optionalTitle?: string,
67
+ optionalDefaultState?: boolean
68
+ ): Array<PdfObject>;
69
+ }
70
+
71
+ export class JPEGImage extends PdfImage {
72
+ static MARKERS = [
73
+ 0xffc0, 0xffc1, 0xffc2, 0xffc3, 0xffc5, 0xffc6, 0xffc7, 0xffc8, 0xffc9,
74
+ 0xffca, 0xffcb, 0xffcc, 0xffcd, 0xffce, 0xffcf,
75
+ ];
76
+ static COLOR_SPACE_MAP = {
77
+ 1: 'DeviceGray',
78
+ 3: 'DeviceRGB',
79
+ 4: 'DeviceCMYK',
80
+ };
81
+ data: Uint8Array;
82
+
83
+ bits: number;
84
+ width: number;
85
+ height: number;
86
+ colorSpace: string;
87
+
88
+ constructor(data: Uint8Array) {
89
+ super();
90
+ let marker;
91
+ this.data = data;
92
+ if (readUint16BE(this.data, 0) !== 0xffd8) {
93
+ throw 'SOI not found in JPEG';
94
+ }
95
+
96
+ let pos = 2;
97
+ while (pos < this.data.length) {
98
+ marker = readUint16BE(this.data, pos);
99
+ pos += 2;
100
+ if (JPEGImage.MARKERS.includes(marker)) {
101
+ break;
102
+ }
103
+ pos += readUint16BE(this.data, pos);
104
+ }
105
+
106
+ if (!marker || !JPEGImage.MARKERS.includes(marker)) {
107
+ throw 'Invalid JPEG.';
108
+ }
109
+ pos += 2;
110
+
111
+ this.bits = this.data[pos++];
112
+ this.height = readUint16BE(this.data, pos);
113
+ pos += 2;
114
+
115
+ this.width = readUint16BE(this.data, pos);
116
+ pos += 2;
117
+
118
+ const channels = this.data[pos++];
119
+ if ([1, 3, 4].indexOf(channels) < 0) {
120
+ throw 'Bad number of channels, only 1, 3 or 4 are supported';
121
+ }
122
+ this.colorSpace = JPEGImage.COLOR_SPACE_MAP[channels as 1 | 3 | 4];
123
+ }
124
+
125
+ toObjects(startNum: number): Array<PdfObject> {
126
+ const obj: PdfDictionary = {
127
+ Type: '/XObject',
128
+ Subtype: '/Image',
129
+ BitsPerComponent: this.bits,
130
+ Width: this.width,
131
+ Height: this.height,
132
+ ColorSpace: `/${this.colorSpace}`,
133
+ Filter: '/DCTDecode',
134
+ Length: this.data.length,
135
+ };
136
+
137
+ // add extra decode params for CMYK images. By swapping the
138
+ // min and max values from the default, we invert the colors. See
139
+ // section 4.8.4 of the spec.
140
+ if (this.colorSpace === 'DeviceCMYK') {
141
+ obj.Decode = [1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0];
142
+ }
143
+ return [{ num: startNum, data: serialize(obj), stream: this.data }];
144
+ }
145
+ }
146
+
147
+ export class PNGImage extends PdfImage {
148
+ label: string | undefined;
149
+ image: PNG;
150
+ width: number;
151
+ height: number;
152
+ imgData: Uint8Array;
153
+
154
+ constructor(data: Uint8Array, label?: string) {
155
+ super();
156
+ this.label = label;
157
+ this.image = new PNG(data);
158
+ this.width = this.image.width;
159
+ this.height = this.image.height;
160
+ this.imgData = this.image.imgData;
161
+ }
162
+
163
+ toObjects(startNum: number): Array<PdfObject> {
164
+ let dataDecoded = false;
165
+
166
+ const hasAlphaChannel = this.image.hasAlphaChannel;
167
+ const isInterlaced = this.image.interlaceMethod === 1;
168
+
169
+ const out: Array<PdfObject> = [];
170
+ let nextNum = startNum;
171
+ const imgObj: PdfObject = {
172
+ num: nextNum++,
173
+ };
174
+ out.push(imgObj);
175
+
176
+ const imgDict: PdfDictionary = {
177
+ Type: '/XObject',
178
+ Subtype: '/Image',
179
+ BitsPerComponent: hasAlphaChannel ? 8 : this.image.bits,
180
+ Width: this.width,
181
+ Height: this.height,
182
+ Filter: '/FlateDecode',
183
+ };
184
+
185
+ if (!hasAlphaChannel) {
186
+ imgDict.DecodeParms = {
187
+ Predictor: isInterlaced ? 1 : 15,
188
+ Colors: this.image.colors,
189
+ BitsPerComponent: this.image.bits,
190
+ Columns: this.width,
191
+ };
192
+ }
193
+
194
+ if (this.image.palette.length === 0) {
195
+ imgDict.ColorSpace = `/${this.image.colorSpace}`;
196
+ } else {
197
+ // embed the color palette in the PDF as an object stream
198
+ const paletteObj: PdfObject = {
199
+ num: nextNum++,
200
+ data: serialize({ Length: this.image.palette.length }),
201
+ stream: new Uint8Array(this.image.palette),
202
+ };
203
+ out.push(paletteObj);
204
+ // build the color space array for the image
205
+ imgDict.ColorSpace = [
206
+ '/Indexed',
207
+ '/DeviceRGB',
208
+ this.image.palette.length / 3 - 1,
209
+ makeRef(paletteObj),
210
+ ];
211
+ }
212
+
213
+ // For PNG color types 0, 2 and 3, the transparency data is stored in
214
+ // a dedicated PNG chunk.
215
+ if (this.image.transparency.grayscale != null) {
216
+ // Use Color Key Masking (spec section 4.8.5)
217
+ // An array with N elements, where N is two times the number of color components.
218
+ const val = this.image.transparency.grayscale;
219
+ imgDict.Mask = [val, val];
220
+ } else if (this.image.transparency.rgb) {
221
+ // Use Color Key Masking (spec section 4.8.5)
222
+ // An array with N elements, where N is two times the number of color components.
223
+ const { rgb } = this.image.transparency;
224
+ const mask = [];
225
+ for (let x of rgb) {
226
+ mask.push(x, x);
227
+ }
228
+
229
+ imgDict.Mask = mask;
230
+ } else if (this.image.transparency.indexed) {
231
+ // Create a transparency SMask for the image based on the data
232
+ // in the PLTE and tRNS sections. See below for details on SMasks.
233
+ const alphaChannelObj = this.loadIndexedAlphaChannel(nextNum++);
234
+ imgDict.SMask = makeRef(alphaChannelObj);
235
+ out.push(alphaChannelObj);
236
+ dataDecoded = true;
237
+ } else if (hasAlphaChannel) {
238
+ // For PNG color types 4 and 6, the transparency data is stored as a alpha
239
+ // channel mixed in with the main image data. Separate this data out into an
240
+ // SMask object and store it separately in the PDF.
241
+ const [imageData, alphaChannelObj] = this.splitAlphaChannel(nextNum++);
242
+ imgObj.stream = zlibSync(imageData);
243
+ imgDict.Length = imgObj.stream?.length ?? 0;
244
+ imgDict.SMask = makeRef(alphaChannelObj);
245
+ out.push(alphaChannelObj);
246
+ }
247
+
248
+ if (isInterlaced && !dataDecoded && !imgObj.stream) {
249
+ imgObj.stream = zlibSync(this.image.decodePixels());
250
+ imgDict.Length = imgObj.stream?.length ?? 0;
251
+ } else if (!imgObj.stream) {
252
+ imgObj.stream = this.imgData;
253
+ imgDict.Length = imgObj.stream.length;
254
+ }
255
+
256
+ imgObj.data = serialize(imgDict);
257
+
258
+ if (out.length < 3) {
259
+ // Add empty dummy objects until we have 3 objects for the image, to stay true
260
+ // to our pre-determined object numbering
261
+ for (let i = out.length; i < 3; i++) {
262
+ out.push({ num: nextNum++ });
263
+ }
264
+ }
265
+ return out;
266
+ }
267
+
268
+ splitAlphaChannel(objNum: number): [Uint8Array, PdfObject] {
269
+ const pixels = this.image.decodePixels();
270
+ const colorCount = this.image.colors;
271
+ const pixelCount = this.width * this.height;
272
+ const imgData = new Uint8Array(pixelCount * colorCount);
273
+ const alphaChannel = new Uint8Array(pixelCount);
274
+
275
+ let i = 0;
276
+ let a = 0;
277
+ let p = 0;
278
+ const len = pixels.length;
279
+ // For 16bit images copy only most significant byte (MSB) - PNG data is always stored in network byte order (MSB first)
280
+ const skipByteCount = this.image.bits === 16 ? 1 : 0;
281
+ while (i < len) {
282
+ for (let colorIndex = 0; colorIndex < colorCount; colorIndex++) {
283
+ imgData[p++] = pixels[i++];
284
+ i += skipByteCount;
285
+ }
286
+ alphaChannel[a++] = pixels[i++];
287
+ i += skipByteCount;
288
+ }
289
+
290
+ const alphaChannelDeflated = zlibSync(alphaChannel);
291
+ return [
292
+ imgData,
293
+ {
294
+ num: objNum,
295
+ data: serialize({
296
+ Type: '/XObject',
297
+ Subtype: '/Image',
298
+ Width: this.width,
299
+ Height: this.height,
300
+ BitsPerComponent: 8,
301
+ Decode: [0, 1],
302
+ ColorSpace: '/DeviceGray',
303
+ Filter: '/FlateDecode',
304
+ Length: alphaChannelDeflated.length,
305
+ }),
306
+ stream: alphaChannelDeflated,
307
+ },
308
+ ];
309
+ }
310
+
311
+ loadIndexedAlphaChannel(objNum: number): PdfObject {
312
+ const pixels = this.image.decodePixels();
313
+ const transparency = this.image.transparency.indexed!;
314
+ const alphaChannel = new Uint8Array(this.width * this.height);
315
+
316
+ let i = 0;
317
+ for (let j = 0, end = pixels.length; j < end; j++) {
318
+ alphaChannel[i++] = transparency[pixels[j]];
319
+ }
320
+
321
+ const alphaChannelDeflated = zlibSync(alphaChannel);
322
+ return {
323
+ num: objNum,
324
+ data: serialize({
325
+ Type: '/XObject',
326
+ Subtype: '/Image',
327
+ Width: this.width,
328
+ Height: this.height,
329
+ BitsPerComponent: 8,
330
+ Decode: [0, 1],
331
+ ColorSpace: '/DeviceGray',
332
+ Filter: '/FlateDecode',
333
+ Length: alphaChannelDeflated.length,
334
+ }),
335
+ stream: alphaChannelDeflated,
336
+ };
337
+ }
338
+ }
339
+
340
+ export default PdfImage;