[nostalgia/core/studio] Move imgconv to Core Studio
This commit is contained in:
@@ -1,19 +1,17 @@
|
||||
|
||||
if(NOSTALGIA_BUILD_TYPE STREQUAL "Native")
|
||||
add_library(
|
||||
NostalgiaCore-Studio SHARED
|
||||
import_tilesheet_wizard.cpp
|
||||
new_tilesheet_wizard.cpp
|
||||
plugin.cpp
|
||||
)
|
||||
endif()
|
||||
add_library(
|
||||
NostalgiaCore-Studio SHARED
|
||||
imgconv.cpp
|
||||
import_tilesheet_wizard.cpp
|
||||
new_tilesheet_wizard.cpp
|
||||
plugin.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(
|
||||
NostalgiaCore-Studio
|
||||
Qt5::Core
|
||||
Qt5::Widgets
|
||||
NostalgiaStudio
|
||||
NostalgiaPack
|
||||
OxFS
|
||||
OxStd
|
||||
)
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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);
|
||||
|
||||
}
|
||||
@@ -11,8 +11,8 @@
|
||||
#include <QFile>
|
||||
|
||||
#include <nostalgia/core/consts.hpp>
|
||||
#include <nostalgia/tools/pack/imgconv.hpp>
|
||||
|
||||
#include "imgconv.hpp"
|
||||
#include "import_tilesheet_wizard.hpp"
|
||||
|
||||
namespace nostalgia::core {
|
||||
|
||||
Reference in New Issue
Block a user