forked from YueDayu/Themis_GUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitmap.c
More file actions
92 lines (81 loc) · 2.55 KB
/
bitmap.c
File metadata and controls
92 lines (81 loc) · 2.55 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "types.h"
#include "stat.h"
#include "fcntl.h"
#include "user.h"
#include "x86.h"
#include "gui_base.h"
#include "bitmap.h"
void readBitmapHeader(int bmpFile, BITMAP_FILE_HEADER *bmpFileHeader, BITMAP_INFO_HEADER *bmpInfoHeader) {
// Read Bitmap file header
read(bmpFile, bmpFileHeader, sizeof(BITMAP_FILE_HEADER));
// Read Bitmap info header
read(bmpFile, bmpInfoHeader, sizeof(BITMAP_INFO_HEADER));
}
int readBitmapFile(char *fileName, RGBA *result, int *height, int *width) {
int i;
int bmpFile = open(fileName, 0);
if (bmpFile < 0) {
return -1;
}
BITMAP_FILE_HEADER bmpFileHeader;
BITMAP_INFO_HEADER bmpInfoHeader;
readBitmapHeader(bmpFile, &bmpFileHeader, &bmpInfoHeader);
*width = bmpInfoHeader.biWidth;
*height = bmpInfoHeader.biHeight;
int column = bmpInfoHeader.biWidth;
int row = bmpInfoHeader.biHeight;
int bits = bmpInfoHeader.biBitCount;
char tmpBytes[3];
int rowBytes = column * bits / 8;
char *buf = (char *) result;
for (i = 0; i < row; i++) {
if (bits == 32) {
read(bmpFile, buf + i * rowBytes, rowBytes);
} else {
int j = 0;
for (j = 0; j < column; j++) {
read(bmpFile, buf + i * column * 4 + j * sizeof(RGBA), 3);
*(buf + i * column * 4 + j * sizeof(RGBA) + 3) = 255;
}
}
if (rowBytes % 4 > 0) {
read(bmpFile, tmpBytes, 4 - (rowBytes % 4));
}
}
close(bmpFile);
return 0;
}
int read24BitmapFile(char *fileName, RGB *result, int *height, int *width) {
int i;
int bmpFile = open(fileName, 0);
if (bmpFile < 0) {
return -1;
}
BITMAP_FILE_HEADER bmpFileHeader;
BITMAP_INFO_HEADER bmpInfoHeader;
readBitmapHeader(bmpFile, &bmpFileHeader, &bmpInfoHeader);
*width = bmpInfoHeader.biWidth;
*height = bmpInfoHeader.biHeight;
int column = bmpInfoHeader.biWidth;
int row = bmpInfoHeader.biHeight;
int bits = bmpInfoHeader.biBitCount;
char tmpBytes[3];
int rowBytes = column * 3;
char *buf = (char *) result;
for (i = 0; i < row; i++) {
if (bits == 24) {
read(bmpFile, buf + i * rowBytes, rowBytes);
} else {
int j = 0;
for (j = 0; j < column; j++) {
read(bmpFile, buf + i * column * 3 + j * sizeof(RGB), 3);
read(bmpFile, tmpBytes, 1);
}
}
if (rowBytes % 4 > 0) {
read(bmpFile, tmpBytes, 4 - (rowBytes % 4));
}
}
close(bmpFile);
return 0;
}