2010-08-06 01:27:47 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <qpdf/QPDF.hh>
|
2018-06-18 19:06:51 +00:00
|
|
|
#include <qpdf/QPDFPageDocumentHelper.hh>
|
|
|
|
#include <qpdf/QPDFPageObjectHelper.hh>
|
2010-08-06 01:27:47 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
|
|
|
#include <qpdf/Buffer.hh>
|
|
|
|
#include <qpdf/QPDFWriter.hh>
|
2019-06-21 03:35:23 +00:00
|
|
|
#include <qpdf/QIntC.hh>
|
2010-08-06 01:27:47 +00:00
|
|
|
|
|
|
|
static char const* whoami = 0;
|
|
|
|
|
|
|
|
void usage()
|
|
|
|
{
|
|
|
|
std::cerr << "Usage: " << whoami << " infile.pdf outfile.pdf [in-password]"
|
|
|
|
<< std::endl
|
|
|
|
<< "Invert some images in infile.pdf;"
|
|
|
|
<< " write output to outfile.pdf" << std::endl;
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Derive a class from StreamDataProvider to provide updated stream
|
2020-04-07 22:11:00 +00:00
|
|
|
// data. The main purpose of using this object is to avoid having to
|
|
|
|
// allocate memory up front for the objects. We want to replace the
|
|
|
|
// stream data with a function of the original stream data. In order
|
|
|
|
// to do this without actually holding all the images in memory, we
|
2021-02-25 17:32:45 +00:00
|
|
|
// create copies of the streams. Copying the streams doesn't actually
|
|
|
|
// copy the data. Internally, the qpdf library is holding onto the
|
|
|
|
// location of the original stream data, which makes it possible for
|
|
|
|
// the StreamDataProvider to access it when it needs it.
|
2010-08-06 01:27:47 +00:00
|
|
|
class ImageInverter: public QPDFObjectHandle::StreamDataProvider
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~ImageInverter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual void provideStreamData(int objid, int generation,
|
2020-04-07 22:11:00 +00:00
|
|
|
Pipeline* pipeline) override;
|
2010-08-06 01:27:47 +00:00
|
|
|
|
2020-04-08 23:46:10 +00:00
|
|
|
void registerImage(
|
|
|
|
QPDFObjectHandle image,
|
|
|
|
PointerHolder<QPDFObjectHandle::StreamDataProvider> self);
|
2020-04-07 22:11:00 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::map<QPDFObjGen, QPDFObjectHandle> copied_images;
|
2010-08-06 01:27:47 +00:00
|
|
|
};
|
|
|
|
|
2020-04-07 22:11:00 +00:00
|
|
|
void
|
2020-04-08 23:46:10 +00:00
|
|
|
ImageInverter::registerImage(
|
|
|
|
QPDFObjectHandle image,
|
|
|
|
PointerHolder<QPDFObjectHandle::StreamDataProvider> self)
|
2020-04-07 22:11:00 +00:00
|
|
|
{
|
|
|
|
// replaceStreamData requires a pointer holder to the stream data
|
|
|
|
// provider, but there's no way for us to generate one ourselves,
|
2020-04-08 23:46:10 +00:00
|
|
|
// so we have to have it handed to us. Don't be tempted to have
|
|
|
|
// the class contain a PointerHolder to itself as a member. Doing
|
|
|
|
// this will prevent the class from ever being deleted since the
|
|
|
|
// reference count will never drop to zero (and PointerHolder
|
|
|
|
// doesn't have weak references).
|
2020-04-07 22:11:00 +00:00
|
|
|
|
|
|
|
QPDFObjGen og(image.getObjGen());
|
|
|
|
// Store information about the images based on the object and
|
|
|
|
// generation number. Recall that a single image object may be
|
|
|
|
// used more than once, so no need to update the same stream
|
|
|
|
// multiple times.
|
|
|
|
if (this->copied_images.count(og) > 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-02-25 17:32:45 +00:00
|
|
|
this->copied_images[og] = image.copyStream();
|
2020-04-07 22:11:00 +00:00
|
|
|
|
|
|
|
// Register our stream data provider for this stream. Future calls
|
|
|
|
// to getStreamData or pipeStreamData will use the new
|
|
|
|
// information. Provide null for both filter and decode
|
|
|
|
// parameters. Note that this does not mean the image data will be
|
|
|
|
// uncompressed when we write the file. By default, QPDFWriter
|
|
|
|
// will use /FlateDecode for anything that is uncompressed or
|
|
|
|
// filterable in the input QPDF object, so we don't have to deal
|
|
|
|
// with it explicitly here. We could explicitly use /DCTDecode and
|
|
|
|
// write through a DCT filter if we wanted.
|
2020-04-08 23:46:10 +00:00
|
|
|
image.replaceStreamData(self,
|
2020-04-07 22:11:00 +00:00
|
|
|
QPDFObjectHandle::newNull(),
|
|
|
|
QPDFObjectHandle::newNull());
|
|
|
|
}
|
|
|
|
|
2010-08-06 01:27:47 +00:00
|
|
|
void
|
|
|
|
ImageInverter::provideStreamData(int objid, int generation,
|
|
|
|
Pipeline* pipeline)
|
|
|
|
{
|
|
|
|
// Use the object and generation number supplied to look up the
|
|
|
|
// image data. Then invert the image data and write the inverted
|
|
|
|
// data to the pipeline.
|
2020-04-07 22:11:00 +00:00
|
|
|
QPDFObjGen og(objid, generation);
|
2013-06-14 15:58:37 +00:00
|
|
|
PointerHolder<Buffer> data =
|
2020-04-07 22:11:00 +00:00
|
|
|
this->copied_images[og].getStreamData(qpdf_dl_all);
|
2010-09-24 20:45:18 +00:00
|
|
|
size_t size = data->getSize();
|
|
|
|
unsigned char* buf = data->getBuffer();
|
2010-08-06 01:27:47 +00:00
|
|
|
unsigned char ch;
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
2019-06-21 03:35:23 +00:00
|
|
|
ch = QIntC::to_uchar(0xff - buf[i]);
|
2010-08-06 01:27:47 +00:00
|
|
|
pipeline->write(&ch, 1);
|
|
|
|
}
|
|
|
|
pipeline->finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
whoami = QUtil::getWhoami(argv[0]);
|
|
|
|
|
|
|
|
// For libtool's sake....
|
|
|
|
if (strncmp(whoami, "lt-", 3) == 0)
|
|
|
|
{
|
|
|
|
whoami += 3;
|
|
|
|
}
|
|
|
|
|
2013-11-30 03:08:25 +00:00
|
|
|
// For test suite
|
|
|
|
bool static_id = false;
|
|
|
|
if ((argc > 1) && (strcmp(argv[1], " --static-id") == 0))
|
|
|
|
{
|
|
|
|
static_id = true;
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
}
|
|
|
|
|
2010-08-06 01:27:47 +00:00
|
|
|
if (! ((argc == 3) || (argc == 4)))
|
|
|
|
{
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
char const* infilename = argv[1];
|
|
|
|
char const* outfilename = argv[2];
|
|
|
|
char const* password = (argc == 4) ? argv[3] : "";
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
QPDF qpdf;
|
|
|
|
qpdf.processFile(infilename, password);
|
|
|
|
|
|
|
|
ImageInverter* inv = new ImageInverter;
|
|
|
|
PointerHolder<QPDFObjectHandle::StreamDataProvider> p = inv;
|
|
|
|
|
|
|
|
// For each page...
|
2018-06-18 19:06:51 +00:00
|
|
|
std::vector<QPDFPageObjectHelper> pages =
|
|
|
|
QPDFPageDocumentHelper(qpdf).getAllPages();
|
|
|
|
for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin();
|
2010-08-06 01:27:47 +00:00
|
|
|
iter != pages.end(); ++iter)
|
|
|
|
{
|
2018-06-18 19:06:51 +00:00
|
|
|
QPDFPageObjectHelper& page(*iter);
|
2010-08-06 01:27:47 +00:00
|
|
|
// Get all images on the page.
|
|
|
|
std::map<std::string, QPDFObjectHandle> images =
|
2020-12-31 18:23:49 +00:00
|
|
|
page.getImages();
|
2020-04-16 15:43:37 +00:00
|
|
|
for (auto& iter2: images)
|
2010-08-06 01:27:47 +00:00
|
|
|
{
|
2020-04-16 15:43:37 +00:00
|
|
|
QPDFObjectHandle& image = iter2.second;
|
2010-08-06 01:27:47 +00:00
|
|
|
QPDFObjectHandle image_dict = image.getDict();
|
|
|
|
QPDFObjectHandle color_space =
|
|
|
|
image_dict.getKey("/ColorSpace");
|
|
|
|
QPDFObjectHandle bits_per_component =
|
|
|
|
image_dict.getKey("/BitsPerComponent");
|
|
|
|
|
|
|
|
// For our example, we can only work with images 8-bit
|
|
|
|
// grayscale images that we can fully decode. Use
|
|
|
|
// pipeStreamData with a null pipeline to determine
|
|
|
|
// whether the image is filterable. Directly inspect
|
|
|
|
// keys to determine the image type.
|
2017-08-19 13:18:14 +00:00
|
|
|
if (image.pipeStreamData(0, qpdf_ef_compress,
|
2020-04-07 22:11:00 +00:00
|
|
|
qpdf_dl_all) &&
|
2010-08-06 01:27:47 +00:00
|
|
|
color_space.isName() &&
|
|
|
|
bits_per_component.isInteger() &&
|
|
|
|
(color_space.getName() == "/DeviceGray") &&
|
|
|
|
(bits_per_component.getIntValue() == 8))
|
|
|
|
{
|
2020-04-08 23:46:10 +00:00
|
|
|
inv->registerImage(image, p);
|
2020-04-07 22:11:00 +00:00
|
|
|
}
|
2010-08-06 01:27:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write out a new file
|
|
|
|
QPDFWriter w(qpdf, outfilename);
|
2013-11-30 03:08:25 +00:00
|
|
|
if (static_id)
|
2010-08-06 01:27:47 +00:00
|
|
|
{
|
|
|
|
// For the test suite, uncompress streams and use static
|
|
|
|
// IDs.
|
2015-11-01 21:39:15 +00:00
|
|
|
w.setStaticID(true); // for testing only
|
2010-08-06 01:27:47 +00:00
|
|
|
}
|
|
|
|
w.write();
|
|
|
|
std::cout << whoami << ": new file written to " << outfilename
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
catch (std::exception &e)
|
|
|
|
{
|
|
|
|
std::cerr << whoami << " processing file " << infilename << ": "
|
|
|
|
<< e.what() << std::endl;
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|