blob: 7539e0f0fc6cd8d3d05f88f6a79ee515ecc25ad1 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2018 Agence spatiale canadienne / Canadian Space Agency
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pierre Allard - initial API and implementation
* Regent L'Archeveque
*
* SPDX-License-Identifier: EPL-1.0
*******************************************************************************/
package org.eclipse.apogy.addons.sensors.imaging.camera.composites;
import org.eclipse.apogy.addons.sensors.imaging.ImageSnapshot;
import org.eclipse.apogy.addons.sensors.imaging.camera.DrawnCameraOverlay;
import org.eclipse.apogy.addons.sensors.imaging.camera.internal.AbstractCameraStub;
import org.eclipse.apogy.common.images.AbstractEImage;
import org.eclipse.apogy.common.images.EImagesUtilities;
import org.eclipse.apogy.common.images.ui.composites.ImageDisplayComposite;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.util.EContentAdapter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DrawnCameraOverlayPreviewComposite extends Composite {
private static final Logger Logger = LoggerFactory.getLogger(DrawnCameraOverlayPreviewComposite.class);
private DrawnCameraOverlay drawnCameraOverlay;
private Adapter drawnCameraOverlayAdapter;
private final AbstractCameraStub abstractCameraStub = new AbstractCameraStub(
"platform:/plugin/org.eclipse.apogy.addons.sensors.imaging.camera/images/unfiltered_image.jpg",
Math.toRadians(45), Math.toRadians(25.3125));
private final ImageDisplayComposite imagePreviewComposite;
public DrawnCameraOverlayPreviewComposite(Composite parent, int style) {
super(parent, style);
setLayout(new FillLayout());
this.imagePreviewComposite = new ImageDisplayComposite(this, SWT.NONE);
}
public DrawnCameraOverlay getDrawnCameraOverlay() {
return this.drawnCameraOverlay;
}
public void setDrawnCameraOverlay(DrawnCameraOverlay drawnCameraOverlay) {
if (this.drawnCameraOverlay != null) {
this.drawnCameraOverlay.eAdapters().remove(getDrawnCameraOverlayAdapter());
}
this.drawnCameraOverlay = drawnCameraOverlay;
if (this.drawnCameraOverlay != null) {
this.drawnCameraOverlay.eAdapters().add(getDrawnCameraOverlayAdapter());
updatePreview();
}
}
protected void updatePreview() {
if (!isDisposed()) {
getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
try {
if (getDrawnCameraOverlay() != null) {
DrawnCameraOverlayPreviewComposite.this.abstractCameraStub.setName("Test Camera");
DrawnCameraOverlayPreviewComposite.this.abstractCameraStub.setDescription("A test camera.");
ImageSnapshot imageSnapshot = DrawnCameraOverlayPreviewComposite.this.abstractCameraStub
.takeSnapshot();
AbstractEImage abstractEImage = imageSnapshot.getImage();
AbstractEImage resultImage = getDrawnCameraOverlay().applyOverlay(
DrawnCameraOverlayPreviewComposite.this.abstractCameraStub, abstractEImage,
getDrawnCameraOverlay().getOverlayAlignment(), abstractEImage.getWidth(),
abstractEImage.getHeight());
ImageData imageData = EImagesUtilities.INSTANCE
.convertToImageData(resultImage.asBufferedImage());
if (DrawnCameraOverlayPreviewComposite.this.imagePreviewComposite != null
&& !DrawnCameraOverlayPreviewComposite.this.imagePreviewComposite.isDisposed()
&& imageData != null) {
DrawnCameraOverlayPreviewComposite.this.imagePreviewComposite.setImageData(imageData);
DrawnCameraOverlayPreviewComposite.this.imagePreviewComposite.fitImage();
}
} else {
// TODO
}
} catch (Exception e) {
Logger.error(e.getMessage(), e);
}
}
});
}
}
private Adapter getDrawnCameraOverlayAdapter() {
if (this.drawnCameraOverlayAdapter == null) {
this.drawnCameraOverlayAdapter = new EContentAdapter() {
@Override
public void notifyChanged(Notification notification) {
updatePreview();
super.notifyChanged(notification);
}
};
}
return this.drawnCameraOverlayAdapter;
}
}