fork download
  1. #include <fstream>
  2. #include <memory>
  3. #include <string>
  4.  
  5. // Loads image from file
  6. int rtf_load_image(char* image, int width, int height)
  7. {
  8. // Set error flag
  9. int error = RTF_SUCCESS;
  10.  
  11. // Check image type
  12. bool err = false;
  13. if ( strstr(image,".bmp") == NULL )
  14. {
  15. if ( strstr(image,".jpg") == NULL )
  16. {
  17. if (strstr(image,".gif") == NULL )
  18. err = true;
  19. }
  20. }
  21.  
  22. // If valid image type
  23. if ( err == false )
  24. {
  25. // Free IPicture object
  26. if ( rtfPicture != NULL )
  27. {
  28. rtfPicture->Release();
  29. rtfPicture = NULL;
  30. }
  31.  
  32. ::std::ifstream ifs( image, ::std::ios::binary );
  33. ifs.seekg( 0, ::std::ios::end );
  34. ::std::size_t sz( ifs.tellg() );
  35. ifs.seekg( 0, ::std::ios::beg );
  36. ::std::unique_ptr<char[]> buf( new char[sz] );
  37. ifs.read( &buf[0], sz );
  38. ifs.close();
  39. /*
  40. ::std::ofstream ofs( ::std::string( image ) + "t", ::std::ios::binary );
  41. ofs.write( &buf[0], sz );
  42. ofs.close();
  43. */
  44.  
  45. // Read image file
  46. //int imageFile = _open( image, _O_RDONLY | _O_BINARY );
  47. //struct _stat st;
  48. //_fstat( imageFile, &st );
  49. //DWORD nSize = st.st_size;
  50. //BYTE* pBuff = new BYTE[nSize];
  51. //_read( imageFile, pBuff, nSize );
  52. // Alocate memory for image data
  53. HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, sz);
  54. void* pData = GlobalLock(hGlobal);
  55. memcpy(pData, &buf[0], sz);
  56. GlobalUnlock(hGlobal);
  57. // Load image using OLE
  58. IStream* pStream = NULL;
  59. if ( CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) == S_OK )
  60. {
  61. HRESULT hr;
  62. if ((hr = OleLoadPicture( pStream, sz, FALSE, IID_IPicture, (LPVOID *)&rtfPicture)) != S_OK)
  63. error = RTF_IMAGE_ERROR;
  64.  
  65. pStream->Release();
  66. }
  67. //delete []pBuff;
  68. //_close(imageFile);
  69.  
  70. // If image is loaded
  71. if ( rtfPicture != NULL )
  72. {
  73. class DC {
  74. public:
  75. operator HDC() const {
  76. return this->_dc;
  77. }
  78. ~DC() {
  79. ::ReleaseDC( ::GetDesktopWindow(), this->_dc );
  80. }
  81. DC()
  82. : _dc( ::GetDC( ::GetDesktopWindow() ) )
  83. {}
  84. protected:
  85. HDC _dc;
  86. };
  87.  
  88. DC dc;
  89. // Calculate image size
  90. long hmWidth;
  91. long hmHeight;
  92. rtfPicture->get_Width(&hmWidth);
  93. rtfPicture->get_Height(&hmHeight);
  94. int nWidth = MulDiv( hmWidth, GetDeviceCaps( dc,LOGPIXELSX), 2540 );
  95. int nHeight = MulDiv( hmHeight, GetDeviceCaps( dc,LOGPIXELSY), 2540 );
  96.  
  97. // Create metafile;
  98. HDC hdcMeta = CreateMetaFile(NULL);
  99.  
  100. // Render picture to metafile
  101. rtfPicture->Render( hdcMeta, 0, 0, nWidth, nHeight, 0, hmHeight, hmWidth, -hmHeight, NULL );
  102.  
  103. // Close metafile
  104. HMETAFILE hmf = CloseMetaFile(hdcMeta);
  105.  
  106. // Get metafile data
  107. UINT size = GetMetaFileBitsEx( hmf, 0, NULL );
  108. BYTE* buffer = new BYTE[size];
  109. GetMetaFileBitsEx( hmf, size, buffer );
  110. DeleteMetaFile(hmf);
  111.  
  112. // Convert metafile binary data to hexadecimal
  113. char* str = rtf_bin_hex_convert( buffer, size );
  114. delete []buffer;
  115.  
  116. // Format picture paragraph
  117. RTF_PARAGRAPH_FORMAT* pf = rtf_get_paragraphformat();
  118. pf->paragraphText = "";
  119. rtf_write_paragraphformat();
  120.  
  121. // Writes RTF picture data
  122. char rtfText[1024];
  123. sprintf( rtfText, "\n{\\pict\\wmetafile8\\picwgoal%d\\pichgoal%d\\picscalex%d\\picscaley%d\n", hmWidth, hmHeight, width, height );
  124. if ( fwrite( rtfText, 1, strlen(rtfText), rtfFile ) < strlen(rtfText) )
  125. error = RTF_IMAGE_ERROR;
  126. fwrite( str, 1, 2*size, rtfFile );
  127. strcpy( rtfText, "}" );
  128. fwrite( rtfText, 1, strlen(rtfText), rtfFile );
  129. }
  130. }
  131. else
  132. {
  133. // Writes RTF picture data
  134. char rtfText[1024];
  135. strcpy( rtfText, "\n\\par\\pard *** Error! Wrong image format ***\\par" );
  136. fwrite( rtfText, 1, strlen(rtfText), rtfFile );
  137. }
  138.  
  139. // Return error flag
  140. return error;
  141. }
  142.  
  143. void main()
  144. {
  145. rtf_open( "R:\\rtf\\rtf\\test.rtf" );
  146.  
  147. rtf_load_image( "R:\\rtf\\rtf\\123.bmp", 32, 32 );
  148.  
  149. rtf_close();
  150. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int rtf_load_image(char*, int, int)’:
prog.cpp:9:14: error: ‘RTF_SUCCESS’ was not declared in this scope
  int error = RTF_SUCCESS;
              ^
prog.cpp:13:26: error: ‘strstr’ was not declared in this scope
  if ( strstr(image,".bmp") == NULL )
                          ^
prog.cpp:26:8: error: ‘rtfPicture’ was not declared in this scope
   if ( rtfPicture != NULL )
        ^
prog.cpp:53:3: error: ‘HGLOBAL’ was not declared in this scope
   HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, sz);
   ^
