Skip to content

Commit 6574f8a

Browse files
committed
Fixing support for image resizing in annotator export.
* Fixing issues with cropping.
1 parent 3992151 commit 6574f8a

File tree

4 files changed

+146
-27
lines changed

4 files changed

+146
-27
lines changed

src/Canvas.vala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ public class Canvas : DrawingArea {
377377

378378
var retval = grab_focus();
379379
if( image.cropping ) {
380-
if( image.cursor_pressed( x, y, n_press ) ) {
380+
if( image.cursor_pressed( ex, ey, n_press ) ) {
381381
queue_draw();
382382
}
383383
} else if( items.cursor_pressed( x, y, n_press ) ) {
@@ -401,7 +401,7 @@ public class Canvas : DrawingArea {
401401
_last_y = y;
402402

403403
if( image.cropping || image.picking ) {
404-
if( image.cursor_moved( x, y ) ) {
404+
if( image.cursor_moved( ex, ey ) ) {
405405
queue_draw();
406406
}
407407
} else if( items.cursor_moved( x, y ) ) {
@@ -417,7 +417,7 @@ public class Canvas : DrawingArea {
417417
var y = scale_y( ey );
418418

419419
if( image.cropping || image.picking ) {
420-
if( image.cursor_released( x, y ) ) {
420+
if( image.cursor_released( ex, ey ) ) {
421421
queue_draw();
422422
}
423423
} else if( items.cursor_released( x, y ) ) {

src/CanvasImage.vala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,8 @@ public class CanvasImage {
601601

602602
node->set_prop( "angle", _angle.to_string() );
603603

604+
node->add_child( info.save() );
605+
604606
return( node );
605607

606608
}
@@ -622,6 +624,15 @@ public class CanvasImage {
622624
// TBD - We will probably want to handle the angle change in the canvas.
623625
}
624626

627+
for( Xml.Node* it=node->children; it!=null; it=it->next ) {
628+
if( (it->type == Xml.ElementType.ELEMENT_NODE) && (it->name == "image-info") ) {
629+
var old_info = new CanvasImageInfo.from_info( info );
630+
var new_info = new CanvasImageInfo.from_info( info );
631+
new_info.load( it );
632+
do_resize( old_info, new_info );
633+
}
634+
}
635+
625636
return( loaded );
626637

627638
}
@@ -680,6 +691,9 @@ public class CanvasImage {
680691

681692
Utils.set_context_color_with_alpha( ctx, black, 0.5 );
682693

694+
ctx.rectangle( crop_rect.x1(), crop_rect.y1(), crop_rect.width, crop_rect.height );
695+
ctx.stroke();
696+
683697
ctx.rectangle( 0, 0, crop_rect.x1(), info.height );
684698
ctx.fill();
685699

@@ -769,6 +783,9 @@ public class CanvasImage {
769783

770784
}
771785

786+
//-------------------------------------------------------------
787+
// Returns the color value at the given pixel location within the
788+
// image buffer.
772789
private RGBA get_color_at( Pixbuf buf, int x, int y ) {
773790

774791
var start = (y * buf.rowstride) + (x * buf.n_channels);

src/CanvasImageInfo.vala

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,48 @@ public class CanvasImageInfo {
9595
return( width - (int)(pixbuf_rect.width + pixbuf_rect.x) );
9696
}
9797

98-
/* Generates a string version of this class for debug output */
98+
//-------------------------------------------------------------
99+
// Generates a string version of this class for debug output.
99100
public string to_string() {
100101
return( "width: %d, height: %d, pixbuf_rect: %s\n".printf( width, height, pixbuf_rect.to_string() ) );
101102
}
102103

104+
//-------------------------------------------------------------
105+
// Saves the canvas image information in XML format.
106+
public Xml.Node* save() {
107+
108+
Xml.Node* node = new Xml.Node( null, "image-info" );
109+
110+
node->set_prop( "width", width.to_string() );
111+
node->set_prop( "height", height.to_string() );
112+
113+
node->add_child( pixbuf_rect.save() );
114+
115+
return( node );
116+
117+
}
118+
119+
//-------------------------------------------------------------
120+
// Loads the canvas image information from XML format.
121+
public void load( Xml.Node* node ) {
122+
123+
var w = node->get_prop( "width" );
124+
if( w != null ) {
125+
width = int.parse( w );
126+
}
127+
128+
var h = node->get_prop( "height" );
129+
if( h != null ) {
130+
height = int.parse( h );
131+
}
132+
133+
for( Xml.Node* it=node->children; it!=null; it=it->next ) {
134+
if( (it->type == Xml.ElementType.ELEMENT_NODE) && (it->name == "rect") ) {
135+
pixbuf_rect.load( it );
136+
}
137+
}
138+
139+
}
140+
103141
}
104142

src/CanvasRect.vala

Lines changed: 87 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,36 +28,44 @@ public class CanvasRect {
2828
public double width { get; set; default = 0.0; }
2929
public double height { get; set; default = 0.0; }
3030

31-
/* Constructor */
31+
//-------------------------------------------------------------
32+
// Constructor
3233
public CanvasRect() {}
3334

34-
/* Constructor */
35+
//-------------------------------------------------------------
36+
// Constructor
3537
public CanvasRect.from_coords( double x, double y, double width, double height ) {
3638
copy_coords( x, y, width, height );
3739
}
3840

39-
/* Copy constructor */
41+
//-------------------------------------------------------------
42+
// Copy constructor
4043
public CanvasRect.from_rect( CanvasRect rect ) {
4144
copy( rect );
4245
}
4346

44-
/* Makes a copy of the given rectangle */
47+
//-------------------------------------------------------------
48+
// Makes a copy of the given rectangle
4549
public void copy( CanvasRect rect ) {
4650
x = rect.x;
4751
y = rect.y;
4852
width = rect.width;
4953
height = rect.height;
5054
}
5155

52-
/* Makes a copy of the given rectangle coordinates to this rectangle */
56+
//-------------------------------------------------------------
57+
// Makes a copy of the given rectangle coordinates to this
58+
// rectangle.
5359
public void copy_coords( double x, double y, double width, double height ) {
5460
this.x = x;
5561
this.y = y;
5662
this.width = width;
5763
this.height = height;
5864
}
5965

60-
/* Converts this rectangle into a Gdk.Rectangle and returns the result */
66+
//-------------------------------------------------------------
67+
// Converts this rectangle into a Gdk.Rectangle and returns the
68+
// result.
6169
public Gdk.Rectangle to_rectangle( double zoom_factor = 1.0 ) {
6270
Gdk.Rectangle rect = {0, 0, 0, 0};
6371
rect.x = (int)(x * zoom_factor);
@@ -67,60 +75,72 @@ public class CanvasRect {
6775
return( rect );
6876
}
6977

70-
/* Returns the x1 value */
78+
//-------------------------------------------------------------
79+
// Returns the x1 value.
7180
public double x1() {
7281
return( x );
7382
}
7483

75-
/* Returns the y1 value */
84+
//-------------------------------------------------------------
85+
// Returns the y1 value.
7686
public double y1() {
7787
return( y );
7888
}
7989

80-
/* Returns the x2 value */
90+
//-------------------------------------------------------------
91+
// Returns the x2 value.
8192
public double x2() {
8293
return( (x + width) );
8394
}
8495

85-
/* Returns the y2 value */
96+
//-------------------------------------------------------------
97+
// Returns the y2 value.
8698
public double y2() {
8799
return( (y + height) );
88100
}
89101

90-
/* Returns the mid-point between x1 and x2 */
102+
//-------------------------------------------------------------
103+
// Returns the mid-point between x1 and x2.
91104
public double mid_x() {
92105
return( x + (width / 2) );
93106
}
94107

95-
/* Returns the mid-point between y1 and y2 */
108+
//-------------------------------------------------------------
109+
// Returns the mid-point between y1 and y2.
96110
public double mid_y() {
97111
return( y + (height / 2) );
98112
}
99113

100-
/* Returns the absolute width of the rectangle */
114+
//-------------------------------------------------------------
115+
// Returns the absolute width of the rectangle.
101116
public double abs_width() {
102117
return( width.abs() );
103118
}
104119

105-
/* Returns the absolute height of the rectangle */
120+
//-------------------------------------------------------------
121+
// Returns the absolute height of the rectangle.
106122
public double abs_height() {
107123
return( height.abs() );
108124
}
109125

110-
/* Resizes the size of the rectangle by the numb of pixels */
126+
//-------------------------------------------------------------
127+
// Resizes the size of the rectangle by the numb of pixels.
111128
public void resize( double amount ) {
112129
x -= amount;
113130
y -= amount;
114131
width += (amount * 2);
115132
height += (amount * 2);
116133
}
117134

118-
/* Returns true if the given rectangle matches this rectangle */
135+
//-------------------------------------------------------------
136+
// Returns true if the given rectangle matches this rectangle.
119137
public bool equals( CanvasRect rect ) {
120138
return( (x == rect.x) && (y == rect.y) && (width == rect.width) && (height == rect.height) );
121139
}
122140

123-
/* Returns true if the given rectangle intersects with this rectangle */
141+
//-------------------------------------------------------------
142+
// Returns true if the given rectangle intersects with this
143+
// rectangle.
124144
public bool intersects( CanvasRect rect ) {
125145
var x5 = (x1() < rect.x1()) ? rect.x1() : x1();
126146
var y5 = (y2() < rect.y2()) ? rect.y2() : y2();
@@ -129,9 +149,9 @@ public class CanvasRect {
129149
return( (x5 <= x6) && (y6 <= y5) );
130150
}
131151

132-
/*
133-
Stores the intersected rectangle of the two specified rectangles in ourself.
134-
*/
152+
//-------------------------------------------------------------
153+
// Stores the intersected rectangle of the two specified
154+
// rectangles in ourself.
135155
public void intersection( CanvasRect a, CanvasRect b ) {
136156
var x1 = (a.x1() < b.x1()) ? b.x1() : a.x1();
137157
var y1 = (a.y1() < b.y1()) ? b.y1() : a.y1();
@@ -143,12 +163,14 @@ public class CanvasRect {
143163
height = (y2 - y1);
144164
}
145165

146-
/* Returns true if this rectangle contains the given point */
166+
//-------------------------------------------------------------
167+
// Returns true if this rectangle contains the given point.
147168
public bool contains( double x, double y ) {
148169
return( Utils.is_within_bounds( x, y, this.x, this.y, this.width, this.height ) );
149170
}
150171

151-
/* Creates a rectangle that is a drawable version of this rectangle */
172+
//-------------------------------------------------------------
173+
// Creates a rectangle that is a drawable version of this rectangle.
152174
public void normalize( out CanvasRect rect ) {
153175
rect = new CanvasRect.from_coords(
154176
((width < 0) ? (x + width) : x),
@@ -158,11 +180,53 @@ public class CanvasRect {
158180
);
159181
}
160182

161-
/* Returns a string version of this rectangle */
183+
//-------------------------------------------------------------
184+
// Returns a string version of this rectangle.
162185
public string to_string() {
163186
return( "x: %g, y: %g, w: %g, h: %g".printf( x, y, width, height ) );
164187
}
165188

189+
//-------------------------------------------------------------
190+
// Saves this rectangle to XML format.
191+
public Xml.Node* save() {
192+
193+
Xml.Node* node = new Xml.Node( null, "rect" );
194+
195+
node->set_prop( "x", x.to_string() );
196+
node->set_prop( "y", y.to_string() );
197+
node->set_prop( "width", width.to_string() );
198+
node->set_prop( "height", height.to_string() );
199+
200+
return( node );
201+
202+
}
203+
204+
//-------------------------------------------------------------
205+
// Loads the rectangle information from XML format.
206+
public void load( Xml.Node* node ) {
207+
208+
var xs = node->get_prop( "x" );
209+
if( xs != null ) {
210+
x = double.parse( xs );
211+
}
212+
213+
var ys = node->get_prop( "y" );
214+
if( ys != null ) {
215+
y = double.parse( ys );
216+
}
217+
218+
var w = node->get_prop( "width" );
219+
if( w != null ) {
220+
width = double.parse( w );
221+
}
222+
223+
var h = node->get_prop( "height" );
224+
if( h != null ) {
225+
height = double.parse( h );
226+
}
227+
228+
}
229+
166230
}
167231

168232

0 commit comments

Comments
 (0)