Use ltmmMemory for Memory Conversion Example for C

#define MAKE_MEDIA_PATH(pFileName) (TEXT("C:\\LEADTOOLS 17.5\\Media\\")TEXT(pFileName))


#define WM_CONVERTNOTIFY (WM_USER+1)

IltmmConvert *g_pConvert;
IltmmMemory  *g_pSource;
IltmmMemory  *g_pTarget;

HRESULT SetAVIRecompression(HWND hwndParent);

void UseltmmMemoryForMemoryConversion_Example (HWND hWnd)
{
   {//1. Create a convert object, g_pSource and g_pTarget objects:

      // create a convert object, g_pSource and g_pTarget memory objects
      CoCreateInstance(&CLSID_ltmmConvert, 
                       NULL, 
                       CLSCTX_INPROC_SERVER, 
                       &IID_IltmmConvert, 
                       (void**) &g_pConvert); 

      CoCreateInstance(&CLSID_ltmmMemory, 
                       NULL, 
                       CLSCTX_INPROC_SERVER, 
                       &IID_IltmmMemory, 
                       (void**) &g_pSource); 

      CoCreateInstance(&CLSID_ltmmMemory, 
                       NULL, 
                       CLSCTX_INPROC_SERVER, 
                       &IID_IltmmMemory, 
                       (void**) &g_pTarget); 

   }//1. Create a convert object, g_pSource and g_pTarget objects:

   // set notify window
   IltmmConvert_SetNotifyWindow(g_pConvert, (long)hWnd, WM_CONVERTNOTIFY); 

   {//2.	Setup the g_pSource object, pre-load it with data:

      SAFEARRAY sa; 
      VARIANT var; 
      UCHAR arr [100];
      long fl = 100;


      // Set the buffer size
      IltmmMemory_put_BufferSize (g_pSource, fl); 

      // pass data from the arr BYTE array... 
      memset(&sa, 0, sizeof(sa)); 
      sa.cbElements = sizeof(unsigned char); 
      sa.cDims = 1; 
      sa.fFeatures = (FADF_AUTO | FADF_FIXEDSIZE); 
      sa.pvData = arr; //data to fill
      sa.rgsabound[0].cElements = fl; 
      VariantInit(&var); 
      V_VT(&var) = (VT_ARRAY | VT_UI1); 
      V_ARRAY(&var) = &sa; 

      IltmmMemory_SetData (g_pSource, 0, fl, var); 

   }//2.	Setup the g_pSource object, preload it with data:

   // Set input memory object
   IltmmConvert_put_SourceStream(g_pConvert, g_pSource); 

   // Set output memory object
   IltmmConvert_put_TargetStream(g_pConvert, g_pTarget); 

   // Set recompression
   SetAVIRecompression(NULL); 

   // Start conversion
   IltmmConvert_StartConvert (g_pConvert); 
}

//
// SetAVIRecompression
// sets up LEAD video compression, MP3 audio compression, and AVI file output
//
// hwndParent = parent window for compressor property dialog boxes
//
HRESULT SetAVIRecompression(HWND hwndParent) 
{
   IltmmCompressors *pCompressors; 
   long index; 
   VARIANT_BOOL f; 
   BSTR bstr; 

   // select the LEAD video compressor
   IltmmConvert_get_VideoCompressors(g_pConvert, &pCompressors); 

   bstr = SysAllocString(L"@device:sw:{33D9A760-90C8-11D0-BD43-00A0C911CE86}\\LEAD MCMP/MJPEG Codec (2.0)");

   IltmmCompressors_Find (pCompressors , bstr, &index); 

   SysFreeString(bstr); 

   if(index < 0) 
   {
      // compressor isn't registered
      IltmmCompressors_Release (pCompressors); 

      return E_FAIL; 
   }
   IltmmCompressors_put_Selection (pCompressors, index); 
   IltmmCompressors_Release(pCompressors); 

   // select the MP3 audio video compressor
   IltmmConvert_get_AudioCompressors(g_pConvert, &pCompressors); 
   bstr = SysAllocString(L"@device:cm:{33D9A761-90C8-11D0-BD43-00A0C911CE86}\\85MPEG Layer-3");
   IltmmCompressors_Find(pCompressors, bstr, &index); 
   SysFreeString(bstr); 
   if(index < 0) 
   {
      // compressor isn't registered
      IltmmCompressors_Release (pCompressors); 
      return E_FAIL; 
   }

   IltmmCompressors_put_Selection (pCompressors, index); 
   IltmmCompressors_Release(pCompressors); 

   // set output format to AVI
   IltmmConvert_put_TargetFormat (g_pConvert, ltmmConvert_TargetFormat_Avi); 

   // set video compressor properties
   IltmmConvert_HasDialog(g_pConvert, ltmmConvert_Dlg_VideoCompressor, &f); 
   if(f) 
      IltmmConvert_ShowDialog (g_pConvert, ltmmConvert_Dlg_VideoCompressor, (long) hwndParent); 

   // set audio compressor properties
   IltmmConvert_HasDialog(g_pConvert, ltmmConvert_Dlg_AudioCompressor, &f); 
   if(f) 
      IltmmConvert_ShowDialog(g_pConvert, ltmmConvert_Dlg_AudioCompressor, (long) hwndParent); 

   return S_OK; 
}

