curve.c 10.5 KB
Newer Older
1
/*  dvdisaster: Additional error correction for optical media.
2
 *  Copyright (C) 2004-2015 Carsten Gnoerlich.
3
 *
4 5 6 7 8 9
 *  Email: carsten@dvdisaster.org  -or-  cgnoerlich@fsfe.org
 *  Project homepage: http://www.dvdisaster.org
 *
 *  This file is part of dvdisaster.
 *
 *  dvdisaster is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation, either version 3 of the License, or
12 13
 *  (at your option) any later version.
 *
14
 *  dvdisaster is distributed in the hope that it will be useful,
15 16 17 18 19
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
20
 *  along with dvdisaster. If not, see <http://www.gnu.org/licenses/>.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
 */

#include "dvdisaster.h"

/***
 *** Constructors and destructors
 ***/

/*
 * Initialize the curve
 */

Curve* CreateCurve(GtkWidget *widget, char *left_label, char *left_format, int n_values, int bottom_format)
{  Curve *curve = g_malloc0(sizeof(Curve));

   curve->widget     = widget;
   curve->layout     = gtk_widget_create_pango_layout(widget, NULL);
   curve->leftLabel  = g_strdup(left_label); 
   curve->leftFormat = g_strdup(left_format);
   curve->bottomFormat = bottom_format;   

   curve->fvalue       = g_malloc0(sizeof(gdouble)*(n_values+1));
   curve->ivalue       = g_malloc0(sizeof(gint)*(n_values+1));
   curve->lvalue       = g_malloc0(sizeof(gint)*(n_values+1));
   curve->lastValueIdx = n_values;

   curve->maxX      = 1;
   curve->maxY      = 1;
   curve->logMaxY   = 1;

   if(bottom_format != CURVE_PERCENT)
     curve->margin = 2;

   return curve;
} 

/*
 * Get rid of it
 */

void FreeCurve(Curve *curve)
{
   g_object_unref(curve->layout);
   g_free(curve->leftLabel);
   if(curve->leftLogLabel)
      g_free(curve->leftLogLabel);
   g_free(curve->leftFormat);
   g_free(curve->fvalue);
   g_free(curve->ivalue);
   g_free(curve->lvalue);
   g_free(curve);
}

/*
 * Reset the values
 */

void ZeroCurve(Curve *curve)
{  int i;

   if(curve)
     for(i=0; i<=curve->lastValueIdx; i++)
     {  curve->fvalue[i] = -1.0;
        curve->ivalue[i] = 0;
        curve->lvalue[i] = 0;
     }
}

/***
 *** Auxiliary functions
 ***/

/*
 * Calculate pixel coords from curve values
 */

int CurveX(Curve *curve, gdouble x)
{  gdouble width = (curve->rightX - curve->leftX - curve->margin);

   return 1 + curve->leftX + ((gdouble)x * width) / 1000.0;
}

int CurveLX(Curve *curve, gdouble x)
{  gdouble width = (curve->rightX - curve->leftX - curve->margin);

   return 1 + curve->leftX + (x * width) / (gdouble)curve->maxX;
}

int CurveY(Curve *curve, gdouble y)
{  gdouble hfact;

   hfact =   (gdouble)(curve->bottomY - curve->topY) 
           / (gdouble)curve->maxY;

   return curve->bottomY - y * hfact;
}

int CurveLogY(Curve *curve, gdouble y) /* not really a log */
{  gdouble hfact;

   if(y<1) return curve->bottomLY;

   hfact = (gdouble)(curve->bottomLY - curve->topLY);
 
   if(y==1) return curve->bottomLY - ((log(2)/log(curve->logMaxY)) * hfact)/2;
            return curve->bottomLY - (log(y)/log(curve->logMaxY)) * hfact;
}

/***
 *** Calculate the curve geometry
 ***/

void UpdateCurveGeometry(Curve *curve, char *largest_left_label, int right_padding)
{  GtkAllocation *a = &curve->widget->allocation;
   int w,h; 

   /* Top and bottom margins */

   SetText(curve->layout, curve->leftLabel, &w, &h);
   curve->topY = h + 10;

   SetText(curve->layout, "0123456789", &w, &h);
   curve->bottomY = a->height - h - 10;

   /* Left and right margins */

   SetText(curve->layout, largest_left_label, &w, &h);
   curve->leftX   = 5 + 6 + 3 + w;
   curve->rightX  = a->width - right_padding;

   /* Add space for the lograithmic curve */

   if(curve->enable & DRAW_LCURVE)
   {  int height = curve->bottomY - curve->topY;

      curve->bottomLY = curve->bottomY;
      curve->bottomY -= height/4;
      curve->topLY = curve->bottomY + h + 15; 
   }
}

