Merge branch 'blender2.7'
[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_unique_ptr.h"
27 #include "util/util_vector.h"
28
29 CCL_NAMESPACE_BEGIN
30
31 class Device;
32 class Progress;
33 class RenderStats;
34 class Scene;
35
36 class ImageMetaData {
37 public:
38         /* Must be set by image file or builtin callback. */
39         bool is_float, is_half;
40         int channels;
41         size_t width, height, depth;
42         bool builtin_free_cache;
43
44         /* Automatically set. */
45         ImageDataType type;
46         bool is_linear;
47
48         bool operator==(const ImageMetaData& other) const
49         {
50                 return is_float == other.is_float &&
51                        is_half == other.is_half &&
52                        channels == other.channels &&
53                        width == other.width &&
54                        height == other.height &&
55                        depth == other.depth &&
56                        type == other.type &&
57                        is_linear == other.is_linear;
58         }
59 };
60
61 class ImageManager {
62 public:
63         explicit ImageManager(const DeviceInfo& info);
64         ~ImageManager();
65
66         int add_image(const string& filename,
67                       void *builtin_data,
68                       bool animated,
69                       float frame,
70                       InterpolationType interpolation,
71                       ExtensionType extension,
72                       bool use_alpha,
73                       ImageMetaData& metadata);
74         void remove_image(int flat_slot);
75         void remove_image(const string& filename,
76                           void *builtin_data,
77                           InterpolationType interpolation,
78                           ExtensionType extension,
79                           bool use_alpha);
80         void tag_reload_image(const string& filename,
81                               void *builtin_data,
82                               InterpolationType interpolation,
83                               ExtensionType extension,
84                               bool use_alpha);
85         bool get_image_metadata(const string& filename,
86                                 void *builtin_data,
87                                 ImageMetaData& metadata);
88         bool get_image_metadata(int flat_slot,
89                                 ImageMetaData& metadata);
90
91         void device_update(Device *device,
92                            Scene *scene,
93                            Progress& progress);
94         void device_update_slot(Device *device,
95                                 Scene *scene,
96                                 int flat_slot,
97                                 Progress *progress);
98         void device_free(Device *device);
99
100         void device_load_builtin(Device *device,
101                                  Scene *scene,
102                                  Progress& progress);
103         void device_free_builtin(Device *device);
104
105         void set_osl_texture_system(void *texture_system);
106         bool set_animation_frame_update(int frame);
107
108         device_memory *image_memory(int flat_slot);
109
110         void collect_statistics(RenderStats *stats);
111
112         bool need_update;
113
114         /* NOTE: Here pixels_size is a size of storage, which equals to
115          *       width * height * depth.
116          *       Use this to avoid some nasty memory corruptions.
117          */
118         function<void(const string &filename,
119                       void *data,
120                       ImageMetaData& metadata)> builtin_image_info_cb;
121         function<bool(const string &filename,
122                       void *data,
123                       unsigned char *pixels,
124                       const size_t pixels_size,
125                       const bool free_cache)> builtin_image_pixels_cb;
126         function<bool(const string &filename,
127                       void *data,
128                       float *pixels,
129                       const size_t pixels_size,
130                       const bool free_cache)> builtin_image_float_pixels_cb;
131
132         struct Image {
133                 string filename;
134                 void *builtin_data;
135                 ImageMetaData metadata;
136
137                 bool use_alpha;
138                 bool need_load;
139                 bool animated;
140                 float frame;
141                 InterpolationType interpolation;
142                 ExtensionType extension;
143
144                 string mem_name;
145                 device_memory *mem;
146
147                 int users;
148         };
149
150 private:
151         int tex_num_images[IMAGE_DATA_NUM_TYPES];
152         int max_num_images;
153         bool has_half_images;
154
155         thread_mutex device_mutex;
156         int animation_frame;
157
158         vector<Image*> images[IMAGE_DATA_NUM_TYPES];
159         void *osl_texture_system;
160
161         bool file_load_image_generic(Image *img, unique_ptr<ImageInput> *in);
162
163         template<TypeDesc::BASETYPE FileFormat,
164                  typename StorageType,
165                  typename DeviceType>
166         bool file_load_image(Image *img,
167                              ImageDataType type,
168                              int texture_limit,
169                              device_vector<DeviceType>& tex_img);
170
171         void device_load_image(Device *device,
172                                Scene *scene,
173                                ImageDataType type,
174                                int slot,
175                                Progress *progress);
176         void device_free_image(Device *device,
177                                ImageDataType type,
178                                int slot);
179 };
180
181 CCL_NAMESPACE_END
182
183 #endif  /* __IMAGE_H__ */