-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab4_gamma.c
62 lines (51 loc) · 1.58 KB
/
lab4_gamma.c
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
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "png_wrapper.h"
#include "histogram.h"
static void process_image(struct Image img, double c, double gamma)
{
for (size_t y = 0; y < img.height; y++)
for (size_t x = 0; x < img.width; x++)
{
double in = img.pixels[y][x] / 255.0;
int out = c * pow(in, gamma) * 255.0 + 0.5;
if (out > 255) out = 255;
img.pixels[y][x] = out;
}
}
int main(int argc, char * const argv[])
{
if (argc != 7)
error("usage: %s <in_file> <out_file> <hist1_out_file> "
"<hist2_out_file> <c (0.0-50.0)> <gamma (0.0-50.0)>", argv[0]);
char *input_filename = argv[1];
char *output_filename = argv[2];
char *hist1_filename = argv[3];
char *hist2_filename = argv[4];
char *s_c = argv[5];
char *s_gamma = argv[6];
double c = atof(s_c);
if (c < 0.0 || c > 50.0)
error("wrong c (%f)", c);
double gamma = atof(s_gamma);
if (gamma < 0.0 || gamma > 50.0)
error("wrong gamma (%f)", gamma);
struct Image img = read_grayscale_png(input_filename);
printf("Input file \"%s\" opened (width = %u, height = %u)\n",
input_filename, img.width, img.height);
struct Image hist1 = { HIST_WIDTH, HIST_HEIGHT, NULL };
alloc_pixels(&hist1);
histogram_draw(img, hist1);
write_grayscale_png(hist1, hist1_filename);
free_pixels(hist1);
process_image(img, c, gamma);
struct Image hist2 = { HIST_WIDTH, HIST_HEIGHT, NULL };
alloc_pixels(&hist2);
histogram_draw(img, hist2);
write_grayscale_png(hist2, hist2_filename);
free_pixels(hist2);
write_grayscale_png(img, output_filename);
free_pixels(img);
return 0;
}