Turi Create  4.0
image_util.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 IMAGE_UTIL_HPP
7 #define IMAGE_UTIL_HPP
8 
9 #include <core/data/flexible_type/flexible_type.hpp>
10 #include <core/storage/sframe_interface/unity_sframe.hpp>
11 
12 namespace turi{
13 
14 namespace image_util {
15 
16 void copy_image_to_memory(const image_type& input, unsigned char* outptr,
17  const std::vector<size_t>& outstrides,
18  const std::vector<size_t>& outshape,
19  bool channel_last);
20 
21 void copy_image_to_memory(const image_type& input, float* outptr,
22  const std::vector<size_t>& outstrides,
23  const std::vector<size_t>& outshape,
24  bool channel_last);
25 
26 /**
27 * Return the head of passed sarray, but cast to string. Used for printing on python side.
28 */
29 std::shared_ptr<unity_sarray> _head_str(std::shared_ptr<unity_sarray> image_sarray, size_t num_rows);
30 
31 /**
32 * Return flex_vec flexible type that is sum of all images with data in vector form.
33 */
34 
35 flexible_type sum(std::shared_ptr<unity_sarray> unity_data);
36 
37 /**
38  * Generate image of mean pixel values of images in a unity sarray
39  */
40 flexible_type generate_mean(std::shared_ptr<unity_sarray> unity_data);
41 
42 
43 /**************************************************************************/
44 /* */
45 /* Image Loader */
46 /* */
47 /**************************************************************************/
48 
49 /**
50  * Construct an sframe of flex_images, with url pointing to directory where images reside.
51  */
52 std::shared_ptr<unity_sframe> load_images(std::string url, std::string format,
53  bool with_path, bool recursive, bool ignore_failure, bool random_order);
54 
55 /**
56  * Construct a single image from url, and format hint.
57  */
58 flexible_type load_image(const std::string& url, const std::string format);
59 
60 
61 /**************************************************************************/
62 /* */
63 /* Encode and Decode */
64 /* */
65 /**************************************************************************/
66 
67 
68 /**
69  * Decode the image into raw pixels
70  */
71 flexible_type decode_image(const flexible_type& data);
72 
73 /**
74  * Decode an sarray of flex_images into raw pixels
75  */
76 std::shared_ptr<unity_sarray> decode_image_sarray(
77  std::shared_ptr<unity_sarray> image_sarray);
78 
79 /**
80  * Encode the image into compressed format (losslessly)
81  * No effect on already encoded images, even if the format is different.
82  */
83 flexible_type encode_image(const flexible_type& data);
84 
85 
86 /**************************************************************************/
87 /* */
88 /* Resize */
89 /* */
90 /**************************************************************************/
91 
92 /** Resize an sarray of flex_images with the new size. The sampling method
93  * is specified as the polynomial order of the resampling kernel, with 0
94  * (nearest neighbor) and 1 (bilinear) supported.
95  */
96 flexible_type resize_image(const flexible_type& image, size_t resized_width,
97  size_t resized_height, size_t resized_channel, bool decode = false,
98  int resample_method = 0);
99 
100 /** Resize an sarray of flex_image with the new size.
101  */
102 std::shared_ptr<unity_sarray> resize_image_sarray(
103  std::shared_ptr<unity_sarray> image_sarray, size_t resized_width,
104  size_t resized_height, size_t resized_channels, bool decode = false,
105  int resample_method = 0);
106 
107 
108 
109 /**************************************************************************/
110 /* */
111 /* Vector <-> Image Conversion */
112 /* */
113 /**************************************************************************/
114 
115 /** Convert sarray of image data to sarray of vector
116  */
117 std::shared_ptr<unity_sarray>
118  image_sarray_to_vector_sarray(std::shared_ptr<unity_sarray> image_sarray,
119  bool undefined_on_failure);
120 
121 /** Convert sarray of vector to sarray of image
122  */
123 std::shared_ptr<unity_sarray>
124  vector_sarray_to_image_sarray(std::shared_ptr<unity_sarray> image_sarray,
125  size_t width, size_t height, size_t channels, bool undefined_on_failure);
126 
127 } // namespace image_util
128 
129 } // end of turicreate
130 
131 #endif /* IMAGE_UTIL_HPP*/