-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBitmap.c
117 lines (114 loc) · 4.2 KB
/
Bitmap.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
/****************************************************************************/
/** **/
/** Bitmap.c **/
/** **/
/** Routines to create screen shots in Windows bitmap format **/
/** **/
/** Copyright (C) Marcel de Kogel 1996 **/
/** You are not allowed to distribute this software commercially **/
/** Please, notify me, if you make any changes to this file **/
/****************************************************************************/
#include <stdio.h>
#include "Bitmap.h"
static void putint (char *p,int n)
{
p[0]=n&255;
p[1]=(n>>8)&255;
p[2]=(n>>16)&255;
p[3]=(n>>24)&255;
}
/****************************************************************************/
/* Return values: */
/* >=0 - Succes */
/* -1 - File creation error */
/* -2 - Write error */
/****************************************************************************/
int WriteBitmap (char *szFileName, int nBitsPerPixel, int nColoursUsed,
int nWidthImageBuffer,
int nWidthImage, int nHeightImage,
char *pBitmapBits, char *pPalette)
{
int i,j,nPadBytes,nImageBytes,nBufBytes;
FILE *f;
static char bitmapheader[]=
{
/* BITMAPFILEHEADER */
0x42,0x4D, /* 0 type='BM' */
0x00,0x00,0x00,0x00, /* 2 size of file in bytes */
0x00,0x00, /* 6 reserved */
0x00,0x00, /* 8 reserved */
0x00,0x00,0x00,0x00, /* 10 offset of bitmap data */
/* BITMAPINFOHEADER */
0x28,0x00,0x00,0x00, /* 14 size of this structure in bytes */
0x00,0x00,0x00,0x00, /* 18 width of the image in pixels */
0x00,0x00,0x00,0x00, /* 22 height of the image in pixels */
0x01,0x00, /* 26 number of planes */
0x00,0x00, /* 28 bits per pixel */
0x00,0x00,0x00,0x00, /* 30 compression method */
0x00,0x00,0x00,0x00, /* 34 size of the image in bytes */
/* set to 0 if no compression is used */
0xE8,0x05,0x00,0x00, /* 38 horizontal resolution in pixels per meter */
0xE8,0x05,0x00,0x00, /* 42 vertical resolution in pixels per meter */
0x00,0x00,0x00,0x00, /* 46 number of colours used in the colour table */
0x00,0x00,0x00,0x00 /* 50 number of colours used in the image */
};
f=fopen (szFileName,"wb");
if (!f) return -1;
bitmapheader[28]=nBitsPerPixel;
putint (bitmapheader+46,nColoursUsed);
putint (bitmapheader+50,nColoursUsed);
putint (bitmapheader+18,nWidthImage);
putint (bitmapheader+22,nHeightImage);
putint (bitmapheader+10,sizeof(bitmapheader)+nColoursUsed*4);
nBufBytes=(nWidthImageBuffer*nBitsPerPixel+7)/8;
i=nImageBytes=(nWidthImage*nBitsPerPixel+7)/8;
nPadBytes=(4-(i&3))&3;
i+=nPadBytes;
putint (bitmapheader+2,(i*nHeightImage)+sizeof(bitmapheader)+nColoursUsed*4);
if (fwrite(bitmapheader,sizeof(bitmapheader),1,f)!=1)
{
fclose (f);
return -2;
}
for (i=0;i<nColoursUsed;++i)
{
if (fputc(pPalette[i*3+2],f)==EOF)
{
fclose (f);
return -2;
}
if (fputc(pPalette[i*3+1],f)==EOF)
{
fclose (f);
return -2;
}
if (fputc(pPalette[i*3+0],f)==EOF)
{
fclose (f);
return -2;
}
if (fputc(0,f)==EOF)
{
fclose (f);
return -2;
}
}
for (i=nHeightImage-1;i>=0;--i)
{
if (fwrite(pBitmapBits+nBufBytes*i,nImageBytes,1,f)!=1)
{
fclose (f);
return -2;
}
for (j=0;j<nPadBytes;++j)
{
if (fputc(0,f)==EOF)
{
fclose (f);
return -2;
}
}
}
fclose (f);
return 0;
}