use modern construct 'nullptr' instead of 'NULL' or '0'
[platform/core/uifw/dali-core.git] / dali / internal / event / images / bitmap-packed-pixel.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/bitmap-packed-pixel.h>
20
21 // EXTERNAL INCLUDES
22 #include <cstdlib>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/object/ref-object.h>
26 #include <dali/internal/common/core-impl.h>
27 #include <dali/integration-api/debug.h>
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34 using namespace Dali::Pixel;
35
36
37 BitmapPackedPixel::BitmapPackedPixel( ResourcePolicy::Discardable discardable, Dali::Integration::PixelBuffer* pixBuf )
38 : Bitmap( discardable, pixBuf ),
39   mBufferWidth(0),
40   mBufferHeight(0),
41   mBytesPerPixel(0)
42 {
43 }
44
45 // use power of two bufferWidth and bufferHeight for better performance
46 Dali::Integration::PixelBuffer* BitmapPackedPixel::ReserveBuffer( Pixel::Format pixelFormat,
47                                                                   uint32_t width,
48                                                                   uint32_t height,
49                                                                   uint32_t bufferWidth,
50                                                                   uint32_t bufferHeight )
51 {
52   // delete existing buffer
53   DeletePixelBuffer();
54
55   Initialize(pixelFormat, width, height, bufferWidth, bufferHeight);
56
57   //allocate buffer
58   uint32_t bufSize = mBufferWidth * mBufferHeight * mBytesPerPixel;
59
60   mData = reinterpret_cast< Dali::Integration::PixelBuffer* >( malloc( bufSize) );
61
62   return mData;
63 }
64
65 void BitmapPackedPixel::AssignBuffer( Pixel::Format pixelFormat,
66                                       Dali::Integration::PixelBuffer* buffer,
67                                       uint32_t bufferSize,
68                                       uint32_t width,
69                                       uint32_t height,
70                                       uint32_t bufferWidth,
71                                       uint32_t bufferHeight)
72 {
73   DALI_ASSERT_DEBUG( buffer );
74
75   // delete existing buffer
76   DeletePixelBuffer();
77
78   Initialize( pixelFormat, width, height, bufferWidth, bufferHeight );
79
80    // make sure the buffer size matches what is being passed in
81   DALI_ASSERT_DEBUG( bufferSize ==  (mBufferWidth * mBufferHeight * mBytesPerPixel))
82
83   mData = buffer;
84 }
85
86 void BitmapPackedPixel::TestForTransparency()
87 {
88   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
89
90   mAlphaChannelUsed = false;
91
92   if(HasAlphaChannel())
93   {
94     uint8_t* pixelBuffer=GetBuffer();
95     if(pixelBuffer != nullptr)
96     {
97       uint8_t* row = pixelBuffer;
98
99       int32_t byte; int32_t bits;
100       Pixel::GetAlphaOffsetAndMask(mPixelFormat, byte, bits);
101
102       int32_t stride       = mBufferWidth * mBytesPerPixel;
103       int32_t pixelsPerRow = mImageWidth;
104
105       for(uint32_t j=0; j<mImageHeight; j++)
106       {
107         uint8_t* pixels = row;
108         for(int32_t i=0; i<pixelsPerRow; i++)
109         {
110           if((pixels[byte] & bits) != bits)
111           {
112             mAlphaChannelUsed = true;
113             j=mImageHeight; // break out of outer loop
114             break;
115           }
116           pixels+=mBytesPerPixel;
117         }
118         row += stride;
119       }
120     }
121   }
122 }
123
124 BitmapPackedPixel::~BitmapPackedPixel()
125 {
126   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
127 }
128
129 void BitmapPackedPixel::Initialize( Pixel::Format pixelFormat,
130                                     uint32_t width,
131                                     uint32_t height,
132                                     uint32_t bufferWidth,
133                                     uint32_t bufferHeight)
134 {
135   Dali::Integration::Bitmap::Initialize(pixelFormat, width, height);
136   mBufferWidth  = (bufferWidth  != 0) ? bufferWidth  : width;
137   mBufferHeight = (bufferHeight != 0) ? bufferHeight : height;
138   mBytesPerPixel = Pixel::GetBytesPerPixel(pixelFormat);
139   DALI_ASSERT_DEBUG(mBufferWidth >= mImageWidth && mBufferHeight >= mImageHeight);
140 }
141
142 uint32_t BitmapPackedPixel::GetBufferStride() const
143 {
144   return mBufferWidth*mBytesPerPixel;
145 }
146
147
148
149 } //namespace Integration
150
151 } //namespace Dali