25c79193bcc8311b8ba8b10e3df16798f17aa574
[platform/core/uifw/lottie-player.git] / src / vector / vbitmap.cpp
1 /* 
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
3  * 
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  * 
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  * 
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 #include "vbitmap.h"
20 #include <string.h>
21 #include "vglobal.h"
22
23 V_BEGIN_NAMESPACE
24
25 struct VBitmap::Impl {
26     uchar *             mData{nullptr};
27     uint                mWidth{0};
28     uint                mHeight{0};
29     uint                mStride{0};
30     uint                mBytes{0};
31     uint                mDepth{0};
32     VBitmap::Format     mFormat{VBitmap::Format::Invalid};
33     bool                mOwnData;
34     bool                mRoData;
35
36     Impl() = delete ;
37
38     Impl(uint width, uint height, VBitmap::Format format):
39         mOwnData(true),
40         mRoData(false)
41     {
42         mDepth = depth(format);
43         uint stride = ((width * mDepth + 31) >> 5) << 2; // bytes per scanline (must be multiple of 4)
44
45         mWidth = width;
46         mHeight = height;
47         mFormat = format;
48         mStride = stride;
49         mBytes = mStride * mHeight;
50         mData = reinterpret_cast<uchar *>(::operator new(mBytes));
51
52         if (!mData) {
53             // handle malloc failure
54             ;
55         }
56     }
57
58     Impl(uchar *data, uint w, uint h, uint bytesPerLine, VBitmap::Format format):
59         mOwnData(false),
60         mRoData(false)
61     {
62         mWidth = w;
63         mHeight = h;
64         mFormat = format;
65         mStride = bytesPerLine;
66         mBytes = mStride * mHeight;
67         mData = data;
68         mDepth = depth(format);
69     }
70
71     ~Impl()
72     {
73         if (mOwnData && mData) ::operator delete(mData);
74     }
75
76     uint stride() const {return mStride;}
77     uint width() const {return mWidth;}
78     uint height() const {return mHeight;}
79     VBitmap::Format format() const {return mFormat;}
80     uchar* data() { return mData;}
81
82     static uint depth(VBitmap::Format format)
83     {
84         uint depth = 1;
85         switch (format) {
86         case VBitmap::Format::Alpha8:
87             depth = 8;
88             break;
89         case VBitmap::Format::ARGB32:
90         case VBitmap::Format::ARGB32_Premultiplied:
91             depth = 32;
92             break;
93         default:
94             break;
95         }
96         return depth;
97     }
98     void fill(uint /*pixel*/)
99     {
100         //@TODO
101     }
102 };
103
104 VBitmap::VBitmap(uint width, uint height, VBitmap::Format format)
105 {
106     if (width <=0 || height <=0 || format == Format::Invalid) return;
107
108     mImpl = std::make_shared<Impl>(width, height, format);
109
110 }
111
112 VBitmap::VBitmap(uchar *data, uint width, uint height, uint bytesPerLine,
113                  VBitmap::Format format)
114 {
115     if (!data ||
116         width <=0 || height <=0 ||
117         bytesPerLine <=0 ||
118         format == Format::Invalid) return;
119
120     mImpl = std::make_shared<Impl>(data, width, height, bytesPerLine, format);
121 }
122
123 uint VBitmap::stride() const
124 {
125     return mImpl ? mImpl->stride() : 0;
126 }
127
128 uint VBitmap::width() const
129 {
130     return mImpl ? mImpl->width() : 0;
131 }
132
133 uint VBitmap::height() const
134 {
135     return mImpl ? mImpl->height() : 0;
136 }
137
138 uint VBitmap::depth() const
139 {
140     return mImpl ? mImpl->mDepth : 0;
141 }
142
143 uchar *VBitmap::data()
144 {
145     return mImpl ? mImpl->data() : nullptr;
146 }
147
148 uchar *VBitmap::data() const
149 {
150     return mImpl ? mImpl->data() : nullptr;
151 }
152
153 bool VBitmap::valid() const
154 {
155     return mImpl ? true : false;
156 }
157
158 VBitmap::Format VBitmap::format() const
159 {
160     return mImpl ? mImpl->format() : VBitmap::Format::Invalid;
161 }
162
163 void VBitmap::fill(uint pixel)
164 {
165     if (mImpl) mImpl->fill(pixel);
166 }
167
168 V_END_NAMESPACE