-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.c
48 lines (42 loc) · 1.14 KB
/
convert.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
#include <stdio.h>
#include <stdlib.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image/stb_image_write.h"
int main(int argc, char* argv[])
{
int width, height, channels;
unsigned char *img = stbi_load(argv[1],&width, &height, &channels, 0);
if (img == NULL)
{
printf("Error in loading the image");
exit(1);
}
printf("Loaded image with a width of %dpx, a height of %dpx and %d channels\n", width, height, channels);
if (channels <= 2)
{
printf("Already a greyscale image\n");
exit(1);
}
size_t img_size = width*height*channels;
int new_channels = channels==4?2:1;
size_t out_size = width*height*new_channels;
unsigned char* d_img = malloc(img_size);
if (d_img == NULL)
{
printf("Unable to allocate memory for image\n");
}
for (unsigned char *p = img, *pd = d_img; p!=img+img_size; p += channels, pd += new_channels)
{
*pd = (unsigned char)((*p + *(p+1) + *(p+2))/3);
if(channels == 4)
{
*(pd + 1) = *(p + 3);
}
}
stbi_write_png(argv[2], width, height, new_channels, d_img, width*new_channels);
stbi_image_free(img);
free(d_img);
return 0;
}