Clone in a terminal:
Commit 7377fd1
Johan Mattsson - 2015-12-11T20:51:23+00:00
7377fd18b513111e2e85b59f62caa8b861605052johan.mattsson.m@gmail.com

Speed optimization for rotation of raster images

Changed files (4)

Diff

diff --git a/libbirdfont/BackgroundImage.vala b/libbirdfont/BackgroundImage.valaindex a50994d..dc62e76 100644--- a/libbirdfont/BackgroundImage.vala+++ b/libbirdfont/BackgroundImage.vala@@ -54,6 +54,7 @@ public class BackgroundImage { 	private double img_scale_y_size = 1; 	 	public double img_rotation = 0;+	private double preview_img_rotation = 0; 	 	private int size = -1; @@ -383,21 +384,23 @@ public class BackgroundImage { 		return get_scaled_backgrounds (); 	} 	+	public void start_rotation_preview () {+		preview_img_rotation = img_rotation;+	}+ 	public void preview_img_rotation_from_coordinate (double x, double y, double view_zoom) { 		double rotation;-		ScaledBackgrounds backgounds;+		ScaledBackgrounds backgrounds;+		ScaledBackground backgound; 		-		if (get_img_rotation_from_coordinate (x, y, out rotation)) {-			img_rotation = rotation;-			backgounds = get_image ();-			ImageSurface rotated;+		if (get_img_rotation_from_coordinate (x, y, out rotation)) {	+			backgrounds = get_scaled_backgrounds ();+			backgound = backgrounds.get_image (view_zoom * img_scale_x); // FIXME: y 			 			img_rotation = rotation; 			 			if (!high_contrast) {-				rotated = rotate ((ImageSurface) get_padded_image ());-				// FIXME: y-				scaled = new ScaledBackgrounds.single_size (rotated, img_scale_x * view_zoom);+				backgound.rotate (rotation - preview_img_rotation); 			} else { 				contrast_image = null; 			}@@ -408,11 +411,17 @@ public class BackgroundImage { 		double rotation; 		if (get_img_rotation_from_coordinate (x, y, out rotation)) { 			img_rotation = rotation;-			scaled = null;-			contrast_image = null;+			Task task = new Task (cache_scaled_image); // cache all sizes+			MainWindow.run_blocking_task (task); 		} 	} 	+	void cache_scaled_image () {+		scaled = null;+		contrast_image = null;+		get_image ();+	}+	 	public bool get_img_rotation_from_coordinate (double x, double y, out double rotation) { 		double bcx, bcy; 		double a, b, c, length;@@ -526,7 +535,7 @@ public class BackgroundImage { 			 			scaled_context.set_source_surface (part.get_image (), scaled_x, scaled_y); 			scaled_context.paint ();-		} else {		+		} else { 			ImageSurface contrast = get_contrast_image (); 			 			image_scale_x = img_scale_x * ((double) size_margin / contrast.get_width ());diff --git a/libbirdfont/BackgroundTool.vala b/libbirdfont/BackgroundTool.valaindex 36f6ec4..aae4960 100644--- a/libbirdfont/BackgroundTool.vala+++ b/libbirdfont/BackgroundTool.vala@@ -80,6 +80,8 @@ public class BackgroundTool : Tool { 			img_width = background.get_img ().get_width () * background.img_scale_x; 			img_height = background.get_img ().get_height () * background.img_scale_y; 			+			background.start_rotation_preview ();+			 			move_bg = true; 			 			last_x = x;diff --git a/libbirdfont/ScaledBackgrounds.vala b/libbirdfont/ScaledBackgrounds.valaindex aa47550..32ec238 100644--- a/libbirdfont/ScaledBackgrounds.vala+++ b/libbirdfont/ScaledBackgrounds.vala@@ -34,14 +34,6 @@ public class ScaledBackgrounds : GLib.Object { 			scaled.add (image); 		} 	}--	public ScaledBackgrounds.single_size (ImageSurface original, double scale_factor) {-		this.original = original;-		scaled = new ArrayList<ScaledBackground> ();--		ScaledBackground image = scale (scale_factor);-		scaled.add (image);-	} 	 	public ScaledBackground get_image (double scale) { 		foreach (ScaledBackground image in scaled) {@@ -63,8 +55,9 @@ public class ScaledBackgrounds : GLib.Object { 			scale_factor = 1; 		} 		-		int width = (int) (scale_factor * original.get_width ());-		int height = (int) (scale_factor * original.get_height ());+		int width = (int) (original.get_width () * scale_factor);+		int height = (int) (original.get_height () * scale_factor);+		 		scaled_image = new ImageSurface (Format.ARGB32, width, height); 		Context context = new Context (scaled_image); 		context.scale (scale_factor, scale_factor);diff --git a/libbirdfont/ScaledImage.vala b/libbirdfont/ScaledImage.valaindex 72161e1..13ca59a 100644--- a/libbirdfont/ScaledImage.vala+++ b/libbirdfont/ScaledImage.vala@@ -20,7 +20,7 @@ namespace BirdFont {  public class ScaledBackground : GLib.Object { 	ImageSurface image;-	ImageSurface rotated;+	ImageSurface original; 	ArrayList<ImageSurface> parts; 	int size; 	int part_width;@@ -33,13 +33,18 @@ public class ScaledBackground : GLib.Object { 			scale = 1; 		} 		+		original = image; 		this.image = image;-		rotated = image; 		this.scale = scale; 		parts = new ArrayList<ImageSurface> ();		 		 		create_parts (); 	}++	public void rotate (double angle) {+		image = BackgroundImage.rotate_image (original, angle);+		create_parts ();+	} 	 	void create_parts () { 		parts.clear ();@@ -65,16 +70,15 @@ public class ScaledBackground : GLib.Object { 				ImageSurface next_part; 				next_part = new ImageSurface (Format.ARGB32, part_width, part_height); 				Context context = new Context (next_part);-				context.set_source_surface (rotated, -x * part_width, -y * part_width);+				context.set_source_surface (image, -x * part_width, -y * part_width); 				context.paint (); 				parts.add (next_part); 			} 		}	 	} 	-	public void rotate (double angle) {-		rotated = BackgroundImage.rotate_image (image, angle);-		create_parts ();+	public void set_scale (double s) {+		scale = s; 	} 	 	public double get_scale () {@@ -141,12 +145,12 @@ public class ScaledBackground : GLib.Object { 			stop_y = size; 		} 				-		int assembled_width = (int) ((stop_x - start_x) * image_width);-		int assembled_height = (int) ((stop_y - start_y) * image_height);+		int assembled_width = (int) ((stop_x - start_x) * part_width);+		int assembled_height = (int) ((stop_y - start_y) * part_height); 		-		image = new ImageSurface (Format.ARGB32, assembled_width, assembled_height);+		ImageSurface assembled_image = new ImageSurface (Format.ARGB32, assembled_width, assembled_height); -		Context context = new Context (image);+		Context context = new Context (assembled_image);  		int start_offset_x = start_x * part_width; 		int start_offset_y = start_y * part_height;@@ -168,7 +172,7 @@ public class ScaledBackground : GLib.Object { 			} 		} 		-		return new ScaledBackgroundPart (image, scale,+		return new ScaledBackgroundPart (assembled_image, scale, 				start_offset_x, start_offset_y); 	} }

Contact

Email: johan.mattsson.m@gmail.com
Mastodon: typo.social/@birdfont
GitHub: johanmattssonm
LinkedIn: johan-mattsson-2ab324112