mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Replace malloc/free with vector for screenshot buffers.
parent
059462814e
commit
c4a638dfe1
|
@ -179,8 +179,8 @@ static bool MakeBMPImage(const char *name, ScreenshotCallback *callb, void *user
|
|||
/* Try to use 64k of memory, store between 16 and 128 lines */
|
||||
uint maxlines = Clamp(65536 / (w * pixelformat / 8), 16, 128); // number of lines per iteration
|
||||
|
||||
uint8_t *buff = MallocT<uint8_t>(maxlines * w * pixelformat / 8); // buffer which is rendered to
|
||||
uint8_t *line = CallocT<uint8_t>(bytewidth); // one line, stored to file
|
||||
std::vector<uint8_t> buff(maxlines * w * pixelformat / 8); // buffer which is rendered to
|
||||
std::vector<uint8_t> line(bytewidth); // one line, stored to file
|
||||
|
||||
/* Start at the bottom, since bitmaps are stored bottom up */
|
||||
do {
|
||||
|
@ -188,18 +188,18 @@ static bool MakeBMPImage(const char *name, ScreenshotCallback *callb, void *user
|
|||
h -= n;
|
||||
|
||||
/* Render the pixels */
|
||||
callb(userdata, buff, h, w, n);
|
||||
callb(userdata, buff.data(), h, w, n);
|
||||
|
||||
/* Write each line */
|
||||
while (n-- != 0) {
|
||||
if (pixelformat == 8) {
|
||||
/* Move to 'line', leave last few pixels in line zeroed */
|
||||
memcpy(line, buff + n * w, w);
|
||||
memcpy(line.data(), buff.data() + n * w, w);
|
||||
} else {
|
||||
/* Convert from 'native' 32bpp to BMP-like 24bpp.
|
||||
* Works for both big and little endian machines */
|
||||
Colour *src = ((Colour *)buff) + n * w;
|
||||
uint8_t *dst = line;
|
||||
Colour *src = ((Colour *)buff.data()) + n * w;
|
||||
uint8_t *dst = line.data();
|
||||
for (uint i = 0; i < w; i++) {
|
||||
dst[i * 3 ] = src[i].b;
|
||||
dst[i * 3 + 1] = src[i].g;
|
||||
|
@ -207,17 +207,13 @@ static bool MakeBMPImage(const char *name, ScreenshotCallback *callb, void *user
|
|||
}
|
||||
}
|
||||
/* Write to file */
|
||||
if (fwrite(line, bytewidth, 1, f) != 1) {
|
||||
free(line);
|
||||
free(buff);
|
||||
if (fwrite(line.data(), bytewidth, 1, f) != 1) {
|
||||
fclose(f);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} while (h != 0);
|
||||
|
||||
free(line);
|
||||
free(buff);
|
||||
fclose(f);
|
||||
|
||||
return true;
|
||||
|
@ -372,7 +368,7 @@ static bool MakePNGImage(const char *name, ScreenshotCallback *callb, void *user
|
|||
maxlines = Clamp(65536 / w, 16, 128);
|
||||
|
||||
/* now generate the bitmap bits */
|
||||
void *buff = CallocT<uint8_t>(static_cast<size_t>(w) * maxlines * bpp); // by default generate 128 lines at a time.
|
||||
std::vector<uint8_t> buff(static_cast<size_t>(w) * maxlines * bpp); // by default generate 128 lines at a time.
|
||||
|
||||
y = 0;
|
||||
do {
|
||||
|
@ -380,19 +376,18 @@ static bool MakePNGImage(const char *name, ScreenshotCallback *callb, void *user
|
|||
n = std::min(h - y, maxlines);
|
||||
|
||||
/* render the pixels into the buffer */
|
||||
callb(userdata, buff, y, w, n);
|
||||
callb(userdata, buff.data(), y, w, n);
|
||||
y += n;
|
||||
|
||||
/* write them to png */
|
||||
for (i = 0; i != n; i++) {
|
||||
png_write_row(png_ptr, (png_bytep)buff + i * w * bpp);
|
||||
png_write_row(png_ptr, (png_bytep)buff.data() + i * w * bpp);
|
||||
}
|
||||
} while (y != h);
|
||||
|
||||
png_write_end(png_ptr, info_ptr);
|
||||
png_destroy_write_struct(&png_ptr, &info_ptr);
|
||||
|
||||
free(buff);
|
||||
fclose(f);
|
||||
return true;
|
||||
}
|
||||
|
@ -479,7 +474,7 @@ static bool MakePCXImage(const char *name, ScreenshotCallback *callb, void *user
|
|||
maxlines = Clamp(65536 / w, 16, 128);
|
||||
|
||||
/* now generate the bitmap bits */
|
||||
uint8_t *buff = CallocT<uint8_t>(static_cast<size_t>(w) * maxlines); // by default generate 128 lines at a time.
|
||||
std::vector<uint8_t> buff(static_cast<size_t>(w) * maxlines); // by default generate 128 lines at a time.
|
||||
|
||||
y = 0;
|
||||
do {
|
||||
|
@ -488,12 +483,12 @@ static bool MakePCXImage(const char *name, ScreenshotCallback *callb, void *user
|
|||
uint i;
|
||||
|
||||
/* render the pixels into the buffer */
|
||||
callb(userdata, buff, y, w, n);
|
||||
callb(userdata, buff.data(), y, w, n);
|
||||
y += n;
|
||||
|
||||
/* write them to pcx */
|
||||
for (i = 0; i != n; i++) {
|
||||
const uint8_t *bufp = buff + i * w;
|
||||
const uint8_t *bufp = buff.data() + i * w;
|
||||
uint8_t runchar = bufp[0];
|
||||
uint runcount = 1;
|
||||
uint j;
|
||||
|
@ -505,13 +500,11 @@ static bool MakePCXImage(const char *name, ScreenshotCallback *callb, void *user
|
|||
if (ch != runchar || runcount >= 0x3f) {
|
||||
if (runcount > 1 || (runchar & 0xC0) == 0xC0) {
|
||||
if (fputc(0xC0 | runcount, f) == EOF) {
|
||||
free(buff);
|
||||
fclose(f);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (fputc(runchar, f) == EOF) {
|
||||
free(buff);
|
||||
fclose(f);
|
||||
return false;
|
||||
}
|
||||
|
@ -524,21 +517,17 @@ static bool MakePCXImage(const char *name, ScreenshotCallback *callb, void *user
|
|||
/* write remaining bytes.. */
|
||||
if (runcount > 1 || (runchar & 0xC0) == 0xC0) {
|
||||
if (fputc(0xC0 | runcount, f) == EOF) {
|
||||
free(buff);
|
||||
fclose(f);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (fputc(runchar, f) == EOF) {
|
||||
free(buff);
|
||||
fclose(f);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} while (y != h);
|
||||
|
||||
free(buff);
|
||||
|
||||
/* write 8-bit colour palette */
|
||||
if (fputc(12, f) == EOF) {
|
||||
fclose(f);
|
||||
|
|
Loading…
Reference in New Issue