1e9297d28Sopenharmony_ci/*
2e9297d28Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3e9297d28Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e9297d28Sopenharmony_ci * you may not use this file except in compliance with the License.
5e9297d28Sopenharmony_ci * You may obtain a copy of the License at
6e9297d28Sopenharmony_ci *
7e9297d28Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e9297d28Sopenharmony_ci *
9e9297d28Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e9297d28Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e9297d28Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e9297d28Sopenharmony_ci * See the License for the specific language governing permissions and
13e9297d28Sopenharmony_ci * limitations under the License.
14e9297d28Sopenharmony_ci */
15e9297d28Sopenharmony_ci
16e9297d28Sopenharmony_ci#include "input.h"
17e9297d28Sopenharmony_ci
18e9297d28Sopenharmony_cinamespace OHOS {
19e9297d28Sopenharmony_cinamespace Rosen {
20e9297d28Sopenharmony_civoid Input::DoProcess(ProcessData& data)
21e9297d28Sopenharmony_ci{
22e9297d28Sopenharmony_ci    if (format_ == "image/jpg" || format_ == "image/png") {
23e9297d28Sopenharmony_ci        DecodeFromFile(data);
24e9297d28Sopenharmony_ci    } else if (format_ == "pixelMap") {
25e9297d28Sopenharmony_ci        DecodeFromPixelMap(data);
26e9297d28Sopenharmony_ci    } else if (format_ == "buffer") {
27e9297d28Sopenharmony_ci        DecodeFromBuffer(data);
28e9297d28Sopenharmony_ci    }
29e9297d28Sopenharmony_ci}
30e9297d28Sopenharmony_ci
31e9297d28Sopenharmony_civoid Input::DecodeFromFile(ProcessData& data)
32e9297d28Sopenharmony_ci{
33e9297d28Sopenharmony_ci    uint32_t errorCode = 0;
34e9297d28Sopenharmony_ci    OHOS::Media::SourceOptions sourceOpts;
35e9297d28Sopenharmony_ci    sourceOpts.formatHint = format_;
36e9297d28Sopenharmony_ci    std::unique_ptr<OHOS::Media::ImageSource> imageSource =
37e9297d28Sopenharmony_ci        OHOS::Media::ImageSource::CreateImageSource(srcImagePath_, sourceOpts, errorCode);
38e9297d28Sopenharmony_ci    OHOS::Media::DecodeOptions decodeOpts;
39e9297d28Sopenharmony_ci    if (imageSource == nullptr) {
40e9297d28Sopenharmony_ci        LOGD("Failed to create imageSource!");
41e9297d28Sopenharmony_ci        return;
42e9297d28Sopenharmony_ci    }
43e9297d28Sopenharmony_ci    pixelMap_ = imageSource->CreatePixelMap(decodeOpts, errorCode);
44e9297d28Sopenharmony_ci    DecodeFromPixelMap(data);
45e9297d28Sopenharmony_ci}
46e9297d28Sopenharmony_ci
47e9297d28Sopenharmony_civoid Input::DecodeFromPixelMap(ProcessData& data)
48e9297d28Sopenharmony_ci{
49e9297d28Sopenharmony_ci    if (pixelMap_ == nullptr) {
50e9297d28Sopenharmony_ci        LOGD("Failed to create PixelMap!");
51e9297d28Sopenharmony_ci        return;
52e9297d28Sopenharmony_ci    }
53e9297d28Sopenharmony_ci    data.textureWidth = pixelMap_->GetWidth();
54e9297d28Sopenharmony_ci    data.textureHeight = pixelMap_->GetHeight();
55e9297d28Sopenharmony_ci
56e9297d28Sopenharmony_ci    glBindTexture(GL_TEXTURE_2D, data.srcTextureID);
57e9297d28Sopenharmony_ci    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
58e9297d28Sopenharmony_ci    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, data.textureWidth, data.textureHeight,
59e9297d28Sopenharmony_ci        0, GL_RGBA, GL_UNSIGNED_BYTE, pixelMap_->GetPixels());
60e9297d28Sopenharmony_ci    glGenerateMipmap(GL_TEXTURE_2D);
61e9297d28Sopenharmony_ci}
62e9297d28Sopenharmony_ci
63e9297d28Sopenharmony_civoid Input::DecodeFromBuffer(ProcessData& data)
64e9297d28Sopenharmony_ci{
65e9297d28Sopenharmony_ci    if (buffer_ == nullptr) {
66e9297d28Sopenharmony_ci        return;
67e9297d28Sopenharmony_ci    }
68e9297d28Sopenharmony_ci    data.textureWidth = bufferWidth_;
69e9297d28Sopenharmony_ci    data.textureHeight = bufferHeight_;
70e9297d28Sopenharmony_ci    glBindTexture(GL_TEXTURE_2D, data.srcTextureID);
71e9297d28Sopenharmony_ci    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
72e9297d28Sopenharmony_ci    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, data.textureWidth, data.textureHeight,
73e9297d28Sopenharmony_ci        0, GL_RGBA, GL_UNSIGNED_BYTE, buffer_.get());
74e9297d28Sopenharmony_ci    glGenerateMipmap(GL_TEXTURE_2D);
75e9297d28Sopenharmony_ci}
76e9297d28Sopenharmony_ciFILTER_TYPE Input::GetFilterType()
77e9297d28Sopenharmony_ci{
78e9297d28Sopenharmony_ci    return FILTER_TYPE::INPUT;
79e9297d28Sopenharmony_ci}
80e9297d28Sopenharmony_ci
81e9297d28Sopenharmony_civoid Input::SetValue(const std::string& key, std::shared_ptr<void> value, int size)
82e9297d28Sopenharmony_ci{
83e9297d28Sopenharmony_ci    if (key == "format" && size > 0) {
84e9297d28Sopenharmony_ci        std::shared_ptr<std::string> format = std::static_pointer_cast<std::string>(value);
85e9297d28Sopenharmony_ci        format_ = *(format.get());
86e9297d28Sopenharmony_ci        if (format_ == "jpg" || format_ == "jpeg") {
87e9297d28Sopenharmony_ci            format_ = "image/jpg";
88e9297d28Sopenharmony_ci        } else if (format_ == "png") {
89e9297d28Sopenharmony_ci            format_ = "image/png";
90e9297d28Sopenharmony_ci        } else if (format_ == "pixelMap") {
91e9297d28Sopenharmony_ci            format_ = "pixelMap";
92e9297d28Sopenharmony_ci        } else if (format_ == "buffer") {
93e9297d28Sopenharmony_ci            format_ = "buffer";
94e9297d28Sopenharmony_ci        }
95e9297d28Sopenharmony_ci        LOGD("The input format is %{public}s.", format_.c_str());
96e9297d28Sopenharmony_ci    } else if (key == "src" && size > 0) {
97e9297d28Sopenharmony_ci        if (format_ == "image/jpg" || format_ == "image/png") {
98e9297d28Sopenharmony_ci            std::shared_ptr<std::string> srcImagePath = std::static_pointer_cast<std::string>(value);
99e9297d28Sopenharmony_ci            srcImagePath_ = *(srcImagePath.get());
100e9297d28Sopenharmony_ci            LOGD("The input source image path is %{public}s.", srcImagePath_.c_str());
101e9297d28Sopenharmony_ci        } else if (format_ == "pixelMap") {
102e9297d28Sopenharmony_ci            pixelMap_ = std::static_pointer_cast<OHOS::Media::PixelMap>(value);
103e9297d28Sopenharmony_ci        } else if (format_ == "buffer") {
104e9297d28Sopenharmony_ci            buffer_ = std::static_pointer_cast<uint8_t>(value);
105e9297d28Sopenharmony_ci        }
106e9297d28Sopenharmony_ci    } else if (key == "bufferWidth" && size > 0) {
107e9297d28Sopenharmony_ci        std::shared_ptr<int> bufferWidth = std::static_pointer_cast<int>(value);
108e9297d28Sopenharmony_ci        bufferWidth_ = *(bufferWidth.get());
109e9297d28Sopenharmony_ci    } else if (key == "bufferHeight" && size > 0) {
110e9297d28Sopenharmony_ci        std::shared_ptr<int> bufferHeight = std::static_pointer_cast<int>(value);
111e9297d28Sopenharmony_ci        bufferHeight_ = *(bufferHeight.get());
112e9297d28Sopenharmony_ci    }
113e9297d28Sopenharmony_ci}
114e9297d28Sopenharmony_ci} // namespcae Rosen
115e9297d28Sopenharmony_ci} // namespace OHOS