summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2018-11-12 21:22:46 +0100
committerTor Andersson <tor.andersson@artifex.com>2018-11-13 21:27:06 +0100
commitc816c486dcc68d522035af3a8ebc7efa8fc1d9ad (patch)
treeb37af80252db8140a874af9eb4d27ba6de46e7c5
parentb35df4b2ae4a80c8f770335b813e5cd3203c175b (diff)
downloadmupdf-c816c486dcc68d522035af3a8ebc7efa8fc1d9ad.tar.xz
Add pdf-dejpx.js example script.
-rw-r--r--docs/examples/pdf-dejpx.js55
1 files changed, 55 insertions, 0 deletions
diff --git a/docs/examples/pdf-dejpx.js b/docs/examples/pdf-dejpx.js
new file mode 100644
index 00000000..0497337a
--- /dev/null
+++ b/docs/examples/pdf-dejpx.js
@@ -0,0 +1,55 @@
+// Find all JPEG-2000 images and turn them into regular images.
+
+var doc = new PDFDocument(scriptArgs[0]);
+
+function isJPXImage(ref) {
+ if ("Filter" in ref) {
+ var filter = ref.Filter;
+ if (filter == "JPXDecode")
+ return true;
+ if (filter.isArray())
+ for (var i = 0; i < filter.length; ++i)
+ if (filter[i] == "JPXDecode")
+ return true;
+ }
+ return false;
+}
+
+var i, n, ref;
+
+var jpxList = {};
+var smaskList = {};
+
+// Preload and destroy all JPX images.
+n = doc.countObjects();
+for (i=1; i < n; ++i) {
+ ref = doc.newIndirect(i, 0);
+ if (isJPXImage(ref)) {
+ print("Loading JPX image:", i)
+ jpxList[i] = doc.loadImage(ref);
+ if ("SMask" in ref)
+ smaskList[i] = ref.SMask;
+ ref.writeObject(null); // make sure we don't reuse the JPX image resource
+ }
+}
+
+for (i in jpxList) {
+ ref = doc.newIndirect(i, 0);
+ var jpx = jpxList[i];
+ var pix = jpx.toPixmap();
+ var raw = new Image(pix);
+
+ // Create a new image, then copy the data to the old object, then delete it.
+ print("Decompressed image:", i);
+ var img = doc.addImage(raw);
+ if (i in smaskList)
+ img.SMask = smaskList[i];
+ ref.writeObject(img.resolve());
+ ref.writeRawStream(img.readRawStream());
+ doc.deleteObject(img);
+
+ // Invoke the GC to free intermediate pixmaps and images.
+ gc();
+}
+
+doc.save(scriptArgs[1], "compress,garbage=compact");