aboutsummaryrefslogtreecommitdiff
path: root/src/openvic/utility
diff options
context:
space:
mode:
author Hop311 <hop3114@gmail.com>2023-09-08 01:34:47 +0200
committer Hop311 <hop3114@gmail.com>2023-09-08 01:34:47 +0200
commit7f9a9a8241ba81be9213e6606b8be4a48f1cbaab (patch)
tree26b67f150ec1b43593343344eabdc7deca47d0d8 /src/openvic/utility
parent3cd1d62ec00690a1b29070dd4903754e8f089a21 (diff)
Remove return_t, use & instead of if(x != SUCCESS)
Diffstat (limited to 'src/openvic/utility')
-rw-r--r--src/openvic/utility/BMP.cpp56
-rw-r--r--src/openvic/utility/BMP.hpp7
2 files changed, 31 insertions, 32 deletions
diff --git a/src/openvic/utility/BMP.cpp b/src/openvic/utility/BMP.cpp
index 6859411..0ea0f30 100644
--- a/src/openvic/utility/BMP.cpp
+++ b/src/openvic/utility/BMP.cpp
@@ -11,57 +11,58 @@ BMP::~BMP() {
close();
}
-return_t BMP::open(char const* filepath) {
+bool BMP::open(char const* filepath) {
reset();
errno = 0;
file = fopen(filepath, "rb");
if (file == nullptr || errno != 0) {
Logger::error("Failed to open BMP file \"", filepath, "\" (errno = ", errno, ")");
file = nullptr;
- return FAILURE;
+ return false;
}
- return SUCCESS;
+ return true;
}
-return_t BMP::read_header() {
+bool BMP::read_header() {
if (header_validated) {
Logger::error("BMP header already validated!");
- return FAILURE;
+ return false;
}
if (file == nullptr) {
Logger::error("Cannot read BMP header before opening a file");
- return FAILURE;
+ return false;
}
if (fseek(file, 0, SEEK_SET) != 0) {
Logger::error("Failed to move to the beginning of the BMP file!");
- return FAILURE;
+ return false;
}
if (fread(&header, sizeof(header), 1, file) != 1) {
Logger::error("Failed to read BMP header!");
- return FAILURE;
+ return false;
}
- return_t ret = SUCCESS;
+
+ header_validated = true;
// Validate constants
static constexpr uint16_t BMP_SIGNATURE = 0x4d42;
if (header.signature != BMP_SIGNATURE) {
Logger::error("Invalid BMP signature: ", header.signature, " (must be ", BMP_SIGNATURE, ")");
- ret = FAILURE;
+ header_validated = false;
}
static constexpr uint32_t DIB_HEADER_SIZE = 40;
if (header.dib_header_size != DIB_HEADER_SIZE) {
Logger::error("Invalid BMP DIB header size: ", header.dib_header_size, " (must be ", DIB_HEADER_SIZE, ")");
- ret = FAILURE;
+ header_validated = false;
}
static constexpr uint16_t NUM_PLANES = 1;
if (header.num_planes != NUM_PLANES) {
Logger::error("Invalid BMP plane count: ", header.num_planes, " (must be ", NUM_PLANES, ")");
- ret = FAILURE;
+ header_validated = false;
}
static constexpr uint16_t COMPRESSION = 0; // Only support uncompressed BMPs
if (header.compression != COMPRESSION) {
Logger::error("Invalid BMP compression method: ", header.compression, " (must be ", COMPRESSION, ")");
- ret = FAILURE;
+ header_validated = false;
}
// Validate sizes and dimensions
@@ -69,17 +70,17 @@ return_t BMP::read_header() {
if (header.file_size != header.offset + header.image_size_bytes) {
Logger::error("Invalid BMP memory sizes: file size = ", header.file_size, " != ", header.offset + header.image_size_bytes,
" = ", header.offset, " + ", header.image_size_bytes, " = image data offset + image data size");
- ret = FAILURE;
+ header_validated = false;
}
// TODO - support negative widths (i.e. horizontal flip)
if (header.width_px <= 0) {
Logger::error("Invalid BMP width: ", header.width_px, " (must be positive)");
- ret = FAILURE;
+ header_validated = false;
}
// TODO - support negative heights (i.e. vertical flip)
if (header.height_px <= 0) {
Logger::error("Invalid BMP height: ", header.height_px, " (must be positive)");
- ret = FAILURE;
+ header_validated = false;
}
// TODO - validate x_resolution_ppm
// TODO - validate y_resolution_ppm
@@ -90,14 +91,14 @@ return_t BMP::read_header() {
static const std::set<uint16_t> BITS_PER_PIXEL { VALID_BITS_PER_PIXEL };
if (!BITS_PER_PIXEL.contains(header.bits_per_pixel)) {
Logger::error("Invalid BMP bits per pixel: ", header.bits_per_pixel, " (must be one of " STR(VALID_BITS_PER_PIXEL) ")");
- ret = FAILURE;
+ header_validated = false;
}
#undef VALID_BITS_PER_PIXEL
#undef STR
static constexpr uint16_t PALETTE_BITS_PER_PIXEL_LIMIT = 8;
if (header.num_colours != 0 && header.bits_per_pixel > PALETTE_BITS_PER_PIXEL_LIMIT) {
Logger::error("Invalid BMP palette size: ", header.num_colours, " (should be 0 as bits per pixel is ", header.bits_per_pixel, " > 8)");
- ret = FAILURE;
+ header_validated = false;
}
// TODO - validate important_colours
@@ -109,37 +110,36 @@ return_t BMP::read_header() {
const uint32_t expected_offset = palette_size * PALETTE_COLOUR_SIZE + sizeof(header);
if (header.offset != expected_offset) {
Logger::error("Invalid BMP image data offset: ", header.offset, " (should be ", expected_offset, ")");
- ret = FAILURE;
+ header_validated = false;
}
- header_validated = ret == SUCCESS;
- return ret;
+ return header_validated;
}
-return_t BMP::read_palette() {
+bool BMP::read_palette() {
if (file == nullptr) {
Logger::error("Cannot read BMP palette before opening a file");
- return FAILURE;
+ return false;
}
if (!header_validated) {
Logger::error("Cannot read palette before BMP header is validated!");
- return FAILURE;
+ return false;
}
if (palette_size == 0) {
Logger::error("Cannot read BMP palette - header indicates this file doesn't have one");
- return FAILURE;
+ return false;
}
if (fseek(file, sizeof(header), SEEK_SET) != 0) {
Logger::error("Failed to move to the palette in the BMP file!");
- return FAILURE;
+ return false;
}
palette.resize(palette_size);
if (fread(palette.data(), palette_size * PALETTE_COLOUR_SIZE, 1, file) != 1) {
Logger::error("Failed to read BMP header!");
palette.clear();
- return FAILURE;
+ return false;
}
- return SUCCESS;
+ return true;
}
void BMP::close() {
diff --git a/src/openvic/utility/BMP.hpp b/src/openvic/utility/BMP.hpp
index 50b333b..7ed36a5 100644
--- a/src/openvic/utility/BMP.hpp
+++ b/src/openvic/utility/BMP.hpp
@@ -4,7 +4,6 @@
#include <vector>
#include "openvic/types/Colour.hpp"
-#include "openvic/types/Return.hpp"
namespace OpenVic {
class BMP {
@@ -41,9 +40,9 @@ namespace OpenVic {
BMP() = default;
~BMP();
- return_t open(char const* filepath);
- return_t read_header();
- return_t read_palette();
+ bool open(char const* filepath);
+ bool read_header();
+ bool read_palette();
void close();
void reset();