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 ArrayList<ImageSurface> parts; 24 int size; 25 int part_width; 26 int part_height; 27 double scale; 28 29 public ScaledBackground (ImageSurface image, double scale) { 30 this.image = image; 31 this.scale = scale; 32 33 parts = new ArrayList<ImageSurface> (); 34 35 size = 10; 36 part_width = image.get_width () / size; 37 part_height = image.get_height () / size; 38 39 for (int y = 0; y < size; y++) { 40 for (int x = 0; x < size; x++) { 41 ImageSurface next_part; 42 next_part = new ImageSurface (Format.RGB24, part_width, part_height); 43 Context context = new Context (next_part); 44 context.set_source_surface (image, -x * part_width, -y * part_width); 45 context.paint (); 46 parts.add (next_part); 47 } 48 } 49 } 50 51 public double get_scale () { 52 return scale; 53 } 54 55 public ImageSurface get_image () { 56 return image; 57 } 58 59 ImageSurface? get_part_at (int x, int y) { 60 int index = y * size + x; 61 62 if (x >= size || x < 0) { 63 return null; 64 } 65 66 if (y >= size || y < 0) { 67 return null; 68 } 69 70 if (unlikely (!(0 <= index < parts.size))) { 71 warning (@"No part at index: $x, $y"); 72 return null; 73 } 74 75 return parts.get (index); 76 } 77 78 public ScaledBackgroundPart get_part (double offset_x, double offset_y, 79 int width, int height) { 80 81 double image_width = part_width * size; 82 double image_height = part_height * size; 83 84 int start_x = (int) ((offset_x / image_width) * size) - 1; 85 int start_y = (int) ((offset_y / image_height) * size) - 1; 86 int stop_x = (int) (((offset_x + width) / image_width) * size) + 2; 87 int stop_y = (int) (((offset_y + height) / image_height) * size) + 2; 88 89 int assembled_width = (int) ((size + 2) * image_width); 90 int assembled_height = (int) ((size + 2) * image_height); 91 image = new ImageSurface (Format.RGB24, assembled_width, assembled_height); 92 Context context = new Context (image); 93 94 int start_offset_x = start_x * part_width; 95 int start_offset_y = start_y * part_height; 96 97 if (start_offset_x > 0) { 98 start_x = -1; 99 start_offset_x = start_x * part_width; 100 } 101 102 if (start_offset_y > 0) { 103 start_y = -1; 104 start_offset_y = start_y * part_height; 105 } 106 107 for (int y = start_y; y < stop_y; y++) { 108 for (int x = start_x; x < stop_x; x++) { 109 ImageSurface? part = get_part_at (x, y); 110 111 if (part != null) { 112 context.save (); 113 114 context.set_source_surface ((!) part, 115 (x - start_x) * part_width, 116 (y - start_y) * part_height); 117 118 context.paint (); 119 context.restore (); 120 } 121 } 122 } 123 124 return new ScaledBackgroundPart (image, scale, 125 start_offset_x, start_offset_y); 126 } 127 } 128 129 } 130