| 1 | /* compile with
|
|---|
| 2 | moc webkit-image.cpp >webkit-image.h
|
|---|
| 3 | g++ webkit-image.cpp -o webkit-image -lQtCore -lQtWebKit -lQtGui -s -O2
|
|---|
| 4 | or under Windows:
|
|---|
| 5 | g++ webkit-image.cpp -o webkit-image -lQtCore4 -lQtWebKit4 -lQtGui4 -s O2
|
|---|
| 6 | adding the correct directories with -L or -I:
|
|---|
| 7 | -I C:\Progra~1\Qt\include -L C:\Progra~1\Qt\lib
|
|---|
| 8 | */
|
|---|
| 9 | #include <QtGui/QApplication>
|
|---|
| 10 | #include <QtCore/QFile>
|
|---|
| 11 | #include <QtCore/QString>
|
|---|
| 12 | #include <QtCore/QDebug>
|
|---|
| 13 | #include <QtWebKit/QWebView>
|
|---|
| 14 |
|
|---|
| 15 | /* using mingw to set binary mode */
|
|---|
| 16 | #ifdef WIN32
|
|---|
| 17 | #include <io.h>
|
|---|
| 18 | #include <fcntl.h>
|
|---|
| 19 | #define BINARYSTDOUT setmode(fileno(stdout), O_BINARY);
|
|---|
| 20 | #else
|
|---|
| 21 | #define BINARYSTDOUT
|
|---|
| 22 | #endif
|
|---|
| 23 |
|
|---|
| 24 | #define WIDTH 2000
|
|---|
| 25 |
|
|---|
| 26 | class Save : public QObject
|
|---|
| 27 | {
|
|---|
| 28 | Q_OBJECT
|
|---|
| 29 | public:
|
|---|
| 30 | Save(QWebView *v) : view(v) {};
|
|---|
| 31 |
|
|---|
| 32 | public slots:
|
|---|
| 33 | void loaded(bool ok)
|
|---|
| 34 | {
|
|---|
| 35 | if(ok)
|
|---|
| 36 | {
|
|---|
| 37 | QImage im = QPixmap::grabWidget(view).toImage();
|
|---|
| 38 | QRgb white = QColor(255,255,255).rgb();
|
|---|
| 39 | /* sometimes the white is shifted one bit */
|
|---|
| 40 | QRgb white2 = QColor(254,254,254).rgb();
|
|---|
| 41 |
|
|---|
| 42 | /* didn't find a way to reduce image directly, so we scan for white background */
|
|---|
| 43 | bool iswhite = true;
|
|---|
| 44 | int xsize = WIDTH;
|
|---|
| 45 | int ysize = WIDTH;
|
|---|
| 46 | for(int x = xsize-1; iswhite && x > 0; --x)
|
|---|
| 47 | {
|
|---|
| 48 | for(int y = 0; iswhite && y < ysize; ++y)
|
|---|
| 49 | {
|
|---|
| 50 | QRgb p = im.pixel(x, y);
|
|---|
| 51 | if(p != white && p != white2 & p)
|
|---|
| 52 | iswhite = false;
|
|---|
| 53 | }
|
|---|
| 54 | if(iswhite)
|
|---|
| 55 | xsize = x;
|
|---|
| 56 | }
|
|---|
| 57 | iswhite = true;
|
|---|
| 58 | for(int y = ysize-1; iswhite && y > 0; --y)
|
|---|
| 59 | {
|
|---|
| 60 | for(int x = 0; iswhite && x < xsize; ++x)
|
|---|
| 61 | {
|
|---|
| 62 | QRgb p = im.pixel(x, y);
|
|---|
| 63 | if(p != white && p != white2 && p)
|
|---|
| 64 | iswhite = false;
|
|---|
| 65 | }
|
|---|
| 66 | if(iswhite)
|
|---|
| 67 | ysize = y;
|
|---|
| 68 | }
|
|---|
| 69 | /* didn't find a way to clip the QImage directly, so we reload it */
|
|---|
| 70 | QPixmap p = QPixmap::grabWidget(view, 0,0,xsize,ysize);
|
|---|
| 71 | QFile f;
|
|---|
| 72 | BINARYSTDOUT
|
|---|
| 73 | if(f.open(stdout, QIODevice::WriteOnly|QIODevice::Unbuffered))
|
|---|
| 74 | {
|
|---|
| 75 | if(!p.save(&f, "JPEG"))
|
|---|
| 76 | {
|
|---|
| 77 | p.save(&f, "PNG");
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 | emit finish();
|
|---|
| 82 | }
|
|---|
| 83 | signals:
|
|---|
| 84 | void finish(void);
|
|---|
| 85 |
|
|---|
| 86 | private:
|
|---|
| 87 | QWebView *view;
|
|---|
| 88 | };
|
|---|
| 89 |
|
|---|
| 90 | #include "webkit-image.h"
|
|---|
| 91 |
|
|---|
| 92 | int main(int argc, char **argv)
|
|---|
| 93 | {
|
|---|
| 94 | if(argc != 2)
|
|---|
| 95 | return 20;
|
|---|
| 96 | QString url = QString(argv[1]);
|
|---|
| 97 |
|
|---|
| 98 | QApplication a( argc, argv );
|
|---|
| 99 | QWebView *view = new QWebView();
|
|---|
| 100 | Save *s = new Save(view);
|
|---|
| 101 |
|
|---|
| 102 | QObject::connect(view, SIGNAL(loadFinished(bool)), s, SLOT(loaded(bool)));
|
|---|
| 103 | QObject::connect(s, SIGNAL(finish(void)), &a, SLOT(quit()));
|
|---|
| 104 | view->resize(WIDTH,WIDTH);
|
|---|
| 105 | view->load(QUrl(url));
|
|---|
| 106 | return a.exec();
|
|---|
| 107 | }
|
|---|