1 | #include "bmpUtil.h"
|
---|
2 |
|
---|
3 | #include <stdio.h>
|
---|
4 | #include <stdlib.h>
|
---|
5 |
|
---|
6 | HPALETTE bmpUtil_getDIBPalette(LPBITMAPINFO bmpInfo, unsigned int colours)
|
---|
7 | { LPBITMAPINFOHEADER bmpHeader;
|
---|
8 | LPLOGPALETTE logicalPalette;
|
---|
9 | HPALETTE reply = 0;
|
---|
10 |
|
---|
11 | bmpHeader = (LPBITMAPINFOHEADER) bmpInfo;
|
---|
12 | MessageBox(0, "A", "A", MB_OK);
|
---|
13 |
|
---|
14 | if (colours)
|
---|
15 | {
|
---|
16 | MessageBox(0, "A", "A", MB_OK);
|
---|
17 | logicalPalette = (LPLOGPALETTE) GlobalAlloc(GMEM_FIXED, sizeof (LOGPALETTE) + sizeof(PALETTEENTRY) * (colours));
|
---|
18 | if (logicalPalette == NULL)
|
---|
19 | { return reply;
|
---|
20 | }
|
---|
21 |
|
---|
22 | logicalPalette->palVersion = 0x300;
|
---|
23 | logicalPalette->palNumEntries = colours;
|
---|
24 | MessageBox(0, "A", "A", MB_OK);
|
---|
25 | for (int i = 0; i < colours; i ++)
|
---|
26 | { logicalPalette->palPalEntry[i].peRed = bmpInfo->bmiColors[i].rgbRed;
|
---|
27 | logicalPalette->palPalEntry[i].peGreen = bmpInfo->bmiColors[i].rgbGreen;
|
---|
28 | logicalPalette->palPalEntry[i].peBlue = bmpInfo->bmiColors[i].rgbBlue;
|
---|
29 | logicalPalette->palPalEntry[i].peFlags = 0;
|
---|
30 | }
|
---|
31 | reply = CreatePalette(logicalPalette);
|
---|
32 | GlobalFree((HGLOBAL) logicalPalette);
|
---|
33 | }
|
---|
34 | return reply;
|
---|
35 | }
|
---|
36 |
|
---|
37 | HBITMAP _export bmpUtil_loadBitmap(HDC device, char *filename)
|
---|
38 | { BYTE dummy[2];
|
---|
39 | BITMAPINFO bitmapInfo;
|
---|
40 | OFSTRUCT of;
|
---|
41 |
|
---|
42 | HFILE file;
|
---|
43 | BITMAPFILEHEADER fileHeader;
|
---|
44 | LPRGBQUAD colours;
|
---|
45 | LPBITMAPINFO bitmapData;
|
---|
46 | LPBYTE image;
|
---|
47 | unsigned int colourCount;
|
---|
48 | HBITMAP reply;
|
---|
49 | char buffer[40];
|
---|
50 |
|
---|
51 | file = OpenFile(filename, &of, OF_READ);
|
---|
52 | if (!file)
|
---|
53 | { return NULL;
|
---|
54 | }
|
---|
55 |
|
---|
56 | if (!_lread(file, &fileHeader, sizeof(BITMAPFILEHEADER)))
|
---|
57 | { MessageBox(0, "M1", "M", MB_OK);
|
---|
58 | }
|
---|
59 | if (!_lread(file, &bitmapInfo.bmiHeader, 40))
|
---|
60 | { MessageBox(0, "M2", "M", MB_OK);
|
---|
61 | }
|
---|
62 |
|
---|
63 | colourCount = 1 << bitmapInfo.bmiHeader.biBitCount;
|
---|
64 | bitmapData = (BITMAPINFO *) GlobalAlloc(GMEM_FIXED, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * colourCount);
|
---|
65 | if (!bitmapData)
|
---|
66 | { MessageBox(0, "M3", "M", MB_OK);
|
---|
67 | }
|
---|
68 |
|
---|
69 | bitmapData->bmiHeader = bitmapInfo.bmiHeader;
|
---|
70 |
|
---|
71 | colours = (RGBQUAD *) &bitmapData->bmiColors;
|
---|
72 | if (_lread(file, colours, sizeof(RGBQUAD) * colourCount) < colourCount)
|
---|
73 | { MessageBox(0, "M4", "M", MB_OK);
|
---|
74 | }
|
---|
75 |
|
---|
76 | image = (BYTE FAR *) GlobalAlloc(GMEM_FIXED, sizeof(BYTE) * bitmapData->bmiHeader.biSizeImage);
|
---|
77 | if (!image)
|
---|
78 | { MessageBox(0, "M5", "M", MB_OK);
|
---|
79 | }
|
---|
80 | if (_lread(file, image, sizeof(BYTE) * bitmapData->bmiHeader.biSizeImage) < bitmapData->bmiHeader.biSizeImage)
|
---|
81 | { MessageBox(0, "M", "M6", MB_OK);
|
---|
82 | }
|
---|
83 |
|
---|
84 | HPALETTE pal = bmpUtil_getDIBPalette(bitmapData, colourCount);
|
---|
85 | sprintf(buffer, "%d", (int) pal);
|
---|
86 | MessageBox(0, buffer, "M0", MB_OK);
|
---|
87 |
|
---|
88 | reply = CreateCompatibleBitmap (device, 501, 228);
|
---|
89 | sprintf(buffer, "%d", reply);
|
---|
90 | MessageBox(0, buffer, "M1", MB_OK);
|
---|
91 | if (reply != NULL)
|
---|
92 | { int r = SetDIBits(device, reply, 0, bitmapData->bmiHeader.biHeight, image, bitmapData, DIB_RGB_COLORS);
|
---|
93 | sprintf(buffer, "%d", r);
|
---|
94 | MessageBox(0, buffer, "M2", MB_OK);
|
---|
95 | DeleteObject(reply);
|
---|
96 | }
|
---|
97 |
|
---|
98 | reply = CreateDIBitmap( device, &bitmapData->bmiHeader, CBM_INIT, image, bitmapData, DIB_RGB_COLORS);
|
---|
99 | sprintf(buffer, "%d", reply);
|
---|
100 | MessageBox(0, buffer, "M", MB_OK);
|
---|
101 | GlobalFree((HLOCAL) bitmapData);
|
---|
102 | GlobalFree((HLOCAL) image);
|
---|
103 | _lclose(file);
|
---|
104 | return reply;
|
---|
105 | }
|
---|