1 | /**
|
---|
2 | * Picture.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.items;
|
---|
20 |
|
---|
21 | import java.io.File;
|
---|
22 | import java.io.IOException;
|
---|
23 | import java.text.DecimalFormat;
|
---|
24 |
|
---|
25 | import org.apache.commons.cli.CommandLine;
|
---|
26 | import org.apache.commons.cli.CommandLineParser;
|
---|
27 | import org.apache.commons.cli.GnuParser;
|
---|
28 | import org.apache.commons.cli.Options;
|
---|
29 | import org.apache.commons.cli.ParseException;
|
---|
30 | import org.expeditee.core.Clip;
|
---|
31 | import org.expeditee.core.Colour;
|
---|
32 | import org.expeditee.core.Dimension;
|
---|
33 | import org.expeditee.core.EnforcedClipStack.EnforcedClipKey;
|
---|
34 | import org.expeditee.core.Image;
|
---|
35 | import org.expeditee.core.Point;
|
---|
36 | import org.expeditee.core.Stroke;
|
---|
37 | import org.expeditee.core.bounds.AxisAlignedBoxBounds;
|
---|
38 | import org.expeditee.core.bounds.PolygonBounds;
|
---|
39 | import org.expeditee.gio.EcosystemManager;
|
---|
40 | import org.expeditee.gio.GraphicsManager;
|
---|
41 | import org.expeditee.gui.DisplayController;
|
---|
42 | import org.expeditee.gui.FrameIO;
|
---|
43 | import org.expeditee.gui.FrameUtils;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * This class represents an Image loaded from a file which is shown on the
|
---|
47 | * screen. Loading of the Image from disk occurs in the constructor, and takes
|
---|
48 | * approximately one second per mb of the Image file size. <br>
|
---|
49 | * <br>
|
---|
50 | * Currently Supported (Tested) Image formats:<br>
|
---|
51 | * BMP<br>
|
---|
52 | * JPG<br>
|
---|
53 | * GIF<br>
|
---|
54 | * GIF (Animated)<br>
|
---|
55 | * <br>
|
---|
56 | * Currently only the default size of the Image is supported, but future
|
---|
57 | * versions may support scaling.
|
---|
58 | *
|
---|
59 | * @author jdm18
|
---|
60 | *
|
---|
61 | */
|
---|
62 | public class Picture extends XRayable {
|
---|
63 |
|
---|
64 | private static final float CROPPING_COMPOSITE_ALPHA = 0.5f;
|
---|
65 |
|
---|
66 | private static final int MINIMUM_WIDTH = 10;
|
---|
67 |
|
---|
68 | public static final int WIDTH = 0;
|
---|
69 |
|
---|
70 | public static final int RATIO = 1;
|
---|
71 |
|
---|
72 | protected Image _image = null;
|
---|
73 |
|
---|
74 | private int _scaleType = RATIO;
|
---|
75 |
|
---|
76 | private float _scale = 1.0f;
|
---|
77 |
|
---|
78 | // Start of the crop relative to START
|
---|
79 | private Point _cropStart = null;
|
---|
80 |
|
---|
81 | // Start of the crop relative to END
|
---|
82 | private Point _cropEnd = null;
|
---|
83 |
|
---|
84 | private Point _start = new Point(0, 0);
|
---|
85 |
|
---|
86 | private Point _end = new Point(0, 0);
|
---|
87 |
|
---|
88 | private double _rotate = 0;
|
---|
89 |
|
---|
90 | private boolean _flipX = false;
|
---|
91 | private boolean _flipY = false;
|
---|
92 |
|
---|
93 | private boolean _showCropping = false;
|
---|
94 |
|
---|
95 | protected Integer _anchorLeft = null;
|
---|
96 | protected Integer _anchorTop = null;
|
---|
97 |
|
---|
98 | private String _path = "";
|
---|
99 |
|
---|
100 | private String _size = "";
|
---|
101 |
|
---|
102 | private String _fileName = null;
|
---|
103 |
|
---|
104 | protected Picture(Text source, Image image) {
|
---|
105 | super(source);
|
---|
106 | _image = image;
|
---|
107 |
|
---|
108 | refresh();
|
---|
109 |
|
---|
110 | if (_image != null) {
|
---|
111 | // Should parsing for minus options also be done?
|
---|
112 | // To be honest, looking at the code, can't really see how _size can be anything but
|
---|
113 | // the empty string at this stage of calling the constructor, although it does have
|
---|
114 | // the 'side' effect of setting other things (such as _start, _end, and _scale)
|
---|
115 | parseSize();
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Creates a new Picture from the given path. The ImageObserver is optional
|
---|
121 | * and can be set to NULL. <br>
|
---|
122 | * Note: It is assumed that the file described in path has already been
|
---|
123 | * checked to exist.
|
---|
124 | *
|
---|
125 | * @param source
|
---|
126 | * The Text Item that was used to create this Picture
|
---|
127 | * @param fileName
|
---|
128 | * the name of the file as it should be displayed in the source
|
---|
129 | * text
|
---|
130 | * @param path
|
---|
131 | * The Path of the Image to load from disk.
|
---|
132 | * @param observer
|
---|
133 | * The ImageObserver to assign when painting the Image on the
|
---|
134 | * screen.
|
---|
135 | */
|
---|
136 | public Picture(Text source, String fileName, String path, String size)
|
---|
137 | {
|
---|
138 | super(source);
|
---|
139 | _fileName = fileName;
|
---|
140 | _path = path;
|
---|
141 |
|
---|
142 | String size_without_options = parseMinusOptions(size);
|
---|
143 | _size = size_without_options;
|
---|
144 |
|
---|
145 | refresh();
|
---|
146 | parseSize();
|
---|
147 | }
|
---|
148 |
|
---|
149 | protected String getImageSize() {
|
---|
150 | return _size;
|
---|
151 | }
|
---|
152 |
|
---|
153 | protected String parseMinusOptions(String cmd_line) {
|
---|
154 |
|
---|
155 | String[] tokens = Text.parseArgsApache(cmd_line);
|
---|
156 |
|
---|
157 | // make anything starting with a '-' lowercase
|
---|
158 | for (int i=0; i<tokens.length; i++) {
|
---|
159 | if (tokens[i].startsWith("-")) {
|
---|
160 | tokens[i] = tokens[i].toLowerCase();
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | // create the command line parser
|
---|
165 | CommandLineParser parser = new GnuParser();
|
---|
166 |
|
---|
167 | // create the Options
|
---|
168 | Options options = new Options();
|
---|
169 | options.addOption( "al", "anchorleft", true, "Anchor the vertical left-hand edge of the interactive widget to the value provided " );
|
---|
170 | options.addOption( "at", "anchortop", true, "Anchor the vertical top edge of the interactive widget to the value provided " );
|
---|
171 |
|
---|
172 | CommandLine core_line;
|
---|
173 | try {
|
---|
174 | // parse the command line arguments
|
---|
175 | core_line = parser.parse( options, tokens );
|
---|
176 |
|
---|
177 | // Update tokens to be version with the options removed
|
---|
178 | tokens = core_line.getArgs();
|
---|
179 |
|
---|
180 | }
|
---|
181 | catch( ParseException exp ) {
|
---|
182 | System.err.println( "Unexpected exception:" + exp.getMessage() );
|
---|
183 | core_line = null;
|
---|
184 |
|
---|
185 | }
|
---|
186 |
|
---|
187 | // Apply any anchor values supplied
|
---|
188 |
|
---|
189 | if(core_line.hasOption( "anchorleft" ) ) {
|
---|
190 | String al_str = core_line.getOptionValue( "anchorleft" );
|
---|
191 |
|
---|
192 | _anchorLeft = Integer.parseInt(al_str);
|
---|
193 | }
|
---|
194 |
|
---|
195 | if(core_line.hasOption( "anchortop" ) ) {
|
---|
196 | String at_str = core_line.getOptionValue( "anchortop" );
|
---|
197 |
|
---|
198 | _anchorTop = Integer.parseInt(at_str);
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|
202 | return String.join(" ", tokens);
|
---|
203 | }
|
---|
204 | protected void parseSize() {
|
---|
205 | String size = getImageSize();
|
---|
206 |
|
---|
207 | if (_end.getX() != 0 || _end.getY() != 0)
|
---|
208 | return;
|
---|
209 |
|
---|
210 | // set the default values for start and end
|
---|
211 | _start.set(0, 0);
|
---|
212 | if (_image == null)
|
---|
213 | _end.set(0, 0);
|
---|
214 | else
|
---|
215 | _end.set(_image.getWidth(), _image.getHeight());
|
---|
216 | size = size.trim();
|
---|
217 | String sizeLower = size.toLowerCase();
|
---|
218 | String[] values = size.split("\\s+");
|
---|
219 | // Now get the cropping values if there are any
|
---|
220 | try {
|
---|
221 | if (values.length > 2) {
|
---|
222 | int startX = Integer.parseInt(values[1]);
|
---|
223 | int startY = Integer.parseInt(values[2]);
|
---|
224 | _start = new Point(startX, startY);
|
---|
225 | if (values.length > 4) {
|
---|
226 | int endX = Integer.parseInt(values[3]);
|
---|
227 | int endY = Integer.parseInt(values[4]);
|
---|
228 | _end = new Point(endX, endY);
|
---|
229 | }
|
---|
230 | scaleCrop();
|
---|
231 | }
|
---|
232 | } catch (Exception e) {
|
---|
233 | }
|
---|
234 |
|
---|
235 | if(sizeLower.contains("flipx")) {
|
---|
236 | _flipX = true;
|
---|
237 | }
|
---|
238 |
|
---|
239 | if(sizeLower.contains("flipy")) {
|
---|
240 | _flipY = true;
|
---|
241 | }
|
---|
242 |
|
---|
243 | int index = sizeLower.indexOf("rotation=");
|
---|
244 | if(index != -1) {
|
---|
245 | int tmp = sizeLower.indexOf(" ", index);
|
---|
246 | String rotation;
|
---|
247 | if(tmp == -1) {
|
---|
248 | rotation = sizeLower.substring(index + "rotation=".length());
|
---|
249 | } else {
|
---|
250 | rotation = sizeLower.substring(index + "rotation=".length(), index + tmp);
|
---|
251 | }
|
---|
252 | _rotate = Double.parseDouble(rotation);
|
---|
253 | }
|
---|
254 |
|
---|
255 | try {
|
---|
256 | if (size.length() == 0) {
|
---|
257 | size = "" + _image.getWidth();
|
---|
258 | _source.setText(getTagText() + size);
|
---|
259 | return;
|
---|
260 | }
|
---|
261 | size = values[0];
|
---|
262 | // parse width or ratio from text
|
---|
263 | if (size.contains(".")) {
|
---|
264 | // this is a ratio
|
---|
265 | _scale = Float.parseFloat(size);
|
---|
266 | _scaleType = RATIO;
|
---|
267 | } else if (size.length() > 0) {
|
---|
268 | // this is an absolute width
|
---|
269 | int width = Integer.parseInt(size);
|
---|
270 | _scaleType = WIDTH;
|
---|
271 | setWidth(width);
|
---|
272 | }
|
---|
273 | } catch (Exception e) {
|
---|
274 | _scale = 1F;
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 | public void setStartCrop(Point p)
|
---|
279 | {
|
---|
280 | if (p != null) setStartCrop(p.getX(), p.getY());
|
---|
281 | }
|
---|
282 |
|
---|
283 | public void setStartCrop(int x, int y) {
|
---|
284 | invalidateCroppedArea();
|
---|
285 | _cropStart = new Point(x - getX(), y - getY());
|
---|
286 | invalidateCroppedArea();
|
---|
287 | }
|
---|
288 |
|
---|
289 | public void setEndCrop(Point p)
|
---|
290 | {
|
---|
291 | if (p != null) setEndCrop(p.getX(), p.getY());
|
---|
292 | }
|
---|
293 |
|
---|
294 | public void setEndCrop(int x, int y) {
|
---|
295 | invalidateCroppedArea();
|
---|
296 | _cropEnd = new Point(x - getX(), y - getY());
|
---|
297 | invalidateCroppedArea();
|
---|
298 | }
|
---|
299 |
|
---|
300 | private void invalidateCroppedArea() {
|
---|
301 | if (_cropStart != null && _cropEnd != null) {
|
---|
302 | Point topLeft = getTopLeftCrop();
|
---|
303 | Point bottomRight = getBottomRightCrop();
|
---|
304 | int startX = getX() + topLeft.getX() - _highlightThickness;
|
---|
305 | int startY = getY() + topLeft.getY() - _highlightThickness;
|
---|
306 | int border = 2 * _highlightThickness;
|
---|
307 | // TODO: Why invalidate specific area just before invalidateAll? cts16
|
---|
308 | invalidate(new AxisAlignedBoxBounds(startX, startY,
|
---|
309 | bottomRight.getX() - topLeft.getX() + 2 * border, bottomRight.getY() - topLeft.getY() + 2 * border));
|
---|
310 | invalidateAll();
|
---|
311 | } else {
|
---|
312 | invalidateAll();
|
---|
313 | }
|
---|
314 | }
|
---|
315 |
|
---|
316 | public Point getTopLeftCrop() {
|
---|
317 | return new Point(Math.min(_cropStart.getX(), _cropEnd.getX()), Math.min(
|
---|
318 | _cropStart.getY(), _cropEnd.getY()));
|
---|
319 | }
|
---|
320 |
|
---|
321 | public Point getBottomRightCrop()
|
---|
322 | {
|
---|
323 | return new Point(Math.max(_cropStart.getX(), _cropEnd.getX()), Math.max(_cropStart.getY(), _cropEnd.getY()));
|
---|
324 | }
|
---|
325 |
|
---|
326 | public void setShowCrop(boolean value) {
|
---|
327 | // invalidateCroppedArea();
|
---|
328 | _showCropping = value;
|
---|
329 | invalidateCroppedArea();
|
---|
330 | }
|
---|
331 |
|
---|
332 | public boolean isBeingCropped()
|
---|
333 | {
|
---|
334 | return (_cropStart != null && _cropEnd != null);
|
---|
335 | }
|
---|
336 |
|
---|
337 | public boolean isCropTooSmall()
|
---|
338 | {
|
---|
339 | if (!isBeingCropped()) return true;
|
---|
340 |
|
---|
341 | int cropWidth = Math.abs(_cropEnd.getX() - _cropStart.getX());
|
---|
342 | int cropHeight = Math.abs(_cropEnd.getY() - _cropStart.getY());
|
---|
343 |
|
---|
344 | return cropWidth < MINIMUM_WIDTH || cropHeight < MINIMUM_WIDTH;
|
---|
345 | }
|
---|
346 |
|
---|
347 | public void clearCropping() {
|
---|
348 | invalidateCroppedArea();
|
---|
349 | _cropStart = null;
|
---|
350 | _cropEnd = null;
|
---|
351 | setShowCrop(false);
|
---|
352 | }
|
---|
353 |
|
---|
354 | public PolygonBounds updateBounds()
|
---|
355 | {
|
---|
356 | if (_image == null) {
|
---|
357 | refresh();
|
---|
358 | parseSize();
|
---|
359 | }
|
---|
360 |
|
---|
361 | Point[] ori = new Point[4];
|
---|
362 | Point centre = new Point();
|
---|
363 |
|
---|
364 | int base_x = (_anchorLeft!=null) ? _anchorLeft : _source.getX();
|
---|
365 | int base_y = (_anchorTop!=null) ? _anchorTop : _source.getY();
|
---|
366 |
|
---|
367 | if (_cropStart == null || _cropEnd == null) {
|
---|
368 | int width = getWidth();
|
---|
369 | int height = getHeight();
|
---|
370 |
|
---|
371 | centre.setX(base_x + width / 2);
|
---|
372 | centre.setY(base_y + height / 2);
|
---|
373 |
|
---|
374 | int xdiff = -MARGIN_RIGHT; // -getLeftMargin();
|
---|
375 |
|
---|
376 | // extra pixel around the image so the highlighting is visible
|
---|
377 | // _poly.addPoint(_source.getX() + 1 + xdiff, _source.getY() - 1);
|
---|
378 | // _poly.addPoint(_source.getX() + width, _source.getY() - 1);
|
---|
379 | // _poly.addPoint(_source.getX() + width, _source.getY() + height);
|
---|
380 | // _poly.addPoint(_source.getX() + 1 + xdiff, _source.getY() + height);
|
---|
381 |
|
---|
382 | ori[0] = new Point(base_x + 1 + xdiff, base_y - 1);
|
---|
383 | ori[1] = new Point(base_x + width, base_y - 1);
|
---|
384 | ori[2] = new Point(base_x + width, base_y + height);
|
---|
385 | ori[3] = new Point(base_x + 1 + xdiff, base_y + height);
|
---|
386 |
|
---|
387 | } else {
|
---|
388 | Point topLeft = getTopLeftCrop();
|
---|
389 | Point bottomRight = getBottomRightCrop();
|
---|
390 |
|
---|
391 | centre.setX(base_x + (bottomRight.getX() - topLeft.getX()) / 2);
|
---|
392 | centre.setY(base_y + (bottomRight.getY() - topLeft.getY()) / 2);
|
---|
393 |
|
---|
394 | AxisAlignedBoxBounds clip = new AxisAlignedBoxBounds(topLeft.getX() + base_x,
|
---|
395 | topLeft.getY() + base_y, bottomRight.getX() - topLeft.getX(),
|
---|
396 | bottomRight.getY() - topLeft.getY());
|
---|
397 | // _poly.addPoint((int) clip.getMinX() - 1, (int) clip.getMinY() - 1);
|
---|
398 | // _poly.addPoint((int) clip.getMinX() - 1, (int) clip.getMaxY());
|
---|
399 | // _poly.addPoint((int) clip.getMaxX(), (int) clip.getMaxY());
|
---|
400 | // _poly.addPoint((int) clip.getMaxX(), (int) clip.getMinY() - 1);
|
---|
401 |
|
---|
402 | ori[0] = new Point((int) clip.getMinX() - 1, (int) clip.getMinY() - 1);
|
---|
403 | ori[1] = new Point((int) clip.getMinX() - 1, (int) clip.getMaxY());
|
---|
404 | ori[2] = new Point((int) clip.getMaxX(), (int) clip.getMaxY());
|
---|
405 | ori[3] = new Point((int) clip.getMaxX(), (int) clip.getMinY() - 1);
|
---|
406 |
|
---|
407 | }
|
---|
408 |
|
---|
409 | PolygonBounds poly = new PolygonBounds();
|
---|
410 | for (Point p : ori) {
|
---|
411 | poly.addPoint(p);
|
---|
412 | }
|
---|
413 | poly.rotate(Math.PI * _rotate / 180, centre);
|
---|
414 |
|
---|
415 | return poly.close();
|
---|
416 | }
|
---|
417 |
|
---|
418 | @Override
|
---|
419 | public double getEnclosedArea() {
|
---|
420 | return getWidth() * getHeight();
|
---|
421 | }
|
---|
422 |
|
---|
423 | @Override
|
---|
424 | public void setWidth(Integer width) {
|
---|
425 | _scale = width * 1F / (_end.getX() - _start.getX());
|
---|
426 | }
|
---|
427 |
|
---|
428 | public Point getStart() {
|
---|
429 | return _start;
|
---|
430 | }
|
---|
431 |
|
---|
432 | public Point getEnd() {
|
---|
433 | return _end;
|
---|
434 | }
|
---|
435 |
|
---|
436 | /**
|
---|
437 | * Gets the width with which the picture is displayed on the screen.
|
---|
438 | */
|
---|
439 | @Override
|
---|
440 | public Integer getWidth() {
|
---|
441 | return Math.round(getUnscaledWidth() * _scale);
|
---|
442 | }
|
---|
443 |
|
---|
444 | /**
|
---|
445 | * Gets the height with which the picture is displayed on the screen.
|
---|
446 | */
|
---|
447 | @Override
|
---|
448 | public int getHeight() {
|
---|
449 | return Math.round(getUnscaledHeight() * _scale);
|
---|
450 | }
|
---|
451 |
|
---|
452 | /**
|
---|
453 | * Dont paint links in audience mode for images.
|
---|
454 | */
|
---|
455 | @Override
|
---|
456 | protected void paintLink()
|
---|
457 | {
|
---|
458 | if (DisplayController.isAudienceMode()) return;
|
---|
459 | super.paintLink();
|
---|
460 | }
|
---|
461 |
|
---|
462 | /**
|
---|
463 | * Paint the image repeatedly tiled over the drawing area.
|
---|
464 | */
|
---|
465 | public void paintImageTiling()
|
---|
466 | {
|
---|
467 | if (_image == null) return;
|
---|
468 |
|
---|
469 | int iw = _image.getWidth();
|
---|
470 | int ih = _image.getHeight();
|
---|
471 | if(iw <= 0 || ih <= 0) return;
|
---|
472 |
|
---|
473 | int base_x = (_anchorLeft != null) ? _anchorLeft : _source.getX();
|
---|
474 | int base_y = (_anchorTop != null) ? _anchorTop : _source.getY();
|
---|
475 |
|
---|
476 | int dX1 = base_x;
|
---|
477 | int dY1 = base_y;
|
---|
478 | int dX2 = base_x + getWidth();
|
---|
479 | int dY2 = base_y + getHeight();
|
---|
480 |
|
---|
481 | Image tmp = Image.createImage(getWidth(), getHeight());
|
---|
482 | EcosystemManager.getGraphicsManager().pushDrawingSurface(tmp);
|
---|
483 |
|
---|
484 | int offX = (tmp.getWidth() - getWidth()) / 2;
|
---|
485 | int offY = (tmp.getHeight() - getHeight()) / 2;
|
---|
486 |
|
---|
487 | int cropStartX = _start.getX();
|
---|
488 | int cropEndX = _end.getX();
|
---|
489 | if(cropEndX > iw) {
|
---|
490 | cropEndX = iw;
|
---|
491 | }
|
---|
492 |
|
---|
493 | for(int x = dX1; x < dX2; ) {
|
---|
494 | // end - start = (cropEnd - cropStart) * scale
|
---|
495 | // => cropEnd = cropStart + (end - start) / scale
|
---|
496 | int w = (int) ((cropEndX - cropStartX) * _scale);
|
---|
497 | int endX = x + w;
|
---|
498 | if(endX > dX2) {
|
---|
499 | endX = dX2;
|
---|
500 | cropEndX = cropStartX + (int) ((dX2 - x) / _scale);
|
---|
501 | }
|
---|
502 |
|
---|
503 | int cropStartY = _start.getY();
|
---|
504 | int cropEndY = _end.getY();
|
---|
505 | if(cropEndY > ih) {
|
---|
506 | cropEndY = ih;
|
---|
507 | }
|
---|
508 |
|
---|
509 | for(int y = dY1; y < dY2; ) {
|
---|
510 | int h = (int) ((cropEndY - cropStartY) * _scale);
|
---|
511 | int endY = y + h;
|
---|
512 | if(endY > dY2) {
|
---|
513 | endY = dY2;
|
---|
514 | cropEndY = cropStartY + (int) ((dY2 - y) / _scale);
|
---|
515 | }
|
---|
516 |
|
---|
517 | int sx = _flipX ? cropEndX : cropStartX;
|
---|
518 | int ex = _flipX ? cropStartX : cropEndX;
|
---|
519 | int sy = _flipY ? cropEndY : cropStartY;
|
---|
520 | int ey = _flipY ? cropStartY : cropEndY;
|
---|
521 |
|
---|
522 | Point topLeft = new Point(x - dX1 + offX, y - dY1 + offY);
|
---|
523 | Dimension size = new Dimension(endX - x, endY - y);
|
---|
524 | Point cropTopLeft = new Point(sx, sy);
|
---|
525 | Dimension cropSize = new Dimension(ex - sx, ey - sy);
|
---|
526 | if (cropSize.width > 0 && cropSize.height > 0) {
|
---|
527 | EcosystemManager.getGraphicsManager().drawImage(_image, topLeft, size, 0.0, cropTopLeft, cropSize);
|
---|
528 | }
|
---|
529 |
|
---|
530 | cropStartY = 0;
|
---|
531 | cropEndY = ih;
|
---|
532 |
|
---|
533 | y = endY;
|
---|
534 | }
|
---|
535 |
|
---|
536 | cropStartX = 0;
|
---|
537 | cropEndX = iw;
|
---|
538 |
|
---|
539 | x = endX;
|
---|
540 | }
|
---|
541 |
|
---|
542 | EcosystemManager.getGraphicsManager().popDrawingSurface();
|
---|
543 | EcosystemManager.getGraphicsManager().drawImage(tmp, new Point(dX1, dY1), null, Math.PI * _rotate / 180);
|
---|
544 | tmp.releaseImage();
|
---|
545 | }
|
---|
546 |
|
---|
547 | @Override
|
---|
548 | public void paint()
|
---|
549 | {
|
---|
550 | if (_image == null) return;
|
---|
551 |
|
---|
552 | paintLink();
|
---|
553 |
|
---|
554 | GraphicsManager g = EcosystemManager.getGraphicsManager();
|
---|
555 |
|
---|
556 | // if we are showing the cropping
|
---|
557 | if (_showCropping && !isCropTooSmall()) {
|
---|
558 | // show the uncropped area as transparent
|
---|
559 | g.setCompositeAlpha(CROPPING_COMPOSITE_ALPHA);
|
---|
560 | paintImageTiling();
|
---|
561 | g.setCompositeAlpha(1.0f);
|
---|
562 |
|
---|
563 | // show the cropped area normally
|
---|
564 | Point topLeft = getTopLeftCrop();
|
---|
565 | Point bottomRight = getBottomRightCrop();
|
---|
566 | int base_x = (_anchorLeft != null) ? _anchorLeft : _source.getX();
|
---|
567 | int base_y = (_anchorTop != null) ? _anchorTop : _source.getY();
|
---|
568 |
|
---|
569 | Clip clip = new Clip(new AxisAlignedBoxBounds( base_x + topLeft.getX(),
|
---|
570 | base_y + topLeft.getY(),
|
---|
571 | bottomRight.getX() - topLeft.getX(),
|
---|
572 | bottomRight.getY() - topLeft.getY()));
|
---|
573 | EnforcedClipKey key = g.pushClip(clip);
|
---|
574 | paintImageTiling();
|
---|
575 | g.popClip(key);
|
---|
576 |
|
---|
577 | // Draw an outline for the crop selection box
|
---|
578 | g.drawRectangle(clip.getBounds(), 0.0, null, getPaintHighlightColor(), HIGHLIGHT_STROKE, null);
|
---|
579 |
|
---|
580 | // otherwise, paint normally
|
---|
581 | } else {
|
---|
582 | paintImageTiling();
|
---|
583 | }
|
---|
584 |
|
---|
585 | PolygonBounds poly = (PolygonBounds) getBounds();
|
---|
586 |
|
---|
587 | if (hasVisibleBorder()) {
|
---|
588 | Stroke borderStroke = new Stroke(getThickness(), DEFAULT_CAP, DEFAULT_JOIN);
|
---|
589 | g.drawPolygon(poly, null, null, 0.0, null, getPaintBorderColor(), borderStroke);
|
---|
590 | }
|
---|
591 |
|
---|
592 | if (isHighlighted()) {
|
---|
593 | Stroke borderStroke = new Stroke(1, DEFAULT_CAP, DEFAULT_JOIN);
|
---|
594 | g.drawPolygon(poly, null, null, 0.0, null, getHighlightColor(), borderStroke);
|
---|
595 | }
|
---|
596 | }
|
---|
597 |
|
---|
598 | @Override
|
---|
599 | public Colour getHighlightColor()
|
---|
600 | {
|
---|
601 | if (_highlightColour.equals(getBorderColor())) return ALTERNATE_HIGHLIGHT;
|
---|
602 | return _highlightColour;
|
---|
603 | }
|
---|
604 |
|
---|
605 | protected Picture createPicture()
|
---|
606 | {
|
---|
607 | return ItemUtils.CreatePicture((Text) _source.copy());
|
---|
608 | }
|
---|
609 |
|
---|
610 | @Override
|
---|
611 | public Picture copy() {
|
---|
612 | Picture p = createPicture();
|
---|
613 | p._image = _image;
|
---|
614 | p._highlightMode = _highlightMode;
|
---|
615 | // Doing Duplicate item duplicates link mark which we dont want to do
|
---|
616 | // when in audience mode because the linkMark will be copied incorrectly
|
---|
617 | // Get all properties from the source
|
---|
618 |
|
---|
619 | if (!isCropTooSmall() && _cropStart != null && _cropEnd != null) {
|
---|
620 | assert (_cropEnd != null);
|
---|
621 | // make the start be the top left
|
---|
622 | // make the end be the bottom right
|
---|
623 | Point topLeft = getTopLeftCrop();
|
---|
624 | Point bottomRight = getBottomRightCrop();
|
---|
625 | int startX = Math.round(topLeft.getX() / _scale) + _start.getX();
|
---|
626 | int startY = Math.round(topLeft.getY() / _scale) + _start.getY();
|
---|
627 | int endX = Math.round(bottomRight.getX() / _scale + _start.getX());
|
---|
628 | int endY = Math.round(bottomRight.getY() / _scale + _start.getY());
|
---|
629 | int width = _image.getWidth();
|
---|
630 | int height = _image.getHeight();
|
---|
631 | // adjust our start and end if the user has dragged outside of the
|
---|
632 | // shape
|
---|
633 | if (endX > width) {
|
---|
634 | endX = width;
|
---|
635 | }
|
---|
636 | if (endY > height) {
|
---|
637 | endY = height;
|
---|
638 | }
|
---|
639 | if (startX < 0) {
|
---|
640 | startX = 0;
|
---|
641 | }
|
---|
642 | if (startY < 0) {
|
---|
643 | startY = 0;
|
---|
644 | }
|
---|
645 | p._start = new Point(startX, startY);
|
---|
646 | p._end = new Point(endX, endY);
|
---|
647 | int base_x = (_anchorLeft!=null) ? _anchorLeft : _source.getX();
|
---|
648 | int base_y = (_anchorTop!=null) ? _anchorTop : _source.getY();
|
---|
649 | p._source.setPosition(topLeft.getX() + base_x, topLeft.getY() + base_y);
|
---|
650 | } else {
|
---|
651 | p._start = new Point(_start);
|
---|
652 | p._end = new Point(_end);
|
---|
653 | }
|
---|
654 | p._scale = _scale;
|
---|
655 | p._scaleType = _scaleType;
|
---|
656 | p._path = _path;
|
---|
657 | p._fileName = _fileName;
|
---|
658 |
|
---|
659 | p.updateSource();
|
---|
660 | p.invalidateBounds();
|
---|
661 |
|
---|
662 | return p;
|
---|
663 | }
|
---|
664 |
|
---|
665 | public float getScale() {
|
---|
666 | return _scale;
|
---|
667 | }
|
---|
668 |
|
---|
669 | public void setScale(float scale) {
|
---|
670 | _scale = scale;
|
---|
671 | }
|
---|
672 |
|
---|
673 | public void scaleCrop() {
|
---|
674 | // scale crop values to within image bounds
|
---|
675 | int iw = _image.getWidth();
|
---|
676 | int ih = _image.getHeight();
|
---|
677 | if(iw > 0 || ih > 0) {
|
---|
678 | while(_start.getX() >= iw) {
|
---|
679 | _start.setX(_start.getX() - iw);
|
---|
680 | _end.setX(_end.getX() - iw);
|
---|
681 | }
|
---|
682 | while(_start.getY() >= ih) {
|
---|
683 | _start.setY(_start.getY() - ih);
|
---|
684 | _end.setY(_end.getY() - ih);
|
---|
685 | }
|
---|
686 | while(_start.getX() < 0) {
|
---|
687 | _start.setX(_start.getX() + iw);
|
---|
688 | _end.setX(_end.getX() + iw);
|
---|
689 | }
|
---|
690 | while(_start.getY() < 0) {
|
---|
691 | _start.setY(_start.getY() + ih);
|
---|
692 | _end.setY(_end.getY() + ih);
|
---|
693 | }
|
---|
694 | }
|
---|
695 | }
|
---|
696 |
|
---|
697 | public void setCrop(int startX, int startY, int endX, int endY) {
|
---|
698 | _start = new Point(startX, startY);
|
---|
699 | _end = new Point(endX, endY);
|
---|
700 | updateSource();
|
---|
701 | }
|
---|
702 |
|
---|
703 | @Override
|
---|
704 | public float getSize() {
|
---|
705 | return _source.getSize();
|
---|
706 | }
|
---|
707 |
|
---|
708 | @Override
|
---|
709 | public void setSize(float size) {
|
---|
710 | float diff = size - _source.getSize();
|
---|
711 | float oldScale = _scale;
|
---|
712 |
|
---|
713 | float multiplier = (1000F + diff * 40F) / 1000F;
|
---|
714 | _scale = _scale * multiplier;
|
---|
715 |
|
---|
716 | // picture must still be at least XX pixels wide
|
---|
717 | if (getWidth() < MINIMUM_WIDTH) {
|
---|
718 | _scale = oldScale;
|
---|
719 | } else {
|
---|
720 | _source.translate(EcosystemManager.getInputManager().getCursorPosition(), multiplier);
|
---|
721 | }
|
---|
722 | updateSource();
|
---|
723 | invalidateBounds();
|
---|
724 | // Make sure items that are resized display the border
|
---|
725 | invalidateAll();
|
---|
726 | }
|
---|
727 |
|
---|
728 | @Override
|
---|
729 | public void setAnnotation(boolean val) {
|
---|
730 | }
|
---|
731 |
|
---|
732 | /**
|
---|
733 | * Returns the Image that this Picture object is painting on the screen.
|
---|
734 | * This is used by Frame to repaint animated GIFs.
|
---|
735 | *
|
---|
736 | * @return The Image that this Picture object represents.
|
---|
737 | */
|
---|
738 | public Image getImage() {
|
---|
739 | return _image;
|
---|
740 | }
|
---|
741 |
|
---|
742 | public Image getCroppedImage() {
|
---|
743 | if (_image == null)
|
---|
744 | return null;
|
---|
745 | if (!isCropped()) {
|
---|
746 | return _image;
|
---|
747 | }
|
---|
748 |
|
---|
749 | return Image.createImageAsCroppedCopy(_image, _start.getX(), _start.getY(), getUnscaledWidth(), getUnscaledHeight());
|
---|
750 | }
|
---|
751 |
|
---|
752 | public int getUnscaledWidth() {
|
---|
753 | return _end.getX() - _start.getX();
|
---|
754 | }
|
---|
755 |
|
---|
756 | public int getUnscaledHeight() {
|
---|
757 | return _end.getY() - _start.getY();
|
---|
758 | }
|
---|
759 |
|
---|
760 | /**
|
---|
761 | * @return true if this is a cropped image.
|
---|
762 | */
|
---|
763 | public boolean isCropped() {
|
---|
764 | return (_end.getX() != 0 && _end.getX() != _image.getWidth()) || (_end.getY() != 0 && _end.getY() != _image.getHeight()) || _start.getY() != 0 || _start.getX() != 0;
|
---|
765 | }
|
---|
766 |
|
---|
767 | @Override
|
---|
768 | public boolean refresh() {
|
---|
769 | // ImageIcon is faster, but cannot handle some formats
|
---|
770 | // (notably.bmp) hence, we try this first, then if it fails we try
|
---|
771 | // ImageIO
|
---|
772 | /*
|
---|
773 | try {
|
---|
774 | _image = new ImageIcon(_path).getImage();
|
---|
775 | } catch (Exception e) {
|
---|
776 | }
|
---|
777 |
|
---|
778 | // if ImageIcon failed to read the image
|
---|
779 | if (_image == null || _image.getWidth() <= 0) {
|
---|
780 | try {
|
---|
781 | _image = ImageIO.read(new File(_path));
|
---|
782 | } catch (IOException e) {
|
---|
783 | // e.printStackTrace();
|
---|
784 | Logger.Log(e);
|
---|
785 | _image = null;
|
---|
786 | return false;
|
---|
787 | }
|
---|
788 | }
|
---|
789 | */
|
---|
790 | _image = Image.getImage(_path);
|
---|
791 | return true;
|
---|
792 | }
|
---|
793 |
|
---|
794 | @Override
|
---|
795 | protected int getLinkYOffset() {
|
---|
796 | return getBoundsHeight() / 2;
|
---|
797 | }
|
---|
798 |
|
---|
799 | @Override
|
---|
800 | public void setLinkMark(boolean state) {
|
---|
801 | // TODO use the more efficient invalidiate method
|
---|
802 | // The commented code below is not quite working
|
---|
803 | // if(!state)
|
---|
804 | // invalidateCommonTrait(ItemAppearence.LinkChanged);
|
---|
805 | _source.setLinkMark(state);
|
---|
806 | // if(state)
|
---|
807 | // invalidateCommonTrait(ItemAppearence.LinkChanged);
|
---|
808 | invalidateAll();
|
---|
809 | }
|
---|
810 |
|
---|
811 | @Override
|
---|
812 | public void setActionMark(boolean state) {
|
---|
813 | // if (!state)
|
---|
814 | // invalidateCommonTrait(ItemAppearence.LinkChanged);
|
---|
815 | _source.setActionMark(state);
|
---|
816 | // if (state)
|
---|
817 | // invalidateCommonTrait(ItemAppearence.LinkChanged);
|
---|
818 | invalidateAll();
|
---|
819 | }
|
---|
820 |
|
---|
821 | @Override
|
---|
822 | public boolean getLinkMark() {
|
---|
823 | return !DisplayController.isAudienceMode() && _source.getLinkMark();
|
---|
824 | }
|
---|
825 |
|
---|
826 | @Override
|
---|
827 | public boolean getActionMark() {
|
---|
828 | return _source.getActionMark();
|
---|
829 | }
|
---|
830 |
|
---|
831 | @Override
|
---|
832 | public String getName() {
|
---|
833 | return _fileName;
|
---|
834 | }
|
---|
835 |
|
---|
836 | public String getPath() {
|
---|
837 | return _path;
|
---|
838 | }
|
---|
839 |
|
---|
840 | /**
|
---|
841 | * Copies the image to the default images folder and updates the reference to it in Expeditee
|
---|
842 | * Used for correcting image references for FrameShare
|
---|
843 | */
|
---|
844 | public void moveToImagesFolder() {
|
---|
845 | File f = new File(getPath());
|
---|
846 | // if the file is not in the default images folder, copy it there
|
---|
847 | if(! f.getParentFile().equals(new File(FrameIO.IMAGES_PATH))) {
|
---|
848 | try {
|
---|
849 | File f2 = new File(FrameIO.IMAGES_PATH + f.getName());
|
---|
850 | FrameUtils.copyFile(f, f2, false);
|
---|
851 | f = f2;
|
---|
852 | } catch (IOException e) {
|
---|
853 | e.printStackTrace();
|
---|
854 | f = null;
|
---|
855 | }
|
---|
856 | }
|
---|
857 | _path = f.getPath();
|
---|
858 | _fileName = f.getName();
|
---|
859 | updateSource();
|
---|
860 | }
|
---|
861 |
|
---|
862 | protected String getTagText() {
|
---|
863 | return "@i: " + _fileName + " ";
|
---|
864 | }
|
---|
865 |
|
---|
866 | /**
|
---|
867 | * Updates the source text for this item to match the current size of the
|
---|
868 | * image.
|
---|
869 | *
|
---|
870 | */
|
---|
871 | private void updateSource() {
|
---|
872 | StringBuffer newText = new StringBuffer(getTagText());
|
---|
873 |
|
---|
874 | switch (_scaleType) {
|
---|
875 | case (RATIO):
|
---|
876 | DecimalFormat format = new DecimalFormat("0.00");
|
---|
877 | newText.append(format.format(_scale));
|
---|
878 | break;
|
---|
879 | case (WIDTH):
|
---|
880 | newText.append(getWidth());
|
---|
881 | break;
|
---|
882 | }
|
---|
883 |
|
---|
884 | scaleCrop();
|
---|
885 |
|
---|
886 | // If the image is cropped add the position for the start and finish of
|
---|
887 | // the crop to the soure text
|
---|
888 | if (_start.getX() > 0 || _start.getY() > 0 || _end.getX() != _image.getWidth()
|
---|
889 | || _end.getY() != _image.getHeight()) {
|
---|
890 | newText.append(" ").append(_start.getX()).append(" ").append(_start.getY());
|
---|
891 | newText.append(" ").append(_end.getX()).append(" ").append(_end.getY());
|
---|
892 | }
|
---|
893 |
|
---|
894 | if(_flipX) {
|
---|
895 | newText.append(" flipX");
|
---|
896 | }
|
---|
897 | if(_flipY) {
|
---|
898 | newText.append(" flipY");
|
---|
899 | }
|
---|
900 | if(Double.compare(_rotate, 0) != 0) {
|
---|
901 | newText.append(" rotation=" + _rotate);
|
---|
902 | }
|
---|
903 |
|
---|
904 | _source.setText(newText.toString());
|
---|
905 | }
|
---|
906 |
|
---|
907 | @Override
|
---|
908 | public void translate(Point origin, double ratio) {
|
---|
909 | _scale *= ratio;
|
---|
910 | updateSource();
|
---|
911 | super.translate(origin, ratio);
|
---|
912 | }
|
---|
913 |
|
---|
914 | @Override
|
---|
915 | public AxisAlignedBoxBounds getDrawingArea() {
|
---|
916 |
|
---|
917 | AxisAlignedBoxBounds da = super.getDrawingArea();
|
---|
918 |
|
---|
919 | if (getLink() != null || hasAction()) {
|
---|
920 | AxisAlignedBoxBounds linkBounds = AxisAlignedBoxBounds.getEnclosing(getLinkBounds());
|
---|
921 | linkBounds.getTopLeft().add(getX() - LEFT_MARGIN, getY() + getLinkYOffset());
|
---|
922 | linkBounds.getSize().width += 2;
|
---|
923 | linkBounds.getSize().height += 2;
|
---|
924 | da.combineWith(linkBounds);
|
---|
925 | }
|
---|
926 |
|
---|
927 | return da;
|
---|
928 |
|
---|
929 | }
|
---|
930 |
|
---|
931 | @Override
|
---|
932 | public void scale(Float scale, int originX, int originY) {
|
---|
933 | setScale(getScale() * scale);
|
---|
934 | super.scale(scale, originX, originY);
|
---|
935 | }
|
---|
936 |
|
---|
937 | public void setFlipX(boolean flip) {
|
---|
938 | _flipX = flip;
|
---|
939 | }
|
---|
940 |
|
---|
941 | public void setFlipY(boolean flip) {
|
---|
942 | _flipY = flip;
|
---|
943 | }
|
---|
944 |
|
---|
945 | public boolean getFlipX() {
|
---|
946 | return _flipX;
|
---|
947 | }
|
---|
948 |
|
---|
949 | public boolean getFlipY() {
|
---|
950 | return _flipY;
|
---|
951 | }
|
---|
952 |
|
---|
953 | public void setRotate(double rotate) {
|
---|
954 | _rotate = rotate;
|
---|
955 | updateSource();
|
---|
956 | invalidateBounds();
|
---|
957 | }
|
---|
958 |
|
---|
959 | public double getRotate() {
|
---|
960 | return _rotate;
|
---|
961 | }
|
---|
962 |
|
---|
963 | public boolean MouseOverBackgroundPixel(int mouseX, int mouseY, Colour bg_col)
|
---|
964 | {
|
---|
965 | int base_x = (_anchorLeft!=null) ? _anchorLeft : _source.getX();
|
---|
966 | int base_y = (_anchorTop!=null) ? _anchorTop : _source.getY();
|
---|
967 | int x = mouseX - base_x;
|
---|
968 | int y = mouseY - base_y;
|
---|
969 |
|
---|
970 | Colour c = _image.getPixel(x, y);
|
---|
971 |
|
---|
972 | int c_red = c.getRed255();
|
---|
973 | int c_green = c.getGreen255();
|
---|
974 | int c_blue = c.getBlue255();
|
---|
975 |
|
---|
976 | int bg_red = (bg_col!=null) ? bg_col.getRed255() : 0xff;
|
---|
977 | int bg_green = (bg_col!=null) ? bg_col.getGreen255() : 0xff;
|
---|
978 | int bg_blue = (bg_col!=null) ? bg_col.getBlue255() : 0xff;
|
---|
979 |
|
---|
980 | int red_diff = Math.abs(c_red - bg_red);
|
---|
981 | int green_diff = Math.abs(c_green - bg_green);
|
---|
982 | int blue_diff = Math.abs(c_blue - bg_blue);
|
---|
983 |
|
---|
984 | return ((red_diff<=2) && (green_diff<=2) && (blue_diff<=2));
|
---|
985 |
|
---|
986 | }
|
---|
987 | }
|
---|