|
| 1 | +/* |
| 2 | + * CLCL |
| 3 | + * |
| 4 | + * Bitmap.c |
| 5 | + * |
| 6 | + * Copyright (C) 1996-2019 by Ohno Tomoaki. All rights reserved. |
| 7 | + * https://www.nakka.com/ |
| 8 | + |
| 9 | + */ |
| 10 | + |
| 11 | +/* Include Files */ |
| 12 | +#define _INC_OLE |
| 13 | +#include <windows.h> |
| 14 | +#undef _INC_OLE |
| 15 | + |
| 16 | +#include "General.h" |
| 17 | +#include "Memory.h" |
| 18 | + |
| 19 | +/* Define */ |
| 20 | + |
| 21 | +/* Global Variables */ |
| 22 | + |
| 23 | +/* Local Function Prototypes */ |
| 24 | + |
| 25 | +/* |
| 26 | + * bitmap_to_dib - ビットマップをDIBに変換 |
| 27 | + */ |
| 28 | +BYTE *bitmap_to_dib(const HBITMAP hbmp, DWORD *size) |
| 29 | +{ |
| 30 | + HDC hdc; |
| 31 | + BITMAP bmp; |
| 32 | + BYTE *ret; // BITMAPINFO |
| 33 | + DWORD biComp = BI_RGB; |
| 34 | + DWORD len; |
| 35 | + DWORD hsize; |
| 36 | + DWORD err; |
| 37 | + int color_bit = 0; |
| 38 | + |
| 39 | + // BITMAP情報取得 |
| 40 | + if (GetObject(hbmp, sizeof(BITMAP), &bmp) == 0) { |
| 41 | + return NULL; |
| 42 | + } |
| 43 | + switch (bmp.bmBitsPixel) { |
| 44 | + case 1: |
| 45 | + color_bit = 2; |
| 46 | + break; |
| 47 | + case 4: |
| 48 | + color_bit = 16; |
| 49 | + break; |
| 50 | + case 8: |
| 51 | + color_bit = 256; |
| 52 | + break; |
| 53 | + case 16: |
| 54 | + case 32: |
| 55 | + color_bit = 3; |
| 56 | + biComp = BI_BITFIELDS; |
| 57 | + break; |
| 58 | + } |
| 59 | + len = (((bmp.bmWidth * bmp.bmBitsPixel) / 8) % 4) |
| 60 | + ? ((((bmp.bmWidth * bmp.bmBitsPixel) / 8) / 4) + 1) * 4 |
| 61 | + : (bmp.bmWidth * bmp.bmBitsPixel) / 8; |
| 62 | + |
| 63 | + if ((hdc = GetDC(NULL)) == NULL) { |
| 64 | + return NULL; |
| 65 | + } |
| 66 | + |
| 67 | + hsize = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * color_bit; |
| 68 | + if ((ret = mem_calloc(hsize + len * bmp.bmHeight)) == NULL) { |
| 69 | + err = GetLastError(); |
| 70 | + ReleaseDC(NULL, hdc); |
| 71 | + SetLastError(err); |
| 72 | + return NULL; |
| 73 | + } |
| 74 | + // DIBヘッダ |
| 75 | + ((BITMAPINFO *)ret)->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); |
| 76 | + ((BITMAPINFO *)ret)->bmiHeader.biWidth = bmp.bmWidth; |
| 77 | + ((BITMAPINFO *)ret)->bmiHeader.biHeight = bmp.bmHeight; |
| 78 | + ((BITMAPINFO *)ret)->bmiHeader.biPlanes = 1; |
| 79 | + ((BITMAPINFO *)ret)->bmiHeader.biBitCount = bmp.bmBitsPixel; |
| 80 | + ((BITMAPINFO *)ret)->bmiHeader.biCompression = biComp; |
| 81 | + ((BITMAPINFO *)ret)->bmiHeader.biSizeImage = len * bmp.bmHeight; |
| 82 | + ((BITMAPINFO *)ret)->bmiHeader.biClrImportant = 0; |
| 83 | + |
| 84 | + // DIB取得 |
| 85 | + if (GetDIBits(hdc, hbmp, 0, bmp.bmHeight, ret + hsize, (BITMAPINFO *)ret, DIB_RGB_COLORS) == 0) { |
| 86 | + err = GetLastError(); |
| 87 | + ReleaseDC(NULL, hdc); |
| 88 | + mem_free(&ret); |
| 89 | + SetLastError(err); |
| 90 | + return NULL; |
| 91 | + } |
| 92 | + ReleaseDC(NULL, hdc); |
| 93 | + *size = hsize + len * bmp.bmHeight; |
| 94 | + return ret; |
| 95 | +} |
| 96 | + |
| 97 | +/* |
| 98 | + * dib_to_bitmap - DIBをビットマップに変換 |
| 99 | + */ |
| 100 | +HBITMAP dib_to_bitmap(const BYTE *dib) |
| 101 | +{ |
| 102 | + HDC hdc; |
| 103 | + HBITMAP ret; |
| 104 | + DWORD hsize; |
| 105 | + DWORD err; |
| 106 | + int color_bit; |
| 107 | + |
| 108 | + if ((hdc = GetDC(NULL)) == NULL) { |
| 109 | + return NULL; |
| 110 | + } |
| 111 | + |
| 112 | + // ヘッダサイズ取得 |
| 113 | + if ((color_bit = ((BITMAPINFOHEADER *)dib)->biClrUsed) == 0) { |
| 114 | + color_bit = ((BITMAPINFOHEADER *)dib)->biPlanes * ((BITMAPINFOHEADER *)dib)->biBitCount; |
| 115 | + if (color_bit == 1) { |
| 116 | + color_bit = 2; |
| 117 | + } else if (color_bit <= 4) { |
| 118 | + color_bit = 16; |
| 119 | + } else if (color_bit <= 8) { |
| 120 | + color_bit = 256; |
| 121 | + } else if (color_bit <= 16) { |
| 122 | + color_bit = 3; |
| 123 | + } else if (color_bit <= 24) { |
| 124 | + color_bit = 0; |
| 125 | + } else { |
| 126 | + color_bit = 3; |
| 127 | + } |
| 128 | + } |
| 129 | + hsize = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * color_bit; |
| 130 | + |
| 131 | + // ビットマップに変換 |
| 132 | + ret = CreateDIBitmap(hdc, (BITMAPINFOHEADER *)dib, CBM_INIT, dib + hsize, (BITMAPINFO *)dib, DIB_RGB_COLORS); |
| 133 | + if (ret == NULL) { |
| 134 | + err = GetLastError(); |
| 135 | + ReleaseDC(NULL, hdc); |
| 136 | + SetLastError(err); |
| 137 | + return NULL; |
| 138 | + } |
| 139 | + ReleaseDC(NULL, hdc); |
| 140 | + return ret; |
| 141 | +} |
| 142 | +/* End of source */ |
0 commit comments