/
image.cpp
96 lines (79 loc) · 3.67 KB
/
image.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
// ======================================================================== //
// Copyright 2009-2016 Intel Corporation //
// //
// Licensed under the Apache License, Version 2.0 (the "License"); //
// you may not use this file except in compliance with the License. //
// You may obtain a copy of the License at //
// //
// http://www.apache.org/licenses/LICENSE-2.0 //
// //
// Unless required by applicable law or agreed to in writing, software //
// distributed under the License is distributed on an "AS IS" BASIS, //
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
// See the License for the specific language governing permissions and //
// limitations under the License. //
// ======================================================================== //
#include "image.h"
#include "../../../common/sys/string.h"
#include <map>
#include <iostream>
namespace embree
{
/*! loads an image from a file with auto-detection of format */
Ref<Image> loadImageFromDisk(const FileName& fileName)
{
std::string ext = toLowerCase(fileName.ext());
#ifdef EMBREE_TUTORIALS_LIBPNG
if (ext == "png" ) return loadPNG(fileName);
#endif
#ifdef EMBREE_TUTORIALS_LIBJPEG
if (ext == "jpg" ) return loadJPEG(fileName);
#endif
#ifdef USE_IMAGEMAGICK
if (ext == "bmp" ) return loadMagick(fileName);
if (ext == "gif" ) return loadMagick(fileName);
if (ext == "tga" ) return loadMagick(fileName);
if (ext == "tif" ) return loadMagick(fileName);
if (ext == "tiff") return loadMagick(fileName);
if (ext == "png" ) return loadMagick(fileName);
if (ext == "jpg" ) return loadMagick(fileName);
#endif
if (ext == "pfm" ) return loadPFM(fileName);
if (ext == "ppm" ) return loadPPM(fileName);
THROW_RUNTIME_ERROR("image format " + ext + " not supported");
}
/*! loads an image from a file with auto-detection of format */
Ref<Image> loadImage(const FileName& fileName, bool cache)
{
static std::map<std::string,Ref<Image> > image_cache;
if (!cache)
return loadImageFromDisk(fileName);
if (image_cache.find(fileName) == image_cache.end())
image_cache[fileName] = loadImageFromDisk(fileName);
return image_cache[fileName];
}
/*! stores an image to file with auto-detection of format */
void storeImage(const Ref<Image>& img, const FileName& fileName)
{
std::string ext = toLowerCase(fileName.ext());
#ifdef EMBREE_TUTORIALS_LIBJPEG
if (ext == "jpg" ) { storeJPEG(img, fileName); return; }
#endif
#ifdef USE_IMAGEMAGICK
if (ext == "bmp" ) { storeMagick(img, fileName); return; }
if (ext == "gif" ) { storeMagick(img, fileName); return; }
if (ext == "tga" ) { storeMagick(img, fileName); return; }
if (ext == "tif" ) { storeMagick(img, fileName); return; }
if (ext == "tiff") { storeMagick(img, fileName); return; }
if (ext == "png" ) { storeMagick(img, fileName); return; }
if (ext == "jpg" ) { storeMagick(img, fileName); return; }
#endif
if (ext == "pfm" ) { storePFM(img, fileName); return; }
if (ext == "ppm" ) { storePPM(img, fileName); return; }
if (ext == "tga" ) { storeTga(img, fileName); return; }
THROW_RUNTIME_ERROR("image format " + ext + " not supported");
}
/*! template instantiations */
template class ImageT<Col3uc>;
template class ImageT<Col3f>;
}