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