-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaveimage.h
More file actions
111 lines (105 loc) · 2.93 KB
/
Copy pathsaveimage.h
File metadata and controls
111 lines (105 loc) · 2.93 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
109
110
111
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "structs.h"
#include "printimage.h"
#ifndef SAVEIMAGE_H
#define SAVEIMAGE_H
void printascii(image *img, FILE *file)
{
if (img->magic[1] == '2') { //imagine pgm in format text
for (int i = 0; i < img->height; i++) {
for (int j = 0; j < img->width; j++)
fprintf(file, "%d ", img->data[i][j]); // afisez pixelii
fprintf(file, "\n");
}
} else { // imagine ppm in format ext
for (int i = 0; i < img->height; i++) {
for (int j = 0; j < img->width; j++) { //afisez pixelii rgb
fprintf(file, "%d ", img->rgb[i][j].red);
fprintf(file, "%d ", img->rgb[i][j].green);
fprintf(file, "%d ", img->rgb[i][j].blue);
}
fprintf(file, "\n");
}
}
}
void printtbinary(image *img, FILE *file)
{
// afisare imagine pgm in format binar
if (img->magic[1] == '5') {
unsigned char c;
for (int i = 0; i < img->height; i++) {
for (int j = 0; j < img->width; j++) {
c = (unsigned char)img->data[i][j];
if (fwrite(&c, sizeof(unsigned char), 1, file) != 1) {
fprintf(stderr, "Error writing to file");
fclose(file);
return;
}
}
}
}
// afisare imagine ppm in format binar
if (img->magic[1] == '6') {
unsigned char c, d, e;
for (int i = 0; i < img->height; i++) {
for (int j = 0; j < img->width; j++) {
c = (unsigned char)img->rgb[i][j].red;
d = (unsigned char)img->rgb[i][j].green;
e = (unsigned char)img->rgb[i][j].blue;
if (fwrite(&c, sizeof(unsigned char), 1, file) != 1 ||
fwrite(&d, sizeof(unsigned char), 1, file) != 1 ||
fwrite(&e, sizeof(unsigned char), 1, file) != 1) {
fprintf(stderr, "Error writing to file");
fclose(file);
return;
}
}
}
}
}
void saveimage(image *img, char *filename)
{
char *format;
format = (char *)malloc(50 * sizeof(char));
if (!format) {
printf("NU am putut creea format\n");
return;
}
// citesc formatul de salvare
fgets(format, sizeof(format), stdin);
format[strcspn(format, "\n")] = '\0';
//format ascii
if (format[0] != '\0' && strcmp(format, " ascii") == 0) {
FILE *file;
file = fopen(filename, "w"); //deschid fisierul in mod write normal
if (!file || (!img->rgb && !img->data)) { //verific daca exista imagine
printf("No image loaded\n");
return;
}
if (img->magic[1] == '5' || img->magic[1] == '6')
img->magic[1] -= 3;
printimage(img, file); // afisez informatiile despre imagine
printascii(img, file);
// confirm succesul operatiei
printf("Saved %s\n", filename);
fclose(file);
} else {
FILE *file;
file = fopen(filename, "wb"); // deschid fisierul in scriere binara
if (!file || (!img->rgb && !img->data)) { //verific existenta imaginii
printf("No image loaded\n");
return;
}
if (img->magic[1] == '2' || img->magic[1] == '3')
img->magic[1] += 3;
printimage(img, file);
printtbinary(img, file);
// confirm succesul operatiei
printf("Saved %s\n", filename);
fclose(file);
}
free(format);
}
#endif