-
Notifications
You must be signed in to change notification settings - Fork 0
/
readwriteppm.c
55 lines (47 loc) · 1.51 KB
/
readwriteppm.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
#include "a4.h"
#include <stdlib.h>
#include <stdio.h>
PPM_IMAGE *read_ppm(const char *file_name){
int width, height, max_colour, Error;
char p6;
FILE *file;
file = fopen(file_name,"r");
Error = fscanf(file, "%s\n", &p6);
Error = fscanf(file, "%d %d\n", &width, &height);
Error = fscanf(file, "%d\n", &max_colour);
if (!Error) return NULL;
PIXEL *A = malloc ((width*height)* sizeof(PIXEL));
int i = 0;
while(!feof(file)){
Error = fscanf(file, "%hhu\n", &(A[i].r));
Error = fscanf(file, "%hhu\n", &(A[i].g));
Error = fscanf(file, "%hhu\n", &(A[i].b));
i++;
}
if (!Error) return NULL;
PPM_IMAGE *Image = malloc(sizeof(PPM_IMAGE));
Image->data=A;
Image->max_color=max_colour;
Image->width=width;
Image->height=height;
fclose(file);
return Image;
}
void write_ppm(const char *file_name, const PPM_IMAGE *image){
int width = image->width;
FILE *file;
file = fopen(file_name,"w");
fprintf(file, "P3\n");
fprintf(file,"%d %d\n", width, image->height);
fprintf(file,"%d\n", image->max_color);
for (int i = 0; i < (image->height) ; i++) {
int t = i * width;
for (int j = 0; j < (image->width) ; j++) {
fprintf(file, "%hhu ", (image->data)[t+j].r);
fprintf(file, "%hhu ", (image->data)[t+j].g);
fprintf(file, "%hhu ", (image->data)[t+j].b);
}
fprintf(file, "\n");
}
fclose(file);
}