1 /*
2 * Copyright (c) 2020-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 "gfx_utils/pixel_format_utils.h"
17
18 namespace OHOS {
19 static struct {
20 ImagePixelFormat pixelFormat;
21 int16_t bpp;
22 } g_mapBpp[] = {
23 {IMAGE_PIXEL_FORMAT_RGB565, 2},
24 {IMAGE_PIXEL_FORMAT_ARGB1555, 2},
25 {IMAGE_PIXEL_FORMAT_RGB888, 3},
26 {IMAGE_PIXEL_FORMAT_ARGB8888, 4},
27 };
28
BppOfPixelFormat(ImagePixelFormat pixelFormat,int16_t & bpp)29 bool PixelFormatUtils::BppOfPixelFormat(ImagePixelFormat pixelFormat, int16_t& bpp)
30 {
31 int16_t len = sizeof(g_mapBpp) / sizeof(g_mapBpp[0]);
32 for (int16_t i = 0; i < len; i++) {
33 if (pixelFormat == g_mapBpp[i].pixelFormat) {
34 bpp = g_mapBpp[i].bpp;
35 return true;
36 }
37 }
38 return false;
39 }
40
ARGB8888ToARGB1555(uint32_t color)41 uint16_t PixelFormatUtils::ARGB8888ToARGB1555(uint32_t color)
42 {
43 PF_ARGB1555 ret;
44 PF_ARGB8888 in;
45 in.full = color;
46 ret.alpha = in.alpha ? 1 : 0;
47 /*
48 * when 32-bitmap image is transformed to 16-bitmap
49 * R should shift right 3 bits
50 * G should shift right 3 bits
51 * B should shift right 3 bits
52 */
53 ret.red = in.red >> 3;
54 ret.green = in.green >> 3;
55 ret.blue = in.blue >> 3;
56 return ret.full;
57 }
58
ARGB1555ToARGB8888(uint16_t color)59 uint32_t PixelFormatUtils::ARGB1555ToARGB8888(uint16_t color)
60 {
61 PF_ARGB8888 ret;
62 PF_ARGB1555 in;
63 in.full = color;
64 ret.alpha = in.alpha ? 0xFF : 0x0;
65 /*
66 * when 16-bitmap image is transformed to 32-bitmap
67 * R should shift left 3 bits
68 * G should shift left 3 bits
69 * B should shift left 3 bits
70 */
71 ret.red = in.red << 3;
72 ret.green = in.green << 3;
73 ret.blue = in.blue << 3;
74 return ret.full;
75 }
76 } // namespace OHOS
77