forked from trosh/ATOI24_TP_Profilage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathppm.c
More file actions
56 lines (41 loc) · 839 Bytes
/
ppm.c
File metadata and controls
56 lines (41 loc) · 839 Bytes
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
#include "ppm.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int ppm_image_init(struct ppm_image *im, int w, int h)
{
memset(im, 0, sizeof(struct ppm_image));
im->width = w;
im->height = h;
im->px = malloc(w * h * sizeof(struct ppm_pixel));
if (!im->px) {
perror("malloc");
return 1;
}
return 0;
}
int ppm_image_release(struct ppm_image *im)
{
if (im == NULL)
return 1;
free(im->px);
im->px = NULL;
im->width = 0;
im->height = 0;
return 0;
}
int ppm_image_dump(struct ppm_image *im, char *path)
{
FILE *out = fopen(path, "w");
if (!out) {
perror("fopen");
return 1;
}
fprintf(out, "P6\n");
fprintf(out, "%d\n", im->width);
fprintf(out, "%d\n", im->height);
fprintf(out, "255\n");
fwrite(im->px, sizeof(struct ppm_pixel), im->width * im->height, out);
fclose(out);
return 0;
}