view mupdf-source/docs/examples/pdf-dejpx.js @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
line wrap: on
line source

// Find all JPEG-2000 images and turn them into regular images.

var doc = Document.openDocument(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");