/***
 *** Redraw the coordinate axes
 ***/

void RedrawAxes(Curve *curve)
{  GdkDrawable *d = curve->widget->window;
   int i,w,h,x,y; 
   int yg=0;
   int step;
   int bottom_y;

   /* Draw and label the left coordinate axis */

   gdk_gc_set_rgb_fg_color(Closure->drawGC, Closure->foreground);

   gdk_draw_line(d, Closure->drawGC,
		 curve->leftX, curve->topY, curve->leftX, curve->bottomY);

   if(curve->enable & DRAW_LCURVE)
   {  gdk_draw_line(d, Closure->drawGC,
		    curve->leftX, curve->topLY, curve->leftX, curve->bottomLY);
   }

   gdk_gc_set_rgb_fg_color(Closure->drawGC, Closure->curveColor);
   SetText(curve->layout, curve->leftLabel, &w, &h);
   x = curve->leftX - w/2;
   if(x < 5) x = 5;
   gdk_draw_layout(d, Closure->drawGC, 
		   x, curve->topY - h - 5, curve->layout);


   /* Draw and label the grid lines for the log curve */

   if(curve->enable & DRAW_LCURVE)
   {  int val;
      char buf[16];

      gdk_gc_set_rgb_fg_color(Closure->drawGC, Closure->logColor);
      SetText(curve->layout, curve->leftLogLabel, &w, &h);

      x = curve->leftX - w/2;
      if(x < 5) x = 5;
      gdk_draw_layout(d, Closure->drawGC, 
		      x, curve->topLY - h - 5, curve->layout);

      
      for(val=400; val>3; val/=2)
      {  y = CurveLogY(curve, val);
	 sprintf(buf,"%d",val);
	 SetText(curve->layout, buf, &w, &h);
	 gdk_gc_set_rgb_fg_color(Closure->drawGC, Closure->logColor);
	 gdk_draw_layout(d, Closure->drawGC, curve->leftX-9-w, y-h/2, curve->layout);
	 gdk_gc_set_rgb_fg_color(Closure->drawGC, Closure->foreground);
	 gdk_draw_line(d, Closure->drawGC, curve->leftX-6, y, curve->leftX, y);
	 gdk_gc_set_rgb_fg_color(Closure->drawGC, Closure->grid);
	 gdk_draw_line(d, Closure->drawGC, curve->leftX, y, curve->rightX, y);

	 val /=2;
	 y = CurveLogY(curve, val);
	 gdk_gc_set_rgb_fg_color(Closure->drawGC, Closure->foreground);
	 gdk_draw_line(d, Closure->drawGC, curve->leftX-3, y, curve->leftX, y);

	 if(curve->bottomLY-curve->topLY > 8*h)
	 {  sprintf(buf,"%d",val);
	    SetText(curve->layout, buf, &w, &h);
	    gdk_gc_set_rgb_fg_color(Closure->drawGC, Closure->logColor);
	    gdk_draw_layout(d, Closure->drawGC, curve->leftX-9-w, y-h/2, curve->layout);
	 }
      }
   }

   /* Draw and label the grid lines for the normal curve */

   if(curve->maxY < 20) step = 4;
   else step = 10;

   for(i=0; i<=curve->maxY; i+=step)
   {  char buf[4];
   
      g_snprintf(buf, 4, curve->leftFormat, i);
      SetText(curve->layout, buf, &w, &h);

      y = yg = CurveY(curve, i);
      gdk_gc_set_rgb_fg_color(Closure->drawGC, Closure->curveColor);
      gdk_draw_layout(d, Closure->drawGC, curve->leftX-9-w, y-h/2, curve->layout);
      gdk_gc_set_rgb_fg_color(Closure->drawGC, Closure->foreground);
      gdk_draw_line(d, Closure->drawGC, curve->leftX-6, y, curve->leftX, y);

      gdk_gc_set_rgb_fg_color(Closure->drawGC, Closure->grid);
      gdk_draw_line(d, Closure->drawGC, curve->leftX, y, curve->rightX, y);

      gdk_gc_set_rgb_fg_color(Closure->drawGC, Closure->foreground);
      y = CurveY(curve, i+step/2);
      if(y >= curve->topY)
        gdk_draw_line(d, Closure->drawGC, curve->leftX-3, y, curve->leftX, y);
   }


   /* Draw the right coordinate axis */

   gdk_gc_set_rgb_fg_color(Closure->drawGC, Closure->foreground);

   gdk_draw_line(d, Closure->drawGC,
		 curve->rightX, curve->topY, curve->rightX, curve->bottomY);

   if(curve->enable & DRAW_LCURVE)
      gdk_draw_line(d, Closure->drawGC,
		    curve->rightX, curve->topLY, curve->rightX, curve->bottomLY);

   /* Draw and label the bottom coordinate axis */

   gdk_gc_set_rgb_fg_color(Closure->drawGC, Closure->foreground);

   gdk_draw_line(d, Closure->drawGC,
		 curve->leftX, curve->bottomY, curve->rightX, curve->bottomY);

   if(curve->enable & DRAW_LCURVE)
   {  gdk_draw_line(d, Closure->drawGC,
		    curve->leftX, curve->bottomLY, curve->rightX, curve->bottomLY);
      bottom_y = curve->bottomLY;
   }
   else bottom_y = curve->bottomY;

   if(curve->maxX <= 100) step = 20;           /* <100M */
   else if(curve->maxX < 1000)  step = 100;    /* 100M ... 1000M */
   else if(curve->maxX < 8000)  step = 1024;   /* 1G .. 8G */
   else if(curve->maxX < 15000) step = 2560;   /* 8G .. 15G */
   else if(curve->maxX < 25000) step = 5120;   /* 15G .. 25G */
   else step = 10240;

   for(i=0; i<=curve->maxX; i+=step)
   {  char buf[10];
   
      switch(curve->bottomFormat)
      {  case CURVE_PERCENT:
 	   g_snprintf(buf, 10, "%d%%", i);
	   break;

         case CURVE_MEGABYTES:
	   if(step <= 100)
	        g_snprintf(buf, 10, "%dM",i);
	   else g_snprintf(buf, 10, "%3.1fG",(gdouble)i/1024.0);
	   break;
      }
      SetText(curve->layout, buf, &w, &h);

      x = CurveLX(curve,i)-1;
      gdk_draw_line(d, Closure->drawGC, x, bottom_y+6, x, bottom_y);
      gdk_draw_layout(d, Closure->drawGC, x-w/2, bottom_y+8, curve->layout);

      if(i && x < curve->rightX)
      {  gdk_gc_set_rgb_fg_color(Closure->drawGC, Closure->grid);
         gdk_draw_line(d, Closure->drawGC, x, curve->bottomY-1, x, yg);

	 if(curve->enable & DRAW_LCURVE)
	    gdk_draw_line(d, Closure->drawGC, x, curve->bottomLY-1, x, curve->topLY);
      }

      gdk_gc_set_rgb_fg_color(Closure->drawGC, Closure->foreground);
      x = CurveLX(curve,i+step/2)-1;
      if(x < curve->rightX)
        gdk_draw_line(d, Closure->drawGC, x, bottom_y+3, x, bottom_y);
   }
}

