From 0806586ee10e29b60a77c351d355a00087f81f57 Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Sat, 29 Dec 2007 14:28:31 -0500 Subject: [PATCH] Start adding image support to liblbx. --- src/Makefile.am | 2 +- src/image.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++ src/image.h | 11 +++++++ 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 src/image.c create mode 100644 src/image.h diff --git a/src/Makefile.am b/src/Makefile.am index d1ddaf2..6867d30 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,7 +2,7 @@ lib_LTLIBRARIES = liblbx.la bin_PROGRAMS = lbxtool include_HEADERS = lbx.h -liblbx_la_SOURCES = byteorder.h lbx.c +liblbx_la_SOURCES = byteorder.h lbx.c image.c lbxtool_SOURCES = lbxtool.c lbxtool_LDADD = liblbx.la diff --git a/src/image.c b/src/image.c new file mode 100644 index 0000000..86f6f98 --- /dev/null +++ b/src/image.c @@ -0,0 +1,80 @@ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include +#include +#include + +#include "lbx.h" +#include "byteorder.h" + +struct lbx_image { + FILE *f; + uint16_t width, height; + uint16_t wtf1, wtf2; + uint16_t offs, frames; + uint32_t offsets[]; +}; + +struct lbx_image *lbximg_fopen(FILE *f) +{ + struct lbx_image tmp = {.f = f}, *new; + + if (fread(&tmp.width, sizeof tmp.width, 1, f) != 1) goto readerr; + if (fread(&tmp.height, sizeof tmp.height, 1, f) != 1) goto readerr; + if (fread(&tmp.wtf1, sizeof tmp.wtf1, 1, f) != 1) goto readerr; + if (fread(&tmp.offs, sizeof tmp.offs, 1, f) != 1) goto readerr; + if (fread(&tmp.frames, sizeof tmp.frames, 1, f) != 1) goto readerr; + if (fread(&tmp.wtf2, sizeof tmp.wtf2, 1, f) != 1) goto readerr; + + tmp.width = letohs(tmp.width); + tmp.height = letohs(tmp.height); + tmp.wtf1 = letohs(tmp.wtf1); + tmp.offs = letohs(tmp.offs); + tmp.frames = letohs(tmp.frames); + tmp.wtf2 = letohs(tmp.wtf2); + + /* + * DEBUG ONLY. These assertions exist to catch otherwise valid image + * files which differ from what I believe to be true of all LBX images. + * If we never find any exceptions, we can replace the assertions with + * assumptions. + */ + assert(tmp.wtf1 == 0); + assert(tmp.offs == tmp.frames + 1); + + new = malloc(sizeof *new + tmp.offs * sizeof *new->offsets); + if (!new) { + lbx_errno = -errno; + return NULL; + } + + *new = tmp; + + if (fread(new->offsets, sizeof *new->offsets, new->offs, f) != new->offs) + goto readerr; + + return new; +readerr: + if (feof(f)) { + lbx_errno = LBX_EEOF; + } else { + lbx_errno = -errno; + } + + free(new); + return NULL; +} + +void lbximg_close(struct lbx_image *img) +{ + if (!img) return; + + if (img->f) { + fclose(img->f); + } + + free(img); +} diff --git a/src/image.h b/src/image.h new file mode 100644 index 0000000..5b5e4a3 --- /dev/null +++ b/src/image.h @@ -0,0 +1,11 @@ +#ifndef LBXIMG_H_ +#define LBXIMG_H_ + +#include + +typedef struct lbx_image LBXIMG; + +LBXIMG *lbximg_fopen(FILE *f); +void lbximg_close(LBXIMG *img); + +#endif -- 2.43.2