1 #include <QAction>
2 #include <QObject>
3 #include <QImage>
4 //#include <QTime>
5 //#include <iostream>
6 #include "util.h"
7 #include "config.h"
8 
9 
10 using namespace std;
11 
12 
rotate_rect(const QRectF & rect,float w,float h,int rotation)13 const QRectF rotate_rect(const QRectF &rect, float w, float h, int rotation) {
14 	if (rotation == 0) {
15 		return rect;
16 	} else if (rotation == 1) {
17 		return QRectF(w - rect.bottom(), rect.left(), rect.height(), rect.width());
18 	} else if (rotation == 2) {
19 		return QRectF(w - rect.right(), h - rect.bottom(), rect.width(), rect.height());
20 	} else {
21 		return QRectF(rect.top(), h - rect.right(), rect.height(), rect.width());
22 	}
23 }
24 
rotate_point(const QPointF & point,float w,float h,int rotation)25 const QPointF rotate_point(const QPointF &point, float w, float h, int rotation) {
26 	if (rotation == 0) {
27 		return point;
28 	} else if (rotation == 1) {
29 		return QPointF(h - point.y(), point.x());
30 	} else if (rotation == 2) {
31 		return QPointF(w - point.x(), h - point.y());
32 	} else {
33 		return QPointF(point.y(), w - point.x());
34 	}
35 }
36 
transform_rect(const QRectF & rect,float scale,int off_x,int off_y)37 QRect transform_rect(const QRectF &rect, float scale, int off_x, int off_y) {
38 	return QRect(rect.x() * scale + off_x,
39 			rect.y() * scale + off_y,
40 			rect.width() * scale,
41 			rect.height() * scale);
42 }
43 
transform_rect_expand(const QRectF & rect,float scale,int off_x,int off_y)44 QRect transform_rect_expand(const QRectF &rect, float scale, int off_x, int off_y) {
45 	static int rect_margin = CFG::get_instance()->get_value("Settings/rect_margin").toInt();
46 	return QRect(rect.x() * scale + off_x - rect_margin,
47 			rect.y() * scale + off_y - rect_margin,
48 			rect.width() * scale + 2 * rect_margin,
49 			rect.height() * scale + 2 * rect_margin);
50 }
51 
add_action(QWidget * base,const char * action,const char * slot,QWidget * receiver)52 void add_action(QWidget *base, const char *action, const char *slot, QWidget *receiver) {
53 	QStringListIterator i(CFG::get_instance()->get_keys(action));
54 	while (i.hasNext()) {
55 		QAction *a = new QAction(base);
56 		a->setShortcut(QKeySequence(i.next()));
57 		base->addAction(a);
58 		QObject::connect(a, SIGNAL(triggered()), receiver, slot);
59 	}
60 }
61 
invert_image(QImage * img)62 void invert_image(QImage *img) {
63 	static QRgb invert_mask = qRgba(255, 255, 255, 0);
64 	static float inverted_contrast =
65 			CFG::get_instance()->get_value("Settings/inverted_color_contrast").toFloat();
66 	static int offset = 255 *
67 			CFG::get_instance()->get_value("Settings/inverted_color_brightening").toFloat();
68 //	QTime time;
69 //	time.start();
70 
71 //	img->invertPixels();
72 
73 	QRgb *pixels = reinterpret_cast<QRgb *>(img->bits());
74 	QRgb *pixels_end = pixels + img->width() * img->height();
75 
76 	while (pixels < pixels_end) {
77 		*pixels ^= invert_mask;
78 		*pixels = qRgb(
79 				(qRed(*pixels)) * inverted_contrast + offset,
80 				(qGreen(*pixels)) * inverted_contrast + offset,
81 				(qBlue(*pixels)) * inverted_contrast + offset);
82 		++pixels;
83 	}
84 //	cout << time.elapsed() << "ms elapsed" << endl;
85 }
86 
87