problem generating URI for PNG

User 870ab5b546

20-01-2016 19:26:07

We use inline URIs to generate molecule images. The user has the option to specify SVG or PNG. The generation of the PNG URI has broken. The code for generating the URI:


public static String getImage(boolean prefersPNG, String molStr,
long qFlags) {
final String SELF = "MolString.getImage: ";
String imageData = "";
final String format = (prefersPNG ? PNG : SVG);
if (!Utils.isEmpty(molStr)) try {
debugPrint(SELF + "molStr:\n", molStr);
final boolean isLewis = molStr.contains("Lewis ");
final Molecule mol = MolImporter.importMol(molStr);
final int[] dims = getBestAppletSize(mol, isLewis);
final String opts =
getImageOpts(format, dims, qFlags, isLewis);
MDocument mDoc = mol.getDocument();
if (mDoc == null) mDoc = new MDocument(mol);
imageData = (prefersPNG
? Utils.toString("<img src=\"data:image/", PNG, ";base64,",
Base64Coder.encode(toBinFormat(mDoc, opts)), "\" />")
: Utils.bytesToString(toBinFormat(mDoc, opts))
.replaceAll("\"clipPath1\"", "\"clipPath2\""));
debugPrint(SELF + "generated image src URI with options ", opts,
"; isLewis = ", isLewis);
debugPrintMRV(SELF + "image data:\n", imageData);
} catch (MolFormatException e) {
Utils.alwaysPrint(SELF + "caught MolFormatException converting "
+ "to format ", format, " on:\n", molStr);
e.printStackTrace();
} // try
return imageData;
} // getImage(boolean, String, long)

public static byte[] toBinFormat(MDocument mDoc, String format) {
try {
return MolExporter.exportToBinFormat(mDoc, format);
} catch (IOException e) {
// do nothing
}
return new byte[0];
} // toBinFormat(MDocument, String)

Example of log output:


INFO: MolString.getImage: molStr:
<?xml version="1.0" ?>
<cml>
<MDocument>
<MRectangle id="o1">
<MPoint x="-7.349999904632568" y="3.616666555404663" />
<MPoint x="-1.8083332777023315" y="3.616666555404663" />
<MPoint x="-1.8083332777023315" y="-2.0999999046325684" />
<MPoint x="-7.349999904632568" y="-2.0999999046325684" />
</MRectangle>
<MChemicalStruct>
<molecule molID="m1">
<atomArray
atomID="a1 a2 a3 a4 a5 a6"
elementType="C C C C C O"
x2="-5.737573810170493 -5.261760476837159 -3.721573810170492 -3.245760476837159 -4.491760476837158 -4.491760476837158"
y2="0.2399016678587596 -1.2244983321412406 -1.2244983321412406 0.2399016678587596 1.145235001192093 2.685235001192093"
/>
<bondArray>
<bond atomRefs2="a5 a1" order="1" />
<bond atomRefs2="a5 a4" order="1" />
<bond atomRefs2="a1 a2" order="1" />
<bond atomRefs2="a2 a3" order="1" />
<bond atomRefs2="a3 a4" order="1" />
<bond atomRefs2="a5 a6" order="2" />
</bondArray>
</molecule>
</MChemicalStruct>
<MRectangle id="o3">
<MPoint x="1.0762502551078796" y="3.616666555404663" />
<MPoint x="6.6179168820381165" y="3.616666555404663" />
<MPoint x="6.6179168820381165" y="-2.0999999046325684" />
<MPoint x="1.0762502551078796" y="-2.0999999046325684" />
</MRectangle>
<MPolyline id="o4" headLength="0.8" headWidth="0.5" tailFlags="1"
tailLength="0.8" tailWidth="0.5">
<MPoint x="-1.8083332777023315" y="0.7583333253860474" />
<MRectanglePoint pos="7" rectRef="o3" />
</MPolyline>
</MDocument>
</cml>

MolString.getBestAppletSize: scaling down to scale 16.501650165016503
MolString.getImage: generated image src URI with options png:w250,h113,valenceErrorVisible,H_heteroterm,cv_inChain,lp; isLewis = false
MolString.getImage: image data:
<img src="data:image/png;base64,[C@110fbe9" />

The SVG URIs are generated just fine. Do you have any ideas? I'm wondering if one of the options is no longer compatible with the PNG generation. We're using JChem/Marvin 15.11.23.

ChemAxon 5433b8e56b

21-01-2016 13:47:52

HI Bob,


for the first sight it seems to me that Utils.toString uses toString on its parameters, and if my assumption is right, Base64Coder.encode returns an array. That would explain the char[].toString like result in the generated String.


If I am wrong, can you please attach the Utils, and Base64Coder you use, or narrow down the code to a simpler reproduction case?


Regards,
IstvanĀ 

User 870ab5b546

21-01-2016 14:14:10

Ah, of course! You are right. Thanks for the advice.