Merge branch 'master' into tizen
[platform/core/uifw/dali-core.git] / dali / internal / event / images / nine-patch-image-impl.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18 #include <dali/internal/event/images/nine-patch-image-impl.h>
19
20 // INTERNAL INCLUDES
21 #include <dali/integration-api/bitmap.h>
22 #include <dali/internal/event/images/bitmap-external.h>
23 #include <dali/internal/event/common/thread-local-storage.h>
24 #include <dali/internal/event/resources/resource-client.h>
25 #include <dali/internal/update/manager/update-manager.h>
26 #include <dali/internal/event/images/image-factory.h>
27 #include <dali/integration-api/platform-abstraction.h>
28 #include <dali/integration-api/resource-types.h>
29 #include <dali/integration-api/resource-cache.h>
30
31
32 namespace
33 {
34 void GetRedOffsetAndMask(Dali::Pixel::Format pixelFormat, int& byteOffset, int& bitMask)
35 {
36   switch (pixelFormat)
37   {
38     case Dali::Pixel::A8:
39     case Dali::Pixel::L8:
40     case Dali::Pixel::LA88:
41     {
42       byteOffset=0;
43       bitMask=0;
44       break;
45     }
46
47     case Dali::Pixel::RGB888:
48     case Dali::Pixel::RGB8888:
49     case Dali::Pixel::RGBA8888:
50     {
51       byteOffset=0;
52       bitMask=0xFF;
53       break;
54     }
55     case Dali::Pixel::BGR8888:
56     case Dali::Pixel::BGRA8888:
57     {
58       byteOffset=2;
59       bitMask=0xff;
60       break;
61     }
62     case Dali::Pixel::RGB565:
63     {
64       byteOffset=0;
65       bitMask=0xf8;
66       break;
67     }
68     case Dali::Pixel::BGR565:
69     {
70       byteOffset=1;
71       bitMask=0x1f;
72       break;
73     }
74
75     case Dali::Pixel::RGBA4444:
76     {
77       byteOffset=0;
78       bitMask=0xf0;
79       break;
80     }
81     case Dali::Pixel::BGRA4444:
82     {
83       byteOffset=1;
84       bitMask=0xf0;
85       break;
86     }
87
88     case Dali::Pixel::RGBA5551:
89     {
90       byteOffset=0;
91       bitMask=0xf8;
92       break;
93     }
94
95     case Dali::Pixel::BGRA5551:
96     {
97       byteOffset=1;
98       bitMask=0x1e;
99       break;
100     }
101
102     case Dali::Pixel::COMPRESSED_R11_EAC:
103     case Dali::Pixel::COMPRESSED_SIGNED_R11_EAC:
104     case Dali::Pixel::COMPRESSED_RG11_EAC:
105     case Dali::Pixel::COMPRESSED_SIGNED_RG11_EAC:
106     case Dali::Pixel::COMPRESSED_RGB8_ETC2:
107     case Dali::Pixel::COMPRESSED_SRGB8_ETC2:
108     case Dali::Pixel::COMPRESSED_RGB8_ETC1:
109     case Dali::Pixel::COMPRESSED_RGB_PVRTC_4BPPV1:
110     case Dali::Pixel::COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
111     case Dali::Pixel::COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
112     case Dali::Pixel::COMPRESSED_RGBA8_ETC2_EAC:
113     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
114     {
115       DALI_LOG_ERROR("Pixel formats for compressed images are not compatible with simple masking-out of per-pixel alpha.\n");
116       byteOffset=0;
117       bitMask=0;
118       break;
119     }
120   }
121 }
122
123 } // anonymous namespace
124
125
126 namespace Dali
127 {
128 namespace Internal
129 {
130
131
132 NinePatchImagePtr NinePatchImage::New( const std::string& filename, const Dali::ImageAttributes& attributes, LoadPolicy loadPol, ReleasePolicy releasePol )
133 {
134   Internal::NinePatchImagePtr internal( new NinePatchImage( filename, attributes, loadPol, releasePol ) );
135   internal->Initialize();
136   return internal;
137 }
138
139 NinePatchImage::NinePatchImage( const std::string& filename, const Dali::ImageAttributes& attributes, LoadPolicy loadPol, ReleasePolicy releasePol)
140 : Image(Dali::Image::Immediate, Dali::Image::Never),
141   mParsedBorder(false)
142 {
143   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
144   mResourceClient = &tls.GetResourceClient();
145
146   Integration::PlatformAbstraction& platformAbstraction = Internal::ThreadLocalStorage::Get().GetPlatformAbstraction();
147
148   Vector2 closestSize;
149   platformAbstraction.GetClosestImageSize( filename, attributes, closestSize );
150   ImageAttributes loadedAttrs;
151   loadedAttrs.SetSize( closestSize );
152   mWidth = closestSize.width;
153   mHeight = closestSize.height;
154   Integration::BitmapResourceType resourceType( loadedAttrs );
155
156   // Note, bitmap is only destroyed when the image is destroyed.
157   Integration::ResourcePointer resource = platformAbstraction.LoadResourceSynchronously(resourceType, filename);
158   mBitmap = static_cast<Integration::Bitmap*>( resource.Get());
159 }
160
161 NinePatchImage* NinePatchImage::GetNinePatchImage( Image* image)
162 {
163   return dynamic_cast<NinePatchImage*>(image);
164 }
165
166 NinePatchImage::~NinePatchImage()
167 {
168 }
169
170 Vector4 NinePatchImage::GetStretchBorders()
171 {
172   if( ! mParsedBorder )
173   {
174     ParseBorders();
175   }
176   return mStretchBorders;
177 }
178
179 Rect<int> NinePatchImage::GetChildRectangle()
180 {
181   if( ! mParsedBorder )
182   {
183     ParseBorders();
184   }
185   return mChildRectangle;
186 }
187
188 Internal::BitmapImagePtr NinePatchImage::CreateCroppedBitmapImage()
189 {
190   Pixel::Format pixelFormat = mBitmap->GetPixelFormat();
191
192   BitmapImagePtr cropped = BitmapImage::New( mWidth-2, mHeight-2, pixelFormat,
193                                              Dali::Image::Immediate, Dali::Image::Never );
194
195   Integration::Bitmap::PackedPixelsProfile* srcProfile = mBitmap->GetPackedPixelsProfile();
196   DALI_ASSERT_DEBUG( srcProfile && "Wrong profile for source bitmap");
197
198   if( srcProfile )
199   {
200     PixelBuffer* destPixels = cropped->GetBuffer();
201     unsigned int destStride = cropped->GetBufferStride();
202     unsigned int pixelWidth = GetBytesPerPixel(pixelFormat);
203
204     PixelBuffer* srcPixels = mBitmap->GetBuffer();
205     unsigned int srcStride = srcProfile->GetBufferStride();
206
207     for( unsigned int row=1; row < mHeight-1; ++row )
208     {
209       PixelBuffer* src  = srcPixels + row*srcStride + pixelWidth;
210       PixelBuffer* dest = destPixels + (row-1)*destStride;
211       memcpy(dest, src, destStride );
212     }
213   }
214
215   RectArea area;
216   cropped->Update(area); // default area has no width or height
217   return cropped;
218 }
219
220 void NinePatchImage::Connect()
221 {
222   if( mConnectionCount == 0 && !mTicket )
223   {
224     const ImageTicketPtr& t = mResourceClient->AddBitmapImage(mBitmap.Get());
225     mTicket = t.Get();
226     mTicket->AddObserver(*this);
227   }
228
229   ++mConnectionCount;
230 }
231
232 void NinePatchImage::Disconnect()
233 {
234   if( mConnectionCount > 0 )
235   {
236     --mConnectionCount;
237   }
238 }
239
240
241 void NinePatchImage::ParseBorders()
242 {
243   Pixel::Format pixelFormat = mBitmap->GetPixelFormat();
244
245   Integration::Bitmap::PackedPixelsProfile* srcProfile = mBitmap->GetPackedPixelsProfile();
246   DALI_ASSERT_DEBUG( srcProfile && "Wrong profile for source bitmap");
247
248   if( srcProfile )
249   {
250     unsigned int pixelWidth = GetBytesPerPixel(pixelFormat);
251     PixelBuffer* srcPixels = mBitmap->GetBuffer();
252     unsigned int srcStride = srcProfile->GetBufferStride();
253
254     int alphaByte=0;
255     int alphaBits=0;
256     Pixel::GetAlphaOffsetAndMask(pixelFormat, alphaByte, alphaBits);
257     int redByte=0;
258     int redBits=0;
259     GetRedOffsetAndMask(pixelFormat, redByte, redBits);
260
261     int testByte = alphaByte;
262     int testBits = alphaBits;
263     int testValue = alphaBits; // Opaque == stretch
264     if( ! alphaBits )
265     {
266       testByte = redByte;
267       testBits = redBits;
268       testValue = 0;           // Black == stretch
269     }
270
271     int startX1=-1;
272     int endX1=-1;
273     int startY1=-1;
274     int endY1=-1;
275     int startX2=-1;
276     int endX2=-1;
277     int startY2=-1;
278     int endY2=-1;
279
280     PixelBuffer* top = srcPixels + pixelWidth;
281     PixelBuffer* bottom = srcPixels + (mHeight-1)*srcStride + pixelWidth;
282
283     // Read the top and bottom rows:
284     // (Also read the last column to ensure end value gets set)
285     for( unsigned int col=1; col < mWidth; ++col )
286     {
287       if( (top[testByte] & testBits) == testValue )
288       {
289         if(startX1 < 0)
290         {
291           startX1 = col;
292         }
293       }
294       else if(startX1 >= 0 && endX1 < 0)
295       {
296         endX1 = col-1;
297         break;
298       }
299
300       if( (bottom[testByte] & testBits) == testValue )
301       {
302         if(startX2 < 0)
303         {
304           startX2 = col;
305         }
306       }
307       else if(startX2 >= 0 && endX2 < 0)
308       {
309         endX2 = col-1;
310         break;
311       }
312       top+=pixelWidth;
313       bottom+=pixelWidth;
314     }
315
316     // Read the left and right columns:
317     PixelBuffer* left  = srcPixels + srcStride;
318     PixelBuffer* right = left + (srcStride - pixelWidth);
319
320     // (Also read the last row to ensure end value gets set)
321     for( unsigned int row=1; row < mHeight; ++row )
322     {
323       if((left[testByte] & testBits) == testValue)
324       {
325         if(startY1 < 0)
326         {
327           startY1 = row;
328         }
329       }
330       else if(startY1 >= 0 && endY1 < 0)
331       {
332         endY1 = row-1;
333         break;
334       }
335
336       if((right[testByte] & testBits) == testValue)
337       {
338         if(startY2 < 0)
339         {
340           startY2 = row;
341         }
342       }
343       else if(startY2 >= 0 && endY2 < 0)
344       {
345         endY2 = row-1;
346         break;
347       }
348       left += srcStride;
349       right += srcStride;
350     }
351
352     mStretchBorders.x = startX1-1;
353     mStretchBorders.y = startY1-1;
354     mStretchBorders.z = mWidth-endX1-1;
355     mStretchBorders.w = mHeight-endY1-1;
356
357     mChildRectangle.x = startX2-1;
358     mChildRectangle.y = startY2-1;
359     mChildRectangle.width = endX2-startX2;
360     mChildRectangle.height = endY2-startY2;
361
362     mParsedBorder = true;
363   }
364 }
365
366 } // namespace Internal
367
368 } // namespace Dali