This repository has been archived by the owner on Jul 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathppmFile.c
188 lines (128 loc) · 3.76 KB
/
ppmFile.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
176
177
178
179
180
181
182
183
184
185
186
187
188
/****************************************************************
*
* ppm.c
*
* Read and write PPM files. Only works for "raw" format.
*
****************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "ppmFile.h"
/************************ private functions ****************************/
/* die gracelessly */
static void
die(char const *message)
{
fprintf(stderr, "ppm: %s\n", message);
exit(1);
}
/* check a dimension (width or height) from the image file for reasonability */
static void
checkDimension(int dim)
{
if (dim < 1 || dim > 6000)
die("file contained unreasonable width or height");
}
/* read a header: verify format and get width and height */
static void
readPPMHeader(FILE *fp, int *width, int *height)
{
char ch;
int maxval;
if (fscanf(fp, "P%c\n", &ch) != 1 || ch != '6')
die("file is not in ppm raw format; cannot read");
/* skip comments */
ch = getc(fp);
while (ch == '#')
{
do {
ch = getc(fp);
} while (ch != '\n'); /* read to the end of the line */
ch = getc(fp);
}
if (!isdigit(ch)) die("cannot read header information from ppm file");
ungetc(ch, fp); /* put that digit back */
/* read the width, height, and maximum value for a pixel */
fscanf(fp, "%d%d%d\n", width, height, &maxval);
if (maxval != 255) die("image is not true-color (24 bit); read failed");
checkDimension(*width);
checkDimension(*height);
}
/************************ exported functions ****************************/
Image *
ImageCreate(int width, int height)
{
Image *image = (Image *) malloc(sizeof(Image));
if (!image) die("cannot allocate memory for new image");
image->width = width;
image->height = height;
image->data = (unsigned char *) malloc(width * height * 3);
if (!image->data) die("cannot allocate memory for new image");
return image;
}
Image *
ImageRead(char const *filename)
{
int width, height, num, size;
Image *image = (Image *) malloc(sizeof(Image));
FILE *fp = fopen(filename, "r");
if (!image) die("cannot allocate memory for new image");
if (!fp) die("cannot open file for reading");
readPPMHeader(fp, &width, &height);
size = width * height * 3;
image->data = (unsigned char*) malloc(size);
image->width = width;
image->height = height;
if (!image->data) die("cannot allocate memory for new image");
num = fread((void *) image->data, 1, (size_t) size, fp);
if (num != size) die("cannot read image data from file");
fclose(fp);
return image;
}
void ImageWrite(Image *image, char const *filename)
{
int num;
int size = image->width * image->height * 3;
FILE *fp = fopen(filename, "w");
if (!fp) die("cannot open file for writing");
fprintf(fp, "P6\n%d %d\n%d\n", image->width, image->height, 255);
num = fwrite((void *) image->data, 1, (size_t) size, fp);
if (num != size) die("cannot write image data to file");
fclose(fp);
}
int
ImageWidth(Image *image)
{
return image->width;
}
int
ImageHeight(Image *image)
{
return image->height;
}
void
ImageClear(Image *image, unsigned char red, unsigned char green, unsigned char blue)
{
int i;
int pix = image->width * image->height;
unsigned char *data = image->data;
for (i = 0; i < pix; i++)
{
*data++ = red;
*data++ = green;
*data++ = blue;
}
}
void
ImageSetPixel(Image *image, int x, int y, int chan, unsigned char val)
{
int offset = (y * image->width + x) * 3 + chan;
image->data[offset] = val;
}
unsigned char
ImageGetPixel(Image *image, int x, int y, int chan)
{
int offset = (y * image->width + x) * 3 + chan;
return image->data[offset];
}