• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "c/drawing_bitmap.h"
17 
18 #include "draw/color.h"
19 #include "image/bitmap.h"
20 
21 using namespace OHOS;
22 using namespace Rosen;
23 using namespace Drawing;
24 
CastToBitmap(OH_Drawing_Bitmap * cBitmap)25 static Bitmap* CastToBitmap(OH_Drawing_Bitmap* cBitmap)
26 {
27     return reinterpret_cast<Bitmap*>(cBitmap);
28 }
29 
CColorFormatCastToColorType(OH_Drawing_ColorFormat cColorFormat)30 static ColorType CColorFormatCastToColorType(OH_Drawing_ColorFormat cColorFormat)
31 {
32     ColorType colorType = COLORTYPE_UNKNOWN;
33     switch (cColorFormat) {
34         case COLOR_FORMAT_UNKNOWN:
35             colorType = COLORTYPE_UNKNOWN;
36             break;
37         case COLOR_FORMAT_ALPHA_8:
38             colorType = COLORTYPE_ALPHA_8;
39             break;
40         case COLOR_FORMAT_RGB_565:
41             colorType = COLORTYPE_RGB_565;
42             break;
43         case COLOR_FORMAT_ARGB_4444:
44             colorType = COLORTYPE_ARGB_4444;
45             break;
46         case COLOR_FORMAT_RGBA_8888:
47             colorType = COLORTYPE_RGBA_8888;
48             break;
49         case COLOR_FORMAT_BGRA_8888:
50             colorType = COLORTYPE_BGRA_8888;
51             break;
52         default:
53             break;
54     }
55     return colorType;
56 }
57 
CAlphaFormatCastToAlphaType(OH_Drawing_AlphaFormat cAlphaFormat)58 static AlphaType CAlphaFormatCastToAlphaType(OH_Drawing_AlphaFormat cAlphaFormat)
59 {
60     AlphaType alphaType = ALPHATYPE_UNKNOWN;
61 
62     switch (cAlphaFormat) {
63         case ALPHA_FORMAT_UNKNOWN:
64             alphaType = ALPHATYPE_UNKNOWN;
65             break;
66         case ALPHA_FORMAT_OPAQUE:
67             alphaType = ALPHATYPE_OPAQUE;
68             break;
69         case ALPHA_FORMAT_PREMUL:
70             alphaType = ALPHATYPE_PREMUL;
71             break;
72         case ALPHA_FORMAT_UNPREMUL:
73             alphaType = ALPHATYPE_UNPREMUL;
74             break;
75         default:
76             break;
77     }
78     return alphaType;
79 }
80 
OH_Drawing_BitmapCreate()81 OH_Drawing_Bitmap* OH_Drawing_BitmapCreate()
82 {
83     return (OH_Drawing_Bitmap*)new Bitmap;
84 }
85 
OH_Drawing_BitmapDestroy(OH_Drawing_Bitmap * cBitmap)86 void OH_Drawing_BitmapDestroy(OH_Drawing_Bitmap* cBitmap)
87 {
88     delete CastToBitmap(cBitmap);
89 }
90 
OH_Drawing_BitmapBuild(OH_Drawing_Bitmap * cBitmap,const uint32_t width,const uint32_t height,const OH_Drawing_BitmapFormat * cBitmapFormat)91 void OH_Drawing_BitmapBuild(OH_Drawing_Bitmap* cBitmap, const uint32_t width, const uint32_t height,
92     const OH_Drawing_BitmapFormat* cBitmapFormat)
93 {
94     ColorType colorType = CColorFormatCastToColorType(cBitmapFormat->colorFormat);
95     AlphaType alphaType = CAlphaFormatCastToAlphaType(cBitmapFormat->alphaFormat);
96 
97     BitmapFormat format { colorType, alphaType };
98     CastToBitmap(cBitmap)->Build(width, height, format);
99 }
100 
OH_Drawing_BitmapGetWidth(OH_Drawing_Bitmap * cBitmap)101 uint32_t OH_Drawing_BitmapGetWidth(OH_Drawing_Bitmap* cBitmap)
102 {
103     return CastToBitmap(cBitmap)->GetWidth();
104 }
105 
OH_Drawing_BitmapGetHeight(OH_Drawing_Bitmap * cBitmap)106 uint32_t OH_Drawing_BitmapGetHeight(OH_Drawing_Bitmap* cBitmap)
107 {
108     return CastToBitmap(cBitmap)->GetHeight();
109 }
110 
OH_Drawing_BitmapGetPixels(OH_Drawing_Bitmap * cBitmap)111 void* OH_Drawing_BitmapGetPixels(OH_Drawing_Bitmap* cBitmap)
112 {
113     return CastToBitmap(cBitmap)->GetPixels();
114 }
115