/*
Copyright 2022 juenbug12851
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* @file
* @brief Run mode: re-run the upscaler over an already-saved image.
*/
import fs from "node:fs";
import doUpscale from "./helpers/imageUpscaler.js";
import convertMetaToJSON from "./convertMetaToJSON.js";
/**
* Re-run the upscaler over an already-saved PNG (reads the file + its sidecar).
* @param {string} name The image file id to upscale.
* @param {object} settings The merged generation settings.
* @param {object} imageSettings The image settings.
* @param {object} upscaleSettings The upscale settings.
* @returns {Promise<void>}
*/
export default async function (name, settings, imageSettings, upscaleSettings) {
// Read file
let png = fs.readFileSync(`${imageSettings.saveTo}/${name}.png`);
png = Buffer.from(png).toString("base64");
let txt;
// Check to see if it's a JSON file or not, convert if it isn't
if (convertMetaToJSON.check(name, imageSettings))
txt = convertMetaToJSON.convert(name, undefined, settings, imageSettings, upscaleSettings);
else txt = JSON.parse(fs.readFileSync(`${imageSettings.saveTo}/${name}.json`, "utf8"));
// Directly save what this is an upscale of
txt.upscaleOf = name;
// Do upscale
await doUpscale(png, txt, imageSettings, upscaleSettings, true);
}