//3. Handle the notification window to plot the data and save it to a file:
LRESULT OnConvertNotify(WPARAM wParam, LPARAM lParam) 
{
   lParam = lParam ;

   switch(wParam) 
   {
      case ltmmConvert_Notify_Complete: 
      {
         // Indicates a successful completion of the conversion process. 

         HANDLE hFile = NULL; 
         long fl = 0; 
         unsigned long lBytesWritten; 
         IltmmMemory *pTarget = NULL; 
         VARIANT varData; 
         unsigned char *pBuffer = NULL; 
         SAFEARRAY sa; 

         // write the data to a file
         hFile = CreateFile( MAKE_MEDIA_PATH("Target.avi"),      // open file name 
                             GENERIC_WRITE,         // open for writing 
                             FILE_SHARE_READ,       // share for reading 
                             NULL,                  // no security 
                             CREATE_ALWAYS,         // re-create the file
                             FILE_ATTRIBUTE_NORMAL, // normal file 
                             NULL);                 // no attr. template 

         IltmmConvert_get_TargetStream(g_pConvert, (IUnknown**)&pTarget); 

         if(!pTarget) 
            break; 

         IltmmMemory_get_BufferSize(pTarget, &fl); 

         VariantInit(&varData); 

         // pass data
         memset(&sa, 0, sizeof(sa)); 
         sa.cbElements = sizeof(unsigned char); 
         sa.cDims = 1; 
         sa.fFeatures = (FADF_AUTO | FADF_FIXEDSIZE); 
         sa.pvData = (UCHAR*) malloc(fl*sizeof (UCHAR)); 
         sa.rgsabound[0].cElements = fl; 
         V_VT(&varData) = (VT_ARRAY | VT_UI1); 
         V_ARRAY(&varData) = &sa; 

         //
         // To lock the buffer for synchronization, use the following 
         // line instead of the line below : 
         // IltmmMemory_LockBuffer (pTarget, &varData); 
         // or
         // IltmmMemory_LockRegion (pTarget, 0, fl, &varData); 
         // You may also use the IltmmMemory_GetData() function
         // To get the data without allocating a SAFEARRAY : 
         // IltmmMemory_GetData (pTarget, 0, fl, &varData); 
         //

         IltmmMemory_CopyData(pTarget, 0, fl, &varData); 

         SafeArrayAccessData(V_ARRAY(&varData), (void**)&pBuffer); 

         WriteFile(hFile, pBuffer, fl, &lBytesWritten, NULL); 

         {// Plot data on screen

            char dataPlot[255]; 
            char data[20]; 
            short sData; 
            int i;

            dataPlot[0] = '\0' ;

            for(i=0; i<fl ; i++)
            {
               IltmmMemory_GetByte (pTarget, i, &sData); 

               strcat (dataPlot,"0x"); 
               strcat (dataPlot,ltoa(sData, data, 16)); 
               strcat (dataPlot,"  "); 
            }

            printf ("%s", dataPlot); 

         }// Plot data on screen

         // if IltmmMemory_LockBuffer() or 
         // IltmmMemory_LockRegion() is used, 
         // IltmmMemory_Unloack() must be called : 
         // IltmmMemory_Unlock (pTarget); 

         SafeArrayUnaccessData(V_ARRAY(&varData)); 

         VariantClear(&varData); 

         CloseHandle(hFile); 

         IltmmMemory_Release(pTarget); 
      }

      break; 
   }

   return 0; 
}