Skip to content

Instantly share code, notes, and snippets.

@homm
Created July 7, 2017 14:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save homm/9b35398e7e105a3c886ab1d60bf598dd to your computer and use it in GitHub Desktop.
Save homm/9b35398e7e105a3c886ab1d60bf598dd to your computer and use it in GitHub Desktop.
/*
// Copyright 2012 2016 Intel Corporation All Rights Reserved.
//
// The source code, information and material ("Material") contained herein is
// owned by Intel Corporation or its suppliers or licensors, and title
// to such Material remains with Intel Corporation or its suppliers or
// licensors. The Material contains proprietary information of Intel
// or its suppliers and licensors. The Material is protected by worldwide
// copyright laws and treaty provisions. No part of the Material may be used,
// copied, reproduced, modified, published, uploaded, posted, transmitted,
// distributed or disclosed in any way without Intel's prior express written
// permission. No license under any patent, copyright or other intellectual
// property rights in the Material is granted to or conferred upon you,
// either expressly, by implication, inducement, estoppel or otherwise.
// Any license under such intellectual property rights must be express and
// approved by Intel in writing.
//
// Unless otherwise agreed by Intel in writing,
// you may not remove or alter this notice or any other notice embedded in
// Materials by Intel or Intel's suppliers or licensors in any way.
//
*/
#include <math.h>
#include <memory>
#include "base.h"
#include "base_image.h"
#include "base_ipp.h"
#include "ippcore.h"
#include "ipps.h"
#include "ippi.h"
static void printVersion()
{
const IppLibraryVersion *pVersion;
printf("\nIntel(R) IPP:\n");
PRINT_LIB_VERSION( , pVersion)
PRINT_LIB_VERSION(s, pVersion)
PRINT_LIB_VERSION(i, pVersion)
}
static void printHelp(const OptDef pOptions[], char* argv[])
{
printf("\nUsage: %s [-i] InputFile [Options]\n", GetProgName(argv));
printf("Options:\n");
OptUsage(pOptions);
}
class Resize
{
public:
Resize()
{
m_pSpec = 0;
m_pInitBuffer = 0;
}
virtual ~Resize()
{
Close();
}
void Close()
{
if(m_pSpec)
{
ippsFree(m_pSpec);
m_pSpec = 0;
}
if(m_pInitBuffer)
{
ippsFree(m_pInitBuffer);
m_pInitBuffer = 0;
}
}
virtual Status Init(Image *pSrcImage, Image *pDstImage,
IppiInterpolationType interpolation, float bVal, float cVal)
{
IppiBorderSize borderSize;
int iSpecSize = 0;
int iInitSize = 0;
if(!pSrcImage || !pSrcImage->ptr() || !pDstImage)
return STS_ERR_NULL_PTR;
if(pSrcImage->m_samples != 1 && pSrcImage->m_samples != 3 && pSrcImage->m_samples != 4)
return STS_ERR_INVALID_PARAMS;
Close();
IppiSize srcSize = {(int) pSrcImage->m_size.width, (int) pSrcImage->m_size.height};
IppiSize dstSize = {(int) pDstImage->m_size.width, (int) pDstImage->m_size.height};
ippiResizeGetSize_8u(srcSize, dstSize, interpolation, 1, &iSpecSize, &iInitSize);
m_pSpec = (IppiResizeSpec_32f*)ippsMalloc_8u(iSpecSize);
if (iInitSize)
m_pInitBuffer = ippsMalloc_8u(iInitSize);
if(interpolation == ippLinear)
ippiResizeAntialiasingLinearInit(srcSize, dstSize, m_pSpec, m_pInitBuffer);
else if(interpolation == ippCubic)
ippiResizeAntialiasingCubicInit(srcSize, dstSize, bVal, cVal, m_pSpec, m_pInitBuffer);
else if(interpolation == ippLanczos)
ippiResizeAntialiasingLanczosInit(srcSize, dstSize, 3, m_pSpec, m_pInitBuffer);
ippiResizeGetBorderSize_8u(m_pSpec, &borderSize);
return STS_OK;
}
virtual Status ResizeImage(Image *pSrcImage, Image *pDstImage)
{
IppiPoint dstRoiOffset = {0, 0};
IppiSize dstRoiSize = {(int)pDstImage->m_size.width, (int)pDstImage->m_size.height};
unsigned char *pSrcPtr = 0;
unsigned char *pDstPtr = 0;
unsigned char *pBuffer = 0;
int iBufferSize = 0;
// adjust input and output buffers to current ROI
pSrcPtr = (unsigned char*)pSrcImage->ptr();
pDstPtr = (unsigned char*)pDstImage->ptr();
ippiResizeGetBufferSize_8u(m_pSpec, dstRoiSize, pSrcImage->m_samples, &iBufferSize);
pBuffer = ippsMalloc_8u(iBufferSize);
// perform resize
if(pSrcImage->m_samples == 1)
ippiResizeAntialiasing_8u_C1R(
pSrcPtr, (int)pSrcImage->m_step, pDstPtr, (int)pDstImage->m_step,
dstRoiOffset, dstRoiSize, ippBorderRepl, 0, m_pSpec, pBuffer);
else if(pSrcImage->m_samples == 3)
ippiResizeAntialiasing_8u_C3R(
pSrcPtr, (int)pSrcImage->m_step, pDstPtr, (int)pDstImage->m_step,
dstRoiOffset, dstRoiSize, ippBorderRepl, 0, m_pSpec, pBuffer);
else if(pSrcImage->m_samples == 4)
ippiResizeAntialiasing_8u_C4R(
pSrcPtr, (int)pSrcImage->m_step, pDstPtr, (int)pDstImage->m_step,
dstRoiOffset, dstRoiSize, ippBorderRepl, 0, m_pSpec, pBuffer);
ippsFree(pBuffer);
return STS_OK;
}
protected:
IppiResizeSpec_32f *m_pSpec;
unsigned char *m_pInitBuffer;
};
void
easy_resize(Image *pSrcImage, Image *pDstImage, int width, int height,
IppiInterpolationType interpolation)
{
Resize *pResize = 0;
pDstImage->m_size.width = width;
pDstImage->m_size.height = height;
pDstImage->Alloc();
pResize = new Resize;
pResize->Init(pSrcImage, pDstImage, interpolation, 0, 0.5);
pResize->ResizeImage(pSrcImage, pDstImage);
delete pResize;
}
int main(int argc, char *argv[])
{
DString sInputFile = "";
unsigned int iLoopsLimit = 1;
char* sIppCpu = 0;
Image srcData;
OptDef cmdOpts[] = {
{ 'i', 1, KT_DSTRING, 0, &sInputFile, "input file name" },
{ 'l', 1, KT_POSITIVE, 0, &iLoopsLimit, "number of loops (overrides test time)" },
{ 'T', 1, KT_STRING, 0, &sIppCpu, "target Intel IPP optimization (SSE, SSE2, SSE3, SSSE3, SSE41, SSE42, AES, AVX, AVX2)" },
{0}
};
if (OptParse(argc, argv, cmdOpts))
{
printHelp(cmdOpts, argv);
PRINT_MESSAGE("invalid input parameters");
return 1;
}
InitPreferredCpu(sIppCpu);
printVersion();
if(!sInputFile.Size())
{
printHelp(cmdOpts, argv);
PRINT_MESSAGE("Cannot open input file");
return 1;
}
// Read from file
printf("\nInput file: %s\n", sInputFile.c_str());
Status status = srcData.Read(sInputFile.c_str());
CHECK_STATUS_PRINT_RS(status, "Image::Read()", GetBaseStatusString(status));
srcData.ConvertColor(CF_BGRA, &srcData);
printf("Input info: %dx%d %s\n\n", (int)srcData.m_size.width, (int)srcData.m_size.height, colorFormatName[srcData.m_color]);
Image dstData = srcData;
IppiInterpolationType FILTERS[3] = {ippLinear, ippCubic, ippLanczos};
float SCALES[4] = {0.01, 0.125, 0.8, 2.14};
for (int j = 0; j < 4; ++j)
{
int width = (int)(SCALES[j] * srcData.m_size.width + .5);
int height = (int)(SCALES[j] * srcData.m_size.height + .5);
for (int i = 0; i < 3; ++i)
{
vm_tick tickAcc = 0;
for(int iLoops = 1; iLoops <= iLoopsLimit; iLoops++)
{
vm_tick tickStart = vm_time_get_tick();
easy_resize(&srcData, &dstData, width, height, FILTERS[i]);
tickAcc += (vm_time_get_tick() - tickStart);
}
double fTime = (double)tickAcc / vm_time_get_frequency();
printf("%dx%d %d avg: %0.6f\n",
width, height, i + 1, fTime/iLoopsLimit);
// char fn[50];
// sprintf(fn, "road.%dx%d.%d.bmp", width, height, i + 1);
// dstData.Write(fn);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment