image.h 5.7 KB

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