Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern "C" _declspec(dllexport) void _stdcall
- nppiResizeImage(uint8_t * inputImage, uint8_t * outputImage, int imageWidth, int imageHeight, double xFactor, double yFactor);
- extern "C" _declspec(dllexport) NppStatus _stdcall
- nppEmphasize(Npp8u* inputImage, Npp8u* outImage, unsigned int width, unsigned int height, int maskWidth, int maskHeight, double factor);
- /*========================================================================================================================*/
- NppStatus nppEmphasize(Npp8u* inputImage, Npp8u* outImage, unsigned int width, unsigned int height, int maskWidth, int maskHeight, double factor)
- {
- //Image Size.
- int totalPixels = width * height;
- Npp8u* newimg;
- cudaMallocHost(&newimg, totalPixels * sizeof(Npp8u));
- NppStatus Status = NPP_NO_ERROR;
- cudaError_t cudaerror;
- maskHeight = (maskHeight & 0x00000001) ? maskHeight : maskHeight + 1;
- maskWidth = (maskWidth & 0x00000001) ? maskWidth : maskWidth + 1;
- //CudaStream
- NppStreamContext nppStreamCtx;
- nppGetStreamContext(&nppStreamCtx);
- //Mask & Origin for CUDA.
- NppiSize maskSize;
- maskSize.height = maskHeight;
- maskSize.width = maskWidth;
- NppiPoint cudaAnchor;
- cudaAnchor.x = (maskWidth) / 2;
- cudaAnchor.y = (maskHeight) / 2;
- //ROI for CUDA.
- NppiSize RoiSize;
- RoiSize.height = height;
- RoiSize.width = width;
- //IntStep
- int nIntStep = width;
- Npp32f* maskKernel = nppsMalloc_32f(maskWidth * maskHeight);
- nppsSet_32f(1.0 / (maskWidth * maskHeight), maskKernel, maskWidth * maskHeight);
- //Allocate mem on device.
- Npp8u *deviceSrcPixel, *deviceDstPixel, *deviceSubDstPixel;
- cudaerror = cudaMallocManaged(&deviceSrcPixel, totalPixels * sizeof(Npp8u));
- cudaerror = cudaMallocManaged(&deviceDstPixel, totalPixels * sizeof(Npp8u));
- cudaerror = cudaMallocManaged(&deviceSubDstPixel, totalPixels * sizeof(Npp8u));
- //Copy host image to device.
- cudaerror = cudaMemcpy(deviceSrcPixel, inputImage, totalPixels * sizeof(Npp8u), cudaMemcpyHostToDevice);
- Status = nppiFilter32f_8u_C1R_Ctx(deviceSrcPixel, nIntStep, deviceDstPixel, nIntStep,
- RoiSize, maskKernel, maskSize, cudaAnchor, nppStreamCtx);
- Status = nppiSub_8u_C1RSfs_Ctx(deviceSrcPixel, nIntStep, deviceDstPixel, nIntStep, deviceSubDstPixel, nIntStep, RoiSize, 0, nppStreamCtx);
- Status = nppiMulC_8u_C1IRSfs_Ctx(factor, deviceSubDstPixel, nIntStep, RoiSize, 0, nppStreamCtx);
- Status = nppiAdd_8u_C1RSfs_Ctx(deviceSrcPixel, nIntStep, deviceSubDstPixel, nIntStep, deviceDstPixel, nIntStep, RoiSize, 0, nppStreamCtx);
- cudaDeviceSynchronize();
- //Copy resultant back to host.
- cudaerror = cudaMemcpy(newimg, deviceDstPixel, totalPixels * sizeof(Npp8u), cudaMemcpyDeviceToHost);
- std::memcpy(outImage, newimg, totalPixels * sizeof(Npp8u));
- cudaFree(deviceDstPixel);
- cudaFree(deviceSrcPixel);
- cudaFree(deviceSubDstPixel);
- cudaFreeHost(newimg);
- nppsFree(maskKernel);
- Error:
- cudaFree(deviceDstPixel);
- cudaFree(deviceSrcPixel);
- cudaFree(deviceSubDstPixel);
- cudaFreeHost(newimg);
- nppsFree(maskKernel);
- return Status;
- }
- void nppiResizeImage(uint8_t * inputImage, uint8_t * outputImage, int imageWidth, int imageHeight, double xFactor, double yFactor)
- {
- int totalPixels = imageWidth * imageHeight;
- int nIntStep = imageWidth;
- //Rect info
- NppiSize roiSize = { imageWidth,imageHeight };
- NppiRect inputRect, outputRect;
- NppiRect destRect;
- inputRect.x = 0;
- inputRect.y = 0;
- inputRect.width = imageWidth;
- inputRect.height = imageHeight;
- outputRect.x = 0;
- outputRect.y = 0;
- outputRect.width = imageWidth * xFactor;
- outputRect.height = imageHeight * yFactor;
- //NppiSize roiDstize = { outputRect.width,outputRect.height };
- //Allocate mem on device.
- int dstTotalPixels = outputRect.width* outputRect.height;
- Npp8u *deviceSrcPixel, *deviceDstPixel;
- cudaMalloc(&deviceSrcPixel, totalPixels * sizeof(Npp8u));
- cudaMalloc(&deviceDstPixel, dstTotalPixels * sizeof(Npp8u));
- cudaMemcpy(deviceSrcPixel, inputImage, totalPixels * sizeof(Npp8u), cudaMemcpyHostToDevice);
- Npp8u* dstImage;
- cudaMallocHost(&dstImage, dstTotalPixels * sizeof(Npp8u));
- nppiResizeSqrPixel_8u_C1R(deviceSrcPixel, roiSize, nIntStep, inputRect, deviceDstPixel, outputRect.width, outputRect, xFactor, yFactor, 0, 0, NPPI_INTER_LINEAR);
- //nppiResize_8u_C1R(deviceSrcPixel, nIntStep, roiSize, inputRect, deviceDstPixel, nIntStep*xFactor, roiDstize, outputRect, NPPI_INTER_NN);
- cudaDeviceSynchronize();
- cudaMemcpy(outputImage, deviceDstPixel, dstTotalPixels * sizeof(Npp8u), cudaMemcpyDeviceToHost);
- cudaFree(deviceSrcPixel);
- cudaFree(deviceDstPixel);
- cudaFreeHost(dstImage);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement