diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2015-02-04 09:00:34 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2015-02-04 09:00:34 +0100 |
commit | e4b1919ba8da9929051777d6a5945fc5625e0d8d (patch) | |
tree | de47c8a29f58aaf35d55f13589ce3dd64647207b | |
parent | 61f47521a94168739ca031aab6eb5fe81531a024 (diff) | |
download | MinedMap-e4b1919ba8da9929051777d6a5945fc5625e0d8d.tar MinedMap-e4b1919ba8da9929051777d6a5945fc5625e0d8d.zip |
Use heap memory for image buffers
-rw-r--r-- | src/MinedMap.cpp | 14 | ||||
-rw-r--r-- | 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<uint32_t[]> image(new uint32_t[DIM*DIM]); + std::memset(image.get(), 0, 4*DIM*DIM); - writeImage(output, reinterpret_cast<const uint8_t*>(image), true, &instat.st_mtim); - writeImage(output_light, lightmap, false, &instat.st_mtim); + std::unique_ptr<uint8_t[]> 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<const uint8_t*>(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 <cerrno> #include <cstdio> #include <cstring> +#include <memory> #include <system_error> #include <png.h> @@ -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<uint8_t[]> 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<uint8_t[]> 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); } } |