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