使用ijl庫Jpg轉RGB

smilestone322發表於2016-03-01

#include “ijl.h”

BOOL ConverJPG2BID(BYTE *lpJpgBuffer,

                      DWORD dwJpgBufferSize,

                      BYTE *lppRgbBuffer,

                      DWORD *lpdwWidth,

                      DWORD *lpdwHeight,

                      DWORD *lpdwNumberOfChannels)

{

     BOOL bres;

     IJLERR   jerr;

     DWORD    dwWholeImageSize;

     BYTE *lpTemp = lppRgbBuffer;

 

     JPEG_CORE_PROPERTIES jcprops;

     bres = TRUE;

 

     __try

     {

         jerr = ijlInit(&jcprops);

         if (IJL_OK != jerr)

         {

              bres = FALSE;

              __leave;

         }

 

         jcprops.JPGFile        = NULL;

         jcprops.JPGBytes   = lpJpgBuffer;

         jcprops.JPGSizeBytes = dwJpgBufferSize;

 

         jerr = ijlRead(&jcprops, IJL_JBUFF_READPARAMS);

         if (IJL_OK != jerr)

         {

              bres = FALSE;

              __leave;

         }

 

         switch (jcprops.JPGChannels)

         {

         case 1:

              {

                   jcprops.JPGColor   = IJL_G;

                   jcprops.DIBColor   = IJL_RGB;

                   jcprops.DIBChannels    = 3;

                   break;

              }

         case 3:

              {

                   jcprops.JPGColor   = IJL_YCBCR;

                   jcprops.DIBColor   = IJL_BGR;

                   jcprops.DIBChannels    = 3;

                   break;

              }

         default:

              {

                   jcprops.JPGColor   = IJL_OTHER;

                   jcprops.DIBColor   = IJL_OTHER;

                  jcprops.DIBChannels    = jcprops.JPGChannels;

                   break;

              }

         }

 

         dwWholeImageSize = jcprops.JPGWidth * jcprops.JPGHeight *

              jcprops.DIBChannels;

 

         jcprops.DIBWidth   = jcprops.JPGWidth;

         jcprops.DIBHeight  = jcprops.JPGHeight;

         jcprops.DIBPadBytes    = 0;

         jcprops.DIBBytes   = lpTemp;

 

         jerr = ijlRead(&jcprops, IJL_JBUFF_READWHOLEIMAGE);

         if (IJL_OK != jerr)

         {

              bres = FALSE;

              __leave;

         }

     } // __try

 

     __finally

     {

         ijlFree(&jcprops);

         *lpdwWidth    = jcprops.DIBWidth;

         *lpdwHeight   = jcprops.DIBHeight;

         *lpdwNumberOfChannels  = jcprops.DIBChannels;

     } // __finally

 

     return bres;

}

 

int CPictureXXX::SetJpegBuffer(void *pJpegBuf,

                                     DWORD len,

                                     BYTE *lppRgbBuffer,

                                     DWORD *lpdwWidth,

                                     DWORD *lpdwHeight)

{

     DWORD nChannels = 0;

 

     ConverJPG2BID((BYTE *)pJpegBuf, len, lppRgbBuffer, &(*lpdwWidth),

                            &(*lpdwHeight), &nChannels);

     return 0;

}

 

引數解釋:

pJpegBuf:儲存Jpeg影象資料的緩衝區;

len:儲存Jpeg影象資料的size

lppRgbBuffer:儲存轉換成RGB後的影象緩衝區;

lpdwWidth:影象的寬,從jpg影象中獲取;

lpdwHeight:影象的高,從jpg影象中獲取;

相關文章