/*
 * Redraw the curve
 */

void RedrawCurve(Curve *curve, int last)
{  int i,x0,x1,fy0,fy1;

   x0  = CurveX(curve, 0);
   fy0 = CurveY(curve, curve->fvalue[0]);

   gdk_gc_set_rgb_fg_color(Closure->drawGC, Closure->curveColor);

   /* Draw the curve */

   for(i=1; i<=last; i++)
   {  x1  = CurveX(curve, i);

      if(curve->enable & DRAW_ICURVE)
      {  int iy  = CurveY(curve, curve->ivalue[i]);

	 if(curve->ivalue[i] > 0)
	 {  gdk_gc_set_rgb_fg_color(Closure->drawGC, Closure->barColor);
	    gdk_draw_rectangle(curve->widget->window,
			       Closure->drawGC, TRUE,
			       x0, iy, x0==x1 ? 1 : x1-x0, curve->bottomY-iy);
	 }
      }

      if(curve->enable & DRAW_LCURVE)
      {  int iy  = CurveLogY(curve, curve->lvalue[i]);

	 if(curve->lvalue[i] > 0)
	 {  gdk_gc_set_rgb_fg_color(Closure->drawGC, Closure->logColor);
	    gdk_draw_rectangle(curve->widget->window,
			       Closure->drawGC, TRUE,
			       x0, iy, x0==x1 ? 1 : x1-x0, curve->bottomLY-iy);
	 }
      }

      if(curve->enable & DRAW_FCURVE && curve->fvalue[i] >= 0)
      {  fy1 = CurveY(curve, curve->fvalue[i]);

	 if(x0 < x1)
	 {  gdk_gc_set_rgb_fg_color(Closure->drawGC, Closure->curveColor);
	    gdk_draw_line(curve->widget->window, Closure->drawGC, x0, fy0, x1, fy1);
	    fy0 = fy1;
	 }
      }

      x0 = x1;
   }
}