| 127 | } |
| 128 | |
| 129 | Valdi::Result<Valdi::BytesView> convertImage(const Valdi::StringBox& inputImageFilePath, |
| 130 | const Valdi::StringBox& outputImageFilePath, |
| 131 | const std::optional<int>& outputWidth, |
| 132 | const std::optional<int>& outputHeight, |
| 133 | double qualityRatio, |
| 134 | bool roundOutput) { |
| 135 | Valdi::Path outputPath(outputImageFilePath.toStringView()); |
| 136 | |
| 137 | snap::drawing::EncodedImageFormat outputFormat; |
| 138 | |
| 139 | auto outputFileExtension = outputPath.getFileExtension(); |
| 140 | |
| 141 | if (outputFileExtension == "png") { |
| 142 | outputFormat = snap::drawing::EncodedImageFormatPNG; |
| 143 | } else if (outputFileExtension == "webp") { |
| 144 | outputFormat = snap::drawing::EncodedImageFormatWebP; |
| 145 | } else if (outputFileExtension == "jpg" || outputFileExtension == "jpeg") { |
| 146 | outputFormat = snap::drawing::EncodedImageFormatJPG; |
| 147 | } else { |
| 148 | return Valdi::Error(STRING_FORMAT( |
| 149 | "Unsupported file extension '{}' for output file '{}'", outputFileExtension, outputImageFilePath)); |
| 150 | } |
| 151 | |
| 152 | auto imageResult = loadImage(inputImageFilePath, outputWidth, outputHeight); |
| 153 | if (!imageResult) { |
| 154 | return imageResult.moveError(); |
| 155 | } |
| 156 | auto image = imageResult.moveValue(); |
| 157 | |
| 158 | int newWidth = image->width(); |
| 159 | int newHeight = image->height(); |
| 160 | auto ratio = static_cast<double>(newWidth) / static_cast<double>(newHeight); |
| 161 | |
| 162 | if (outputWidth && outputHeight) { |
| 163 | newWidth = outputWidth.value(); |
| 164 | newHeight = outputHeight.value(); |
| 165 | } else if (outputWidth) { |
| 166 | newWidth = outputWidth.value(); |
| 167 | newHeight = static_cast<int>(round(newWidth / ratio)); |
| 168 | } else if (outputHeight) { |
| 169 | newHeight = outputHeight.value(); |
| 170 | newWidth = static_cast<int>(round(newHeight * ratio)); |
| 171 | } |
| 172 | |
| 173 | if (newWidth != image->width() || newHeight != image->height()) { |
| 174 | image = image->resized(newWidth, newHeight); |
| 175 | } |
| 176 | |
| 177 | if (roundOutput) { |
| 178 | if (newWidth != newHeight) { |
| 179 | return Valdi::Error("Round image output requires equal width and height"); |
| 180 | } |
| 181 | |
| 182 | auto clippedImage = clipImageToCircle(image); |
| 183 | if (!clippedImage) { |
| 184 | return clippedImage.moveError(); |
| 185 | } |
| 186 |
no test coverage detected