PDA

View Full Version : QImage loadFromData not run... XPDF data



patrik08
24th April 2007, 11:47
I try to load a Portable pixmap to QImage
http://en.wikipedia.org/wiki/Portable_Pixmap_file_format and QImage not load ..

I hack the XPDF 3.0.2 to enable build on QT4.3 i build on Win Mingw

http://ppk.ciz.ch/qt_c++/XPDF_2_QT.tar.bz2 400kb

the hard work was the pro file to accept minumum to make Portable pixmap


only lib give no problem. main apps say ...

main.cpp: In function `int main(int, char**)':
main.cpp:54: error: conversion from `SplashBitmap*' to non-scalar type `QByteArr
ay' requested





#include <QFile>
#include <QtCore>
#include <QCoreApplication>

#include <aconf.h>
#include <stdio.h>
#include "parseargs.h"
#include "GString.h"
#include "Object.h"
#include "PDFDoc.h"
#include "SplashBitmap.h"
#include "Splash.h"
#include "SplashOutputDev.h"
#include "config.h"
#include <iostream>
#include <QImage>
using namespace std;

/* xpdf 3.02 PDF 1.4 dir and libs hacked to QT4 get http://ppk.ciz.ch/qt_c++/XPDF_2_QT.tar.bz2 400kb */


int main(int argc, char *argv[]) {
QCoreApplication a( argc, argv );

PDFDoc *doc;
GString *fileName;
GString *ownerPW, *userPW;
SplashColor paperColor;
SplashOutputDev *splashOut;
GBool ok;
int resolution = 300;
ownerPW = NULL;
userPW = NULL;
int sumpage;

const QString filepdf = argv[1];
if (filepdf.size() < 1) {
cout << "Usage:\n";
cout << "appsname pdf_file";

return 0;
} else {
/* build the class ! */
fileName = new GString(new GString(filepdf)); /* GString is hack to accept QString */
doc = new PDFDoc(fileName, ownerPW, userPW);
paperColor[0] = paperColor[1] = paperColor[2] = 0xff;
splashOut = new SplashOutputDev(splashModeRGB8, 1, gFalse, paperColor);
sumpage = doc->getNumPages();
splashOut->startDoc(doc->getXRef());

for (int i=0;i<sumpage;i++) {
int pagenr = i + 1; /* render page pdf nr. */
doc->displayPage(splashOut, pagenr, resolution, resolution, 0, gFalse, gTrue, gFalse);
QByteArray dat = splashOut->takeBitmap();
QImage pdfpage;
pdfpage.loadFromData(dat);
/* if ok save .... */
if (!pdfpage.isNull()) {
const QString filepage = QString("Page%1.png").arg(pagenr);
pdfpage.save(filepage,"PNG",100);
}

}

/* remove class */
delete splashOut;
delete doc;

}
return 0;
}

marcel
24th April 2007, 11:53
Could we see the SplashBitmap type?
You cannot simply convert from SplashBitmap to QByteArray.

Also, use QString::toAscii().constData() when you build the GString. So many allocations aren't good :).

Regards

wysota
24th April 2007, 11:55
If you want PPM support, why not use libppm to implement an imageformat plugin for Qt?

marcel
24th April 2007, 12:00
fileName = new GString(new GString(filepdf)); /* GString is hack to accept QString */

Actually, I don't think this is going to work at all. You will get an invalid GString.
GString also has ha constructor GString( const char *). So you can pass filePdf.toAscii().constData() instead.

regards

patrik08
24th April 2007, 12:31
fileName = new GString(new GString(filepdf)); /* GString is hack to accept QString */

Actually, I don't think this is going to work at all. You will get an invalid GString.
GString also has ha constructor GString( const char *). So you can pass filePdf.toAscii().constData() instead.

regards

no this is not problem .... if
qDebug() << "### doc->isOk() "<< doc->isOk();

say true....

