[nostalgia/core/studio] Move imgconv to Core Studio

This commit is contained in:
2019-10-22 23:47:58 -05:00
parent aa171fa069
commit 0a809037ac
6 changed files with 12 additions and 27 deletions

View File

@@ -1,7 +1,6 @@
add_library(
NostalgiaPack SHARED
imgconv.cpp
pack.cpp
)

View File

@@ -1,121 +0,0 @@
/*
* Copyright 2016 - 2019 gtalent2@gmail.com
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <QColor>
#include <QImage>
#include <QMap>
#include <nostalgia/core/gba/gba.hpp>
#include <nostalgia/core/core.hpp>
#include "imgconv.hpp"
namespace nostalgia {
namespace {
[[nodiscard]] uint16_t toGbaColor(QColor c) {
const auto r = static_cast<uint32_t>(c.red()) >> 3;
const auto g = static_cast<uint32_t>(c.green()) >> 3;
const auto b = static_cast<uint32_t>(c.blue()) >> 3;
return (r << 10) | (g << 5) | (b << 0);
}
[[nodiscard]] int pointToIdx(int w, int x, int y) {
const auto colLength = 64;
const auto rowLength = (w / 8) * colLength;
const auto colStart = colLength * (x / 8);
const auto rowStart = rowLength * (y / 8);
const auto colOffset = x % 8;
const auto rowOffset = (y % 8) * 8;
return colStart + colOffset + rowStart + rowOffset;
}
}
[[nodiscard]] static int countColors(const QImage &img, int argTiles) {
QMap<QRgb, bool> colors;
// copy pixels as color ids
for (int x = 0; x < img.width(); x++) {
for (int y = 0; y < img.height(); y++) {
auto destI = pointToIdx(img.width(), x, y);
if (destI <= argTiles * 64) {
auto c = img.pixel(x, y);
// assign color a color id for the palette
if (!colors.contains(c)) {
colors[c] = true;
}
}
}
}
return colors.size();
}
[[nodiscard]] std::unique_ptr<core::NostalgiaGraphic> imgToNg(QString argSrc, int argTiles, int argBpp) {
constexpr auto TilePixels = 64;
QImage src(argSrc);
if (src.isNull()) {
return {};
}
const auto Pixels = argTiles ? argTiles * TilePixels : src.width() * src.height();
if (argTiles == 0) {
argTiles = Pixels / 64;
}
const auto Colors = countColors(src, argTiles);
if (argBpp != 4 && argBpp != 8) {
argBpp = Colors > 16 ? 8 : 4;
}
QMap<QRgb, int> colors;
auto ng = std::make_unique<core::NostalgiaGraphic>();
ng->pal.resize(countColors(src, argTiles));
if (argBpp == 4) {
ng->tiles.resize(Pixels / 2);
} else {
ng->tiles.resize(Pixels);
}
ng->bpp = argBpp;
int colorIdx = 0;
// copy pixels as color ids
for (int x = 0; x < src.width(); x++) {
for (int y = 0; y < src.height(); y++) {
auto destI = pointToIdx(src.width(), x, y);
if (destI <= argTiles * 64) {
const auto c = src.pixel(x, y);
// assign color a color id for the palette
if (!colors.contains(c)) {
colors[c] = colorIdx;
colorIdx++;
}
// set pixel color
if (argBpp == 4) {
if (destI % 2) { // is odd number pixel
ng->tiles[destI / 2] |= colors[c] << 4;
} else {
ng->tiles[destI / 2] |= colors[c];
}
} else {
ng->tiles[destI] = colors[c];
}
}
}
}
// store colors in palette with the corresponding color id
for (auto key : colors.keys()) {
auto colorId = colors[key];
ng->pal[colorId] = toGbaColor(key);
}
return ng;
}
}

View File

@@ -1,34 +0,0 @@
/*
* Copyright 2016 - 2019 gtalent2@gmail.com
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <QString>
#include <ox/std/error.hpp>
#include <ox/std/types.hpp>
#include <ox/mc/mc.hpp>
#include <nostalgia/core/gfx.hpp>
namespace nostalgia {
template<typename T>
[[nodiscard]] ox::ValErr<std::vector<uint8_t>> toBuffer(T *data, std::size_t buffSize = ox::units::MB) {
std::vector<uint8_t> buff(buffSize);
std::size_t sz = 0;
oxReturnError(ox::writeMC(buff.data(), buff.size(), data, &sz));
if (sz > buffSize) {
return OxError(1);
}
buff.resize(sz);
return buff;
}
[[nodiscard]] std::unique_ptr<core::NostalgiaGraphic> imgToNg(QString argInPath, int argTiles, int argBpp = -1);
}

View File

@@ -10,7 +10,6 @@
#include <string_view>
#include <vector>
#include "imgconv.hpp"
#include "pack.hpp"
namespace nostalgia {
@@ -83,21 +82,10 @@ namespace {
} else {
std::vector<uint8_t> buff;
// do transforms
const std::string OldExt = path.substr(path.find_last_of('.'));
constexpr std::string_view NgExt = ".ng";
if (OldExt != NgExt) {
// load file from full path and transform
const auto fullPath = src->basePath() + currentFile;
oxReturnError(toBuffer(imgToNg(fullPath.c_str(), 0, 0).get()).get(&buff));
currentFile = currentFile.substr(0, currentFile.size() - OldExt.size()) + NgExt.data();
if (!buff.size()) {
return OxError(1);
}
} else {
// load file
buff.resize(stat.size);
oxReturnError(src->read(currentFile.c_str(), buff.data(), buff.size()));
}
//const std::string OldExt = path.substr(path.find_last_of('.'));
// load file
buff.resize(stat.size);
oxReturnError(src->read(currentFile.c_str(), buff.data(), buff.size()));
// write file to dest
std::cout << "writing " << currentFile << '\n';
oxReturnError(dest->write(currentFile.c_str(), buff.data(), buff.size()));