package ie.dcu.apps.ist.actions; import ie.dcu.segment.*; import ie.dcu.swt.SwtUtils; import ie.dcu.util.FileUtils; import java.io.*; import java.util.logging.Level; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.*; import org.eclipse.swt.widgets.FileDialog; public class ExportTransparentPNGAction extends AppAction { private static final String DEFAULT_SUFFIX = ".png"; private FileDialog dialog; private String directory; public ExportTransparentPNGAction(ActionManager m) { super(m); } @Override public void run() { if (window.hasContext()) { File file = getFile(); if (file != null) { directory = file.getParent(); export(file); } } } private void export(File file) { ImageData im = createSemiTransparentImage(); try { SwtUtils.saveImage(im, file); } catch (IOException e) { handleError(file, e); } } private ImageData createSemiTransparentImage() { SegmentationContext ctx = window.getContext(); ImageData image = ctx.getImageData(); SegmentationMask mask = ctx.getMask(); ImageData result = new ImageData(image.width, image.height, 32, image.palette); int[] pixels = new int[image.width]; byte[] alphas = new byte[image.width]; for (int y = 0; y < image.height; y++) { image.getPixels(0, y, image.width, pixels, 0); int offset = y * mask.cols; for (int i = 0; i < mask.cols; i++) { if (mask.values[i+offset] == SegmentationMask.BACKGROUND) { alphas[i] = (byte) 0x00; } else { alphas[i] = (byte) 0xff; } } result.setPixels(0, y, image.width, pixels, 0); result.setAlphas(0, y, image.width, alphas, 0); } return result; } private void handleError(File file, IOException e) { // Log log(Level.WARNING, "Error exporting view as image", e); // Show error dialog error("Error exporting view as image %s: %s", file.getName(), e.getLocalizedMessage() ); // Set status message status(Error, "Error exporting view as image %s", file.getName()); } private File getFile() { createExportDialog(); String path = dialog.open(); if (path != null) { return checkFile(path); } return null; } private File checkFile(String path) { File file = new File(path); if (SwtUtils.getImageFormat(file) != -1) { return file; } // Unknown file extension, so assume jpg return new File(FileUtils.replaceExtension(path, DEFAULT_SUFFIX)); } private void createExportDialog() { if (dialog == null) { dialog = new FileDialog(window.getShell(), SWT.SAVE | SWT.SHEET); dialog.setText("Export Transparent PNG"); dialog.setFilterExtensions(new String[]{"*.png"}); dialog.setFilterNames(new String[]{"PNG Images"}); } dialog.setFileName(getFileName()); String dir = getDirectory(); if (dir != null) { // Unfortunately this is buggy on SWT/GTK and // the filter path isin't always set :'-( dialog.setFilterPath(dir); } } private String getFileName() { File file = window.getContext().getFile(); return FileUtils.replaceExtension(file.getName(), DEFAULT_SUFFIX); } private String getDirectory() { if (directory == null) { File folder = window.getContext().getFolder(); directory = (folder != null) ? directory = folder.getAbsolutePath() : null; } return directory; } }