summaryrefslogtreecommitdiff
path: root/docs/examples/pdf-dejpx.js
blob: 0497337ac1b853d8a5838472645512cbf1dbf693 (plain)
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
// 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");