forked from jwetzl/MAPSuperresolution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageIO.cpp
147 lines (116 loc) · 3.45 KB
/
ImageIO.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* ___ _ _ ___ _ __ __ _ ___
* / __| | | | \ /_\ | \/ | /_\ | _ \
* | (__| |_| | |) / _ \ | |\/| |/ _ \| _/
* \___|\___/|___/_/_\_\_|_|__|_/_/_\_\_|_ ___
* / __| | | | _ \ __| _ \___| _ \ __/ __|
* \__ \ |_| | _/ _|| /___| / _|\__ \
* |___/\___/|_| |___|_|_\ |_|_\___|___/
* 2012
*
* by Jens Wetzl ([email protected])
* and Oliver Taubmann ([email protected])
*
* This work is licensed under a Creative Commons
* Attribution 3.0 Unported License. (CC-BY)
* http://creativecommons.org/licenses/by/3.0/
*
**/
#include "ImageIO.h"
#include "cudalbfgs_error_checking.h"
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
ImagePtr ImageIO::loadGrayscaleImage(const char *filename)
{
ImagePtr image = FreeImage_Load(FIF_PNG, filename);
ImagePtr imGrayscale;
FREE_IMAGE_COLOR_TYPE type = FreeImage_GetColorType(image);
switch(type)
{
case FIC_MINISBLACK:
imGrayscale = image;
break;
case FIC_RGB:
case FIC_RGBALPHA:
imGrayscale = FreeImage_ConvertToGreyscale(image);
FreeImage_Unload(image);
break;
default:
cerr << "Error: Image type unsupported" << endl;
exit(EXIT_FAILURE);
}
cout << "Loaded " << filename << "." << endl;
return imGrayscale;
}
void ImageIO::saveGPUImage(const std::string &filename, const float *d_image,
const size_t width, const size_t height, const size_t ld)
{
const size_t pixels = ld * height;
float *h_image = new float[pixels];
CudaSafeCall( cudaMemcpy(h_image, d_image, pixels * sizeof(float),
cudaMemcpyDeviceToHost) );
if (filename.substr(filename.size() - 3) == "txt")
{
// Output TXT
ofstream file(filename.c_str(), ios_base::out | ios_base::trunc);
file << setprecision(4);
for (size_t h = 0; h < height; ++h)
{
file << h_image[h * ld];
for (size_t w = 1; w < width; ++w)
{
file << "," << h_image[h*ld + w];
}
file << endl;
}
}
else
{
// Output PNG
ImagePtr out = FreeImage_Allocate(width, height, 8);
if (!out)
{
cerr << "Error: Couldn't allocate output image." << endl;
exit(EXIT_FAILURE);
}
for (size_t y = 0; y < height; ++y)
{
BYTE *line = FreeImage_GetScanLine(out, y);
float *fline = h_image + (height - y - 1) * ld;
for (size_t x = 0; x < width; ++x)
{
int val = (int)floor(fline[x] * 255.0f + 0.5f);
line[x] = (BYTE)max(0, min(255, val));
}
}
FreeImage_Save(FIF_PNG, out, filename.c_str());
FreeImage_Unload(out);
}
delete [] h_image;
}
float *ImageIO::uploadImage(const ImagePtr &img, size_t &width, size_t &height, size_t &ld)
{
ImageDims dims(img);
const size_t sz = 8;
width = dims.width;
height = dims.height;
ld = (dims.width % sz == 0) ? dims.width : ((dims.width / sz) + 1) * sz;
// height = (dims.height % sz == 0) ? dims.height : ((dims.height / sz) + 1) * sz;
const size_t numPixels = ld * height;
float *h_img = new float[numPixels]();
for (size_t y = dims.height; y > 0; --y)
{
BYTE *line = FreeImage_GetScanLine(img, y-1);
for (size_t x = 0; x < dims.width; ++x)
h_img[(height-y) * ld + x] = float(line[x]) / 255.0f;
}
float *d_img;
CudaSafeCall( cudaMalloc((void**) &d_img,
numPixels * sizeof(float)) );
CudaSafeCall( cudaMemcpy(d_img, h_img, numPixels * sizeof(float),
cudaMemcpyHostToDevice) );
delete [] h_img;
return d_img;
}