1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2011 Authors:
4  *   Nicolas Dufour <nicoduf@yahoo.fr>
5  *
6  * Released under GNU GPL v2+, read the file 'COPYING' for more information.
7  */
8 
9 #include "2geom/transforms.h"
10 #include "extension/effect.h"
11 #include "extension/system.h"
12 
13 #include "crop.h"
14 #include "selection-chemistry.h"
15 #include "object/sp-item.h"
16 #include "object/sp-item-transform.h"
17 #include <Magick++.h>
18 
19 namespace Inkscape {
20 namespace Extension {
21 namespace Internal {
22 namespace Bitmap {
23 
24 void
applyEffect(Magick::Image * image)25 Crop::applyEffect(Magick::Image *image) {
26     int width = image->baseColumns() - (_left + _right);
27     int height = image->baseRows() - (_top + _bottom);
28     if (width > 0 and height > 0) {
29         image->crop(Magick::Geometry(width, height, _left, _top, false, false));
30         image->page("+0+0");
31     }
32 }
33 
34 void
postEffect(Magick::Image * image,SPItem * item)35 Crop::postEffect(Magick::Image *image, SPItem *item) {
36 
37     // Scale bbox
38     Geom::Scale scale (0,0);
39     scale = Geom::Scale(image->columns() / (double) image->baseColumns(),
40                         image->rows() / (double) image->baseRows());
41     item->scale_rel(scale);
42 
43     // Translate proportionaly to the image/bbox ratio
44     Geom::OptRect bbox(item->desktopGeometricBounds());
45     //g_warning("bbox. W:%f, H:%f, X:%f, Y:%f", bbox->dimensions()[Geom::X], bbox->dimensions()[Geom::Y], bbox->min()[Geom::X], bbox->min()[Geom::Y]);
46 
47     Geom::Translate translate (0,0);
48     translate = Geom::Translate(((_left - _right) / 2.0) * (bbox->dimensions()[Geom::X] / (double) image->columns()),
49                                 ((_bottom - _top) / 2.0) * (bbox->dimensions()[Geom::Y] / (double) image->rows()));
50     item->move_rel(translate);
51 }
52 
53 void
refreshParameters(Inkscape::Extension::Effect * module)54 Crop::refreshParameters(Inkscape::Extension::Effect *module) {
55     _top = module->get_param_int("top");
56     _bottom = module->get_param_int("bottom");
57     _left = module->get_param_int("left");
58     _right = module->get_param_int("right");
59 }
60 
61 #include "../clear-n_.h"
62 
63 void
init()64 Crop::init()
65 {
66     // clang-format off
67     Inkscape::Extension::build_from_mem(
68         "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
69           "<name>" N_("Crop") "</name>\n"
70           "<id>org.inkscape.effect.bitmap.crop</id>\n"
71           "<param name=\"top\" gui-text=\"" N_("Top (px):") "\" type=\"int\" min=\"0\" max=\"100000\">0</param>\n"
72           "<param name=\"bottom\" gui-text=\"" N_("Bottom (px):") "\" type=\"int\" min=\"0\" max=\"100000\">0</param>\n"
73           "<param name=\"left\" gui-text=\"" N_("Left (px):") "\" type=\"int\" min=\"0\" max=\"100000\">0</param>\n"
74           "<param name=\"right\" gui-text=\"" N_("Right (px):") "\" type=\"int\" min=\"0\" max=\"100000\">0</param>\n"
75           "<effect>\n"
76             "<object-type>all</object-type>\n"
77             "<effects-menu>\n"
78               "<submenu name=\"" N_("Raster") "\" />\n"
79             "</effects-menu>\n"
80             "<menu-tip>" N_("Crop selected bitmap(s)") "</menu-tip>\n"
81           "</effect>\n"
82         "</inkscape-extension>\n", new Crop());
83     // clang-format on
84 }
85 
86 }; /* namespace Bitmap */
87 }; /* namespace Internal */
88 }; /* namespace Extension */
89 }; /* namespace Inkscape */
90