From e4b1919ba8da9929051777d6a5945fc5625e0d8d Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 4 Feb 2015 09:00:34 +0100 Subject: Use heap memory for image buffers --- src/MinedMap.cpp | 14 +++++++++----- src/PNG.cpp | 20 +++++++++++--------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/MinedMap.cpp b/src/MinedMap.cpp index 8f548b6..8cf9393 100644 --- a/src/MinedMap.cpp +++ b/src/MinedMap.cpp @@ -127,12 +127,16 @@ static void doRegion(const std::string &input, const std::string &output, const std::printf("Generating %s from %s...\n", output.c_str(), input.c_str()); try { - uint32_t image[DIM*DIM] = {}; - uint8_t lightmap[2*DIM*DIM] = {}; - World::Region::visitChunks(input.c_str(), [&] (size_t X, size_t Z, const World::Chunk *chunk) { addChunk(image, lightmap, X, Z, chunk); }); + std::unique_ptr image(new uint32_t[DIM*DIM]); + std::memset(image.get(), 0, 4*DIM*DIM); - writeImage(output, reinterpret_cast(image), true, &instat.st_mtim); - writeImage(output_light, lightmap, false, &instat.st_mtim); + std::unique_ptr lightmap(new uint8_t[2*DIM*DIM]); + std::memset(lightmap.get(), 0, 2*DIM*DIM); + + World::Region::visitChunks(input.c_str(), [&] (size_t X, size_t Z, const World::Chunk *chunk) { addChunk(image.get(), lightmap.get(), X, Z, chunk); }); + + writeImage(output, reinterpret_cast(image.get()), true, &instat.st_mtim); + writeImage(output_light, lightmap.get(), false, &instat.st_mtim); } catch (const std::exception& ex) { std::fprintf(stderr, "Failed to generate %s: %s\n", output.c_str(), ex.what()); diff --git a/src/PNG.cpp b/src/PNG.cpp index f56c870..ad52564 100644 --- a/src/PNG.cpp +++ b/src/PNG.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -125,8 +126,8 @@ static void readScaled(uint8_t *data, size_t offset_w, size_t offset_h, const ch size_t b = (colored ? 4 : 2); - uint8_t input[4*width*height]; - read(file, input, width, height, colored); + std::unique_ptr input(new uint8_t[b*width*height]); + read(file, input.get(), width, height, colored); for (size_t h = 0; h < width/2; h++) { for (size_t w = 0; w < width/2; w++) { @@ -139,15 +140,16 @@ static void readScaled(uint8_t *data, size_t offset_w, size_t offset_h, const ch } void mipmap(const char *output, size_t width, size_t height, bool colored, const char *nw, const char *ne, const char *sw, const char *se) { - uint8_t data[(colored ? 4 : 2)*width*height]; - std::memset(data, 0, sizeof(data)); + size_t size = (colored ? 4 : 2)*width*height; + std::unique_ptr data(new uint8_t[size]); + std::memset(data.get(), 0, size); - readScaled(data, 0, 0, nw, width, height, colored); - readScaled(data, width/2, 0, ne, width, height, colored); - readScaled(data, 0, height/2, sw, width, height, colored); - readScaled(data, width/2, height/2, se, width, height, colored); + readScaled(data.get(), 0, 0, nw, width, height, colored); + readScaled(data.get(), width/2, 0, ne, width, height, colored); + readScaled(data.get(), 0, height/2, sw, width, height, colored); + readScaled(data.get(), width/2, height/2, se, width, height, colored); - write(output, data, width, height, colored); + write(output, data.get(), width, height, colored); } } -- cgit v1.2.3