Skip to content

Commit

Permalink
Support jpeg and jpg formats
Browse files Browse the repository at this point in the history
  • Loading branch information
BaharWeb authored and sbrunner committed Jan 26, 2024
1 parent e472ed6 commit 00ae85f
Show file tree
Hide file tree
Showing 104 changed files with 425 additions and 113 deletions.
10 changes: 8 additions & 2 deletions core/src/main/java/org/mapfish/print/ImageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ private ImageUtils() {
public static void writeImage(final BufferedImage im, final String formatName, final File output)
throws IOException {
if (!ImageIO.write(im, formatName, output)) {
throw new RuntimeException("Image format not supported: " + formatName);
throw new RuntimeException(
String.format(
"Image format '%s' not supported, supported format: %s",
formatName, String.join(", ", ImageIO.getWriterFormatNames())));
}
}

Expand All @@ -39,7 +42,10 @@ public static void writeImage(
final BufferedImage im, final String formatName, final OutputStream output)
throws IOException {
if (!ImageIO.write(im, formatName, output)) {
throw new RuntimeException("Image format not supported: " + formatName);
throw new RuntimeException(
String.format(
"Image format '%s' not supported, supported format: %s",
formatName, String.join(", ", ImageIO.getWriterFormatNames())));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.mapfish.print.output;

import static java.util.Map.entry;
import static org.mapfish.print.Constants.PDF_DPI;

import java.awt.BasicStroke;
Expand All @@ -10,6 +11,7 @@
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperPrintManager;
Expand All @@ -19,7 +21,16 @@
public final class JasperReportImageOutputFormat extends AbstractJasperReportOutputFormat
implements OutputFormat {

private int imageType = BufferedImage.TYPE_INT_ARGB;
// Use to get the image type from the output format
public static final Map<String, Integer> IMAGE_TYPES =
Map.ofEntries(
entry("png", BufferedImage.TYPE_4BYTE_ABGR),
entry("jpg", BufferedImage.TYPE_3BYTE_BGR),
entry("jpeg", BufferedImage.TYPE_3BYTE_BGR),
entry("tif", BufferedImage.TYPE_4BYTE_ABGR),
entry("tiff", BufferedImage.TYPE_4BYTE_ABGR),
entry("gif", BufferedImage.TYPE_4BYTE_ABGR),
entry("bmp", BufferedImage.TYPE_3BYTE_BGR));

private String fileSuffix;

Expand Down Expand Up @@ -49,13 +60,18 @@ protected void doExport(final OutputStream outputStream, final Print print)
final int separatorHeight = 1;
final int separatorHeightOnImage = (int) (separatorHeight * dpiRatio);

final int imageType = IMAGE_TYPES.get(getFileSuffix().toLowerCase());
BufferedImage reportImage =
new BufferedImage(
pageWidthOnImage,
numPages * pageHeightOnImage + (numPages - 1) * separatorHeightOnImage,
this.imageType);

imageType);
Graphics2D graphics2D = reportImage.createGraphics();
if (imageType != BufferedImage.TYPE_4BYTE_ABGR) {
graphics2D.setColor(Color.WHITE);
graphics2D.fillRect(0, 0, pageWidthOnImage, pageHeightOnImage);
}

try {
JasperPrintManager printManager = JasperPrintManager.getInstance(print.context);

Expand Down Expand Up @@ -89,13 +105,4 @@ protected void doExport(final OutputStream outputStream, final Print print)

ImageUtils.writeImage(reportImage, getFileSuffix(), outputStream);
}

/**
* One of {@link java.awt.image.BufferedImage} TYPE_ values.
*
* @param imageType the buffered image type to create.
*/
public void setImageType(final int imageType) {
this.imageType = imageType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.geotools.renderer.lite.RendererUtilities.worldToScreenTransform;
import static org.mapfish.print.Constants.PDF_DPI;
import static org.mapfish.print.output.JasperReportImageOutputFormat.IMAGE_TYPES;

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
Expand Down Expand Up @@ -305,12 +306,11 @@ private URI createMergedGraphic(
document.close();
}
} else {
boolean isJpeg = RenderType.fromFileExtension(outputFormat) == RenderType.JPEG;
final BufferedImage bufferedImage =
new BufferedImage(
width, height, isJpeg ? BufferedImage.TYPE_3BYTE_BGR : BufferedImage.TYPE_4BYTE_ABGR);

final int imageType = IMAGE_TYPES.get(outputFormat.toLowerCase());
final BufferedImage bufferedImage = new BufferedImage(width, height, imageType);
Graphics g = bufferedImage.getGraphics();
if (isJpeg) {
if (imageType != BufferedImage.TYPE_4BYTE_ABGR) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
}
Expand Down
16 changes: 12 additions & 4 deletions core/src/main/resources/mapfish-spring-config-output-formats.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<bean id="pdfOutputFormat" class="org.mapfish.print.output.JasperReportPDFOutputFormat" scope="prototype" />
<bean id="pngOutputFormat" class="org.mapfish.print.output.JasperReportImageOutputFormat" scope="prototype"
p:fileSuffix="png"/>
<bean id="jpgOutputFormat" class="org.mapfish.print.output.JasperReportImageOutputFormat" scope="prototype"
p:fileSuffix="jpg"/>
<bean id="jpegOutputFormat" class="org.mapfish.print.output.JasperReportImageOutputFormat" scope="prototype"
p:fileSuffix="jpeg"/>
<bean id="tifOutputFormat" class="org.mapfish.print.output.JasperReportImageOutputFormat" scope="prototype"
p:fileSuffix="tif"/>
<bean id="tiffOutputFormat" class="org.mapfish.print.output.JasperReportImageOutputFormat" scope="prototype"
Expand All @@ -17,16 +21,20 @@
p:fileSuffix="bmp"/>
<bean id="svgOutputFormat" class="org.mapfish.print.output.JasperReportSvgOutputFormat" scope="prototype" />

<bean id="pdfMapOutputFormat" class="org.mapfish.print.output.MapExportOutputFormat" scope="prototype"
p:fileSuffix="pdf" p:contentType="application/pdf" />
<bean id="pngMapOutputFormat" class="org.mapfish.print.output.MapExportOutputFormat" scope="prototype"
p:fileSuffix="png" p:contentType="image/png"/>
<bean id="jpgMapOutputFormat" class="org.mapfish.print.output.MapExportOutputFormat" scope="prototype"
p:fileSuffix="jpg" p:contentType="image/jpeg"/>
<bean id="jpegMapOutputFormat" class="org.mapfish.print.output.MapExportOutputFormat" scope="prototype"
p:fileSuffix="jpeg" p:contentType="image/jpeg"/>
<bean id="tiffMapOutputFormat" class="org.mapfish.print.output.MapExportOutputFormat" scope="prototype"
p:fileSuffix="tiff" p:contentType="image/tiff"/>
<bean id="tifMapOutputFormat" class="org.mapfish.print.output.MapExportOutputFormat" scope="prototype"
p:fileSuffix="tif" p:contentType="image/tiff" />
<bean id="pdfMapOutputFormat" class="org.mapfish.print.output.MapExportOutputFormat" scope="prototype"
p:fileSuffix="pdf" p:contentType="application/pdf" />
<bean id="tiffMapOutputFormat" class="org.mapfish.print.output.MapExportOutputFormat" scope="prototype"
p:fileSuffix="tiff" p:contentType="image/tiff"/>
<bean id="gifMapOutputFormat" class="org.mapfish.print.output.MapExportOutputFormat" scope="prototype"
p:fileSuffix="gif" p:contentType="image/gif" />
<bean id="bmpMapOutputFormat" class="org.mapfish.print.output.MapExportOutputFormat" scope="prototype"
p:fileSuffix="bmp" p:contentType="image/bmp" />
</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ public void testAllOutputFormats() throws Exception {
final PJsonObject requestData = loadJsonRequestData();

for (OutputFormat format : this.outputFormat.values()) {
if (format.getFileSuffix().equals("bmp")) {
// BMP does not support transparency
if ("bmp".equals(format.getFileSuffix())
|| "jpeg".equals(format.getFileSuffix())
|| "jpg".equals(format.getFileSuffix())) {
// BMP and JPEG do not support transparency
continue;
}
final OutputStream outputStream = new ByteArrayOutputStream();
Expand Down
21 changes: 11 additions & 10 deletions examples/src/test/java/org/mapfish/print/ExamplesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class ExamplesTest {
"classpath:mapfish-spring-application-context.xml";
public static final String TEST_SPRING_XML =
"classpath:test-http-request-factory-application-context.xml";
public static final String[] BITMAP_FORMATS = {"png", "jpeg", "tiff"};
public static final String[] BITMAP_FORMATS = {"bmp", "png", "jpeg", "tiff", "jpg", "tif"};
private static final Logger LOGGER = LoggerFactory.getLogger(ExamplesTest.class);
private static final String REQUEST_DATA_FILE = "requestData(-.*)?.json";
private static final String CONFIG_FILE = "config.yaml";
Expand Down Expand Up @@ -182,7 +182,7 @@ public void testAllExamples() {

for (File example : Objects.requireNonNull(examplesDir.listFiles())) {
if (example.isDirectory() && exampleFilter.matcher(example.getName()).matches()) {
testsRan += runExample(example, errors, true);
testsRan += runExample(example, errors);
}
}

Expand Down Expand Up @@ -228,11 +228,11 @@ private void reportErrors(final Map<String, Throwable> errors, final int testsRa
public void testPDFA() {
final File examplesDir = getFile(ExamplesTest.class, "/examples");
Map<String, Throwable> errors = new HashMap<>();
runExample(new File(examplesDir, "pdf_a_compliant"), errors, false);
runExample(new File(examplesDir, "pdf_a_compliant"), errors);
reportErrors(errors, 1);
}

private int runExample(File example, Map<String, Throwable> errors, boolean forceBitmap) {
private int runExample(File example, Map<String, Throwable> errors) {
int testsRan = 0;
try {
final File configFile = new File(example, CONFIG_FILE);
Expand Down Expand Up @@ -260,10 +260,6 @@ private int runExample(File example, Map<String, Throwable> errors, boolean forc

testsRan++;
String outputFormat = jsonSpec.getInternalObj().getString("outputFormat");
if (forceBitmap && !ArrayUtils.contains(BITMAP_FORMATS, outputFormat)) {
jsonSpec.getInternalObj().put("outputFormat", "png");
outputFormat = "png";
}

URL url =
new URL(
Expand Down Expand Up @@ -300,12 +296,14 @@ private int runExample(File example, Map<String, Throwable> errors, boolean forc
}

Map<String, String> content_types = new HashMap<String, String>();
content_types.put("pdf", "application/pdf");
content_types.put("png", "image/png");
content_types.put("jpg", "image/jpeg");
content_types.put("jpeg", "image/jpeg");
content_types.put("tif", "image/tiff");
content_types.put("tiff", "image/tiff");
content_types.put("pdf", "application/pdf");
content_types.put("gif", "image/gif");
content_types.put("bmp", "image/bmp");
Assert.equals(content_types.get(outputFormat), http.getHeaderField("Content-Type"));

BufferedImage image = ImageIO.read(connection.getInputStream());
Expand All @@ -319,7 +317,10 @@ private int runExample(File example, Map<String, Throwable> errors, boolean forc
new Exception("File not found: " + expectedOutput.toString()));
}

new ImageSimilarity(expectedOutput).assertSimilarity(image);
if (!"bmp".equals(outputFormat)) {
// BMP is not supported by ImageIO
new ImageSimilarity(expectedOutput).assertSimilarity(image);
}
}
}
} catch (Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"layout": "A4 landscape",
"outputFormat": "pdf",
"outputFormat": "png",
"attributes": {
"map": {
"projection": "EPSG:3857",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"layout": "A4 landscape",
"outputFormat": "pdf",
"outputFormat": "png",
"attributes": {
"map": {
"projection": "EPSG:3857",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"layout": "A4 landscape",
"outputFormat": "pdf",
"outputFormat": "png",
"attributes": {
"map": {
"projection": "EPSG:3857",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"layout": "PDF Preview",
"title": "dm[geo] map print",
"outputFilename": "dmgeo-print",
"outputFormat": "pdf",
"outputFormat": "png",
"attributes": {
"mainMap": {
"dpi": "72",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"layout": "PDF Preview portrait",
"title": "dm[geo] map print",
"outputFilename": "dmgeo-print",
"outputFormat": "pdf",
"outputFormat": "png",
"attributes": {
"mainMap": {
"dpi": "72",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"layout": "A4 landscape",
"outputFormat": "pdf",
"outputFormat": "png",
"attributes": {
"map": {
"projection": "EPSG:4326",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"layout": "A4 landscape",
"outputFormat": "pdf",
"outputFormat": "png",
"attributes": {
"map": {
"projection": "EPSG:3857",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"layout": "A4 portrait",
"outputFormat": "pdf",
"outputFormat": "png",
"attributes": {
"number": 45,
"name": "Hello",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,5 @@
}
},
"layout": "A4 landscape",
"outputFormat": "pdf"
"outputFormat": "png"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"layout": "A4 landscape",
"outputFormat": "pdf",
"outputFormat": "png",
"attributes": {
"title": "Report title",
"map": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"layout": "A4 portrait",
"outputFormat": "pdf",
"outputFormat": "png",
"attributes": {
"title": "Report title",
"map": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"layout": "A4 portrait",
"outputFormat": "pdf",
"outputFormat": "png",
"attributes": {
"title": "",
"comments": "",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"layout": "A4 portrait",
"outputFormat": "pdf",
"outputFormat": "png",
"attributes": {
"title": "Restaurants",
"comments": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras egestas, massa eget placerat fermentum, nunc massa facilisis enim, id eleifend orci lacus sed sem.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"layout": "A4 portrait",
"outputFormat": "pdf",
"outputFormat": "png",
"attributes": {
"map": {
"bbox": [100, -1, 106, 1.538],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"layout": "A4 landscape",
"outputFormat": "pdf",
"outputFormat": "png",
"attributes": {
"map": {
"projection": "EPSG:4326",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
]
}
},
"outputFormat": "pdf",
"outputFormat": "png",
"layout": "A4 landscape"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"layout": "A4 landscape",
"outputFormat": "pdf",
"outputFormat": "png",
"attributes": {
"map": {
"bbox": [4150836.3839982115, 7600898.092677928, 5703147.34969658, 8257645.039704161],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"layout": "A4 landscape",
"outputFormat": "pdf",
"outputFormat": "png",
"attributes": {
"map": {
"bbox": [4150836.3839982115, 7600898.092677928, 5703147.34969658, 8257645.039704161],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,5 +204,5 @@
}
},
"layout": "A4 landscape",
"outputFormat": "pdf"
"outputFormat": "png"
}
Original file line number Diff line number Diff line change
Expand Up @@ -271,5 +271,5 @@
}
},
"layout": "A4 landscape",
"outputFormat": "pdf"
"outputFormat": "png"
}
Loading

0 comments on commit 00ae85f

Please sign in to comment.