summaryrefslogtreecommitdiffstats
path: root/minui/resources.c
diff options
context:
space:
mode:
Diffstat (limited to 'minui/resources.c')
-rw-r--r--minui/resources.c158
1 files changed, 158 insertions, 0 deletions
diff --git a/minui/resources.c b/minui/resources.c
index 064325edf..605edbca7 100644
--- a/minui/resources.c
+++ b/minui/resources.c
@@ -186,6 +186,164 @@ exit:
return result;
}
+static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr,
+ png_uint_32* width, png_uint_32* height, png_byte* channels) {
+ char resPath[256];
+ unsigned char header[8];
+ int result = 0;
+
+ snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
+ resPath[sizeof(resPath)-1] = '\0';
+ FILE* fp = fopen(resPath, "rb");
+ if (fp == NULL) {
+ result = -1;
+ goto exit;
+ }
+
+ size_t bytesRead = fread(header, 1, sizeof(header), fp);
+ if (bytesRead != sizeof(header)) {
+ result = -2;
+ goto exit;
+ }
+
+ if (png_sig_cmp(header, 0, sizeof(header))) {
+ result = -3;
+ goto exit;
+ }
+
+ *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+ if (!*png_ptr) {
+ result = -4;
+ goto exit;
+ }
+
+ *info_ptr = png_create_info_struct(*png_ptr);
+ if (!*info_ptr) {
+ result = -5;
+ goto exit;
+ }
+
+ if (setjmp(png_jmpbuf(*png_ptr))) {
+ result = -6;
+ goto exit;
+ }
+
+ png_init_io(*png_ptr, fp);
+ png_set_sig_bytes(*png_ptr, sizeof(header));
+ png_read_info(*png_ptr, *info_ptr);
+
+ int color_type, bit_depth;
+ png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth,
+ &color_type, NULL, NULL, NULL);
+
+ *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);
+ result = -7;
+ goto exit;
+ }
+
+ return result;
+
+ exit:
+ if (result < 0) {
+ png_destroy_read_struct(png_ptr, info_ptr, NULL);
+ }
+ if (fp != NULL) {
+ fclose(fp);
+ }
+
+ return result;
+}
+
+int res_create_multi_display_surface(const char* name, int* frames, gr_surface** pSurface) {
+ gr_surface* surface = NULL;
+ int result = 0;
+ png_structp png_ptr = NULL;
+ png_infop info_ptr = NULL;
+ png_uint_32 width, height;
+ png_byte channels;
+ int i;
+
+ *pSurface = NULL;
+ *frames = -1;
+
+ result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
+ if (result < 0) return result;
+
+ *frames = 1;
+ png_textp text;
+ int num_text;
+ if (png_get_text(png_ptr, info_ptr, &text, &num_text)) {
+ for (i = 0; i < num_text; ++i) {
+ if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) {
+ *frames = atoi(text[i].text);
+ break;
+ }
+ }
+ printf(" found frames = %d\n", *frames);
+ }
+
+ if (height % *frames != 0) {
+ printf("bad height (%d) for frame count (%d)\n", height, *frames);
+ result = -9;
+ goto exit;
+ }
+
+ surface = malloc(*frames * sizeof(gr_surface));
+ if (surface == NULL) {
+ result = -8;
+ goto exit;
+ }
+ for (i = 0; i < *frames; ++i) {
+ surface[i] = NULL;//init_display_surface(width, height / *frames);
+ if (surface[i] == NULL) {
+ result = -8;
+ goto exit;
+ }
+ }
+
+ /*unsigned char* p_row = malloc(width * 4);
+ unsigned int y;
+ for (y = 0; y < height; ++y) {
+ png_read_row(png_ptr, p_row, NULL);
+ int frame = y % *frames;
+ unsigned char* out_row = surface[frame]->data +
+ (y / *frames) * surface[frame]->row_bytes;
+ transform_rgb_to_draw(p_row, out_row, channels, width);
+ }
+ free(p_row); this will need to be brought in line with the older resources and graphics code */
+
+ *pSurface = (gr_surface*) surface;
+
+exit:
+ png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
+
+ if (result < 0) {
+ if (surface) {
+ for (i = 0; i < *frames; ++i) {
+ if (surface[i]) free(surface[i]);
+ }
+ free(surface);
+ }
+ }
+ return result;
+}
+
static int matches_locale(const char* loc) {
if (locale == NULL) return 0;