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

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

sometimes white is not white

File size: 2.3 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 /* sometimes the white is shifted one bit */
37 QRgb white2 = QColor(254,254,254).rgb();
38
39 /* didn't find a way to reduce image directly, so we scan for white background */
40 bool iswhite = true;
41 int xsize = WIDTH;
42 int ysize = WIDTH;
43 for(int x = xsize-1; iswhite && x > 0; --x)
44 {
45 for(int y = 0; iswhite && y < ysize; ++y)
46 {
47 QRgb p = im.pixel(x, y);
48 if(p != white && p != white2 & p)
49 iswhite = false;
50 }
51 if(iswhite)
52 xsize = x;
53 }
54 iswhite = true;
55 for(int y = ysize-1; iswhite && y > 0; --y)
56 {
57 for(int x = 0; iswhite && x < xsize; ++x)
58 {
59 QRgb p = im.pixel(x, y);
60 if(p != white && p != white2 && p)
61 iswhite = false;
62 }
63 if(iswhite)
64 ysize = y;
65 }
66 /* didn't find a way to clip the QImage directly, so we reload it */
67 QPixmap p = QPixmap::grabWidget(view, 0,0,xsize,ysize);
68 QFile f;
69 BINARYSTDOUT
70 if(f.open(stdout, QIODevice::WriteOnly|QIODevice::Unbuffered))
71 p.save(&f, "JPG");
72 }
73 emit finish();
74 }
75signals:
76 void finish(void);
77
78private:
79 QWebView *view;
80};
81
82#include "webkit-image.h"
83
84int main(int argc, char **argv)
85{
86 if(argc != 2)
87 return 20;
88 QString url = QString(argv[1]);
89
90 QApplication a( argc, argv );
91 QWebView *view = new QWebView();
92 Save *s = new Save(view);
93
94 QObject::connect(view, SIGNAL(loadFinished(bool)), s, SLOT(loaded(bool)));
95 QObject::connect(s, SIGNAL(finish(void)), &a, SLOT(quit()));
96 view->resize(WIDTH,WIDTH);
97 view->load(QUrl(url));
98 return a.exec();
99}
Note: See TracBrowser for help on using the repository browser.