-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_utils.cpp
More file actions
97 lines (80 loc) · 2.14 KB
/
Copy pathimage_utils.cpp
File metadata and controls
97 lines (80 loc) · 2.14 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
#ifdef __APPLE__
#include <OpenCL/opencl.h>
#include <stdlib.h>
#else
#include "CL/cl.h"
#endif
#include "image_utils.h"
#include <iostream>
#include <cmath>
using namespace std;
tga::TGAImage loadImage(const char *path)
{
tga::TGAImage image;
tga::LoadTGA(&image, path);
cout << "load image " << path << ". width=" << image.width << "; height=" << image.height << endl;
cout << "image data: " << image.imageData.size() << endl;
return image;
}
double** setupGaussFilterKernel(int radius)
{
double sigma = max(radius / 2, 1);
int height = 2 * radius + 1;
int width = 2 * radius + 1;
double sum = 0;
int x,y;
double** _kernel = new double* [width];
for(int i = 0; i < width; i++) {
_kernel[i] = new double [height];
}
for(y = -radius; y <= radius; y++) {
for(x = -radius; x <= radius; x++) {
_kernel[x + radius][y + radius] = exp(-(x * x + y * y) / (2 * sigma * sigma)) / (2 * CL_M_PI * sigma * sigma);
sum += _kernel[x + radius][y + radius];
}
}
for (y = 0 ; y < height ; y++) {
for (x = 0 ; x < width ; x++) {
_kernel[y][x] /= sum;
}
}
return _kernel;
}
void convertPixelsToImage(PixelValue **pixels, tga::TGAImage &image)
{
vector<unsigned char> outData;
for(unsigned int y = 0; y < image.height; y++) {
for(unsigned int x = 0; x < image.width; x++) {
PixelValue pixel = pixels[y][x];
outData.push_back(pixel.r * 255);
outData.push_back(pixel.g * 255);
outData.push_back(pixel.b * 255);
}
}
image.imageData = outData;
}
PixelValue **convertImageToPixels(tga::TGAImage image)
{
int w = image.width;
int h = image.height;
PixelValue **pixels;
pixels = new PixelValue* [h];
for(int i = 0; i < h; i++) {
pixels[i] = new PixelValue [w];
}
int pos = 0;
for(int y = 0; y < h; y++) {
for(int x = 0; x < w; x++) {
PixelValue pixelValue;
pixelValue.r = image.imageData[pos] / 255.f;
pixelValue.g = image.imageData[pos + 1] / 255.f;
pixelValue.b = image.imageData[pos + 2] / 255.f;
pixels[y][x] = pixelValue;
pos += 3;
}
}
return pixels;
}
void printPixel(PixelValue value) {
cout << "(" << value.r << "," << value.g << "," << value.b << ")" << endl;
}