1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26
27 #include "ImageIO.h"
28
29 #include <unordered_map>
30 #include <Core/Utility/Console.h>
31 #include <Core/Utility/FileSystem.h>
32
33 namespace three{
34
35 namespace {
36
37 static const std::unordered_map<std::string,
38 std::function<bool(const std::string &, Image &)>>
39 file_extension_to_image_read_function
40 {{"png", ReadImageFromPNG},
41 {"jpg", ReadImageFromJPG},
42 {"jpeg", ReadImageFromJPG},
43 };
44
45 static const std::unordered_map<std::string,
46 std::function<bool(const std::string &, const Image &, int)>>
47 file_extension_to_image_write_function
48 {{"png", WriteImageToPNG},
49 {"jpg", WriteImageToJPG},
50 {"jpeg", WriteImageToJPG},
51 };
52
53 } // unnamed namespace
54
CreateImageFromFile(const std::string & filename)55 std::shared_ptr<Image> CreateImageFromFile(const std::string &filename)
56 {
57 auto image = std::make_shared<Image>();
58 ReadImage(filename, *image);
59 return image;
60 }
61
ReadImage(const std::string & filename,Image & image)62 bool ReadImage(const std::string &filename, Image &image)
63 {
64 std::string filename_ext =
65 filesystem::GetFileExtensionInLowerCase(filename);
66 if (filename_ext.empty()) {
67 PrintWarning("Read Image failed: unknown file extension.\n");
68 return false;
69 }
70 auto map_itr = file_extension_to_image_read_function.find(filename_ext);
71 if (map_itr == file_extension_to_image_read_function.end()) {
72 PrintWarning("Read Image failed: unknown file extension.\n");
73 return false;
74 }
75 return map_itr->second(filename, image);
76 }
77
WriteImage(const std::string & filename,const Image & image,int quality)78 bool WriteImage(const std::string &filename, const Image &image,
79 int quality/* = 90*/)
80 {
81 std::string filename_ext =
82 filesystem::GetFileExtensionInLowerCase(filename);
83 if (filename_ext.empty()) {
84 PrintWarning("Write Image failed: unknown file extension.\n");
85 return false;
86 }
87 auto map_itr = file_extension_to_image_write_function.find(filename_ext);
88 if (map_itr == file_extension_to_image_write_function.end()) {
89 PrintWarning("Write Image failed: unknown file extension.\n");
90 return false;
91 }
92 return map_itr->second(filename, image, quality);
93 }
94
95 } // namespace three
96