| 153 | |
| 154 | static Error invalidFilterError() { |
| 155 | return Error("Invalid filter"); |
| 156 | } |
| 157 | |
| 158 | static Result<Value> preprocessFilter(const Value& value) { |
| 159 | if (value.isNullOrUndefined()) { |
| 160 | return value; |
| 161 | } |
| 162 | |
| 163 | const auto* array = value.getArray(); |
| 164 | if (array == nullptr) { |
| 165 | return invalidFilterError(); |
| 166 | } |
| 167 | |
| 168 | if (array->empty()) { |
| 169 | return Value(); |
| 170 | } |
| 171 | |
| 172 | static constexpr int32_t kFilterTypeBlur = 1; |
| 173 | static constexpr int32_t kFilterTypeColorMatrix = 2; |
| 174 | |
| 175 | Ref<ImageFilter> filter = makeShared<ImageFilter>(); |
| 176 | |
| 177 | size_t i = 0; |
| 178 | size_t end = array->size(); |
| 179 | |
| 180 | while (i < end) { |
| 181 | switch ((*array)[i++].toInt()) { |
| 182 | case kFilterTypeBlur: |
| 183 | if (i + 1 > end) { |
| 184 | return invalidFilterError(); |
| 185 | } |
| 186 | if (filter->getBlurRadius() != 0.0f) { |
| 187 | return Error("Multiple blur filters are not supported"); |
| 188 | } |
| 189 | filter->setBlurRadius((*array)[i++].toFloat()); |
| 190 | break; |
| 191 | case kFilterTypeColorMatrix: |
| 192 | if (i + ImageFilter::kColorMatrixSize > end) { |
| 193 | return invalidFilterError(); |
| 194 | } |
| 195 | float colorMatrix[ImageFilter::kColorMatrixSize]; |
| 196 | for (float& j : colorMatrix) { |
| 197 | j = (*array)[i++].toFloat(); |
| 198 | } |
| 199 | |
| 200 | filter->concatColorMatrix(colorMatrix); |
| 201 | break; |
| 202 | default: |
| 203 | return invalidFilterError(); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | return Value(filter); |
| 208 | } |
nothing calls this directly
no test coverage detected