comparison mupdf-source/docs/examples/pdf-dejpx.js @ 3:2c135c81b16c

MERGE: upstream PyMuPDF 1.26.4 with MuPDF 1.26.7
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:44:09 +0200
parents b50eed0cc0ef
children
comparison
equal deleted inserted replaced
0:6015a75abc2d 3:2c135c81b16c
1 // Find all JPEG-2000 images and turn them into regular images.
2
3 var doc = Document.openDocument(scriptArgs[0]);
4
5 function isJPXImage(ref) {
6 if ("Filter" in ref) {
7 var filter = ref.Filter;
8 if (filter == "JPXDecode")
9 return true;
10 if (filter.isArray())
11 for (var i = 0; i < filter.length; ++i)
12 if (filter[i] == "JPXDecode")
13 return true;
14 }
15 return false;
16 }
17
18 var i, n, ref;
19
20 var jpxList = {};
21 var smaskList = {};
22
23 // Preload and destroy all JPX images.
24 n = doc.countObjects();
25 for (i=1; i < n; ++i) {
26 ref = doc.newIndirect(i, 0);
27 if (isJPXImage(ref)) {
28 print("Loading JPX image:", i)
29 jpxList[i] = doc.loadImage(ref);
30 if ("SMask" in ref)
31 smaskList[i] = ref.SMask;
32 ref.writeObject(null); // make sure we don't reuse the JPX image resource
33 }
34 }
35
36 for (i in jpxList) {
37 ref = doc.newIndirect(i, 0);
38 var jpx = jpxList[i];
39 var pix = jpx.toPixmap();
40 var raw = new Image(pix);
41
42 // Create a new image, then copy the data to the old object, then delete it.
43 print("Decompressed image:", i);
44 var img = doc.addImage(raw);
45 if (i in smaskList)
46 img.SMask = smaskList[i];
47 ref.writeObject(img.resolve());
48 ref.writeRawStream(img.readRawStream());
49 doc.deleteObject(img);
50
51 // Invoke the GC to free intermediate pixmaps and images.
52 gc();
53 }
54
55 doc.save(scriptArgs[1], "compress,garbage=compact");