3 * @brief Windows用ビットマップファイル読み込み処理パッケージ /
4 * This package provides a routine to read a DIB file and set up the device dependent version of the image.
7 * This file has been modified for use with "Angband 2.8.2"
11 * (C) Copyright Microsoft Corp. 1993. All rights reserved.
13 * You have a royalty-free right to use, modify, reproduce and
14 * distribute the Sample Files (and/or any modified version) in
15 * any way you find useful, provided that you agree that
16 * Microsoft has no warranty obligations or liability for any
17 * Sample Application Files which are modified.
19 * mind.cとあるが実際には超能力者、練気術師、狂戦士、鏡使い、忍者までの
29 * Extract the "WIN32" flag from the compiler
31 #if defined(__WIN32__) || defined(__WINNT__) || defined(__NT__)
38 * Make sure "huge" is legal
47 * Needed for lcc-win32
55 * 一度にファイルから読み込むデータ量 / Number of bytes to be read during each read operation
60 * @brief 32KBのデータ読み取りを繰り返すことで、64KB以上のデータを一度に読み取るサブルーチン
61 * Private routine to read more than 64K at a time Reads data in steps of 32k till all the data has been read.
66 * 取得できたデータ量をバイトで返す。0ならば何らかのエラー。
67 * Returns number of bytes requested, or zero if something went wrong.
69 static DWORD PASCAL lread(int fh, VOID FAR *pv, DWORD ul)
74 while (ul > (DWORD)MAXREAD)
76 if (_lread(fh, (LPSTR)hp, (WORD)MAXREAD) != MAXREAD)
81 if (_lread(fh, (LPSTR)hp, (WORD)ul) != ul)
87 * @brief BITMAPINFOHEADERを取得してカラーテーブルを基本としたパレットを作成する。
88 * Given a BITMAPINFOHEADER, create a palette based on the color table.
89 * @param lpInfo BITMAPINFOHEADERのポインタ
91 * パレットの参照を返す。NULLならばエラー。
92 * Returns the handle of a palette, or zero if something went wrong.
94 static HPALETTE PASCAL NEAR MakeDIBPalette(LPBITMAPINFOHEADER lpInfo)
102 * since biClrUsed field was filled during the loading of the DIB,
103 * we know it contains the number of colors in the color table.
105 if (lpInfo->biClrUsed)
107 npPal = (PLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) +
108 (WORD)lpInfo->biClrUsed * sizeof(PALETTEENTRY));
112 npPal->palVersion = 0x300;
113 npPal->palNumEntries = (WORD)lpInfo->biClrUsed;
115 /* get pointer to the color table */
116 lpRGB = (RGBQUAD FAR *)((LPSTR)lpInfo + lpInfo->biSize);
118 /* copy colors from the color table to the LogPalette structure */
119 for (i = 0; i < (WORD)lpInfo->biClrUsed; i++, lpRGB++)
121 npPal->palPalEntry[i].peRed = lpRGB->rgbRed;
122 npPal->palPalEntry[i].peGreen = lpRGB->rgbGreen;
123 npPal->palPalEntry[i].peBlue = lpRGB->rgbBlue;
124 npPal->palPalEntry[i].peFlags = PC_NOCOLLAPSE;
127 hLogPal = CreatePalette((LPLOGPALETTE)npPal);
128 LocalFree((HANDLE)npPal);
133 * 24-bit DIB with no color table. return default palette. Another
134 * option would be to create a 256 color "rainbow" palette to provide
135 * some good color choices.
139 return(GetStockObject(DEFAULT_PALETTE));
146 * ビットマップファイルを受け取り、画像のデバイス依存の描画のために使われる共通パレットとビットマップを作成する
147 * Given a DIB, create a bitmap and corresponding palette to be used for a
148 * device-dependent representation of the image.
149 * @param hDC デバイスコンテキストハンドル
150 * @param hDIB ビットマップ画像ハンドル
151 * @param phPal パレット取得ハンドル
152 * @param phBitmap ビットマップ取得ハンドル
154 * 成功したならばTRUEを返す。失敗の場合FALSE。
155 * Returns TRUE on success (phPal and phBitmap are filled with appropriate
156 * handles. Caller is responsible for freeing objects) and FALSE on failure
157 * (unable to create objects, both pointer are invalid).
159 static BOOL NEAR PASCAL MakeBitmapAndPalette(HDC hDC, HANDLE hDIB,
160 HPALETTE * phPal, HBITMAP * phBitmap)
162 LPBITMAPINFOHEADER lpInfo;
165 HPALETTE hPalette, hOldPal;
168 lpInfo = (LPBITMAPINFOHEADER) GlobalLock(hDIB);
169 if ((hPalette = MakeDIBPalette(lpInfo)) != 0)
171 /* Need to realize palette for converting DIB to bitmap. */
172 hOldPal = SelectPalette(hDC, hPalette, TRUE);
175 lpBits = ((LPSTR)lpInfo + (WORD)lpInfo->biSize +
176 (WORD)lpInfo->biClrUsed * sizeof(RGBQUAD));
177 hBitmap = CreateDIBitmap(hDC, lpInfo, CBM_INIT, lpBits,
178 (LPBITMAPINFO)lpInfo, DIB_RGB_COLORS);
180 SelectPalette(hDC, hOldPal, TRUE);
185 DeleteObject(hPalette);
201 * ビットマップファイルを読み込み、BITMAPINFO構造体にハンドルを取得する。
202 * Reads a DIB from a file, obtains a handle to its BITMAPINFO struct, and
203 * loads the DIB. Once the DIB is loaded, the function also creates a bitmap
204 * and palette out of the DIB for a device-dependent form.
205 * device-dependent representation of the image.
206 * @param hWnd ウィンドウハンドル
207 * @param lpFileName 読み込むビットマップファイル
208 * @param pInfo 取得情報を補完するビットマップ情報構造体ポインタ
210 * Returns TRUE if the DIB is loaded and the bitmap/palette created, in which
211 * case, the DIBINIT structure pointed to by pInfo is filled with the appropriate
212 * handles, and FALSE if something went wrong.
214 * Reads a DIB from a file, obtains a handle to its BITMAPINFO struct, and
215 * loads the DIB. Once the DIB is loaded, the function also creates a bitmap
216 * and palette out of the DIB for a device-dependent form.
218 BOOL ReadDIB(HWND hWnd, LPSTR lpFileName, DIBINIT *pInfo)
221 LPBITMAPINFOHEADER lpbi;
229 BOOL bCoreHead = FALSE;
231 /* Open the file and get a handle to it's BITMAPINFO */
232 fh = OpenFile(lpFileName, &of, OF_READ);
235 wsprintf(str, "Can't open file '%s'", (LPSTR)lpFileName);
236 MessageBox(NULL, str, "Error", MB_ICONSTOP | MB_OK);
240 pInfo->hDIB = GlobalAlloc(GHND, (DWORD)(sizeof(BITMAPINFOHEADER) +
241 256 * sizeof(RGBQUAD)));
246 lpbi = (LPBITMAPINFOHEADER)GlobalLock(pInfo->hDIB);
248 /* read the BITMAPFILEHEADER */
249 if (sizeof (bf) != _lread(fh, (LPSTR)&bf, sizeof(bf)))
253 if (bf.bfType != 0x4d42)
256 if (sizeof(BITMAPCOREHEADER) != _lread(fh, (LPSTR)lpbi, sizeof(BITMAPCOREHEADER)))
259 if (lpbi->biSize == sizeof(BITMAPCOREHEADER))
261 lpbi->biSize = sizeof(BITMAPINFOHEADER);
262 lpbi->biBitCount = ((LPBITMAPCOREHEADER)lpbi)->bcBitCount;
263 lpbi->biPlanes = ((LPBITMAPCOREHEADER)lpbi)->bcPlanes;
264 lpbi->biHeight = ((LPBITMAPCOREHEADER)lpbi)->bcHeight;
265 lpbi->biWidth = ((LPBITMAPCOREHEADER)lpbi)->bcWidth;
270 /* get to the start of the header and read INFOHEADER */
271 _llseek(fh, sizeof(BITMAPFILEHEADER), SEEK_SET);
272 if (sizeof(BITMAPINFOHEADER) != _lread(fh, (LPSTR)lpbi, sizeof(BITMAPINFOHEADER)))
276 nNumColors = (WORD)lpbi->biClrUsed;
279 /* no color table for 24-bit, default size otherwise */
280 if (lpbi->biBitCount != 24)
281 nNumColors = 1 << lpbi->biBitCount;
284 /* fill in some default values if they are zero */
285 if (lpbi->biClrUsed == 0)
286 lpbi->biClrUsed = nNumColors;
288 if (lpbi->biSizeImage == 0)
290 lpbi->biSizeImage = (((((lpbi->biWidth * (DWORD)lpbi->biBitCount) + 31) & ~31) >> 3)
294 /* otherwise wouldn't work with 16 color bitmaps -- S.K. */
295 else if ((nNumColors == 16) && (lpbi->biSizeImage > bf.bfSize))
297 lpbi->biSizeImage /= 2;
300 /* get a proper-sized buffer for header, color table and bits */
301 GlobalUnlock(pInfo->hDIB);
302 pInfo->hDIB = GlobalReAlloc(pInfo->hDIB, lpbi->biSize +
303 nNumColors * sizeof(RGBQUAD) +
304 lpbi->biSizeImage, 0);
306 /* can't resize buffer for loading */
310 lpbi = (LPBITMAPINFOHEADER)GlobalLock(pInfo->hDIB);
312 /* read the color table */
315 _lread(fh, (LPSTR)(lpbi) + lpbi->biSize, nNumColors * sizeof(RGBQUAD));
321 RGBTRIPLE FAR *pTriple;
323 _lread(fh, (LPSTR)(lpbi) + lpbi->biSize, nNumColors * sizeof(RGBTRIPLE));
325 pQuad = (RGBQUAD FAR *)((LPSTR)lpbi + lpbi->biSize);
326 pTriple = (RGBTRIPLE FAR *) pQuad;
327 for (i = nNumColors - 1; i >= 0; i--)
329 pQuad[i].rgbRed = pTriple[i].rgbtRed;
330 pQuad[i].rgbBlue = pTriple[i].rgbtBlue;
331 pQuad[i].rgbGreen = pTriple[i].rgbtGreen;
332 pQuad[i].rgbReserved = 0;
336 /* offset to the bits from start of DIB header */
337 offBits = (WORD)lpbi->biSize + nNumColors * sizeof(RGBQUAD);
339 if (bf.bfOffBits != 0L)
341 _llseek(fh,bf.bfOffBits,SEEK_SET);
344 /* Use local version of '_lread()' above */
345 if (lpbi->biSizeImage == lread(fh, (LPSTR)lpbi + offBits, lpbi->biSizeImage))
347 GlobalUnlock(pInfo->hDIB);
350 if (!MakeBitmapAndPalette(hDC, pInfo->hDIB, &((HPALETTE)pInfo->hPalette),
351 &((HBITMAP)pInfo->hBitmap)))
365 GlobalUnlock(pInfo->hDIB);
367 GlobalFree(pInfo->hDIB);