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