Skip to content

Commit

Permalink
Test images creator
Browse files Browse the repository at this point in the history
  • Loading branch information
awawa-dev committed Aug 12, 2024
1 parent fa7eff8 commit b97f332
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/utils-image/utils-image.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ namespace utils_image
void _IMAGE_SHARED_API svg2png(const std::string& svgFile, int width, int height, std::vector<uint8_t>& buffer);
ColorRgb _IMAGE_SHARED_API colorRgbfromString(const std::string& colorName);
void _IMAGE_SHARED_API encodeJpeg(std::vector<uint8_t>& buffer, Image<ColorRgb>& inputImage, bool scaleDown);
bool _IMAGE_SHARED_API savePng(const std::string& filename, const Image<ColorRgb>& image);
};

115 changes: 113 additions & 2 deletions sources/lut-calibrator/LutCalibrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include <led-strip/ColorSpaceCalibration.h>
#include <lut-calibrator/ColorSpace.h>
#include <linalg.h>
#include <utils-image/utils-image.h>


using namespace linalg;
Expand Down Expand Up @@ -78,6 +79,118 @@ ColorRgb LutCalibrator::primeColors[] = {
LutCalibrator* LutCalibrator::instance = nullptr;


namespace
{
const int SCREEN_BLOCKS_X = 60;
const int SCREEN_BLOCKS_Y = 30;
const int SCREEN_COLOR_STEP = 16;
}

static int indexToColorAndPos(int index, ColorRgb& color, int2& position)
{
int currentIndex = 0;
int boardIndex = 0;

position = int2(0, 1);

for (int R = 0; R <= 256; R += SCREEN_COLOR_STEP)
for (int G = 0; G <= 256; G += SCREEN_COLOR_STEP)
for (int B = 0; B <= 256; B += SCREEN_COLOR_STEP, currentIndex++)
if (index == currentIndex)
{
color.red = std::min(R, 255);
color.green = std::min(G, 255);
color.blue = std::min(B, 255);
return boardIndex;
}
else
{
position.x++;
if (position.x >= SCREEN_BLOCKS_X)
{
position.x = 0;
position.y++;
}
if (position.y >= SCREEN_BLOCKS_Y - 1)
{
position.y = 1;
boardIndex++;
}
}
return -1;
}

static void createTestBoards()
{
int maxIndex = std::pow(((256 / SCREEN_COLOR_STEP) + 1), 3);
int boardIndex = 0;
Image<ColorRgb> image(1920, 1080);
const int dX = image.width() / SCREEN_BLOCKS_X;
const int dY = image.height() / SCREEN_BLOCKS_Y;

auto saveImage = [](Image<ColorRgb> &image, int dX, int dY, int boardIndex)
{
for (int line = 0; line < SCREEN_BLOCKS_Y; line += SCREEN_BLOCKS_Y - 1)
{
int currentX = 0;

// white
int sx = currentX * dX, sy = line * dY;
int ex = (++currentX) * dX - 1, ey = (line + 1) * dY - 1;
image.fastBox(sx, sy, ex, ey, 255, 255, 255);

// black
sx = currentX * dX;
ex = (++currentX) * dX - 1;
image.fastBox(sx, sy, ex, ey, 0, 0, 0);

// crc
for (int x = 15; x >= 0; x--)
{
int crc = (((boardIndex%2) ? 0x55 : 0xaa) << 8) | boardIndex;
sx = currentX * dX;
ex = (++currentX) * dX - 1;

uint8_t color = (crc & (1 << x)) ? 255 : 0;
image.fastBox(sx, sy, ex, ey, color, color, color);
}
}
utils_image::savePng(QString("D:/table_%1.png").arg(QString::number(boardIndex)).toStdString(), image);
};


image.clear();

if (256 % SCREEN_COLOR_STEP > 0 || image.width() % SCREEN_BLOCKS_X || image.height() % SCREEN_BLOCKS_Y)
return;

for(int index = 0; index < maxIndex; index++)
{
ColorRgb color;
int2 position;
int currentBoard = indexToColorAndPos(index, color, position);

if (currentBoard < 0)
return;

if (boardIndex != currentBoard)
{
saveImage(image, dX, dY, boardIndex);
image.clear();
boardIndex = currentBoard;
}

int sx = position.x * dX;
int sy = position.y * dY;
int ex = (position.x + 1) * dX - 1;
int ey = (position.y + 1) * dY - 1;

image.fastBox(sx, sy, ex, ey, color.red, color.green, color.blue);

}
saveImage(image, dX, dY, boardIndex);
}

LutCalibrator::LutCalibrator()
{
_log = Logger::getInstance("CALIBRATOR");
Expand Down Expand Up @@ -140,8 +253,6 @@ enum TEST_COLOR_ID {
};

namespace {
const int SCREEN_BLOCKS_X = 64;
const int SCREEN_BLOCKS_Y = 36;
const int LOGIC_BLOCKS_X_SIZE = 4;
const int LOGIC_BLOCKS_X = std::floor((SCREEN_BLOCKS_X - 1) / LOGIC_BLOCKS_X_SIZE);
const int COLOR_DIVIDES = 32;
Expand Down
5 changes: 5 additions & 0 deletions sources/utils-image/utils-image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,9 @@ namespace utils_image
tjDestroy(_jpegCompressor);
tjFree(compressedImage);
}

bool _IMAGE_SHARED_API savePng(const std::string& filename, const Image<ColorRgb>& image)
{
return stbi_write_png(filename.c_str(), image.width(), image.height(), 3, image.rawMem(), 0);
}
};

0 comments on commit b97f332

Please sign in to comment.