1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-28 22:54:29 +01:00

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 "bmp.h"
#include "core/bitmath_func.hpp"
#include "core/alloc_func.hpp"
#include "core/mem_func.hpp"
#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->bpp <= 8) {
uint i;
/* Reads number of colours if available in info header */
if (header_size >= 16) {
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. */
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++) {
data->palette[i].b = ReadByte(buffer);
data->palette[i].g = ReadByte(buffer);
data->palette[i].r = ReadByte(buffer);
for (auto &colour : data->palette) {
colour.b = ReadByte(buffer);
colour.g = ReadByte(buffer);
colour.r = ReadByte(buffer);
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);
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 */
SetStreamOffset(buffer, info->offset);
@@ -413,10 +410,3 @@ bool BmpReadBitmap(BmpBuffer *buffer, BmpInfo *info, BmpData *data)
default: NOT_REACHED();
}
}
void BmpDestroyData(BmpData *data)
{
assert(data != nullptr);
free(data->palette);
free(data->bitmap);
}