Cleaning the code

This commit is contained in:
Christophe Meneboeuf 2022-06-16 23:39:24 +02:00
parent ed03b6a48f
commit ff08549ef1
12 changed files with 82 additions and 316 deletions

View File

@ -25,7 +25,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_CURRENT_SOURCE_DIR}/bin/debug)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_CURRENT_SOURCE_DIR}/bin/release)
# Library
add_library(${PROJECT_NAME} src/Common.h
add_library(${PROJECT_NAME}
src/HiRes.cpp
src/HiRes.h
src/ImageQuantized.cpp

View File

@ -1,6 +1,6 @@
/* Rgb2Hires
* Copyright (C) 2016 Christophe Meneboeuf <christophe@xtof.info>
* Copyright (C) 2016-2022 Christophe Meneboeuf <christophe@xtof.info>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by

View File

@ -1,52 +0,0 @@
/* Rgb2Hires
* Copyright (C) 2016 Christophe Meneboeuf <christophe@xtof.info>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __COMMON_H__
#define __COMMON_H__
#include <cstdint>
#include <SDL2/SDL.h>
namespace RgbToHires {
struct Color : public SDL_Color
{
inline bool operator==(const Color& rhs) const
{
return r == rhs.r && g == rhs.g && b == rhs.b;
}
inline bool operator==(const SDL_Color& rhs) const
{
return r == rhs.r && g == rhs.g && b == rhs.b;
}
};
constexpr Color WHITE {0xFF, 0xFF, 0xFF, 0xFF};
constexpr Color BLACK {0x00, 0x00, 0x00, 0xFF};
constexpr Color BLUE {0x07, 0xA8, 0xE0, 0xFF};
constexpr Color GREEN {0x43, 0xC8, 0x00, 0xFF};
constexpr Color ORANGE{0xF9, 0x56, 0x1D, 0xFF};
constexpr Color VIOLET{0xBB, 0x36, 0xFF, 0xFF};
constexpr unsigned WIDTH = 140u;
constexpr unsigned HEIGHT = 192u;
}
#endif

View File

@ -1,3 +1,21 @@
/* Rgb2Hires
* Copyright (C) 2016-2022 Christophe Meneboeuf <christophe@xtof.info>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <filesystem>
#include <stdexcept>
#include <chrono>
@ -6,7 +24,6 @@
#include <SDL.h>
#include <SDL_image.h>
#include "Picture.h"
#include "Display.h"
@ -16,7 +33,7 @@ using namespace std;
{
//! @brief Output the colors from a 14-dot block
void UpdateHiResRGBCell(const int x, const uint8_t* pLineAddr, rgba8Bits_t* pOut);
void UpdateHiResRGBCell(const int x, const uint8_t* pLineAddr, ColorRgb* pOut);
std::unique_ptr<Screen> ComputeRgbBuffer(const uint8_t* hires);
@ -99,7 +116,7 @@ using namespace std;
} error;
auto pViewport = ComputeRgbBuffer(hiresblob);
SDL_UpdateTexture(_pTexture, nullptr, pViewport->data(), sizeof(rgba8Bits_t) * 560);
SDL_UpdateTexture(_pTexture, nullptr, pViewport->data(), sizeof(ColorRgb) * 560);
SDL_RenderClear(_pRenderer);
SDL_RenderCopy(_pRenderer, _pTexture, NULL, NULL);
SDL_RenderPresent(_pRenderer);
@ -164,7 +181,7 @@ using namespace std;
// update the display with rgb data
{
std::lock_guard<std::mutex> lock{ this->_mutex }; // protecting pViewport
SDL_UpdateTexture(_pTexture, nullptr, pViewport->data(), sizeof(rgba8Bits_t) * 560);
SDL_UpdateTexture(_pTexture, nullptr, pViewport->data(), sizeof(ColorRgb) * 560);
}
SDL_RenderClear(_pRenderer);
SDL_RenderCopy(_pRenderer, _pTexture, NULL, NULL);
@ -184,14 +201,14 @@ using namespace std;
}
constexpr std::array<rgba8Bits_t, 7> Palette = {
rgba8Bits_t{0x00,0x00,0x00, 0xFF}, // black
rgba8Bits_t{0xFF,0xFF,0xFF, 0xFF}, // white
rgba8Bits_t{0x07,0xA8,0xE0, 0xFF}, // blue
rgba8Bits_t{0xF9,0x56,0x1D, 0xFF}, // orange
rgba8Bits_t{0x43,0xC8,0x00, 0xFF}, // green
rgba8Bits_t{0xBB,0x36,0xFF, 0xFF}, // violet
rgba8Bits_t{0x80,0x80,0x80, 0xFF} // dummy as AppleWin's code can overflow :( (no time to correct it)
constexpr std::array<ColorRgb, 7> Palette = {
BLACK,
WHITE,
BLUE,
ORANGE,
GREEN,
VIOLET,
BLACK // dummy
};
@ -205,7 +222,7 @@ using namespace std;
//! @param x Vertical position of the 14-dot block
//! @param pLineAddr pointer to the start of the line
//! @param pOut pointer to the 28-subdot block to draw
void UpdateHiResRGBCell(const int x, const uint8_t* pLineAddr, rgba8Bits_t* pOut)
void UpdateHiResRGBCell(const int x, const uint8_t* pLineAddr, ColorRgb* pOut)
{
const int xpixel = x * 14;
int xoffset = x & 1; // offset to start of the 2 bytes
@ -221,7 +238,7 @@ using namespace std;
uint32_t dwordval = (byteval1 & 0x7F) | ((byteval2 & 0x7F) << 7) | ((byteval3 & 0x7F) << 14) | ((byteval4 & 0x7F) << 21);
// Extraction of 14 color pixels
rgba8Bits_t colors[14];
ColorRgb colors[14];
int idxColor = 0;
uint32_t dwordval_tmp = dwordval;
dwordval_tmp = dwordval_tmp >> 7;
@ -238,7 +255,7 @@ using namespace std;
if (i % 2) dwordval_tmp >>= 2;
}
// Black and White
rgba8Bits_t bw[2];
ColorRgb bw[2];
bw[0] = Palette[0];
bw[1] = Palette[1];

View File

@ -29,18 +29,13 @@ struct SDL_Window;
struct SDL_Renderer;
struct SDL_Texture;
using namespace RgbToHires;
namespace Display
{
struct rgba8Bits_t
{
uint8_t r = 0;
uint8_t g = 0;
uint8_t b = 0;
uint8_t a = 0xff;
};
using Block = std::array<rgba8Bits_t, 14>;
using Block = std::array<ColorRgb, 14>;
using Line = std::array<Block, 40>;
using Screen = std::array<Line, 192 * 2>;

View File

@ -1,6 +1,6 @@
/* Rgb2Hires
* Copyright (C) 2016 Christophe Meneboeuf <christophe@xtof.info>
* Copyright (C) 2016-2022 Christophe Meneboeuf <christophe@xtof.info>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -34,7 +34,7 @@ namespace RgbToHires {
_data[1] = 0;
}
BlockHr::BlockHr(const BlockPixel& source)
BlockHr::BlockHr(const BlockPixelRgb& source)
{
const auto groups = getGroup(source);
//Init data, depending on the group
@ -56,7 +56,7 @@ namespace RgbToHires {
}
pair<BlockHr::eColorGroup, BlockHr::eColorGroup> BlockHr::getGroup(const BlockPixel& block) const
pair<BlockHr::eColorGroup, BlockHr::eColorGroup> BlockHr::getGroup(const BlockPixelRgb& block) const
{
pair<eColorGroup, eColorGroup> groups{ UNDEF, UNDEF };
//1st block group, including the last semi-pixel
@ -96,7 +96,7 @@ namespace RgbToHires {
}
uint8_t BlockHr::getDibit(const Color& color) const
uint8_t BlockHr::getDibit(const ColorRgb& color) const
{
if (color == WHITE) {
return 3;

View File

@ -1,6 +1,6 @@
/* Rgb2Hires
* Copyright (C) 2016 Christophe Meneboeuf <christophe@xtof.info>
* Copyright (C) 2016-2022 Christophe Meneboeuf <christophe@xtof.info>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -25,28 +25,52 @@
#include <vector>
#include <string>
#include "Common.h"
#include <SDL.h>
namespace RgbToHires
{
struct ColorRgb : public SDL_Color
{
inline bool operator==(const ColorRgb& rhs) const
{
return r == rhs.r && g == rhs.g && b == rhs.b;
}
inline bool operator==(const SDL_Color& rhs) const
{
return r == rhs.r && g == rhs.g && b == rhs.b;
}
};
// HIRES Palette
constexpr ColorRgb WHITE{ 0xFF, 0xFF, 0xFF, 0xFF };
constexpr ColorRgb BLACK{ 0x00, 0x00, 0x00, 0xFF };
constexpr ColorRgb BLUE{ 0x07, 0xA8, 0xE0, 0xFF };
constexpr ColorRgb GREEN{ 0x43, 0xC8, 0x00, 0xFF };
constexpr ColorRgb ORANGE{ 0xF9, 0x56, 0x1D, 0xFF };
constexpr ColorRgb VIOLET{ 0xBB, 0x36, 0xFF, 0xFF };
constexpr unsigned WIDTH = 140u;
constexpr unsigned HEIGHT = 192u;
static constexpr unsigned NB_PIXEL_PER_BLOCK = 7u;
static constexpr unsigned NB_BLOCK_PER_LINE = 20u;
static constexpr unsigned NB_LINES_PER_SCREEN = 192u;
constexpr std::array<const uint16_t, 192 / 8> LineAdresses = {
0x0000, 0x0080, 0x0100, 0x0180, 0x0200, 0x0280, 0x0300, 0x0380,
0x0028, 0x00a8, 0x0128, 0x01a8, 0x0228, 0x02a8, 0x0328, 0x03a8,
0x0050, 0x00d0, 0x0150, 0x01d0, 0x0250, 0x02d0, 0x0350, 0x03d0
};
}; ///< Base address of the interleaved lines
constexpr std::array<const uint16_t, 8> LineOffsets = {
0x0, 0x400, 0x800, 0xc00, 0x1000, 0x1400, 0x1800, 0x1c00
};
}; ///< Offset to get the full address
using BlockPixel = std::array<Color, 7u>;
using BlockPixelRgb = std::array<ColorRgb, 7u>;
/// @brief A block of 7 pixels
class BlockHr
@ -55,7 +79,7 @@ namespace RgbToHires
/// @brief Default constructor: all black
BlockHr();
/// @brief Construction from 7 pixels
BlockHr(const BlockPixel& );
BlockHr(const BlockPixelRgb& );
/// @brief returns the position of the first element
inline std::array<uint8_t, 2>::const_iterator begin() const {
return _data.begin();
@ -72,9 +96,9 @@ namespace RgbToHires
UNDEF // black and white can be group1 or group2
};
/// @brief Returns the color group of these two 3.5 pixel blocks
std::pair<eColorGroup, eColorGroup> getGroup(const BlockPixel&) const;
std::pair<eColorGroup, eColorGroup> getGroup(const BlockPixelRgb&) const;
/// @brief Returns the bit pait corresponding to the given color
uint8_t getDibit(const Color&) const;
uint8_t getDibit(const ColorRgb&) const;
std::array<uint8_t, 2> _data;
};

View File

@ -47,7 +47,7 @@ namespace RgbToHires {
BlockRgb blockRgb;
for (auto& pixel : blockRgb)
{
Color color;
ColorRgb color;
SDL_GetRGB(*srcPx, rgb->format, &color.r, &color.g, &color.b);
pixel = Quantize(color);
srcPx++;
@ -120,7 +120,7 @@ namespace RgbToHires {
double ImageQuantized::Distance(const Color& color1, const Color& color2)
double ImageQuantized::Distance(const ColorRgb& color1, const ColorRgb& color2)
{
static constexpr double LUMA_RED = 0.299;
@ -142,7 +142,7 @@ namespace RgbToHires {
}
inline Color ImageQuantized::Quantize(const Color& color)
inline ColorRgb ImageQuantized::Quantize(const ColorRgb& color)
{
const auto distBlack = Distance(BLACK, color);
auto distMin = distBlack;

View File

@ -1,6 +1,6 @@
/* Rgb2Hires
* Copyright (C) 2016 Christophe Meneboeuf <christophe@xtof.info>
* Copyright (C) 2016-2022 Christophe Meneboeuf <christophe@xtof.info>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -25,7 +25,6 @@
#include <map>
#include <string>
#include "Common.h"
#include "HiRes.h"
namespace RgbToHires {
@ -37,7 +36,7 @@ namespace RgbToHires {
public:
// rgb
using BlockRgb = std::array<Color, NB_PIXEL_PER_BLOCK>;
using BlockRgb = std::array<ColorRgb, NB_PIXEL_PER_BLOCK>;
using Line = std::array<BlockRgb, NB_BLOCK_PER_LINE>;
using BlobRgb = std::array<Line, NB_LINES_PER_SCREEN>;
// hires
@ -56,9 +55,9 @@ namespace RgbToHires {
/// @brief Returns an HIRES block
private:
Color Quantize(const Color& color);
ColorRgb Quantize(const ColorRgb& color);
/// @brief Computes the euclidian distance between two colors
double Distance(const Color&, const Color&);
double Distance(const ColorRgb&, const ColorRgb&);
private:

View File

@ -1,159 +0,0 @@
#include <memory>
#include "Picture.h"
using namespace std;
namespace RgbToHires
{
Picture::Picture(const CImg<uint8_t>& source)
{
//Filling the storage with BlockHrs
int lineNr = 0;
for (auto& line : _blobRgb)
{
int colNr = 0;
line.reserve(NB_BLOCKS_PER_LINE);
//Useful data
for (auto blockNr = 0u; blockNr < NB_BLOCKS_PER_LINE; ++blockNr)
{
BlockPixel blockPxRgb;
for (auto& pxRgb : blockPxRgb)
{
pxRgb = {
source(colNr, lineNr, 0, 0),
source(colNr, lineNr, 0, 1),
source(colNr, lineNr, 0, 2)
};
++colNr;
}
line.emplace_back(BlockHr{ blockPxRgb });
}
++lineNr;
}
//Constructing the map used to interleave the lines
auto i = 0u;
for (const auto& line : _blobRgb) {
auto addr_interleaved = LineAdresses[i / 8] + LineOffsets[i % 8];
_hrOrderedLines.insert(pair<const uint16_t, const LineHr*>(addr_interleaved, &line));
++i;
}
//Adding the 8 byte "memory holes"
for (auto line : _hrOrderedLines) {
if ((line.first & 0xFF) == 0x50 || (line.first & 0xFF) == 0xD0) {
for (auto i = 0u; i < 4u; ++i) {
const_cast<LineHr*>(line.second)->emplace_back(BlockHr{});
}
}
}
}
unique_ptr<array<uint8_t, Picture::FRAME_SIZE>> Picture::getBlob() const
{
auto blob = unique_ptr<array<uint8_t, FRAME_SIZE>>{ new array<uint8_t, FRAME_SIZE> };
auto byte_blob = begin(*blob);
for (const auto& line : _hrOrderedLines) {
for (const auto& block : *(line.second)) {
for (const auto byte_block : block) {
*byte_blob++ = byte_block;
}
}
}
return blob;
}
string Picture::getAsm() const
{
string assembly{ "Picture:\n" };
for (const auto& line : _hrOrderedLines) {
assembly += "\t.byte\t";
for (const auto& block : *(line.second)) {
for (const auto byte : block) {
assembly += to_string(byte) + ", ";
}
}
assembly.pop_back(); //removing the last coma
assembly.pop_back();
assembly += "\n";
}
return assembly;
}
void Picture::Quantize()
{
/*const auto dim = size();
if (dim.height() != HEIGHT || dim.width() != WIDTH) {
throw std::runtime_error("Image dimension must be 140x192 pixels.");
}
auto pixelpacket = getPixels(0u, 0u, WIDTH, HEIGHT);
for (auto i = 0u; i < HEIGHT; ++i)
{
for (auto j = 0u; j < WIDTH; ++j)
{
auto color = *pixelpacket;
const auto distBlack = Distance(BLACK, color);
const auto distWhite = Distance(WHITE, color);
const auto distBlue = Distance(BLUE, color);
const auto distGreen = Distance(GREEN, color);
const auto distOrange = Distance(ORANGE, color);
const auto distViolet = Distance(VIOLET, color);
const auto distMin = std::min({ distBlack, distWhite, distBlue, \
distGreen, distOrange, distViolet });
if (distMin == distBlack) {
*pixelpacket++ = BLACK;
}
else if (distMin == distWhite) {
*pixelpacket++ = WHITE;
}
else if (distMin == distBlue) {
*pixelpacket++ = BLUE;
}
else if (distMin == distGreen) {
*pixelpacket++ = GREEN;
}
else if (distMin == distOrange) {
*pixelpacket++ = ORANGE;
}
else {
*pixelpacket++ = VIOLET;
}
}
}*/
}
double Picture::Distance(const RGB_t& color1, const RGB_t& color2)
{
static constexpr double LUMA_RED = 0.299;
static constexpr double LUMA_GREEN = 0.587;
static constexpr double LUMA_BLUE = 0.114;
const auto y1 = LUMA_RED * color1.r + LUMA_GREEN * color1.g + LUMA_BLUE * color1.b;
const auto u1 = 0.492 * (color1.b - y1);
const auto v1 = 0.877 * (color1.r - y1);
const auto y2 = LUMA_RED * color2.r + LUMA_GREEN * color2.g + LUMA_BLUE * color2.b;
const auto u2 = 0.492 * (color2.b - y2);
const auto v2 = 0.877 * (color2.r - y2);
const auto dy = (y1 - y2);
const auto du = (u1 - u2);
const auto dv = (v1 - v2);
return (dy * dy + du * du + dv * dv);
}
}

