SDL2 port: Tile

This commit is contained in:
Christophe Meneboeuf 2022-06-18 01:01:47 +02:00
parent a33c95d6c0
commit 7239fff361
6 changed files with 106 additions and 40 deletions

View File

@ -56,23 +56,20 @@ set_property(TARGET Picture PROPERTY
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG}) RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG})
## Application Tile # Application Tile
#add_executable(Tile src/App_Tile.cpp add_executable(Tile src/App_Tile.cpp)
# src/Tile.h ## custom definitions
# src/Tile.cpp target_compile_definitions(Tile PRIVATE cimg_use_png)
# ) ## dependencies
### custom definitions if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND "${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS "9")
#target_compile_definitions(Tile PRIVATE cimg_use_png) target_link_libraries(Tile stdc++fs) # filesystem lib not included in stdc++ for gcc < 9
### dependencies endif()
#if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND "${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS "9") conan_set_find_library_paths(Tile)
# target_link_libraries(Tile stdc++fs) # filesystem lib not included in stdc++ for gcc < 9 conan_target_link_libraries(Tile)
#endif() target_link_libraries(Tile ${PROJECT_NAME})
#conan_set_find_library_paths(Tile) ## output
#conan_target_link_libraries(Tile) set_property(TARGET Tile PROPERTY
#target_link_libraries(Picture ${PROJECT_NAME}) RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG}
### output )
#set_property(TARGET Tile PROPERTY
# RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG}
#)

View File

@ -23,16 +23,18 @@
#include <string> #include <string>
#include <sys/stat.h> #include <sys/stat.h>
#include <Magick++.h> #include <SDL2/SDL_image.h>
#include <tclap/CmdLine.h> #include <tclap/CmdLine.h>
#include "ImageQuantized.h" #include "ImageQuantized.h"
#include "Picture.h"
#include "Tile.h" #include "Tile.h"
using namespace std; using namespace std;
using namespace RgbToHires; using namespace RgbToHires;
/// @brief Returns true if a file exists /// @brief Returns true if a file exists
inline bool exists(const std::string& path) inline bool exists(const std::string& path)
{ {
@ -40,10 +42,25 @@ inline bool exists(const std::string& path)
return (stat(path.c_str(), &buffer) == 0); return (stat(path.c_str(), &buffer) == 0);
} }
void ExitOnError(const std::string& message,
std::vector<SDL_Surface*> surfaces = {})
{
std::cout << "Error\n" << message << '\n';
for (auto surface : surfaces)
{
SDL_FreeSurface(surface);
}
SDL_Quit();
IMG_Quit();
exit(-1);
}
/// @brief Program entry point /// @brief Program entry point
int main( int argc, char *argv[] ) int main( int argc, char *argv[] )
{ {
Magick::InitializeMagick(*argv);
//Parsing command line //Parsing command line
TCLAP::CmdLine cmd("Tile - by Christophe Meneboeuf <christophe@xtof.info>", ' ', "0"); TCLAP::CmdLine cmd("Tile - by Christophe Meneboeuf <christophe@xtof.info>", ' ', "0");
@ -69,28 +86,34 @@ int main( int argc, char *argv[] )
std::cout << "Line number shall be < 11" << std::endl; std::cout << "Line number shall be < 11" << std::endl;
return -1; return -1;
} }
try const auto filepath = imagePath.getValue();
{ if (!exists(filepath)) {
const auto filepath = imagePath.getValue(); throw runtime_error("Cannot read " + filepath);
if (!exists(filepath)) { }
throw runtime_error("Cannot read " + filepath); std::vector<SDL_Surface*> surfaces;
} SDL_Surface* surfaceRgb = IMG_Load(filepath.c_str());
const auto imageRgb = Magick::Image{ filepath }; surfaces.push_back(surfaceRgb);
auto imageQuantized = ImageQuantized{ imageRgb }; if (surfaceRgb == nullptr)
const auto tileHiRes = Tile{ imageQuantized, column.getValue(), line.getValue()}; {
ExitOnError("Cannot decode " + filepath, surfaces);
}
const ImageQuantized imageHiRes{ surfaceRgb };
const auto tileHiRes = imageHiRes.getTile(column.getValue(), line.getValue());
// Always output in asm // Always output in asm
ofstream output(outputPath.getValue()); ofstream output(outputPath.getValue());
output << tileHiRes.getHiresAsm(); output << tileHiRes;
for (auto surface : surfaces)
{
SDL_FreeSurface(surface);
} }
//Fatal error
catch (const exception& e) { SDL_Quit();
cout << e.what(); IMG_Quit();
return -1;
}
return 0; return 0;
} }

View File

@ -18,6 +18,8 @@
#include <stdexcept> #include <stdexcept>
#include <iterator> #include <iterator>
#include <iomanip>
#include <sstream>
#include "ImageQuantized.h" #include "ImageQuantized.h"
@ -119,6 +121,43 @@ namespace RgbToHires {
std::string ImageQuantized::getTile(const unsigned col, const unsigned line) const
{
constexpr int TILE_W = 14;
constexpr int TILE_H = 16;
std::string assembly;
for (auto lineNr = line * TILE_H;
lineNr < TILE_H * (line + 1); ++lineNr)
{
std::stringstream stream;
stream << ".byte ";
auto& line = _blobHr[lineNr];
const unsigned nbBlockPerTile = TILE_W / NB_PIXEL_PER_BLOCK;
for (unsigned blockHrNr = col * nbBlockPerTile;
blockHrNr < nbBlockPerTile * (col + 1); ++blockHrNr)
{
const auto& block = line[blockHrNr];
for (const auto byte : block)
{
stream << '$' << std::setw(2) << std::setfill('0') << std::uppercase
<< std::hex << static_cast<int>(byte) << ", ";
}
}
assembly += stream.str();
assembly.pop_back(); //removing the last coma
assembly.pop_back();
assembly += "\n";
}
return assembly;
}
double ImageQuantized::Distance(const ColorRgb& color1, const ColorRgb& color2) double ImageQuantized::Distance(const ColorRgb& color1, const ColorRgb& color2)
{ {

View File

@ -48,11 +48,18 @@ namespace RgbToHires {
ImageQuantized(SDL_Surface* const source); ImageQuantized(SDL_Surface* const source);
~ImageQuantized() = default; ~ImageQuantized() = default;
// Buffer
/// @brief Returns the binary hires picture /// @brief Returns the binary hires picture
std::unique_ptr <std::array<uint8_t, FRAME_SIZE>> getHiresBuffer() const; std::unique_ptr <std::array<uint8_t, FRAME_SIZE>> getHiresBuffer() const;
/// @brief Returns asm code corresponding to the image in memory (CA65 format) /// @brief Returns asm code corresponding to the image in memory (CA65 format)
std::string getHiresAsm() const; std::string getHiresAsm() const;
/// @brief Returns an HIRES block
// Tile
/// @brief Returns an HIRES tile. The line are not interleaved as in an HIRES framebuffer
/// @param col Position: column number of the tile, from 0 to 9
/// @param col Position: line number of the tile, from 0 to 11
std::string getTile(const unsigned col, const unsigned line) const;
private: private:
ColorRgb Quantize(const ColorRgb& color); ColorRgb Quantize(const ColorRgb& color);

View File

Before

Width:  |  Height:  |  Size: 4.1 KiB

After

Width:  |  Height:  |  Size: 4.1 KiB

BIN
Rgb2Hires_PC/testTile.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB