Comparar commits
1 Commits
| Autor | SHA1 | Data | |
|---|---|---|---|
| 3305e23b01 |
+116
-21
@@ -45,6 +45,8 @@ import java.awt.Color;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.OutputStream;
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.*;
|
||||
|
||||
import org.gephi.io.exporter.spi.ByteExporter;
|
||||
import org.gephi.io.exporter.spi.VectorExporter;
|
||||
import org.gephi.preview.api.PreviewController;
|
||||
@@ -58,13 +60,13 @@ import org.gephi.utils.progress.Progress;
|
||||
import org.gephi.utils.progress.ProgressTicket;
|
||||
import org.openide.util.Lookup;
|
||||
import processing.core.PGraphicsJava2D;
|
||||
import processing.core.PMatrix;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Mathieu Bastian
|
||||
*/
|
||||
public class PNGExporter implements VectorExporter, ByteExporter, LongTask {
|
||||
|
||||
|
||||
private ProgressTicket progress;
|
||||
private boolean cancel = false;
|
||||
private Workspace workspace;
|
||||
@@ -74,24 +76,123 @@ public class PNGExporter implements VectorExporter, ByteExporter, LongTask {
|
||||
private boolean transparentBackground = false;
|
||||
private int margin = 4;
|
||||
private ProcessingTarget target;
|
||||
|
||||
|
||||
|
||||
private int extraMargin() {
|
||||
|
||||
Progress.start(progress);
|
||||
|
||||
PreviewController controller = Lookup.getDefault().lookup(PreviewController.class);
|
||||
controller.getModel(workspace).getProperties().putValue(PreviewProperty.VISIBILITY_RATIO, 1.0);
|
||||
PreviewProperties props = controller.getModel(workspace).getProperties();
|
||||
|
||||
int extraMargin = (int) Math.min(50, 0.05 * Math.max(width, height));
|
||||
System.out.println("@Var: extraMargin: "+extraMargin);
|
||||
props.putValue("width", width - 2 * extraMargin);
|
||||
props.putValue("height", height - 2 * extraMargin);
|
||||
props.putValue("margin", extraMargin);
|
||||
|
||||
controller.refreshPreview(workspace);
|
||||
target = (ProcessingTarget) controller.getRenderTarget(RenderTarget.PROCESSING_TARGET, workspace);
|
||||
target.refresh();
|
||||
|
||||
// transparent color
|
||||
Color transparentColor = new Color(255, 255, 255, 0);
|
||||
int transparentColorInt = transparentColor.getRGB();
|
||||
|
||||
// searching for external pixels
|
||||
PGraphicsJava2D pg2 = (PGraphicsJava2D) target.getGraphics();
|
||||
System.out.println("@Var: pg2: "+pg2);
|
||||
|
||||
boolean flag = false;
|
||||
int newMargin = extraMargin;
|
||||
System.out.println(pg2.pixels.length);
|
||||
for (int window = 0; window < extraMargin; window++) {
|
||||
// up
|
||||
if (flag) {
|
||||
newMargin = window;
|
||||
break;
|
||||
}
|
||||
for (int i = 0; i < width; i++) {
|
||||
if (pg2.pixels[window * width + i] != transparentColorInt) {
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// bottom
|
||||
if (flag) {
|
||||
newMargin = window;
|
||||
break;
|
||||
}
|
||||
for (int i = 0; i < width; i++) {
|
||||
if (pg2.pixels[height - 1 - window * width + i] != transparentColorInt) {
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// left
|
||||
if (flag) {
|
||||
newMargin = window;
|
||||
break;
|
||||
}
|
||||
for (int i = 0; i < height; i++) {
|
||||
if (pg2.pixels[i * width + window] != transparentColorInt) {
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// right
|
||||
if (flag) {
|
||||
newMargin = window;
|
||||
break;
|
||||
}
|
||||
for (int i = 0; i < height; i++) {
|
||||
if (pg2.pixels[i * width + height - 1 + window] != transparentColorInt) {
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
props.removeSimpleValue("width");
|
||||
props.removeSimpleValue("height");
|
||||
props.removeSimpleValue("margin");
|
||||
|
||||
return extraMargin - newMargin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute() {
|
||||
Progress.start(progress);
|
||||
|
||||
|
||||
PreviewController controller = Lookup.getDefault().lookup(PreviewController.class);
|
||||
controller.getModel(workspace).getProperties().putValue(PreviewProperty.VISIBILITY_RATIO, 1.0);
|
||||
|
||||
|
||||
int newMargin = extraMargin();
|
||||
System.out.println("@Var: newMargin: "+newMargin);
|
||||
|
||||
|
||||
PreviewProperties props = controller.getModel(workspace).getProperties();
|
||||
props.putValue("width", width);
|
||||
props.putValue("height", height);
|
||||
Color oldColor = props.getColorValue(PreviewProperty.BACKGROUND_COLOR);
|
||||
Color transparentColor = new Color(255, 255, 255, 0);
|
||||
int transparentColorInt = transparentColor.getRGB();
|
||||
if (transparentBackground) {
|
||||
props.putValue(PreviewProperty.BACKGROUND_COLOR, new Color(255, 255, 255, 0));//White transparent
|
||||
props.putValue(PreviewProperty.BACKGROUND_COLOR, transparentColor);//White transparent
|
||||
}
|
||||
props.putValue(PreviewProperty.MARGIN, new Float((float) margin));
|
||||
controller.refreshPreview(workspace);
|
||||
target = (ProcessingTarget) controller.getRenderTarget(RenderTarget.PROCESSING_TARGET, workspace);
|
||||
target.refresh();
|
||||
|
||||
|
||||
if (target instanceof LongTask) {
|
||||
((LongTask) target).setProgressTicket(progress);
|
||||
}
|
||||
@@ -100,40 +201,34 @@ public class PNGExporter implements VectorExporter, ByteExporter, LongTask {
|
||||
props.removeSimpleValue("width");
|
||||
props.removeSimpleValue("height");
|
||||
props.removeSimpleValue(PreviewProperty.MARGIN);
|
||||
|
||||
try {
|
||||
target.refresh();
|
||||
|
||||
Progress.switchToIndeterminate(progress);
|
||||
|
||||
PGraphicsJava2D pg2 = (PGraphicsJava2D) target.getGraphics();
|
||||
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
||||
img.setRGB(0, 0, width, height, pg2.pixels, 0, width);
|
||||
ImageIO.write(img, "png", stream);
|
||||
stream.close();
|
||||
|
||||
props.putValue(PreviewProperty.BACKGROUND_COLOR, oldColor);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
Progress.finish(progress);
|
||||
|
||||
return !cancel;
|
||||
}
|
||||
|
||||
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
|
||||
public void setHeight(int height) {
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
|
||||
public void setWidth(int width) {
|
||||
this.width = width;
|
||||
}
|
||||
@@ -153,22 +248,22 @@ public class PNGExporter implements VectorExporter, ByteExporter, LongTask {
|
||||
public void setTransparentBackground(boolean transparentBackground) {
|
||||
this.transparentBackground = transparentBackground;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setWorkspace(Workspace workspace) {
|
||||
this.workspace = workspace;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Workspace getWorkspace() {
|
||||
return workspace;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setOutputStream(OutputStream stream) {
|
||||
this.stream = stream;
|
||||
}
|
||||
|
||||
|
||||
public boolean cancel() {
|
||||
cancel = true;
|
||||
if (target instanceof LongTask) {
|
||||
@@ -176,7 +271,7 @@ public class PNGExporter implements VectorExporter, ByteExporter, LongTask {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public void setProgressTicket(ProgressTicket progressTicket) {
|
||||
this.progress = progressTicket;
|
||||
}
|
||||
|
||||
+1400
Diferenças do arquivo suprimidas por serem muito extensas
Carregar Diff
+68
@@ -0,0 +1,68 @@
|
||||
package org.gephi.io.exporter.preview;
|
||||
|
||||
import org.gephi.desktop.welcome.WelcomeTopComponent;
|
||||
import org.gephi.io.exporter.spi.ByteExporter;
|
||||
import org.gephi.io.importer.api.Container;
|
||||
import org.gephi.io.importer.api.ImportController;
|
||||
import org.gephi.io.importer.spi.FileImporter;
|
||||
import org.gephi.io.processor.plugin.DefaultProcessor;
|
||||
import org.gephi.project.api.ProjectController;
|
||||
import org.gephi.project.api.Workspace;
|
||||
import org.junit.Test;
|
||||
import org.openide.util.Lookup;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
* User: edubecks
|
||||
* Date: 5/2/13
|
||||
* Time: 6:25 PM
|
||||
* To change this template use File | Settings | File Templates.
|
||||
*/
|
||||
public class PNGExporterTest {
|
||||
|
||||
@Test
|
||||
public void testExecute() throws Exception {
|
||||
|
||||
//Init a project - and therefore a workspace
|
||||
|
||||
ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
|
||||
pc.newProject();
|
||||
Workspace workspace = pc.getCurrentWorkspace();
|
||||
|
||||
//Append container to graph structure
|
||||
String sample = "/org/gephi/desktop/welcome/samples/Les Miserables.gexf";
|
||||
|
||||
String fileString = "/org/gephi/desktop/welcome/samples/graph.gexf";
|
||||
File file = new File(fileString);
|
||||
|
||||
|
||||
final InputStream stream = WelcomeTopComponent.class.getResourceAsStream(fileString);
|
||||
System.out.println("@Var: stream: "+stream);
|
||||
|
||||
// final InputStream stream = new FileInputStream("/Users/eduBecKs/Desktop/partitions.gephi");
|
||||
ImportController importController = Lookup.getDefault().lookup(ImportController.class);
|
||||
FileImporter fileImporter = importController.getFileImporter(".gexf");
|
||||
// FileImporter fileImporter = importController.getFileImporter(new File("/Users/eduBecKs/Desktop/partitions.gephi"));
|
||||
System.out.println("@Var: fileImporter: " + fileImporter);
|
||||
|
||||
Container container = importController.importFile(stream, fileImporter);
|
||||
|
||||
importController.process(container, new DefaultProcessor(), workspace);
|
||||
|
||||
|
||||
PNGExporter fileExporter = new PNGExporter();
|
||||
fileExporter.setHeight(1024);
|
||||
fileExporter.setWidth(1024);
|
||||
fileExporter.setMargin(0);
|
||||
fileExporter.setTransparentBackground(true);
|
||||
fileExporter.setWorkspace(workspace);
|
||||
File fileOutput = new File("/Volumes/datos/temp/test.png");
|
||||
OutputStream outStream = new BufferedOutputStream(new FileOutputStream(fileOutput));
|
||||
((ByteExporter) fileExporter).setOutputStream(outStream);
|
||||
fileExporter.execute();
|
||||
System.out.println("Holaaa");
|
||||
|
||||
}
|
||||
}
|
||||
Arquivo binário não exibido.
Referência em uma Nova Issue
Bloquear um usuário