-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCameraImageWrapper.cpp
188 lines (146 loc) · 4.62 KB
/
CameraImageWrapper.cpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "CameraImageWrapper.h"
#include <QColor>
#include <QApplication>
#include <QDesktopWidget>
CameraImageWrapper::CameraImageWrapper() : LuminanceSource(0,0)
{
}
CameraImageWrapper::CameraImageWrapper(const QImage &sourceImage) : LuminanceSource(sourceImage.width(), sourceImage.height())
{
image = sourceImage.copy();
}
CameraImageWrapper::CameraImageWrapper(CameraImageWrapper& otherInstance) : LuminanceSource(otherInstance.getWidth(), otherInstance.getHeight())
{
image = otherInstance.getOriginalImage().copy();
}
CameraImageWrapper::~CameraImageWrapper()
{
}
CameraImageWrapper *CameraImageWrapper::Factory(const QImage &sourceImage, int maxWidth, int maxHeight, bool smoothTransformation)
{
QImage image;
if((maxWidth != 1 || maxHeight != 1) && (sourceImage.width() > maxWidth || sourceImage.height() > maxHeight))
image = sourceImage.scaled(
maxWidth != -1 ? maxWidth : sourceImage.width(),
maxHeight != -1 ? maxHeight : sourceImage.height(),
Qt::KeepAspectRatio,
smoothTransformation ? Qt::SmoothTransformation : Qt::FastTransformation);
else
image = sourceImage;
return new CameraImageWrapper(image);
}
int CameraImageWrapper::getWidth() const
{
return image.width();
}
int CameraImageWrapper::getHeight() const
{
return image.height();
}
unsigned char CameraImageWrapper::getPixel(int x, int y) const
{
QRgb pixel = image.pixel(x,y);
return qGray(pixel);//((qRed(pixel) + qGreen(pixel) + qBlue(pixel)) / 3);
}
unsigned char* CameraImageWrapper::copyMatrix() const
{
unsigned char* newMatrix = (unsigned char*)malloc(image.width()*image.height()*sizeof(unsigned char));
int cnt = 0;
for(int i=0; i<image.width(); i++)
{
for(int j=0; j<image.height(); j++)
{
newMatrix[cnt++] = getPixel(i,j);
}
}
return newMatrix;
}
//bool CameraImageWrapper::setImage(QString fileName, int maxWidth, int maxHeight)
//{
// bool isLoaded = image.load(fileName);
// if(!isLoaded)
// return false;
// width = image.width();
// height = image.height();
// scale(maxWidth, maxHeight);
// return true;
//}
//bool CameraImageWrapper::setImage(QImage newImage, int maxWidth, int maxHeight)
//{
// if(newImage.isNull())
// return false;
// image = newImage.copy();
// width = image.width();
// height = image.height();
// scale(maxWidth, maxHeight);
// return true;
//}
QImage CameraImageWrapper::grayScaleImage(QImage::Format f)
{
QImage tmp(image.width(), image.height(), f);
for(int i=0; i<image.width(); i++)
{
for(int j=0; j<image.height(); j++)
{
int pix = (int)getPixel(i,j);
tmp.setPixel(i,j, qRgb(pix ,pix,pix));
}
}
return tmp;
}
QImage CameraImageWrapper::getOriginalImage()
{
return image;
}
ArrayRef<char> CameraImageWrapper::getRow(int y, ArrayRef<char> row) const
{
int width = getWidth();
if (row->size() != width)
row.reset(ArrayRef<char>(width));
for (int x = 0; x < width; x++)
row[x] = getPixel(x,y);
return row;
}
ArrayRef<char> CameraImageWrapper::getMatrix() const
{
int width = getWidth();
int height = getHeight();
char* matrix = new char[width*height];
char* m = matrix;
for(int y=0; y<height; y++)
{
ArrayRef<char> tmpRow;
tmpRow = getRow(y, ArrayRef<char>(width));
#ifdef Q_OS_ANDROID
memcpy(m, &tmpRow->values()[0], width);
#elif __cplusplus > 199711L
memcpy(m, &tmpRow->values()..data(), width);
#else
memcpy(m, &tmpRow->values()[0], width);
#endif
m += width * sizeof(unsigned char);
//delete tmpRow;
}
//pMatrix = matrix;
ArrayRef<char> arr = ArrayRef<char>(matrix, width*height);
if(matrix)
delete matrix;
return arr;
}
//void CameraImageWrapper::scale(int maxWidth, int maxHeight)
//{
// image = scale_s(image, maxWidth, maxHeight, isSmoothTransformationEnabled);
//}
//QImage CameraImageWrapper::scale_s(const QImage &image, int maxWidth, int maxHeight, bool smoothTransformation)
//{
// QImage transformedImage;
// if((maxWidth != 1 || maxHeight != 1) && (image.width() > maxWidth || image.height() > maxHeight))
// transformedImage = image.scaled(
// maxWidth != -1 ? maxWidth : image.width(),
// maxHeight != -1 ? maxHeight : image.height(),
// Qt::KeepAspectRatio,
// smoothTransformation ? Qt::SmoothTransformation : Qt::FastTransformation);
// else
// transformedImage = image;
// return transformedImage;
//}