/*! * 02_2_canvas.cpp * * Copyright (c) 2015-2024, NADAL Jean-Baptiste. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301 USA * * @Author: NADAL Jean-Baptiste * @Date: 31/01/2024 * */ /*---------------------------------------------------------------------------*/ #include #include "canvas.h" using namespace Raytracer; /* ------------------------------------------------------------------------- */ SCENARIO("Creating a canvas", "[features/canvas.feature]") { GIVEN("c <-canvas(10, 20)") { Canvas c(10, 20); THEN("c.width = 10") { REQUIRE(c.width() == 10); } AND_THEN("c.height = 20") { REQUIRE(c.height() == 20); } for (int i = 0; i < 10; ++i) { for (int j = 0; j < 20; ++j) { AND_THEN("every pixel of c is color(0,0,0)") { REQUIRE(c.pixel_at(2, 3) == Color(0, 0, 0)); } } } } } /* ------------------------------------------------------------------------- */ SCENARIO("Writing pixels to a canvas", "[features/canvas.feature]") { GIVEN("c <-canvas(10, 20)") { Canvas c(10, 20); AND_GIVEN("red <-color(1, 0, 0)") { Color red(1, 0, 0); WHEN("write_pixel(c,2,3,red)") { c.write_pixel(2, 3, red); THEN("pixel_at(c,2,3) = red") { REQUIRE(c.pixel_at(2, 3) == red); } } } } } /* ------------------------------------------------------------------------- */ SCENARIO("Constructing the PPM pixel data", "[features/canvas.feature]") { std::string ppm, the_ref_ppm; GIVEN("c <-canvas(5, 3)") { Canvas c(5, 3); AND_GIVEN(" c1 color(1.5, 0, 0)") { Color c1(1.5, 0, 0); AND_GIVEN(" c2 color(0, 0.5, 0)") { Color c2(0, 0.5, 0); AND_GIVEN(" c3 color(-0.5, 0, 1))") { Color c3(-0.5, 0, 1); WHEN("write_pixel(c, 0, 0, c1)") { c.write_pixel(0, 0, c1); AND_WHEN("write_pixel(c, 2, 1, c2)") { c.write_pixel(2, 1, c2); AND_WHEN("write_pixel(c, 4, 2, c3)") { c.write_pixel(4, 2, c3); AND_WHEN("ppm <- canvas_to_ppm(c)") { ppm = c.to_ppm(); the_ref_ppm = "P3\n5 3\n255\n"; the_ref_ppm += "255 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n"; the_ref_ppm += "0 0 0 0 0 0 0 128 0 0 0 0 0 0 0\n"; the_ref_ppm += "0 0 0 0 0 0 0 0 0 0 0 0 0 0 255\n"; THEN("line 4-6 of ppm are 255 0 0 ....") { REQUIRE(ppm == the_ref_ppm); } } } } } } } } } } /* ------------------------------------------------------------------------- */ SCENARIO("Split long lines in PPM files", "[features/canvas.feature]") { std::string ppm, the_ref_ppm; GIVEN("c <-canvas(10, 2)") { Canvas c(10, 2); WHEN("every pixel of c is set to color(1,0.8,0.6)") { for (int j = 0; j < 2; ++j) { for (int i = 0; i < 10; ++i) { c.write_pixel(i, j, Color(1, 0.8, 0.6)); } } AND_WHEN("ppm <- canvas_to_ppm(c)") { ppm = c.to_ppm(); the_ref_ppm = "P3\n10 2\n255\n"; the_ref_ppm += "255 204 153 255 204 153 255 204 153 255 204 153 255 204 153 255 204\n"; the_ref_ppm += "153 255 204 153 255 204 153 255 204 153 255 204 153\n"; the_ref_ppm += "255 204 153 255 204 153 255 204 153 255 204 153 255 204 153 255 204\n"; the_ref_ppm += "153 255 204 153 255 204 153 255 204 153 255 204 153\n"; THEN("line 4-7 of ppm are 255 204 153 255 204 ...") { REQUIRE(ppm == the_ref_ppm); } } } } }