Singe file drop-in read/write PPM library for C and C++
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
3.6 KiB

3 years ago
#include <iostream>
#include "../src/pnm.cpp"
void make_test_img() {
3 years ago
try {
pnm::PPM image;
3 years ago
const uint32_t width = 512;
const uint32_t height = 512;
for (uint32_t y = 0; y < height; y++) {
for (uint32_t x = 0; x < width; x++) {
image.put_pixel(x, y, pnm::RGB(x / 2, y / 2, x / 2 + y / 2));
}
}
3 years ago
image.save("test_img512x512.ppm");
} catch(const std::exception& e) {
std::string error_message("make_test_img: ", e.what());
throw std::runtime_error(error_message);
}
}
void green_rectangle_on_top_of_existing_image() {
try {
pnm::PPM image;
image.read("test_img512x512.ppm");
// image.read("img.ppm");
uint32_t rec_width = 100;
uint32_t rec_height = 100;
for (uint32_t y = 0; y < rec_height; y++) {
for (uint32_t x = 0; x < rec_width; x++) {
image.put_pixel(x, y, pnm::RGB(0, 255, 0));
3 years ago
}
}
image.save("result_image.ppm");
} catch(const std::exception& e) {
std::string error_message("green_rectangle_on_top_of_existing_image: ", e.what());
throw std::runtime_error(error_message);
}
}
void no_pixel_assign() {
try {
pnm::PPM image;
image.save("result_image.ppm");
std::string error_message("no_pixel_assign: should`ve gotten an error, but there weren`t any thrown");
throw std::runtime_error(error_message);
} catch(const std::exception& e) {}
}
void write_comment() {
try {
pnm::PPM image;
const uint32_t width = 512;
const uint32_t height = 512;
for (uint32_t y = 0; y < height; y++) {
for (uint32_t x = 0; x < width; x++) {
image.put_pixel(x, y, pnm::RGB(x / 2, y / 2, x / 2 + y / 2));
}
}
image.add_comment("comment 1");
image.add_comment("comment 2");
image.add_comment("comment 3\n\n\n");
image.save("result_image.ppm");
} catch(const std::exception& e) {
std::string error_message("write_comment:", e.what());
throw std::runtime_error(error_message);
}
}
void remove_comments() {
try {
pnm::PPM image;
image.read("result_image.ppm");
image.remove_last_comment();
image.remove_all_comments();
image.save("result_image.ppm");
} catch(const std::exception& e) {
std::string error_message("remove_comment: ", e.what());
throw std::runtime_error(error_message);
}
}
void read_comment(bool print) {
try {
write_comment();
pnm::PPM image;
image.read("result_image.ppm");
std::vector<std::string> comments = image.get_comments();
if (comments.size() == 0) {
throw std::runtime_error("No comments found, though there should be");
}
if (print) {
for (const std::string& comment : comments) {
std::cout << comment << "\n";
}
}
} catch(const std::exception& e) {
std::string error_message("read_comment:", e.what());
throw std::runtime_error(error_message);
}
}
int main() {
try {
make_test_img();
green_rectangle_on_top_of_existing_image();
no_pixel_assign();
write_comment();
read_comment(false);
remove_comments();
3 years ago
} catch(const std::exception& e) {
std::cout << "[ERROR] " << e.what() << "\n";
return 1;
}
std::cout << "[ALL TESTS WERE COMPLETED SUCCESSFULLY]\n";
3 years ago
return 0;
}