Merge "Move the png open and destroy functions into a class" am: e687c5a1e1
am: 297a994416
Change-Id: Ic33be526b2bae0533d42477dfb618579f9e97035
This commit is contained in:
+269
-276
@@ -25,10 +25,12 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include <android-base/stringprintf.h>
|
||||||
#include <android-base/strings.h>
|
#include <android-base/strings.h>
|
||||||
#include <png.h>
|
#include <png.h>
|
||||||
|
|
||||||
@@ -46,89 +48,126 @@ static GRSurface* malloc_surface(size_t data_size) {
|
|||||||
return surface;
|
return surface;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr,
|
// This class handles the png file parsing. It also holds the ownership of the png pointer and the
|
||||||
png_uint_32* width, png_uint_32* height, png_byte* channels) {
|
// opened file pointer. Both will be destroyed/closed when this object goes out of scope.
|
||||||
char resPath[256];
|
class PngHandler {
|
||||||
unsigned char header[8];
|
public:
|
||||||
int result = 0;
|
PngHandler(const std::string& name);
|
||||||
int color_type, bit_depth;
|
|
||||||
size_t bytesRead;
|
|
||||||
|
|
||||||
snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
|
~PngHandler();
|
||||||
resPath[sizeof(resPath)-1] = '\0';
|
|
||||||
FILE* fp = fopen(resPath, "rbe");
|
|
||||||
if (fp == NULL) {
|
|
||||||
result = -1;
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
bytesRead = fread(header, 1, sizeof(header), fp);
|
png_uint_32 width() const {
|
||||||
if (bytesRead != sizeof(header)) {
|
return width_;
|
||||||
result = -2;
|
}
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (png_sig_cmp(header, 0, sizeof(header))) {
|
png_uint_32 height() const {
|
||||||
result = -3;
|
return height_;
|
||||||
goto exit;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
*png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
png_byte channels() const {
|
||||||
if (!*png_ptr) {
|
return channels_;
|
||||||
result = -4;
|
}
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
*info_ptr = png_create_info_struct(*png_ptr);
|
png_structp png_ptr() const {
|
||||||
if (!*info_ptr) {
|
return png_ptr_;
|
||||||
result = -5;
|
}
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (setjmp(png_jmpbuf(*png_ptr))) {
|
png_infop info_ptr() const {
|
||||||
result = -6;
|
return info_ptr_;
|
||||||
goto exit;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
png_init_io(*png_ptr, fp);
|
int error_code() const {
|
||||||
png_set_sig_bytes(*png_ptr, sizeof(header));
|
return error_code_;
|
||||||
png_read_info(*png_ptr, *info_ptr);
|
};
|
||||||
|
|
||||||
png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth,
|
operator bool() const {
|
||||||
&color_type, NULL, NULL, NULL);
|
return error_code_ == 0;
|
||||||
|
}
|
||||||
|
|
||||||
*channels = png_get_channels(*png_ptr, *info_ptr);
|
private:
|
||||||
|
png_structp png_ptr_{ nullptr };
|
||||||
|
png_infop info_ptr_{ nullptr };
|
||||||
|
png_uint_32 width_;
|
||||||
|
png_uint_32 height_;
|
||||||
|
png_byte channels_;
|
||||||
|
|
||||||
if (bit_depth == 8 && *channels == 3 && color_type == PNG_COLOR_TYPE_RGB) {
|
// The |error_code_| is set to a negative value if an error occurs when opening the png file.
|
||||||
// 8-bit RGB images: great, nothing to do.
|
int error_code_;
|
||||||
} else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_GRAY) {
|
// After initialization, we'll keep the file pointer open before destruction of PngHandler.
|
||||||
// 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray.
|
std::unique_ptr<FILE, decltype(&fclose)> png_fp_;
|
||||||
png_set_expand_gray_1_2_4_to_8(*png_ptr);
|
};
|
||||||
} else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE) {
|
|
||||||
// paletted images: expand to 8-bit RGB. Note that we DON'T
|
|
||||||
// currently expand the tRNS chunk (if any) to an alpha
|
|
||||||
// channel, because minui doesn't support alpha channels in
|
|
||||||
// general.
|
|
||||||
png_set_palette_to_rgb(*png_ptr);
|
|
||||||
*channels = 3;
|
|
||||||
} else {
|
|
||||||
fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n",
|
|
||||||
bit_depth, *channels, color_type);
|
|
||||||
result = -7;
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
PngHandler::PngHandler(const std::string& name) : error_code_(0), png_fp_(nullptr, fclose) {
|
||||||
|
std::string res_path = android::base::StringPrintf("/res/images/%s.png", name.c_str());
|
||||||
|
png_fp_.reset(fopen(res_path.c_str(), "rbe"));
|
||||||
|
if (!png_fp_) {
|
||||||
|
error_code_ = -1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
exit:
|
unsigned char header[8];
|
||||||
if (result < 0) {
|
size_t bytesRead = fread(header, 1, sizeof(header), png_fp_.get());
|
||||||
png_destroy_read_struct(png_ptr, info_ptr, NULL);
|
if (bytesRead != sizeof(header)) {
|
||||||
}
|
error_code_ = -2;
|
||||||
if (fp != NULL) {
|
return;
|
||||||
fclose(fp);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
if (png_sig_cmp(header, 0, sizeof(header))) {
|
||||||
|
error_code_ = -3;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
png_ptr_ = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
|
||||||
|
if (!png_ptr_) {
|
||||||
|
error_code_ = -4;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
info_ptr_ = png_create_info_struct(png_ptr_);
|
||||||
|
if (!info_ptr_) {
|
||||||
|
error_code_ = -5;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (setjmp(png_jmpbuf(png_ptr_))) {
|
||||||
|
error_code_ = -6;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
png_init_io(png_ptr_, png_fp_.get());
|
||||||
|
png_set_sig_bytes(png_ptr_, sizeof(header));
|
||||||
|
png_read_info(png_ptr_, info_ptr_);
|
||||||
|
|
||||||
|
int color_type;
|
||||||
|
int bit_depth;
|
||||||
|
png_get_IHDR(png_ptr_, info_ptr_, &width_, &height_, &bit_depth, &color_type, nullptr, nullptr,
|
||||||
|
nullptr);
|
||||||
|
|
||||||
|
channels_ = png_get_channels(png_ptr_, info_ptr_);
|
||||||
|
|
||||||
|
if (bit_depth == 8 && channels_ == 3 && color_type == PNG_COLOR_TYPE_RGB) {
|
||||||
|
// 8-bit RGB images: great, nothing to do.
|
||||||
|
} else if (bit_depth <= 8 && channels_ == 1 && color_type == PNG_COLOR_TYPE_GRAY) {
|
||||||
|
// 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray.
|
||||||
|
png_set_expand_gray_1_2_4_to_8(png_ptr_);
|
||||||
|
} else if (bit_depth <= 8 && channels_ == 1 && color_type == PNG_COLOR_TYPE_PALETTE) {
|
||||||
|
// paletted images: expand to 8-bit RGB. Note that we DON'T
|
||||||
|
// currently expand the tRNS chunk (if any) to an alpha
|
||||||
|
// channel, because minui doesn't support alpha channels in
|
||||||
|
// general.
|
||||||
|
png_set_palette_to_rgb(png_ptr_);
|
||||||
|
channels_ = 3;
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n", bit_depth,
|
||||||
|
channels_, color_type);
|
||||||
|
error_code_ = -7;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PngHandler::~PngHandler() {
|
||||||
|
if (png_ptr_) {
|
||||||
|
png_destroy_read_struct(&png_ptr_, &info_ptr_, nullptr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// "display" surfaces are transformed into the framebuffer's required
|
// "display" surfaces are transformed into the framebuffer's required
|
||||||
@@ -198,178 +237,152 @@ static void transform_rgb_to_draw(unsigned char* input_row,
|
|||||||
}
|
}
|
||||||
|
|
||||||
int res_create_display_surface(const char* name, GRSurface** pSurface) {
|
int res_create_display_surface(const char* name, GRSurface** pSurface) {
|
||||||
GRSurface* surface = NULL;
|
*pSurface = nullptr;
|
||||||
int result = 0;
|
|
||||||
png_structp png_ptr = NULL;
|
|
||||||
png_infop info_ptr = NULL;
|
|
||||||
png_uint_32 width, height;
|
|
||||||
png_byte channels;
|
|
||||||
unsigned char* p_row;
|
|
||||||
unsigned int y;
|
|
||||||
|
|
||||||
*pSurface = NULL;
|
PngHandler png_handler(name);
|
||||||
|
if (!png_handler) return png_handler.error_code();
|
||||||
|
|
||||||
result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
|
png_structp png_ptr = png_handler.png_ptr();
|
||||||
if (result < 0) return result;
|
png_uint_32 width = png_handler.width();
|
||||||
|
png_uint_32 height = png_handler.height();
|
||||||
|
|
||||||
surface = init_display_surface(width, height);
|
GRSurface* surface = init_display_surface(width, height);
|
||||||
if (surface == NULL) {
|
if (!surface) {
|
||||||
result = -8;
|
return -8;
|
||||||
goto exit;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
|
#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
|
||||||
png_set_bgr(png_ptr);
|
png_set_bgr(png_ptr);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
p_row = static_cast<unsigned char*>(malloc(width * 4));
|
for (png_uint_32 y = 0; y < height; ++y) {
|
||||||
for (y = 0; y < height; ++y) {
|
std::vector<unsigned char> p_row(width * 4);
|
||||||
png_read_row(png_ptr, p_row, NULL);
|
png_read_row(png_ptr, p_row.data(), nullptr);
|
||||||
transform_rgb_to_draw(p_row, surface->data + y * surface->row_bytes, channels, width);
|
transform_rgb_to_draw(p_row.data(), surface->data + y * surface->row_bytes,
|
||||||
}
|
png_handler.channels(), width);
|
||||||
free(p_row);
|
}
|
||||||
|
|
||||||
*pSurface = surface;
|
*pSurface = surface;
|
||||||
|
|
||||||
exit:
|
return 0;
|
||||||
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
|
||||||
if (result < 0 && surface != NULL) free(surface);
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int res_create_multi_display_surface(const char* name, int* frames, int* fps,
|
int res_create_multi_display_surface(const char* name, int* frames, int* fps,
|
||||||
GRSurface*** pSurface) {
|
GRSurface*** pSurface) {
|
||||||
GRSurface** surface = NULL;
|
*pSurface = nullptr;
|
||||||
int result = 0;
|
*frames = -1;
|
||||||
png_structp png_ptr = NULL;
|
|
||||||
png_infop info_ptr = NULL;
|
|
||||||
png_uint_32 width, height;
|
|
||||||
png_byte channels;
|
|
||||||
png_textp text;
|
|
||||||
int num_text;
|
|
||||||
unsigned char* p_row;
|
|
||||||
unsigned int y;
|
|
||||||
|
|
||||||
*pSurface = NULL;
|
PngHandler png_handler(name);
|
||||||
*frames = -1;
|
if (!png_handler) return png_handler.error_code();
|
||||||
|
|
||||||
result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
|
png_structp png_ptr = png_handler.png_ptr();
|
||||||
if (result < 0) return result;
|
png_uint_32 width = png_handler.width();
|
||||||
|
png_uint_32 height = png_handler.height();
|
||||||
|
|
||||||
*frames = 1;
|
*frames = 1;
|
||||||
*fps = 20;
|
*fps = 20;
|
||||||
if (png_get_text(png_ptr, info_ptr, &text, &num_text)) {
|
png_textp text;
|
||||||
for (int i = 0; i < num_text; ++i) {
|
int num_text;
|
||||||
if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) {
|
if (png_get_text(png_ptr, png_handler.info_ptr(), &text, &num_text)) {
|
||||||
*frames = atoi(text[i].text);
|
for (int i = 0; i < num_text; ++i) {
|
||||||
} else if (text[i].key && strcmp(text[i].key, "FPS") == 0 && text[i].text) {
|
if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) {
|
||||||
*fps = atoi(text[i].text);
|
*frames = atoi(text[i].text);
|
||||||
}
|
} else if (text[i].key && strcmp(text[i].key, "FPS") == 0 && text[i].text) {
|
||||||
}
|
*fps = atoi(text[i].text);
|
||||||
printf(" found frames = %d\n", *frames);
|
}
|
||||||
printf(" found fps = %d\n", *fps);
|
|
||||||
}
|
}
|
||||||
|
printf(" found frames = %d\n", *frames);
|
||||||
|
printf(" found fps = %d\n", *fps);
|
||||||
|
}
|
||||||
|
|
||||||
if (*frames <= 0 || *fps <= 0) {
|
int result = 0;
|
||||||
printf("bad number of frames (%d) and/or FPS (%d)\n", *frames, *fps);
|
GRSurface** surface = nullptr;
|
||||||
result = -10;
|
if (*frames <= 0 || *fps <= 0) {
|
||||||
goto exit;
|
printf("bad number of frames (%d) and/or FPS (%d)\n", *frames, *fps);
|
||||||
}
|
result = -10;
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
|
||||||
if (height % *frames != 0) {
|
if (height % *frames != 0) {
|
||||||
printf("bad height (%d) for frame count (%d)\n", height, *frames);
|
printf("bad height (%d) for frame count (%d)\n", height, *frames);
|
||||||
result = -9;
|
result = -9;
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
surface = static_cast<GRSurface**>(calloc(*frames, sizeof(GRSurface*)));
|
surface = static_cast<GRSurface**>(calloc(*frames, sizeof(GRSurface*)));
|
||||||
if (surface == NULL) {
|
if (!surface) {
|
||||||
result = -8;
|
result = -8;
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < *frames; ++i) {
|
for (int i = 0; i < *frames; ++i) {
|
||||||
surface[i] = init_display_surface(width, height / *frames);
|
surface[i] = init_display_surface(width, height / *frames);
|
||||||
if (surface[i] == NULL) {
|
if (!surface[i]) {
|
||||||
result = -8;
|
result = -8;
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
|
#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
|
||||||
png_set_bgr(png_ptr);
|
png_set_bgr(png_ptr);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
p_row = static_cast<unsigned char*>(malloc(width * 4));
|
for (png_uint_32 y = 0; y < height; ++y) {
|
||||||
for (y = 0; y < height; ++y) {
|
std::vector<unsigned char> p_row(width * 4);
|
||||||
png_read_row(png_ptr, p_row, NULL);
|
png_read_row(png_ptr, p_row.data(), nullptr);
|
||||||
int frame = y % *frames;
|
int frame = y % *frames;
|
||||||
unsigned char* out_row = surface[frame]->data +
|
unsigned char* out_row = surface[frame]->data + (y / *frames) * surface[frame]->row_bytes;
|
||||||
(y / *frames) * surface[frame]->row_bytes;
|
transform_rgb_to_draw(p_row.data(), out_row, png_handler.channels(), width);
|
||||||
transform_rgb_to_draw(p_row, out_row, channels, width);
|
}
|
||||||
}
|
|
||||||
free(p_row);
|
|
||||||
|
|
||||||
*pSurface = surface;
|
*pSurface = surface;
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
if (result < 0) {
|
||||||
|
if (surface) {
|
||||||
if (result < 0) {
|
for (int i = 0; i < *frames; ++i) {
|
||||||
if (surface) {
|
free(surface[i]);
|
||||||
for (int i = 0; i < *frames; ++i) {
|
}
|
||||||
free(surface[i]);
|
free(surface);
|
||||||
}
|
|
||||||
free(surface);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return result;
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
int res_create_alpha_surface(const char* name, GRSurface** pSurface) {
|
int res_create_alpha_surface(const char* name, GRSurface** pSurface) {
|
||||||
GRSurface* surface = NULL;
|
*pSurface = nullptr;
|
||||||
int result = 0;
|
|
||||||
png_structp png_ptr = NULL;
|
|
||||||
png_infop info_ptr = NULL;
|
|
||||||
png_uint_32 width, height;
|
|
||||||
png_byte channels;
|
|
||||||
|
|
||||||
*pSurface = NULL;
|
PngHandler png_handler(name);
|
||||||
|
if (!png_handler) return png_handler.error_code();
|
||||||
|
|
||||||
result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
|
if (png_handler.channels() != 1) {
|
||||||
if (result < 0) return result;
|
return -7;
|
||||||
|
}
|
||||||
|
|
||||||
if (channels != 1) {
|
png_structp png_ptr = png_handler.png_ptr();
|
||||||
result = -7;
|
png_uint_32 width = png_handler.width();
|
||||||
goto exit;
|
png_uint_32 height = png_handler.height();
|
||||||
}
|
|
||||||
|
|
||||||
surface = malloc_surface(width * height);
|
GRSurface* surface = malloc_surface(width * height);
|
||||||
if (surface == NULL) {
|
if (!surface) {
|
||||||
result = -8;
|
return -8;
|
||||||
goto exit;
|
}
|
||||||
}
|
surface->width = width;
|
||||||
surface->width = width;
|
surface->height = height;
|
||||||
surface->height = height;
|
surface->row_bytes = width;
|
||||||
surface->row_bytes = width;
|
surface->pixel_bytes = 1;
|
||||||
surface->pixel_bytes = 1;
|
|
||||||
|
|
||||||
#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
|
#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
|
||||||
png_set_bgr(png_ptr);
|
png_set_bgr(png_ptr);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
unsigned char* p_row;
|
for (png_uint_32 y = 0; y < height; ++y) {
|
||||||
unsigned int y;
|
unsigned char* p_row = surface->data + y * surface->row_bytes;
|
||||||
for (y = 0; y < height; ++y) {
|
png_read_row(png_ptr, p_row, nullptr);
|
||||||
p_row = surface->data + y * surface->row_bytes;
|
}
|
||||||
png_read_row(png_ptr, p_row, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
*pSurface = surface;
|
*pSurface = surface;
|
||||||
|
|
||||||
exit:
|
return 0;
|
||||||
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
|
||||||
if (result < 0 && surface != NULL) free(surface);
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function tests if a locale string stored in PNG (prefix) matches
|
// This function tests if a locale string stored in PNG (prefix) matches
|
||||||
@@ -397,109 +410,89 @@ bool matches_locale(const std::string& prefix, const std::string& locale) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> get_locales_in_png(const std::string& png_name) {
|
std::vector<std::string> get_locales_in_png(const std::string& png_name) {
|
||||||
png_structp png_ptr = nullptr;
|
PngHandler png_handler(png_name);
|
||||||
png_infop info_ptr = nullptr;
|
if (!png_handler) {
|
||||||
png_uint_32 width, height;
|
printf("Failed to open %s, error: %d\n", png_name.c_str(), png_handler.error_code());
|
||||||
png_byte channels;
|
|
||||||
|
|
||||||
int status = open_png(png_name.c_str(), &png_ptr, &info_ptr, &width, &height, &channels);
|
|
||||||
if (status < 0) {
|
|
||||||
printf("Failed to open %s\n", png_name.c_str());
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
if (channels != 1) {
|
if (png_handler.channels() != 1) {
|
||||||
printf("Expect input png to have 1 data channel, this file has %d\n", channels);
|
printf("Expect input png to have 1 data channel, this file has %d\n", png_handler.channels());
|
||||||
png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> result;
|
std::vector<std::string> result;
|
||||||
std::vector<unsigned char> row(width);
|
std::vector<unsigned char> row(png_handler.width());
|
||||||
for (png_uint_32 y = 0; y < height; ++y) {
|
for (png_uint_32 y = 0; y < png_handler.height(); ++y) {
|
||||||
png_read_row(png_ptr, row.data(), nullptr);
|
png_read_row(png_handler.png_ptr(), row.data(), nullptr);
|
||||||
int h = (row[3] << 8) | row[2];
|
int h = (row[3] << 8) | row[2];
|
||||||
std::string loc(reinterpret_cast<char*>(&row[5]));
|
std::string loc(reinterpret_cast<char*>(&row[5]));
|
||||||
if (!loc.empty()) {
|
if (!loc.empty()) {
|
||||||
result.push_back(loc);
|
result.push_back(loc);
|
||||||
}
|
}
|
||||||
for (int i = 0; i < h; ++i, ++y) {
|
for (int i = 0; i < h; ++i, ++y) {
|
||||||
png_read_row(png_ptr, row.data(), NULL);
|
png_read_row(png_handler.png_ptr(), row.data(), nullptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
int res_create_localized_alpha_surface(const char* name,
|
int res_create_localized_alpha_surface(const char* name,
|
||||||
const char* locale,
|
const char* locale,
|
||||||
GRSurface** pSurface) {
|
GRSurface** pSurface) {
|
||||||
GRSurface* surface = NULL;
|
*pSurface = nullptr;
|
||||||
int result = 0;
|
if (locale == nullptr) {
|
||||||
png_structp png_ptr = NULL;
|
return 0;
|
||||||
png_infop info_ptr = NULL;
|
}
|
||||||
png_uint_32 width, height;
|
|
||||||
png_byte channels;
|
|
||||||
png_uint_32 y;
|
|
||||||
std::vector<unsigned char> row;
|
|
||||||
|
|
||||||
*pSurface = NULL;
|
PngHandler png_handler(name);
|
||||||
|
if (!png_handler) return png_handler.error_code();
|
||||||
|
|
||||||
if (locale == NULL) {
|
if (png_handler.channels() != 1) {
|
||||||
return result;
|
return -7;
|
||||||
|
}
|
||||||
|
|
||||||
|
png_structp png_ptr = png_handler.png_ptr();
|
||||||
|
png_uint_32 width = png_handler.width();
|
||||||
|
png_uint_32 height = png_handler.height();
|
||||||
|
|
||||||
|
for (png_uint_32 y = 0; y < height; ++y) {
|
||||||
|
std::vector<unsigned char> row(width);
|
||||||
|
png_read_row(png_ptr, row.data(), nullptr);
|
||||||
|
int w = (row[1] << 8) | row[0];
|
||||||
|
int h = (row[3] << 8) | row[2];
|
||||||
|
__unused int len = row[4];
|
||||||
|
char* loc = reinterpret_cast<char*>(&row[5]);
|
||||||
|
|
||||||
|
if (y + 1 + h >= height || matches_locale(loc, locale)) {
|
||||||
|
printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y);
|
||||||
|
|
||||||
|
GRSurface* surface = malloc_surface(w * h);
|
||||||
|
if (!surface) {
|
||||||
|
return -8;
|
||||||
|
}
|
||||||
|
surface->width = w;
|
||||||
|
surface->height = h;
|
||||||
|
surface->row_bytes = w;
|
||||||
|
surface->pixel_bytes = 1;
|
||||||
|
|
||||||
|
for (int i = 0; i < h; ++i, ++y) {
|
||||||
|
png_read_row(png_ptr, row.data(), nullptr);
|
||||||
|
memcpy(surface->data + i * w, row.data(), w);
|
||||||
|
}
|
||||||
|
|
||||||
|
*pSurface = surface;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
|
for (int i = 0; i < h; ++i, ++y) {
|
||||||
if (result < 0) return result;
|
png_read_row(png_ptr, row.data(), nullptr);
|
||||||
|
|
||||||
if (channels != 1) {
|
|
||||||
result = -7;
|
|
||||||
goto exit;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
row.resize(width);
|
return 0;
|
||||||
for (y = 0; y < height; ++y) {
|
|
||||||
png_read_row(png_ptr, row.data(), NULL);
|
|
||||||
int w = (row[1] << 8) | row[0];
|
|
||||||
int h = (row[3] << 8) | row[2];
|
|
||||||
__unused int len = row[4];
|
|
||||||
char* loc = reinterpret_cast<char*>(&row[5]);
|
|
||||||
|
|
||||||
if (y+1+h >= height || matches_locale(loc, locale)) {
|
|
||||||
printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y);
|
|
||||||
|
|
||||||
surface = malloc_surface(w*h);
|
|
||||||
if (surface == NULL) {
|
|
||||||
result = -8;
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
surface->width = w;
|
|
||||||
surface->height = h;
|
|
||||||
surface->row_bytes = w;
|
|
||||||
surface->pixel_bytes = 1;
|
|
||||||
|
|
||||||
int i;
|
|
||||||
for (i = 0; i < h; ++i, ++y) {
|
|
||||||
png_read_row(png_ptr, row.data(), NULL);
|
|
||||||
memcpy(surface->data + i*w, row.data(), w);
|
|
||||||
}
|
|
||||||
|
|
||||||
*pSurface = surface;
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
int i;
|
|
||||||
for (i = 0; i < h; ++i, ++y) {
|
|
||||||
png_read_row(png_ptr, row.data(), NULL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
exit:
|
|
||||||
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
|
||||||
if (result < 0 && surface != NULL) free(surface);
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void res_free_surface(GRSurface* surface) {
|
void res_free_surface(GRSurface* surface) {
|
||||||
free(surface);
|
free(surface);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user