summaryrefslogtreecommitdiff
path: root/support/dktools/hbimgdim.ctr
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
committerNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
commite0c6872cf40896c7be36b11dcc744620f10adf1d (patch)
tree60335e10d2f4354b0674ec22d7b53f0f8abee672 /support/dktools/hbimgdim.ctr
Initial commit
Diffstat (limited to 'support/dktools/hbimgdim.ctr')
-rw-r--r--support/dktools/hbimgdim.ctr192
1 files changed, 192 insertions, 0 deletions
diff --git a/support/dktools/hbimgdim.ctr b/support/dktools/hbimgdim.ctr
new file mode 100644
index 0000000000..920d9950e2
--- /dev/null
+++ b/support/dktools/hbimgdim.ctr
@@ -0,0 +1,192 @@
+%% options
+
+copyright owner = Dirk Krause
+copyright year = 2013-xxxx
+license = bsd
+
+
+
+%% header
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** Find image dimensions.
+ @param job Job structure.
+ @param fn Image file name.
+ @param w Pointer to result variable for width.
+ @param h Pointer to result variable for height.
+ @param save Flag: Save result to cache.
+ @return 1 on success, 0 on error.
+*/
+int
+hbimgdim_find(
+ hb_job_t *job,
+ dkChar const *fn,
+ unsigned long *w,
+ unsigned long *h,
+ int save
+);
+
+/** Create image dimension cache entry, allocate memory.
+ @param fn Image file name.
+ @param w Image width.
+ @param h Image height.
+ @param app Application structure for diagnostics, may be NULL.
+ @return Pointer to new entry on success, NULL on error.
+*/
+hb_img_dim_t *
+hbimgdim_new(
+ dkChar const *fn,
+ unsigned long w,
+ unsigned long h,
+ dk3_app_t *app
+);
+
+/** Delete image dimension cache entry, release memory.
+ @param ptr Cache entry to delete.
+*/
+void
+hbimgdim_delete(hb_img_dim_t *ptr);
+
+/** Compare two image dimension entries.
+ @param l Left entry pointer.
+ @param r Right entry pointer or file name pointer.
+ @param cr Comparison criteria (0=entry/entry, 1=entry/name).
+ @return Comparison result.
+*/
+int
+hbimgdim_compare(void const *l, void const *r, int cr);
+
+#ifdef __cplusplus
+}
+#endif
+
+
+
+%% module
+
+
+#include "dk3all.h"
+#include "dk3bif.h"
+#include "htmlbook.h"
+
+
+
+$!trace-include
+
+
+
+int
+hbimgdim_compare(void const *l, void const *r, int cr)
+{
+ int back = 0;
+ hb_img_dim_t *pl;
+ hb_img_dim_t *pr;
+ $? "+ hbimgdim_compare PTR=%d PTR=%d", TR_IPTR(l), TR_IPTR(r)
+ if(l) {
+ if(r) {
+ pl = (hb_img_dim_t *)l;
+ switch(cr) {
+ case 1: {
+ back = dk3str_fncmp(pl->fn, (dkChar const *)r);
+ } break;
+ default: {
+ pr = (hb_img_dim_t *)r;
+ back = dk3str_fncmp(pl->fn, pr->fn);
+ } break;
+ }
+ if(-1 > back) { back = -1; }
+ if( 1 < back) { back = 1; }
+ } else back = 1;
+ } else {
+ if(r) back = -1;
+ } $? "- hbimgdim_compare %d", back
+ return back;
+}
+
+
+
+void
+hbimgdim_delete(hb_img_dim_t *ptr)
+{
+ $? "+ hbimgdim_delete"
+ if(ptr) {
+ dk3_release(ptr->fn);
+ ptr->w = 0UL;
+ ptr->h = 0UL;
+ dk3_delete(ptr);
+ } $? "- hbimgdim_delete"
+}
+
+
+
+hb_img_dim_t *
+hbimgdim_new(dkChar const *fn, unsigned long w, unsigned long h, dk3_app_t *app)
+{
+ hb_img_dim_t *back = NULL;
+ $? "+ hbimgdim_new"
+ back = dk3_new_app(hb_img_dim_t,1,app);
+ if(back) {
+ back->w = w;
+ back->h = h;
+ back->fn = dk3str_dup_app(fn,app);
+ if(!(back->fn)) {
+ hbimgdim_delete(back);
+ back = NULL;
+ }
+ } $? "- hbimgdim_new PTR=%d", TR_IPTR(back)
+ return back;
+}
+
+
+
+int
+hbimgdim_find(
+ hb_job_t *job,
+ dkChar const *fn,
+ unsigned long *w,
+ unsigned long *h,
+ int save
+)
+{
+ hb_img_dim_t *idptr; /* Image dimension found or new */
+ dk3_bif_t *bp; /* BIF object for graphics file */
+ unsigned long xw; /* Width read from file */
+ unsigned long xh; /* Height read from file */
+ int back = 0;
+ $? "+ hbimgdim_find"
+ if((job) && (fn) && (w) && (h)) {
+ idptr = dk3sto_it_find_like(job->i_imgdim, fn, 1);
+ if(idptr) {
+ *w = idptr->w;
+ *h = idptr->h;
+ back = 1;
+ } else {
+ bp = dk3bif_open_filename_app(fn, DK3_BIF_IMAGE_TYPE_UNKNOWN, job->app);
+ if(bp) {
+ xw = dk3bif_get_width(bp);
+ xh = dk3bif_get_height(bp);
+ dk3bif_close(bp);
+ if((xw) && (xh)) {
+ *w = xw;
+ *h = xh;
+ back = 1;
+ if(save) {
+ idptr = hbimgdim_new(fn, xw, xh, job->app);
+ if(idptr) {
+ if(!(dk3sto_add(job->s_imgdim, idptr))) {
+ hbimgdim_delete(idptr);
+ }
+ }
+ }
+ }
+ }
+ }
+ } $? "- hbimgdim_find %d", back
+ return back;
+}
+
+
+