prog.cpp:53:11: error: expected ‘;’ before ‘hGlobal’
   HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, sz);
           ^
prog.cpp:54:28: error: ‘hGlobal’ was not declared in this scope
   void* pData = GlobalLock(hGlobal);
                            ^
prog.cpp:54:35: error: ‘GlobalLock’ was not declared in this scope
   void* pData = GlobalLock(hGlobal);
                                   ^
prog.cpp:55:28: error: ‘memcpy’ was not declared in this scope
   memcpy(pData, &buf[0], sz);
                            ^
prog.cpp:56:23: error: ‘GlobalUnlock’ was not declared in this scope
   GlobalUnlock(hGlobal);
                       ^
prog.cpp:58:3: error: ‘IStream’ was not declared in this scope
   IStream* pStream = NULL;
   ^
prog.cpp:58:12: error: ‘pStream’ was not declared in this scope
   IStream* pStream = NULL;
            ^
prog.cpp:59:39: error: ‘TRUE’ was not declared in this scope
   if ( CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) == S_OK )
                                       ^
prog.cpp:59:53: error: ‘CreateStreamOnHGlobal’ was not declared in this scope
   if ( CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) == S_OK )
                                                     ^
prog.cpp:59:58: error: ‘S_OK’ was not declared in this scope
   if ( CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) == S_OK )
                                                          ^
prog.cpp:61:4: error: ‘HRESULT’ was not declared in this scope
    HRESULT hr;
    ^
prog.cpp:61:12: error: expected ‘;’ before ‘hr’
    HRESULT hr;
            ^
prog.cpp:62:9: error: ‘hr’ was not declared in this scope
    if ((hr = OleLoadPicture( pStream, sz, FALSE, IID_IPicture, (LPVOID *)&rtfPicture)) != S_OK)
         ^
prog.cpp:62:43: error: ‘FALSE’ was not declared in this scope
    if ((hr = OleLoadPicture( pStream, sz, FALSE, IID_IPicture, (LPVOID *)&rtfPicture)) != S_OK)
                                           ^
prog.cpp:62:50: error: ‘IID_IPicture’ was not declared in this scope
    if ((hr = OleLoadPicture( pStream, sz, FALSE, IID_IPicture, (LPVOID *)&rtfPicture)) != S_OK)
                                                  ^
prog.cpp:62:65: error: ‘LPVOID’ was not declared in this scope
    if ((hr = OleLoadPicture( pStream, sz, FALSE, IID_IPicture, (LPVOID *)&rtfPicture)) != S_OK)
                                                                 ^
prog.cpp:62:73: error: expected primary-expression before ‘)’ token
    if ((hr = OleLoadPicture( pStream, sz, FALSE, IID_IPicture, (LPVOID *)&rtfPicture)) != S_OK)
                                                                         ^
