Canvas implementation and add build of LodePNG

This commit is contained in:
Godzil
2020-02-14 16:04:28 +00:00
parent a9321b5051
commit fac2212661
6 changed files with 153 additions and 5 deletions

View File

@@ -17,6 +17,12 @@ ExternalProject_Add(googletest
TEST_COMMAND ""
)
# LodePNG don't make a .a or .so, so let's build a library here
add_library(LodePNG STATIC)
set(LODEPNG_INCLUDE_FOLDER ${CMAKE_CURRENT_SOURCE_DIR}/external/lodepng)
target_sources(LodePNG PRIVATE external/lodepng/lodepng.cpp external/lodepng/lodepng.h)
# Main app
add_subdirectory(source)
# Unit Tests

View File

@@ -3,14 +3,14 @@
# First most is build as a library
add_library(rayonnement STATIC)
set(RAY_HEADERS include/tuples.h include/math_helper.h include/colour.h)
set(RAY_SOURCES tuples.cpp math_helper.cpp colour.cpp)
set(RAY_HEADERS include/tuples.h include/math_helper.h include/colour.h include/canvas.h)
set(RAY_SOURCES tuples.cpp math_helper.cpp colour.cpp canvas.cpp)
target_include_directories(rayonnement PUBLIC include)
target_sources(rayonnement PRIVATE ${RAY_HEADERS} ${RAY_SOURCES})
target_link_libraries(rayonnement LodePNG)
# Second we build the main executable
add_executable(dorayme main.cpp)
target_include_directories(rayonnement PUBLIC include)
target_include_directories(rayonnement PUBLIC include ${LODEPNG_INCLUDE_FOLDER})
target_link_libraries(dorayme rayonnement)

57
source/canvas.cpp Normal file
View File

@@ -0,0 +1,57 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Canvas implementation
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#include <canvas.h>
#include <lodepng.h>
#define BPP (24)
#define BytePP (BPP / 8)
#define MIN(_a, _b) ((_a)<(_b)?(_a):(_b))
#define MAX(_a, _b) ((_a)>(_b)?(_a):(_b))
Canvas::Canvas(uint32_t width, uint32_t height) : width(width), height(height)
{
this->bitmap = (uint8_t *)calloc(4, width * height);
this->stride = BytePP * width;
}
Canvas::~Canvas()
{
if (this->bitmap != nullptr)
{
free(this->bitmap);
}
}
void Canvas::put_pixel(uint32_t x, uint32_t y, Colour c)
{
uint32_t offset = y * this->stride + x * BytePP;
this->bitmap[offset + 0] = MAX(MIN(c.red() * 255, 255), 0);
this->bitmap[offset + 1] = MAX(MIN(c.green() * 255, 255), 0);
this->bitmap[offset + 2] = MAX(MIN(c.blue() * 255, 255), 0);
}
Colour Canvas::get_pixel(uint32_t x, uint32_t y)
{
uint32_t offset = y * this->stride + x * BytePP;
return Colour(this->bitmap[offset + 0] / 255, this->bitmap[offset + 1] / 255, this->bitmap[offset + 2] / 255);
}
bool Canvas::SaveAsPNG(const char *filename)
{
uint32_t ret = lodepng_encode24_file(filename, this->bitmap, this->width, this->height);
if (ret > 0)
{
printf("lodepng_encode_file returned %d!\n", ret);
}
return ret == 0;
}

32
source/include/canvas.h Normal file
View File

@@ -0,0 +1,32 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Canvas header
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#ifndef DORAYME_CANVAS_H
#define DORAYME_CANVAS_H
#include <stdint.h>
#include <colour.h>
class Canvas
{
private:
uint8_t *bitmap;
uint32_t stride;
public:
uint32_t width, height;
Canvas(uint32_t width, uint32_t height);
~Canvas();
void put_pixel(uint32_t x, uint32_t y, Colour c);
Colour get_pixel(uint32_t x, uint32_t y);
bool SaveAsPNG(const char *filename);
};
#endif /* DORAYME_CANVAS_H */

View File

@@ -3,7 +3,7 @@ project(DoRayTested)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
set(TESTS_SRC tuples_test.cpp colour_test.cpp)
set(TESTS_SRC tuples_test.cpp colour_test.cpp canvas_test.cpp)
add_executable(testMyRays)
target_include_directories(testMyRays PUBLIC ${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})

53
tests/canvas_test.cpp Normal file
View File

@@ -0,0 +1,53 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Canvas unit tests
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#include <colour.h>
#include <canvas.h>
#include <math.h>
#include <gtest/gtest.h>
TEST(CanvasTest, Creating_a_canvas)
{
Canvas c = Canvas(10, 20);
int x, y;
ASSERT_EQ(c.width, 10);
ASSERT_EQ(c.height, 20);
for(y = 0; y < 20; y++)
{
for(x = 0; x < 10; x++)
{
ASSERT_EQ(c.get_pixel(x, y), Colour(0, 0, 0));
}
}
}
TEST(CanvasTest, Test_Writing_pixels_to_a_canvas_Test)
{
Canvas c = Canvas(10, 20);
Colour red = Colour(1, 0, 0);
c.put_pixel(2, 3, red);
ASSERT_EQ(c.get_pixel(2, 3), red);
}
TEST(CanvasTest, Save_a_PNG_file)
{
Canvas c = Canvas(5, 3);
Colour c1 = Colour(1.5, 0, 0);
Colour c2 = Colour(0, 0.5, 0);
Colour c3 = Colour(-0.5, 0, 1);
c.put_pixel(0, 0, c1);
c.put_pixel(2, 1, c2);
c.put_pixel(4, 2, c3);
ASSERT_TRUE(c.SaveAsPNG("Save_a_PNG_file.png"));
}