-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmpWriting_v1_1_3.h
162 lines (153 loc) · 4.92 KB
/
bmpWriting_v1_1_3.h
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#ifndef _BMP_H
#define _BMP_H 0x010103
#include <stdio.h>
#include <malloc.h>
#ifdef __cplusplus
extern "C" {
#endif
char bmpHeader[] = {
0x42, 0x4d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 7
0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00, //15
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //23
0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00, //31
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //39
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //47
0x00, 0x00, 0x00, 0x00, 0x00, 0x00 //53
};
char pad[3] = {0, 0, 0};
typedef unsigned long _ulong;
struct _Bitmap {
FILE* file;
int flag;
int readable;
_ulong width;
_ulong height;
_ulong padSize;
_ulong rowSize;
_ulong dataSize;
_ulong fileSize;
};
typedef struct _Bitmap BITMAP;
BITMAP* BitmapHeaderInit(const char* filename, int readable, _ulong width, _ulong height) {
BITMAP *bitmap = (BITMAP*)malloc(sizeof(BITMAP));
bitmap -> file = fopen(filename, readable?"wb+":"wb");
bitmap -> flag = ferror(bitmap -> file) ? 0 : 1;
bitmap -> readable = readable;
bitmap -> width = width;
bitmap -> height = height;
bitmap -> padSize = width % 4;
bitmap -> rowSize = width * 3 + bitmap -> padSize;
bitmap -> dataSize = bitmap -> rowSize * height;
bitmap -> fileSize = bitmap -> dataSize + 54;
*(_ulong*)(bmpHeader + 0x02) = bitmap -> fileSize;
*(_ulong*)(bmpHeader + 0x12) = width;
*(_ulong*)(bmpHeader + 0x16) = height;
*(_ulong*)(bmpHeader + 0x22) = bitmap -> dataSize;
fwrite(bmpHeader, 54, 1, bitmap -> file);
fflush(bitmap -> file);
return bitmap;
}
BITMAP* BitmapInit(char* filename, int readable, _ulong width, _ulong height, _ulong bgcolor) {
BITMAP *bitmap = BitmapHeaderInit(filename, readable, width, height);
_ulong i, j;
for(i=0; i<height; ++i) {
for(j=0; j<width; ++j){
fwrite((char*)&bgcolor, 3, 1, bitmap -> file);
}
fwrite(pad, bitmap -> padSize, 1, bitmap -> file);
fflush(bitmap -> file);
}
return bitmap;
}
inline void BitmapSetAt(BITMAP* bitmap, _ulong x, _ulong y, _ulong color) {
fseek(bitmap -> file, 54 + bitmap -> rowSize * x + 3 * y, 0);
fwrite((char*)&color, 3, 1, bitmap -> file);
}
inline void BitmapSetThrough(BITMAP* bitmap, _ulong color) {
fwrite((char*)&color, 3, 1, bitmap -> file);
}
inline void BitmapGetAt(BITMAP* bitmap, _ulong x, _ulong y, _ulong* color) {
fseek(bitmap -> file, 54 + bitmap -> rowSize * x + 3 * y, 0);
fread((char*)color, 3, 1, bitmap -> file);
}
inline void BitmapGetThrough(BITMAP* bitmap, _ulong* color) {
fread((char*)color, 3, 1, bitmap -> file);
}
inline void BitmapWritePad(BITMAP* bitmap) {
fwrite(pad, bitmap -> padSize, 1, bitmap -> file);
}
inline void BitmapSeek(BITMAP* bitmap, _ulong x, _ulong y) {
fseek(bitmap -> file, 54 + bitmap -> rowSize * x + 3 * y, 0);
}
inline void BitmapFlush(BITMAP* bitmap) {
fflush(bitmap -> file);
}
inline void BitmapClose(BITMAP* bitmap) {
fclose(bitmap -> file);
}
inline _ulong BitmapRainbowColor(_ulong x) {
switch(x / 15) {
case 0: return 0xff00ff | (15-x) * 17 << 8;
case 1: return 0x0000ff | (30-x) * 17 << 16;
case 2: return 0x0000ff | (x-30) * 17 << 8;
case 3: return 0x00ff00 | (60-x) * 17;
case 4: return 0x00ff00 | (x-60) * 17 << 16;
case 5: return 0xff0000 | (90-x) * 17 << 8;
case 6: return 0xff0000 | (x-90) * 17 << 16;
case 7: return (120-x) * 17 << 16;
default:return 0x000000;
}
return 0;
}
inline _ulong BitmapReversedRainbowColor(_ulong x) {
switch(x / 15) {
case 0: return 0xff00ff | (15-x) * 17 << 8;
case 1: return 0xff0000 | (30-x) * 17;
case 2: return 0xff0000 | (x-30) * 17 << 8;
case 3: return 0x00ff00 | (60-x) * 17 << 16;
case 4: return 0x00ff00 | (x-60) * 17;
case 5: return 0x0000ff | (90-x) * 17 << 8;
case 6: return 0x0000ff | (x-90) * 17;
case 7: return (120-x) * 17;
default:return 0x000000;
}
return 0;
}
inline _ulong BitmapLongRainbowColor(_ulong x) {
switch(x / 17) {
case 0: return 0xff00ff | (17-x) * 15 << 8;
case 1: return 0x0000ff | (34-x) * 15 << 16;
case 2: return 0x0000ff | (x-34) * 15 << 8;
case 3: return 0x00ff00 | (68-x) * 15;
case 4: return 0x00ff00 | (x-68) * 15 << 16;
case 5: return 0xff0000 | (102-x)* 15 << 8;
case 6: return 0xff0000 | (x-102)* 15 << 16;
case 7: return (136-x) * 15 << 16;
default:return 0x000000;
}
return 0;
}
inline _ulong BitmapLongReversedRainbowColor(_ulong x) {
switch(x / 17) {
case 0: return 0xff00ff | (17-x) * 15 << 8;
case 1: return 0xff0000 | (34-x) * 15;
case 2: return 0xff0000 | (x-34) * 15 << 8;
case 3: return 0x00ff00 | (68-x) * 15 << 16;
case 4: return 0x00ff00 | (x-68) * 15;
case 5: return 0x0000ff | (102-x)* 15 << 8;
case 6: return 0x0000ff | (x-102)* 15;
case 7: return (136-x) * 15;
default:return 0x000000;
}
return 0;
}
inline double BitmapConvertCenterLocalCoord(double midpoint, double ratio, double width, double coord) {
return midpoint + (coord - width / 2) / ratio;
}
inline double BitmapConvertEdgeLocalCoord(double edge, double ratio, double coord) {
return edge + coord / ratio;
}
#ifdef __cplusplus
}
#endif
#endif