i suppose the GlobalParams.h from xpdf is not enable to run on all OS....
and a default setting not exist on this class....



this run... GString support QString now ...

i cute out 200 line and insert QT proper....



GString::GString(const QString in) {
int n = in.size();
s = NULL;
QByteArray der = in.toAscii();
const char *sA = der.data();
resize(length = n);
memcpy(s, sA, n + 1);
}


other .... ...

GString *getHomeDir() {

const QString st = QDir::homePath();

return new GString(st);
}

GString *getCurrentDir() {
const QString st = QDir::currentPath();
return new GString(st);
}

GString *appendToPath(GString *path, char *fileName) {

QString xpat = path->QValue();
if (!xpat.endsWith("/")) {
xpat.append("/");
}
xpat.append(fileName);
const QString st = xpat;
return new GString(st);
}

GString *grabPath(char *fileName) {

QString sfile = QString(fileName);
QFileInfo now(sfile);
QString xpat = now.absolutePath();
return new GString(xpat);
}

GBool isAbsolutePath(char *path) {

QString spat = QString(path);
QDir now(spat);
return now.isAbsolute();
}

GString *makePathAbsolute(GString *path) {

QString xpati = path->QValue();
QDir now(xpati);
QString xpat = now.absolutePath();
return new GString(xpat);
}

time_t getModTime(char *fileName) {

QString sfile = QString(fileName);

QFileInfo now(sfile);
QDateTime filet = now.lastModified();
return filet.toTime_t();
}

marcel
24th April 2007, 12:36
Well, it's OK then. I thought you're using the unmodified GString from xpdf.

What about the SplashBitmap?

patrik08
24th April 2007, 12:46
If you want PPM support, why not use libppm to implement an imageformat plugin for Qt?


QT4 already read PPM ...

i found on

QList<QByteArray> formats = QImageReader::supportedImageFormats();

I need pdf and postscript eps to image.....
Write a plug-in is not a game... :(

patrik08
24th April 2007, 12:51
Well, it's OK then. I thought you're using the unmodified GString from xpdf.

What about the SplashBitmap?

You think to take out data from here ...

this run and write file....



grab data from this??

case splashModeRGB8:

How?






SplashError SplashBitmap::writePNMFile(char *fileName) {
FILE *f;
SplashColorPtr row, p;
int x, y;

if (!(f = fopen(fileName, "wb"))) {
return splashErrOpenFile;
}

switch (mode) {

case splashModeMono1:
fprintf(f, "P4\n%d %d\n", width, height);
row = data;
for (y = 0; y < height; ++y) {
p = row;
for (x = 0; x < width; x += 8) {
fputc(*p ^ 0xff, f);
++p;
}
row += rowSize;
}
break;

case splashModeMono8:
fprintf(f, "P5\n%d %d\n255\n", width, height);
row = data;
for (y = 0; y < height; ++y) {
p = row;
for (x = 0; x < width; ++x) {
fputc(*p, f);
++p;
}
row += rowSize;
}
break;

case splashModeRGB8:
fprintf(f, "P6\n%d %d\n255\n", width, height);
row = data;
for (y = 0; y < height; ++y) {
p = row;
for (x = 0; x < width; ++x) {
fputc(splashRGB8R(p), f);
fputc(splashRGB8G(p), f);
fputc(splashRGB8B(p), f);
p += 3;
}
row += rowSize;
}
break;

case splashModeBGR8:
fprintf(f, "P6\n%d %d\n255\n", width, height);
row = data;
for (y = 0; y < height; ++y) {
p = row;
for (x = 0; x < width; ++x) {
fputc(splashBGR8R(p), f);
fputc(splashBGR8G(p), f);
fputc(splashBGR8B(p), f);
p += 3;
}
row += rowSize;
}
break;

#if SPLASH_CMYK
case splashModeCMYK8:
// PNM doesn't support CMYK
break;
#endif
}

fclose(f);
return splashOk;
}