// dwgbmp.cpp // 单文件命令行工具:从 DWG 中提取内嵌预览图(BMP 或 PNG) // // 用法: // dwgbmp.exe -> 默认输出 .png // dwgbmp.exe -> 按扩展名: .png 转 PNG, 其他写 BMP // // VC2010 编译(命令行): // cl /EHsc dwgbmp.cpp // 若链接报错找不到 CImage 相关符号,在项目属性->链接器->附加依赖项 加 atls.lib // // 注意:本文件请保存为“带 BOM 的 UTF-8”或“GBK”,避免中文注释在 VC2010 下乱码。 #include #include #include #include #include // 必须在 atlimage.h 之前,提供 CString #include // CImage using std::string; // ----------------------------- 辅助函数 ----------------------------- static bool ends_with_ci(const string& s, const string& suffix) { if (s.size() < suffix.size()) return false; for (size_t i = 0; i < suffix.size(); ++i) { char a = s[s.size() - suffix.size() + i]; char b = suffix[i]; if (a >= 'A' && a <= 'Z') a = a - 'A' + 'a'; if (b >= 'A' && b <= 'Z') b = b - 'A' + 'a'; if (a != b) return false; } return true; } enum OutFormat { OUT_BMP, OUT_PNG }; static OutFormat detect_out_format(const char* path) { string p(path); if (ends_with_ci(p, ".png")) return OUT_PNG; return OUT_BMP; } static string make_default_output(const char* dwg_path) { string p(dwg_path); size_t dot = p.find_last_of('.'); size_t sep = p.find_last_of("\\/"); if (dot != string::npos && (sep == string::npos || dot > sep)) { p = p.substr(0, dot); } p += ".png"; return p; } // 根据数据魔数判断实际图像格式 enum DataKind { DATA_UNKNOWN, DATA_PNG, DATA_BMP_FILE, DATA_DIB }; static DataKind detect_data_kind(const unsigned char* data, int size) { if (size >= 8 && data[0] == 0x89 && data[1] == 0x50 && data[2] == 0x4E && data[3] == 0x47 && data[4] == 0x0D && data[5] == 0x0A && data[6] == 0x1A && data[7] == 0x0A) { return DATA_PNG; } if (size >= 2 && data[0] == 'B' && data[1] == 'M') { return DATA_BMP_FILE; } if (size >= 40) { unsigned int biSize = 0; memcpy(&biSize, data, sizeof(unsigned int)); if (biSize == 40 || biSize == 108 || biSize == 124) { return DATA_DIB; } } return DATA_UNKNOWN; } static int calc_palette_size(const unsigned char* dib, int dib_size) { if (dib_size < 40) return 0; unsigned int biSize = 0; memcpy(&biSize, dib, sizeof(unsigned int)); if (biSize < 40 || (int)biSize > dib_size) return 0; unsigned short biBitCount = 0; memcpy(&biBitCount, dib + 14, sizeof(unsigned short)); if (biBitCount > 8) return 0; return (1 << biBitCount) * 4; } static bool write_bmp_with_header(const char* bmp_path, const char* dib_data, int dib_size) { if (dib_size < 40) { fprintf(stderr, "[错误] DIB 数据太小 (%d 字节)\n", dib_size); return false; } const unsigned char* dib = (const unsigned char*)dib_data; unsigned int biSize = 0; memcpy(&biSize, dib, sizeof(unsigned int)); int biWidth = 0, biHeight = 0; memcpy(&biWidth, dib + 4, sizeof(int)); memcpy(&biHeight, dib + 8, sizeof(int)); unsigned short biBitCount = 0; memcpy(&biBitCount, dib + 14, sizeof(unsigned short)); unsigned int biCompression = 0; memcpy(&biCompression, dib + 16, sizeof(unsigned int)); printf("[BMP] DIB 头: biSize=%u, %dx%d, 位深=%u, 压缩=%u\n", biSize, biWidth, biHeight, (unsigned)biBitCount, biCompression); if (biSize < 40 || (int)biSize > dib_size) { fprintf(stderr, "[错误] DIB 头大小异常: biSize=%u\n", biSize); return false; } int palette_size = calc_palette_size(dib, dib_size); int pixel_offset = 14 + (int)biSize + palette_size; printf("[BMP] 调色板大小=%d 字节, 像素数据偏移=%d\n", palette_size, pixel_offset); FILE* fp = fopen(bmp_path, "wb"); if (!fp) { fprintf(stderr, "[错误] 无法创建 BMP 文件: %s\n", bmp_path); return false; } unsigned char file_header[14] = {0}; file_header[0] = 'B'; file_header[1] = 'M'; int file_size = 14 + dib_size; memcpy(file_header + 2, &file_size, 4); memcpy(file_header + 10, &pixel_offset, 4); fwrite(file_header, 1, 14, fp); fwrite(dib_data, 1, dib_size, fp); fclose(fp); printf("[BMP] 写出完成: %d 字节\n", file_size); return true; } // ----------------------------- 核心解析 ----------------------------- static int extract_preview(const char* dwg_path, const char* tmp_bmp_path, const char* out_png_path, bool* out_is_png) { *out_is_png = false; printf("[1/6] 打开 DWG: %s\n", dwg_path); FILE* fd = fopen(dwg_path, "rb"); if (!fd) { fprintf(stderr, "[错误] 无法打开 DWG 文件: %s\n", dwg_path); return 1; } fseek(fd, 13, SEEK_SET); unsigned int sentinel_pos = 0; if (fread(&sentinel_pos, sizeof(unsigned int), 1, fd) != 1) { fprintf(stderr, "[错误] 读取预览指针失败\n"); fclose(fd); return 2; } printf("[2/6] 预览容器指针 sentinel_pos = 0x%08X (%u)\n", sentinel_pos, sentinel_pos); const int SIZE_SN = 39; char buf[SIZE_SN] = {0}; unsigned char SN[] = {0x1F, 0x25, 0x6D, 0x07, 0xD4, 0x36, 0x28, 0x28, 0x9D, 0x57, 0xCA, 0x3F, 0x9D, 0x44, 0x10, 0x2B}; fseek(fd, sentinel_pos, SEEK_SET); if (fread(buf, SIZE_SN, 1, fd) != 1) { fprintf(stderr, "[错误] 读取预览容器头失败\n"); fclose(fd); return 3; } bool isdwg = true; for (int i = 0; i != 16; i++) { isdwg = isdwg && ((unsigned char)buf[i] == SN[i]); } unsigned char image_type = (unsigned char)buf[20]; printf("[3/6] 哨兵校验: %s, 预览类型码 = %u (%s)\n", isdwg ? "OK" : "BAD", (unsigned)image_type, image_type == 2 ? "BMP" : (image_type == 6 ? "PNG" : "未知")); if (!isdwg || (image_type != 2 && image_type != 6)) { fprintf(stderr, "[错误] 不是有效的 DWG 预览容器,或类型不支持 " "(哨兵=%s, 类型=%u)\n", isdwg ? "OK" : "BAD", (unsigned)image_type); fclose(fd); return 4; } int data_size = 0; memcpy(&data_size, &buf[35], sizeof(int)); int pos = 0; memcpy(&pos, &buf[31], sizeof(int)); printf("[4/6] 预览数据: pos = %d, data_size = %d 字节\n", pos, data_size); if (data_size <= 0 || pos <= 0) { fprintf(stderr, "[错误] 非法的预览数据大小/偏移: size=%d pos=%d\n", data_size, pos); fclose(fd); return 5; } char* raw_buf = new char[data_size]; fseek(fd, pos, SEEK_SET); if ((int)fread(raw_buf, 1, data_size, fd) != data_size) { fprintf(stderr, "[错误] 读取预览数据失败\n"); delete[] raw_buf; fclose(fd); return 6; } fclose(fd); printf("[5/6] 读取预览数据成功 (%d 字节)\n", data_size); // ---- 关键:用数据魔数判定真实格式,不再盲信类型码 ---- DataKind kind = detect_data_kind((const unsigned char*)raw_buf, data_size); printf("[5.5/6] 数据魔数判定: %s\n", kind == DATA_PNG ? "PNG" : kind == DATA_BMP_FILE ? "完整 BMP 文件(带 BM 头)" : kind == DATA_DIB ? "裸 DIB(需补 BMP 头)" : "未知"); bool ok = false; if (kind == DATA_PNG) { printf("[6/6] 实际为 PNG,原样写出到: %s\n", out_png_path); FILE* fp = fopen(out_png_path, "wb"); if (fp) { ok = (fwrite(raw_buf, 1, data_size, fp) == (size_t)data_size); fclose(fp); } *out_is_png = true; } else if (kind == DATA_BMP_FILE) { printf("[6/6] 数据已带 BMP 文件头,直接写出到临时文件: %s\n", tmp_bmp_path); FILE* fp = fopen(tmp_bmp_path, "wb"); if (fp) { ok = (fwrite(raw_buf, 1, data_size, fp) == (size_t)data_size); fclose(fp); } *out_is_png = false; } else if (kind == DATA_DIB) { printf("[6/6] 裸 DIB,动态补 BMP 文件头写到: %s\n", tmp_bmp_path); ok = write_bmp_with_header(tmp_bmp_path, raw_buf, data_size); *out_is_png = false; } else { fprintf(stderr, "[错误] 无法识别预览数据格式。前 16 字节: "); for (int i = 0; i < 16 && i < data_size; ++i) { fprintf(stderr, "%02X ", (unsigned char)raw_buf[i]); } fprintf(stderr, "\n"); ok = false; } delete[] raw_buf; return ok ? 0 : 7; } // ----------------------------- 主流程 ----------------------------- static bool DWGThumbnail(const char* dwg_filename, const char* out_filename) { printf("==== DWG 预览图提取 ====\n"); printf("输入: %s\n", dwg_filename); printf("输出: %s\n", out_filename); string ext(dwg_filename); if (!ends_with_ci(ext, ".dwg")) { fprintf(stderr, "[错误] 输入文件不是 .dwg: %s\n", dwg_filename); return false; } OutFormat out_fmt = detect_out_format(out_filename); printf("目标格式: %s\n", out_fmt == OUT_PNG ? "PNG" : "BMP"); char tmp_bmp[L_tmpnam + 8] = {0}; if (!tmpnam(tmp_bmp)) { fprintf(stderr, "[错误] 无法生成临时文件名\n"); return false; } strcat(tmp_bmp, ".bmp"); bool is_png = false; int rc = extract_preview(dwg_filename, tmp_bmp, out_filename, &is_png); if (rc != 0) { remove(tmp_bmp); fprintf(stderr, "[失败] 提取预览图失败,错误码 %d\n", rc); return false; } bool ok = true; if (is_png) { if (out_fmt == OUT_BMP) { fprintf(stderr, "[警告] 实际预览为 PNG,目标扩展名为 .bmp," "文件内容仍是 PNG\n"); } ok = true; printf("[完成] PNG 预览已直接写出\n"); } else { if (out_fmt == OUT_PNG) { printf("[转换] BMP -> PNG,使用 CImage...\n"); CImage image; HRESULT hr = image.Load(tmp_bmp); if (FAILED(hr)) { fprintf(stderr, "[错误] CImage 加载 BMP 失败, hr=0x%08X\n", (unsigned)hr); ok = false; } else { hr = image.Save(out_filename); if (FAILED(hr)) { fprintf(stderr, "[错误] CImage 保存 PNG 失败, hr=0x%08X\n", (unsigned)hr); ok = false; } else { printf("[完成] BMP 已转换为 PNG\n"); } } } else { printf("[写出] 目标为 BMP,移动临时文件到目标路径...\n"); if (rename(tmp_bmp, out_filename) != 0) { FILE* in = fopen(tmp_bmp, "rb"); FILE* out = fopen(out_filename, "wb"); if (!in || !out) { if (in) fclose(in); if (out) fclose(out); ok = false; } else { char cbuf[4096]; size_t n; while ((n = fread(cbuf, 1, sizeof(cbuf), in)) > 0) { fwrite(cbuf, 1, n, out); } fclose(in); fclose(out); ok = true; } } if (ok) printf("[完成] BMP 已写出\n"); } } remove(tmp_bmp); return ok; } int main(int argc, char* argv[]) { if (argc != 2 && argc != 3) { fprintf(stderr, "用法:\n" " %s 默认输出 .png\n" " %s 按扩展名: .png 转 PNG, 其他写 BMP\n" " 从 DWG 文件中提取内嵌预览图(BMP 或 PNG)。\n", argv[0], argv[0]); return 1; } string out_path; if (argc == 3) { out_path = argv[2]; } else { out_path = make_default_output(argv[1]); } bool ok = DWGThumbnail(argv[1], out_path.c_str()); if (ok) { printf("\n[成功] 已输出: %s\n", out_path.c_str()); return 0; } fprintf(stderr, "\n[失败] 未能提取预览图\n"); return 2; }