dwgthumbnail.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <windows.h>
  5. bool thumbnail(const char *filePath, const char *savePath) {
  6. FILE *fd = fopen(filePath, "rb");
  7. if (fd == NULL) {
  8. fprintf(stderr, "无法打开文件\n");
  9. return false;
  10. }
  11. fseek(fd, 13, SEEK_SET);
  12. unsigned int sentinel_pos;
  13. fread(&sentinel_pos, sizeof(unsigned int), 1, fd);
  14. printf("Hex: %X\n", sentinel_pos);
  15. const int SIZE_SN = 39;
  16. char buf[SIZE_SN] = {0};
  17. unsigned char SN[] = {0x1F, 0x25, 0x6D, 0x07, 0xD4, 0x36, 0x28, 0x28,
  18. 0x9D, 0x57, 0xCA, 0x3F, 0x9D, 0x44, 0x10, 0x2B};
  19. fseek(fd, sentinel_pos, SEEK_SET);
  20. fread(buf, SIZE_SN, 1, fd);
  21. bool isdwg = true;
  22. for (int i = 0; i != 16; i++) {
  23. isdwg &= ((unsigned char)buf[i] == SN[i]);
  24. }
  25. if (isdwg && buf[20] == 2) {
  26. for (int i = 0; i != sizeof(buf); i++)
  27. printf("%0.2X ", buf[i] & 0xFF);
  28. } else {
  29. return false;
  30. }
  31. int data_size;
  32. memcpy(&data_size, &buf[35], sizeof(int));
  33. int pos;
  34. memcpy(&pos, &buf[31], sizeof(int));
  35. printf("\ndata_size: 0x%0.4X", data_size);
  36. printf("\npos: 0x%0.4X\n", pos);
  37. char *bmp_buf = new char[data_size];
  38. memset(bmp_buf, 0, data_size);
  39. fseek(fd, pos, SEEK_SET);
  40. if (fread(bmp_buf, 1, data_size, fd) != data_size) {
  41. fprintf(stderr, "读取失败\n");
  42. delete[] bmp_buf;
  43. fclose(fd);
  44. return false;
  45. }
  46. FILE *bmp_file = fopen(savePath, "wb");
  47. if (bmp_file == NULL) {
  48. fprintf(stderr, "无法创建文件\n");
  49. delete[] bmp_buf;
  50. fclose(fd);
  51. return false;
  52. }
  53. struct _BITMAP_HEADER {
  54. char magic[2];
  55. int file_size;
  56. int reserved;
  57. int offset;
  58. } bmp_h;
  59. /* Write bmp file header */
  60. bmp_h.magic[0] = 'B';
  61. bmp_h.magic[1] = 'M';
  62. bmp_h.file_size = 14 + data_size; // file header + DIB data
  63. bmp_h.reserved = 0;
  64. bmp_h.offset = 14 + 40 + 4 * 256; // file header + DIB header + color table
  65. // 写 BMP 文件头
  66. fwrite(&bmp_h.magic[0], sizeof(char), 2, bmp_file);
  67. fwrite(&bmp_h.file_size, 4, 3, bmp_file);
  68. // 写bmp 图片数据
  69. fwrite(bmp_buf, 1, data_size, bmp_file);
  70. printf("\n ---------> thumbnail.bmp");
  71. delete[] bmp_buf;
  72. fclose(fd);
  73. fclose(bmp_file);
  74. return true;
  75. }
  76. int main(int argc, char *argv[]) {
  77. const char *filePath = "example.dwg";
  78. const char *savePath = "thumbnail.bmp";
  79. if (2 == argc)
  80. filePath = argv[1];
  81. thumbnail(filePath, savePath);
  82. return 0;
  83. }
  84. /*
  85. # 参考代码
  86. # https://github.com/LibreDWG/libredwg/blob/master/programs/dwgbmp.c
  87. # DWG文件的预览图像数据结构
  88. # https://blog.csdn.net/lzljy/article/details/103110823
  89. */