progress.h
Go to the documentation of this file.
00001 /* Copyright (C) 2001-2007 Peter Selinger.
00002    This file is part of Potrace. It is free software and it is covered
00003    by the GNU General Public License. See the file COPYING for details. */
00004 
00005 /* operations on potrace_progress_t objects, which are defined in
00006    potracelib.h. Note: the code attempts to minimize runtime overhead
00007    when no progress monitoring was requested. It also tries to
00008    minimize excessive progress calculations beneath the "epsilon"
00009    threshold. */
00010 
00011 #ifndef PROGRESS_H
00012 #define PROGRESS_H
00013 
00014 /* structure to hold progress bar callback data */
00015 struct progress_s {
00016   void (*callback)(double progress, void *privdata); /* callback fn */
00017   void *data;          /* callback function's private data */
00018   double min, max;     /* desired range of progress, e.g. 0.0 to 1.0 */
00019   double epsilon;      /* granularity: can skip smaller increments */
00020   double b;            /* upper limit of subrange in superrange units */
00021   double d_prev;       /* previous value of d */
00022 };
00023 typedef struct progress_s progress_t;
00024 
00025 /* notify given progress object of current progress. Note that d is
00026    given in the 0.0-1.0 range, which will be scaled and translated to
00027    the progress object's range. */
00028 static inline void progress_update(double d, progress_t *prog) {
00029   double d_scaled;
00030 
00031   if (prog->callback != NULL) {
00032     d_scaled = prog->min * (1-d) + prog->max * d;
00033     if (d == 1.0 || d_scaled >= prog->d_prev + prog->epsilon) {
00034       prog->callback(prog->min * (1-d) + prog->max * d, prog->data);
00035       prog->d_prev = d_scaled;
00036     }
00037   }
00038 }
00039 
00040 /* start a subrange of the given progress object. The range is
00041    narrowed to [a..b], relative to 0.0-1.0 coordinates. If new range
00042    is below granularity threshold, disable further subdivisions. */
00043 static inline void progress_subrange_start(double a, double b, const progress_t *prog, progress_t *sub) {
00044   double min, max;
00045 
00046   if (prog->callback == NULL) {
00047     sub->callback = NULL;
00048     return;
00049   }
00050 
00051   min = prog->min * (1-a) + prog->max * a;
00052   max = prog->min * (1-b) + prog->max * b;
00053 
00054   if (max - min < prog->epsilon) {
00055     sub->callback = NULL;    /* no further progress info in subrange */
00056     sub->b = b;
00057     return;
00058   }
00059   sub->callback = prog->callback;
00060   sub->data = prog->data;
00061   sub->epsilon = prog->epsilon;
00062   sub->min = min;
00063   sub->max = max;
00064   sub->d_prev = prog->d_prev;
00065   return;
00066 }
00067 
00068 static inline void progress_subrange_end(progress_t *prog, progress_t *sub) {
00069   if (prog->callback != NULL) {
00070     if (sub->callback == NULL) {
00071       progress_update(sub->b, prog);
00072     } else {
00073       prog->d_prev = sub->d_prev;
00074     }
00075   }    
00076 }
00077 
00078 #endif /* PROGRESS_H */
00079 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


portrait_painter
Author(s): Niklas Meinzer, Ina Baumgarten
autogenerated on Wed Dec 26 2012 16:00:43