mirror of
https://github.com/OpenTTD/OpenTTD
synced 2026-01-20 02:42:42 +01:00
Codechange: Pass BmpInfo and BmpData as references.
Avoids needing to check for nullptr.
This commit is contained in:
committed by
Peter Nelson
parent
f829b1d74a
commit
d0d5c5c400
@@ -206,19 +206,19 @@ static bool ReadHeightmapPNG(const char *filename, uint *x, uint *y, uint8_t **m
|
||||
/**
|
||||
* The BMP Heightmap loader.
|
||||
*/
|
||||
static void ReadHeightmapBMPImageData(uint8_t *map, BmpInfo *info, BmpData *data)
|
||||
static void ReadHeightmapBMPImageData(uint8_t *map, const BmpInfo &info, const BmpData &data)
|
||||
{
|
||||
uint x, y;
|
||||
uint8_t gray_palette[256];
|
||||
|
||||
if (!data->palette.empty()) {
|
||||
if (!data.palette.empty()) {
|
||||
uint i;
|
||||
bool all_gray = true;
|
||||
|
||||
if (info->palette_size != 2) {
|
||||
for (i = 0; i < info->palette_size && (info->palette_size != 16 || all_gray); i++) {
|
||||
all_gray &= data->palette[i].r == data->palette[i].g && data->palette[i].r == data->palette[i].b;
|
||||
gray_palette[i] = RGBToGrayscale(data->palette[i].r, data->palette[i].g, data->palette[i].b);
|
||||
if (info.palette_size != 2) {
|
||||
for (i = 0; i < info.palette_size && (info.palette_size != 16 || all_gray); i++) {
|
||||
all_gray &= data.palette[i].r == data.palette[i].g && data.palette[i].r == data.palette[i].b;
|
||||
gray_palette[i] = RGBToGrayscale(data.palette[i].r, data.palette[i].g, data.palette[i].b);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -227,9 +227,9 @@ static void ReadHeightmapBMPImageData(uint8_t *map, BmpInfo *info, BmpData *data
|
||||
* the first entry is the sea (level 0), the second one
|
||||
* level 1, etc.
|
||||
*/
|
||||
if (info->palette_size == 16 && !all_gray) {
|
||||
for (i = 0; i < info->palette_size; i++) {
|
||||
gray_palette[i] = 256 * i / info->palette_size;
|
||||
if (info.palette_size == 16 && !all_gray) {
|
||||
for (i = 0; i < info.palette_size; i++) {
|
||||
gray_palette[i] = 256 * i / info.palette_size;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -243,12 +243,12 @@ static void ReadHeightmapBMPImageData(uint8_t *map, BmpInfo *info, BmpData *data
|
||||
}
|
||||
|
||||
/* Read the raw image data and convert in 8-bit grayscale */
|
||||
for (y = 0; y < info->height; y++) {
|
||||
uint8_t *pixel = &map[y * info->width];
|
||||
uint8_t *bitmap = &data->bitmap[y * info->width * (info->bpp == 24 ? 3 : 1)];
|
||||
for (y = 0; y < info.height; y++) {
|
||||
uint8_t *pixel = &map[y * info.width];
|
||||
const uint8_t *bitmap = &data.bitmap[y * info.width * (info.bpp == 24 ? 3 : 1)];
|
||||
|
||||
for (x = 0; x < info->width; x++) {
|
||||
if (info->bpp != 24) {
|
||||
for (x = 0; x < info.width; x++) {
|
||||
if (info.bpp != 24) {
|
||||
*pixel++ = gray_palette[*bitmap++];
|
||||
} else {
|
||||
*pixel++ = RGBToGrayscale(*bitmap, *(bitmap + 1), *(bitmap + 2));
|
||||
@@ -278,7 +278,7 @@ static bool ReadHeightmapBMP(const char *filename, uint *x, uint *y, uint8_t **m
|
||||
|
||||
BmpInitializeBuffer(&buffer, f);
|
||||
|
||||
if (!BmpReadHeader(&buffer, &info, &data)) {
|
||||
if (!BmpReadHeader(&buffer, info, data)) {
|
||||
ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_BMPMAP_IMAGE_TYPE, WL_ERROR);
|
||||
fclose(f);
|
||||
return false;
|
||||
@@ -291,14 +291,14 @@ static bool ReadHeightmapBMP(const char *filename, uint *x, uint *y, uint8_t **m
|
||||
}
|
||||
|
||||
if (map != nullptr) {
|
||||
if (!BmpReadBitmap(&buffer, &info, &data)) {
|
||||
if (!BmpReadBitmap(&buffer, info, data)) {
|
||||
ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_BMPMAP_IMAGE_TYPE, WL_ERROR);
|
||||
fclose(f);
|
||||
return false;
|
||||
}
|
||||
|
||||
*map = MallocT<uint8_t>(static_cast<size_t>(info.width) * info.height);
|
||||
ReadHeightmapBMPImageData(*map, &info, &data);
|
||||
ReadHeightmapBMPImageData(*map, info, data);
|
||||
}
|
||||
|
||||
*x = info.width;
|
||||
|
||||
Reference in New Issue
Block a user