-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·254 lines (192 loc) · 6 KB
/
Copy pathmain.cpp
File metadata and controls
executable file
·254 lines (192 loc) · 6 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// CUDA Runtime
#include <cuda_runtime.h>
// Utility and system includes
#include <helper_cuda.h>
// helper for shared that are common to CUDA Samples
#include <helper_functions.h>
#include <helper_timer.h>
#include "hist-equ.h"
void run_cpu_color_test(PPM_IMG img_in);
void run_gpu_color_test(PPM_IMG img_in);
void run_cpu_gray_test(PGM_IMG img_in);
void run_gpu_gray_test(PGM_IMG img_in);
int main(){
int * init;
cudaMalloc(&init,0);
PGM_IMG img_ibuf_g;
PPM_IMG img_ibuf_c;
printf("Running contrast enhancement for gray-scale images.\n");
img_ibuf_g = read_pgm("in.pgm");
run_cpu_gray_test(img_ibuf_g);
run_gpu_gray_test(img_ibuf_g);
free_pgm(img_ibuf_g);
printf("\n\n");
printf("Running contrast enhancement for color images.\n");
img_ibuf_c = read_ppm("in.ppm");
run_cpu_color_test(img_ibuf_c);
run_gpu_color_test(img_ibuf_c);
free_ppm(img_ibuf_c);
return 0;
}
void run_gpu_color_test(PPM_IMG img_in)
{
StopWatchInterface *timer=NULL;
PPM_IMG img_obuf_hsl, img_obuf_yuv;
printf("Starting GPU processing...\n");
sdkCreateTimer(&timer);
sdkStartTimer(&timer);
img_obuf_hsl = contrast_enhancement_c_hsl_gpu(img_in);
sdkStopTimer(&timer);
printf("HSL processing time: %f (ms)\n", sdkGetTimerValue(&timer));
sdkDeleteTimer(&timer);
write_ppm(img_obuf_hsl, "out_hsl_gpu.ppm");
sdkCreateTimer(&timer);
sdkStartTimer(&timer);
img_obuf_yuv = contrast_enhancement_c_yuv_gpu(img_in);
sdkStopTimer(&timer);
printf("YUV processing time: %f (ms)\n", sdkGetTimerValue(&timer));
sdkDeleteTimer(&timer);
write_ppm(img_obuf_yuv, "out_yuv_gpu.ppm");
free_ppm(img_obuf_hsl);
free_ppm(img_obuf_yuv);
}
void run_gpu_gray_test(PGM_IMG img_in)
{
StopWatchInterface *timer = NULL;
PGM_IMG img_obuf;
printf("Starting GPU processing...\n");
sdkCreateTimer(&timer);
sdkStartTimer(&timer);
img_obuf = contrast_enhancement_g_gpu(img_in);
sdkStopTimer(&timer);
printf("Processing time: %f (ms)\n", sdkGetTimerValue(&timer));
sdkDeleteTimer(&timer);
write_pgm(img_obuf, "out_gpu.pgm");
free_pgm(img_obuf);
}
void run_cpu_color_test(PPM_IMG img_in)
{
StopWatchInterface *timer=NULL;
PPM_IMG img_obuf_hsl, img_obuf_yuv;
printf("Starting CPU processing...\n");
sdkCreateTimer(&timer);
sdkStartTimer(&timer);
img_obuf_hsl = contrast_enhancement_c_hsl(img_in);
sdkStopTimer(&timer);
printf("HSL processing time: %f (ms)\n", sdkGetTimerValue(&timer));
sdkDeleteTimer(&timer);
write_ppm(img_obuf_hsl, "out_hsl_cpu.ppm");
sdkCreateTimer(&timer);
sdkStartTimer(&timer);
img_obuf_yuv = contrast_enhancement_c_yuv(img_in);
sdkStopTimer(&timer);
printf("YUV processing time: %f (ms)\n", sdkGetTimerValue(&timer));
sdkDeleteTimer(&timer);
write_ppm(img_obuf_yuv, "out_yuv_cpu.ppm");
free_ppm(img_obuf_hsl);
free_ppm(img_obuf_yuv);
}
void run_cpu_gray_test(PGM_IMG img_in)
{
StopWatchInterface *timer = NULL;
PGM_IMG img_obuf;
printf("Starting CPU processing...\n");
sdkCreateTimer(&timer);
sdkStartTimer(&timer);
img_obuf = contrast_enhancement_g(img_in);
sdkStopTimer(&timer);
printf("Processing time: %f (ms)\n", sdkGetTimerValue(&timer));
sdkDeleteTimer(&timer);
write_pgm(img_obuf, "out_cpu.pgm");
free_pgm(img_obuf);
}
PPM_IMG read_ppm(const char * path){
FILE * in_file;
char sbuf[256];
char *ibuf;
PPM_IMG result;
int v_max, i;
in_file = fopen(path, "r");
if (in_file == NULL){
printf("Input file not found!\n");
exit(1);
}
/*Skip the magic number*/
fscanf(in_file, "%s", sbuf);
//result = malloc(sizeof(PPM_IMG));
fscanf(in_file, "%d",&result.w);
fscanf(in_file, "%d",&result.h);
fscanf(in_file, "%d\n",&v_max);
printf("Image size: %d x %d\n", result.w, result.h);
result.img_r = (unsigned char *)malloc(result.w * result.h * sizeof(unsigned char));
result.img_g = (unsigned char *)malloc(result.w * result.h * sizeof(unsigned char));
result.img_b = (unsigned char *)malloc(result.w * result.h * sizeof(unsigned char));
ibuf = (char *)malloc(3 * result.w * result.h * sizeof(char));
fread(ibuf,sizeof(unsigned char), 3 * result.w*result.h, in_file);
for(i = 0; i < result.w*result.h; i ++){
result.img_r[i] = ibuf[3*i + 0];
result.img_g[i] = ibuf[3*i + 1];
result.img_b[i] = ibuf[3*i + 2];
}
fclose(in_file);
free(ibuf);
return result;
}
void write_ppm(PPM_IMG img, const char * path){
FILE * out_file;
int i;
char * obuf = (char *)malloc(3 * img.w * img.h * sizeof(char));
for(i = 0; i < img.w*img.h; i ++){
obuf[3*i + 0] = img.img_r[i];
obuf[3*i + 1] = img.img_g[i];
obuf[3*i + 2] = img.img_b[i];
}
out_file = fopen(path, "wb");
fprintf(out_file, "P6\n");
fprintf(out_file, "%d %d\n255\n",img.w, img.h);
fwrite(obuf,sizeof(unsigned char), 3*img.w*img.h, out_file);
fclose(out_file);
free(obuf);
}
void free_ppm(PPM_IMG img)
{
free(img.img_r);
free(img.img_g);
free(img.img_b);
}
PGM_IMG read_pgm(const char * path){
FILE * in_file;
char sbuf[256];
PGM_IMG result;
int v_max;//, i;
printf("Path=%s\n", path);
in_file = fopen(path, "r");
if (in_file == NULL){
printf("Input file not found!\n");
exit(1);
}
fscanf(in_file, "%s", sbuf); /*Skip the magic number*/
fscanf(in_file, "%d",&result.w);
fscanf(in_file, "%d",&result.h);
fscanf(in_file, "%d\n",&v_max);
printf("Image size: %d x %d\n", result.w, result.h);
result.img = (unsigned char *)malloc(result.w * result.h * sizeof(unsigned char));
fread(result.img,sizeof(unsigned char), result.w*result.h, in_file);
fclose(in_file);
return result;
}
void write_pgm(PGM_IMG img, const char * path){
FILE * out_file;
out_file = fopen(path, "wb");
fprintf(out_file, "P5\n");
fprintf(out_file, "%d %d\n255\n",img.w, img.h);
fwrite(img.img,sizeof(unsigned char), img.w*img.h, out_file);
fclose(out_file);
}
void free_pgm(PGM_IMG img)
{
free(img.img);
}