operations apply over images

main
Brett 2024-01-16 14:53:31 -05:00
parent c4b2de9503
commit dc7bd2fd22
7 changed files with 248 additions and 30 deletions

Binary file not shown.

View File

@ -202,3 +202,11 @@
26737 26901 1705257472228626060 libraries/BLT-With-Graphics-Template/libraries/freetype-2.13.2/libfreetype.a 1ef1507fdc5a3960 26737 26901 1705257472228626060 libraries/BLT-With-Graphics-Template/libraries/freetype-2.13.2/libfreetype.a 1ef1507fdc5a3960
26901 27054 1705257472380619772 libraries/BLT-With-Graphics-Template/libBLT_WITH_GRAPHICS.a 2582d8919b3ccd7b 26901 27054 1705257472380619772 libraries/BLT-With-Graphics-Template/libBLT_WITH_GRAPHICS.a 2582d8919b3ccd7b
27054 27191 1705257472520613980 gp_image_test 627b40e9bcf815e3 27054 27191 1705257472520613980 gp_image_test 627b40e9bcf815e3
9 49 1705257551849331804 libraries/BLT-With-Graphics-Template/libraries/openal-soft/version_witness.txt 6325405b7e3e96f7
9 49 1705257551849331804 libraries/BLT-With-Graphics-Template/libraries/openal-soft/version.h 6325405b7e3e96f7
9 49 1705257551849331804 /home/brett/Documents/code/c++/gp_image_test/cmake-build-release/libraries/BLT-With-Graphics-Template/libraries/openal-soft/version_witness.txt 6325405b7e3e96f7
9 49 1705257551849331804 /home/brett/Documents/code/c++/gp_image_test/cmake-build-release/libraries/BLT-With-Graphics-Template/libraries/openal-soft/version.h 6325405b7e3e96f7
9 2237 1705433087834197608 libraries/BLT-With-Graphics-Template/libraries/BLT/CMakeFiles/BLT.dir/src/blt/std/system.cpp.o 19facd5153bbeb65
2237 2427 1705433088022195202 libraries/BLT-With-Graphics-Template/libraries/BLT/libBLT.a 5e3491f3bb42050d
9 3115 1705433088714186351 CMakeFiles/gp_image_test.dir/src/main.cpp.o d2247c8eaef04c80
3116 3265 1705433088862184459 gp_image_test 627b40e9bcf815e3

View File

@ -1,3 +1,3 @@
Start testing: Jan 15 14:34 EST Start testing: Jan 16 14:24 EST
---------------------------------------------------------- ----------------------------------------------------------
End testing: Jan 15 14:34 EST End testing: Jan 16 14:24 EST

Binary file not shown.

121
include/image.h Normal file
View File

@ -0,0 +1,121 @@
/*
* <Short Description>
* Copyright (C) 2024 Brett Terpstra
*
* 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 <https://www.gnu.org/licenses/>.
*/
#ifndef GP_IMAGE_TEST_IMAGE_H
#define GP_IMAGE_TEST_IMAGE_H
#define NO_BLT_NAMESPACE_ON_TYPES
#include <blt/std/types.h>
#include <array>
#include <optional>
#include "blt/std/assert.h"
inline constexpr i32 width = 1024, height = 1024;
class image
{
private:
std::array<u8, width * height * 4> data;
public:
image() = default;
u8 get(i32 x, i32 y)
{
return data[y * height + x];
}
void set(u8 c, i32 x, i32 y)
{
data[y * height + x] = c;
}
};
enum class function_t
{
// FUNC // inputs
ADD, // 2
SUB, // 2
MUL, // 2
DIV, // 2
EXP, // 2
LOG, // 1
SQRT, // 1
QUAD, // 1
RANDOM, // 0
NOISE, // 0
COLOR, // 0
SCALAR // 0
};
inline i32 inputs(function_t op)
{
// don't like this but it'll get compiled out
// and we get warnings when new enum is added
switch (op)
{
case function_t::ADD:
case function_t::SUB:
case function_t::MUL:
case function_t::DIV:
case function_t::EXP:
return 2;
case function_t::LOG:
case function_t::SQRT:
case function_t::QUAD:
return 1;
case function_t::RANDOM:
case function_t::NOISE:
case function_t::COLOR:
case function_t::SCALAR:
return 0;
}
BLT_THROW(std::runtime_error("If you are seeing this the universe has broken. Enjoy whatever hellhole is left"));
}
struct op_con
{
function_t op;
std::optional<image*> i1, i2;
op_con(function_t op, const std::optional<image*>& i1, const std::optional<image*>& i2): op(op), i1(i1), i2(i2)
{}
[[nodiscard]] bool has_both() const
{
return i1.has_value() && i2.has_value();
}
[[nodiscard]] bool has_one() const
{
return i1.has_value() || i2.has_value();
}
[[nodiscard]] image* getOne()
{
if (i1)
return i1.value();
if (i2)
return i2.value();
BLT_THROW(std::runtime_error("Unable to get when one is missing! You have an error in your GA!"));
}
};
#endif //GP_IMAGE_TEST_IMAGE_H

