-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfc_SAVE.c
More file actions
108 lines (94 loc) · 2.42 KB
/
Copy pathfc_SAVE.c
File metadata and controls
108 lines (94 loc) · 2.42 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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Leca Maria Catalina 315CA
#include "functions.h"
void save(img image, char *arg)
{
if (image.type == 0) {
printf("No image loaded\n");
return;
}
char filename[50];
char *p = strtok(arg, " ");
strcpy(filename, p); // extragere nume fisier
p = strtok(NULL, " ");
int txt = 0;
if (p) {
if (strcmp(p, "ascii") == 0) {
txt = 1; // verificare daca e text
} else {
printf("Invalid command\n");
return;
}
p = strtok(NULL, " "); // daca mai exista parametrii
if (p) {
printf("Invalid command\n");
return;
}
}
if (txt)
save_text(filename, image);
else
save_binary(filename, image);
printf("Saved %s\n", filename);
}
void save_text(char *filename, img image)
{
FILE *picture = fopen(filename, "wt");
if (!picture) {
printf("Failed to save\n");
return;
}
if (image.type == 2 || image.type == 5) //daca e gri
fprintf(picture, "P2\n");
else
fprintf(picture, "P3\n");
fprintf(picture, "%d %d\n", image.width, image.height);
fprintf(picture, "%d\n", image.max);
for (int i = 0; i < image.height; i++) {
for (int j = 0; j < image.width; j++) {
if (image.type == 2 || image.type == 5) { // daca e gri
fprintf(picture, "%d ", image.gray[i][j]);
} else {
fprintf(picture, "%d ", image.color[i][j].red);
fprintf(picture, "%d ", image.color[i][j].green);
fprintf(picture, "%d ", image.color[i][j].blue);
}
}
fprintf(picture, "\n");
}
fclose(picture);
}
void save_binary(char *filename, img image)
{
FILE *picture = fopen(filename, "wb");
if (!picture) {
printf("Failed to save\n");
return;
}
if (image.type == 2 || image.type == 5)
fwrite("P5\n", 3, 1, picture);
else
fwrite("P6\n", 3, 1, picture);
// transforma numerele in sir de caractere pentru a le folosi in fwrite()
char ascii[20];
to_string(image.width, ascii);
fwrite(ascii, strlen(ascii), 1, picture);
fwrite(" ", 1, 1, picture);
to_string(image.height, ascii);
fwrite(ascii, strlen(ascii), 1, picture);
fwrite("\n", 1, 1, picture);
to_string(image.max, ascii);
fwrite(ascii, strlen(ascii), 1, picture);
fwrite("\n", 1, 1, picture);
for (int i = 0; i < image.height; i++) {
for (int j = 0; j < image.width; j++) {
if (image.type == 2 || image.type == 5) {
fwrite(&image.gray[i][j], 1, 1, picture);
} else {
fwrite(&image.color[i][j].red, 1, 1, picture);
fwrite(&image.color[i][j].green, 1, 1, picture);
fwrite(&image.color[i][j].blue, 1, 1, picture);
}
}
}
fclose(picture);
}