New option 'optimize=y' for utils that take a -jpegopt param, pdftocairo and pdftoppm. This corresponds to the cjpeg -optimize flag, and slightly reduces the size of the output jpeg but uses additional cpu and memory.
New jpegOptimize boolean in splash/SplashBitmap WriteImgParams. New setOptimize method on goo/JpegWriter taking a boolean. Signed-off-by: Martin Packman <gzl...@googlemail.com> --- goo/JpegWriter.cc | 11 +++++++++++ goo/JpegWriter.h | 1 + splash/SplashBitmap.cc | 1 + splash/SplashBitmap.h | 1 + utils/pdftocairo.cc | 10 ++++++++++ utils/pdftoppm.cc | 10 ++++++++++ 6 files changed, 34 insertions(+)
diff --git a/goo/JpegWriter.cc b/goo/JpegWriter.cc index 37c15c2a..9ac9b63b 100644 --- a/goo/JpegWriter.cc +++ b/goo/JpegWriter.cc @@ -25,6 +25,7 @@ extern "C" { struct JpegWriterPrivate { bool progressive; + bool optimize; int quality; JpegWriter::Format format; struct jpeg_compress_struct cinfo; @@ -46,6 +47,7 @@ JpegWriter::JpegWriter(int q, bool p, Format formatA) { priv = new JpegWriterPrivate; priv->progressive = p; + priv->optimize = false; priv->quality = q; priv->format = formatA; } @@ -54,6 +56,7 @@ JpegWriter::JpegWriter(Format formatA) { priv = new JpegWriterPrivate; priv->progressive = false; + priv->optimize = false; priv->quality = -1; priv->format = formatA; } @@ -75,6 +78,11 @@ void JpegWriter::setProgressive(bool progressive) priv->progressive = progressive; } +void JpegWriter::setOptimize(bool optimize) +{ + priv->optimize = optimize; +} + bool JpegWriter::init(FILE *f, int width, int height, int hDPI, int vDPI) { // Setup error handler @@ -137,6 +145,9 @@ bool JpegWriter::init(FILE *f, int width, int height, int hDPI, int vDPI) jpeg_simple_progression(&priv->cinfo); } + // Set whether to compute optimal Huffman coding tables + priv->cinfo.optimize_coding = priv->optimize; + // Get ready for data jpeg_start_compress(&priv->cinfo, TRUE); diff --git a/goo/JpegWriter.h b/goo/JpegWriter.h index 9a68bcc9..bb87b949 100644 --- a/goo/JpegWriter.h +++ b/goo/JpegWriter.h @@ -41,6 +41,7 @@ public: void setQuality(int quality); void setProgressive(bool progressive); + void setOptimize(bool optimize); bool init(FILE *f, int width, int height, int hDPI, int vDPI) override; bool writePointers(unsigned char **rowPointers, int rowCount) override; diff --git a/splash/SplashBitmap.cc b/splash/SplashBitmap.cc index 2bec9f93..6f5e4cf8 100644 --- a/splash/SplashBitmap.cc +++ b/splash/SplashBitmap.cc @@ -356,6 +356,7 @@ void SplashBitmap::setJpegParams(ImgWriter *writer, WriteImgParams* params) #ifdef ENABLE_LIBJPEG if (params) { static_cast<JpegWriter*>(writer)->setProgressive(params->jpegProgressive); + static_cast<JpegWriter*>(writer)->setOptimize(params->jpegOptimize); if (params->jpegQuality >= 0) static_cast<JpegWriter*>(writer)->setQuality(params->jpegQuality); } diff --git a/splash/SplashBitmap.h b/splash/SplashBitmap.h index 092bd4cf..9756164f 100644 --- a/splash/SplashBitmap.h +++ b/splash/SplashBitmap.h @@ -81,6 +81,7 @@ public: int jpegQuality = -1; GBool jpegProgressive = gFalse; GooString tiffCompression; + GBool jpegOptimize = gFalse; }; SplashError writeImgFile(SplashImageFileFormat format, char *fileName, int hDPI, int vDPI, WriteImgParams* params = nullptr); diff --git a/utils/pdftocairo.cc b/utils/pdftocairo.cc index 05ceaa3b..e5e49a19 100644 --- a/utils/pdftocairo.cc +++ b/utils/pdftocairo.cc @@ -129,6 +129,7 @@ static GBool printHelp = gFalse; static GooString jpegOpt; static int jpegQuality = -1; static bool jpegProgressive = false; +static bool jpegOptimize = false; static GooString printer; static GooString printOpt; @@ -369,6 +370,14 @@ static GBool parseJpegOptions() fprintf(stderr, "jpeg progressive option must be \"y\" or \"n\"\n"); return gFalse; } + } else if (opt.cmp("optimize") == 0 || opt.cmp("optimise") == 0) { + jpegOptimize = gFalse; + if (value.cmp("y") == 0) { + jpegOptimize = gTrue; + } else if (value.cmp("n") != 0) { + fprintf(stderr, "jpeg optimize option must be \"y\" or \"n\"\n"); + return gFalse; + } } else { fprintf(stderr, "Unknown jpeg option \"%s\"\n", opt.getCString()); return gFalse; @@ -415,6 +424,7 @@ static void writePageImage(GooString *filename) else writer = new JpegWriter(JpegWriter::RGB); + static_cast<JpegWriter*>(writer)->setOptimize(jpegOptimize); static_cast<JpegWriter*>(writer)->setProgressive(jpegProgressive); if (jpegQuality >= 0) static_cast<JpegWriter*>(writer)->setQuality(jpegQuality); diff --git a/utils/pdftoppm.cc b/utils/pdftoppm.cc index 4d567bd4..8ea81d04 100644 --- a/utils/pdftoppm.cc +++ b/utils/pdftoppm.cc @@ -92,6 +92,7 @@ static GBool tiff = gFalse; static GooString jpegOpt; static int jpegQuality = -1; static bool jpegProgressive = false; +static bool jpegOptimize = false; #ifdef SPLASH_CMYK static GBool overprint = gFalse; #endif @@ -257,6 +258,14 @@ static GBool parseJpegOptions() fprintf(stderr, "jpeg progressive option must be \"y\" or \"n\"\n"); return gFalse; } + } else if (opt.cmp("optimize") == 0 || opt.cmp("optimise") == 0) { + jpegOptimize = gFalse; + if (value.cmp("y") == 0) { + jpegOptimize = gTrue; + } else if (value.cmp("n") != 0) { + fprintf(stderr, "jpeg optimize option must be \"y\" or \"n\"\n"); + return gFalse; + } } else { fprintf(stderr, "Unknown jpeg option \"%s\"\n", opt.getCString()); return gFalse; @@ -286,6 +295,7 @@ static void savePageSlice(PDFDoc *doc, SplashBitmap::WriteImgParams params; params.jpegQuality = jpegQuality; params.jpegProgressive = jpegProgressive; + params.jpegOptimize = jpegOptimize; params.tiffCompression.Set(TiffCompressionStr); if (ppmFile != nullptr) {
_______________________________________________ poppler mailing list poppler@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/poppler