View File

@ -3,9 +3,12 @@
#include <blt/gfx/window.h> #include <blt/gfx/window.h>
#include <blt/gfx/state.h> #include <blt/gfx/state.h>
#include <blt/gfx/stb/stb_image.h> #include <blt/gfx/stb/stb_image.h>
#include <blt/gfx/stb/stb_perlin.h>
#include <blt/std/hashmap.h> #include <blt/std/hashmap.h>
#include <image.h>
#include "blt/gfx/renderer/resource_manager.h" #include "blt/gfx/renderer/resource_manager.h"
#include "blt/gfx/renderer/batch_2d_renderer.h" #include "blt/gfx/renderer/batch_2d_renderer.h"
#include "blt/std/assert.h"
#include <variant> #include <variant>
#include <random> #include <random>
@ -15,29 +18,118 @@ blt::gfx::batch_renderer_2d renderer_2d(resources);
struct node; struct node;
blt::area_allocator<node, 32000> allocator; blt::area_allocator<node, 32000> node_allocator;
blt::area_allocator<image, 32000> img_allocator;
std::variant<float, blt::vec3f> input_t; node* createNode()
enum class function_t
{ {
// FUNC // inputs auto* n = node_allocator.allocate(1);
ADD, // 2 node_allocator.construct(n);
SUB, // 2 return n;
MUL, // 2 }
DIV, // 2
image* createImage()
LOG, // 1 {
EXP, // 1 auto* i = img_allocator.allocate(1);
SQRT, // 1 img_allocator.construct(i);
POW, // 1 return i;
QUAD, // 1 }
RANDOM, // 0 void destroyNode(node* n)
NOISE, // 0 {
COLOR, // 0 node_allocator.destroy(n);
SCALAR // 0 node_allocator.deallocate(n, 1);
}; }
void destroyImage(image* img)
{
img_allocator.destroy(img);
img_allocator.deallocate(img, 1);
}
image* apply_operator(op_con operation)
{
auto* ret = createImage();
for (i32 y = 0; y < height; y++)
{
for (i32 x = 0; x < width; x++)
{
switch (operation.op)
{
case function_t::ADD:
{
BLT_ASSERT(operation.has_both());
ret->set((operation.i1.value()->get(x, y) + operation.i2.value()->get(x, y)) % std::numeric_limits<u8>::max(), x, y);
break;
}
case function_t::SUB:
{
BLT_ASSERT(operation.has_both());
ret->set((operation.i1.value()->get(x, y) - operation.i2.value()->get(x, y)) % std::numeric_limits<u8>::max(), x, y);
break;
}
case function_t::MUL:
{
BLT_ASSERT(operation.has_both());
ret->set((operation.i1.value()->get(x, y) * operation.i2.value()->get(x, y)) % std::numeric_limits<u8>::max(), x, y);
break;
}
case function_t::DIV:
{
BLT_ASSERT(operation.has_both());
auto den = operation.i2.value()->get(x, y);
if (den == 0)
ret->set(0, x, y);
else
ret->set((operation.i1.value()->get(x, y) / den) % std::numeric_limits<u8>::max(), x, y);
break;
}
case function_t::EXP:
{
BLT_ASSERT(operation.has_both());
ret->set(static_cast<u8>(std::pow(operation.i1.value()->get(x, y), operation.i2.value()->get(x, y))), x, y);
break;
}
case function_t::LOG:
{
BLT_ASSERT(operation.has_one());
ret->set(static_cast<u8>(std::log(static_cast<double>(operation.getOne()->get(x, y)))), x, y);
break;
}
case function_t::SQRT:
{
BLT_ASSERT(operation.has_one());
ret->set(static_cast<u8>(std::sqrt(static_cast<double>(operation.getOne()->get(x, y)))), x, y);
break;
}
case function_t::QUAD:
{
BLT_ASSERT(operation.has_one());
auto v = operation.getOne()->get(x, y);
ret->set(static_cast<u8>(v * v), x, y);
break;
}
case function_t::RANDOM:
{
break;
}
case function_t::NOISE:
{
break;
}
case function_t::COLOR:
{
break;
}
case function_t::SCALAR:
{
break;
}
}
}
}
}
static constexpr int OPERATOR_COUNT = 13; static constexpr int OPERATOR_COUNT = 13;
@ -45,7 +137,7 @@ class tree;
struct node struct node
{ {
friend tree; friend tree;
private: private:
node* left; node* left;
node* right; node* right;
@ -55,8 +147,7 @@ struct node
static node* construct_random() static node* construct_random()
{ {
node* n = allocator.allocate(1); node* n = createNode();
allocator.construct(n);
std::random_device dev; std::random_device dev;
std::mt19937_64 engine{dev()}; std::mt19937_64 engine{dev()};
@ -72,10 +163,8 @@ struct node
~node() ~node()
{ {
allocator.destroy(left); destroyNode(left);
allocator.deallocate(left, 1); destroyNode(right);
allocator.destroy(right);
allocator.deallocate(right, 1);
} }
}; };