-
Notifications
You must be signed in to change notification settings - Fork 2
/
bmp.c
125 lines (108 loc) · 4.26 KB
/
bmp.c
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
/****************************************************************************
bmp.c - read and write bmp images.
Distributed with Xplanet.
Copyright (C) 2002 Hari Nair <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct BMPHeader
{
char bfType[2]; /* "BM" */
int bfSize; /* Size of file in bytes */
int bfReserved; /* set to 0 */
int bfOffBits; /* Byte offset to actual bitmap data (= 54) */
int biSize; /* Size of BITMAPINFOHEADER, in bytes (= 40) */
int biWidth; /* Width of image, in pixels */
int biHeight; /* Height of images, in pixels */
short biPlanes; /* Number of planes in target device (set to 1) */
short biBitCount; /* Bits per pixel (24 in this case) */
int biCompression; /* Type of compression (0 if no compression) */
int biSizeImage; /* Image size, in bytes (0 if no compression) */
int biXPelsPerMeter; /* Resolution in pixels/meter of display device */
int biYPelsPerMeter; /* Resolution in pixels/meter of display device */
int biClrUsed; /* Number of colors in the color table (if 0, use
maximum allowed by biBitCount) */
int biClrImportant; /* Number of important colors. If 0, all colors
are important */
};
int
read_bmp(const char *filename, int *width, int *height, unsigned char *rgb)
{
fprintf(stderr, "Sorry, reading of .bmp files isn't supported yet.\n");
return(0);
}
int
write_bmp(const char *filename, int width, int height, char *rgb)
{
int i, j, ipos;
int bytesPerLine;
unsigned char *line;
FILE *file;
struct BMPHeader bmph;
/* The length of each line must be a multiple of 4 bytes */
bytesPerLine = (3 * (width + 1) / 4) * 4;
strcpy(bmph.bfType, "BM");
bmph.bfOffBits = 54;
bmph.bfSize = bmph.bfOffBits + bytesPerLine * height;
bmph.bfReserved = 0;
bmph.biSize = 40;
bmph.biWidth = width;
bmph.biHeight = height;
bmph.biPlanes = 1;
bmph.biBitCount = 24;
bmph.biCompression = 0;
bmph.biSizeImage = bytesPerLine * height;
bmph.biXPelsPerMeter = 0;
bmph.biYPelsPerMeter = 0;
bmph.biClrUsed = 0;
bmph.biClrImportant = 0;
file = fopen (filename, "wb");
if (file == NULL) return(0);
fwrite(&bmph.bfType, 2, 1, file);
fwrite(&bmph.bfSize, 4, 1, file);
fwrite(&bmph.bfReserved, 4, 1, file);
fwrite(&bmph.bfOffBits, 4, 1, file);
fwrite(&bmph.biSize, 4, 1, file);
fwrite(&bmph.biWidth, 4, 1, file);
fwrite(&bmph.biHeight, 4, 1, file);
fwrite(&bmph.biPlanes, 2, 1, file);
fwrite(&bmph.biBitCount, 2, 1, file);
fwrite(&bmph.biCompression, 4, 1, file);
fwrite(&bmph.biSizeImage, 4, 1, file);
fwrite(&bmph.biXPelsPerMeter, 4, 1, file);
fwrite(&bmph.biYPelsPerMeter, 4, 1, file);
fwrite(&bmph.biClrUsed, 4, 1, file);
fwrite(&bmph.biClrImportant, 4, 1, file);
line = (unsigned char*)malloc(bytesPerLine);
if (line == NULL)
{
fprintf(stderr, "Can't allocate memory for BMP file.\n");
return(0);
}
for (i = height - 1; i >= 0; i--)
{
for (j = 0; j < width; j++)
{
ipos = 3 * (width * i + j);
line[3*j] = rgb[ipos + 2];
line[3*j+1] = rgb[ipos + 1];
line[3*j+2] = rgb[ipos];
}
fwrite(line, bytesPerLine, 1, file);
}
free(line);
fclose(file);
return(1);
}