The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

ScaledImage.vala in libbirdfont

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git

Revisions

View the latest version of libbirdfont/ScaledImage.vala.
More rotation and high contrast fixes
1 /* 2 Copyright (C) 2015 Johan Mattsson 3 4 This library is free software; you can redistribute it and/or modify 5 it under the terms of the GNU Lesser General Public License as 6 published by the Free Software Foundation; either version 3 of the 7 License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 */ 14 15 using Cairo; 16 using Math; 17 using Gee; 18 19 namespace BirdFont { 20 21 public class ScaledBackground : GLib.Object { 22 ImageSurface image; 23 double scale; 24 ArrayList<ImageSurface> parts; 25 int size; 26 int part_width; 27 int part_height; 28 29 public ScaledBackground (ImageSurface image, double scale) { 30 this.image = image; 31 this.scale = scale; 32 parts = new ArrayList<ImageSurface> (); 33 34 size = 10; 35 part_width = image.get_width () / size; 36 part_height = image.get_height () / size; 37 38 for (int y = 0; y < size; y++) { 39 for (int x = 0; x < size; x++) { 40 ImageSurface next_part; 41 next_part = new ImageSurface (Format.RGB24, part_width, part_height); 42 Context context = new Context (next_part); 43 context.set_source_surface (image, -x * part_width, -y * part_width); 44 context.paint (); 45 parts.add (next_part); 46 } 47 } 48 } 49 50 public double get_scale () { 51 return scale; 52 } 53 54 public ImageSurface get_image () { 55 return image; 56 } 57 58 ImageSurface? get_part_at (int x, int y) { 59 int index = y * size + x; 60 61 if (x >= size || x < 0) { 62 return null; 63 } 64 65 if (y >= size || y < 0) { 66 return null; 67 } 68 69 if (unlikely (!(0 <= index < parts.size))) { 70 warning (@"No part at index: $x, $y"); 71 return null; 72 } 73 74 return parts.get (index); 75 } 76 77 public ScaledBackgroundPart get_part (double offset_x, double offset_y, 78 int width, int height) { 79 80 double image_width = part_width * size; 81 double image_height = part_height * size; 82 83 int start_x = (int) ((offset_x / image_width) * size) - 1; 84 int start_y = (int) ((offset_y / image_height) * size) - 1; 85 int stop_x = (int) (((offset_x + width) / image_width) * size) + 2; 86 int stop_y = (int) (((offset_y + height) / image_height) * size) + 2; 87 88 int assembled_width = (int) ((size + 2) * image_width); 89 int assembled_height = (int) ((size + 2) * image_height); 90 image = new ImageSurface (Format.RGB24, assembled_width, assembled_height); 91 Context context = new Context (image); 92 93 int start_offset_x = start_x * part_width; 94 int start_offset_y = start_y * part_height; 95 96 if (start_offset_x > 0) { 97 start_x = -1; 98 start_offset_x = start_x * part_width; 99 } 100 101 if (start_offset_y > 0) { 102 start_y = -1; 103 start_offset_y = start_y * part_height; 104 } 105 106 for (int y = start_y; y < stop_y; y++) { 107 for (int x = start_x; x < stop_x; x++) { 108 ImageSurface? part = get_part_at (x, y); 109 110 if (part != null) { 111 context.save (); 112 113 context.set_source_surface ((!) part, 114 (x - start_x) * part_width, 115 (y - start_y) * part_height); 116 117 context.paint (); 118 context.restore (); 119 } 120 } 121 } 122 123 return new ScaledBackgroundPart (image, scale, 124 start_offset_x, start_offset_y); 125 } 126 } 127 128 } 129