Skip to content
Snippets Groups Projects
Commit 36b6ac19 authored by David Hoksza's avatar David Hoksza
Browse files

trimming of blank spaces in converted image + max size of image

parent b0531d0e
No related branches found
No related tags found
3 merge requests!630WIP: Resolve "The privileges of a new user are not saved in some cases",!442Problem with exporting to celldesigner and parsing from celldesigner,!440Problem with exporting to celldesigner
Pipeline #6670 passed
......@@ -60,6 +60,8 @@ public class ConvertController extends BaseController {
public @ResponseBody ResponseEntity<byte[]> convertInputToImage(//
@PathVariable(value = "fromFormat") String fromFormat, //
@PathVariable(value = "toFormat") String toFormat, //
//@RequestParam(value = "targetWidth", defaultValue = "0") Double targetWidth, //
//@RequestParam(value = "targetHeight", defaultValue = "0") Double targetHeight, //
@RequestBody String body, //
@CookieValue(value = Configuration.AUTH_TOKEN) String token //
)
......
......@@ -51,7 +51,8 @@ public class ConvertRestImpl extends BaseRestImpl {
return IOUtils.toString(exporter.exportModelToInputStream(model));
}
public ByteArrayOutputStream converToImage(String token, String fromFormat, String toFormat, String input)
public ByteArrayOutputStream converToImage(String token, String fromFormat, String toFormat, String input,
Double targetWidth, Double targetHeight)
throws SecurityException, InvalidInputDataExecption,
SBMLException, InconsistentModelException, IOException,
ConverterException,XMLStreamException, DrawingException,
......@@ -59,12 +60,21 @@ public class ConvertRestImpl extends BaseRestImpl {
{
Model model = getModelParserByNameOrClass(fromFormat).createModel(createConvertParams(input));
AbstractImageGenerator generator = getImageGenerator(toFormat, createImageParams(model));
AbstractImageGenerator generator = getImageGenerator(toFormat, createImageParams(model, targetWidth, targetHeight));
ByteArrayOutputStream os = new ByteArrayOutputStream();
generator.saveToOutputStream(os);
return os;
}
public ByteArrayOutputStream converToImage(String token, String fromFormat, String toFormat, String input)
throws SecurityException, InvalidInputDataExecption,
SBMLException, InconsistentModelException, IOException,
ConverterException,XMLStreamException, DrawingException,
QueryException
{
return converToImage(token, fromFormat, toFormat, input, 0.0, 0.0);
}
public Map<String, Object> getInformation(String token){
Map<String, Object> info = new LinkedHashMap<>();
......@@ -151,28 +161,38 @@ public class ConvertRestImpl extends BaseRestImpl {
throw new QueryException("Image generator for extension " + extOrClass + " not available.");
}
private AbstractImageGenerator.Params createImageParams(Model model){
private AbstractImageGenerator.Params createImageParams(Model model, Double targetWidth, Double targetHeight){
//if (targetHeight == 0) targetHeight = 848.0;
//if (targetWidth == 0) targetWidth = 595.0;
Double targetHeight = 848.0;
Double targetWidth = 595.0;
Double padding = 5.0;
Double maxDim = 10000.0;
Double w = model.getWidth();
Double h = model.getHeight();
if (targetHeight == 0) targetHeight = Math.min(h, maxDim);
if (targetWidth == 0) targetWidth = Math.min(w, maxDim);
Double scale = targetWidth / w;
if (h * scale > targetHeight) {
scale = targetHeight / h;
}
Double wScaled = w*scale;
Double hScaled = h*scale;
return new AbstractImageGenerator.Params().//
model(model).//
width(targetWidth+padding).//
height(targetHeight+padding).//
width(wScaled + padding).//
height(hScaled + padding).//
scale(1/scale);
}
private ConverterParams createConvertParams(String input) {
}
private ConverterParams createConvertParams(String input) {
ConverterParams params = new ConverterParams();
InputStream is = new ByteArrayInputStream(input.getBytes());
......
package lcsb.mapviewer.api.convert;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
......@@ -12,6 +13,8 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.stream.XMLStreamException;
......@@ -150,14 +153,39 @@ public class ConvertRestImplTest extends RestTestFunctions {
}
}
@Test
public void testScaling() throws Exception {
try {
File file = new File("testFiles/convert/sample-cd.xml");
String content = FileUtils.readFileToString(file);
String result1 = convertRestImpl.converToImage(token, "CellDesigner_SBML", "svg", content, 50.0, 50.0).toString();
String result2 = convertRestImpl.converToImage(token, "CellDesigner_SBML", "svg", content, 100.0, 100.0).toString();
Pattern pattern = Pattern.compile(".*scale\\(([0-9]+\\.[0-9]+),([0-9]+\\.[0-9]+)\\).*");
Matcher matcher1 = pattern.matcher(result1);
Matcher matcher2 = pattern.matcher(result2);
assertTrue(matcher1.find());
assertTrue(matcher2.find());
assertFalse(matcher1.group(1).toString().equals(matcher2.group(1).toString()));
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
/*
@Test
public void test() throws Exception {
try {
File file = new File("testFiles/convert/pdmap_spring18_cd.xml");
File file = new File("testFiles/convert/sample-cd.xml");
String content = FileUtils.readFileToString(file);
ByteArrayOutputStream result = convertRestImpl.converToImage(token, "CellDesigner_SBML", "svg", content);
ByteArrayOutputStream result = convertRestImpl.converToImage(token, "CellDesigner_SBML", "png", content);
FileOutputStream outputStream = new FileOutputStream("testFiles/convert/pdmap_spring18.svg");
FileOutputStream outputStream = new FileOutputStream("testFiles/convert/sample-cd.png");
outputStream.write(result.toByteArray());
outputStream.close();
......@@ -166,6 +194,7 @@ public class ConvertRestImplTest extends RestTestFunctions {
throw e;
}
}
*/
@Test
public void testCelDesigner2Sbgn() throws Exception {
......@@ -173,7 +202,7 @@ public class ConvertRestImplTest extends RestTestFunctions {
File file = new File("testFiles/convert/sample-cd.xml");
String content = FileUtils.readFileToString(file);
String result = convertRestImpl.convert(token, "CellDesigner_SBML", "SBGN-ML", content).toString();
assertTrue(result.contains("<rect"));
assertTrue(result.contains("glyph class=\"complex\""));
} catch (Exception e) {
e.printStackTrace();
throw e;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment