int load_image(DispContext *dc, const char *filename) { SDL_Surface *img; Frame *frames; FILE *f; uint8_t buf[BPG_DECODER_INFO_BUF_SIZE]; int len, i, frame_count, loop_count; BPGImageInfo bi; f = fopen(filename, "rb"); if (!f) goto fail; len = fread(buf, 1, sizeof(buf), f); if (bpg_decoder_get_info_from_buf(&bi, NULL, buf, len) >= 0) { fseek(f, 0, SEEK_SET); frames = bpg_load(f, &frame_count, &loop_count); if (!frames) goto fail; fclose(f); } else { /* use SDL image loader */ img = IMG_Load(filename); if (!img) { fail: fprintf(stderr, "Could not load '%s'\n", filename); return -1; } frame_count = 1; frames = malloc(sizeof(dc->frames[0]) * frame_count); frames[0].img = img; frames[0].delay = 0; loop_count = 1; } for(i = 0; i < dc->frame_count; i++) { SDL_FreeSurface(dc->frames[i].img); } free(dc->frames); if (dc->frame_timer_id) { SDL_RemoveTimer(dc->frame_timer_id); dc->frame_timer_id = 0; } dc->frame_count = frame_count; dc->frames = frames; dc->frame_index = 0; dc->loop_counter = 0; dc->loop_count = loop_count; dc->img_w = dc->frames[0].img->w; dc->img_h = dc->frames[0].img->h; /* start the animation timer if needed */ if (dc->frame_count > 1) { restart_frame_timer(dc); } return 0; }
static void bpg_show_info(const char *filename, int show_extensions) { uint8_t *buf; int buf_len, ret, buf_len_max; FILE *f; BPGImageInfo p_s, *p = &p_s; BPGExtensionData *first_md, *md; static const char *format_str[6] = { "Gray", "4:2:0", "4:2:2", "4:4:4", "4:2:0_video", "4:2:2_video", }; static const char *color_space_str[BPG_CS_COUNT] = { "YCbCr", "RGB", "YCgCo", "YCbCr_BT709", "YCbCr_BT2020", }; static const char *extension_tag_str[] = { "Unknown", "EXIF", "ICC profile", "XMP", "Thumbnail", "Animation control", }; f = fopen(filename, "rb"); if (!f) { fprintf(stderr, "Could not open %s\n", filename); exit(1); } if (show_extensions) { fseek(f, 0, SEEK_END); buf_len_max = ftell(f); fseek(f, 0, SEEK_SET); } else { /* if no extension are shown, just need the header */ buf_len_max = BPG_DECODER_INFO_BUF_SIZE; } buf = malloc(buf_len_max); buf_len = fread(buf, 1, buf_len_max, f); ret = bpg_decoder_get_info_from_buf(p, show_extensions ? &first_md : NULL, buf, buf_len); free(buf); fclose(f); if (ret < 0) { fprintf(stderr, "Not a BPG image\n"); exit(1); } printf("size=%dx%d color_space=%s", p->width, p->height, p->format == BPG_FORMAT_GRAY ? "Gray" : color_space_str[p->color_space]); if (p->has_w_plane) { printf(" w_plane=%d", p->has_w_plane); } if (p->has_alpha) { printf(" alpha=%d premul=%d", p->has_alpha, p->premultiplied_alpha); } printf(" format=%s limited_range=%d bit_depth=%d animation=%d\n", format_str[p->format], p->limited_range, p->bit_depth, p->has_animation); if (first_md) { const char *tag_name; printf("Extension data:\n"); for(md = first_md; md != NULL; md = md->next) { if (md->tag <= 5) tag_name = extension_tag_str[md->tag]; else tag_name = extension_tag_str[0]; printf(" tag=%d (%s) length=%d\n", md->tag, tag_name, md->buf_len); } bpg_decoder_free_extension_data(first_md); } }