prog.cpp:62:75: error: ‘rtfPicture’ was not declared in this scope
    if ((hr = OleLoadPicture( pStream, sz, FALSE, IID_IPicture, (LPVOID *)&rtfPicture)) != S_OK)
                                                                           ^
prog.cpp:62:85: error: ‘OleLoadPicture’ was not declared in this scope
    if ((hr = OleLoadPicture( pStream, sz, FALSE, IID_IPicture, (LPVOID *)&rtfPicture)) != S_OK)
                                                                                     ^
prog.cpp:63:13: error: ‘RTF_IMAGE_ERROR’ was not declared in this scope
     error = RTF_IMAGE_ERROR;
             ^
prog.cpp:71:8: error: ‘rtfPicture’ was not declared in this scope
   if ( rtfPicture != NULL )
        ^
prog.cpp:75:14: error: expected type-specifier before ‘HDC’
     operator HDC() const {
              ^
prog.cpp:85:5: error: ‘HDC’ does not name a type
     HDC _dc;
     ^
prog.cpp: In destructor ‘rtf_load_image(char*, int, int)::DC::~DC()’:
prog.cpp:79:6: error: ‘::ReleaseDC’ has not been declared
      ::ReleaseDC( ::GetDesktopWindow(), this->_dc );
      ^
prog.cpp:79:19: error: ‘::GetDesktopWindow’ has not been declared
      ::ReleaseDC( ::GetDesktopWindow(), this->_dc );
                   ^
prog.cpp:79:47: error: ‘class rtf_load_image(char*, int, int)::DC’ has no member named ‘_dc’
      ::ReleaseDC( ::GetDesktopWindow(), this->_dc );
                                               ^
prog.cpp: In constructor ‘rtf_load_image(char*, int, int)::DC::DC()’:
prog.cpp:82:8: error: class ‘rtf_load_image(char*, int, int)::DC’ does not have any field named ‘_dc’
      : _dc( ::GetDC( ::GetDesktopWindow() ) )
        ^
prog.cpp:82:13: error: ‘::GetDC’ has not been declared
      : _dc( ::GetDC( ::GetDesktopWindow() ) )
             ^
prog.cpp:82:22: error: ‘::GetDesktopWindow’ has not been declared
      : _dc( ::GetDC( ::GetDesktopWindow() ) )
                      ^
prog.cpp: In function ‘int rtf_load_image(char*, int, int)’:
prog.cpp:94:52: error: ‘LOGPIXELSX’ was not declared in this scope
    int nWidth = MulDiv( hmWidth, GetDeviceCaps( dc,LOGPIXELSX), 2540 );
                                                    ^
prog.cpp:94:62: error: ‘GetDeviceCaps’ was not declared in this scope
    int nWidth = MulDiv( hmWidth, GetDeviceCaps( dc,LOGPIXELSX), 2540 );
                                                              ^
prog.cpp:94:70: error: ‘MulDiv’ was not declared in this scope
    int nWidth = MulDiv( hmWidth, GetDeviceCaps( dc,LOGPIXELSX), 2540 );
                                                                      ^
prog.cpp:95:54: error: ‘LOGPIXELSY’ was not declared in this scope
    int nHeight = MulDiv( hmHeight, GetDeviceCaps( dc,LOGPIXELSY), 2540 );
                                                      ^
prog.cpp:98:4: error: ‘HDC’ was not declared in this scope
    HDC hdcMeta = CreateMetaFile(NULL);
    ^
prog.cpp:98:8: error: expected ‘;’ before ‘hdcMeta’
    HDC hdcMeta = CreateMetaFile(NULL);
        ^
prog.cpp:101:24: error: ‘hdcMeta’ was not declared in this scope
    rtfPicture->Render( hdcMeta, 0, 0, nWidth, nHeight, 0, hmHeight, hmWidth, -hmHeight, NULL );
                        ^
prog.cpp:104:4: error: ‘HMETAFILE’ was not declared in this scope
    HMETAFILE hmf = CloseMetaFile(hdcMeta);
    ^
prog.cpp:104:14: error: expected ‘;’ before ‘hmf’
    HMETAFILE hmf = CloseMetaFile(hdcMeta);
              ^
prog.cpp:107:4: error: ‘UINT’ was not declared in this scope
    UINT size = GetMetaFileBitsEx( hmf, 0, NULL );
    ^
prog.cpp:107:9: error: expected ‘;’ before ‘size’
    UINT size = GetMetaFileBitsEx( hmf, 0, NULL );
         ^
prog.cpp:108:4: error: ‘BYTE’ was not declared in this scope
    BYTE* buffer = new BYTE[size];
    ^
prog.cpp:108:10: error: ‘buffer’ was not declared in this scope
    BYTE* buffer = new BYTE[size];
          ^
prog.cpp:108:23: error: expected type-specifier before ‘BYTE’
    BYTE* buffer = new BYTE[size];
                       ^
prog.cpp:108:23: error: expected ‘;’ before ‘BYTE’
prog.cpp:109:23: error: ‘hmf’ was not declared in this scope
    GetMetaFileBitsEx( hmf, size, buffer );
                       ^
prog.cpp:109:28: error: ‘size’ was not declared in this scope
    GetMetaFileBitsEx( hmf, size, buffer );
                            ^
prog.cpp:109:41: error: ‘GetMetaFileBitsEx’ was not declared in this scope
    GetMetaFileBitsEx( hmf, size, buffer );
                                         ^
prog.cpp:110:22: error: ‘DeleteMetaFile’ was not declared in this scope
    DeleteMetaFile(hmf);
                      ^
prog.cpp:113:50: error: ‘rtf_bin_hex_convert’ was not declared in this scope
    char* str = rtf_bin_hex_convert( buffer, size );
                                                  ^
prog.cpp:114:13: error: type ‘<type error>’ argument given to ‘delete’, expected pointer
    delete []buffer;
             ^
prog.cpp:117:4: error: ‘RTF_PARAGRAPH_FORMAT’ was not declared in this scope
    RTF_PARAGRAPH_FORMAT* pf = rtf_get_paragraphformat();
    ^
prog.cpp:117:26: error: ‘pf’ was not declared in this scope
    RTF_PARAGRAPH_FORMAT* pf = rtf_get_paragraphformat();
                          ^
prog.cpp:117:55: error: ‘rtf_get_paragraphformat’ was not declared in this scope
    RTF_PARAGRAPH_FORMAT* pf = rtf_get_paragraphformat();
                                                       ^
prog.cpp:119:30: error: ‘rtf_write_paragraphformat’ was not declared in this scope
    rtf_write_paragraphformat();
                              ^
prog.cpp:123:132: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘long int’ [-Wformat=]
    sprintf( rtfText, "\n{\\pict\\wmetafile8\\picwgoal%d\\pichgoal%d\\picscalex%d\\picscaley%d\n", hmWidth, hmHeight, width, height );
                                                                                                                                    ^
prog.cpp:123:132: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘long int’ [-Wformat=]
prog.cpp:124:43: error: ‘strlen’ was not declared in this scope
    if ( fwrite( rtfText, 1, strlen(rtfText), rtfFile ) < strlen(rtfText) )
                                           ^
prog.cpp:124:46: error: ‘rtfFile’ was not declared in this scope
    if ( fwrite( rtfText, 1, strlen(rtfText), rtfFile ) < strlen(rtfText) )
                                              ^
prog.cpp:125:13: error: ‘RTF_IMAGE_ERROR’ was not declared in this scope
     error = RTF_IMAGE_ERROR;
             ^
prog.cpp:126:28: error: ‘rtfFile’ was not declared in this scope
    fwrite( str, 1, 2*size, rtfFile );
                            ^
prog.cpp:127:25: error: ‘strcpy’ was not declared in this scope
    strcpy( rtfText, "}" );
                         ^
prog.cpp:128:38: error: ‘strlen’ was not declared in this scope
    fwrite( rtfText, 1, strlen(rtfText), rtfFile );
                                      ^
prog.cpp:135:75: error: ‘strcpy’ was not declared in this scope
   strcpy( rtfText, "\n\\par\\pard *** Error! Wrong image format ***\\par" );
                                                                           ^
prog.cpp:136:37: error: ‘strlen’ was not declared in this scope
   fwrite( rtfText, 1, strlen(rtfText), rtfFile );
                                     ^
prog.cpp:136:40: error: ‘rtfFile’ was not declared in this scope
   fwrite( rtfText, 1, strlen(rtfText), rtfFile );
                                        ^
prog.cpp: At global scope:
prog.cpp:143:11: error: ‘::main’ must return ‘int’
 void main()
           ^
prog.cpp: In function ‘int main()’:
prog.cpp:145:37: error: ‘rtf_open’ was not declared in this scope
  rtf_open( "R:\\rtf\\rtf\\test.rtf" );
                                     ^
prog.cpp:147:50: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
  rtf_load_image( "R:\\rtf\\rtf\\123.bmp", 32, 32 );
                                                  ^
prog.cpp:149:12: error: ‘rtf_close’ was not declared in this scope
  rtf_close();
            ^
stdout
Standard output is empty