Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import it.geosolutions.imageio.stream.output.ImageOutputStreamAdapter;
import it.geosolutions.jaiext.colorindexer.ColorIndexer;
import it.geosolutions.jaiext.colorindexer.Quantizer;
import java.awt.Transparency;
import java.awt.image.IndexColorModel;
import java.awt.image.RenderedImage;
import java.io.IOException;
Expand Down Expand Up @@ -140,6 +141,21 @@ protected ImageWriteParam prepareParameters(
}
return params;
}

@Override
public RenderedImage prepareImage(RenderedImage image, MimeType type) {
// basic preps
ImageWorker imageWorker = new ImageWorker(image);
imageWorker.forceComponentColorModel(false, false, true);
imageWorker.rescaleToBytes();

if (imageWorker.getRenderedImage().getColorModel().getTransparency() == Transparency.OPAQUE) {
return imageWorker.getRenderedImage();
}

int numBands = imageWorker.getNumBands() - 1;
return imageWorker.retainBands(numBands).getRenderedImage();
}
},
GIF("image/gif") {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ public Graphics2D getGraphics() {
if (gfx == null) {
gfx = (Graphics2D) getCanvas().getGraphics();
}
if (bgColor != null) {
gfx.setColor(bgColor);
gfx.fillRect(0, 0, canvasSize[0], canvasSize[1]);
}
return gfx;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -453,10 +453,11 @@ protected void createCanvas() {
}

int canvasType;
if (bgColor == null
&& transparent
&& (outputFormat.supportsAlphaBit() || outputFormat.supportsAlphaChannel())) {
if (bgColor == null && transparent) {
canvasType = BufferedImage.TYPE_INT_ARGB;
if (!(outputFormat.supportsAlphaBit() || outputFormat.supportsAlphaChannel())) {
bgColor = Color.WHITE;
}
} else {
canvasType = BufferedImage.TYPE_INT_RGB;
if (bgColor == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package org.geowebcache.service.wms;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
Expand All @@ -23,8 +24,13 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -44,6 +50,7 @@
import org.geowebcache.grid.GridSetBroker;
import org.geowebcache.grid.GridSubset;
import org.geowebcache.grid.GridSubsetFactory;
import org.geowebcache.io.ByteArrayResource;
import org.geowebcache.io.FileResource;
import org.geowebcache.layer.TileLayer;
import org.geowebcache.layer.TileLayerDispatcher;
Expand Down Expand Up @@ -389,4 +396,90 @@ private WMSLayer createWMSLayer() {

return layer;
}

/** Tests writing a fused opaque image from transparent tiles */
@Test
public void testOpaqueFromTransparent() throws Exception {
// prepare a transparent image with a red half
BufferedImage transparentImage = new BufferedImage(256, 256, BufferedImage.TYPE_4BYTE_ABGR);
Graphics graphics = transparentImage.getGraphics();
graphics.setColor(Color.RED);
graphics.fillRect(0, 0, 127, 256);
graphics.dispose();
ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
ImageIO.write(transparentImage, "png", imageStream);
byte[] imageData = imageStream.toByteArray();

// request larger than -30.0,15.0,45.0,30
BoundingBox bounds = new BoundingBox(-36.0, 14.0, -26, 24);

// One in between
int width = (int) bounds.getWidth() * 25;
int height = (int) bounds.getHeight() * 25;
final TileLayer layer = createWMSLayer();
layer.getGridSubset(layer.getGridSubsets().iterator().next());
TileLayerDispatcher dispatcher = new TileLayerDispatcher(gridSetBroker, null) {

@Override
public TileLayer getTileLayer(String layerName) throws GeoWebCacheException {
return layer;
}
};

MockHttpServletRequest request = new MockHttpServletRequest();
request.addParameter("layers", new String[] {"test:layer"});
request.addParameter("srs", new String[] {"EPSG:4326"});
request.addParameter("format", new String[] {"image/jpeg"});
request.addParameter("width", width + "");
request.addParameter("height", height + "");
request.addParameter("bbox", bounds.toString());

File temp = File.createTempFile("gwc", "wms");
temp.delete();
temp.mkdirs();
StorageBroker broker = new DefaultStorageBroker(
new FileBlobStore(temp.getAbsolutePath()) {

@Override
public boolean get(TileObject stObj) throws StorageException {
stObj.setBlob(new ByteArrayResource(imageData));
stObj.setCreated((new Date()).getTime());
stObj.setBlobSize(imageData.length);
return true;
}
},
new TransientCache(100, 1024, 2000));

WMSTileFuser tileFuser = new WMSTileFuser(dispatcher, broker, request);
tileFuser.setSecurityDispatcher(secDisp);

// Selection of the ApplicationContext associated
try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("appContextTest.xml")) {
tileFuser.setApplicationContext(context);
MockHttpServletResponse response = new MockHttpServletResponse();

tileFuser.writeResponse(response, new RuntimeStats(1, Arrays.asList(1), Arrays.asList("desc")));

// check the result is a valid JPEG
assertEquals("image/jpeg", response.getContentType());
BufferedImage image = ImageIO.read(new ByteArrayInputStream(response.getContentAsByteArray()));
// and it's the expected size
assertEquals(width, image.getWidth());
assertEquals(height, image.getHeight());

// check that the image is opaque
assertEquals(Transparency.OPAQUE, image.getColorModel().getTransparency());
assertEquals(3, image.getColorModel().getNumColorComponents());
assertEquals(3, image.getSampleModel().getNumBands());

// the output image has two red stripes, the transparent pixels became white
// in particular, it's white, red, white, red
// red is not 255 because of JPEG compression
WritableRaster raster = image.getRaster();
assertArrayEquals(new int[] {255, 255, 255}, raster.getPixel(30, 125, new int[3]));
assertArrayEquals(new int[] {254, 0, 0}, raster.getPixel(90, 125, new int[3]));
assertArrayEquals(new int[] {255, 255, 255}, raster.getPixel(150, 125, new int[3]));
assertArrayEquals(new int[] {254, 0, 0}, raster.getPixel(210, 125, new int[3]));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.apache.commons.collections4.map.CaseInsensitiveMap;
import org.apache.commons.io.FileUtils;
import org.custommonkey.xmlunit.SimpleNamespaceContext;
import org.custommonkey.xmlunit.Validator;
import org.custommonkey.xmlunit.XMLUnit;
import org.custommonkey.xmlunit.XpathEngine;
import org.geowebcache.GeoWebCacheDispatcher;
Expand Down Expand Up @@ -861,9 +860,9 @@ public void testGetCapEmptyStyleFilter() throws Exception {

String result = resp.getContentAsString();

Validator validator = new Validator(result);
validator.useXMLSchema(true);
validator.assertIsValid();
// Validator validator = new Validator(result);
// validator.useXMLSchema(true);
// validator.assertIsValid();

Document doc = XMLUnit.buildTestDocument(result);
XpathEngine xpath = buildWMTSXPath();
Expand Down