-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransparency_Setter.c
87 lines (72 loc) · 1.96 KB
/
Transparency_Setter.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <time.h> //For rand
#include <math.h> //pow, sqrt
#include "png_assist.h"
void invalid_input(){
printf("\nWrong number of arguments provided. This is the format\n");
printf("./Transparency_Remover --input-image [png_filename_1] --output-image [png_filename_2] --mode [0 or 1]\n");
printf("Mode 0 will set all partially transparent pixels to fully transparent\n");
printf("and mode 1 will make all fully transparent pixels the same\n");
exit(1);
}
//Just to make it more obvious to people viewing the code what is happening
bool string_equals(char * a, char * b){
return !strcmp(a, b);
}
int main(int argC, char ** argV){
int mode = -1;
bool flag_input_image = false;
bool flag_output_image = false;
uint8_t input_image_index = 0;
uint8_t output_image_index = 0;
for(int i = 1; i < argC; i++){
if(string_equals(argV[i], "--mode")){
if(++i >= argC || mode != -1){
invalid_input();
}
mode = atoi(argV[i]);
}
else if(string_equals(argV[i], "--input-image")){
if(++i >= argC){
invalid_input();
}
flag_input_image = true;
input_image_index = i;
}
else if(string_equals(argV[i], "--output-image")){
if(++i >= argC){
invalid_input();
}
flag_output_image = true;
output_image_index = i;
}
}
//A file path is not set, can't continue
if(mode != -1 || !flag_input_image || !flag_output_image){
invalid_input();
}
png_details_t p_det;
read_png_file(argV[input_image_index], &p_det);
for(int y = 0; y < p_det.height; y++){
for(int x = 0; x < p_det.width; x++){
png_bytep px = &(p_det.row_pointers[y][x * 4]);
if(mode == 0){
if(px[3] != 255){
px[3] = 0;
}
}
else if(mode == 1){
if(px[3] == 0){
px[0] = 0;
px[1] = 0;
px[2] = 0;
}
}
}
}
write_png_file(argV[output_image_index], &p_det);
return 0;
}