%% options copyright owner = Dirk Krause copyright year = 2013-2014 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 %s %s", TR_PTR(l), TR_PTR(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 %s", TR_PTR(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; }