Merge branch 'devel/master' into tizen
[platform/core/uifw/dali-core.git] / dali / public-api / images / resource-image.h
1 #ifndef __DALI_RESOURCE_IMAGE_H__
2 #define __DALI_RESOURCE_IMAGE_H__
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <string>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/common/loading-state.h>
26 #include <dali/public-api/images/image.h>
27 #include <dali/public-api/signals/dali-signal.h>
28 #include <dali/public-api/images/image-operations.h>
29
30 namespace Dali
31 {
32 /**
33  * @addtogroup dali_core_images
34  * @{
35  */
36
37 namespace Internal DALI_INTERNAL
38 {
39 class ResourceImage;
40 }
41
42 /**
43  * @brief ResourceImage is an image loaded using a URL
44  *
45  * <h3>ResourceImage Loading</h3>
46  *
47  * When the ResourceImage is created, resource loading will be attempted unless a compatible resource is found in cache.
48  * Scaling of images to a desired smaller size can be requested by providing desired dimensions,
49  * scaling mode and filter mode to to ResourceImage::New().
50  *
51  * <i>Custom load requests</i>
52  * Size, scaling mode, filter mode, and orientation compensation can be set when requesting an image.
53  *
54  * <i>Compatible resources</i>
55  *
56  * Before loading a new ResourceImage the internal image resource cache is checked by DALi.
57  * If there is an image already loaded in memory and is deemed "compatible" with the requested image,
58  * that resource is reused.
59  * This happens for example if a loaded image exists with the same URL, scaling and filtering modes,
60  * and the difference between both of the dimensions is less than a few pixels.
61  *
62  * <i>Reloading images</i>
63  *
64  * The same request used on creating the ResourceImage is re-issued when reloading images.
65  * If the file changed since the last load operation, this might result in a different resource.
66  * Reload only takes effect if the ResourceImage has already finished loading
67  *
68  * Signals
69  * | %Signal Name         | Method                       |
70  * |----------------------|------------------------------|
71  * | imageLoadingFinished | @ref LoadingFinishedSignal() |
72  * @SINCE_1_0.0
73  */
74 class DALI_IMPORT_API ResourceImage : public Image
75 {
76 public:
77
78   /**
79    * @DEPRECATED_1_1.3. Image loading starts immediately in the frame when then ResourceImage object is created.
80    *
81    * @brief LoadPolicy controls the way images are loaded into memory.
82    * @SINCE_1_0.0
83    */
84   enum LoadPolicy
85   {
86     IMMEDIATE, ///< load image once it is created (default) @SINCE_1_0.0
87     ON_DEMAND  ///< delay loading until the image is being used (a related actor is added to Stage) @SINCE_1_0.0
88   };
89
90   /**
91    * @brief Type of signal for LoadingFinished and Uploaded.
92    * @SINCE_1_0.0
93    */
94   typedef Signal< void (ResourceImage) > ResourceImageSignal;
95
96 public:
97
98   /**
99    * @brief Get the size of an image from disk.
100    *
101    * This function will read the header info from file on disk and is
102    * synchronous, so it should not be used repeatedly or in tight
103    * loops.
104    *
105    * @SINCE_1_0.0
106    * @param [in] url The URL of the image file.
107    * @return The width and height in pixels of the image.
108    */
109   static ImageDimensions GetImageSize( const std::string& url );
110
111   /**
112    * @brief Constructor which creates an empty ResourceImage object.
113    *
114    * Use ResourceImage::New(...) to create an initialised object.
115    * @SINCE_1_0.0
116    */
117   ResourceImage();
118
119   /**
120    * @brief Destructor
121    *
122    * This is non-virtual since derived Handle types must not contain data or virtual methods.
123    * @SINCE_1_0.0
124    */
125   ~ResourceImage();
126
127   /**
128    * @brief This copy constructor is required for (smart) pointer semantics.
129    *
130    * @SINCE_1_0.0
131    * @param [in] handle A reference to the copied handle
132    */
133   ResourceImage( const ResourceImage& handle );
134
135   /**
136    * @brief This assignment operator is required for (smart) pointer semantics.
137    *
138    * @SINCE_1_0.0
139    * @param [in] rhs  A reference to the copied handle
140    * @return A reference to this
141    */
142   ResourceImage& operator=( const ResourceImage& rhs );
143
144   /**
145    * @name ResourceImageFactoryFunctions
146    * Create ResourceImage object instances using these functions.
147    */
148   ///@{
149
150   /**
151    * @brief Create an initialised ResourceImage object.
152    *
153    * Uses defaults for all options.
154    *
155    * @sa Dali::FittingMode::Type Dali::SamplingMode::Type
156    * @SINCE_1_0.0
157    * @param [in] url The URL of the image file to use.
158    * @param [in] orientationCorrection Reorient the image to respect any orientation metadata in its header.
159    * @return A handle to a newly allocated object
160    */
161   static ResourceImage New( const std::string& url, bool orientationCorrection = true );
162
163   /**
164    * @DEPRECATED_1_1.3. Use New( const std::string& url ) instead.
165    *
166    * @brief Create an initialised ResourceImage object.
167    *
168    * @SINCE_1_0.0
169    * @param [in] url The URL of the image file to use.
170    * @param [in] loadPol    The LoadPolicy to apply when loading the image resource.
171    * @param [in] releasePol The ReleasePolicy to apply to Image.
172    * @param [in] orientationCorrection Reorient the image to respect any orientation metadata in its header.
173    * @return A handle to a newly allocated object
174    */
175   static ResourceImage New( const std::string& url, LoadPolicy loadPol, ReleasePolicy releasePol, bool orientationCorrection = true );
176
177   /**
178    * @brief Create an initialised ResourceImage object.
179    *
180    * @SINCE_1_0.0
181    * @param [in] url The URL of the image file to use.
182    * @param [in] size The width and height to fit the loaded image to.
183    * @param [in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter.
184    * @param [in] samplingMode The filtering method used when sampling pixels from the input image while fitting it to desired size.
185    * @param [in] orientationCorrection Reorient the image to respect any orientation metadata in its header.
186    * @return A handle to a newly allocated object
187    */
188   static ResourceImage New( const std::string& url,
189                             ImageDimensions size,
190                             FittingMode::Type fittingMode = FittingMode::DEFAULT,
191                             SamplingMode::Type samplingMode = SamplingMode::DEFAULT,
192                             bool orientationCorrection = true );
193
194   /**
195    * @DEPRECATED_1_1.3. Use New( const std::string& url, ImageDimensions size ) instead.
196    *
197    * @brief Create an initialised ResourceImage object.
198    *
199    * @SINCE_1_0.0
200    * @param [in] url The URL of the image file to use.
201    * @param [in] loadPol    The LoadPolicy to apply when loading the image resource.
202    * @param [in] releasePol The ReleasePolicy to apply to Image.
203    * @param [in] size The width and height to fit the loaded image to.
204    * @param [in] fittingMode The method used to fit the shape of the image before loading to the shape defined by the size parameter.
205    * @param [in] samplingMode The filtering method used when sampling pixels from the input image while fitting it to desired size.
206    * @param [in] orientationCorrection Reorient the image to respect any orientation metadata in its header.
207    * @return A handle to a newly allocated object
208    */
209   static ResourceImage New( const std::string& url,
210                             LoadPolicy loadPol,
211                             ReleasePolicy releasePol,
212                             ImageDimensions size,
213                             FittingMode::Type fittingMode = FittingMode::DEFAULT,
214                             SamplingMode::Type samplingMode = SamplingMode::DEFAULT,
215                             bool orientationCorrection = true );
216
217   ///@}
218
219   /**
220    * @brief Downcast an Object handle to ResourceImage handle.
221    *
222    * If handle points to a ResourceImage object the
223    * downcast produces valid handle. If not the returned handle is left uninitialized.
224    * @SINCE_1_0.0
225    * @param[in] handle to An object
226    * @return handle to a Image object or an uninitialized handle
227    */
228   static ResourceImage DownCast( BaseHandle handle );
229
230   /**
231    * @DEPRECATED_1_1.3
232    *
233    * @brief Return load policy.
234    *
235    * @SINCE_1_0.0
236    * @return resource load policy
237    */
238   LoadPolicy GetLoadPolicy() const;
239
240   /**
241    * @brief Query whether the image data has loaded.
242    *
243    * The asynchronous loading begins when the Image object is created.
244    * After the Image object is discarded, the image data will be released from memory.
245    * @SINCE_1_0.0
246    * @return The loading state, either Loading, Success or Failed.
247    */
248   LoadingState GetLoadingState() const;
249
250   /**
251    * @brief Returns the URL of the image.
252    *
253    * @SINCE_1_0.0
254    * @return The URL of the image file.
255    */
256   std::string GetUrl() const;
257
258   /**
259    * @brief Reload image from filesystem.
260    *
261    * The original set of image loading attributes (requested dimensions, scaling
262    * mode and filter mode) are used when requesting the image again.
263    * @SINCE_1_0.0
264    * @note If image is offstage and OnDemand policy is set, the reload request is
265    * ignored.
266    */
267   void Reload();
268
269 public: // Signals
270
271   /**
272    * @brief Emitted when the image data loads successfully, or when the loading fails.
273    *
274    * @SINCE_1_0.0
275    * @return A signal object to Connect() with.
276    */
277   ResourceImageSignal& LoadingFinishedSignal();
278
279 public: // Not intended for application developers
280
281   explicit DALI_INTERNAL ResourceImage( Internal::ResourceImage* );
282 };
283
284 /**
285  * @}
286  */
287 } // namespace Dali
288
289 #endif // __DALI_RESOURCE_IMAGE_H__