-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitmap_loader.h
113 lines (95 loc) · 2.89 KB
/
bitmap_loader.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
/*
* bitmap_loader.h
*
* Created on: Jan 8, 2017
* Updated on: January 27, 2017
* Author: Jisan Mahmud
*/
#pragma warning(disable:4996)
#include <stdio.h>
#define _RGB_TO_INT(rgb) ((rgb).rgbtRed | ((rgb).rgbtGreen << 8) | ((rgb).rgbtBlue << 16))
//
// Puts a BMP image on screen
//
// parameters:
// x - x coordinate
// y - y coordinate
// fileName - name of the BMP file
// ignoreColor - A specified color that should not be rendered. If you have an
// image strip that should be rendered on top of another back
// ground image, then the background of the image strip should
// not get rendered. Use the background color of the image strip
// in ignoreColor parameter. Then the strip's background does
// not get rendered.
//
// To disable this feature, put -1 in this parameter
//
void iShowBMPAlternative2(int x, int y, char fileName[], int ignoreColor)
{
FILE *bmpFile;
if ((bmpFile = fopen(fileName, "rb")) == NULL)
return;
BITMAPFILEHEADER fileHeader;
BITMAPINFOHEADER infoHeader;
fread(&fileHeader, sizeof(fileHeader), 1, bmpFile);
fread(&infoHeader, sizeof(infoHeader), 1, bmpFile);
int effectiveH = infoHeader.biHeight + (y < 0 ? y : 0);
int effectiveW = infoHeader.biWidth + (x < 0 ? x : 0);
if (effectiveH < 1 || effectiveW < 1)
{
fclose(bmpFile);
return;
}
int *pixelMap = (int *)malloc(effectiveW * effectiveH * sizeof(int));
int curPixel, r, c, pixelTrack = 0;
RGBTRIPLE rgb;
int alphaSkip = (0xFF << 24);
for (r = 0; r < infoHeader.biHeight; r++)
{
for (c = 0; c < infoHeader.biWidth; c++)
{
fread(&rgb, sizeof(rgb), 1, bmpFile);
if (r >= -y && c >= -x)
{
curPixel = _RGB_TO_INT(rgb);
pixelMap[pixelTrack++] = (curPixel == ignoreColor ? curPixel : curPixel | alphaSkip);
}
}
}
fclose(bmpFile);
glRasterPos2f(x < 0 ? 0 : x, y < 0 ? 0 : y);
glDrawPixels(effectiveW, effectiveH, GL_RGBA, GL_UNSIGNED_BYTE, pixelMap);
free(pixelMap);
}
//
// Puts a BMP image on screen
//
// parameters:
// x - x coordinate
// y - y coordinate
// fileName - name of the BMP file
//
void iShowBMPAlternative(int x, int y, char fileName[])
{
iShowBMPAlternative2(x, y, fileName, -1);
}
void iShowBMPAlternativeSkipBlack(int x, int y, char fileName[])
{
iShowBMPAlternative2(x, y, fileName, 0);
}
void iShowBMPAlternativeSkipRed(int x, int y, char fileName[])
{
iShowBMPAlternative2(x, y, fileName, 0xFF);
}
void iShowBMPAlternativeSkipGreen(int x, int y, char fileName[])
{
iShowBMPAlternative2(x, y, fileName, (0xFF << 8));
}
void iShowBMPAlternativeSkipBlue(int x, int y, char fileName[])
{
iShowBMPAlternative2(x, y, fileName, (0xFF << 16));
}
void iShowBMPAlternativeSkipWhite(int x, int y, char fileName[])
{
iShowBMPAlternative2(x, y, fileName, (1 << 24) - 1);
}