patch for pdftopng
---------- Forwarded message ----------
From: Shen Liang <[email protected]>
Date: Tue, May 26, 2009 at 12:47 PM
Subject: Re: [poppler] Fwd: Donate some codes for poppler
To: Albert Astals Cid <[email protected]>
I'm sorry I got a mistake on the attach.
On Sat, May 23, 2009 at 5:57 PM, Albert Astals Cid <[email protected]> wrote:
> A Divendres, 22 de maig de 2009, Shen Liang va escriure:
>> I'd like to donate some codes for Poppler. I added pdftopng by
>> modifying pdftoppm. The codes were quit dirty now, and I will do more
>> later.
>
> Hi, thanks for contributing to poppler :-)
>
> Unfortunately the zip you attach is not correct.
>
> First for pdftopng you are attaching a shell scripts instead of the code.
>
> Second for already existing files like SplashBitmap.[cc|h] you should attach a
> diff file as generated by git diff if you use git or diff -u if you are
> working on released tarballs.
>
> Albert
>
> _______________________________________________
> poppler mailing list
> [email protected]
> http://lists.freedesktop.org/mailman/listinfo/poppler
>
--
Help Si Chuan Students
帮助四川灾区学生
https://match.sichuanteachers.org
四川教师志愿者组织
--
Help Si Chuan Students
帮助四川灾区学生
https://match.sichuanteachers.org
四川教师志愿者组织
--- /media/disk-1/projects/poppler-0.10.0/splash/SplashBitmap.cc 2008-09-20 22:08:50.000000000 +0800
+++ /media/disk-1/projects/poppler-0.10.0/splash/SplashBitmap.cc 2008-11-30 14:48:10.000000000 +0800
@@ -30,6 +30,7 @@
#include "SplashErrorCodes.h"
#include "SplashBitmap.h"
+#include <png.h>
//------------------------------------------------------------------------
// SplashBitmap
//------------------------------------------------------------------------
@@ -237,3 +238,108 @@
Guchar SplashBitmap::getAlpha(int x, int y) {
return alpha[y * width + x];
}
+
+//To PNG
+SplashError SplashBitmap::writePNGFile(char *fileName) {
+ FILE *f;
+ SplashColorPtr row, p;
+ int x, y;
+
+ if (!(f = fopen(fileName, "wb"))) {
+ return splashErrOpenFile;
+ }
+
+ png_byte color_type;
+ png_byte bit_depth;
+ png_byte interlace_type;
+
+ png_structp png_ptr;
+ png_infop info_ptr;
+
+ png_bytep row_pointers[height];
+ /* initialize stuff */
+ png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+
+ if (!png_ptr)
+ fprintf (stderr, "png_create_write_struct failed");
+
+ info_ptr = png_create_info_struct(png_ptr);
+ if (!info_ptr)
+ fprintf (stderr, "png_create_info_struct failed");
+
+ if (setjmp(png_jmpbuf(png_ptr)))
+ fprintf (stderr, "png_create_info_struct failed");
+
+ png_init_io(png_ptr, f);
+
+ /* write header */
+ if (setjmp(png_jmpbuf(png_ptr)))
+ fprintf (stderr, "Error during writing header");
+
+ bit_depth = 8;
+ color_type = PNG_COLOR_TYPE_RGB;
+ interlace_type = PNG_INTERLACE_ADAM7;
+//PNG_INTERLACE_NONE
+
+ png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, color_type, interlace_type, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
+
+ png_write_info(png_ptr, info_ptr);
+
+ switch (mode) {
+
+ case splashModeMono1:
+ break;
+
+ case splashModeMono8:
+ break;
+
+ case splashModeRGB8:
+ row = data;
+
+ for (y=0; y<height; ++y) {
+// row_pointers[y]= png_malloc(png_ptr, sizeof(row));
+// row_pointers[y] = (png_byte*) row;
+ row_pointers[y] = row;
+// row_pointer = row;
+// png_write_row (png_ptr, row_pointer);
+ row += rowSize;
+ }
+ png_write_image(png_ptr, row_pointers);
+// png_write_rows(png_ptr, row_pointers, height);
+
+ break;
+
+/* case splashModeXBGR8:
+ break;
+
+
+ case splashModeBGR8:
+ break;
+*/
+#if SPLASH_CMYK
+ case splashModeCMYK8:
+ // PNM doesn't support CMYK
+ break;
+#endif
+ }
+
+ /* write bytes */
+ if (setjmp(png_jmpbuf(png_ptr)))
+ fprintf (stderr, "Error during writing bytes");
+
+ /* end write */
+ if (setjmp(png_jmpbuf(png_ptr)))
+ fprintf (stderr, "Error during end of write");
+
+ //png_write_end(png_ptr, NULL);
+ png_write_end(png_ptr, info_ptr);
+
+ /* cleanup heap allocation */
+ png_destroy_write_struct(&png_ptr, &info_ptr);
+// for (y=0; y<height; y++)
+// free(row_pointers[y]);
+// free(row_pointers);
+
+ fclose(f);
+ return splashOk;
+}
//========================================================================
//
// pdftopng.cc
//
// Copyright 2008 SHEN Liang
//
//========================================================================
//#include <aconf.h>
#include <stdio.h>
#include "parseargs.h"
#include "goo/gmem.h"
#include "goo/GooString.h"
#include "GlobalParams.h"
#include "Object.h"
#include "PDFDoc.h"
#include "splash/SplashBitmap.h"
#include "splash/Splash.h"
#include "SplashOutputDev.h"
#include "config.h"
static int firstPage = 1;
static int lastPage = 0;
static int resolution = 150;
static GBool mono = gFalse;
static GBool gray = gFalse;
static char enableT1libStr[16] = "";
static char enableFreeTypeStr[16] = "";
static char antialiasStr[16] = "";
static char vectorAntialiasStr[16] = "";
static char ownerPassword[33] = "";
static char userPassword[33] = "";
static GBool quiet = gFalse;
static char cfgFileName[256] = "";
static GBool printVersion = gFalse;
static GBool printHelp = gFalse;
static ArgDesc argDesc[] = {
{"-f", argInt, &firstPage, 0,
"first page to print"},
{"-l", argInt, &lastPage, 0,
"last page to print"},
{"-r", argInt, &resolution, 0,
"resolution, in DPI (default is 150)"},
{"-mono", argFlag, &mono, 0,
"generate a monochrome PBM file"},
{"-gray", argFlag, &gray, 0,
"generate a grayscale PGM file"},
#if HAVE_T1LIB_H
{"-t1lib", argString, enableT1libStr, sizeof(enableT1libStr),
"enable t1lib font rasterizer: yes, no"},
#endif
#if HAVE_FREETYPE_FREETYPE_H | HAVE_FREETYPE_H
{"-freetype", argString, enableFreeTypeStr, sizeof(enableFreeTypeStr),
"enable FreeType font rasterizer: yes, no"},
#endif
{"-aa", argString, antialiasStr, sizeof(antialiasStr),
"enable font anti-aliasing: yes, no"},
{"-aaVector", argString, vectorAntialiasStr, sizeof(vectorAntialiasStr),
"enable vector anti-aliasing: yes, no"},
{"-opw", argString, ownerPassword, sizeof(ownerPassword),
"owner password (for encrypted files)"},
{"-upw", argString, userPassword, sizeof(userPassword),
"user password (for encrypted files)"},
{"-q", argFlag, &quiet, 0,
"don't print any messages or errors"},
{"-cfg", argString, cfgFileName, sizeof(cfgFileName),
"configuration file to use in place of .xpdfrc"},
{"-v", argFlag, &printVersion, 0,
"print copyright and version info"},
{"-h", argFlag, &printHelp, 0,
"print usage information"},
{"-help", argFlag, &printHelp, 0,
"print usage information"},
{"--help", argFlag, &printHelp, 0,
"print usage information"},
{"-?", argFlag, &printHelp, 0,
"print usage information"},
{NULL}
};
int main(int argc, char *argv[]) {
PDFDoc *doc;
GooString *fileName;
char *ppmRoot;
char ppmFile[512];
GooString *ownerPW, *userPW;
SplashColor paperColor;
SplashOutputDev *splashOut;
GBool ok;
int exitCode;
int pg;
exitCode = 99;
// parse args
ok = parseArgs(argDesc, &argc, argv);
if (mono && gray) {
ok = gFalse;
}
if (!ok || argc != 3 || printVersion || printHelp) {
fprintf(stderr, "pdftoppm version %s\n", PACKAGE_VERSION);
fprintf(stderr, "%s\n", xpdfCopyright);
if (!printVersion) {
printUsage("pdftoppm", "<PDF-file> <PPM-root>", argDesc);
}
goto err0;
}
fileName = new GooString(argv[1]);
ppmRoot = argv[2];
// read config file
globalParams = new GlobalParams();
/* globalParams->setupBaseFonts(NULL);
if (enableT1libStr[0]) {
if (!globalParams->setEnableT1lib(enableT1libStr)) {
fprintf(stderr, "Bad '-t1lib' value on command line\n");
}
}
*/
if (enableFreeTypeStr[0]) {
if (!globalParams->setEnableFreeType(enableFreeTypeStr)) {
fprintf(stderr, "Bad '-freetype' value on command line\n");
}
}
if (antialiasStr[0]) {
if (!globalParams->setAntialias(antialiasStr)) {
fprintf(stderr, "Bad '-aa' value on command line\n");
}
}
if (vectorAntialiasStr[0]) {
if (!globalParams->setVectorAntialias(vectorAntialiasStr)) {
fprintf(stderr, "Bad '-aaVector' value on command line\n");
}
}
if (quiet) {
globalParams->setErrQuiet(quiet);
}
// open PDF file
if (ownerPassword[0]) {
ownerPW = new GooString(ownerPassword);
} else {
ownerPW = NULL;
}
if (userPassword[0]) {
userPW = new GooString(userPassword);
} else {
userPW = NULL;
}
doc = new PDFDoc(fileName, ownerPW, userPW);
if (userPW) {
delete userPW;
}
if (ownerPW) {
delete ownerPW;
}
if (!doc->isOk()) {
exitCode = 1;
goto err1;
}
// get page range
if (firstPage < 1)
firstPage = 1;
if (lastPage < 1 || lastPage > doc->getNumPages())
lastPage = doc->getNumPages();
// write PNG files
if (mono) {
paperColor[0] = 0xff;
splashOut = new SplashOutputDev(splashModeMono1, 1, gFalse, paperColor);
} else if (gray) {
paperColor[0] = 0xff;
splashOut = new SplashOutputDev(splashModeMono8, 1, gFalse, paperColor);
} else {
paperColor[0] = paperColor[1] = paperColor[2] = 0xff;
splashOut = new SplashOutputDev(splashModeRGB8, 1, gFalse, paperColor);
}
splashOut->startDoc(doc->getXRef());
for (pg = firstPage; pg <= lastPage; ++pg) {
doc->displayPage(splashOut, pg, resolution, resolution, 0,
gFalse, gTrue, gFalse);
sprintf(ppmFile, "%.*s-%06d.%s",
(int)sizeof(ppmFile) - 32, ppmRoot, pg,
mono ? "pbm" : gray ? "pgm" : "png");
splashOut->getBitmap()->writePNGFile(ppmFile);
}
delete splashOut;
exitCode = 0;
// clean up
err1:
delete doc;
delete globalParams;
err0:
// check for memory leaks
Object::memCheck(stderr);
gMemReport(stderr);
return exitCode;
}
_______________________________________________
poppler mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/poppler