Turi Create  4.0
io_impl.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #ifndef TURI_IMAGE_IMAGE_IO_IMPL_HPP
7 #define TURI_IMAGE_IMAGE_IO_IMPL_HPP
8 
9 #ifndef png_infopp_NULL
10 #define png_infopp_NULL (png_infopp)NULL
11 #endif
12 
13 #ifndef int_p_NULL
14 #define int_p_NULL (int*)NULL
15 #endif
16 
17 #include <core/data/image/image_type.hpp>
18 #include <boost/gil/extension/io/jpeg.hpp>
19 #include <boost/gil/extension/io/jpeg/old.hpp>
20 #include <boost/gil/extension/io/png.hpp>
21 #include <boost/gil/extension/io/png/old.hpp>
22 #include <core/logging/logger.hpp>
23 
24 using namespace boost::gil;
25 
26 namespace turi {
27 
28 template<typename pixel_type>
29 void write_image_impl(std::string filename, char* data, size_t& width, size_t& height, size_t& channels, Format format ) {
30  auto view = interleaved_view(width, height, (pixel_type*)data, width * channels * sizeof(char));
31  if (format == Format::JPG) {
32  jpeg_write_view(filename, view);
33  } else if (format == Format::PNG){
34  png_write_view(filename, view);
35  }
36 }
37 
38 // Template specialization: JPEG does not support RGBA
39 template<>
40 void write_image_impl<rgba8_pixel_t>(std::string filename, char* data, size_t& width, size_t& height, size_t& channels, Format format ) {
41  auto view = interleaved_view(width, height, (rgba8_pixel_t*)data, width * channels * sizeof(char));
42  if (format == Format::JPG) {
43  throw ("JPEG does not support RGBA color type");
44  } else if (format == Format::PNG){
45  png_write_view(filename, view);
46  }
47 }
48 
49 /**************************************************************************/
50 /* */
51 /* Boost Prototype Code, Not used in actual code */
52 /* */
53 /**************************************************************************/
54 template<typename pixel_type>
55 void boost_read_image_impl(std::string filename, char** data, size_t& width, size_t& height, size_t& channels, Format format ) {
56  char* buf = new char[width * height * channels];
57  auto view = interleaved_view(width, height, (pixel_type*)buf, width * channels * sizeof(char));
58  if (format == Format::JPG) {
59  jpeg_read_view(filename, view);
60  } else if (format == Format::PNG){
61  png_read_view(filename, view);
62  }
63  *data = buf;
64 }
65 
66 // Template specialization: JPEG does not support RGBA
67 template<>
68 void boost_read_image_impl<rgba8_pixel_t>(std::string filename, char** data, size_t& width, size_t& height, size_t& channels, Format format ) {
69  char* buf = new char[width * height * channels];
70  auto view = interleaved_view(width, height, (rgba8_pixel_t*)buf, width * channels * sizeof(char));
71  if (format == Format::JPG) {
72  throw ("JPEG does not support RGBA color type");
73  } else if (format == Format::PNG){
74  png_read_view(filename, view);
75  }
76  *data = buf;
77 }
78 
79 
80 }
81 #endif