9 ImgControl::ImgControl()
14 ImgControl::~ImgControl()
19 unsigned long long ImgControl::GetBitmapSize(
int width,
int height,
int channel)
21 return (((width * channel) + 3) & ~3) * height;
26 if (src ==
nullptr)
return;
28 unsigned long long newsize;
29 uint nBytePerLine = ((
w * ch) + 3) & ~3;
30 uint nNewBytePerLine = ((neww * ch) + 3) & ~3;
34 newsize = GetBitmapSize(neww, newh, ch);
35 dst =
new unsigned char[newsize];
38 newsize = nNewBytePerLine * newh;
41 #pragma omp parallel for firstprivate(nNewBytePerLine, nBytePerLine) 43 for (
int y = 0; y < newh; y++)
45 uint nbppY = y * nNewBytePerLine;
46 for (
int x = 0; x < neww; x++)
48 float gx = (x / (float)neww) * (
w - 1);
49 float gy = (y / (float)newh) * (
h - 1);
55 uint32_t a00, a01, a10, a11;
57 a00 = src[gxi + 0 + gyi * nBytePerLine];
58 a01 = src[gxi + 1 + gyi * nBytePerLine];
59 a10 = src[gxi + 0 + (gyi + 1) * nBytePerLine];
60 a11 = src[gxi + 1 + (gyi + 1) * nBytePerLine];
65 float w1 = (1 - dx) * (1 - dy);
66 float w2 = dx * (1 - dy);
67 float w3 = (1 - dx) * dy;
69 if ( x + y * neww < newsize)
70 dst[x + y * neww] = int(a00 * w1 + a01 * w2 + a10 * w3 + a11 * w4);
73 uint32_t b00[3], b01[3], b10[3], b11[3];
77 b00[0] = src[srcX + 0 + gyi * nBytePerLine];
78 b00[1] = src[srcX + 1 + gyi * nBytePerLine];
79 b00[2] = src[srcX + 2 + gyi * nBytePerLine];
81 b01[0] = src[srcX + 3 + gyi * nBytePerLine];
82 b01[1] = src[srcX + 4 + gyi * nBytePerLine];
83 b01[2] = src[srcX + 5 + gyi * nBytePerLine];
85 b10[0] = src[srcX + 0 + (gyi + 1) * nBytePerLine];
86 b10[1] = src[srcX + 1 + (gyi + 1) * nBytePerLine];
87 b10[2] = src[srcX + 2 + (gyi + 1) * nBytePerLine];
89 b11[0] = src[srcX + 3 + (gyi + 1) * nBytePerLine];
90 b11[1] = src[srcX + 4 + (gyi + 1) * nBytePerLine];
91 b11[2] = src[srcX + 5 + (gyi + 1) * nBytePerLine];
96 float w1 = (1 - dx) * (1 - dy);
97 float w2 = dx * (1 - dy);
98 float w3 = (1 - dx) * dy;
100 if ((dstX + 2 + nbppY) < newsize)
102 dst[dstX + 0 + nbppY] = int(b00[0] * w1 + b01[0] * w2 + b10[0] * w3 + b11[0] * w4);
103 dst[dstX + 1 + nbppY] = int(b00[1] * w1 + b01[1] * w2 + b10[1] * w3 + b11[1] * w4);
104 dst[dstX + 2 + nbppY] = int(b00[2] * w1 + b01[2] * w2 + b10[2] * w3 + b11[2] * w4);
111 bool ImgControl::Rotate(
double rotate,
unsigned char *src,
unsigned char *dst,
int w,
int h,
int neww,
int newh,
int ch)
113 if (src ==
nullptr)
return false;
114 if (ch > 4)
return false;
116 bool bChangeSize =
false;
117 if (neww !=
w || newh !=
h) {
121 unsigned long long nImgSize = bChangeSize ? GetBitmapSize(neww, newh, ch) : GetBitmapSize(
w,
h, ch);
125 dst =
new unsigned char[nImgSize];
128 unsigned char *temp =
new unsigned char[nImgSize];
132 Resize(src, temp,
w,
h, neww, newh, ch);
137 memcpy(temp, src, nImgSize);
140 int nBytePerLine = ((
w * ch) + 3) & ~3;
141 double radian =
RADIAN(rotate);
142 double cc = cos(radian);
143 double ss = sin(-radian);
144 double centerX = (double)
w / 2.0;
145 double centerY = (double)
h / 2.0;
147 #pragma omp parallel for firstprivate(nBytePerLine, ss, cc, centerX, centerY) 149 for (
int y = 0; y <
h; y++) {
150 int dstY = y * nBytePerLine;
151 for (
int x = 0; x <
w; x++) {
152 int origX = (int)(centerX + ((
double)y - centerY)*ss + ((
double)x - centerX)*cc);
153 int origY = (int)(centerY + ((
double)y - centerY)*cc - ((
double)x - centerX)*ss);
155 unsigned char pixels[4] = { 0, };
156 if ((origY >= 0 && origY <
h) && (origX >= 0 && origX <
w)) {
157 int offsetX = origX * ch;
158 int offsetY = origY * nBytePerLine;
160 memcpy(pixels, &temp[offsetY + offsetX],
sizeof(
unsigned char) * ch);
163 memcpy(&dst[dstY + (x * ch)], pixels,
sizeof(
unsigned char) * ch);
175 if (src ==
nullptr)
return false;
181 dst =
new unsigned char[GetBitmapSize(
w,
h, ch)];
184 int nBytePerLine = ((
w * ch) + 3) & ~3;
187 #pragma omp parallel for firstprivate(nBytePerLine) 189 for (
int y = 0; y <
h; y++) {
190 int offset = y * nBytePerLine;
191 int offset2 = (
h - y - 1) * nBytePerLine;
192 for (
int x = 0; x <
w; x++) {
193 memcpy(&dst[offset + (x * ch)], &src[offset2 + (x * ch)],
sizeof(
unsigned char) * ch);
199 #pragma omp parallel for firstprivate(nBytePerLine) 201 for (
int y = 0; y <
h; y++) {
202 int offset = y * nBytePerLine;
203 for (
int x = 0; x <
w; x++) {
204 memcpy(&dst[offset + (x * ch)], &src[offset + ((
w * ch) - ((x + 1) * ch))],
sizeof(
unsigned char) * ch);
208 else if (mode == FLIP::BOTH) {
210 #pragma omp parallel for firstprivate(nBytePerLine) 212 for (
int y = 0; y <
h; y++) {
213 int offset = y * nBytePerLine;
214 int offset2 = (
h - y - 1) * nBytePerLine;
215 for (
int x = 0; x <
w; x++) {
216 memcpy(&dst[offset + (x * ch)], &src[offset2 + ((
w * ch) - ((x + 1) * ch))],
sizeof(
unsigned char) * ch);
228 bool ImgControl::Crop(
unsigned char *src,
unsigned char *dst,
int w,
int h,
int ch,
int x,
int y,
int neww,
int newh)
230 if (src ==
nullptr)
return false;
231 if (x < 0 || y < 0 || x + neww >
w || y + newh >
h)
return false;
235 unsigned long long nImgSize = GetBitmapSize(neww, newh, ch);
236 dst =
new unsigned char[nImgSize];
237 memset(dst, 0, nImgSize);
241 int nBytePerLine = ((neww * ch) + 3) & ~3;
242 int nBytePerLine2 = ((
w * ch) + 3) & ~3;
243 int offsetX = x * ch;
246 #pragma omp parallel for firstprivate(nBytePerLine, nBytePerLine2, y, ch, neww) 248 for (
int i = 0; i < newh; i++) {
249 int offset = i * nBytePerLine;
250 int offsetY = (y + i) * nBytePerLine2;
251 memcpy(&dst[offset], &src[offsetY + offsetX],
sizeof(
unsigned char) * ch * neww);
260 FILE *fp = fopen(path,
"rb");
266 fseek(fp, 0, SEEK_END);
bool Rotate(double rotate, unsigned char *src, unsigned char *dst, int w, int h, int neww, int newh, int ch)
bool Crop(unsigned char *src, unsigned char *dst, int w, int h, int ch, int x, int y, int neww, int newh)
bool GetSize(const char *path, unsigned int *size)
bool Flip(FLIP mode, unsigned char *src, unsigned char *dst, int w, int h, int ch)
void Resize(unsigned char *src, unsigned char *dst, int w, int h, int neww, int newh, int ch)