📄 chenimagedct.cpp
字号:
// ChenImageDCT.cpp
#include <ChenImage.h>
#include <ChenImageDCT.h>
#include "ChenImageMatlab.h"
#ifdef _USEIPP
#include <ippi.h>
#include <ipps.h>
#include <ippm.h>
#endif
int ChenImageDCT_DCT8x8Fwd_32f_I(int nBlockSize, float *pBlock)
{
Engine* pEngine = ChenImageMatlab_getEngine();
if (pEngine != NULL)
{
//Send the input image and parameters to MATLAB
ChenImageMatlab_putImageInWorkspace(nBlockSize, nBlockSize, 1, pBlock, "pBlockI");
// Carry out forward DCT transform
engEvalString(pEngine, "pBlockO = dct2(pBlockI)");
//Get the result image
ChenImageMatlab_getImageFromWorkspace(nBlockSize, nBlockSize, 1, "pBlockO", pBlock);
return GOOD_RETURN;
}
else
{
return BAD_RETURN;
}
}
int ChenImageDCT_DCT8x8Fwd_32f(int nBlockSize, float *pBlockI, float *pBlockO)
{
Engine* pEngine = ChenImageMatlab_getEngine();
if (pEngine != NULL)
{
//Send the input image and parameters to MATLAB
ChenImageMatlab_putImageInWorkspace(nBlockSize, nBlockSize, 1, pBlockI, "pBlockI");
// Carry out forward DCT transform
engEvalString(pEngine, "pBlockO = dct2(pBlockI)");
//Get the result image
ChenImageMatlab_getImageFromWorkspace(nBlockSize, nBlockSize, 1, "pBlockO", pBlockO);
return GOOD_RETURN;
}
else
{
return BAD_RETURN;
}
}
int ChenImageDCT_DCT8x8Inv_32f_I(int nBlockSize, float *pBlock)
{
Engine* pEngine = ChenImageMatlab_getEngine();
if (pEngine != NULL)
{
//Send the input image and parameters to MATLAB
ChenImageMatlab_putImageInWorkspace(nBlockSize, nBlockSize, 1, pBlock, "pBlockI");
// Carry out the inverse DCT transform
engEvalString(pEngine, "pBlockO = idct2(pBlockI)");
//Get the result image
ChenImageMatlab_getImageFromWorkspace(nBlockSize, nBlockSize, 1, "pBlockO", pBlock);
return GOOD_RETURN;
}
else
{
return BAD_RETURN;
}
}
int ChenImageDCT_DCT8x8Inv_32f(int nBlockSize, float *pBlockI, float *pBlockO)
{
Engine* pEngine = ChenImageMatlab_getEngine();
if (pEngine != NULL)
{
//Send the input image and parameters to MATLAB
ChenImageMatlab_putImageInWorkspace(nBlockSize, nBlockSize, 1, pBlockI, "pBlockI");
// Carry out the inverse DCT transform
engEvalString(pEngine, "pBlockO = idct2(pBlockI)");
//Get the result image
ChenImageMatlab_getImageFromWorkspace(nBlockSize, nBlockSize, 1, "pBlockO", pBlockO);
return GOOD_RETURN;
}
else
{
return BAD_RETURN;
}
}
int ChenImageDCT_block88DCTQuantize(const int nImageW, const int nImageH, const short* pSrc, const short* pTable, short* pDstQuant)
{
#ifdef _USEIPP
bool bSuccess = true;
int nPixels = nImageW * nImageH;
float* pDstQuantF = new float[nPixels];
float* pSrcF = new float[nPixels];
RECORD_IPP_SUCCESS( ippsConvert_16s32f((const Ipp16s*)pSrc, (Ipp32f*)pSrcF, nPixels), bSuccess );
RECORD_SUCCESS( ChenImageDCT_block88DCTQuantize(nImageW, nImageH, pSrcF, pTable, pDstQuantF), bSuccess );
IppRoundMode rndMode = ippRndNear;
int nScaleFactor = 0;
RECORD_IPP_SUCCESS( ippsConvert_32f16s_Sfs((const Ipp32f*)pDstQuantF, (Ipp16s*)pDstQuant, nPixels, rndMode, nScaleFactor), bSuccess );
delete [] pDstQuantF;
delete [] pSrcF;
return bSuccess ? GOOD_RETURN : BAD_RETURN;
#else
bool bSuccess = true;
int nPixels = nImageW * nImageH;
float* pDstQuantF = new float[nPixels];
float* pSrcF = new float[nPixels];
if ((pSrcF != NULL) && (pDstQuantF != NULL))
{
for (int i = 0; i < nPixels; i++)
{
pSrcF[i] = (float)(pSrc[i]);
}
RECORD_SUCCESS( ChenImageDCT_block88DCTQuantize(nImageW, nImageH, pSrcF, pTable, pDstQuantF), bSuccess );
for (int i = 0; i < nPixels; i++)
{
pDstQuant[i] = ChenImage_RoundFloat(pDstQuantF[i]);
}
delete [] pSrcF;
delete [] pDstQuantF;
return bSuccess ? GOOD_RETURN : BAD_RETURN;
}
else
{
if (pSrcF)
delete [] pSrcF;
if (pDstQuantF)
delete [] pDstQuantF;
return BAD_RETURN;
}
#endif
}
int ChenImageDCT_block88DCTQuantize(const int nImageW, const int nImageH, const float* pSrc, const short* pTable, float* pDstQuant)
{
#ifdef _USEIPP
bool bSuccess = true;
int nBlockSize = 8;
float* pBlock = new float [nBlockSize*nBlockSize];
float* pTableF = new float [nBlockSize*nBlockSize];
int nSrcStep;
int nDstStep;
IppiSize roiSize = {nBlockSize, nBlockSize};
// Convert table to float format
nSrcStep = nBlockSize*sizeof(short);
nDstStep = nBlockSize*sizeof(float);
RECORD_IPP_SUCCESS( ippiConvert_16s32f_C1R((const Ipp16s*) pTable, nSrcStep, (Ipp32f*)pTableF, nDstStep, roiSize), bSuccess );
// Loop over blocks
int nBlockRows = nImageH / nBlockSize;
int nBlockCols = nImageW / nBlockSize;
for (int nBlockRow = 0; nBlockRow < nBlockRows; nBlockRow++) {
for (int nBlockCol = 0; nBlockCol < nBlockCols; nBlockCol++) {
// Copy from source image into block
for (int nRow = 0; nRow < nBlockSize; nRow++) {
int nRowReal = nBlockRow*nBlockSize + nRow;
for (int nCol = 0; nCol < nBlockSize; nCol++) {
int nColReal = nBlockCol*nBlockSize + nCol;
pBlock[nRow*nBlockSize+nCol] = pSrc[nRowReal*nImageW+nColReal];
}
}
// Perform 8x8 DCT
RECORD_IPP_SUCCESS( ippiDCT8x8Fwd_32f_C1I((Ipp32f*) pBlock), bSuccess );
// Quantize
nSrcStep = nBlockSize*sizeof(float);
RECORD_IPP_SUCCESS( ippiDiv_32f_C1IR((const Ipp32f*) pTableF, nSrcStep, (Ipp32f*) pBlock, nDstStep, roiSize), bSuccess );
// Copy from block into quantized destination image
for (int nRow = 0; nRow < nBlockSize; nRow++) {
int nRowReal = nBlockRow*nBlockSize + nRow;
for (int nCol = 0; nCol < nBlockSize; nCol++) {
int nColReal = nBlockCol*nBlockSize + nCol;
pDstQuant[nRowReal*nImageW+nColReal] = pBlock[nRow*nBlockSize+nCol];
}
}
} // end nBlockCol
} // end nBlockRow
delete [] pBlock;
delete [] pTableF;
return bSuccess ? GOOD_RETURN : BAD_RETURN;
#else
int nBlockSize = 8;
float* pBlock = new float [nBlockSize*nBlockSize];
// Loop over blocks
int nBlockRows = nImageH / nBlockSize;
int nBlockCols = nImageW / nBlockSize;
for (int nBlockRow = 0; nBlockRow < nBlockRows; nBlockRow++) {
for (int nBlockCol = 0; nBlockCol < nBlockCols; nBlockCol++) {
// Copy from source image into block
for (int nRow = 0; nRow < nBlockSize; nRow++) {
int nRowReal = nBlockRow*nBlockSize + nRow;
for (int nCol = 0; nCol < nBlockSize; nCol++) {
int nColReal = nBlockCol*nBlockSize + nCol;
pBlock[nRow*nBlockSize+nCol] = pSrc[nRowReal*nImageW+nColReal];
}
}
// Perform 8x8 DCT using MATLAB
ChenImageDCT_DCT8x8Fwd_32f_I(nBlockSize, pBlock);
// Quantize
for (int i = 0; i < nBlockSize*nBlockSize; i++)
{
pBlock[i] = pBlock[i]/pTable[i];
}
// Copy from block into quantized destination image
for (int nRow = 0; nRow < nBlockSize; nRow++) {
int nRowReal = nBlockRow*nBlockSize + nRow;
for (int nCol = 0; nCol < nBlockSize; nCol++) {
int nColReal = nBlockCol*nBlockSize + nCol;
pDstQuant[nRowReal*nImageW+nColReal] = pBlock[nRow*nBlockSize+nCol];
}
}
} // end nBlockCol
} // end nBlockRow
delete [] pBlock;
return GOOD_RETURN;
#endif
}
int ChenImageDCT_block88InvDCTDequantize(const int nImageW, const int nImageH, const short* pSrc, const short* pTable, short* pDst)
{
#ifdef _USEIPP
bool bSuccess = true;
int nPixels = nImageW * nImageH;
float* pSrcF = new float[nPixels];
float* pDstF = new float[nPixels];
RECORD_IPP_SUCCESS( ippsConvert_16s32f((const Ipp16s*)pSrc, (Ipp32f*)pSrcF, nPixels), bSuccess );
RECORD_SUCCESS( ChenImageDCT_block88InvDCTDequantize(nImageW, nImageH, pSrcF, pTable, pDstF), bSuccess );
IppRoundMode rndMode = ippRndNear;
int nScaleFactor = 0;
RECORD_IPP_SUCCESS( ippsConvert_32f16s_Sfs((const Ipp32f*)pDstF, (Ipp16s*)pDst, nPixels, rndMode, nScaleFactor), bSuccess );
delete [] pSrcF;
delete [] pDstF;
return bSuccess ? GOOD_RETURN : BAD_RETURN;
#else
bool bSuccess = true;
int nPixels = nImageW * nImageH;
float* pSrcF = new float[nPixels];
float* pDstF = new float[nPixels];
if ((pSrcF != NULL) && (pDstF != NULL))
{
for (int i = 0; i < nPixels; i++)
{
pSrcF[i] = (float)(pSrc[i]);
}
RECORD_SUCCESS( ChenImageDCT_block88InvDCTDequantize(nImageW, nImageH, pSrcF, pTable, pDstF), bSuccess );
for (int i = 0; i < nPixels; i++)
{
pDst[i] = ChenImage_RoundFloat(pDstF[i]);
}
delete [] pSrcF;
delete [] pDstF;
return bSuccess ? GOOD_RETURN : BAD_RETURN;
}
else
{
if (pSrcF)
delete [] pSrcF;
if (pDstF)
delete [] pDstF;
return BAD_RETURN;
}
#endif
}
int ChenImageDCT_block88InvDCTDequantize(const int nImageW, const int nImageH, const float* pSrc, const short* pTable, float* pDst)
{
#ifdef _USEIPP
bool bSuccess = true;
int nBlockSize = 8;
float* pBlock = new float [nBlockSize*nBlockSize];
float* pTableF = new float [nBlockSize*nBlockSize];
int nSrcStep;
int nDstStep;
IppiSize roiSize = {nBlockSize, nBlockSize};
// Convert table to float format
nSrcStep = nBlockSize*sizeof(short);
nDstStep = nBlockSize*sizeof(float);
RECORD_IPP_SUCCESS( ippiConvert_16s32f_C1R((const Ipp16s*) pTable, nSrcStep, (Ipp32f*)pTableF, nDstStep, roiSize), bSuccess );
// Loop over blocks
int nBlockRows = nImageH / nBlockSize;
int nBlockCols = nImageW / nBlockSize;
for (int nBlockRow = 0; nBlockRow < nBlockRows; nBlockRow++) {
for (int nBlockCol = 0; nBlockCol < nBlockCols; nBlockCol++) {
// Copy from source image into block
for (int nRow = 0; nRow < nBlockSize; nRow++) {
int nRowReal = nBlockRow*nBlockSize + nRow;
for (int nCol = 0; nCol < nBlockSize; nCol++) {
int nColReal = nBlockCol*nBlockSize + nCol;
pBlock[nRow*nBlockSize+nCol] = pSrc[nRowReal*nImageW+nColReal];
}
}
// Dequantize
nSrcStep = nBlockSize*sizeof(float);
nDstStep = nBlockSize*sizeof(float);
RECORD_IPP_SUCCESS( ippiMul_32f_C1IR((const Ipp32f*) pTableF, nSrcStep, (Ipp32f*) pBlock, nDstStep, roiSize), bSuccess );
// Perform 8x8 inverse DCT
RECORD_IPP_SUCCESS( ippiDCT8x8Inv_32f_C1I( (Ipp32f*) pBlock ), bSuccess );
// Copy from block into destination image
for (int nRow = 0; nRow < nBlockSize; nRow++) {
int nRowReal = nBlockRow*nBlockSize + nRow;
for (int nCol = 0; nCol < nBlockSize; nCol++) {
int nColReal = nBlockCol*nBlockSize + nCol;
pDst[nRowReal*nImageW+nColReal] = pBlock[nRow*nBlockSize+nCol];
}
}
} // end nBlockCol
} // end nBlockRow
if (pBlock) delete [] pBlock;
if (pTableF) delete [] pTableF;
return bSuccess ? GOOD_RETURN : BAD_RETURN;
#else
bool bSuccess = true;
int nBlockSize = 8;
float* pBlock = new float [nBlockSize*nBlockSize];
// Loop over blocks
int nBlockRows = nImageH / nBlockSize;
int nBlockCols = nImageW / nBlockSize;
for (int nBlockRow = 0; nBlockRow < nBlockRows; nBlockRow++) {
for (int nBlockCol = 0; nBlockCol < nBlockCols; nBlockCol++) {
// Copy from source image into block
for (int nRow = 0; nRow < nBlockSize; nRow++) {
int nRowReal = nBlockRow*nBlockSize + nRow;
for (int nCol = 0; nCol < nBlockSize; nCol++) {
int nColReal = nBlockCol*nBlockSize + nCol;
pBlock[nRow*nBlockSize+nCol] = pSrc[nRowReal*nImageW+nColReal];
}
}
// Dequantize
for (int i = 0; i < nBlockSize*nBlockSize; i++)
{
pBlock[i] = pBlock[i]*pTable[i];
}
// Perform 8x8 inverse DCT
ChenImageDCT_DCT8x8Inv_32f_I( nBlockSize, pBlock );
// Copy from block into destination image
for (int nRow = 0; nRow < nBlockSize; nRow++) {
int nRowReal = nBlockRow*nBlockSize + nRow;
for (int nCol = 0; nCol < nBlockSize; nCol++) {
int nColReal = nBlockCol*nBlockSize + nCol;
pDst[nRowReal*nImageW+nColReal] = pBlock[nRow*nBlockSize+nCol];
}
}
} // end nBlockCol
} // end nBlockRow
if (pBlock) delete [] pBlock;
return bSuccess ? GOOD_RETURN : BAD_RETURN;
#endif
}
int ChenImageDCT_blockDCTQuantize(const int nBlockSize, const int nImageW, const int nImageH, const short* pSrc, const short* pTable, short* pDstQuant)
{
#ifdef _USEIPP
bool bSuccess = true;
int nPixels = nImageW * nImageH;
float* pDstQuantF = new float[nPixels];
float* pSrcF = new float[nPixels];
RECORD_IPP_SUCCESS( ippsConvert_16s32f((const Ipp16s*)pSrc, (Ipp32f*)pSrcF, nPixels), bSuccess );
RECORD_SUCCESS( ChenImageDCT_blockDCTQuantize(nBlockSize, nImageW, nImageH, pSrcF, pTable, pDstQuantF), bSuccess );
IppRoundMode rndMode = ippRndNear;
int nScaleFactor = 0;
RECORD_IPP_SUCCESS( ippsConvert_32f16s_Sfs((const Ipp32f*)pDstQuantF, (Ipp16s*)pDstQuant, nPixels, rndMode, nScaleFactor), bSuccess );
delete [] pDstQuantF;
delete [] pSrcF;
return bSuccess ? GOOD_RETURN : BAD_RETURN;
#else
bool bSuccess = true;
int nPixels = nImageW * nImageH;
float* pSrcF = new float[nPixels];
float* pDstQuantF = new float[nPixels];
if ((pSrcF != NULL) && (pDstQuantF != NULL))
{
for (int i = 0; i < nPixels; i++)
{
pSrcF[i] = (float)(pSrc[i]);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -