Remove TypeRegistration from deprecated Image classes
[platform/core/uifw/dali-core.git] / dali / internal / event / images / nine-patch-image-impl.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
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
18 // CLASS HEADER
19 #include <dali/internal/event/images/nine-patch-image-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <cstring> // for memcmp
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/object/type-registry.h>
26 #include <dali/integration-api/bitmap.h>
27 #include <dali/internal/event/common/thread-local-storage.h>
28 #include <dali/internal/update/manager/update-manager.h>
29 #include <dali/integration-api/platform-abstraction.h>
30 #include <dali/integration-api/resource-types.h>
31
32
33 namespace
34 {
35 void GetRedOffsetAndMask(Dali::Pixel::Format pixelFormat, int& byteOffset, int& bitMask)
36 {
37   switch (pixelFormat)
38   {
39     case Dali::Pixel::A8:
40     case Dali::Pixel::L8:
41     case Dali::Pixel::LA88:
42     {
43       byteOffset=0;
44       bitMask=0;
45       break;
46     }
47
48     case Dali::Pixel::RGB888:
49     case Dali::Pixel::RGB8888:
50     case Dali::Pixel::RGBA8888:
51     {
52       byteOffset=0;
53       bitMask=0xFF;
54       break;
55     }
56     case Dali::Pixel::BGR8888:
57     case Dali::Pixel::BGRA8888:
58     {
59       byteOffset=2;
60       bitMask=0xff;
61       break;
62     }
63     case Dali::Pixel::RGB565:
64     {
65       byteOffset=0;
66       bitMask=0xf8;
67       break;
68     }
69     case Dali::Pixel::BGR565:
70     {
71       byteOffset=1;
72       bitMask=0x1f;
73       break;
74     }
75
76     case Dali::Pixel::RGBA4444:
77     {
78       byteOffset=0;
79       bitMask=0xf0;
80       break;
81     }
82     case Dali::Pixel::BGRA4444:
83     {
84       byteOffset=1;
85       bitMask=0xf0;
86       break;
87     }
88
89     case Dali::Pixel::RGBA5551:
90     {
91       byteOffset=0;
92       bitMask=0xf8;
93       break;
94     }
95
96     case Dali::Pixel::BGRA5551:
97     {
98       byteOffset=1;
99       bitMask=0x1e;
100       break;
101     }
102
103     case Dali::Pixel::INVALID:
104     case Dali::Pixel::COMPRESSED_R11_EAC:
105     case Dali::Pixel::COMPRESSED_SIGNED_R11_EAC:
106     case Dali::Pixel::COMPRESSED_RG11_EAC:
107     case Dali::Pixel::COMPRESSED_SIGNED_RG11_EAC:
108     case Dali::Pixel::COMPRESSED_RGB8_ETC2:
109     case Dali::Pixel::COMPRESSED_SRGB8_ETC2:
110     case Dali::Pixel::COMPRESSED_RGB8_ETC1:
111     case Dali::Pixel::COMPRESSED_RGB_PVRTC_4BPPV1:
112     case Dali::Pixel::COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
113     case Dali::Pixel::COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
114     case Dali::Pixel::COMPRESSED_RGBA8_ETC2_EAC:
115     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
116     case Dali::Pixel::COMPRESSED_RGBA_ASTC_4x4_KHR:
117     case Dali::Pixel::COMPRESSED_RGBA_ASTC_5x4_KHR:
118     case Dali::Pixel::COMPRESSED_RGBA_ASTC_5x5_KHR:
119     case Dali::Pixel::COMPRESSED_RGBA_ASTC_6x5_KHR:
120     case Dali::Pixel::COMPRESSED_RGBA_ASTC_6x6_KHR:
121     case Dali::Pixel::COMPRESSED_RGBA_ASTC_8x5_KHR:
122     case Dali::Pixel::COMPRESSED_RGBA_ASTC_8x6_KHR:
123     case Dali::Pixel::COMPRESSED_RGBA_ASTC_8x8_KHR:
124     case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x5_KHR:
125     case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x6_KHR:
126     case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x8_KHR:
127     case Dali::Pixel::COMPRESSED_RGBA_ASTC_10x10_KHR:
128     case Dali::Pixel::COMPRESSED_RGBA_ASTC_12x10_KHR:
129     case Dali::Pixel::COMPRESSED_RGBA_ASTC_12x12_KHR:
130     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
131     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
132     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
133     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
134     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
135     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
136     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
137     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
138     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
139     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
140     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
141     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
142     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
143     case Dali::Pixel::COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
144     {
145       DALI_LOG_ERROR("Pixel formats for compressed images are not compatible with simple masking-out of per-pixel alpha.\n");
146       byteOffset=0;
147       bitMask=0;
148       break;
149     }
150
151     case Dali::Pixel::RGB16F:
152     case Dali::Pixel::RGB32F:
153     {
154       DALI_LOG_ERROR("Pixel format not compatible.\n");
155       byteOffset=0;
156       bitMask=0;
157       break;
158     }
159   }
160 }
161
162 } // anonymous namespace
163
164
165 namespace Dali
166 {
167 namespace Internal
168 {
169
170 NinePatchImagePtr NinePatchImage::New( const std::string& filename )
171 {
172   Internal::NinePatchImagePtr internal( new NinePatchImage( filename ) );
173   internal->Initialize();
174   return internal;
175 }
176
177 NinePatchImage::NinePatchImage( const std::string& filename )
178 : ResourceImage(),
179   mParsedBorder(false)
180 {
181   mUrl = filename;
182   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
183
184   Integration::PlatformAbstraction& platformAbstraction = tls.GetPlatformAbstraction();
185   Integration::BitmapResourceType resourceType;
186
187   // Note, bitmap is only destroyed when the image is destroyed.
188   Integration::ResourcePointer resource = platformAbstraction.LoadImageSynchronously( resourceType, filename );
189   if( resource )
190   {
191     mBitmap = static_cast<Integration::Bitmap*>( resource.Get());
192
193     mWidth = mBitmap->GetImageWidth();
194     mHeight = mBitmap->GetImageHeight();
195     mTexture = Texture::New( Dali::TextureType::TEXTURE_2D, mBitmap->GetPixelFormat(), mWidth, mHeight );
196
197     uint32_t bufferSize = mBitmap->GetBufferSize();
198     uint8_t* buffer = new uint8_t[bufferSize];
199     memcpy( buffer, mBitmap->GetBuffer(), bufferSize );
200     PixelDataPtr pixelData = PixelData::New( buffer, bufferSize, mWidth, mHeight, mBitmap->GetPixelFormat(), Dali::PixelData::DELETE_ARRAY );
201     mTexture->Upload( pixelData );
202   }
203   else
204   {
205     mBitmap.Reset();
206     mWidth = 0;
207     mHeight = 0;
208   }
209 }
210
211 NinePatchImage* NinePatchImage::DownCast( Image* image)
212 {
213   return dynamic_cast<NinePatchImage*>(image);
214 }
215
216 NinePatchImage::~NinePatchImage()
217 {
218 }
219
220 const NinePatchImage::StretchRanges& NinePatchImage::GetStretchPixelsX()
221 {
222   if( ! mParsedBorder )
223   {
224     ParseBorders();
225   }
226   return mStretchPixelsX;
227 }
228
229 const NinePatchImage::StretchRanges& NinePatchImage::GetStretchPixelsY()
230 {
231   if( ! mParsedBorder )
232   {
233     ParseBorders();
234   }
235   return mStretchPixelsY;
236 }
237
238 Rect<int> NinePatchImage::GetChildRectangle()
239 {
240   if( ! mParsedBorder )
241   {
242     ParseBorders();
243   }
244   return mChildRectangle;
245 }
246
247 Internal::BufferImagePtr NinePatchImage::CreateCroppedBufferImage()
248 {
249   BufferImagePtr cropped;
250
251   if( ! mBitmap )
252   {
253     DALI_LOG_ERROR( "NinePatchImage: Bitmap not loaded, cannot perform operation\n");
254   }
255   else
256   {
257     Pixel::Format pixelFormat = mBitmap->GetPixelFormat();
258
259     cropped = BufferImage::New( mWidth-2, mHeight-2, pixelFormat );
260
261     Integration::Bitmap::PackedPixelsProfile* srcProfile = mBitmap->GetPackedPixelsProfile();
262     DALI_ASSERT_DEBUG( srcProfile && "Wrong profile for source bitmap");
263
264     if( srcProfile )
265     {
266       PixelBuffer* destPixels = cropped->GetBuffer();
267       uint32_t destStride = cropped->GetBufferStride();
268       uint32_t pixelWidth = GetBytesPerPixel(pixelFormat);
269
270       PixelBuffer* srcPixels = mBitmap->GetBuffer();
271       uint32_t srcStride = srcProfile->GetBufferStride();
272
273       for( uint32_t row=1; row < mHeight-1; ++row )
274       {
275         PixelBuffer* src  = srcPixels + row*srcStride + pixelWidth;
276         PixelBuffer* dest = destPixels + (row-1)*destStride;
277         memcpy(dest, src, destStride );
278       }
279     }
280
281     RectArea area;
282     cropped->Update(area); // default area has no width or height
283   }
284   return cropped;
285 }
286
287 const std::string& NinePatchImage::GetUrl() const
288 {
289   return mUrl;
290 }
291
292 void NinePatchImage::ParseBorders()
293 {
294   if( !mBitmap )
295   {
296     DALI_LOG_ERROR( "NinePatchImage: Bitmap not loaded, cannot perform operation\n");
297     return;
298   }
299
300   mStretchPixelsX.Clear();
301   mStretchPixelsY.Clear();
302
303   Pixel::Format pixelFormat = mBitmap->GetPixelFormat();
304
305   const Integration::Bitmap::PackedPixelsProfile* srcProfile = mBitmap->GetPackedPixelsProfile();
306   DALI_ASSERT_DEBUG( srcProfile && "Wrong profile for source bitmap" );
307
308   if( srcProfile )
309   {
310     int alphaByte = 0;
311     int alphaBits = 0;
312     Pixel::GetAlphaOffsetAndMask( pixelFormat, alphaByte, alphaBits );
313
314     int testByte = alphaByte;
315     int testBits = alphaBits;
316     int testValue = alphaBits; // Opaque == stretch
317     if( ! alphaBits )
318     {
319       GetRedOffsetAndMask( pixelFormat, testByte, testBits );
320       testValue = 0;           // Black == stretch
321     }
322
323     uint32_t pixelWidth = GetBytesPerPixel( pixelFormat );
324     const PixelBuffer* srcPixels = mBitmap->GetBuffer();
325     uint32_t srcStride = srcProfile->GetBufferStride();
326
327     //TOP
328     const PixelBuffer* top = srcPixels + pixelWidth;
329     uint32_t index = 0;
330     uint32_t width = mBitmap->GetImageWidth();
331     uint32_t height = mBitmap->GetImageHeight();
332
333     for(; index < width - 2; )
334     {
335       Uint16Pair range = ParseRange( index, width - 2, top, pixelWidth, testByte, testBits, testValue );
336       if( range.GetX() != 0xFFFF )
337       {
338         mStretchPixelsX.PushBack( range );
339       }
340     }
341
342     //LEFT
343     const PixelBuffer* left  = srcPixels + srcStride;
344     index = 0;
345     for(; index < height - 2; )
346     {
347       Uint16Pair range = ParseRange( index, height - 2, left, srcStride, testByte, testBits, testValue );
348       if( range.GetX() != 0xFFFF )
349       {
350         mStretchPixelsY.PushBack( range );
351       }
352     }
353
354     //If there are no stretch pixels then make the entire image stretchable
355     if( mStretchPixelsX.Size() == 0 )
356     {
357       mStretchPixelsX.PushBack( Uint16Pair( 0, width - 2 ) );
358     }
359     if( mStretchPixelsY.Size() == 0 )
360     {
361       mStretchPixelsY.PushBack( Uint16Pair( 0, height - 2 ) );
362     }
363
364     //Child Rectangle
365     //BOTTOM
366     const PixelBuffer* bottom = srcPixels + ( height - 1 ) * srcStride + pixelWidth;
367     index = 0;
368     Uint16Pair contentRangeX = ParseRange( index, width - 2, bottom, pixelWidth, testByte, testBits, testValue );
369     if( contentRangeX.GetX() == 0xFFFF )
370     {
371       contentRangeX = Uint16Pair();
372     }
373
374     //RIGHT
375     const PixelBuffer* right = srcPixels + srcStride + ( width - 1 ) * pixelWidth;
376     index = 0;
377     Uint16Pair contentRangeY = ParseRange( index, height - 2, right, srcStride, testByte, testBits, testValue );
378     if( contentRangeY.GetX() == 0xFFFF )
379     {
380       contentRangeY = Uint16Pair();
381     }
382
383     mChildRectangle.x = contentRangeX.GetX() + 1;
384     mChildRectangle.y = contentRangeY.GetX() + 1;
385     mChildRectangle.width = contentRangeX.GetY() - contentRangeX.GetX();
386     mChildRectangle.height = contentRangeY.GetY() - contentRangeY.GetX();
387
388     mParsedBorder = true;
389   }
390 }
391
392 Uint16Pair NinePatchImage::ParseRange( uint32_t& index, uint32_t width, const PixelBuffer* & pixel, uint32_t pixelStride, int testByte, int testBits, int testValue )
393 {
394   uint32_t start = 0xFFFF;
395   for( ; index < width; ++index, pixel += pixelStride )
396   {
397     if( ( pixel[ testByte ] & testBits ) == testValue )
398     {
399         start = index;
400         ++index;
401         pixel += pixelStride;
402         break;
403     }
404   }
405
406   uint32_t end = width;
407   for( ; index < width; ++index, pixel += pixelStride )
408   {
409     if( ( pixel[ testByte ] & testBits ) != testValue )
410     {
411         end = index;
412         ++index;
413         pixel += pixelStride;
414         break;
415     }
416   }
417
418   return Uint16Pair( start, end );
419 }
420
421 bool NinePatchImage::IsNinePatchUrl( const std::string& url )
422 {
423   bool match = false;
424
425   std::string::const_reverse_iterator iter = url.rbegin();
426   enum { SUFFIX, HASH, HASH_DOT, DONE } state = SUFFIX;
427   while(iter < url.rend())
428   {
429     switch(state)
430     {
431       case SUFFIX:
432       {
433         if(*iter == '.')
434         {
435           state = HASH;
436         }
437         else if(!isalnum(*iter))
438         {
439           state = DONE;
440         }
441       }
442       break;
443       case HASH:
444       {
445         if( *iter == '#' || *iter == '9' )
446         {
447           state = HASH_DOT;
448         }
449         else
450         {
451           state = DONE;
452         }
453       }
454       break;
455       case HASH_DOT:
456       {
457         if(*iter == '.')
458         {
459           match = true;
460         }
461         state = DONE; // Stop testing characters
462       }
463       break;
464       case DONE:
465       {
466       }
467       break;
468     }
469
470     // Satisfy prevent
471     if( state == DONE )
472     {
473       break;
474     }
475
476     ++iter;
477   }
478   return match;
479 }
480
481 } // namespace Internal
482
483 } // namespace Dali