-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadimage.h
More file actions
68 lines (61 loc) · 1.44 KB
/
Copy pathloadimage.h
File metadata and controls
68 lines (61 loc) · 1.44 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
#include <stdio.h>
#include <stdlib.h>
#include "structs.h"
#include "readp2.h"
#include "readp3.h"
#include "readp5.h"
#include "readp6.h"
#include <string.h>
#ifndef LOADIMAGE_H
#define LOADIMAGE_H
void loadimage(char *filename, image *img)
{
// deschid fisierul
FILE *file = fopen(filename, "rb");
// verific existenta fisierului
if (!file) {
printf("Failed to load %s\n", filename);
img->rgb = NULL;
img->data = NULL;
return;
}
char line[1000];
// ignor comentariile pana gasesc magic word ul
while (fgets(line, sizeof(line), file)) {
if (line[0] == '#')
continue;
if (sscanf(line, "%s", img->magic) == 1)
break;
}
// verific daca exista ceva in fisier
if (strlen(img->magic) == 0) {
printf("Failed to load %s\n", filename);
return;
}
// ignor comentariile pana gasesc dimensiunile
while (fgets(line, sizeof(line), file)) {
if (line[0] == '#')
continue;
if (sscanf(line, "%d %d", &img->width, &img->height) == 2)
break;
}
// ignor comentariile pana gasesc maxval
while (fgets(line, sizeof(line), file)) {
if (line[0] == '#')
continue;
if (sscanf(line, "%d", &img->maxval) == 1)
break;
}
// verific tipul imaginii
if (strcmp(img->magic, "P2") == 0)
readp2(file, img);
if (strcmp(img->magic, "P5") == 0)
readp5(file, img);
if (strcmp(img->magic, "P3") == 0)
readp3(file, img);
if (strcmp(img->magic, "P6") == 0)
readp6(file, img);
printf("Loaded %s\n", filename);
fclose(file);
}
#endif