| 1 | /* compile with
|
|---|
| 2 | moc webkit-image.cpp >webkit-image.h
|
|---|
| 3 | g++ webkit-image.cpp -o webkit-image -lQtCore -lQtWebKit -lQtGui
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | #include <QtGui/QApplication>
|
|---|
| 7 | #include <QtCore/QFile>
|
|---|
| 8 | #include <QtCore/QString>
|
|---|
| 9 | #include <QtCore/QDebug>
|
|---|
| 10 | #include <QtWebKit/QWebView>
|
|---|
| 11 |
|
|---|
| 12 | #define WIDTH 2000
|
|---|
| 13 |
|
|---|
| 14 | class Save : public QObject
|
|---|
| 15 | {
|
|---|
| 16 | Q_OBJECT
|
|---|
| 17 | public:
|
|---|
| 18 | Save(QWebView *v) : view(v) {};
|
|---|
| 19 |
|
|---|
| 20 | public slots:
|
|---|
| 21 | void loaded(bool ok)
|
|---|
| 22 | {
|
|---|
| 23 | if(ok)
|
|---|
| 24 | {
|
|---|
| 25 | QImage im = QPixmap::grabWidget(view).toImage();
|
|---|
| 26 | QRgb white = QColor(255,255,255).rgb();
|
|---|
| 27 |
|
|---|
| 28 | /* didn't find a way to reduce image directly, so we scan for white background */
|
|---|
| 29 | bool iswhite = true;
|
|---|
| 30 | int xsize = WIDTH;
|
|---|
| 31 | int ysize = WIDTH;
|
|---|
| 32 | for(int x = xsize-1; iswhite && x > 0; --x)
|
|---|
| 33 | {
|
|---|
| 34 | for(int y = 0; iswhite && y < ysize; ++y)
|
|---|
| 35 | {
|
|---|
| 36 | if(im.pixel(x, y) != white)
|
|---|
| 37 | iswhite = false;
|
|---|
| 38 | }
|
|---|
| 39 | if(iswhite)
|
|---|
| 40 | xsize = x;
|
|---|
| 41 | }
|
|---|
| 42 | iswhite = true;
|
|---|
| 43 | for(int y = ysize-1; iswhite && y > 0; --y)
|
|---|
| 44 | {
|
|---|
| 45 | for(int x = 0; iswhite && x < xsize; ++x)
|
|---|
| 46 | {
|
|---|
| 47 | if(im.pixel(x, y) != white)
|
|---|
| 48 | iswhite = false;
|
|---|
| 49 | }
|
|---|
| 50 | if(iswhite)
|
|---|
| 51 | ysize = y;
|
|---|
| 52 | }
|
|---|
| 53 | /* didn't find a way to clip the QImage directly, so we reload it */
|
|---|
| 54 | QPixmap p = QPixmap::grabWidget(view, 0,0,xsize,ysize);
|
|---|
| 55 | QFile f;
|
|---|
| 56 | if(f.open(stdout,QIODevice::WriteOnly|QIODevice::Unbuffered))
|
|---|
| 57 | p.save(&f, "PNG");
|
|---|
| 58 | }
|
|---|
| 59 | emit finish();
|
|---|
| 60 | }
|
|---|
| 61 | signals:
|
|---|
| 62 | void finish(void);
|
|---|
| 63 |
|
|---|
| 64 | private:
|
|---|
| 65 | QWebView *view;
|
|---|
| 66 | };
|
|---|
| 67 |
|
|---|
| 68 | #include "webkit-image.h"
|
|---|
| 69 |
|
|---|
| 70 | int main(int argc, char **argv)
|
|---|
| 71 | {
|
|---|
| 72 | if(argc != 2)
|
|---|
| 73 | return 20;
|
|---|
| 74 | QString url = QString(argv[1]);
|
|---|
| 75 |
|
|---|
| 76 | QApplication a( argc, argv );
|
|---|
| 77 | QWebView *view = new QWebView();
|
|---|
| 78 | Save *s = new Save(view);
|
|---|
| 79 |
|
|---|
| 80 | QObject::connect(view, SIGNAL(loadFinished(bool)), s, SLOT(loaded(bool)));
|
|---|
| 81 | QObject::connect(s, SIGNAL(finish(void)), &a, SLOT(quit()));
|
|---|
| 82 | view->resize(WIDTH,WIDTH);
|
|---|
| 83 | view->load(QUrl(url));
|
|---|
| 84 | return a.exec();
|
|---|
| 85 | }
|
|---|