Changed ResourceImage to use Dali::Texture internally
[platform/core/uifw/dali-core.git] / dali / internal / event / images / nine-patch-image-impl.h
1 #ifndef __DALI_INTERNAL_NINE_PATCH_IMAGE_H__
2 #define __DALI_INTERNAL_NINE_PATCH_IMAGE_H__
3
4 /*
5  * Copyright (c) 2014 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 // INTERNAL INCLUDES
22 #include <dali/devel-api/images/nine-patch-image.h>
23 #include <dali/internal/event/images/resource-image-impl.h>
24 #include <dali/internal/event/images/buffer-image-impl.h>
25
26 namespace Dali
27 {
28
29 namespace Internal
30 {
31
32 class NinePatchImage;
33 typedef IntrusivePtr<NinePatchImage> NinePatchImagePtr;
34
35 class ResourceClient;
36 class ResourceManager;
37
38 namespace SceneGraph
39 {
40 class UpdateManager;
41 }
42
43 /**
44  * NinePatchImage represents an image resource that can be added to actors etc.
45  * It's image data has a border which determines stretch and fill areas
46  * Its pixel buffer data is loaded synchronously from file.
47  */
48 class NinePatchImage : public ResourceImage
49 {
50 public:
51   typedef Dali::Vector< Uint16Pair > StretchRanges;
52
53 public:
54
55   /**
56    * Create a new NinePatchImage.
57    * Also a pixel buffer for image data is allocated.
58    * Dali has ownership of the buffer.
59    * @param [in] filename    File to load synchronously into buffer
60    */
61   static NinePatchImagePtr New( const std::string& filename );
62
63   /**
64    * Create a new NinePatchImage
65    * For better performance and portability use power of two dimensions.
66    * The maximum size of the image is limited by GL_MAX_TEXTURE_SIZE.
67    * @param [in] filename    File to load synchronously into buffer
68    */
69   NinePatchImage( const std::string& filename );
70
71   /**
72    * Convert Image object to a 9 patch image object if possible.
73    * @param[in] image The image to convert
74    * @return A pointer to the 9 patch image object, or NULL
75    * if the conversion is not possible.
76    */
77   static NinePatchImage* DownCast( Image* image);
78
79
80 protected:
81   /**
82    * A reference counted object may only be deleted by calling Unreference()
83    */
84   virtual ~NinePatchImage();
85
86 public:
87   /**
88    * @copydoc Dali::NinePatchImage::GetStretchPixelsX
89    */
90   const StretchRanges& GetStretchPixelsX();
91
92   /**
93    * @copydoc Dali::NinePatchImage::GetStretchPixelsY
94    */
95   const StretchRanges& GetStretchPixelsY();
96
97   /**
98    * Get the child rectangle
99    * @return the position and size of the child rectangle
100    */
101   Rect<int> GetChildRectangle();
102
103   /**
104    * @brief Create a cropped image from the bitmap with the 1 pixel border cropped off.
105    * This does not change the internal bitmap.
106    *
107    * @return the cropped bitmap.
108    */
109   BufferImagePtr CreateCroppedBufferImage();
110
111   /**
112    *
113    * @copydoc Dali::NinePatchImage::
114    */
115   static bool IsNinePatchUrl( const std::string& url );
116
117 private: // from ResourceImage
118
119   /**
120    * @copydoc ResourceImage::GetUrl()
121    */
122   virtual const std::string& GetUrl() const;
123
124   /**
125    * Read the borders of the bitmap and determine the child area
126    * and stretch borders
127    */
128   void ParseBorders();
129
130   Uint16Pair ParseRange( unsigned int& index, unsigned int width, const PixelBuffer* & pixel, unsigned int pixelStride, int testByte, int testBits, int testValue );
131
132 private:
133   ResourceClient*               mResourceClient;
134   Integration::BitmapPtr        mBitmap;
135   std::string                   mUrl;
136   StretchRanges                 mStretchPixelsX;  //< The horizontal stretchable pixels in the cropped image space
137   StretchRanges                 mStretchPixelsY;  //< The vertical stretchable pixels in the cropped image space
138   Rect<int>                     mChildRectangle;
139   bool                          mParsedBorder;
140 };
141
142 } // namespace Internal
143
144 /**
145  * Helper methods for public API.
146  */
147 inline Internal::NinePatchImage& GetImplementation(Dali::NinePatchImage& handle)
148 {
149   DALI_ASSERT_ALWAYS( handle && "NinePatchImage handle is empty" );
150
151   BaseObject& image = handle.GetBaseObject();
152
153   return static_cast<Internal::NinePatchImage&>(image);
154 }
155
156 inline const Internal::NinePatchImage& GetImplementation(const Dali::NinePatchImage& handle)
157 {
158   DALI_ASSERT_ALWAYS( handle && "NinePatchImage handle is empty" );
159
160   const BaseObject& image = handle.GetBaseObject();
161
162   return static_cast<const Internal::NinePatchImage&>(image);
163 }
164
165 } // namespace Dali
166
167 #endif // __DALI_INTERNAL_NINE_PATCH_IMAGE_H__