1 /*
2 * Copyright (C) 2023 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 #include "ext_pixel_convert.h"
16 #include "media_errors.h"
17
18 namespace {
19 constexpr uint32_t NUM_0 = 0;
20 constexpr uint32_t NUM_3 = 3;
21 constexpr uint32_t NUM_4 = 4;
22 }
23
24 namespace OHOS {
25 namespace ImagePlugin {
26 using namespace Media;
27
28 template<typename T>
Cast(uint8_t * buffer)29 static T* Cast(uint8_t* buffer)
30 {
31 void* tmp = buffer;
32 return static_cast<T*>(tmp);
33 }
34
35 struct RGBxPixel {
36 uint8_t r;
37 uint8_t g;
38 uint8_t b;
39 uint8_t x;
40 };
41
42 struct RGBPixel {
43 uint8_t r;
44 uint8_t g;
45 uint8_t b;
46 };
47
PixelCopy(RGBPixel * src,RGBxPixel * dst)48 void PixelCopy(RGBPixel* src, RGBxPixel* dst)
49 {
50 dst->r = src->r;
51 dst->g = src->g;
52 dst->b = src->b;
53 dst->x = NUM_0;
54 }
55
PixelCopy(RGBxPixel * src,RGBPixel * dst)56 void PixelCopy(RGBxPixel* src, RGBPixel* dst)
57 {
58 dst->r = src->r;
59 dst->g = src->g;
60 dst->b = src->b;
61 }
62
RGBxToRGB(const ExtPixels & src,ExtPixels & dst)63 uint32_t ExtPixelConvert::RGBxToRGB(const ExtPixels &src, ExtPixels &dst)
64 {
65 if (src.byteCount % NUM_4 != NUM_0) {
66 return ERR_IMAGE_INVALID_PARAMETER;
67 }
68 size_t srcPixelCount = src.byteCount / NUM_4;
69 if (srcPixelCount * NUM_3 > dst.byteCount) {
70 return ERR_IMAGE_TOO_LARGE;
71 }
72 RGBxPixel* srcPixel = Cast<RGBxPixel>(src.data);
73 RGBPixel* dstPixel = Cast<RGBPixel>(dst.data);
74 for (uint32_t i = NUM_0 ; i < srcPixelCount; i++) {
75 PixelCopy(&srcPixel[i], &dstPixel[i]);
76 }
77 return SUCCESS;
78 }
79
RGBToRGBx(const ExtPixels & src,ExtPixels & dst)80 uint32_t ExtPixelConvert::RGBToRGBx(const ExtPixels &src, ExtPixels &dst)
81 {
82 if (src.byteCount % NUM_3 != NUM_0) {
83 return ERR_IMAGE_INVALID_PARAMETER;
84 }
85 size_t srcPixelCount = src.byteCount / NUM_3;
86 if (srcPixelCount * NUM_4 > dst.byteCount) {
87 return ERR_IMAGE_TOO_LARGE;
88 }
89 RGBPixel* srcPixel = Cast<RGBPixel>(src.data);
90 RGBxPixel* dstPixel = Cast<RGBxPixel>(dst.data);
91 for (uint32_t i = NUM_0 ; i < srcPixelCount; i++) {
92 PixelCopy(&srcPixel[i], &dstPixel[i]);
93 }
94 return SUCCESS;
95 }
96 } // namespace ImagePlugin
97 } // namespace OHOS
98