1
0

dwgthumbnail.cpp 2.8 KB

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