forked from paulhoad/gp2trackeditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitmaps.cpp
More file actions
executable file
·215 lines (172 loc) · 4.83 KB
/
Copy pathBitmaps.cpp
File metadata and controls
executable file
·215 lines (172 loc) · 4.83 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "stdafx.h"
#include "imagedit.h"
#include <stdio.h>
#include <io.h>
#include <fcntl.h>// For NT fstat().
#include <sys\types.h>// For fstat() types.
#include <sys\stat.h>// For fstat() function.
BOOL LoadBitmapFile(PSTR pszFullFileName)
{
HFILE hf;
OFSTRUCT OfStruct;
struct stat FileStatus;
DWORD dwFileSize;
DWORD dwDIBSize;
BITMAPFILEHEADER bfh;
BITMAPINFOHEADER bih;
PIMAGEINFO pImage;
INT nColors;
HANDLE hDIB;
PDEVICE pDevice;
if ((hf = (HFILE)OpenFile(pszFullFileName, (LPOFSTRUCT)&OfStruct, OF_READ)) == (HFILE)-1) {
return FALSE;
}
fstat((INT)_open_osfhandle((long)(hf), (int)(O_RDONLY)), &FileStatus);
dwFileSize = (DWORD)FileStatus.st_size;
// ImageLinkFreeList();
/*
* Read the Bitmap File Header.
*/
if (!MyFileRead(hf, (LPSTR)&bfh, sizeof(BITMAPFILEHEADER), pszFullFileName, FT_BITMAP))
goto Error1;
/*
* Check for the "BM" at the start of the file, and the file size
* in the header must match the real file size.
*/
if (bfh.bfType != 0x4D42 || bfh.bfSize != dwFileSize) {
Message(MSG_BADBMPFILE, pszFullFileName);
goto Error1;
}
/*
* Read the Bitmap Info Header.
*/
if (!MyFileRead(hf, (LPSTR)&bih, sizeof(BITMAPINFOHEADER), pszFullFileName, FT_BITMAP))
goto Error1;
/*
* The DIB size should be the size of the file less the bitmap
* file header.
*/
dwDIBSize = dwFileSize - sizeof(BITMAPFILEHEADER);
if (!IsValidDIB((LPBITMAPINFO)&bih, dwDIBSize, FALSE)) {
Message(MSG_BADBMPFILE, pszFullFileName);
goto Error1;
}
/*
* There is a limit to the size of image we will edit. For icons
* and cursors, the field that carries the dimensions is a byte,
* so the size cannot be greater than 256. This is also what
* we limit bitmaps to.
*/
if (bih.biWidth > MAXIMAGEDIM || bih.biHeight > MAXIMAGEDIM) {
Message(MSG_BADBMPSIZE, MAXIMAGEDIM, MAXIMAGEDIM);
goto Error1;
}
switch (bih.biBitCount) {
case 1:
nColors = 2;
break;
case 4:
nColors = 16;
break;
default:
Message(MSG_NOTSUPPORT);
goto Error1;
}
if (!(pDevice = DeviceLinkAlloc(FT_BITMAP, NULL, nColors, (INT)bih.biWidth, (INT)bih.biHeight))) {
goto Error1;
}
if (!(pImage = ImageLinkAlloc(pDevice, (INT)bih.biWidth, (INT)bih.biHeight, 0, 0, nColors)))
goto Error1;
/*
* Allocate space for the DIB for this image.
*/
if (!(hDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, dwDIBSize))) {
Message(MSG_OUTOFMEMORY);
goto Error2;
}
pImage->DIBSize = dwDIBSize;
pImage->DIBhandle = hDIB;
pImage->DIBPtr = (LPSTR)GlobalLock(hDIB);
/*
* Jump back to the start of the DIB.
*/
SetFilePointer((HANDLE)hf, sizeof(BITMAPFILEHEADER), NULL, (DWORD)0);
/*
* Read the entire DIB (including info and color table) into
* the allocated memory for it.
*/
if (!MyFileRead(hf, pImage->DIBPtr, (DWORD)pImage->DIBSize, pszFullFileName, FT_BITMAP))
goto Error2;
_lclose((HFILE)hf);
fFileDirty = FALSE;
SetFileName(pszFullFileName);
giType = FT_BITMAP;
/*
* Bitmaps only have one "image" in the file.
*/
gnImages = 1;
ImageOpen2(pImage);
return TRUE;
Error2:
ImageLinkFreeList();
Error1:
_lclose((HFILE)hf);
return FALSE;
}
/************************************************************************
* SaveBitmapFile
*
*
*
* Arguments:
*
* Returns:
* TRUE if successful, FALSE otherwise.
*
* History:
*
************************************************************************/
BOOL SaveBitmapFile(PSTR pszFullFileName)
{
HCURSOR hcurOld;
BITMAPFILEHEADER bfh;
HFILE hf;
OFSTRUCT OfStruct;
hcurOld = SetCursor(hcurWait);
/*
* Save the bits of the current image.
*/
ImageSave();
/*
* Open the file for writing.
*/
if ((hf = (HFILE)OpenFile(pszFullFileName, &OfStruct, OF_CREATE | OF_READWRITE)) == (HFILE)-1) {
Message(MSG_CANTCREATE, pszFullFileName);
goto Error1;
}
bfh.bfType = 0x4D42;
bfh.bfSize = sizeof(BITMAPFILEHEADER) + gpImageCur->DIBSize;
bfh.bfReserved1 = 0;
bfh.bfReserved2 = 0;
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (gpImageCur->nColors * sizeof(RGBQUAD));
/*
* Write the header to disk.
*/
if (!MyFileWrite(hf, (LPSTR)&bfh, sizeof(BITMAPFILEHEADER), pszFullFileName))
goto Error2;
/*
* Now write the DIB (a bitmap file only has one).
*/
if (!MyFileWrite(hf, (LPSTR)gpImageCur->DIBPtr, (DWORD)gpImageCur->DIBSize, pszFullFileName))
goto Error2;
_lclose((HFILE)hf);
fFileDirty = FALSE;
SetFileName(pszFullFileName);
SetCursor(hcurOld);
return TRUE;
Error2:
_lclose((HFILE)hf);
Error1:
SetCursor(hcurOld);
return FALSE;
}