-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvertor.cpp
More file actions
129 lines (115 loc) · 3.68 KB
/
convertor.cpp
File metadata and controls
129 lines (115 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "convertor.h"
#include "sequence.h"
#include <QString>
#include <QPixmap>
#include <QFileDialog>
#include <QImage>
#include <QRgb>
#include <map>
#include <vector>
#include <utility>
const int reds=6;
const int greens=8;
const int blues=5;
const float greys[16] = {
0.05859375, 0.1171875, 0.17578125, 0.234375, 0.29296875, 0.3515625, 0.41015625, 0.46875,
0.52734375, 0.5859375, 0.64453125, 0.703125, 0.76171875, 0.8203125, 0.87890625, 0.9375
};
QRgb colorToRgb(Color color){
int r = color.r;
int g = color.g;
int b = color.b;
QRgb rgb = qRgb(r, g, b);
return rgb;
}
bool compare_color(Color c1, Color c2){
return c1.r == c2.r && c1.g == c2.g && c1.b == c2.b;
}
void Convertor::convert(QPixmap pixmap) {
QImage img = pixmap.toImage();
int imageWidth = img.width();
int imageHeight = img.height();
Color basicColors[160][100]; // massive with basic colors of image.
for(int i=0; i < imageWidth; i++){
for(int j=0; j < imageHeight; j++){
QRgb rgb = img.pixel(i,j);
basicColors[i][j].r = qRed(rgb);
basicColors[i][j].g = qGreen(rgb);
basicColors[i][j].b = qBlue(rgb);
}
}
Pair_Color max_pair;
std::vector<Pair_Color> pair_statistics;
for(int i = 0; i < imageWidth; i++){
for(int j = 0; j < imageHeight / 2;j += 2){
Color bcolor = basicColors[i][j];
Color fcolor = basicColors[i][j + 1];
bool push = true;
for(int q = 0; q < pair_statistics.size(); q++){
Pair_Color pair = pair_statistics[q];
if(compare_color(pair.bcolor, bcolor) && compare_color(pair.fcolor, fcolor)){
pair.count++;
push = false;
if(pair.count > max_pair.count){
max_pair = pair;
}
}
}
if(push){
Pair_Color pair = {bcolor, fcolor, 1}; //Basic back and foreground.
pair_statistics.push_back(pair);
}
}
}
std::list<Sequence> list;
for(int y = 0; y < imageHeight/2; y++){
for(int x = 0; x < imageWidth; x++){
QRgb upper = inflate(deflate(colorToRgb(basicColors[x][y*2])));
QRgb lower = inflate(deflate(colorToRgb(basicColors[x][y*2+1])));
}
}
}
void encodeColor(QRgb rgb, Color &colRGB) {
int r = qRed(rgb) * 255;
int g = qGreen(rgb) * 255;
int b = qBlue(rgb) * 255;
colRGB.r = r;
colRGB.g = g;
colRGB.b = b;
}
void encodeLen(int len, Len &llen) {
llen.x = len / 256;
llen.y = len % 256;
}
double delta(QRgb a, QRgb b) {
int dr = qRed(a) - qRed(b);
int dg = qGreen(a) - qGreen(b);
int db = qBlue(a) - qBlue(b);
return 0.2126 * dr * dr + 0.7152 * dg * dg + 0.0722 * db * db;
}
int deflate(QRgb rgb) {
int idxR = qRed(rgb) * 255 * (reds - 1) / 0xFF + 0.5;
int idxG = qGreen(rgb) * 255 * (greens - 1.0) / 0xFF + 0.5;
int idxB = qBlue(rgb) * 255 * (blues - 1.0) / 0xFF + 0.5;
int compressed = 16 + idxR * greens * blues + idxG * blues + idxB;
for(int i=0; i < 16; i++){
if(delta(inflate(i), rgb) < delta(inflate(compressed), rgb))
compressed = i;
}
return compressed;
}
QRgb inflate(int value) {
if(value < 16){
return greys[value];
} else {
int index = value - 16;
float idxB = index % blues;
float idxG = (index / blues) % greens;
float idxR = (index / blues / greens) % reds;
int r = (idxR * 0xFF / (reds - 1) + 0.5);
int g = (idxG * 0xFF / (greens - 1) + 0.5);
int b = (idxB * 0xFF / (blues - 1) + 0.5);
QRgb rgb = qRgb(r, g, b);
return rgb;
}
}