2 * Copyright 1993-2013 NVIDIA Corporation. All rights reserved.
4 * Please refer to the NVIDIA end user license agreement (EULA) associated
5 * with this source code for terms and conditions that govern your use of
6 * this software. Any use, reproduction, disclosure, or distribution of
7 * this software and related documentation outside the terms of the EULA
8 * is strictly prohibited.
12 #ifndef NV_UTIL_NPP_IMAGE_IO_H
13 #define NV_UTIL_NPP_IMAGE_IO_H
15 #include "ImagesCPU.h"
16 #include "ImagesNPP.h"
18 #include "FreeImage.h"
19 #include "Exceptions.h"
25 // Error handler for FreeImage library.
26 // In case this handler is invoked, it throws an NPP exception.
28 FreeImageErrorHandler(FREE_IMAGE_FORMAT oFif, const char *zMessage)
30 throw npp::Exception(zMessage);
35 // Load a gray-scale image from disk.
37 loadImage(const std::string &rFileName, ImageCPU_8u_C1 &rImage)
39 // set your own FreeImage error handler
40 FreeImage_SetOutputMessage(FreeImageErrorHandler);
42 FREE_IMAGE_FORMAT eFormat = FreeImage_GetFileType(rFileName.c_str());
44 // no signature? try to guess the file format from the file extension
45 if (eFormat == FIF_UNKNOWN)
47 eFormat = FreeImage_GetFIFFromFilename(rFileName.c_str());
50 NPP_ASSERT(eFormat != FIF_UNKNOWN);
51 // check that the plugin has reading capabilities ...
54 if (FreeImage_FIFSupportsReading(eFormat))
56 pBitmap = FreeImage_Load(eFormat, rFileName.c_str());
59 NPP_ASSERT(pBitmap != 0);
60 // make sure this is an 8-bit single channel image
61 NPP_ASSERT(FreeImage_GetColorType(pBitmap) == FIC_MINISBLACK);
62 NPP_ASSERT(FreeImage_GetBPP(pBitmap) == 8);
64 // create an ImageCPU to receive the loaded image data
65 ImageCPU_8u_C1 oImage(FreeImage_GetWidth(pBitmap), FreeImage_GetHeight(pBitmap));
67 // Copy the FreeImage data into the new ImageCPU
68 unsigned int nSrcPitch = FreeImage_GetPitch(pBitmap);
69 const Npp8u *pSrcLine = FreeImage_GetBits(pBitmap) + nSrcPitch * (FreeImage_GetHeight(pBitmap) -1);
70 Npp8u *pDstLine = oImage.data();
71 unsigned int nDstPitch = oImage.pitch();
73 for (size_t iLine = 0; iLine < oImage.height(); ++iLine)
75 memcpy(pDstLine, pSrcLine, oImage.width() * sizeof(Npp8u));
76 pSrcLine -= nSrcPitch;
77 pDstLine += nDstPitch;
80 // swap the user given image with our result image, effecively
81 // moving our newly loaded image data into the user provided shell
85 // Save an gray-scale image to disk.
87 saveImage(const std::string &rFileName, const ImageCPU_8u_C1 &rImage)
89 // create the result image storage using FreeImage so we can easily
91 FIBITMAP *pResultBitmap = FreeImage_Allocate(rImage.width(), rImage.height(), 8 /* bits per pixel */);
92 NPP_ASSERT_NOT_NULL(pResultBitmap);
93 unsigned int nDstPitch = FreeImage_GetPitch(pResultBitmap);
94 Npp8u *pDstLine = FreeImage_GetBits(pResultBitmap) + nDstPitch * (rImage.height()-1);
95 const Npp8u *pSrcLine = rImage.data();
96 unsigned int nSrcPitch = rImage.pitch();
98 for (size_t iLine = 0; iLine < rImage.height(); ++iLine)
100 memcpy(pDstLine, pSrcLine, rImage.width() * sizeof(Npp8u));
101 pSrcLine += nSrcPitch;
102 pDstLine -= nDstPitch;
105 // now save the result image
107 bSuccess = FreeImage_Save(FIF_PGM, pResultBitmap, rFileName.c_str(), 0) == TRUE;
108 NPP_ASSERT_MSG(bSuccess, "Failed to save result image.");
111 // Load a gray-scale image from disk.
113 loadImage(const std::string &rFileName, ImageNPP_8u_C1 &rImage)
115 ImageCPU_8u_C1 oImage;
116 loadImage(rFileName, oImage);
117 ImageNPP_8u_C1 oResult(oImage);
118 rImage.swap(oResult);
121 // Save an gray-scale image to disk.
123 saveImage(const std::string &rFileName, const ImageNPP_8u_C1 &rImage)
125 ImageCPU_8u_C1 oHostImage(rImage.size());
126 // copy the device result data
127 rImage.copyTo(oHostImage.data(), oHostImage.pitch());
128 saveImage(rFileName, oHostImage);
133 #endif // NV_UTIL_NPP_IMAGE_IO_H