[FEAT] Add canvas ppm part.

This commit is contained in:
NADAL Jean-Baptiste
2024-01-31 18:19:26 +01:00
parent 770b784383
commit ec96114111
6 changed files with 221 additions and 4 deletions

View File

@@ -56,3 +56,53 @@ TEST_CASE("[Canvas] Writing pixels to a canvas", "[Canvas]")
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);
}