• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "filter_factory.h"
17 #include "ec_log.h"
18 #include "builder.h"
19 
20 namespace OHOS {
21 namespace Rosen {
CreateFromConfig(std::string path)22 ImageChain* Builder::CreateFromConfig(std::string path)
23 {
24     char newpath[PATH_MAX + 1] = { 0x00 };
25     if (strlen(path.c_str()) > PATH_MAX || realpath(path.c_str(), newpath) == NULL) {
26         return nullptr;
27     }
28     std::ifstream configFile;
29     // open files
30     configFile.open(newpath);
31     std::stringstream JFilterParamsStream;
32     // read file's buffer contents into streams
33     JFilterParamsStream << configFile.rdbuf();
34     // close file handlers
35     configFile.close();
36     // convert stream into string
37     std::string JFilterParamsString = JFilterParamsStream.str();
38     cJSON* overallData = cJSON_Parse(JFilterParamsString.c_str());
39     if (overallData != nullptr) {
40         filters_ = cJSON_GetObjectItem(overallData, "filters");
41         if (filters_ != nullptr) {
42             AnalyseFilters(filters_);
43         }
44         connections_ = cJSON_GetObjectItem(overallData, "connections");
45         if (connections_ != nullptr) {
46             ConnectPipeline(connections_);
47         }
48         cJSON_Delete(overallData);
49     } else {
50         LOGE("The json file fails to compile.");
51         return nullptr;
52     }
53     if (inputs_.size() != 0) {
54         return new ImageChain(inputs_);
55     } else {
56         LOGE("No input.");
57         return nullptr;
58     }
59 }
60 
AnalyseFilters(cJSON * filters)61 void Builder::AnalyseFilters(cJSON* filters)
62 {
63     int size = cJSON_GetArraySize(filters);
64     for (int i = 0; i < size; i++) {
65         cJSON* item = cJSON_GetArrayItem(filters, i);
66         cJSON* type = cJSON_GetObjectItem(item, "type");
67         cJSON* name = cJSON_GetObjectItem(item, "name");
68         cJSON* params = cJSON_GetObjectItem(item, "params");
69         if (type != nullptr && name != nullptr) {
70             nameType_[name->valuestring] = type->valuestring;
71             auto tempFilter = algoFilterFactory->GetFilter(type);
72             if (tempFilter != nullptr) {
73                 ParseParams(tempFilter, params);
74                 nameFilter_[name->valuestring] = tempFilter;
75             }
76         }
77     }
78 }
79 
ParseParams(std::shared_ptr<Filter> filter,cJSON * params)80 void Builder::ParseParams(std::shared_ptr<Filter> filter, cJSON* params)
81 {
82     if (params == nullptr) {
83         return;
84     }
85     cJSON* childParam = params->child;
86     while (childParam != nullptr) {
87         if (cJSON_IsArray(childParam)) {
88             int arrayLength = cJSON_GetArraySize(childParam);
89             std::vector<float> tempArray(arrayLength, 0);
90             for (int i = 0; i < arrayLength; i++) {
91                 cJSON* arrayItem = cJSON_GetArrayItem(childParam, i);
92                 tempArray[i] = arrayItem->valuedouble;
93             }
94             filter->SetValue(childParam->string, &tempArray[0], arrayLength);
95         } else if (cJSON_IsNumber(childParam)) {
96             float tempValue = childParam->valuedouble;
97             filter->SetValue(childParam->string, &tempValue, 1);
98         } else if (cJSON_IsString(childParam)) {
99             filter->SetValue(childParam->string, childParam->valuestring, 1);
100         } else {
101             LOGE("Invalid input parameters!");
102         }
103         childParam = childParam->next;
104     }
105 }
106 
ConnectPipeline(cJSON * connections)107 void Builder::ConnectPipeline(cJSON* connections)
108 {
109     int size = cJSON_GetArraySize(connections);
110     for (int i = 0; i < size; i++) {
111         cJSON* item = cJSON_GetArrayItem(connections, i);
112         cJSON* from = cJSON_GetObjectItem(item, "from");
113         cJSON* to = cJSON_GetObjectItem(item, "to");
114         std::shared_ptr<Filter> fFilter = nullptr;
115         std::shared_ptr<Filter> tFilter = nullptr;
116         if (from != nullptr && to != nullptr) {
117             std::string fTypeName = nameType_[from->valuestring];
118             auto itFrom = nameFilter_.find(from->valuestring);
119             if (itFrom != nameFilter_.end()) {
120                 fFilter = itFrom->second;
121                 if (fFilter->GetFilterType() == FILTER_TYPE::INPUT) {
122                     inputs_.push_back(std::static_pointer_cast<Input>(fFilter));
123                 }
124             } else {
125                 LOGE("The from filter %{public}s fails to be connected", from->valuestring);
126             }
127             auto itTo = nameFilter_.find(to->valuestring);
128             if (itTo != nameFilter_.end()) {
129                 tFilter = itTo->second;
130             } else {
131                 LOGE("The to filter %{public}s fails to be connected", to->valuestring);
132             }
133             if (fFilter != nullptr && tFilter != nullptr) {
134                 fFilter->AddNextFilter(tFilter);
135             }
136         }
137     }
138 }
139 } // namespcae Rosen
140 } // namespace OHOS
141