View File

@ -1,58 +0,0 @@
/* Rgb2Hires
* Copyright (C) 2016 Christophe Meneboeuf <christophe@xtof.info>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _PICTURE_H_
#define _PICTURE_H_
#include <map>
#include "ImageQuantized.h"
#include "HiRes.h"
namespace RgbToHires
{
/// @brief A fullscreen HIRES picture
class Picture
{
static constexpr unsigned NB_BLOCKS_PER_LINE = 20u;
static constexpr unsigned NB_LINES_PER_SCREEN = 192u;
public:
static constexpr unsigned FRAME_SIZE = 192 * 40 + 512; ///< Frame size in byte
Picture(const ImageQuantized&);
~Picture() = default;
/// @brief Returns the binary hires picture
std::unique_ptr <std::array<uint8_t, FRAME_SIZE>> getBlob() const;
/// @brief Returns asm code corresponding to the image in memory (CA65 format)
std::string getHiresAsm() const;
private:
using LineHr = std::vector<BlockHr>;
using Blob = std::array<LineHr, NB_LINES_PER_SCREEN>;
Blob _blobRgb; ///< A frame ordered buffer of hires data
std::map<const uint16_t, const LineHr*> _hrOrderedLines; ///< map<adress,line's data>
};
}
#endif

View File

@ -13,7 +13,7 @@ RgbToHires::Tile::Tile(const ImageQuantized& source, const unsigned col, const u
line.reserve(NB_BLOCKS_PER_TILE);
//Useful data
for (auto blockNr = 0u; blockNr < NB_BLOCKS_PER_TILE; ++blockNr) {
BlockPixel blockPxRgb;
BlockPixelRgb blockPxRgb;
for (auto& pxRgb : blockPxRgb) {
pxRgb = *pixel_src++;
}