Skip to content

Instantly share code, notes, and snippets.

Created October 25, 2012 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3952671 to your computer and use it in GitHub Desktop.
Save anonymous/3952671 to your computer and use it in GitHub Desktop.
XPDF rendering to jpeg
#include <stdio.h>
#include <cstdio>
#include <cstdlib>
#include <assert.h>
#include <stdexcept>
extern "C" {
#include <jpeglib.h>
}
//xpdf includes
#include "GlobalParams.h"
#include "xpdf.h"
#include "xpdf/SplashOutputDev.h"
#include "splash/SplashBitmap.h"
//Stuff you'll want to tweak
#define IN_PDF "./input.pdf"
//resolution in DPI
#define RESOLUTION 300
#define OUT_FILE "./out%i.jpeg"
#define JPEG_BASELINE true
//jpeg quality from 0 to 100
#define JPEG_QUALITY 100
void libjpeg_error_exit(j_common_ptr cinfo) {
printf("TODO:handle jpeg error");
}
void libjpeg_output_message(j_common_ptr cinfo) {
printf("TODO:libjpeg_output_message");
}
//quick and dirty func to dump a SplashOutputDev to a jpeg file
//You may want to rely on OpenCV or QT for this
void write_jpeg(SplashOutputDev *dev, char *filename) {
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
SplashBitmap *bmp = dev->getBitmap();
int width = bmp->getWidth();
int height = bmp->getHeight();
unsigned char *dataPtr = bmp->getDataPtr();
unsigned int stride = width * 3;
FILE *fp = NULL;
cinfo.err = jpeg_std_error(&jerr);
jerr.error_exit = libjpeg_error_exit;
jerr.output_message = libjpeg_output_message;
jpeg_create_compress(&cinfo);
assert(sizeof(JSAMPLE) == 1);
cinfo.client_data = NULL; //Put anything here you would like to use in jpeg error handling funcs
jpeg_set_quality(&cinfo, JPEG_QUALITY, JPEG_BASELINE);
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = 3;
cinfo.in_color_space = (J_COLOR_SPACE) JCS_RGB;
jpeg_set_defaults(&cinfo);
//eo header
if ((fp = fopen(filename, "wb")) == NULL)
throw std::runtime_error("Cannot open jpeg output file");
jpeg_stdio_dest(&cinfo, fp);
jpeg_start_compress(&cinfo, true);
while (cinfo.next_scanline < cinfo.image_height) {
jpeg_write_scanlines(&cinfo, &dataPtr, 1);
dataPtr += stride;
}
jpeg_finish_compress(&cinfo);
fclose(fp);
jpeg_destroy_compress(&cinfo);
}
int main(int argc, char **argv) {
globalParams = new GlobalParams("");
globalParams->setupBaseFonts(NULL);
globalParams->setTextEncoding("UTF-8");
GString *infile = new GString(IN_PDF);
GString userpw("");
PDFDoc *pdoc = new PDFDoc(infile, &userpw);
if (((PDFDoc*) pdoc)->isOk() == gFalse) {
printf("Failed to open PDF file %s", IN_PDF);
delete ((PDFDoc*) pdoc);
return 2;
}
unsigned char defaultPaperColor[] = { 0xFF, 0xFF, 0xFF };
//I assume yout RGB , you could also choose grayscale or CMYK
SplashColorMode spColorMode = splashModeRGB8;
SplashOutputDev *surf = new SplashOutputDev(spColorMode, 1, gFalse,
&defaultPaperColor[0]);
surf->startDoc(pdoc->getXRef());
int numPages = ((PDFDoc*) pdoc)->getNumPages();
char buf[1024];
for (int pi = 1; pi <= numPages; pi++) {
pdoc->displayPage(surf, pi, RESOLUTION, RESOLUTION, 0, 1, 1, 0, 0, 0);
sprintf(buf, OUT_FILE, pi);
write_jpeg(surf, buf);
}
delete pdoc;
delete globalParams;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment