1
1

dwg_thumbnail.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "dwg_thumbnail.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <regex>
  6. #include <string>
  7. #include <atlimage.h>
  8. using std::string;
  9. bool dwg_getbmp(const char *dwgfile, const char *thumb_bmpfile) {
  10. FILE *fd = fopen(dwgfile, "rb");
  11. if (fd == NULL) {
  12. // fprintf(stderr, "无法打开文件\n");
  13. return false;
  14. }
  15. fseek(fd, 13, SEEK_SET);
  16. unsigned int sentinel_pos;
  17. fread(&sentinel_pos, sizeof(unsigned int), 1, fd);
  18. // printf("Hex: %X\n", sentinel_pos);
  19. const int SIZE_SN = 39;
  20. char buf[SIZE_SN] = {0};
  21. unsigned char SN[] = {0x1F, 0x25, 0x6D, 0x07, 0xD4, 0x36, 0x28, 0x28,
  22. 0x9D, 0x57, 0xCA, 0x3F, 0x9D, 0x44, 0x10, 0x2B};
  23. fseek(fd, sentinel_pos, SEEK_SET);
  24. fread(buf, SIZE_SN, 1, fd);
  25. bool isdwg = true;
  26. for (int i = 0; i != 16; i++) {
  27. isdwg &= ((unsigned char)buf[i] == SN[i]);
  28. }
  29. // if (isdwg && buf[20] == 2) {
  30. // for (int i = 0; i != sizeof(buf); i++)
  31. // printf("%0.2X ", buf[i] & 0xFF);
  32. // } else {
  33. // return false;
  34. // }
  35. if (!isdwg && buf[20] != 2)
  36. return false;
  37. int data_size;
  38. memcpy(&data_size, &buf[35], sizeof(int));
  39. int pos;
  40. memcpy(&pos, &buf[31], sizeof(int));
  41. // printf("\ndata_size: 0x%0.4X", data_size);
  42. // printf("\npos: 0x%0.4X\n", pos);
  43. char *bmp_buf = new char[data_size];
  44. memset(bmp_buf, 0, data_size);
  45. fseek(fd, pos, SEEK_SET);
  46. if (fread(bmp_buf, 1, data_size, fd) != data_size) {
  47. // fprintf(stderr, "读取失败\n");
  48. delete[] bmp_buf;
  49. fclose(fd);
  50. return false;
  51. }
  52. FILE *bmp_file = fopen(thumb_bmpfile, "wb");
  53. if (bmp_file == NULL) {
  54. // fprintf(stderr, "无法创建文件\n");
  55. delete[] bmp_buf;
  56. fclose(fd);
  57. return false;
  58. }
  59. struct _BITMAP_HEADER {
  60. char magic[2];
  61. int file_size;
  62. int reserved;
  63. int offset;
  64. } bmp_h;
  65. /* Write bmp file header */
  66. bmp_h.magic[0] = 'B';
  67. bmp_h.magic[1] = 'M';
  68. bmp_h.file_size = 14 + data_size; // file header + DIB data
  69. bmp_h.reserved = 0;
  70. bmp_h.offset = 14 + 40 + 4 * 256; // file header + DIB header + color table
  71. // 写 BMP 文件头
  72. fwrite(&bmp_h.magic[0], sizeof(char), 2, bmp_file);
  73. fwrite(&bmp_h.file_size, 4, 3, bmp_file);
  74. // 写bmp 图片数据
  75. fwrite(bmp_buf, 1, data_size, bmp_file);
  76. // printf ("Success. Written thumbnail image to '%s'\n", thumb_bmpfile);
  77. delete[] bmp_buf;
  78. fclose(fd);
  79. fclose(bmp_file);
  80. return true;
  81. }
  82. bool DWGThumbnail(const char* dwg_filename, const char* png_filename)
  83. {
  84. string file_ext(dwg_filename);
  85. string rs = "(.+)(\\.(?:dwg|DWG|Dwg|DWg|dWG))";
  86. std::regex expression(rs);
  87. bool ret = regex_match(file_ext, expression);
  88. if (!ret) {
  89. return ret ;
  90. }
  91. FILE* pfile = fopen(dwg_filename, "rb");
  92. if(NULL == pfile){
  93. // MessageBoxA(NULL, cdr_filename, "错误File", MB_OK);
  94. return false;
  95. }
  96. char temp_filename[128];
  97. const char* tmpBmpFile = tmpnam(temp_filename);
  98. strcat(temp_filename,".bmp");
  99. ret = dwg_getbmp(dwg_filename , tmpBmpFile);
  100. if (!ret)
  101. return false ;
  102. CImage image; // bmp 转换 png ,需要CImage类,头文件 atlimage.h
  103. image.Load(tmpBmpFile);
  104. image.Save(png_filename);
  105. if (remove(tmpBmpFile) != 0)
  106. perror("Error deleting file");
  107. return ret;
  108. }
  109. bool DWGThumbnail_W(const wchar_t* dwg_filename, const wchar_t* png_filename)
  110. {
  111. char fromfile[MAX_PATH] = {0};
  112. char tofile[MAX_PATH] = {0};
  113. WCHARTochar(fromfile, dwg_filename);
  114. WCHARTochar(tofile, png_filename);
  115. bool ret = DWGThumbnail(fromfile, tofile);
  116. return ret;
  117. }