/*! * 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; /* ------------------------------------------------------------------------- */ TEST_CASE("[Canvas] Creating a canvas", "[Canvas]") { Canvas c(10, 20); REQUIRE(c.width() == 10); REQUIRE(c.height() == 20); for (int i = 0; i < 10; ++i) { for (int j = 0; j < 20; ++j) { REQUIRE(c.pixel_at(2, 3) == Color(0, 0, 0)); } } } /* ------------------------------------------------------------------------- */ TEST_CASE("[Canvas] Writing pixels to a canvas", "[Canvas]") { Canvas c(10, 20); Color red(1, 0, 0); c.write_pixel(2, 3, red); REQUIRE(c.pixel_at(2, 3) == red); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Canvas] Constructing the PPM pixel data", "[Canvas]") { std::string ppm, the_ref_ppm; Canvas c(5, 3); Color c1(1.5, 0, 0); Color c2(0, 0.5, 0); Color c3(-0.5, 0, 1); c.write_pixel(0, 0, c1); c.write_pixel(2, 1, c2); c.write_pixel(4, 2, c3); 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"; REQUIRE(ppm == the_ref_ppm); } /* ------------------------------------------------------------------------- */ TEST_CASE("[Canvas] Split long lines in PPM files", "[Canvas]") { std::string ppm, the_ref_ppm; Canvas c(10, 2); 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)); } } 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"; REQUIRE(ppm == the_ref_ppm); }