念住向GalPhotoAuto加处理*.gal的功能,不过最后因为Gale.spi静系可以返回24位的图片,坑之。
下边代码可以系Visual Studio 2012直接编译。
一,源码
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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
#include "stdafx.h" #include <windows.h> //--------------------------------------------------------------------------- typedef struct PictureInfo { long left, top; // 起点坐标 long width; // 宽度 long height; // 高度 WORD x_density; // 水平方向密度 WORD y_density; // 垂直方向密度 short colorDepth; // 色深 HLOCAL hInfo; // 图像中的文本信息 } PictureInfo; //--------------------------------------------------------------------------- typedef int (__stdcall *SPI_GetPluginInfo)(int infono, LPSTR buf, int buflen); typedef int (__stdcall *SPI_IsSupported)(LPSTR filename, DWORD dw); typedef int (__stdcall *SPI_GetPictureInfo)(LPSTR buf, long len, unsigned int flag, struct PictureInfo *lpInfo); typedef int (CALLBACK *SPI_PROGRESS)(int, int, long); typedef int (__stdcall *SPI_GetPicture)(LPSTR buf, long len, unsigned int flag, HANDLE *pHBInfo, HANDLE *pHBm, SPI_PROGRESS lpPrgressCallback, long lData); typedef int (__stdcall *SPI_GetPreview)(LPSTR buf, long len, unsigned int flag, HANDLE *pHBInfo, HANDLE *pHBm, FARPROC lpPrgressCallback, long lData); typedef int (__stdcall *SPI_ConfigurationDlg)(HWND parent, int fnc); SPI_GetPluginInfo GetPluginInfo; SPI_IsSupported IsSupported; SPI_GetPictureInfo GetPictureInfo; SPI_GetPicture GetPicture; SPI_GetPreview GetPreview; SPI_ConfigurationDlg ConfigurationDlg; HMODULE handle; //--------------------------------------------------------------------------- bool spi_load(char *spipath) { bool ret = false; handle = LoadLibrary(spipath); if (NULL != handle) { GetPluginInfo = (SPI_GetPluginInfo)GetProcAddress(handle, "GetPluginInfo"); IsSupported = (SPI_IsSupported)GetProcAddress(handle, "IsSupported"); GetPictureInfo = (SPI_GetPictureInfo)GetProcAddress(handle, "GetPictureInfo"); GetPicture = (SPI_GetPicture)GetProcAddress(handle, "GetPicture"); GetPreview = (SPI_GetPreview)GetProcAddress(handle, "GetPreview"); ConfigurationDlg = (SPI_ConfigurationDlg)GetProcAddress(handle, "ConfigurationDlg"); ret = (NULL != IsSupported) & (NULL != GetPictureInfo) & (NULL != GetPicture); } return ret; } //--------------------------------------------------------------------------- void spi_info(void) { const int SPIINFO_SIZE = 256; char spiinfo[SPIINFO_SIZE]; int ret; char *mes[] = {"Plug-in API 版本: ", "Plug-in名字、版本和版权:", "文件后缀名:", "文件格式名称:"}; for (int i=0; i < 4; i++) { printf(mes[i]); ret = (*GetPluginInfo)(i, spiinfo, SPIINFO_SIZE); if (0 != ret) { printf(spiinfo); printf("\n"); } else { printf("NULL\n"); return; } } } //--------------------------------------------------------------------------- bool spi_check(char *filepath) { FILE *fp; fopen_s(&fp, filepath, "rb"); if (NULL == fp) { fprintf(stderr, "文件不能打开。\n"); return false; } const int HEADBUF_SIZE = 2000; char h_buf[HEADBUF_SIZE]; memset(h_buf, 0, HEADBUF_SIZE); fread(h_buf, 1, HEADBUF_SIZE, fp); fclose(fp); char *pdata = h_buf; int ret = (*IsSupported)(filepath, (DWORD)pdata); if (0 == ret) { fprintf(stderr, "不支持的格式。\n"); return false; } return true; } //--------------------------------------------------------------------------- bool spi_picinfo(char *filepath) { PictureInfo picinfo; memset(&picinfo, 0, sizeof(picinfo)); int ret = (*GetPictureInfo)(filepath, 0, 0, &picinfo); if (0 != ret) { fprintf(stderr, "GetPictureInfo 错误: %d\n", ret); return false; } printf("left : %d\n", picinfo.left); printf("top : %d\n", picinfo.top); printf("width : %d\n", picinfo.width); printf("height : %d\n", picinfo.height); printf("x_density : %d\n", picinfo.x_density); printf("y_density : %d\n", picinfo.y_density); printf("colorDepth : %d\n", picinfo.colorDepth); printf("hInfo : "); if (NULL != picinfo.hInfo) { char *pinfo = (char *)GlobalLock(picinfo.hInfo); printf(pinfo); GlobalUnlock(picinfo.hInfo); GlobalFree(picinfo.hInfo); } else { printf("NULL\n"); } printf("\n"); return true; } //--------------------------------------------------------------------------- bool spi_pic(char *filepath) { HANDLE bmpinfo, bmpdata; int ret = (*GetPicture)(filepath, 0, 0, &bmpinfo, &bmpdata, NULL, 0); if (0 != ret) { fprintf(stderr, "GetPicture 错误:%d\n", ret); return false; } FILE *fp; fopen_s(&fp, "bmpinfo.bin", "wb"); char *pdata = (char *)LocalLock(bmpinfo); UINT size = LocalSize(bmpinfo); fwrite(pdata, 1, size, fp); LocalUnlock(bmpinfo); fclose(fp); fopen_s(&fp, "bmpdata.bin", "wb"); pdata = (char *)LocalLock(bmpdata); size = LocalSize(bmpdata); fwrite(pdata, 1, size, fp); LocalUnlock(bmpdata); fclose(fp); LocalFree(bmpinfo); LocalFree(bmpdata); return true; } //--------------------------------------------------------------------------- bool spi_Prepic(char *filepath) { HANDLE bmpinfo, bmpdata; int ret = (*GetPreview)(filepath, 0, 0, &bmpinfo, &bmpdata, NULL, 0); if (0 != ret) { fprintf(stderr, "GetPreview 错误:%d\n", ret); return false; } FILE *fp; fopen_s(&fp, "Prebmpinfo.bin", "wb"); char *pdata = (char *)LocalLock(bmpinfo); UINT size = LocalSize(bmpinfo); fwrite(pdata, 1, size, fp); LocalUnlock(bmpinfo); fclose(fp); fopen_s(&fp, "Prebmpdata.bin", "wb"); pdata = (char *)LocalLock(bmpdata); size = LocalSize(bmpdata); fwrite(pdata, 1, size, fp); LocalUnlock(bmpdata); fclose(fp); LocalFree(bmpinfo); LocalFree(bmpdata); return true; } //--------------------------------------------------------------------------- bool spi_dlg(int fnc) { int ret = (*ConfigurationDlg)(GetConsoleWindow(), fnc); if (0 != ret) { fprintf(stderr, "ConfigurationDlg 错误:%d\n", ret); return false; } return true; } int _tmain(int argc, _TCHAR* argv[]) { if (3 > argc) { return 0; } char *spiname = argv[1]; if (!spi_load(spiname)) { fprintf(stderr, "spi 文件载入失败。\n"); goto err; } char *filename = argv[2]; // 插件信息 spi_info(); // 检查SPI是否支持文件 if (!spi_check(filename)) goto err; // 取得图片文件的信息 if (!spi_picinfo(filename)) goto err; // 取得经过SPI处理过的BMP图片数据 if (!spi_pic(filename)) goto err; // 取得经过SPI处理过的BMP图片数据的预览图,一般都不会有人特别写结构的,多数直接挂接GetPicture if (!spi_Prepic(filename)) goto err; // 0 为显示插件的关于窗口,1 为显示设置窗口,2 为以后保留 if (!spi_dlg(0)) goto err; err: FreeLibrary(handle); system("pause"); return 0; } |
二,BMP数据合成
例如,spi_pic 取得的 bmpdata 只是数据部分,bmpinfo 是再前一D的信息,仲差最开头的14位数据。
完整的BMP是:开头的14位数据 + bmpinfo + bmpdata
开头的14位数据:
42 4D E6 E5 00 00 00 00 00 00 36 00 00 00
其中
E6 E5 00 00 = B0 E5 00 00 + 36
原bmpinfo:
28 00 00 00 8C 00 00 00 8C 00 00 00 01 00 18 00 00 00 00 00 B0 E5 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
修改为:
28 00 00 00 8C 00 00 00 8C 00 00 00 01 00 18 00 00 00 00 00 B0 E5 00 00 12 0B 00 00 12 0B 00 00 00 00 00 00 00 00 00 00