The Birdfont Source Code


All Repositories / birdfont.git / commit – RSS feed

Fix optimized rendering of raster images

These changes was commited to the Birdfont repository Fri, 11 Dec 2015 00:25:27 +0000.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git
author Johan Mattsson <johan.mattsson.m@gmail.com>
Fri, 11 Dec 2015 00:25:27 +0000 (01:25 +0100)
committer Johan Mattsson <johan.mattsson.m@gmail.com>
Fri, 11 Dec 2015 00:25:27 +0000 (01:25 +0100)
commit 22b9d2e8a1a7b3b5eb83743033ebc43515d6052b
tree 8c64fb0617e66d1aa087418dda36d65b3117650f
parent 4f64c1e76e8d25fdb10e4e383b311172af1e91ba
Fix optimized rendering of raster images

libbirdfont/BackgroundImage.vala
libbirdfont/ScaledBackgrounds.vala
libbirdfont/ScaledImage.vala
--- a/libbirdfont/BackgroundImage.vala +++ b/libbirdfont/BackgroundImage.vala @@ -25,9 +25,33 @@ /** Image position in canvas coordinates. */ public double img_x = 0; public double img_y = 0; + + public double img_scale_x { + get { + return img_scale_x_size; + } - public double img_scale_x = 1; - public double img_scale_y = 1; + set { + if (value > 0.0001) { + img_scale_x_size = value; + } + } + } + + public double img_scale_y { + get { + return img_scale_y_size; + } + + set { + if (value > 0.0001) { + img_scale_y_size = value; + } + } + } + + private double img_scale_x_size = 1; + private double img_scale_y_size = 1; public double img_rotation = 0; @@ -458,17 +482,20 @@ ScaledBackgroundPart part; scaled = backgrounds.get_image (view_zoom * img_scale_x); // FIXME: y - + double part_offset_x = img_offset_x - view_offset_x; - part_offset_x *= view_zoom; + part_offset_x /= img_scale_x / scaled.get_scale (); part_offset_x = -part_offset_x; double part_offset_y = img_offset_y - view_offset_y; - part_offset_y *= view_zoom; + part_offset_y /= img_scale_y / scaled.get_scale (); part_offset_y = -part_offset_y; + + double zoom = 1 / view_zoom; + zoom /= img_scale_y / scaled.get_scale (); part = scaled.get_part (part_offset_x, part_offset_y, - (int) (allocation.width * view_zoom), (int) (allocation.height * view_zoom)); + (int) (allocation.width * zoom), (int) (allocation.height * zoom)); scale_x = view_zoom * image_scale_x; scale_y = view_zoom * image_scale_y;
--- a/libbirdfont/ScaledBackgrounds.vala +++ b/libbirdfont/ScaledBackgrounds.vala @@ -57,6 +57,12 @@ private ScaledBackground scale (double scale_factor) { ImageSurface scaled_image; + + if (scale_factor <= 0) { + warning ("scale_factor <= 0"); + scale_factor = 1; + } + int width = (int) (scale_factor * original.get_width ()); int height = (int) (scale_factor * original.get_height ()); scaled_image = new ImageSurface (Format.ARGB32, width, height);
--- a/libbirdfont/ScaledImage.vala +++ b/libbirdfont/ScaledImage.vala @@ -28,6 +28,11 @@ double scale; public ScaledBackground (ImageSurface image, double scale) { + if (scale <= 0) { + warning ("scale <= 0"); + scale = 1; + } + this.image = image; rotated = image; this.scale = scale; @@ -46,7 +51,15 @@ part_width = image.get_width () / size; part_height = image.get_height () / size; - + + if (part_width < 1) { + part_width = 1; + } + + if (part_height < 1) { + part_height = 1; + } + for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { ImageSurface next_part; @@ -65,6 +78,11 @@ } public double get_scale () { + if (unlikely (scale == 0)) { + warning ("Zero scale."); + return 1; + } + return scale; } @@ -93,17 +111,30 @@ public ScaledBackgroundPart get_part (double offset_x, double offset_y, int width, int height) { - + + if (width <= 0 || height <= 0) { + warning ("width <= 0 || height <= 0"); + scale = 1; + } + double image_width = part_width * size; double image_height = part_height * size; - int start_x = (int) ((offset_x / image_width) * size) - 1; - int start_y = (int) ((offset_y / image_height) * size) - 1; - int stop_x = (int) (((offset_x + width) / image_width) * size) + 2; - int stop_y = (int) (((offset_y + height) / image_height) * size) + 2; + int start_x = (int) ((offset_x / image_width) * size); + int start_y = (int) ((offset_y / image_height) * size); + int stop_x = (int) (((offset_x + width) / image_width) * size) + 1; + int stop_y = (int) (((offset_y + height) / image_height) * size) + 1; + + if (start_x < 0) { + start_x = 0; + } + + if (start_y < 0) { + start_y = 0; + } - int assembled_width = (int) ((size + 2) * image_width); - int assembled_height = (int) ((size + 2) * image_height); + int assembled_width = (int) ((stop_x - start_x) * image_width); + int assembled_height = (int) ((stop_y - start_y) * image_height); image = new ImageSurface (Format.ARGB32, assembled_width, assembled_height); @@ -112,28 +143,18 @@ int start_offset_x = start_x * part_width; int start_offset_y = start_y * part_height; - if (start_offset_x > 0) { - start_x = -1; - start_offset_x = start_x * part_width; - } - - if (start_offset_y > 0) { - start_y = -1; - start_offset_y = start_y * part_height; - } - for (int y = start_y; y < stop_y; y++) { for (int x = start_x; x < stop_x; x++) { ImageSurface? part = get_part_at (x, y); if (part != null) { context.save (); - + context.set_source_surface ((!) part, (x - start_x) * part_width, (y - start_y) * part_height); - context.paint (); + context.restore (); } }