source: osm/applications/editors/josm/plugins/wmsplugin/webkit-image.cpp@ 11394

Last change on this file since 11394 was 11394, checked in by stoecker, 17 years ago

windows fix

File size: 2.1 KB
Line 
1/* compile with
2moc webkit-image.cpp >webkit-image.h
3g++ 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/* using mingw to set binary mode */
13#ifdef WIN32
14#include <io.h>
15#include <fcntl.h>
16#define BINARYSTDOUT setmode(fileno(stdout), O_BINARY);
17#else
18#define BINARYSTDOUT
19#endif
20
21#define WIDTH 2000
22
23class Save : public QObject
24{
25Q_OBJECT
26public:
27 Save(QWebView *v) : view(v) {};
28
29public slots:
30 void loaded(bool ok)
31 {
32 if(ok)
33 {
34 QImage im = QPixmap::grabWidget(view).toImage();
35 QRgb white = QColor(255,255,255).rgb();
36
37 /* didn't find a way to reduce image directly, so we scan for white background */
38 bool iswhite = true;
39 int xsize = WIDTH;
40 int ysize = WIDTH;
41 for(int x = xsize-1; iswhite && x > 0; --x)
42 {
43 for(int y = 0; iswhite && y < ysize; ++y)
44 {
45 if(im.pixel(x, y) != white)
46 iswhite = false;
47 }
48 if(iswhite)
49 xsize = x;
50 }
51 iswhite = true;
52 for(int y = ysize-1; iswhite && y > 0; --y)
53 {
54 for(int x = 0; iswhite && x < xsize; ++x)
55 {
56 if(im.pixel(x, y) != white)
57 iswhite = false;
58 }
59 if(iswhite)
60 ysize = y;
61 }
62 /* didn't find a way to clip the QImage directly, so we reload it */
63 QPixmap p = QPixmap::grabWidget(view, 0,0,xsize,ysize);
64 QFile f;
65 BINARYSTDOUT
66 if(f.open(stdout, QIODevice::WriteOnly|QIODevice::Unbuffered))
67 p.save(&f, "PNG");
68 }
69 emit finish();
70 }
71signals:
72 void finish(void);
73
74private:
75 QWebView *view;
76};
77
78#include "webkit-image.h"
79
80int main(int argc, char **argv)
81{
82 if(argc != 2)
83 return 20;
84 QString url = QString(argv[1]);
85
86 QApplication a( argc, argv );
87 QWebView *view = new QWebView();
88 Save *s = new Save(view);
89
90 QObject::connect(view, SIGNAL(loadFinished(bool)), s, SLOT(loaded(bool)));
91 QObject::connect(s, SIGNAL(finish(void)), &a, SLOT(quit()));
92 view->resize(WIDTH,WIDTH);
93 view->load(QUrl(url));
94 return a.exec();
95}
Note: See TracBrowser for help on using the repository browser.