[PATCH] Add the -jpeg flag to pdftoppm to output to JPEG
Stefan Thomas
thomas at eload24.com
Mon Sep 7 01:55:40 PDT 2009
Added generic ImgWriter class and SplashBitmap::writeImgFile method.
---
goo/ImgWriter.h | 31 ++++++++++++++++
goo/JpegWriter.cc | 95 ++++++++++++++++++++++++++++++++++++++++++++++++
goo/JpegWriter.h | 45 +++++++++++++++++++++++
goo/Makefile.am | 2 +
goo/PNGWriter.cc | 4 +-
goo/PNGWriter.h | 9 +++--
splash/SplashBitmap.cc | 46 ++++++++++++++++-------
splash/SplashBitmap.h | 5 ++-
splash/SplashTypes.h | 10 +++++
utils/pdftoppm.cc | 24 ++++++++++---
10 files changed, 244 insertions(+), 27 deletions(-)
create mode 100644 goo/ImgWriter.h
create mode 100644 goo/JpegWriter.cc
create mode 100644 goo/JpegWriter.h
diff --git a/goo/ImgWriter.h b/goo/ImgWriter.h
new file mode 100644
index 0000000..8d6a05e
--- /dev/null
+++ b/goo/ImgWriter.h
@@ -0,0 +1,31 @@
+//========================================================================
+//
+// ImgWriter.h
+//
+// This file is licensed under the GPLv2 or later
+//
+// Copyright (C) 2009 Stefan Thomas <thomas at eload24.com>
+//
+//========================================================================
+
+#ifndef IMGWRITER_H
+#define IMGWRITER_H
+
+#include <config.h>
+#include <cstdio>
+
+typedef unsigned char img_byte;
+typedef img_byte *img_bytep;
+
+class ImgWriter
+{
+ public:
+ virtual bool init(FILE *f, int width, int height) = 0;
+
+ virtual bool writePointers(img_bytep *rowPointers, int rowCount) = 0;
+ virtual bool writeRow(img_bytep *row) = 0;
+
+ virtual bool close() = 0;
+};
+
+#endif
diff --git a/goo/JpegWriter.cc b/goo/JpegWriter.cc
new file mode 100644
index 0000000..48c0875
--- /dev/null
+++ b/goo/JpegWriter.cc
@@ -0,0 +1,95 @@
+//========================================================================
+//
+// JpegWriter.cc
+//
+// This file is licensed under the GPLv2 or later
+//
+// Copyright (C) 2009 Stefan Thomas <thomas at eload24.com>
+// Copyright (C) 2009 Warren Toomey <wkt at tuhs.org>
+// Copyright (C) 2009 Shen Liang <shenzhuxi at gmail.com>
+// Copyright (C) 2009 Albert Astals Cid <aacid at kde.org>
+//
+//========================================================================
+
+#include "JpegWriter.h"
+
+#ifdef ENABLE_LIBJPEG
+
+#include "poppler/Error.h"
+
+void outputMessage(j_common_ptr cinfo)
+{
+ char buffer[JMSG_LENGTH_MAX];
+
+ // Create the message
+ (*cinfo->err->format_message) (cinfo, buffer);
+
+ // Send it to poppler's error handler
+ error(-1, buffer);
+}
+
+JpegWriter::JpegWriter()
+{
+}
+
+JpegWriter::~JpegWriter()
+{
+ // cleanup
+ jpeg_destroy_compress(&cinfo);
+}
+
+bool JpegWriter::init(FILE *f, int width, int height)
+{
+ // Setup error handler
+ cinfo.err = jpeg_std_error(&jerr);
+ jerr.output_message = &outputMessage;
+
+ // Initialize libjpeg
+ jpeg_create_compress(&cinfo);
+
+ // Set destination file
+ jpeg_stdio_dest(&cinfo, f);
+
+ // Set libjpeg configuration
+ cinfo.image_width = width;
+ cinfo.image_height = height;
+ cinfo.input_components = 3; /* # of color components per pixel */
+ cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
+ jpeg_set_defaults(&cinfo);
+
+ // Set quality
+ //jpeg_set_quality(&cinfo, 80, true);
+
+ // Use progressive mode
+ //jpeg_simple_progression(&cinfo);
+
+ // Get ready for data
+ jpeg_start_compress(&cinfo, TRUE);
+
+ return true;
+}
+
+bool JpegWriter::writePointers(img_bytep *rowPointers, int rowCount)
+{
+ // Write all rows to the file
+ jpeg_write_scanlines(&cinfo, rowPointers, rowCount);
+
+ return true;
+}
+
+bool JpegWriter::writeRow(img_bytep *row)
+{
+ // Write the row to the file
+ jpeg_write_scanlines(&cinfo, row, 1);
+
+ return true;
+}
+
+bool JpegWriter::close()
+{
+ jpeg_finish_compress(&cinfo);
+
+ return true;
+}
+
+#endif
diff --git a/goo/JpegWriter.h b/goo/JpegWriter.h
new file mode 100644
index 0000000..1206f9f
--- /dev/null
+++ b/goo/JpegWriter.h
@@ -0,0 +1,45 @@
+//========================================================================
+//
+// JpegWriter.h
+//
+// This file is licensed under the GPLv2 or later
+//
+// Copyright (C) 2009 Stefan Thomas <thomas at eload24.com>
+// Copyright (C) 2009 Warren Toomey <wkt at tuhs.org>
+// Copyright (C) 2009 Shen Liang <shenzhuxi at gmail.com>
+// Copyright (C) 2009 Albert Astals Cid <aacid at kde.org>
+//
+//========================================================================
+
+#ifndef JPEGWRITER_H
+#define JPEGWRITER_H
+
+#include <config.h>
+
+#ifdef ENABLE_LIBJPEG
+
+#include <cstdio>
+#include <jpeglib.h>
+#include "ImgWriter.h"
+
+class JpegWriter : public ImgWriter
+{
+ public:
+ JpegWriter();
+ ~JpegWriter();
+
+ bool init(FILE *f, int width, int height);
+
+ bool writePointers(img_bytep *rowPointers, int rowCount);
+ bool writeRow(img_bytep *row);
+
+ bool close();
+
+ private:
+ struct jpeg_compress_struct cinfo;
+ struct jpeg_error_mgr jerr;
+};
+
+#endif
+
+#endif
diff --git a/goo/Makefile.am b/goo/Makefile.am
index f10fd15..ad11af5 100644
--- a/goo/Makefile.am
+++ b/goo/Makefile.am
@@ -15,6 +15,7 @@ poppler_goo_include_HEADERS = \
gfile.h \
FixedPoint.h \
PNGWriter.h \
+ JpegWriter.h \
gstrtod.h
endif
@@ -32,4 +33,5 @@ libgoo_la_SOURCES = \
gmem.cc \
FixedPoint.cc \
PNGWriter.cc \
+ JpegWriter.cc \
gstrtod.cc
diff --git a/goo/PNGWriter.cc b/goo/PNGWriter.cc
index 864c976..9140ebf 100644
--- a/goo/PNGWriter.cc
+++ b/goo/PNGWriter.cc
@@ -71,7 +71,7 @@ bool PNGWriter::init(FILE *f, int width, int height)
return true;
}
-bool PNGWriter::writePointers(png_bytep *rowPointers)
+bool PNGWriter::writePointers(img_bytep *rowPointers, int rowCount)
{
png_write_image(png_ptr, rowPointers);
/* write bytes */
@@ -83,7 +83,7 @@ bool PNGWriter::writePointers(png_bytep *rowPointers)
return true;
}
-bool PNGWriter::writeRow(png_bytep *row)
+bool PNGWriter::writeRow(img_bytep *row)
{
// Write the row to the file
png_write_rows(png_ptr, row, 1);
diff --git a/goo/PNGWriter.h b/goo/PNGWriter.h
index 0540bd7..7f62a8e 100644
--- a/goo/PNGWriter.h
+++ b/goo/PNGWriter.h
@@ -19,8 +19,9 @@
#include <cstdio>
#include <png.h>
+#include "ImgWriter.h"
-class PNGWriter
+class PNGWriter : public ImgWriter
{
public:
PNGWriter();
@@ -28,8 +29,8 @@ class PNGWriter
bool init(FILE *f, int width, int height);
- bool writePointers(png_bytep *rowPointers);
- bool writeRow(png_bytep *row);
+ bool writePointers(img_bytep *rowPointers, int rowCount);
+ bool writeRow(img_bytep *row);
bool close();
@@ -40,4 +41,4 @@ class PNGWriter
#endif
-#endif
\ No newline at end of file
+#endif
diff --git a/splash/SplashBitmap.cc b/splash/SplashBitmap.cc
index 25a71a0..661112e 100644
--- a/splash/SplashBitmap.cc
+++ b/splash/SplashBitmap.cc
@@ -14,6 +14,7 @@
// Copyright (C) 2006, 2009 Albert Astals Cid <aacid at kde.org>
// Copyright (C) 2007 Ilmari Heikkinen <ilmari.heikkinen at gmail.com>
// Copyright (C) 2009 Shen Liang <shenzhuxi at gmail.com>
+// Copyright (C) 2009 Stefan Thomas <thomas at eload24.com>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
@@ -32,7 +33,8 @@
#include "SplashErrorCodes.h"
#include "SplashBitmap.h"
#include "poppler/Error.h"
-#include "PNGWriter.h"
+#include "goo/JpegWriter.h"
+#include "goo/PNGWriter.h"
//------------------------------------------------------------------------
// SplashBitmap
@@ -244,7 +246,7 @@ Guchar SplashBitmap::getAlpha(int x, int y) {
return alpha[y * width + x];
}
-SplashError SplashBitmap::writePNGFile(char *fileName) {
+SplashError SplashBitmap::writeImgFile(SplashImageType type, char *fileName) {
FILE *f;
SplashError e;
@@ -252,23 +254,40 @@ SplashError SplashBitmap::writePNGFile(char *fileName) {
return splashErrOpenFile;
}
- e = writePNGFile(f);
+ e = writeImgFile(type, f);
fclose(f);
return e;
}
-SplashError SplashBitmap::writePNGFile(FILE *f) {
-#ifndef ENABLE_LIBPNG
- error(-1, "PNG support not compiled in");
- return splashErrGeneric;
-#else
+SplashError SplashBitmap::writeImgFile(SplashImageType type, FILE *f) {
+ ImgWriter *writer;
+
+ switch (type) {
+ #ifdef ENABLE_LIBPNG
+ case splashTypePng:
+ writer = (ImgWriter*)new PNGWriter();
+ break;
+ #endif
+
+ #ifdef ENABLE_LIBJPEG
+ case splashTypeJpeg:
+ writer = (ImgWriter*)new JpegWriter();
+ break;
+ #endif
+
+ default:
+ // Not the greatest error message, but users of this function should
+ // have already checked whether their desired format is compiled in.
+ error(-1, "Support for this image type not compiled in");
+ return splashErrGeneric;
+ }
+
if (mode != splashModeRGB8 && mode != splashModeMono8 && mode != splashModeMono1) {
error(-1, "unsupported SplashBitmap mode");
return splashErrGeneric;
}
- PNGWriter *writer = new PNGWriter();
if (!writer->init(f, width, height)) {
delete writer;
return splashErrGeneric;
@@ -278,14 +297,14 @@ SplashError SplashBitmap::writePNGFile(FILE *f) {
case splashModeRGB8:
{
SplashColorPtr row;
- png_bytep *row_pointers = new png_bytep[height];
+ img_bytep *row_pointers = new img_bytep[height];
row = data;
for (int y = 0; y < height; ++y) {
row_pointers[y] = row;
row += rowSize;
}
- if (!writer->writePointers(row_pointers)) {
+ if (!writer->writePointers(row_pointers, height)) {
delete[] row_pointers;
delete writer;
return splashErrGeneric;
@@ -296,7 +315,7 @@ SplashError SplashBitmap::writePNGFile(FILE *f) {
case splashModeMono8:
{
- png_byte *row = new png_byte[3 * width];
+ img_byte *row = new img_byte[3 * width];
for (int y = 0; y < height; y++) {
// Convert into a PNG row
for (int x = 0; x < width; x++) {
@@ -317,7 +336,7 @@ SplashError SplashBitmap::writePNGFile(FILE *f) {
case splashModeMono1:
{
- png_byte *row = new png_byte[3 * width];
+ img_byte *row = new img_byte[3 * width];
for (int y = 0; y < height; y++) {
// Convert into a PNG row
for (int x = 0; x < width; x++) {
@@ -349,5 +368,4 @@ SplashError SplashBitmap::writePNGFile(FILE *f) {
delete writer;
return splashOk;
-#endif
}
diff --git a/splash/SplashBitmap.h b/splash/SplashBitmap.h
index 5085763..16061bb 100644
--- a/splash/SplashBitmap.h
+++ b/splash/SplashBitmap.h
@@ -28,6 +28,7 @@
#endif
#include "SplashTypes.h"
+#include "goo/ImgWriter.h"
#include <stdio.h>
//------------------------------------------------------------------------
@@ -58,8 +59,8 @@ public:
SplashError writePNMFile(char *fileName);
SplashError writePNMFile(FILE *f);
- SplashError writePNGFile(char *fileName);
- SplashError writePNGFile(FILE *f);
+ SplashError writeImgFile(SplashImageType type, char *fileName);
+ SplashError writeImgFile(SplashImageType type, FILE *f);
void getPixel(int x, int y, SplashColorPtr pixel);
Guchar getAlpha(int x, int y);
diff --git a/splash/SplashTypes.h b/splash/SplashTypes.h
index a868652..0b10f72 100644
--- a/splash/SplashTypes.h
+++ b/splash/SplashTypes.h
@@ -150,4 +150,14 @@ struct SplashScreenParams {
typedef int SplashError;
+
+//------------------------------------------------------------------------
+// image file formats
+//------------------------------------------------------------------------
+
+enum SplashImageType {
+ splashTypeJpeg,
+ splashTypePng
+};
+
#endif
diff --git a/utils/pdftoppm.cc b/utils/pdftoppm.cc
index 38c26fd..f2013ee 100644
--- a/utils/pdftoppm.cc
+++ b/utils/pdftoppm.cc
@@ -36,6 +36,8 @@
#include "splash/SplashBitmap.h"
#include "splash/Splash.h"
#include "SplashOutputDev.h"
+#include "goo/JpegWriter.h"
+#include "goo/PNGWriter.h"
#define PPM_FILE_SZ 512
@@ -56,6 +58,7 @@ static GBool useCropBox = gFalse;
static GBool mono = gFalse;
static GBool gray = gFalse;
static GBool png = gFalse;
+static GBool jpeg = gFalse;
static char enableFreeTypeStr[16] = "";
static char antialiasStr[16] = "";
static char vectorAntialiasStr[16] = "";
@@ -105,6 +108,10 @@ static const ArgDesc argDesc[] = {
{"-png", argFlag, &png, 0,
"generate a PNG file"},
#endif
+#if ENABLE_LIBJPEG
+ {"-jpeg", argFlag, &jpeg, 0,
+ "generate a JPEG file"},
+#endif
#if HAVE_FREETYPE_FREETYPE_H | HAVE_FREETYPE_H
{"-freetype", argString, enableFreeTypeStr, sizeof(enableFreeTypeStr),
"enable FreeType font rasterizer: yes, no"},
@@ -140,6 +147,9 @@ static void savePageSlice(PDFDoc *doc,
int pg, int x, int y, int w, int h,
double pg_w, double pg_h,
char *ppmFile) {
+ SplashBitmap *bitmap = splashOut->getBitmap();
+ ImgWriter *writer;
+
if (w == 0) w = (int)ceil(pg_w);
if (h == 0) h = (int)ceil(pg_h);
w = (x+w > pg_w ? (int)ceil(pg_w-x) : w);
@@ -152,15 +162,19 @@ static void savePageSlice(PDFDoc *doc,
);
if (ppmFile != NULL) {
if (png) {
- splashOut->getBitmap()->writePNGFile(ppmFile);
+ bitmap->writeImgFile(splashTypePng, ppmFile);
+ } else if (jpeg) {
+ bitmap->writeImgFile(splashTypeJpeg, ppmFile);
} else {
- splashOut->getBitmap()->writePNMFile(ppmFile);
+ bitmap->writePNMFile(ppmFile);
}
} else {
if (png) {
- splashOut->getBitmap()->writePNGFile(stdout);
+ bitmap->writeImgFile(splashTypePng, stdout);
+ } else if (jpeg) {
+ bitmap->writeImgFile(splashTypeJpeg, stdout);
} else {
- splashOut->getBitmap()->writePNMFile(stdout);
+ bitmap->writePNMFile(stdout);
}
}
}
@@ -301,7 +315,7 @@ int main(int argc, char *argv[]) {
if (ppmRoot != NULL) {
snprintf(ppmFile, PPM_FILE_SZ, "%.*s-%0*d.%s",
PPM_FILE_SZ - 32, ppmRoot, pg_num_len, pg,
- png ? "png" : mono ? "pbm" : gray ? "pgm" : "ppm");
+ png ? "png" : jpeg ? "jpg" : mono ? "pbm" : gray ? "pgm" : "ppm");
savePageSlice(doc, splashOut, pg, x, y, w, h, pg_w, pg_h, ppmFile);
} else {
savePageSlice(doc, splashOut, pg, x, y, w, h, pg_w, pg_h, NULL);
--
1.6.0.4
--------------070301030007040103020208--
More information about the poppler
mailing list