Use heap memory for image buffers

This commit is contained in:
Matthias Schiffer 2015-02-04 09:00:34 +01:00
parent 61f47521a9
commit e4b1919ba8
2 changed files with 20 additions and 14 deletions

View file

@ -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()); std::printf("Generating %s from %s...\n", output.c_str(), input.c_str());
try { try {
uint32_t image[DIM*DIM] = {}; std::unique_ptr<uint32_t[]> image(new uint32_t[DIM*DIM]);
uint8_t lightmap[2*DIM*DIM] = {}; std::memset(image.get(), 0, 4*DIM*DIM);
World::Region::visitChunks(input.c_str(), [&] (size_t X, size_t Z, const World::Chunk *chunk) { addChunk(image, lightmap, X, Z, chunk); });
writeImage(output, reinterpret_cast<const uint8_t*>(image), true, &instat.st_mtim); std::unique_ptr<uint8_t[]> lightmap(new uint8_t[2*DIM*DIM]);
writeImage(output_light, lightmap, false, &instat.st_mtim); 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) { catch (const std::exception& ex) {
std::fprintf(stderr, "Failed to generate %s: %s\n", output.c_str(), ex.what()); std::fprintf(stderr, "Failed to generate %s: %s\n", output.c_str(), ex.what());

View file

@ -29,6 +29,7 @@
#include <cerrno> #include <cerrno>
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <memory>
#include <system_error> #include <system_error>
#include <png.h> #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); size_t b = (colored ? 4 : 2);
uint8_t input[4*width*height]; std::unique_ptr<uint8_t[]> input(new uint8_t[b*width*height]);
read(file, input, width, height, colored); read(file, input.get(), width, height, colored);
for (size_t h = 0; h < width/2; h++) { for (size_t h = 0; h < width/2; h++) {
for (size_t w = 0; w < width/2; w++) { 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) { 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]; size_t size = (colored ? 4 : 2)*width*height;
std::memset(data, 0, sizeof(data)); 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.get(), 0, 0, nw, width, height, colored);
readScaled(data, width/2, 0, ne, width, height, colored); readScaled(data.get(), width/2, 0, ne, width, height, colored);
readScaled(data, 0, height/2, sw, width, height, colored); readScaled(data.get(), 0, height/2, sw, width, height, colored);
readScaled(data, width/2, height/2, se, 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);
} }
} }