Replace `libpng` code for `stb_image` one.
It is a very straightforward replacement calls.
This commit is contained in:
parent
8f48c1f01d
commit
513af13ddc
79
src/Util.cpp
79
src/Util.cpp
|
@ -13,11 +13,17 @@
|
|||
|
||||
#include <zlib.h>
|
||||
|
||||
#ifndef NO_PNG
|
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
extern "C" {
|
||||
#include <png.h>
|
||||
#include "../headers/stb/stb_image.h"
|
||||
}
|
||||
|
||||
#define STBI_MSC_SECURE_CRT
|
||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||
extern "C" {
|
||||
#include "../headers/stb/stb_image_write.h"
|
||||
}
|
||||
#endif
|
||||
|
||||
#include "NLS.h"
|
||||
#include "System.h"
|
||||
|
@ -179,58 +185,19 @@ void utilReadScreenPixels(uint8_t *dest, int w, int h)
|
|||
}
|
||||
}
|
||||
|
||||
#define CHANNEL_NUM 3 // RGB
|
||||
|
||||
bool utilWritePNGFile(const char *fileName, int w, int h, uint8_t *pix)
|
||||
{
|
||||
#ifndef NO_PNG
|
||||
uint8_t writeBuffer[512 * 3];
|
||||
|
||||
FILE *fp = fopen(fileName, "wb");
|
||||
|
||||
if (!fp) {
|
||||
systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), fileName);
|
||||
return false;
|
||||
}
|
||||
|
||||
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
||||
if (!png_ptr) {
|
||||
fclose(fp);
|
||||
return false;
|
||||
}
|
||||
|
||||
png_infop info_ptr = png_create_info_struct(png_ptr);
|
||||
|
||||
if (!info_ptr) {
|
||||
png_destroy_write_struct(&png_ptr, NULL);
|
||||
fclose(fp);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (setjmp(png_jmpbuf(png_ptr))) {
|
||||
png_destroy_write_struct(&png_ptr, NULL);
|
||||
fclose(fp);
|
||||
return false;
|
||||
}
|
||||
|
||||
png_init_io(png_ptr, fp);
|
||||
|
||||
png_set_IHDR(png_ptr,
|
||||
info_ptr,
|
||||
w,
|
||||
h,
|
||||
8,
|
||||
PNG_COLOR_TYPE_RGB,
|
||||
PNG_INTERLACE_NONE,
|
||||
PNG_COMPRESSION_TYPE_DEFAULT,
|
||||
PNG_FILTER_TYPE_DEFAULT);
|
||||
|
||||
png_write_info(png_ptr, info_ptr);
|
||||
uint8_t writeBuffer[512 * CHANNEL_NUM];
|
||||
|
||||
uint8_t *b = writeBuffer;
|
||||
|
||||
int sizeX = w;
|
||||
int sizeY = h;
|
||||
|
||||
switch (systemColorDepth) {
|
||||
switch (systemColorDepth)
|
||||
{
|
||||
case 16: {
|
||||
uint16_t *p = (uint16_t *)(pix + (w + 2) * 2); // skip first black line
|
||||
for (int y = 0; y < sizeY; y++) {
|
||||
|
@ -243,8 +210,6 @@ bool utilWritePNGFile(const char *fileName, int w, int h, uint8_t *pix)
|
|||
}
|
||||
p++; // skip black pixel for filters
|
||||
p++; // skip black pixel for filters
|
||||
png_write_row(png_ptr, writeBuffer);
|
||||
|
||||
b = writeBuffer;
|
||||
}
|
||||
} break;
|
||||
|
@ -266,8 +231,6 @@ bool utilWritePNGFile(const char *fileName, int w, int h, uint8_t *pix)
|
|||
*b++ = blue;
|
||||
}
|
||||
}
|
||||
png_write_row(png_ptr, writeBuffer);
|
||||
|
||||
b = writeBuffer;
|
||||
}
|
||||
} break;
|
||||
|
@ -282,24 +245,12 @@ bool utilWritePNGFile(const char *fileName, int w, int h, uint8_t *pix)
|
|||
*b++ = ((v >> systemBlueShift) & 0x001f) << 3; // B
|
||||
}
|
||||
pixU32++;
|
||||
|
||||
png_write_row(png_ptr, writeBuffer);
|
||||
|
||||
b = writeBuffer;
|
||||
}
|
||||
} break;
|
||||
}
|
||||
|
||||
png_write_end(png_ptr, info_ptr);
|
||||
|
||||
png_destroy_write_struct(&png_ptr, &info_ptr);
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
return (0 != stbi_write_png(fileName, w, h, CHANNEL_NUM, writeBuffer, w * CHANNEL_NUM));
|
||||
}
|
||||
|
||||
void utilPutDword(uint8_t *p, uint32_t value)
|
||||
|
|
Loading…
Reference in New Issue