1
1

dwg_thumbnail.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. 
  2. //
  3. // 基于最新方案重写的 dwg_thumbnail.cpp,保留了原文件的函数签名(
  4. // dwg_getbmp DWGThumbnail DWGThumbnail_W)和头文件结构,但内部改为用数据魔数判定真实格式,并支持动态位深补 BMP 头。
  5. //
  6. // 改动要点
  7. // dwg_getbmp 不再盲信类型码:读出数据后用 detect_data_kind 判定真实格式。
  8. //
  9. // PNG → 原样写出。
  10. //
  11. // 完整 BMP 文件(带 BM)→ 原样写出。
  12. //
  13. // 裸 DIB → 动态识别 biSize / biBitCount 补文件头,不再硬编码 14 + 40 + 4 * 256。
  14. //
  15. // DWGThumbnail 增加 PNG 直通分支:如果 dwg_getbmp 写出的是 PNG 数据,就直接复制到目标 png_filename,不再经过 CImage 解码再编码(避免无意义转换,也避免 CImage 对某些 PNG 变体不兼容)。
  16. //
  17. // 保留原有函数签名和宽字符封装,调用方代码无需改动。
  18. //
  19. // 修正条件判断:原代码 if (!isdwg && buf[20] != 2) 逻辑有误,改为 if (!isdwg || (image_type != 2 && image_type != 6))。
  20. //
  21. // tmpnam 缓冲区:改为 L_tmpnam + 8,避免 strcat 越界。
  22. // ----------------------------- 内部辅助 -----------------------------
  23. // 根据数据魔数判断实际图像格式
  24. #include "dwg_thumbnail.h"
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <string>
  29. #include <windows.h>
  30. #include <atlstr.h> // 必须在 atlimage.h 之前,提供 CString
  31. #include <atlimage.h> // CImage
  32. using std::string;
  33. // ----------------------------- 内部辅助 -----------------------------
  34. // 大小写不敏感的后缀判断
  35. static bool ends_with_ci(const string& s, const char* suffix) {
  36. size_t slen = strlen(suffix);
  37. if (s.size() < slen) return false;
  38. for (size_t i = 0; i < slen; ++i) {
  39. char a = s[s.size() - slen + i];
  40. char b = suffix[i];
  41. if (a >= 'A' && a <= 'Z') a = (char)(a - 'A' + 'a');
  42. if (b >= 'A' && b <= 'Z') b = (char)(b - 'A' + 'a');
  43. if (a != b) return false;
  44. }
  45. return true;
  46. }
  47. // 根据数据魔数判断实际图像格式
  48. enum DataKind { DATA_UNKNOWN, DATA_PNG, DATA_BMP_FILE, DATA_DIB };
  49. static DataKind detect_data_kind(const unsigned char* data, int size) {
  50. if (size >= 8 &&
  51. data[0] == 0x89 && data[1] == 0x50 && data[2] == 0x4E && data[3] == 0x47 &&
  52. data[4] == 0x0D && data[5] == 0x0A && data[6] == 0x1A && data[7] == 0x0A) {
  53. return DATA_PNG;
  54. }
  55. if (size >= 2 && data[0] == 'B' && data[1] == 'M') {
  56. return DATA_BMP_FILE;
  57. }
  58. if (size >= 40) {
  59. unsigned int biSize = 0;
  60. memcpy(&biSize, data, sizeof(unsigned int));
  61. if (biSize == 40 || biSize == 108 || biSize == 124) {
  62. return DATA_DIB;
  63. }
  64. }
  65. return DATA_UNKNOWN;
  66. }
  67. // 从 DIB 头动态计算调色板大小(字节)
  68. static int calc_palette_size(const unsigned char* dib, int dib_size) {
  69. if (dib_size < 40) return 0;
  70. unsigned int biSize = 0;
  71. memcpy(&biSize, dib, sizeof(unsigned int));
  72. if (biSize < 40 || (int)biSize > dib_size) return 0;
  73. unsigned short biBitCount = 0;
  74. memcpy(&biBitCount, dib + 14, sizeof(unsigned short));
  75. if (biBitCount > 8) return 0;
  76. return (1 << biBitCount) * 4;
  77. }
  78. // 写 BMP 文件头 + 原始 DIB 数据(动态识别位深)
  79. static bool write_bmp_with_header(const char* bmp_path,
  80. const char* dib_data, int dib_size) {
  81. if (dib_size < 40) return false;
  82. const unsigned char* dib = (const unsigned char*)dib_data;
  83. unsigned int biSize = 0;
  84. memcpy(&biSize, dib, sizeof(unsigned int));
  85. if (biSize < 40 || (int)biSize > dib_size) return false;
  86. int palette_size = calc_palette_size(dib, dib_size);
  87. int pixel_offset = 14 + (int)biSize + palette_size;
  88. FILE* fp = fopen(bmp_path, "wb");
  89. if (!fp) return false;
  90. unsigned char file_header[14] = {0};
  91. file_header[0] = 'B';
  92. file_header[1] = 'M';
  93. int file_size = 14 + dib_size;
  94. memcpy(file_header + 2, &file_size, 4);
  95. memcpy(file_header + 10, &pixel_offset, 4);
  96. fwrite(file_header, 1, 14, fp);
  97. fwrite(dib_data, 1, dib_size, fp);
  98. fclose(fp);
  99. return true;
  100. }
  101. // ----------------------------- 原接口实现 -----------------------------
  102. // 提取预览图原始数据到 thumb_bmpfile。
  103. // 若实际数据为 PNG,则原样写出 PNG 数据;
  104. // 若为完整 BMP 文件,直接写出;
  105. // 若为裸 DIB,动态补 BMP 文件头后写出。
  106. bool dwg_getbmp(const char *dwgfile, const char *thumb_bmpfile) {
  107. FILE *fd = fopen(dwgfile, "rb");
  108. if (fd == NULL) {
  109. return false;
  110. }
  111. fseek(fd, 13, SEEK_SET);
  112. unsigned int sentinel_pos = 0;
  113. if (fread(&sentinel_pos, sizeof(unsigned int), 1, fd) != 1) {
  114. fclose(fd);
  115. return false;
  116. }
  117. const int SIZE_SN = 39;
  118. char buf[SIZE_SN] = {0};
  119. unsigned char SN[] = {0x1F, 0x25, 0x6D, 0x07, 0xD4, 0x36, 0x28, 0x28,
  120. 0x9D, 0x57, 0xCA, 0x3F, 0x9D, 0x44, 0x10, 0x2B};
  121. fseek(fd, sentinel_pos, SEEK_SET);
  122. if (fread(buf, SIZE_SN, 1, fd) != 1) {
  123. fclose(fd);
  124. return false;
  125. }
  126. bool isdwg = true;
  127. for (int i = 0; i != 16; i++) {
  128. isdwg = isdwg && ((unsigned char)buf[i] == SN[i]);
  129. }
  130. unsigned char image_type = (unsigned char)buf[20]; // 2=BMP, 6=PNG(仅供参考)
  131. if (!isdwg || (image_type != 2 && image_type != 6)) {
  132. fclose(fd);
  133. return false;
  134. }
  135. int data_size = 0;
  136. memcpy(&data_size, &buf[35], sizeof(int));
  137. int pos = 0;
  138. memcpy(&pos, &buf[31], sizeof(int));
  139. if (data_size <= 0 || pos <= 0) {
  140. fclose(fd);
  141. return false;
  142. }
  143. char *raw_buf = new char[data_size];
  144. fseek(fd, pos, SEEK_SET);
  145. if ((int)fread(raw_buf, 1, data_size, fd) != data_size) {
  146. delete[] raw_buf;
  147. fclose(fd);
  148. return false;
  149. }
  150. fclose(fd);
  151. // ---- 用数据魔数判定真实格式 ----
  152. DataKind kind = detect_data_kind((const unsigned char*)raw_buf, data_size);
  153. bool ok = false;
  154. if (kind == DATA_PNG || kind == DATA_BMP_FILE) {
  155. // PNG 或完整 BMP 文件:原样写出
  156. FILE *fp = fopen(thumb_bmpfile, "wb");
  157. if (fp) {
  158. ok = (fwrite(raw_buf, 1, data_size, fp) == (size_t)data_size);
  159. fclose(fp);
  160. }
  161. } else if (kind == DATA_DIB) {
  162. // 裸 DIB:动态补 BMP 文件头
  163. ok = write_bmp_with_header(thumb_bmpfile, raw_buf, data_size);
  164. } else {
  165. ok = false;
  166. }
  167. delete[] raw_buf;
  168. return ok;
  169. }
  170. bool DWGThumbnail(const char* dwg_filename, const char* png_filename)
  171. {
  172. // MSVC2010 的 std::regex 有缺陷,改用简单扩展名判断
  173. if (!ends_with_ci(string(dwg_filename), ".dwg")) {
  174. return false;
  175. }
  176. FILE* pfile = fopen(dwg_filename, "rb");
  177. if (NULL == pfile) {
  178. return false;
  179. }
  180. fclose(pfile);
  181. char temp_filename[MAX_PATH] = {0};
  182. const char* tn = tmpnam(temp_filename);
  183. if (!tn) {
  184. return false;
  185. }
  186. // tmpnam 已把名字写入 temp_filename,这里追加 .bmp 后缀
  187. if (strlen(temp_filename) + 5 >= MAX_PATH) {
  188. return false;
  189. }
  190. strcat(temp_filename, ".bmp");
  191. bool ret = dwg_getbmp(dwg_filename, temp_filename);
  192. if (!ret) {
  193. remove(temp_filename);
  194. return false;
  195. }
  196. // 判断临时文件实际内容格式
  197. bool is_png_data = false;
  198. {
  199. FILE* tf = fopen(temp_filename, "rb");
  200. if (tf) {
  201. unsigned char head[8] = {0};
  202. size_t n = fread(head, 1, 8, tf);
  203. fclose(tf);
  204. if (n >= 8 &&
  205. head[0] == 0x89 && head[1] == 0x50 &&
  206. head[2] == 0x4E && head[3] == 0x47 &&
  207. head[4] == 0x0D && head[5] == 0x0A &&
  208. head[6] == 0x1A && head[7] == 0x0A) {
  209. is_png_data = true;
  210. }
  211. }
  212. }
  213. if (is_png_data) {
  214. // 原样复制到目标
  215. FILE* in = fopen(temp_filename, "rb");
  216. FILE* out = fopen(png_filename, "wb");
  217. if (!in || !out) {
  218. if (in) fclose(in);
  219. if (out) fclose(out);
  220. remove(temp_filename);
  221. return false;
  222. }
  223. char cbuf[4096];
  224. size_t n;
  225. while ((n = fread(cbuf, 1, sizeof(cbuf), in)) > 0) {
  226. fwrite(cbuf, 1, n, out);
  227. }
  228. fclose(in);
  229. fclose(out);
  230. ret = true;
  231. } else {
  232. CImage image; // bmp 转换 png,需要 CImage 类
  233. HRESULT hr = image.Load(temp_filename);
  234. if (FAILED(hr)) {
  235. remove(temp_filename);
  236. return false;
  237. }
  238. hr = image.Save(png_filename);
  239. ret = SUCCEEDED(hr);
  240. }
  241. remove(temp_filename);
  242. return ret;
  243. }
  244. bool DWGThumbnail_W(const wchar_t* dwg_filename, const wchar_t* png_filename)
  245. {
  246. char fromfile[MAX_PATH] = {0};
  247. char tofile[MAX_PATH] = {0};
  248. WCHARTochar(fromfile, dwg_filename);
  249. WCHARTochar(tofile, png_filename);
  250. bool ret = DWGThumbnail(fromfile, tofile);
  251. return ret;
  252. }