Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.55 KB | None | 0 0
  1. extern "C" _declspec(dllexport) void _stdcall
  2. nppiResizeImage(uint8_t * inputImage, uint8_t * outputImage, int imageWidth, int imageHeight, double xFactor, double yFactor);
  3.  
  4. extern "C" _declspec(dllexport) NppStatus _stdcall
  5. nppEmphasize(Npp8u* inputImage, Npp8u* outImage, unsigned int width, unsigned int height, int maskWidth, int maskHeight, double factor);
  6.  
  7. /*========================================================================================================================*/
  8. NppStatus nppEmphasize(Npp8u* inputImage, Npp8u* outImage, unsigned int width, unsigned int height, int maskWidth, int maskHeight, double factor)
  9. {
  10.     //Image Size.
  11.     int totalPixels = width * height;
  12.  
  13.     Npp8u* newimg;
  14.     cudaMallocHost(&newimg, totalPixels * sizeof(Npp8u));
  15.  
  16.     NppStatus Status = NPP_NO_ERROR;
  17.     cudaError_t cudaerror;
  18.     maskHeight = (maskHeight & 0x00000001) ? maskHeight : maskHeight + 1;
  19.     maskWidth = (maskWidth & 0x00000001) ? maskWidth : maskWidth + 1;
  20.  
  21.     //CudaStream
  22.     NppStreamContext nppStreamCtx;
  23.     nppGetStreamContext(&nppStreamCtx);
  24.  
  25.  
  26.     //Mask & Origin for CUDA.
  27.     NppiSize maskSize;
  28.     maskSize.height = maskHeight;
  29.     maskSize.width = maskWidth;
  30.  
  31.     NppiPoint cudaAnchor;
  32.     cudaAnchor.x = (maskWidth) / 2;
  33.     cudaAnchor.y = (maskHeight) / 2;
  34.  
  35.     //ROI for CUDA.
  36.     NppiSize RoiSize;
  37.     RoiSize.height = height;
  38.     RoiSize.width = width;
  39.  
  40.  
  41.     //IntStep
  42.     int nIntStep = width;
  43.  
  44.     Npp32f* maskKernel = nppsMalloc_32f(maskWidth * maskHeight);
  45.     nppsSet_32f(1.0 / (maskWidth * maskHeight), maskKernel, maskWidth * maskHeight);
  46.  
  47.     //Allocate mem on device.
  48.     Npp8u *deviceSrcPixel, *deviceDstPixel, *deviceSubDstPixel;
  49.     cudaerror = cudaMallocManaged(&deviceSrcPixel, totalPixels * sizeof(Npp8u));
  50.     cudaerror = cudaMallocManaged(&deviceDstPixel, totalPixels * sizeof(Npp8u));
  51.     cudaerror = cudaMallocManaged(&deviceSubDstPixel, totalPixels * sizeof(Npp8u));
  52.  
  53.     //Copy host image to device.
  54.     cudaerror = cudaMemcpy(deviceSrcPixel, inputImage, totalPixels * sizeof(Npp8u), cudaMemcpyHostToDevice);
  55.  
  56.  
  57.     Status = nppiFilter32f_8u_C1R_Ctx(deviceSrcPixel, nIntStep, deviceDstPixel, nIntStep,
  58.         RoiSize, maskKernel, maskSize, cudaAnchor, nppStreamCtx);
  59.     Status = nppiSub_8u_C1RSfs_Ctx(deviceSrcPixel, nIntStep, deviceDstPixel, nIntStep, deviceSubDstPixel, nIntStep, RoiSize, 0, nppStreamCtx);
  60.     Status = nppiMulC_8u_C1IRSfs_Ctx(factor, deviceSubDstPixel, nIntStep, RoiSize, 0, nppStreamCtx);
  61.     Status = nppiAdd_8u_C1RSfs_Ctx(deviceSrcPixel, nIntStep, deviceSubDstPixel, nIntStep, deviceDstPixel, nIntStep, RoiSize, 0, nppStreamCtx);
  62.     cudaDeviceSynchronize();
  63.     //Copy resultant back to host.
  64.     cudaerror = cudaMemcpy(newimg, deviceDstPixel, totalPixels * sizeof(Npp8u), cudaMemcpyDeviceToHost);
  65.     std::memcpy(outImage, newimg, totalPixels * sizeof(Npp8u));
  66.     cudaFree(deviceDstPixel);
  67.     cudaFree(deviceSrcPixel);
  68.     cudaFree(deviceSubDstPixel);
  69.     cudaFreeHost(newimg);
  70.     nppsFree(maskKernel);
  71. Error:
  72.     cudaFree(deviceDstPixel);
  73.     cudaFree(deviceSrcPixel);
  74.     cudaFree(deviceSubDstPixel);
  75.     cudaFreeHost(newimg);
  76.     nppsFree(maskKernel);
  77.     return Status;
  78. }
  79.  
  80. void nppiResizeImage(uint8_t * inputImage, uint8_t * outputImage, int imageWidth, int imageHeight, double xFactor, double yFactor)
  81. {
  82.     int totalPixels = imageWidth * imageHeight;
  83.     int nIntStep = imageWidth;
  84.  
  85.     //Rect info
  86.     NppiSize roiSize = { imageWidth,imageHeight };
  87.     NppiRect inputRect, outputRect;
  88.     NppiRect destRect;
  89.     inputRect.x = 0;
  90.     inputRect.y = 0;
  91.     inputRect.width = imageWidth;
  92.     inputRect.height = imageHeight;
  93.     outputRect.x = 0;
  94.     outputRect.y = 0;
  95.     outputRect.width = imageWidth * xFactor;
  96.     outputRect.height = imageHeight * yFactor;
  97.     //NppiSize roiDstize = { outputRect.width,outputRect.height };
  98.  
  99.  
  100.     //Allocate mem on device.
  101.     int dstTotalPixels = outputRect.width* outputRect.height;
  102.     Npp8u *deviceSrcPixel, *deviceDstPixel;
  103.     cudaMalloc(&deviceSrcPixel, totalPixels * sizeof(Npp8u));
  104.     cudaMalloc(&deviceDstPixel, dstTotalPixels * sizeof(Npp8u));
  105.     cudaMemcpy(deviceSrcPixel, inputImage, totalPixels * sizeof(Npp8u), cudaMemcpyHostToDevice);
  106.  
  107.     Npp8u* dstImage;
  108.     cudaMallocHost(&dstImage, dstTotalPixels * sizeof(Npp8u));
  109.     nppiResizeSqrPixel_8u_C1R(deviceSrcPixel, roiSize, nIntStep, inputRect, deviceDstPixel, outputRect.width, outputRect, xFactor, yFactor, 0, 0, NPPI_INTER_LINEAR);
  110.     //nppiResize_8u_C1R(deviceSrcPixel, nIntStep, roiSize, inputRect, deviceDstPixel, nIntStep*xFactor, roiDstize, outputRect, NPPI_INTER_NN);
  111.     cudaDeviceSynchronize();
  112.     cudaMemcpy(outputImage, deviceDstPixel, dstTotalPixels * sizeof(Npp8u), cudaMemcpyDeviceToHost);
  113.  
  114.     cudaFree(deviceSrcPixel);
  115.     cudaFree(deviceDstPixel);
  116.     cudaFreeHost(dstImage);
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement