Skip to content

Commit

Permalink
Fix background layer with opacity too dark
Browse files Browse the repository at this point in the history
If we have a background layer with a non 100% opacity, we don't want it to
be darker, but lighter. That would give us the same behavior as in the client.
For that, we need to stop using the default black background, and use a white one.
  • Loading branch information
Patrick Valsecchi committed Apr 4, 2017
1 parent 43e80f6 commit f413ed8
Showing 1 changed file with 60 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,27 @@
*/
public final class CreateMapProcessor extends AbstractProcessor<CreateMapProcessor.Input, CreateMapProcessor.Output> {
private static final Logger LOGGER = LoggerFactory.getLogger(CreateMapProcessor.class);

enum BufferedImageType {
TYPE_4BYTE_ABGR(BufferedImage.TYPE_4BYTE_ABGR),
TYPE_4BYTE_ABGR_PRE(BufferedImage.TYPE_4BYTE_ABGR_PRE),
TYPE_3BYTE_BGR(BufferedImage.TYPE_3BYTE_BGR),
TYPE_BYTE_BINARY(BufferedImage.TYPE_BYTE_BINARY),
TYPE_BYTE_GRAY(BufferedImage.TYPE_BYTE_GRAY),
TYPE_BYTE_INDEXED(BufferedImage.TYPE_BYTE_INDEXED),
TYPE_INT_BGR(BufferedImage.TYPE_INT_BGR),
TYPE_INT_RGB(BufferedImage.TYPE_INT_RGB),
TYPE_INT_ARGB(BufferedImage.TYPE_INT_ARGB),
TYPE_INT_ARGB_PRE(BufferedImage.TYPE_INT_ARGB_PRE),
TYPE_USHORT_555_RGB(BufferedImage.TYPE_USHORT_555_RGB),
TYPE_USHORT_565_RGB(BufferedImage.TYPE_USHORT_565_RGB),
TYPE_USHORT_GRAY(BufferedImage.TYPE_USHORT_GRAY);
TYPE_4BYTE_ABGR(BufferedImage.TYPE_4BYTE_ABGR, true),
TYPE_4BYTE_ABGR_PRE(BufferedImage.TYPE_4BYTE_ABGR_PRE, true),
TYPE_3BYTE_BGR(BufferedImage.TYPE_3BYTE_BGR, false),
TYPE_BYTE_BINARY(BufferedImage.TYPE_BYTE_BINARY, false),
TYPE_BYTE_GRAY(BufferedImage.TYPE_BYTE_GRAY, false),
TYPE_BYTE_INDEXED(BufferedImage.TYPE_BYTE_INDEXED, false),
TYPE_INT_BGR(BufferedImage.TYPE_INT_BGR, false),
TYPE_INT_RGB(BufferedImage.TYPE_INT_RGB, false),
TYPE_INT_ARGB(BufferedImage.TYPE_INT_ARGB, true),
TYPE_INT_ARGB_PRE(BufferedImage.TYPE_INT_ARGB_PRE, true),
TYPE_USHORT_555_RGB(BufferedImage.TYPE_USHORT_555_RGB, false),
TYPE_USHORT_565_RGB(BufferedImage.TYPE_USHORT_565_RGB, false),
TYPE_USHORT_GRAY(BufferedImage.TYPE_USHORT_GRAY, false);
private final int value;
private final boolean transparency;

private BufferedImageType(final int value) {
BufferedImageType(final int value, final boolean transparency) {
this.value = value;
this.transparency = transparency;
}

static BufferedImageType lookupValue(final String name) {
Expand All @@ -135,15 +137,15 @@ static BufferedImageType lookupValue(final String name) {

@Autowired
FeatureLayer.Plugin featureLayerPlugin;

@Autowired
private MetricRegistry metricRegistry;

@Resource(name = "requestForkJoinPool")
private ForkJoinPool requestForkJoinPool;

private BufferedImageType imageType = BufferedImageType.TYPE_4BYTE_ABGR;

private BufferedImageType jpegImageType = BufferedImageType.TYPE_3BYTE_BGR;

/**
Expand All @@ -169,10 +171,10 @@ public Output execute(final Input param, final ExecutionContext context) throws
final List<URI> graphics = createLayerGraphics(param.tempTaskDirectory, param.clientHttpRequestFactory,
mapValues, context, mapContext);
checkCancelState(context);

final URI mapSubReport;
if (param.map.getTemplate().isMapExport()) {
mapSubReport = createMergedGraphic(param.tempTaskDirectory, mapValues.getMapSize(), graphics, mapContext, param.outputFormat);
mapSubReport = createMergedGraphic(param.tempTaskDirectory, graphics, mapContext, param.outputFormat);
} else {
mapSubReport = createMapSubReport(param.tempTaskDirectory, mapValues.getMapSize(), graphics, mapValues.getDpi());
}
Expand All @@ -186,17 +188,16 @@ protected void extraValidation(final List<Throwable> validationErrors, final Con
validationErrors.add(new ConfigurationException("No imageType defined in " + getClass().getName()));
}
}

private URI createMergedGraphic(final File printDirectory,
final Dimension mapSize,
final List<URI> graphics,
final MapfishMapContext mapContext,
final String outputFormat) throws IOException, JRException {

final List<URI> graphics,
final MapfishMapContext mapContext,
final String outputFormat) throws IOException, JRException {

final File mergedGraphic = File.createTempFile("map-", "." + outputFormat, printDirectory);
int width = (int) Math.round(mapContext.getMapSize().width);
int height = (int) Math.round(mapContext.getMapSize().height);
int width = Math.round(mapContext.getMapSize().width);
int height = Math.round(mapContext.getMapSize().height);

if ("pdf".equalsIgnoreCase(outputFormat)) {
com.lowagie.text.Document document = new com.lowagie.text.Document(
new com.lowagie.text.Rectangle(width, height));
Expand All @@ -219,7 +220,7 @@ private URI createMergedGraphic(final File printDirectory,
}
} else {
boolean isJpeg = RenderType.fromFileExtension(outputFormat) == RenderType.JPEG;
final BufferedImage bufferedImage = new BufferedImage(width, height,
final BufferedImage bufferedImage = new BufferedImage(width, height,
(isJpeg ? this.jpegImageType.value : this.imageType.value));
Graphics g = bufferedImage.getGraphics();
if (isJpeg) {
Expand All @@ -233,10 +234,10 @@ private URI createMergedGraphic(final File printDirectory,
}
ImageIO.write(bufferedImage, outputFormat, mergedGraphic);
}

return mergedGraphic.toURI();
}

private void drawGraphics(final int width, final int height,
final List<URI> graphics, final Graphics g) throws IOException, JRException {
for (URI graphic : graphics) {
Expand Down Expand Up @@ -266,23 +267,23 @@ private URI createMapSubReport(final File printDirectory,

return compiledReport.toURI();
}

private RenderType getSupportedRenderType(final RenderType renderType) {
if (renderType == RenderType.UNKNOWN || renderType == RenderType.TIFF) {
return RenderType.PNG;
} else {
return renderType;
}
}

private void warnIfDifferentRenderType(final RenderType renderType, final MapLayer layer) {
if (renderType != layer.getRenderType()) {
LOGGER.warn(
"Layer " + layer.getName() + " has " +
layer.getRenderType().toString() + " format, storing as PNG.");
}
}

private MapfishMapContext getTransformer(final MapfishMapContext mapContext, final double imageBufferScaling) {
return new MapfishMapContext(
mapContext,
Expand All @@ -298,7 +299,7 @@ private MapfishMapContext getTransformer(final MapfishMapContext mapContext, fin
mapContext.isDpiSensitiveStyle()
);
}

private List<URI> createLayerGraphics(final File printDirectory,
final MfClientHttpRequestFactory clientHttpRequestFactory,
final MapAttribute.MapAttributeValues mapValues,
Expand All @@ -309,28 +310,27 @@ private List<URI> createLayerGraphics(final File printDirectory,
final List<MapLayer> layers = Lists.reverse(Lists.newArrayList(mapValues.getLayers()));

final AreaOfInterest areaOfInterest = addAreaOfInterestLayer(mapValues, layers);

final String mapKey = UUID.randomUUID().toString();
final List<URI> graphics = new ArrayList<URI>(layers.size());

HttpRequestCache cache = new HttpRequestCache(printDirectory, this.metricRegistry);

//prepare layers for rendering
for (int i = 0; i < layers.size(); i++) {
final MapLayer layer = layers.get(i);
layer.prepareRender(mapContext);
final MapfishMapContext transformer = getTransformer(mapContext,
for (final MapLayer layer : layers) {
layer.prepareRender(mapContext);
final MapfishMapContext transformer = getTransformer(mapContext,
layer.getImageBufferScaling());
layer.cacheResources(cache, clientHttpRequestFactory, transformer);
}

//now we download and cache all images at once
cache.cache(this.requestForkJoinPool);

for (int i = 0; i < layers.size(); i++) {
MapLayer layer = layers.get(i);
checkCancelState(context);
File path = null;
final File path;
RenderType renderType = getSupportedRenderType(layer.getRenderType());
if (layer.getRenderType() == RenderType.SVG) {
// render layer as SVG
Expand All @@ -350,15 +350,24 @@ private List<URI> createLayerGraphics(final File printDirectory,
// render layer as raster graphic
warnIfDifferentRenderType(renderType, layer);
final double imageBufferScaling = layer.getImageBufferScaling();
final BufferedImageType layerImageType =
renderType == RenderType.JPEG ? this.jpegImageType : this.imageType;
final BufferedImage bufferedImage = new BufferedImage(
(int) Math.round(mapContext.getMapSize().width * imageBufferScaling),
(int) Math.round(mapContext.getMapSize().height * imageBufferScaling),
renderType == RenderType.JPEG ? this.jpegImageType.value : this.imageType.value
layerImageType.value
);
Graphics2D graphics2D = createClippedGraphics(
mapContext, areaOfInterest,
bufferedImage.createGraphics()
);
if (!layerImageType.transparency) {
// the image is opaque and therefore needs a white background
final Color prevColor = graphics2D.getColor();
graphics2D.setColor(Color.WHITE);
graphics2D.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());
graphics2D.setColor(prevColor);
}

try {
MapfishMapContext transformer = getTransformer(mapContext, layer.getImageBufferScaling());
Expand All @@ -377,7 +386,7 @@ private List<URI> createLayerGraphics(final File printDirectory,
warnIfDifferentRenderType(renderType, layer);
layer.render(graphics2D, clientHttpRequestFactory, transformer);
}

path = new File(
printDirectory,
mapKey + "_layer_" + i + "." + renderType.toString().toLowerCase());
Expand All @@ -386,7 +395,7 @@ private List<URI> createLayerGraphics(final File printDirectory,
graphics2D.dispose();
}
}
graphics.add(path.toURI());
graphics.add(path.toURI());
}

return graphics;
Expand Down Expand Up @@ -647,9 +656,9 @@ private ReferencedEnvelope getFeatureBounds(
public void setImageType(final String imageType) {
this.imageType = BufferedImageType.lookupValue(imageType);
}

/**
* Set the type of buffered image rendered to for JPEG files.
* Set the type of buffered image rendered to for JPEG files.
* See {@link org.mapfish.print.processor.map.CreateMapProcessor.BufferedImageType}.
* <p></p>
* Default is {@link org.mapfish.print.processor.map.CreateMapProcessor.BufferedImageType#TYPE_3BYTE_BGR}.
Expand Down Expand Up @@ -680,7 +689,7 @@ public static class Input {
* The path to the temporary directory for the print task.
*/
public File tempTaskDirectory;

/**
* The output format.
*/
Expand Down

0 comments on commit f413ed8

Please sign in to comment.