forked from nowke/hpc_lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnegative.c
More file actions
60 lines (47 loc) · 1.5 KB
/
Copy pathnegative.c
File metadata and controls
60 lines (47 loc) · 1.5 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
#include <stdio.h>
#include <gd.h>
#include <string.h>
#include <omp.h>
int main(int argc, char *argv[]) {
if (argc < 4) {
printf("Usage: ./negative input.png output.png num_threads\n");
return 1;
}
char *input_file = argv[1];
char *output_file = argv[2];
int num_threads = atoi(argv[3]);
int color, x, y, i;
int red, green, blue;
FILE *fp;
if((fp = fopen(input_file, "r")) == NULL) {
printf("Error opening file %s\n", input_file);
return 1;
}
gdImagePtr img = gdImageCreateFromPng(fp);
int width = gdImageSX(img);
int height = gdImageSY(img);
double t1 = omp_get_wtime();
#pragma omp parallel for private(y, color, red, green, blue) num_threads(num_threads)
for(x=0; x<width; x++) {
for(y=0; y<height; y++) {
color = x + 0;
color = gdImageGetPixel(img, x, y);
red = 255 - gdImageRed(img, color);
green = 255 - gdImageGreen(img, color);
blue = 255 - gdImageBlue(img, color);
color = gdImageColorAllocate(img, red, green, blue);
gdImageSetPixel(img, x, y, color);
}
}
double t2 = omp_get_wtime();
if((fp=fopen(output_file, "w")) == NULL) {
printf("Error opening output file %s\n", output_file);
return 1;
}
gdImagePng(img, fp);
gdImageDestroy(img);
fclose(fp);
printf("File Size: %dx%d\n", width, height);
printf("Time Taken: %.3lfms\n",(t2 - t1) * 1000);
return 0;
}