-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.c
63 lines (51 loc) · 1.57 KB
/
io.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
63
#include "io.h"
#include <stdio.h>
#include <stdlib.h>
#include "lodepng.h"
void processImage(const char *filename, rgb_image *img)
{
decodeTwoSteps(filename, img);
transformToGray(img);
encodeOneStep("wynik.png", img);
}
void encodeOneStep(const char* filename, rgb_image *img)
{
/*Encode the image*/
unsigned error = lodepng_encode32_file(filename, img->image, img->width, img->height);
/*if there's an error, display it*/
if(error) printf("error %u: %s\n", error, lodepng_error_text(error));
}
void decodeTwoSteps(const char* filename, rgb_image *img)
{
unsigned error;
unsigned char* png;
size_t pngsize;;
unsigned x, y;
float gray;
lodepng_load_file(&png, &pngsize, filename);
error = lodepng_decode32(&img->image, &img->width, &img->height, png, pngsize);
if(error) printf("error %u: %s\n", error, lodepng_error_text(error));
free(png);
}
void transformToGray(rgb_image *img)
{
unsigned char* image = img->image;
unsigned width = img->width;
unsigned height = img->height;
unsigned x, y;
float gray;
float r, g, b, a;
a = 255;
for(y = 0; y < height; y++)
for(x=0; x< width; x++)
{
r = image[4 * width * y + 4 * x + 0];
g = image[4 * width * y + 4 * x + 1];
b = image[4 * width * y + 4 * x + 2];
gray = .299f*r + .587f*g + .114f*b;
image[4 * width * y + 4 * x + 0] = gray;
image[4 * width * y + 4 * x + 1] = gray;
image[4 * width * y + 4 * x + 2] = gray;
image[4 * width * y + 4 * x + 3] = a;
}
}