protected void writeImage(DataRaster raster, String formatSuffix, java.io.File file) throws java.io.IOException { BufferedImageRaster bufferedImageRaster = (BufferedImageRaster) raster; BufferedImage image = bufferedImageRaster.getBufferedImage(); ByteArrayOutputStream os = new ByteArrayOutputStream(); ByteArrayOutputStream osalpha = new ByteArrayOutputStream(); ImageIO.write(image, formatSuffix, os); try { InputStream input = new ByteArrayInputStream(os.toByteArray()); PNGDecoder decoder = new PNGDecoder(input); ByteBuffer buf = ByteBuffer.allocateDirect(3 * decoder.getWidth() * decoder.getHeight()); decoder.decode(buf, decoder.getWidth() * 3, Format.RGB); buf.flip(); // per NicoLino: // se guardi BufferedImage.TYPE_INT_ARGB la descrizione dice che i pixel sono scritti in // formato RGBA: // ----public static final int TYPE_INT_ARGB // ------Represents an image with 8-bit RGBA color components packed into integer pixels. 6 // ------The image has a DirectColorModel with alpha. The color data in this image is // considered not to be premultiplied with alpha. // Qundi mi pare abbastanza facile usare laclasse PNGDecoder2 che ha anche il formato // Format.RGBA // Per provare ti basta quindi andare a cercare i TODO che ho lasciato io dove ho forzato // l'immagine a OPAQUE (prima era forzato a TRANSPARENT). // Se funziona anche sui transparent è una figata e basta evitare che scriva i pkm per i layer // di overlay (basta mattere un parametro come il TRILOGIS della trasparenza) // Se non funziona fanculo.... // a memoria gli OPAQUE messi sono: BUFFEREDIMAGERASTER nella funzione doGetSubraster // e TRANSPARENT TILEDIMAGE PRODUCER protected DataRaster createDataRaster vedi un po String filenamepkm = file.getAbsolutePath().replace(".png", "") + ".pkm"; writeToFilePkm(buf, decoder.getWidth(), decoder.getHeight(), filenamepkm); if (image.getTransparency() == Transparency.TRANSLUCENT) { BufferedImage alpha = extractAlphaInline(image); ImageIO.write(alpha, formatSuffix, osalpha); InputStream inputa = new ByteArrayInputStream(osalpha.toByteArray()); PNGDecoder decodera = new PNGDecoder(inputa); ByteBuffer bufa = ByteBuffer.allocateDirect(3 * decodera.getWidth() * decodera.getHeight()); decodera.decode( bufa, decoder.getWidth() * 3, Format.RGB); // TODO maybe rgb is wrong since extract alpha creates bgr bufa.flip(); String filenamepkmalpha = file.getAbsolutePath().replace(".png", "") + "_alpha.pkm"; writeToFilePkm(bufa, decodera.getWidth(), decodera.getHeight(), filenamepkmalpha); // writeAlphaToFile(bufa,decodera.getWidth(), decodera.getHeight(), // file.getAbsolutePath()); } System.out.println( "Written PKM: '" + file.getParentFile().getParentFile().getName() + "/" + file.getParentFile().getName() + "/" + file.getName() + "'"); } catch (Exception e) { System.out.println( "Exception saving '" + file.getParentFile().getParentFile().getName() + "/" + file.getParentFile().getName() + "/" + file.getName() + "' pkm: " + e.getMessage()); } finally { if (os != null) { os.close(); } if (osalpha != null) { osalpha.close(); } } }
public ImageFormatter serviceRequest(IMapRequest req) throws IOException, WMSServiceException { ImageFormatter formatter = null; try { Sector reqExtent = (SingleFileLayer.this.isElevation) ? req.getExtentForElevationRequest() : req.getExtent(); if (null == this.intersects(reqExtent, SingleFileLayer.this.getBBox())) { String msg = Logging.getMessage( "WMS.Layer.OutOfCoverage", reqExtent.toString(), SingleFileLayer.this.getBBox().toString()); Logging.logger().severe(msg); throw new WMSServiceException(msg); } if (req.getHeight() <= 0 || req.getWidth() <= 0) { String msg = Logging.getMessage("generic.InvalidImageSize", req.getWidth(), req.getHeight()); Logging.logger().severe(msg); throw new WMSServiceException(msg); } DataRaster[] rasters = SingleFileLayer.this.rasters; if (null == rasters || 0 == rasters.length) { String msg = Logging.getMessage("nullValue.RasterIsNull"); Logging.logger().severe(msg); throw new WMSServiceException(msg); } double missingDataReplacement = (double) SingleFileLayer.this.nodataReplacement; try { String s = req.getBGColor(); if (null != s) { missingDataReplacement = Double.parseDouble(s); } } catch (Exception e) { missingDataReplacement = (double) SingleFileLayer.this.nodataReplacement; } DataRaster raster = rasters[0]; if (raster instanceof BufferedImageRaster) { BufferedImageRaster reqRaster = new BufferedImageRaster( req.getWidth(), req.getHeight(), Transparency.TRANSLUCENT, reqExtent); raster.drawOnTo(reqRaster); BufferedImage img = reqRaster.getBufferedImage(); this.makeNoDataTransparent( img, SingleFileLayer.this.nodataSignal, (short) missingDataReplacement); formatter = new BufferedImageFormatter(img); } else if (raster instanceof ByteBufferRaster) { AVList reqParams = new AVListImpl(); reqParams.setValue(AVKey.WIDTH, req.getWidth()); reqParams.setValue(AVKey.HEIGHT, req.getHeight()); reqParams.setValue(AVKey.SECTOR, reqExtent); reqParams.setValue( AVKey.BYTE_ORDER, AVKey.LITTLE_ENDIAN); // by default BIL is LITTLE ENDIAN reqParams.setValue(AVKey.PIXEL_FORMAT, AVKey.ELEVATION); String reqFormat = req.getFormat(); if (null != reqFormat && reqFormat.endsWith("32")) { reqParams.setValue(AVKey.DATA_TYPE, AVKey.FLOAT32); } else { reqParams.setValue(AVKey.DATA_TYPE, AVKey.INT16); } reqParams.setValue(AVKey.MISSING_DATA_REPLACEMENT, missingDataReplacement); ByteBufferRaster reqRaster = new ByteBufferRaster(req.getWidth(), req.getHeight(), reqExtent, reqParams); raster.drawOnTo(reqRaster); if (SingleFileLayer.this.convertFeetToMeters) { this.convertFeetToMeters(reqRaster); } formatter = new DataRasterFormatter(reqRaster); } else { String msg = Logging.getMessage( "generic.UnrecognizedImageSourceType", raster.getClass().getName()); Logging.logger().severe(msg); throw new WMSServiceException(msg); } } catch (WMSServiceException wmsse) { throw wmsse; } catch (Exception ex) { Logging.logger() .log( java.util.logging.Level.SEVERE, SingleFileLayer.this.getThreadId() + ex.getMessage(), ex); // throw new WMSServiceException( s ); } finally { } return formatter; }
protected void doDrawOnTo(BufferedImageRaster canvas) { Sector sector = this.getSector(); if (null == sector) { String message = Logging.getMessage("nullValue.SectorIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } if (!sector.intersects(canvas.getSector())) { return; } java.awt.Graphics2D g2d = null; java.awt.Shape prevClip = null; java.awt.Composite prevComposite = null; java.lang.Object prevInterpolation = null, prevAntialiasing = null; try { int canvasWidth = canvas.getWidth(); int canvasHeight = canvas.getHeight(); // Apply the transform that correctly maps the image onto the canvas. java.awt.geom.AffineTransform transform = this.computeSourceToDestTransform( this.getWidth(), this.getHeight(), this.getSector(), canvasWidth, canvasHeight, canvas.getSector()); AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR); Rectangle2D rect = op.getBounds2D(this.getBufferedImage()); int clipWidth = (int) Math.ceil((rect.getMaxX() >= canvasWidth) ? canvasWidth : rect.getMaxX()); int clipHeight = (int) Math.ceil((rect.getMaxY() >= canvasHeight) ? canvasHeight : rect.getMaxY()); if (clipWidth <= 0 || clipHeight <= 0) { return; } g2d = canvas.getGraphics(); prevClip = g2d.getClip(); prevComposite = g2d.getComposite(); prevInterpolation = g2d.getRenderingHint(RenderingHints.KEY_INTERPOLATION); prevAntialiasing = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING); // Set the alpha composite for appropriate alpha blending. g2d.setComposite(java.awt.AlphaComposite.SrcOver); g2d.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.drawImage(this.getBufferedImage(), transform, null); } // catch (java.awt.image.ImagingOpException ioe) // { // // If we catch a ImagingOpException, then the transformed image has a width or // height of 0. // // This indicates that there is no intersection between the source image and the // canvas, // // or the intersection is smaller than one pixel. // } // catch (java.awt.image.RasterFormatException rfe) // { // // If we catch a RasterFormatException, then the transformed image has a width or // height of 0. // // This indicates that there is no intersection between the source image and the // canvas, // // or the intersection is smaller than one pixel. // } catch (Throwable t) { String reason = WWUtil.extractExceptionReason(t); Logging.logger().log(java.util.logging.Level.SEVERE, reason, t); } finally { // Restore the previous clip, composite, and transform. try { if (null != g2d) { if (null != prevClip) g2d.setClip(prevClip); if (null != prevComposite) g2d.setComposite(prevComposite); if (null != prevInterpolation) g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, prevInterpolation); if (null != prevAntialiasing) g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, prevAntialiasing); } } catch (Throwable t) { Logging.logger().log(java.util.logging.Level.FINEST, WWUtil.extractExceptionReason(t), t); } } }