-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLadeBMP.h
More file actions
67 lines (55 loc) · 1.65 KB
/
LadeBMP.h
File metadata and controls
67 lines (55 loc) · 1.65 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
#pragma once
#include <stdio.h>
#include <stdlib.h>
unsigned char *loadBMP24(const char *filename, int *width, int *height) {
printf("Reading image %s\n", filename);
unsigned char header[54];
unsigned int imageSize;
// Die BGR Daten
unsigned char *data;
FILE *file = fopen(filename, "rb");
if (!file) {
printf("Textur %s wurde nicht gefunden!\n", filename);
getchar();
return 0;
}
// Dateiheader einlesen
if (fread(header, 1, 54, file) != 54) {
printf("Nicht das richte (24bit) Bitmapformat!\n");
fclose(file);
return 0;
}
// Sind die ersten beiden Bytes "BM"?
if (header[0] != 'B' || header[1] != 'M') {
printf("Nicht das richte (24bit) Bitmapformat!\n");
fclose(file);
return 0;
}
// Darf keine Kompression beinhalten
if (*(int *)&(header[0x1E]) != 0) {
printf("Nicht das richte Bitmapformat (keine Kompression erlaubt)!\n");
fclose(file);
return 0;
}
// Farbtiefe muss bei 24 bit sein
if (*(int *)&(header[0x1C]) != 24) {
printf("Nicht das richte (24bit) Bitmapformat!\n");
fclose(file);
return 0;
}
// Bilddaten ermitteln
imageSize = *(int *)&(header[0x22]);
*width = *(int *)&(header[0x12]);
*height = *(int *)&(header[0x16]);
// Ergänzugen bei fehlerhaftem Header
if (imageSize == 0) {
imageSize = (*width) * (*height) *
3; // 3 : one byte for each Red, Green and Blue component
}
data = (unsigned char *)malloc(sizeof(unsigned char) * imageSize);
// Lesen der Binärdaten
fread(data, 1, imageSize, file);
fclose(file);
// Pixeldaten zurückgeben
return data;
}