The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

BackgroundImage.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/BackgroundImage.vala.
Cache overview items
1 /* 2 Copyright (C) 2012 2013 2014 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 18 namespace BirdFont { 19 20 public class BackgroundImage { 21 22 public string name = ""; 23 public Gee.ArrayList<BackgroundSelection> selections; 24 25 /** Image position in canvas coordinates. */ 26 public double img_x = 0; 27 public double img_y = 0; 28 29 public double img_scale_x { 30 get { 31 return img_scale_x_size; 32 } 33 34 set { 35 if (value > 0.0001) { 36 img_scale_x_size = value; 37 } 38 } 39 } 40 41 public double img_scale_y { 42 get { 43 return img_scale_y_size; 44 } 45 46 set { 47 if (value > 0.0001) { 48 img_scale_y_size = value; 49 } 50 } 51 } 52 53 private double img_scale_x_size = 1; 54 private double img_scale_y_size = 1; 55 56 public double img_rotation = 0; 57 private double preview_img_rotation = 0; 58 59 private int size = -1; 60 61 public int active_handle = -1; 62 public int selected_handle = -1; 63 64 private ImageSurface? background_image = null; 65 private ImageSurface? original_image = null; 66 67 private string path; 68 69 public bool high_contrast = false; 70 private Gee.ArrayList<TracedPoint> points = new Gee.ArrayList<TracedPoint> (); 71 private Gee.ArrayList<TracedPoint> start_points = new Gee.ArrayList<TracedPoint> (); 72 73 public signal void updated (); 74 75 private ScaledBackgrounds? scaled = null; 76 private ImageSurface? contrast_image = null; 77 78 public double img_offset_x { 79 get { return img_x + Glyph.xc (); } 80 set { img_x = value - Glyph.xc (); } 81 } 82 83 public double img_offset_y { 84 get { return Glyph.yc () - img_y; } 85 set { img_y = Glyph.yc () - value; } 86 } 87 88 public int size_margin { 89 get { 90 if (unlikely (size == -1)) { 91 size = (int) (Math.sqrt (Math.pow (get_img ().get_height (), 2) 92 + Math.pow (get_img ().get_width (), 2)) + 0.5); 93 } 94 95 return size; 96 } 97 } 98 99 public int margin_left { 100 get { 101 return size_margin - get_img ().get_width (); 102 } 103 } 104 105 public int margin_top { 106 get { 107 return size_margin - get_img ().get_height (); 108 } 109 } 110 111 public double img_middle_x { 112 get { return img_x + (size_margin * img_scale_x) / 2; } 113 set { img_x = value - (size_margin * img_scale_x) / 2; } 114 } 115 116 public double img_middle_y { 117 get { return img_y - (size_margin * img_scale_y) / 2; } 118 set { img_y = value + (size_margin * img_scale_y) / 2; } 119 } 120 121 public BackgroundImage (string file_name) { 122 path = file_name; 123 selections = new Gee.ArrayList<BackgroundSelection> (); 124 } 125 126 public BackgroundImage copy () { 127 BackgroundImage bg = new BackgroundImage (path); 128 129 bg.img_x = img_x; 130 bg.img_y = img_y; 131 132 bg.img_scale_x = img_scale_x; 133 bg.img_scale_y = img_scale_y; 134 bg.img_rotation = img_rotation; 135 136 bg.high_contrast = high_contrast; 137 138 foreach (BackgroundSelection b in selections) { 139 bg.selections.add (b); 140 } 141 142 return bg; 143 } 144 145 public ScaledBackgrounds get_scaled_backgrounds () { 146 if (scaled == null) { 147 ImageSurface rotated = rotate ((ImageSurface) get_padded_image ()); 148 scaled = new ScaledBackgrounds (rotated); 149 } 150 151 return (!) scaled; 152 } 153 154 public void add_selection (BackgroundSelection bs) { 155 selections.add (bs); 156 } 157 158 public void set_high_contrast (bool t) { 159 high_contrast = t; 160 } 161 162 public double get_margin_width () { 163 return ((size_margin - get_img ().get_width ()) / 2.0); 164 } 165 166 public double get_margin_height () { 167 return ((size_margin - get_img ().get_height ()) / 2.0); 168 } 169 170 public void set_img_offset (double x, double y) { 171 img_offset_x = x; 172 img_offset_y = y; 173 } 174 175 public void set_position (double coordinate_x, double coordinate_y) { 176 img_x = coordinate_x; 177 img_y = coordinate_y; 178 } 179 180 public ImageSurface get_img () { 181 if (!path.has_suffix (".png")) { 182 create_png (); 183 } 184 185 if (background_image == null) { 186 background_image = new ImageSurface.from_png (path); 187 original_image = new ImageSurface.from_png (path); 188 } 189 190 return (!) background_image; 191 } 192 193 public ImageSurface get_original () { 194 if (!path.has_suffix (".png")) { 195 create_png (); 196 } 197 198 if (background_image == null) { 199 background_image = new ImageSurface.from_png (path); 200 original_image = new ImageSurface.from_png (path); 201 } 202 203 return (!) original_image; 204 } 205 206 public bool is_valid () { 207 FileInfo file_info; 208 File file = File.new_for_path (path); 209 210 if (!file.query_exists ()) { 211 return false; 212 } 213 214 try { 215 file_info = file.query_info ("*", FileQueryInfoFlags.NONE); 216 217 if (file_info.get_size () == 0) { 218 return false; 219 } 220 } catch (GLib.Error e) { 221 warning (e.message); 222 return false; 223 } 224 225 return true; 226 } 227 228 public string get_png_base64 () { 229 try { 230 File file = File.new_for_path (path); 231 FileInfo file_info = file.query_info ("*", FileQueryInfoFlags.NONE); 232 uint8[] buffer = new uint8[file_info.get_size ()]; 233 FileInputStream file_stream; 234 DataInputStream png_stream; 235 236 if (!file.query_exists ()) { 237 warning (@"Can't to save image $path, file does not exist."); 238 return ""; 239 } 240 241 if (is_null (buffer)) { 242 warning (@"Can not allocate a buffer of $(file_info.get_size ()) bytes to store $path."); 243 return ""; 244 } 245 246 file_stream = file.read (); 247 248 png_stream = new DataInputStream (file_stream); 249 png_stream.read (buffer); 250 251 return Base64.encode (buffer); 252 } catch (GLib.Error e) { 253 warning (e.message); 254 } 255 256 return ""; 257 258 } 259 260 public void create_background_folders (Font font) { 261 File dir; 262 263 dir = BirdFont.get_settings_directory (); 264 if (!dir.query_exists ()) { 265 DirUtils.create ((!) dir.get_path (), 0755); 266 } 267 268 dir = font.get_backgrounds_folder (); 269 if (!dir.query_exists ()) { 270 DirUtils.create ((!) dir.get_path (), 0755); 271 } 272 273 dir = get_child (font.get_backgrounds_folder (), "parts"); 274 if (!dir.query_exists ()) { 275 DirUtils.create ((!) dir.get_path (), 0755); 276 } 277 } 278 279 public void copy_if_new (File destination) { 280 if (!destination.query_exists ()) { 281 copy_file (destination); 282 } 283 } 284 285 public void copy_file (File destination) { 286 File source; 287 FileInfo info; 288 289 try { 290 if (destination.query_exists ()) { 291 info = destination.query_info ("standard::*", FileQueryInfoFlags.NONE); 292 if (info.get_file_type () == FileType.DIRECTORY) { 293 warning (@"$((!) destination.get_path ()) is a directory."); 294 return; 295 } 296 } 297 298 if (!((!)destination.get_parent ()).query_exists ()) { 299 warning (@"Directory for file $((!) destination.get_path ()) is not created."); 300 return; 301 } 302 303 if (destination.query_exists ()) { 304 warning (@"Image $((!) destination.get_path ()) is already created."); 305 return; 306 } 307 308 source = File.new_for_path (path); 309 source.copy (destination, FileCopyFlags.NONE); 310 } catch (GLib.Error e) { 311 warning (e.message); 312 } 313 } 314 315 public string get_sha1 () { 316 try { 317 File file = File.new_for_path (path); 318 FileInfo file_info; 319 uint8[] buffer; 320 FileInputStream file_stream; 321 DataInputStream png_stream; 322 323 if (!file.query_exists ()) { 324 warning (@"Can't save $path file does not exist."); 325 return ""; 326 } 327 328 file_info = file.query_info ("*", FileQueryInfoFlags.NONE); 329 330 if (file_info.get_size () == 0) { 331 warning (@"length of image $path is zero"); 332 return ""; 333 } 334 335 buffer = new uint8[file_info.get_size ()]; 336 file_stream = file.read (); 337 png_stream = new DataInputStream (file_stream); 338 339 png_stream.read (buffer); 340 341 return Checksum.compute_for_data (ChecksumType.SHA1, buffer); 342 } catch (GLib.Error e) { 343 warning (e.message); 344 } 345 346 return ""; 347 } 348 349 private void create_png () { 350 string file_name = @"$path.png"; 351 Font font = BirdFont.get_current_font (); 352 File folder = font.get_backgrounds_folder (); 353 File original = File.new_for_path (file_name); 354 File png_image = get_child (folder, @"full_$((!)original.get_basename ())"); 355 bool converted; 356 357 if (png_image.query_exists ()) { 358 path = (!) png_image.get_path (); 359 return; 360 } 361 362 if (is_null (path)) { 363 warning ("Background image path is null."); 364 return; 365 } 366 367 try { 368 folder.make_directory (); 369 } catch (GLib.Error e) { 370 warning (e.message); 371 } 372 373 converted = MainWindow.native_window.convert_to_png (path.dup (), ((!) png_image.get_path ()).dup ()); 374 375 if (!converted) { 376 warning ("Failed to convert image: $(path)"); 377 return; 378 } 379 380 path = (!) png_image.get_path (); 381 } 382 383 private ScaledBackgrounds get_image () { 384 return get_scaled_backgrounds (); 385 } 386 387 public void start_rotation_preview () { 388 preview_img_rotation = img_rotation; 389 } 390 391 public void preview_img_rotation_from_coordinate (double x, double y, double view_zoom) { 392 double rotation; 393 ScaledBackgrounds backgrounds; 394 ScaledBackground backgound; 395 396 if (get_img_rotation_from_coordinate (x, y, out rotation)) { 397 backgrounds = get_scaled_backgrounds (); 398 backgound = backgrounds.get_image (view_zoom * img_scale_x); // FIXME: y 399 400 img_rotation = rotation; 401 402 if (!high_contrast) { 403 backgound.rotate (rotation - preview_img_rotation); 404 } else { 405 contrast_image = null; 406 } 407 } 408 } 409 410 public void set_img_rotation_from_coordinate (double x, double y) { 411 double rotation; 412 if (get_img_rotation_from_coordinate (x, y, out rotation)) { 413 img_rotation = rotation; 414 Task task = new Task (cache_scaled_image); // cache all sizes 415 MainWindow.run_blocking_task (task); 416 } 417 } 418 419 void cache_scaled_image () { 420 scaled = null; 421 contrast_image = null; 422 get_image (); 423 } 424 425 public bool get_img_rotation_from_coordinate (double x, double y, out double rotation) { 426 double bcx, bcy; 427 double a, b, c, length; 428 429 rotation = 0; 430 431 bcx = img_middle_x; 432 bcy = img_middle_y; 433 434 a = bcx - x; 435 b = bcy - y; 436 c = a * a + b * b; 437 438 if (c == 0) { 439 return false; 440 } 441 442 length = sqrt (fabs (c)); 443 444 if (c < 0) { 445 length = -length; 446 } 447 448 rotation = (y > bcy) ? acos (a / length) + PI : -acos (a / length) + PI; 449 return true; 450 } 451 452 public void set_img_scale (double xs, double ys) { 453 img_scale_x = xs; 454 img_scale_y = ys; 455 } 456 457 public void reset_scale (Glyph g) { 458 double w, h; 459 460 w = g.get_width (); 461 h = g.get_height (); 462 463 img_scale_x = 1; 464 img_scale_y = 1; 465 466 img_offset_x = g.get_line ("left").pos; 467 img_offset_y = g.get_line ("top").pos; 468 } 469 470 public void draw (Context cr, WidgetAllocation allocation, 471 double view_offset_x, double view_offset_y, double view_zoom) { 472 473 double scale_x, scale_y; 474 double image_scale_x, image_scale_y; 475 476 ScaledBackgrounds backgrounds = get_scaled_backgrounds (); 477 478 if (unlikely (get_img ().status () != Cairo.Status.SUCCESS)) { 479 warning (@"Background image is invalid. (\"$path\")\n"); 480 MainWindow.get_current_glyph ().set_background_visible (false); 481 return; 482 } 483 484 image_scale_x = img_scale_x; 485 image_scale_y = img_scale_y; 486 487 ImageSurface scaled_image; 488 Context scaled_context; 489 490 if (!high_contrast) { 491 ScaledBackground scaled; 492 ScaledBackgroundPart part; 493 494 scaled = backgrounds.get_image (view_zoom * img_scale_x); // FIXME: y 495 496 double part_offset_x = img_offset_x - view_offset_x; 497 part_offset_x /= img_scale_x / scaled.get_scale (); 498 part_offset_x = -part_offset_x; 499 500 double part_offset_y = img_offset_y - view_offset_y; 501 part_offset_y /= img_scale_y / scaled.get_scale (); 502 part_offset_y = -part_offset_y; 503 504 double part_allocation_width; 505 double part_allocation_height; 506 507 part_allocation_height = allocation.height; 508 part_allocation_height /= view_zoom; 509 part_allocation_height /= image_scale_x; 510 511 part_allocation_width = allocation.width; 512 part_allocation_width /= view_zoom; 513 part_allocation_width /= image_scale_y; 514 515 part = scaled.get_part (part_offset_x, part_offset_y, 516 (int) (part_allocation_width), (int) (part_allocation_height)); 517 518 scale_x = view_zoom * image_scale_x; 519 scale_y = view_zoom * image_scale_y; 520 521 scale_x /= part.get_scale (); 522 scale_y /= part.get_scale (); 523 524 scaled_image = new ImageSurface (Format.ARGB32, allocation.width, allocation.height); 525 526 scaled_context = new Context (scaled_image); 527 528 scaled_context.scale (scale_x, scale_y); 529 530 double scaled_x = part.offset_x; 531 double scaled_y = part.offset_y; 532 533 scaled_x += view_zoom * (img_offset_x / scale_x - view_offset_x / scale_x); 534 scaled_y += view_zoom * (img_offset_y / scale_y - view_offset_y / scale_y); 535 536 scaled_context.set_source_surface (part.get_image (), scaled_x, scaled_y); 537 scaled_context.paint (); 538 } else { 539 ImageSurface contrast = get_contrast_image (); 540 541 image_scale_x = img_scale_x * ((double) size_margin / contrast.get_width ()); 542 image_scale_y = img_scale_y * ((double) size_margin / contrast.get_height ()); 543 544 scaled_image = new ImageSurface (Format.ARGB32, allocation.width, allocation.height); 545 Context contrast_context = new Context (scaled_image); 546 contrast_context.save (); 547 548 contrast_context.set_source_rgba (1, 1, 1, 1); 549 contrast_context.rectangle (0, 0, allocation.width, allocation.height); 550 contrast_context.fill (); 551 552 // scale both canvas and image at the same time 553 scale_x = view_zoom * image_scale_x; 554 scale_y = view_zoom * image_scale_y; 555 556 contrast_context.scale (scale_x, scale_y); 557 contrast_context.translate (-view_offset_x / image_scale_x, -view_offset_y / image_scale_y); 558 559 contrast_context.set_source_surface (contrast, img_offset_x / image_scale_x, img_offset_y / image_scale_y); 560 561 contrast_context.paint (); 562 contrast_context.restore (); 563 } 564 565 // add it 566 cr.save (); 567 cr.set_source_surface (scaled_image, 0, 0); 568 cr.paint (); 569 cr.restore (); 570 } 571 572 public Surface get_padded_image () { 573 double x, y; 574 double iw, ih; 575 int h, w; 576 double oy, ox; 577 578 Surface o; 579 Surface sg; 580 Context cg; 581 582 double wc, hc; 583 584 o = get_original (); 585 586 // add margin 587 sg = new Surface.similar (o, o.get_content (), size_margin, size_margin); 588 cg = new Context (sg); 589 590 wc = get_margin_width (); 591 hc = get_margin_height (); 592 593 Theme.color (cg, "Background 1"); 594 cg.rectangle (0, 0, size_margin, size_margin); 595 cg.fill (); 596 597 cg.set_source_surface (get_img (), wc, hc); 598 cg.paint (); 599 600 x = Glyph.reverse_path_coordinate_x (img_offset_x); 601 y = Glyph.reverse_path_coordinate_y (img_offset_y); 602 603 ih = (int) get_img ().get_height (); 604 iw = (int) get_img ().get_width (); 605 606 w = (int) iw; 607 h = (int) ih; 608 609 oy = size_margin; 610 ox = size_margin; 611 612 return sg; 613 } 614 615 private ImageSurface rotate (ImageSurface padded_image) { 616 return rotate_image (padded_image, img_rotation); 617 } 618 619 public static ImageSurface rotate_image (ImageSurface padded_image, double angle) { 620 ImageSurface s; 621 Context c; 622 623 int w = padded_image.get_width (); 624 int h = padded_image.get_height (); 625 626 s = new ImageSurface (Format.ARGB32, w, h); 627 c = new Context (s); 628 629 c.save (); 630 631 c.translate (w * 0.5, h * 0.5); 632 c.rotate (angle); 633 c.translate (-w * 0.5, -h * 0.5); 634 635 c.set_source_surface (padded_image, 0, 0); 636 c.paint (); 637 c.restore (); 638 639 return s; 640 } 641 642 public void handler_release (double nx, double ny) { 643 selected_handle = 0; 644 handler_move (nx, ny); 645 } 646 647 public void handler_press (double nx, double ny) { 648 if (is_over_rotate (nx, ny)) { 649 selected_handle = 2; 650 } else if (is_over_resize (nx, ny)) { 651 selected_handle = 1; 652 } else { 653 selected_handle = 0; 654 } 655 } 656 657 bool is_over_rotate (double nx, double ny) { 658 double x, y, d; 659 660 x = Glyph.reverse_path_coordinate_x (img_middle_x); 661 y = Glyph.reverse_path_coordinate_y (img_middle_y); 662 663 x += cos (img_rotation) * 75; 664 y += sin (img_rotation) * 75; 665 666 d = Path.distance (x, nx, y, ny); 667 668 return d < 15 * MainWindow.units; 669 } 670 671 bool is_over_resize (double nx, double ny) { 672 double x, y, size; 673 bool inx, iny; 674 675 size = 12 * MainWindow.units; 676 677 x = img_middle_x - (img_scale_x * get_img ().get_width () / 2); 678 y = img_middle_y - (img_scale_y * get_img ().get_height () / 2); 679 680 x = Glyph.reverse_path_coordinate_x (x); 681 y = Glyph.reverse_path_coordinate_y (y); 682 683 inx = x - size <= nx <= x + size; 684 iny = y - size <= ny <= y + size; 685 686 return inx && iny; 687 } 688 689 public void handler_move (double nx, double ny) { 690 int prev_handle = active_handle; 691 692 if (is_over_rotate (nx, ny)) { 693 active_handle = 2; 694 } else if (is_over_resize (nx, ny)) { 695 active_handle = 1; 696 } else { 697 active_handle = 0; 698 } 699 700 if (prev_handle != active_handle) { 701 GlyphCanvas.redraw (); 702 } 703 } 704 705 public void draw_handle (Context cr, Glyph g) { 706 draw_resize_handle (cr, g); 707 draw_rotate_handle (cr, g); 708 } 709 710 public void draw_resize_handle (Context cr, Glyph g) { 711 double x, y; 712 cr.save (); 713 714 Theme.color (cr, "Menu Background"); 715 716 x = img_middle_x - (img_scale_x * get_img ().get_width () / 2); 717 y = img_middle_y - (img_scale_y * get_img ().get_height () / 2); 718 719 x = Glyph.reverse_path_coordinate_x (x); 720 y = Glyph.reverse_path_coordinate_y (y); 721 722 draw_handle_triangle (x, y, cr, g, 6); 723 724 cr.restore (); 725 } 726 727 public void draw_rotate_handle (Context cr, Glyph g) { 728 double x, y, hx, hy, x2, y2; 729 730 double ivz = 1.0 / (g.view_zoom); 731 732 cr.save (); 733 734 cr.scale (g.view_zoom, g.view_zoom); 735 736 if (selected_handle == 2) Theme.color (cr, "Highlighted 1"); 737 else if (active_handle == 2) Theme.color (cr, "Default Background"); 738 else Theme.color (cr, "Menu Background"); 739 740 x = img_offset_x - g.view_offset_x + (size_margin / 2) * img_scale_x; 741 y = img_offset_y - g.view_offset_y + (size_margin / 2) * img_scale_y; 742 743 cr.rectangle (x, y, 5 * ivz, 5 * ivz); 744 cr.fill (); 745 746 hx = cos (img_rotation) * 75 * ivz; 747 hy = sin (img_rotation) * 75 * ivz; 748 749 x2 = x + hx; 750 y2 = y + hy; 751 752 cr.rectangle (x2, y2, 5 * ivz, 5 * ivz); 753 cr.fill (); 754 755 cr.set_line_width (ivz); 756 cr.move_to (x + 2.5 * ivz, y + 2.5 * ivz); 757 cr.line_to (x2 + 2.5 * ivz, y2 + 2.5 * ivz); 758 cr.stroke (); 759 760 cr.restore (); 761 } 762 763 void draw_handle_triangle (double x, double y, Context cr, Glyph g, int direction, double s = 1) 764 requires (0 < direction < 8) 765 { 766 double ivz = 1.0 / (g.view_zoom); 767 double size; 768 769 cr.save (); 770 cr.set_line_width (ivz); 771 772 if (selected_handle == 1) Theme.color (cr, "Highlighted 1"); 773 else if (active_handle == 1) Theme.color (cr, "Default Background"); 774 else Theme.color (cr, "Menu Background"); 775 776 size = (8) * s; 777 778 cr.scale (1, 1); 779 cr.new_path (); 780 781 // up + left 782 if (direction == 1) { 783 cr.move_to (x - size, y - size); 784 cr.line_to (x + size, y - size); 785 cr.line_to (x - size, y + size); 786 } 787 788 if (direction == 6) { 789 cr.move_to (x + size, y + size); 790 cr.line_to (x - size, y + size); 791 cr.line_to (x - size, y - size); 792 } 793 794 cr.close_path(); 795 cr.fill (); 796 797 cr.restore (); 798 799 } 800 801 public void update_background () { 802 background_image = null; 803 contrast_image = null; 804 805 GlyphCanvas.redraw (); 806 updated (); 807 } 808 809 ImageSurface get_contrast_image () { 810 if (contrast_image == null) { 811 contrast_image = get_contrast_image_surface (); 812 } 813 814 return (!) contrast_image; 815 } 816 817 ImageSurface get_contrast_image_surface () { 818 ImageSurface s; 819 Context c; 820 821 ImageSurface sg; 822 int scaled_width; 823 824 unowned uchar[] pix_buff; 825 int i, len; 826 double thres; 827 int stride; 828 829 ImageSurface img; 830 ImageSurface ns; 831 832 double trace_resolution = DrawingTools.auto_trace_resolution.get_value (); 833 double threshold = DrawingTools.background_threshold.get_value (); 834 835 thres = (threshold - 0.5) * 255; 836 837 scaled_width = (int) (600 * trace_resolution); 838 839 s = new ImageSurface (Format.RGB24, scaled_width, scaled_width); 840 sg = (ImageSurface) get_padded_image (); 841 sg = rotate (sg); 842 c = new Context (s); 843 844 c.save (); 845 Theme.color (c, "Background 1"); 846 c.rectangle (0, 0, scaled_width, scaled_width); 847 c.fill (); 848 849 c.translate (scaled_width * 0.5, scaled_width * 0.5); 850 c.rotate (img_rotation); 851 c.translate (-scaled_width * 0.5, -scaled_width * 0.5); 852 853 c.scale ((double) scaled_width / sg.get_width (), (double) scaled_width / sg.get_height ()); 854 855 c.set_source_surface (sg, 0, 0); 856 c.paint (); 857 c.restore (); 858 859 img = (ImageSurface) s; 860 pix_buff = img.get_data (); 861 862 len = s.get_height () * s.get_stride (); 863 864 uint8* outline_img = new uint8[len]; 865 866 for (i = 0; i < len - 4; i += 4) { 867 uint8 o = (uint8) ((pix_buff[i] + pix_buff[i + 1] + pix_buff[i + 2]) / 3.0); 868 uint8 bw = (o < thres) ? 0 : 255; 869 outline_img[i] = bw; 870 outline_img[i + 1] = bw; 871 outline_img[i + 2] = bw; 872 outline_img[i + 3] = bw; 873 } 874 875 // fill blur with black 876 stride = s.get_stride (); 877 for (int m = 0; m < 2; m++) { 878 i = stride + 4; 879 while (i < len - 4 - stride) { 880 if ((outline_img[i] == 255 && outline_img[i + 4] == 0 881 && outline_img[i + stride] == 0 && outline_img[i + stride + 4] == 255) 882 || (outline_img[i] == 0 && outline_img[i + 4] == 255 883 && outline_img[i + stride] == 255 && outline_img[i + stride + 4] == 0)) { 884 outline_img[i] = 0; 885 outline_img[i + 4] = 0; 886 outline_img[i + stride] = 0; 887 outline_img[i + stride + 4] = 0; 888 } 889 890 if (outline_img[i] == 255 && outline_img[i + 4] == 0 && outline_img[i + 8] == 255 891 || outline_img[i] == 0 && outline_img[i + 4] == 255 && outline_img[i + 8] == 0) { 892 outline_img[i] = 0; 893 outline_img[i + 4] = 0; 894 outline_img[i + stride] = 0; 895 outline_img[i + stride + 4] = 0; 896 } 897 i += 4; 898 } 899 } 900 901 ns = new ImageSurface.for_data ((uchar[])outline_img, s.get_format (), s.get_width (), s.get_height (), s.get_stride ()); 902 background_image = null; 903 original_image = null; 904 905 return (ImageSurface) ns; 906 } 907 908 public PathList autotrace () { 909 ImageSurface img; 910 int len, w, h, i, s; 911 Path p; 912 PathList pl; 913 uint8* outline_img; 914 915 ImageSurface scaled_image; 916 double scale; 917 918 p = new Path (); 919 pl = new PathList (); 920 921 get_img (); 922 923 if (background_image == null) { 924 return pl; 925 } 926 927 img = get_contrast_image (); 928 929 w = img.get_width(); 930 h = img.get_height(); 931 932 if (unlikely (img.status () != Cairo.Status.SUCCESS)) { 933 warning ("Error"); 934 return pl; 935 } 936 937 if (img.get_format () != Format.RGB24) { 938 warning ("Wrong format"); 939 return pl; 940 } 941 942 scaled_image = img; 943 944 img = (ImageSurface) scaled_image; 945 w = img.get_width (); 946 h = img.get_height (); 947 s = img.get_stride (); 948 len = s * h; 949 950 outline_img = (uint8*) img.get_data (); 951 952 start_points = new Gee.ArrayList<TracedPoint> (); 953 points = new Gee.ArrayList<TracedPoint> (); 954 955 int direction_vertical = 4; 956 int direction_horizontal = s; // FIXME: SET AT FIND START 957 int last_move = 0; 958 int pp = 0; 959 960 scale = 1; 961 i = 0; 962 while ((i = find_start_point (outline_img, len, s, i)) != -1) { 963 pp = 0; 964 965 while (4 + s <= i < len - 4 - s) { 966 pp++; 967 if (is_traced (i)) { 968 Path np = generate_path (outline_img, s, w, h, len); 969 970 if (np.points.size >= 3) { 971 if (Path.is_counter (pl, np)) { 972 np.force_direction (Direction.COUNTER_CLOCKWISE); 973 } else { 974 np.force_direction (Direction.CLOCKWISE); 975 } 976 977 pl.add (np); 978 } 979 980 break; 981 } 982 983 if (outline_img[i] == 255 && outline_img[i + 4] == 255 984 && outline_img[i + s] == 255 && outline_img[i + s + 4] == 255) { 985 warning ("Lost path"); 986 Path np = generate_path (outline_img, s, w, h, len); 987 pl.add (np); 988 break; 989 } 990 991 if (outline_img[i] == 0 && outline_img[i + 4] == 0 992 && outline_img[i + s] == 0 && outline_img[i + s + 4] == 0) { 993 warning ("Lost path"); 994 Path np = generate_path (outline_img, s, w, h, len); 995 pl.add (np); 996 break; 997 } 998 999 if (outline_img[i] == 0 && outline_img[i + 4] == 255 1000 && outline_img[i + s] == 255 && outline_img[i + s + 4] == 255) { 1001 points.add (new TracedPoint (i)); 1002 1003 if (last_move == direction_horizontal) { 1004 direction_vertical = -s; 1005 i += direction_vertical; 1006 last_move = direction_vertical; 1007 } else { 1008 direction_horizontal = -4; 1009 i += direction_horizontal; 1010 last_move = direction_horizontal; 1011 } 1012 1013 } else if (outline_img[i] == 0 && outline_img[i + 4] == 255 1014 && outline_img[i + s] == 255 && outline_img[i + s + 4] == 255) { 1015 points.add (new TracedPoint (i)); 1016 1017 if (last_move == direction_horizontal) { 1018 direction_vertical = -s; 1019 i += direction_vertical; 1020 last_move = direction_vertical; 1021 } else { 1022 direction_horizontal = -4; 1023 i += direction_horizontal; 1024 last_move = direction_horizontal; 1025 } 1026 1027 } else if (outline_img[i] == 255 && outline_img[i + 4] == 0 1028 && outline_img[i + s] == 255 && outline_img[i + s + 4] == 255) { 1029 points.add (new TracedPoint (i)); 1030 1031 if (last_move == direction_horizontal) { 1032 direction_vertical = -s; 1033 i += direction_vertical; 1034 last_move = direction_vertical; 1035 } else { 1036 direction_horizontal = 4; 1037 i += direction_horizontal; 1038 last_move = direction_horizontal; 1039 } 1040 1041 } else if (outline_img[i] == 0 && outline_img[i + 4] == 0 1042 && outline_img[i + s] == 255 && outline_img[i + s + 4] == 0) { 1043 points.add (new TracedPoint (i)); 1044 1045 if (last_move == direction_horizontal) { 1046 direction_vertical = s; 1047 i += direction_vertical; 1048 last_move = direction_vertical; 1049 } else { 1050 direction_horizontal = -4; 1051 i += direction_horizontal; 1052 last_move = direction_horizontal; 1053 } 1054 1055 } else if (outline_img[i] == 255 && outline_img[i + 4] == 255 1056 && outline_img[i + s] == 255 && outline_img[i + s + 4] == 0) { 1057 points.add (new TracedPoint (i)); 1058 1059 if (last_move == direction_horizontal) { 1060 direction_vertical = s; 1061 i += direction_vertical; 1062 last_move = direction_vertical; 1063 } else { 1064 direction_horizontal = 4; 1065 i += direction_horizontal; 1066 last_move = direction_horizontal; 1067 } 1068 1069 } else if (outline_img[i] == 255 && outline_img[i + 4] == 255 1070 && outline_img[i + s] == 0 && outline_img[i + s + 4] == 255) { 1071 points.add (new TracedPoint (i)); 1072 1073 if (last_move == direction_horizontal) { 1074 direction_vertical = s; 1075 i += direction_vertical; 1076 last_move = direction_vertical; 1077 } else { 1078 direction_horizontal = -4; 1079 i += direction_horizontal; 1080 last_move = direction_horizontal; 1081 } 1082 1083 } else if (outline_img[i] == 255 && outline_img[i + 4] == 0 1084 && outline_img[i + s] == 0 && outline_img[i + s + 4] == 0) { 1085 points.add (new TracedPoint (i)); 1086 1087 if (last_move == direction_horizontal) { 1088 direction_vertical = -s; 1089 i += direction_vertical; 1090 last_move = direction_vertical; 1091 } else { 1092 direction_horizontal = -4; 1093 i += direction_horizontal; 1094 last_move = direction_horizontal; 1095 } 1096 } else if (outline_img[i] == 0 && outline_img[i + 4] == 255 1097 && outline_img[i + s] == 0 && outline_img[i + s + 4] == 0) { 1098 points.add (new TracedPoint (i)); 1099 1100 if (last_move == direction_horizontal) { 1101 direction_vertical = -s; 1102 i += direction_vertical; 1103 last_move = direction_vertical; 1104 } else { 1105 direction_horizontal = 4; 1106 i += direction_horizontal; 1107 last_move = direction_horizontal; 1108 } 1109 } else if (outline_img[i] == 0 && outline_img[i + 4] == 0 1110 && outline_img[i + s] == 0 && outline_img[i + s + 4] == 255) { 1111 points.add (new TracedPoint (i)); 1112 1113 if (last_move == direction_horizontal) { 1114 direction_vertical = s; 1115 i += direction_vertical; 1116 last_move = direction_vertical; 1117 } else { 1118 direction_horizontal = 4; 1119 i += direction_horizontal; 1120 last_move = direction_horizontal; 1121 } 1122 } else if (outline_img[i] == 255 && outline_img[i + 4] == 255 1123 && outline_img[i + s] == 0 && outline_img[i + s + 4] == 255) { 1124 points.add (new TracedPoint (i)); 1125 1126 if (last_move == direction_horizontal) { 1127 direction_vertical = s; 1128 i += direction_vertical; 1129 last_move = direction_vertical; 1130 } else { 1131 direction_horizontal = -4; 1132 i += direction_horizontal; 1133 last_move = direction_horizontal; 1134 } 1135 } else if (outline_img[i] == 255 && outline_img[i + 4] == 255 1136 && outline_img[i + s] == 0 && outline_img[i + s + 4] == 0) { 1137 points.add (new TracedPoint (i)); 1138 1139 i += direction_horizontal; 1140 last_move = direction_horizontal; 1141 1142 } else if (outline_img[i] == 0 && outline_img[i + 4] == 0 1143 && outline_img[i + s] == 255 && outline_img[i + s + 4] == 255) { 1144 points.add (new TracedPoint (i)); 1145 1146 i += direction_horizontal; 1147 last_move = direction_horizontal; 1148 } else if (outline_img[i] == 255 && outline_img[i + 4] == 0 1149 && outline_img[i + s] == 255 && outline_img[i + s + 4] == 0) { 1150 points.add (new TracedPoint (i)); 1151 1152 i += direction_vertical; 1153 last_move = direction_vertical; 1154 } else if (outline_img[i] == 0 && outline_img[i + 4] == 255 1155 && outline_img[i + s] == 0 && outline_img[i + s + 4] == 255) { 1156 points.add (new TracedPoint (i)); 1157 1158 i += direction_vertical; 1159 last_move = direction_vertical; 1160 } else if ((outline_img[i] == 255 && outline_img[i + 4] == 0 1161 && outline_img[i + s] == 0 && outline_img[i + s + 4] == 255) 1162 || (outline_img[i] == 0 && outline_img[i + 4] == 255 1163 && outline_img[i + s] == 255 && outline_img[i + s + 4] == 0)) { 1164 points.add (new TracedPoint (i)); 1165 1166 warning ("Bad edge"); 1167 i += last_move; 1168 1169 } else { 1170 points.add (new TracedPoint (i)); 1171 warning (@"No direction\n $(outline_img[i]) $(outline_img[i + 4])\n $(outline_img[i + s]) $(outline_img[i + s + 4])"); 1172 i += 4; 1173 } 1174 } 1175 } 1176 1177 start_points.clear (); 1178 points.clear (); 1179 1180 return pl; 1181 } 1182 1183 int find_start_point (uint8* outline_img, int len, int s, int start_index) { 1184 // find start point 1185 int i = start_index; 1186 1187 if (i < s + 4) { 1188 i = s + 4; 1189 } 1190 1191 while (i < len - 4 - s) { 1192 if (outline_img[i] == 0 && outline_img[i + 4] == 255 1193 && outline_img[i + s] == 255 && outline_img[i + s + 4] == 255 1194 && !has_start_point (i)) { 1195 return i; 1196 } else if (outline_img[i] == 255 && outline_img[i + 4] == 0 1197 && outline_img[i + s] == 255 && outline_img[i + s + 4] == 255 1198 && !has_start_point (i)) { 1199 return i; 1200 } else if (outline_img[i] == 255 && outline_img[i + 4] == 255 1201 && outline_img[i + s] == 0 && outline_img[i + s + 4] == 255 1202 && !has_start_point (i)) { 1203 return i; 1204 } else if (outline_img[i] == 255 && outline_img[i + 4] == 255 1205 && outline_img[i + s] == 255 && outline_img[i + s + 4] == 0 1206 && !has_start_point (i)) { 1207 return i; 1208 } else if (outline_img[i] == 255 && outline_img[i + 4] == 0 1209 && outline_img[i + s] == 0 && outline_img[i + s + 4] == 0 1210 && !has_start_point (i)) { 1211 return i; 1212 } else if (outline_img[i] == 0 && outline_img[i + 4] == 255 1213 && outline_img[i + s] == 0 && outline_img[i + s + 4] == 0 1214 && !has_start_point (i)) { 1215 return i; 1216 } else if (outline_img[i] == 0 && outline_img[i + 4] == 0 1217 && outline_img[i + s] == 255 && outline_img[i + s + 4] == 0 1218 && !has_start_point (i)) { 1219 return i; 1220 } else if (outline_img[i] == 0 && outline_img[i + 4] == 0 1221 && outline_img[i + s] == 0 && outline_img[i + s + 4] == 255 1222 && !has_start_point (i)) { 1223 return i; 1224 } 1225 1226 i +=4; 1227 } 1228 1229 return -1; 1230 } 1231 1232 void find_corner (Path path, int point_index, int end, double points_per_unit, ref double x, ref double y) { 1233 TracedPoint tp0; 1234 double sx = 0; 1235 double sy = 0; 1236 double d = 0; 1237 double mind = double.MAX; 1238 int index = 0; 1239 int pi; 1240 EditPoint ep0, ep1; 1241 double dx, dy; 1242 1243 pi = point_index - 1; 1244 if (pi < 0) { 1245 pi += path.points.size; 1246 } 1247 ep0 = path.points.get (pi); 1248 1249 pi = point_index + 1; 1250 pi %= path.points.size; 1251 ep1 = path.points.get (pi); 1252 1253 Path.find_intersection_handle (ep0.get_left_handle (), ep1.get_right_handle (), out sx, out sy); 1254 1255 dx = x - ep0.x; 1256 dy = y - ep0.y; 1257 1258 sx += 3 * dx; 1259 sy += 3 * dy; 1260 1261 dx = x - ep1.x; 1262 dy = y - ep1.y; 1263 1264 sx += 3 * dx; 1265 sy += 3 * dy; 1266 1267 end += (int) (points_per_unit / 2.0); 1268 for (int i = 0; i < 2 * points_per_unit; i++) { 1269 index = end - i; 1270 1271 if (index < 0) { 1272 index += points.size; 1273 } else { 1274 index %= points.size; 1275 } 1276 1277 tp0 = points.get (index); 1278 1279 d = Path.distance (tp0.x, sx, tp0.y, sy); 1280 if (d < mind) { 1281 mind = d; 1282 x = tp0.x; 1283 y = tp0.y; 1284 } 1285 } 1286 } 1287 1288 Path generate_path (uint8* outline_img, int stride, int w, int h, int length) { 1289 double x, y, np; 1290 int i, index; 1291 double sumx, sumy, points_per_unit; 1292 Path path = new Path (); 1293 Gee.ArrayList<int?> sp = new Gee.ArrayList<int?> (); 1294 double corner; 1295 Gee.ArrayList<TracedPoint> traced = new Gee.ArrayList<TracedPoint> (); 1296 Gee.ArrayList<EditPoint> corners = new Gee.ArrayList<EditPoint> (); 1297 EditPoint ep; 1298 EditPointHandle r, l; 1299 double la, a; 1300 PointSelection ps; 1301 double image_scale_x; 1302 double image_scale_y; 1303 TracedPoint average_point; 1304 int pi; 1305 ImageSurface img; 1306 double simplification = DrawingTools.auto_trace_simplify.get_value (); 1307 1308 img = get_contrast_image (); 1309 1310 image_scale_x = ((double) size_margin / img.get_width ()); 1311 image_scale_y = ((double) size_margin / img.get_height ()); 1312 1313 foreach (TracedPoint p in points) { 1314 start_points.add (p); 1315 } 1316 1317 sumx = 0; 1318 sumy = 0; 1319 np = 0; 1320 1321 points_per_unit = 9; 1322 corner = PI / 3.5; 1323 1324 i = 0; 1325 foreach (TracedPoint p in points) { 1326 index = p.index; 1327 x = -w * img_scale_x / 2 + (((index + 4) % stride) / 4) * img_scale_x; 1328 y = h * img_scale_y / 2 + -((index - x * 4) / stride) * img_scale_y; 1329 1330 x *= image_scale_x; 1331 y *= image_scale_y; 1332 1333 x += img_middle_x; 1334 y += img_middle_y; 1335 1336 p.x = x; 1337 p.y = y; 1338 1339 np++; 1340 1341 sumx += x; 1342 sumy += y; 1343 1344 if (np >= points_per_unit) { 1345 average_point = new TracedPoint (-1); 1346 average_point.x = sumx / np; 1347 average_point.y = sumy / np; 1348 traced.add (average_point); 1349 1350 sp.add (i); 1351 1352 np = 0; 1353 sumx = 0; 1354 sumy = 0; 1355 } 1356 1357 i++; 1358 } 1359 1360 if (np != 0) { 1361 average_point = new TracedPoint (-1); 1362 average_point.x = sumx / np; 1363 average_point.y = sumy / np; 1364 traced.add (average_point); 1365 sp.add (i); 1366 } 1367 1368 foreach (TracedPoint avgp in traced) { 1369 ep = new EditPoint (avgp.x, avgp.y); 1370 1371 path.points.add (ep); 1372 1373 if (DrawingTools.point_type == PointType.CUBIC) { 1374 ep.type = PointType.CUBIC; 1375 ep.get_right_handle ().type = PointType.LINE_CUBIC; 1376 ep.get_left_handle ().type = PointType.LINE_CUBIC; 1377 } else { 1378 ep.type = PointType.DOUBLE_CURVE; 1379 ep.get_right_handle ().type = PointType.LINE_DOUBLE_CURVE; 1380 ep.get_left_handle ().type = PointType.LINE_DOUBLE_CURVE; 1381 } 1382 } 1383 1384 path.close (); 1385 path.create_list (); 1386 path.recalculate_linear_handles (); 1387 1388 // Find corners 1389 pi = 0; 1390 for (i = 1; i < sp.size; i += 2) { 1391 return_val_if_fail (0 <= i < path.points.size, path); 1392 ep = path.points.get (i); 1393 1394 pi = i + 2; 1395 pi %= path.points.size; 1396 return_val_if_fail (0 <= pi < path.points.size, path); 1397 l = path.points.get (pi).get_left_handle (); 1398 1399 pi = i - 2; 1400 if (pi < 0) { 1401 pi += path.points.size; 1402 } 1403 return_val_if_fail (0 <= pi < path.points.size, path); 1404 r = path.points.get (pi).get_right_handle (); 1405 1406 la = l.angle - PI; 1407 1408 while (la < 0) { 1409 la += 2 * PI; 1410 } 1411 1412 if (r.angle > (2.0 / 3.0) * PI && la < PI / 2) { 1413 la += 2 * PI; 1414 } else if (la > (2.0 / 3.0) * PI && r.angle < PI / 2) { 1415 la -= 2 * PI; 1416 } 1417 1418 a = r.angle - la; 1419 1420 if (fabs (a) > corner) { // corner 1421 ep.set_tie_handle (false); 1422 find_corner (path, i, (!) sp.get (i), points_per_unit, ref ep.x, ref ep.y); 1423 corners.add (ep); 1424 } else { 1425 ep.set_tie_handle (true); 1426 } 1427 } 1428 1429 path.recalculate_linear_handles (); 1430 path.remove_points_on_points (); 1431 path.create_list (); 1432 foreach (EditPoint e in path.points) { 1433 if (e.tie_handles) { 1434 e.process_tied_handle (); 1435 } 1436 } 1437 1438 if (simplification > 0.01) { 1439 for (i = 0; i < path.points.size; i++) { 1440 ep = path.points.get (i); 1441 ps = new PointSelection (ep, path); 1442 if (corners.index_of (ep) == -1) { 1443 PenTool.remove_point_simplify (ps, simplification); 1444 } 1445 } 1446 } 1447 1448 for (i = 0; i < path.points.size; i++) { 1449 ep = path.points.get (i); 1450 ps = new PointSelection (ep, path); 1451 if (corners.index_of (ep) == -1) { 1452 ep.set_selected (true); 1453 } 1454 } 1455 1456 path = PenTool.simplify (path, true, simplification); 1457 points.clear (); 1458 path.update_region_boundaries (); 1459 1460 return path; 1461 } 1462 1463 bool has_start_point (int i) { 1464 foreach (TracedPoint p in start_points) { 1465 if (p.index == i) { 1466 return true; 1467 } 1468 } 1469 return false; 1470 } 1471 1472 bool is_traced (int i) { 1473 foreach (TracedPoint p in points) { 1474 if (p.index == i) { 1475 return true; 1476 } 1477 } 1478 return false; 1479 } 1480 1481 public void center_in_glyph (Glyph? glyph = null) { 1482 Glyph g; 1483 Font f = BirdFont.get_current_font (); 1484 1485 if (glyph != null) { 1486 g = (!) glyph; 1487 } else { 1488 g = MainWindow.get_current_glyph (); 1489 } 1490 1491 img_middle_x = g.left_limit + (g.right_limit - g.left_limit) / 2; 1492 img_middle_y = f.bottom_position + (f.top_position - f.bottom_position) / 2; 1493 } 1494 1495 class TracedPoint { 1496 public int index; 1497 1498 public double x = 0; 1499 public double y = 0; 1500 1501 public TracedPoint (int index) { 1502 this.index = index; 1503 } 1504 } 1505 } 1506 1507 } 1508