Merge branch 'master' into blender2.8
[blender.git] / intern / cycles / render / image.h
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef __IMAGE_H__
18 #define __IMAGE_H__
19
20 #include "device/device.h"
21 #include "device/device_memory.h"
22
23 #include "util/util_image.h"
24 #include "util/util_string.h"
25 #include "util/util_thread.h"
26 #include "util/util_vector.h"
27
28 CCL_NAMESPACE_BEGIN
29
30 class Device;
31 class Progress;
32 class RenderStats;
33 class Scene;
34
35 class ImageMetaData {
36 public:
37         /* Must be set by image file or builtin callback. */
38         bool is_float, is_half;
39         int channels;
40         size_t width, height, depth;
41         bool builtin_free_cache;
42
43         /* Automatically set. */
44         ImageDataType type;
45         bool is_linear;
46 };
47
48 class ImageManager {
49 public:
50         explicit ImageManager(const DeviceInfo& info);
51         ~ImageManager();
52
53         int add_image(const string& filename,
54                       void *builtin_data,
55                       bool animated,
56                       float frame,
57                       InterpolationType interpolation,
58                       ExtensionType extension,
59                       bool use_alpha,
60                       ImageMetaData& metadata);
61         void remove_image(int flat_slot);
62         void remove_image(const string& filename,
63                           void *builtin_data,
64                           InterpolationType interpolation,
65                           ExtensionType extension,
66                           bool use_alpha);
67         void tag_reload_image(const string& filename,
68                               void *builtin_data,
69                               InterpolationType interpolation,
70                               ExtensionType extension,
71                               bool use_alpha);
72         bool get_image_metadata(const string& filename,
73                                 void *builtin_data,
74                                 ImageMetaData& metadata);
75         bool get_image_metadata(int flat_slot,
76                                 ImageMetaData& metadata);
77
78         void device_update(Device *device,
79                            Scene *scene,
80                            Progress& progress);
81         void device_update_slot(Device *device,
82                                 Scene *scene,
83                                 int flat_slot,
84                                 Progress *progress);
85         void device_free(Device *device);
86
87         void device_load_builtin(Device *device,
88                                  Scene *scene,
89                                  Progress& progress);
90         void device_free_builtin(Device *device);
91
92         void set_osl_texture_system(void *texture_system);
93         bool set_animation_frame_update(int frame);
94
95         device_memory *image_memory(int flat_slot);
96
97         void collect_statistics(RenderStats *stats);
98
99         bool need_update;
100
101         /* NOTE: Here pixels_size is a size of storage, which equals to
102          *       width * height * depth.
103          *       Use this to avoid some nasty memory corruptions.
104          */
105         function<void(const string &filename,
106                       void *data,
107                       ImageMetaData& metadata)> builtin_image_info_cb;
108         function<bool(const string &filename,
109                       void *data,
110                       unsigned char *pixels,
111                       const size_t pixels_size,
112                       const bool free_cache)> builtin_image_pixels_cb;
113         function<bool(const string &filename,
114                       void *data,
115                       float *pixels,
116                       const size_t pixels_size,
117                       const bool free_cache)> builtin_image_float_pixels_cb;
118
119         struct Image {
120                 string filename;
121                 void *builtin_data;
122                 ImageMetaData metadata;
123
124                 bool use_alpha;
125                 bool need_load;
126                 bool animated;
127                 float frame;
128                 InterpolationType interpolation;
129                 ExtensionType extension;
130
131                 string mem_name;
132                 device_memory *mem;
133
134                 int users;
135         };
136
137 private:
138         int tex_num_images[IMAGE_DATA_NUM_TYPES];
139         int max_num_images;
140         bool has_half_images;
141
142         thread_mutex device_mutex;
143         int animation_frame;
144
145         vector<Image*> images[IMAGE_DATA_NUM_TYPES];
146         void *osl_texture_system;
147
148         bool file_load_image_generic(Image *img,
149                                      ImageInput **in);
150
151         template<TypeDesc::BASETYPE FileFormat,
152                  typename StorageType,
153                  typename DeviceType>
154         bool file_load_image(Image *img,
155                              ImageDataType type,
156                              int texture_limit,
157                              device_vector<DeviceType>& tex_img);
158
159         void device_load_image(Device *device,
160                                Scene *scene,
161                                ImageDataType type,
162                                int slot,
163                                Progress *progress);
164         void device_free_image(Device *device,
165                                ImageDataType type,
166                                int slot);
167 };
168
169 CCL_NAMESPACE_END
170
171 #endif  /* __IMAGE_H__ */