| 128 | } |
| 129 | |
| 130 | void upscalerModel(const char *UpscaleModelPath, uint32_t UpscaleRepeats, |
| 131 | int32_t NThreads, uint32_t BatchCount, sd_image_t *Results) { |
| 132 | // unused for RealESRGAN_x4plus_anime_6B.pth |
| 133 | int UpscaleFactor = 4; |
| 134 | upscaler_ctx_t *UpscalerCtx = new_upscaler_ctx(UpscaleModelPath, NThreads); |
| 135 | if (UpscalerCtx == nullptr) { |
| 136 | spdlog::error("[WasmEdge-StableDiffusion] Create upscaler ctx failed."sv); |
| 137 | } else { |
| 138 | for (uint32_t I = 0; I < BatchCount; I++) { |
| 139 | if (Results[I].data == nullptr) { |
| 140 | continue; |
| 141 | } |
| 142 | sd_image_t CurrentImage = Results[I]; |
| 143 | for (uint32_t U = 0; U < UpscaleRepeats; ++U) { |
| 144 | sd_image_t UpscaledImage = |
| 145 | upscale(UpscalerCtx, CurrentImage, UpscaleFactor); |
| 146 | if (UpscaledImage.data == nullptr) { |
| 147 | spdlog::error("[WasmEdge-StableDiffusion] Upscale failed."sv); |
| 148 | break; |
| 149 | } |
| 150 | free(CurrentImage.data); |
| 151 | CurrentImage = UpscaledImage; |
| 152 | } |
| 153 | // Set the final upscaled image as the result. |
| 154 | Results[I] = CurrentImage; |
| 155 | } |
| 156 | } |
| 157 | free(UpscalerCtx); |
| 158 | } |
| 159 | |
| 160 | bool saveResults(sd_image_t *Results, uint32_t BatchCount, |
| 161 | std::string OutputPath, uint32_t OutputPathLen, |