OSDN Git Service

new file: Integration/Tomography/Makefile.recent
[eos/hostdependX86LINUX64.git] / hostdepend / X86MAC64 / util / X86MAC64 / cuda / samples / 7_CUDALibraries / common / UtilNPP / ImageIO.h
1 /**
2  * Copyright 1993-2013 NVIDIA Corporation.  All rights reserved.
3  *
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.
9  *
10  */
11
12 #ifndef NV_UTIL_NPP_IMAGE_IO_H
13 #define NV_UTIL_NPP_IMAGE_IO_H
14
15 #include "ImagesCPU.h"
16 #include "ImagesNPP.h"
17
18 #include "FreeImage.h"
19 #include "Exceptions.h"
20
21 #include <string>
22 #include "string.h"
23
24
25 // Error handler for FreeImage library.
26 //  In case this handler is invoked, it throws an NPP exception.
27 void
28 FreeImageErrorHandler(FREE_IMAGE_FORMAT oFif, const char *zMessage)
29 {
30     throw npp::Exception(zMessage);
31 }
32
33 namespace npp
34 {
35     // Load a gray-scale image from disk.
36     void
37     loadImage(const std::string &rFileName, ImageCPU_8u_C1 &rImage)
38     {
39         // set your own FreeImage error handler
40         FreeImage_SetOutputMessage(FreeImageErrorHandler);
41
42         FREE_IMAGE_FORMAT eFormat = FreeImage_GetFileType(rFileName.c_str());
43
44         // no signature? try to guess the file format from the file extension
45         if (eFormat == FIF_UNKNOWN)
46         {
47             eFormat = FreeImage_GetFIFFromFilename(rFileName.c_str());
48         }
49
50         NPP_ASSERT(eFormat != FIF_UNKNOWN);
51         // check that the plugin has reading capabilities ...
52         FIBITMAP *pBitmap;
53
54         if (FreeImage_FIFSupportsReading(eFormat))
55         {
56             pBitmap = FreeImage_Load(eFormat, rFileName.c_str());
57         }
58
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);
63
64         // create an ImageCPU to receive the loaded image data
65         ImageCPU_8u_C1 oImage(FreeImage_GetWidth(pBitmap), FreeImage_GetHeight(pBitmap));
66
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();
72
73         for (size_t iLine = 0; iLine < oImage.height(); ++iLine)
74         {
75             memcpy(pDstLine, pSrcLine, oImage.width() * sizeof(Npp8u));
76             pSrcLine -= nSrcPitch;
77             pDstLine += nDstPitch;
78         }
79
80         // swap the user given image with our result image, effecively
81         // moving our newly loaded image data into the user provided shell
82         oImage.swap(rImage);
83     }
84
85     // Save an gray-scale image to disk.
86     void
87     saveImage(const std::string &rFileName, const ImageCPU_8u_C1 &rImage)
88     {
89         // create the result image storage using FreeImage so we can easily
90         // save
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();
97
98         for (size_t iLine = 0; iLine < rImage.height(); ++iLine)
99         {
100             memcpy(pDstLine, pSrcLine, rImage.width() * sizeof(Npp8u));
101             pSrcLine += nSrcPitch;
102             pDstLine -= nDstPitch;
103         }
104
105         // now save the result image
106         bool bSuccess;
107         bSuccess = FreeImage_Save(FIF_PGM, pResultBitmap, rFileName.c_str(), 0) == TRUE;
108         NPP_ASSERT_MSG(bSuccess, "Failed to save result image.");
109     }
110
111     // Load a gray-scale image from disk.
112     void
113     loadImage(const std::string &rFileName, ImageNPP_8u_C1 &rImage)
114     {
115         ImageCPU_8u_C1 oImage;
116         loadImage(rFileName, oImage);
117         ImageNPP_8u_C1 oResult(oImage);
118         rImage.swap(oResult);
119     }
120
121     // Save an gray-scale image to disk.
122     void
123     saveImage(const std::string &rFileName, const ImageNPP_8u_C1 &rImage)
124     {
125         ImageCPU_8u_C1 oHostImage(rImage.size());
126         // copy the device result data
127         rImage.copyTo(oHostImage.data(), oHostImage.pitch());
128         saveImage(rFileName, oHostImage);
129     }
130 }
131
132
133 #endif // NV_UTIL_NPP_IMAGE_IO_H