-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileIO.c
175 lines (161 loc) · 4.16 KB
/
FileIO.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*********************************************************************/
/* FileIO.c: program file for I/O module */
/* */
/* 11/01/17 Mihnea Chirila updated for EECS22 assignment 4 Fall2017 */
/* 10/31/16 Huan Chen updated for EECS22 assignment4 Fall2016 */
/* 11/03/15 Guantao Liu updated for EECS22 assignment4 Fall2015 */
/* 10/29/13 Alex Chu updated for EECS22 assignment4 Fall2013 */
/* 10/16/11 Weiwei Chen updated for EECS22 assignment3 FAll2012 */
/* 10/07/11 Weiwei Chen: initial solution version */
/* for EECS22 assignment3 FAll2011 */
/*********************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "Constants.h"
#include "FileIO.h"
#include "Image.h"
IMAGE *LoadImage(const char *fname)
{
FILE *File;
char Type[SLEN];
int W, H, MaxValue;
unsigned int x, y;
char ftype[] = ".ppm";
char fname_tmp[SLEN];
IMAGE *image;
strcpy(fname_tmp, fname);
strcat(fname_tmp, ftype);
File = fopen(fname_tmp, "r");
if (!File) {
#ifdef DEBUG
printf("\nCan't open file \"%s\" for reading!\n", fname);
#endif
return NULL;
}
fscanf(File, "%79s", Type);
if (Type[0] != 'P' || Type[1] != '6' || Type[2] != 0) {
#ifdef DEBUG
printf("\nUnsupported file format!\n");
#endif
fclose(File);
return NULL;
}
fscanf(File, "%d", &W);
if (W <= 0) {
#ifdef DEBUG
printf("\nUnsupported image width %d!\n", W);
#endif
fclose(File);
return NULL;
}
fscanf(File, "%d", &H);
if (H <= 0) {
#ifdef DEBUG
printf("\nUnsupported image height %d!\n", H);
#endif
fclose(File);
return NULL;
}
fscanf(File, "%d", &MaxValue);
if (MaxValue != 255) {
#ifdef DEBUG
printf("\nUnsupported image maximum value %d!\n", MaxValue);
#endif
fclose(File);
return NULL;
}
if ('\n' != fgetc(File)) {
#ifdef DEBUG
printf("\nCarriage return expected at the end of the file!\n");
#endif
fclose(File);
return NULL;
}
image = CreateImage(W, H);
if (!image) {
#ifdef DEBUG
printf("\nError creating image from %s!\n", fname_tmp);
#endif
fclose(File);
return NULL;
}
else {
for (y = 0; y < ImageHeight(image); y++)
for (x = 0; x < ImageWidth(image); x++) {
SetPixelR(image, x, y, fgetc(File));
SetPixelG(image, x, y, fgetc(File));
SetPixelB(image, x, y, fgetc(File));
}
if (ferror(File)) {
#ifdef DEBUG
printf("\nFile error while reading from file!\n");
#endif
DeleteImage(image);
return NULL;
}
#ifdef DEBUG
printf("%s was read successfully!\n", fname_tmp);
#endif
fclose(File);
return image;
}
}
int SaveImage(const char *fname, const IMAGE *image)
{
assert(image != NULL && "No image to save!\n");
FILE *File;
int x, y;
char SysCmd[SLEN * 5];
char ftype[] = ".ppm";
char fname_tmp[SLEN];
char fname_tmp2[SLEN];
unsigned int WIDTH = ImageWidth(image);
unsigned int HEIGHT = ImageHeight(image);
strcpy(fname_tmp, fname);
strcpy(fname_tmp2, fname);
strcat(fname_tmp2, ftype);
File = fopen(fname_tmp2, "w");
if (!File) {
#ifdef DEBUG
printf("\nCan't open file \"%s\" for writing!\n", fname);
#endif
return 1;
}
fprintf(File, "P6\n");
fprintf(File, "%d %d\n", WIDTH, HEIGHT);
fprintf(File, "255\n");
for (y = 0; y < HEIGHT; y++)
for (x = 0; x < WIDTH; x++) {
fputc(GetPixelR(image, x, y), File);
fputc(GetPixelG(image, x, y), File);
fputc(GetPixelB(image, x, y), File);
}
if (ferror(File)) {
#ifdef DEBUG
printf("\nError while writing to file!\n");
#endif
return 2;
}
fclose(File);
#ifdef DEBUG
printf("%s was saved successfully. \n", fname_tmp2);
#endif
/*
* Rename file to image.ppm, convert it to ~/public_html/<fname>.jpg
* and make it world readable
*/
sprintf(SysCmd, "/users/grad2/doemer/eecs22/bin/pnmtojpeg_hw4.tcsh %s",
fname_tmp2);
if (system(SysCmd) != 0) {
#ifdef DEBUG
printf("\nError while converting to JPG:\nCommand \"%s\" failed!\n", SysCmd);
#endif
return 3;
}
#ifdef DEBUG
printf("%s.jpg was stored for viewing. \n", fname_tmp);
#endif
return 0;
}