Codechange: Replace BmpData palette and bitmap with vectors.

BmpInfo width and height members are now size_t to avoid multiplication warnings.

This avoids manual memory management and allows BmpData to clean up after itself.
This commit is contained in:
Peter Nelson 2024-05-15 13:21:30 +01:00 committed by Peter Nelson
parent 0633b94e8f
commit f829b1d74a
3 changed files with 10 additions and 29 deletions

View File

@ -10,7 +10,6 @@
#include "stdafx.h" #include "stdafx.h"
#include "bmp.h" #include "bmp.h"
#include "core/bitmath_func.hpp" #include "core/bitmath_func.hpp"
#include "core/alloc_func.hpp"
#include "core/mem_func.hpp" #include "core/mem_func.hpp"
#include "safeguards.h" #include "safeguards.h"
@ -359,8 +358,6 @@ bool BmpReadHeader(BmpBuffer *buffer, BmpInfo *info, BmpData *data)
if (info->compression > 2 || (info->compression > 0 && !(info->bpp == 4 || info->bpp == 8))) return false; if (info->compression > 2 || (info->compression > 0 && !(info->bpp == 4 || info->bpp == 8))) return false;
if (info->bpp <= 8) { if (info->bpp <= 8) {
uint i;
/* Reads number of colours if available in info header */ /* Reads number of colours if available in info header */
if (header_size >= 16) { if (header_size >= 16) {
SkipBytes(buffer, 12); // skip image size and resolution SkipBytes(buffer, 12); // skip image size and resolution
@ -374,12 +371,12 @@ bool BmpReadHeader(BmpBuffer *buffer, BmpInfo *info, BmpData *data)
/* More palette colours than palette indices is not supported. */ /* More palette colours than palette indices is not supported. */
if (info->palette_size > maximum_palette_size) return false; if (info->palette_size > maximum_palette_size) return false;
data->palette = CallocT<Colour>(info->palette_size); data->palette.resize(info->palette_size);
for (i = 0; i < info->palette_size; i++) { for (auto &colour : data->palette) {
data->palette[i].b = ReadByte(buffer); colour.b = ReadByte(buffer);
data->palette[i].g = ReadByte(buffer); colour.g = ReadByte(buffer);
data->palette[i].r = ReadByte(buffer); colour.r = ReadByte(buffer);
if (!info->os2_bmp) SkipBytes(buffer, 1); // unused if (!info->os2_bmp) SkipBytes(buffer, 1); // unused
} }
} }
@ -395,7 +392,7 @@ bool BmpReadBitmap(BmpBuffer *buffer, BmpInfo *info, BmpData *data)
{ {
assert(info != nullptr && data != nullptr); assert(info != nullptr && data != nullptr);
data->bitmap = CallocT<uint8_t>(static_cast<size_t>(info->width) * info->height * ((info->bpp == 24) ? 3 : 1)); data->bitmap.resize(static_cast<size_t>(info->width) * info->height * ((info->bpp == 24) ? 3 : 1));
/* Load image */ /* Load image */
SetStreamOffset(buffer, info->offset); SetStreamOffset(buffer, info->offset);
@ -413,10 +410,3 @@ bool BmpReadBitmap(BmpBuffer *buffer, BmpInfo *info, BmpData *data)
default: NOT_REACHED(); default: NOT_REACHED();
} }
} }
void BmpDestroyData(BmpData *data)
{
assert(data != nullptr);
free(data->palette);
free(data->bitmap);
}

View File

@ -23,8 +23,8 @@ struct BmpInfo {
}; };
struct BmpData { struct BmpData {
Colour *palette; std::vector<Colour> palette;
uint8_t *bitmap; std::vector<uint8_t> bitmap;
}; };
#define BMP_BUFFER_SIZE 1024 #define BMP_BUFFER_SIZE 1024
@ -40,6 +40,5 @@ struct BmpBuffer {
void BmpInitializeBuffer(BmpBuffer *buffer, FILE *file); void BmpInitializeBuffer(BmpBuffer *buffer, FILE *file);
bool BmpReadHeader(BmpBuffer *buffer, BmpInfo *info, BmpData *data); bool BmpReadHeader(BmpBuffer *buffer, BmpInfo *info, BmpData *data);
bool BmpReadBitmap(BmpBuffer *buffer, BmpInfo *info, BmpData *data); bool BmpReadBitmap(BmpBuffer *buffer, BmpInfo *info, BmpData *data);
void BmpDestroyData(BmpData *data);
#endif /* BMP_H */ #endif /* BMP_H */

View File

@ -211,7 +211,7 @@ static void ReadHeightmapBMPImageData(uint8_t *map, BmpInfo *info, BmpData *data
uint x, y; uint x, y;
uint8_t gray_palette[256]; uint8_t gray_palette[256];
if (data->palette != nullptr) { if (!data->palette.empty()) {
uint i; uint i;
bool all_gray = true; bool all_gray = true;
@ -267,12 +267,9 @@ static bool ReadHeightmapBMP(const char *filename, uint *x, uint *y, uint8_t **m
{ {
FILE *f; FILE *f;
BmpInfo info; BmpInfo info;
BmpData data; BmpData data{};
BmpBuffer buffer; BmpBuffer buffer;
/* Init BmpData */
memset(&data, 0, sizeof(data));
f = FioFOpenFile(filename, "rb", HEIGHTMAP_DIR); f = FioFOpenFile(filename, "rb", HEIGHTMAP_DIR);
if (f == nullptr) { if (f == nullptr) {
ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_PNGMAP_FILE_NOT_FOUND, WL_ERROR); ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_PNGMAP_FILE_NOT_FOUND, WL_ERROR);
@ -284,14 +281,12 @@ static bool ReadHeightmapBMP(const char *filename, uint *x, uint *y, uint8_t **m
if (!BmpReadHeader(&buffer, &info, &data)) { if (!BmpReadHeader(&buffer, &info, &data)) {
ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_BMPMAP_IMAGE_TYPE, WL_ERROR); ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_BMPMAP_IMAGE_TYPE, WL_ERROR);
fclose(f); fclose(f);
BmpDestroyData(&data);
return false; return false;
} }
if (!IsValidHeightmapDimension(info.width, info.height)) { if (!IsValidHeightmapDimension(info.width, info.height)) {
ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_HEIGHTMAP_TOO_LARGE, WL_ERROR); ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_HEIGHTMAP_TOO_LARGE, WL_ERROR);
fclose(f); fclose(f);
BmpDestroyData(&data);
return false; return false;
} }
@ -299,7 +294,6 @@ static bool ReadHeightmapBMP(const char *filename, uint *x, uint *y, uint8_t **m
if (!BmpReadBitmap(&buffer, &info, &data)) { if (!BmpReadBitmap(&buffer, &info, &data)) {
ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_BMPMAP_IMAGE_TYPE, WL_ERROR); ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_BMPMAP_IMAGE_TYPE, WL_ERROR);
fclose(f); fclose(f);
BmpDestroyData(&data);
return false; return false;
} }
@ -307,8 +301,6 @@ static bool ReadHeightmapBMP(const char *filename, uint *x, uint *y, uint8_t **m
ReadHeightmapBMPImageData(*map, &info, &data); ReadHeightmapBMPImageData(*map, &info, &data);
} }
BmpDestroyData(&data);
*x = info.width; *x = info.width;
*y = info.height; *y = info.height;