/**
   * Create a new set of render settings, based on the given constant base mode and the provided
   * special rendering hints.
   *
   * <p>
   *
   * @param baseMode the base mode of the settings, for example {@link Simapi#RENDER_QUALITY}
   * @param hints the special rendering hints to use for image processing operations like scaling
   *     etc.
   */
  public RenderSettings(int baseMode, RenderingHints hints) {

    switch (baseMode) {
      case Simapi.RENDER_SPEED:
        m_hints = HINTS_SPEED;
        m_compressionQuality = 0.5f;
        m_isUseBlur = false;
        break;
      case Simapi.RENDER_MEDIUM:
        m_hints = HINTS_MEDIUM;
        m_compressionQuality = 0.75f;
        m_isUseBlur = false;
        break;
      case Simapi.RENDER_QUALITY:
      default:
        m_hints = HINTS_QUALITY;
        m_compressionQuality = 0.95f;
        m_isUseBlur = true;
        break;
    }
    if (hints != null) {
      // must create a new object to modify, otherwise constant values would be affected
      RenderingHints newHints = new RenderingHints(null);
      newHints.add(m_hints);
      newHints.add(hints);
      m_hints = newHints;
    }
    m_transparentReplaceColor = Color.WHITE;
    m_imageFilters = new ArrayList();
    m_maximumBlurSize = (2500 * 2500);
    m_threadNicePriority = Thread.MIN_PRIORITY;
  }
Beispiel #2
0
  public void setAntialias(boolean antiAlias) {
    if (antiAlias) {

      RenderingHints hints =
          new RenderingHints(
              RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
      hints.add(
          new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
      hints.add(
          new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
      // hints.add(new RenderingHints(RenderingHints.KEY_ALPHA_INTERPOLATION,
      // RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY));
      gc.setRenderingHints(hints);
    }
  }
 public static void setRendererHints(final Graphics2D graphics) {
   if (graphics == null) return;
   RenderingHints hints =
       new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
   hints.add(
       new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
   graphics.setRenderingHints(hints);
 }
 private static BufferedImage getGaussianBlur(int size, BufferedImage image) {
   KernelJAI kernel = Functions.getGaussKernel(size / 3.0);
   ParameterBlock pb = new ParameterBlock();
   pb.addSource(image);
   pb.add(kernel);
   RenderingHints hints =
       new RenderingHints(
           JAI.KEY_BORDER_EXTENDER, BorderExtender.createInstance(BorderExtender.BORDER_COPY));
   hints.add(JAIContext.noCacheHint);
   return JAI.create("LCSeparableConvolve", pb, hints).getAsBufferedImage();
 }
  public RoundedBorders(
      Insets aMargin, Insets aPadding, int aRadii, boolean aNoTopBevel, boolean aNoBottomBevel) {
    radii = aRadii;
    margin = aMargin;
    insets =
        new Insets(
            margin.top + aPadding.top,
            margin.left + aPadding.left,
            margin.bottom + aPadding.bottom,
            margin.right + aPadding.right);
    hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    hints.add(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED));

    noTopBevel = aNoTopBevel;
    noBottomBevel = aNoBottomBevel;

    clipFull = new Area(new Rectangle(0, 0, 2 * radii, 2 * radii));
    clipFull.subtract(new Area(new Ellipse2D.Float(0, 0, 2 * radii, 2 * radii)));
  }
  public void render(Graphics2D g2d, IProgressMonitor monitor) throws RenderException {
    try {
      final IRenderContext currentContext = getContext();
      currentContext.setStatus(ILayer.WAIT);
      CoordinateReferenceSystem destinationCRS = currentContext.getCRS();

      // the bounds of the visible area in world coordinates
      // get the envelope and the screen extent
      Envelope envelope = getRenderBounds();
      if (envelope == null || envelope.isNull()) {
        envelope = context.getImageBounds();
      }

      Point upperLeft =
          currentContext.worldToPixel(new Coordinate(envelope.getMinX(), envelope.getMinY()));
      Point bottomRight =
          currentContext.worldToPixel(new Coordinate(envelope.getMaxX(), envelope.getMaxY()));
      Rectangle screenSize = new Rectangle(upperLeft);
      screenSize.add(bottomRight);

      final IGeoResource resource = getContext().getGeoResource();
      if (resource == null || !resource.canResolve(JGrassMapGeoResource.class)) {
        return;
      }
      JGrassMapGeoResource grassMapGeoResource =
          resource.resolve(JGrassMapGeoResource.class, monitor);

      JGrassRegion fileWindow = new JGrassRegion(grassMapGeoResource.getFileWindow());
      JGrassMapsetGeoResource parent =
          (JGrassMapsetGeoResource) grassMapGeoResource.parent(new NullProgressMonitor());
      CoordinateReferenceSystem grassCrs = parent.getLocationCrs();
      JGrassRegion screenDrawWindow =
          new JGrassRegion(
              envelope.getMinX(),
              envelope.getMaxX(),
              envelope.getMinY(),
              envelope.getMaxY(),
              fileWindow.getRows(),
              fileWindow.getCols());

      // to intersect with the data window, we transform the screen window
      JGrassRegion reprojectedScreenDrawWindow = screenDrawWindow;
      if (!CRS.equalsIgnoreMetadata(destinationCRS, grassCrs)) {
        reprojectedScreenDrawWindow = screenDrawWindow.reproject(destinationCRS, grassCrs, true);
      }

      /*
       * if the map is not visible, do not render it
       */
      // JGrassRegion fileWindow = grassMapGeoResource.getFileWindow();
      Rectangle2D.Double fileRectDouble = fileWindow.getRectangle();
      Double reprojScreenRectangle = reprojectedScreenDrawWindow.getRectangle();
      if (!reprojScreenRectangle.intersects(fileRectDouble)) {
        getContext().setStatus(ILayer.DONE);
        getContext().setStatusMessage(THE_MAP_IS_OUTSIDE_OF_THE_VISIBLE_REGION);
        System.out.println(THE_MAP_IS_OUTSIDE_OF_THE_VISIBLE_REGION);
        return;
      }
      /*
       * we will draw only the intersection of the map in the display system = part of visible map
       */
      Rectangle2D drawMapRectangle =
          reprojectedScreenDrawWindow.getRectangle().createIntersection(fileRectDouble);
      // Rectangle2D drawMapRectangle = fileRectDouble.getBounds2D();
      // resolution is that of the file window
      double ewRes = fileWindow.getWEResolution();
      double nsRes = fileWindow.getNSResolution();
      if (fileRectDouble.getWidth() < ewRes || fileRectDouble.getHeight() < nsRes) {
        getContext().setStatus(ILayer.DONE);
        getContext().setStatusMessage(THE_MAP_IS_OUTSIDE_OF_THE_VISIBLE_REGION);
        System.out.println(THE_MAP_IS_OUTSIDE_OF_THE_VISIBLE_REGION);
        return;
      }
      MathTransform transform = CRS.findMathTransform(destinationCRS, grassCrs, true);
      Coordinate pixelSize = getContext().getPixelSize();

      Coordinate c1 = new Coordinate(envelope.getMinX(), envelope.getMinY());
      Coordinate c2 =
          new Coordinate(envelope.getMinX() + pixelSize.x, envelope.getMinY() + pixelSize.y);
      Envelope envy = new Envelope(c1, c2);
      Envelope envyTrans = JTS.transform(envy, transform);

      pixelSize = new Coordinate(envyTrans.getWidth(), envyTrans.getHeight());
      /*
       * if the resolution is higher of that of the screen, it doesn't make much sense to draw it
       * all. So for visualization we just use the screen resolution to do things faster.
       */
      if (ewRes < pixelSize.x) {
        ewRes = pixelSize.x;
      }
      if (nsRes < pixelSize.y) {
        nsRes = pixelSize.y;
      }
      fileWindow.setNSResolution(nsRes);
      fileWindow.setWEResolution(ewRes);
      nsRes = fileWindow.getNSResolution();
      ewRes = fileWindow.getWEResolution();
      /*
       * redefine the region of the map to be drawn
       */
      /*
       * snap the screen to fit into the active region grid. This is mandatory for the exactness
       * of the query of the pixels (ex. d.what.rast).
       */
      JGrassRegion activeWindow = grassMapGeoResource.getActiveWindow();
      Coordinate minXY =
          JGrassRegion.snapToNextHigherInRegionResolution(
              drawMapRectangle.getMinX(), drawMapRectangle.getMinY(), activeWindow);
      Coordinate maxXY =
          JGrassRegion.snapToNextHigherInRegionResolution(
              drawMapRectangle.getMaxX(), drawMapRectangle.getMaxY(), activeWindow);

      JGrassRegion drawMapRegion =
          new JGrassRegion(minXY.x, maxXY.x, minXY.y, maxXY.y, ewRes, nsRes);
      // JGrassRegion drawMapRegion = new JGrassRegion(drawMapRectangle.getMinX(),
      // drawMapRectangle.getMaxX(), drawMapRectangle.getMinY(), drawMapRectangle
      // .getMaxY(), ewRes, nsRes);
      JGrassMapEnvironment grassMapEnvironment = grassMapGeoResource.getjGrassMapEnvironment();
      GridCoverage2D coverage =
          JGrassCatalogUtilities.getGridcoverageFromGrassraster(grassMapEnvironment, drawMapRegion);
      coverage = coverage.view(ViewType.RENDERED);
      if (coverage != null) {

        // setting rendering hints
        RenderingHints hints = new RenderingHints(Collections.EMPTY_MAP);
        hints.add(
            new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED));
        hints.add(
            new RenderingHints(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE));
        hints.add(
            new RenderingHints(
                RenderingHints.KEY_ALPHA_INTERPOLATION,
                RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED));
        hints.add(
            new RenderingHints(
                RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_SPEED));
        hints.add(
            new RenderingHints(
                RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR));
        hints.add(
            new RenderingHints(
                RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE));
        hints.add(
            new RenderingHints(
                RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF));
        hints.add(new RenderingHints(JAI.KEY_INTERPOLATION, new InterpolationNearest()));
        g2d.addRenderingHints(hints);
        final TileCache tempCache = JAI.createTileCache();
        tempCache.setMemoryCapacity(16 * 1024 * 1024);
        tempCache.setMemoryThreshold(1.0f);
        hints.add(new RenderingHints(JAI.KEY_TILE_CACHE, tempCache));

        // draw

        AffineTransform worldToScreen =
            RendererUtilities.worldToScreenTransform(envelope, screenSize, destinationCRS);
        final GridCoverageRenderer paint =
            new GridCoverageRenderer(destinationCRS, envelope, screenSize, worldToScreen, hints);
        RasterSymbolizer rasterSymbolizer =
            CommonFactoryFinder.getStyleFactory(null).createRasterSymbolizer();

        paint.paint(g2d, coverage, rasterSymbolizer);

        tempCache.flush();

        // IBlackboard blackboard = context.getMap().getBlackboard();
        // String legendString = coverageReader.getLegendString();
        // String name = grassMapGeoResource.getTitle();
        // blackboard.putString(JGrassMapGeoResource.READERID + "#" + name, legendString);

      }

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      getContext().setStatus(ILayer.DONE);
      getContext().setStatusMessage(null);
    }
  }
Beispiel #7
0
  /**
   * Applies the band select operation to a grid coverage.
   *
   * @param cropEnvelope the target envelope; always not null
   * @param cropROI the target ROI shape; nullable
   * @param roiTolerance; as read from op's params
   * @param sourceCoverage is the source {@link GridCoverage2D} that we want to crop.
   * @param hints A set of rendering hints, or {@code null} if none.
   * @param sourceGridToWorldTransform is the 2d grid-to-world transform for the source coverage.
   * @return The result as a grid coverage.
   */
  private static GridCoverage2D buildResult(
      final GeneralEnvelope cropEnvelope,
      final Geometry cropROI,
      final double roiTolerance,
      final boolean forceMosaic,
      final Hints hints,
      final GridCoverage2D sourceCoverage,
      final AffineTransform sourceGridToWorldTransform) {

    //
    // Getting the source coverage and its child geolocation objects
    //
    final RenderedImage sourceImage = sourceCoverage.getRenderedImage();
    final GridGeometry2D sourceGridGeometry = ((GridGeometry2D) sourceCoverage.getGridGeometry());
    final GridEnvelope2D sourceGridRange = sourceGridGeometry.getGridRange2D();

    //
    // Now we try to understand if we have a simple scale and translate or a
    // more elaborated grid-to-world transformation n which case a simple
    // crop could not be enough, but we may need a more elaborated chain of
    // operation in order to do a good job. As an instance if we
    // have a rotation which is not multiple of PI/2 we have to use
    // the mosaic with a ROI
    //
    final boolean isSimpleTransform =
        CoverageUtilities.isSimpleGridToWorldTransform(sourceGridToWorldTransform, EPS);

    // Do we need to explode the Palette to RGB(A)?
    //
    int actionTaken = 0;

    // //
    //
    // Layout
    //
    // //
    final RenderingHints targetHints = new RenderingHints(null);
    if (hints != null) targetHints.add(hints);
    final ImageLayout layout = initLayout(sourceImage, targetHints);
    targetHints.put(JAI.KEY_IMAGE_LAYOUT, layout);

    //
    // prepare the processor to use for this operation
    //
    final JAI processor = OperationJAI.getJAI(targetHints);
    final boolean useProvidedProcessor = !processor.equals(JAI.getDefaultInstance());

    try {

      if (cropROI != null) {
        // replace the cropEnvelope with the envelope of the intersection
        // of the ROI and the cropEnvelope.
        // Remember that envelope(intersection(roi,cropEnvelope)) != intersection(cropEnvelope,
        // envelope(roi))
        final Polygon modelSpaceROI = FeatureUtilities.getPolygon(cropEnvelope, GFACTORY);
        Geometry intersection = IntersectUtils.intersection(cropROI, modelSpaceROI);
        Envelope2D e2d =
            JTS.getEnvelope2D(
                intersection.getEnvelopeInternal(), cropEnvelope.getCoordinateReferenceSystem());
        GeneralEnvelope ge = new GeneralEnvelope((org.opengis.geometry.Envelope) e2d);
        cropEnvelope.setEnvelope(ge);
      }

      // //
      //
      // Build the new range by keeping into
      // account translation of grid geometry constructor for respecting
      // OGC PIXEL-IS-CENTER ImageDatum assumption.
      //
      // //
      final AffineTransform sourceWorldToGridTransform = sourceGridToWorldTransform.createInverse();

      // //
      //
      // finalRasterArea will hold the smallest rectangular integer raster area that contains the
      // floating point raster
      // area which we obtain when applying the world-to-grid transform to the cropEnvelope. Note
      // that we need to intersect
      // such an area with the area covered by the source coverage in order to be sure we do not try
      // to crop outside the
      // bounds of the source raster.
      //
      // //
      final Rectangle2D finalRasterAreaDouble =
          XAffineTransform.transform(
              sourceWorldToGridTransform, cropEnvelope.toRectangle2D(), null);
      final Rectangle finalRasterArea = finalRasterAreaDouble.getBounds();

      // intersection with the original range in order to not try to crop outside the image bounds
      Rectangle.intersect(finalRasterArea, sourceGridRange, finalRasterArea);
      if (finalRasterArea.isEmpty())
        throw new CannotCropException(Errors.format(ErrorKeys.CANT_CROP));

      // //
      //
      // It is worth to point out that doing a crop the G2W transform
      // should not change while the envelope might change as
      // a consequence of the rounding of the underlying image datum
      // which uses integer factors or in case the G2W is very
      // complex. Note that we will always strive to
      // conserve the original grid-to-world transform.
      //
      // //

      // we do not have to crop in this case (should not really happen at
      // this time)
      if (finalRasterArea.equals(sourceGridRange) && isSimpleTransform && cropROI == null)
        return sourceCoverage;

      // //
      //
      // if I get here I have something to crop
      // using the world-to-grid transform for going from envelope to the
      // new grid range.
      //
      // //
      final double minX = finalRasterArea.getMinX();
      final double minY = finalRasterArea.getMinY();
      final double width = finalRasterArea.getWidth();
      final double height = finalRasterArea.getHeight();

      // //
      //
      // Check if we need to use mosaic or crop
      //
      // //
      final PlanarImage croppedImage;
      final ParameterBlock pbj = new ParameterBlock();
      pbj.addSource(sourceImage);
      java.awt.Polygon rasterSpaceROI = null;
      String operatioName = null;
      if (!isSimpleTransform || cropROI != null) {
        // /////////////////////////////////////////////////////////////////////
        //
        // We don't have a simple scale and translate transform, JAI
        // crop MAY NOT suffice. Let's decide whether or not we'll use
        // the Mosaic.
        //
        // /////////////////////////////////////////////////////////////////////
        Polygon modelSpaceROI = FeatureUtilities.getPolygon(cropEnvelope, GFACTORY);

        // //
        //
        // Now convert this polygon back into a shape for the source
        // raster space.
        //
        // //
        final List<Point2D> points = new ArrayList<Point2D>(5);
        rasterSpaceROI =
            FeatureUtilities.convertPolygonToPointArray(
                modelSpaceROI, ProjectiveTransform.create(sourceWorldToGridTransform), points);
        if (rasterSpaceROI == null || rasterSpaceROI.getBounds().isEmpty())
          if (finalRasterArea.isEmpty())
            throw new CannotCropException(Errors.format(ErrorKeys.CANT_CROP));
        final boolean doMosaic =
            forceMosaic
                ? true
                : decideJAIOperation(roiTolerance, rasterSpaceROI.getBounds2D(), points);
        if (doMosaic || cropROI != null) {
          // prepare the params for the mosaic
          final ROI[] roiarr;
          try {
            if (cropROI != null) {
              final LiteShape2 cropRoiLS2 =
                  new LiteShape2(
                      cropROI, ProjectiveTransform.create(sourceWorldToGridTransform), null, false);
              ROI cropRS = new ROIShape(cropRoiLS2);
              Rectangle2D rt = cropRoiLS2.getBounds2D();
              if (!hasIntegerBounds(rt)) {
                // Approximate Geometry
                Geometry geo = (Geometry) cropRoiLS2.getGeometry().clone();
                transformGeometry(geo);
                cropRS = new ROIShape(new LiteShape2(geo, null, null, false));
              }
              roiarr = new ROI[] {cropRS};
            } else {
              final ROIShape roi = new ROIShape(rasterSpaceROI);
              roiarr = new ROI[] {roi};
            }
          } catch (FactoryException ex) {
            throw new CannotCropException(Errors.format(ErrorKeys.CANT_CROP), ex);
          }
          pbj.add(MosaicDescriptor.MOSAIC_TYPE_OVERLAY);
          pbj.add(null);
          pbj.add(roiarr);
          pbj.add(null);
          pbj.add(CoverageUtilities.getBackgroundValues(sourceCoverage));

          // prepare the final layout
          final Rectangle bounds = rasterSpaceROI.getBounds2D().getBounds();
          Rectangle.intersect(bounds, sourceGridRange, bounds);
          if (bounds.isEmpty()) throw new CannotCropException(Errors.format(ErrorKeys.CANT_CROP));

          // we do not have to crop in this case (should not really happen at
          // this time)
          if (!doMosaic && bounds.getBounds().equals(sourceGridRange) && isSimpleTransform)
            return sourceCoverage;

          // nice trick, we use the layout to do the actual crop
          final Rectangle boundsInt = bounds.getBounds();
          layout.setMinX(boundsInt.x);
          layout.setWidth(boundsInt.width);
          layout.setMinY(boundsInt.y);
          layout.setHeight(boundsInt.height);
          operatioName = "Mosaic";
        }
      }

      // do we still have to set the operation name? If so that means we have to go for crop.
      if (operatioName == null) {
        // executing the crop
        pbj.add((float) minX);
        pbj.add((float) minY);
        pbj.add((float) width);
        pbj.add((float) height);
        operatioName = "GTCrop";
      }
      // //
      //
      // Apply operation
      //
      // //
      if (!useProvidedProcessor) {
        croppedImage = JAI.create(operatioName, pbj, targetHints);
      } else {
        croppedImage = processor.createNS(operatioName, pbj, targetHints);
      }

      // conserve the input grid to world transformation
      Map sourceProperties = sourceCoverage.getProperties();
      Map properties = null;
      if (sourceProperties != null && !sourceProperties.isEmpty()) {
        properties = new HashMap(sourceProperties);
      }
      if (rasterSpaceROI != null) {
        if (properties != null) {
          properties.put("GC_ROI", rasterSpaceROI);
        } else {
          properties = Collections.singletonMap("GC_ROI", rasterSpaceROI);
        }
      }

      return new GridCoverageFactory(hints)
          .create(
              sourceCoverage.getName(),
              croppedImage,
              new GridGeometry2D(
                  new GridEnvelope2D(croppedImage.getBounds()),
                  sourceGridGeometry.getGridToCRS2D(PixelOrientation.CENTER),
                  sourceCoverage.getCoordinateReferenceSystem()),
              (GridSampleDimension[])
                  (actionTaken == 1 ? null : sourceCoverage.getSampleDimensions().clone()),
              new GridCoverage[] {sourceCoverage},
              properties);

    } catch (TransformException e) {
      throw new CannotCropException(Errors.format(ErrorKeys.CANT_CROP), e);
    } catch (NoninvertibleTransformException e) {
      throw new CannotCropException(Errors.format(ErrorKeys.CANT_CROP), e);
    }
  }
    @Override
    public RenderedImage postProcessMosaic(
        RenderedImage mosaic, ROI overallROI, RenderingHints hints) {

      // force the current image in RGB or Gray
      final ImageWorker imageWorker = new ImageWorker(mosaic);
      hints = prepareHints(hints);
      imageWorker.setRenderingHints(hints);

      // make sure the mosaic image is either gray of RGB
      if (!imageWorker.isColorSpaceGRAYScale()) {
        if (!imageWorker.isColorSpaceRGB()) {
          imageWorker.forceColorSpaceRGB();
        }
      }
      imageWorker.forceComponentColorModel(); // todo optimize with paletted imagery

      // do we already have a alpha band in the input image?
      if (imageWorker.getRenderedImage().getColorModel().hasAlpha()) {
        // if so we reuse it applying the ROI on top of it
        RenderedImage alpha = imageWorker.retainLastBand().getRenderedImage();
        RenderedImage maskedAlpha =
            new ImageWorker(hints)
                .mosaic(
                    new RenderedImage[] {alpha},
                    MosaicDescriptor.MOSAIC_TYPE_OVERLAY,
                    null,
                    new ROI[] {overallROI},
                    null,
                    null)
                .getRenderedImage();

        imageWorker.retainBands(mosaic.getColorModel().getNumColorComponents());
        imageWorker.addBand(maskedAlpha, false, true, null);
      } else {

        // turn the roi into a single band image and add it to the mosaic as transparency
        final ImageWorker roiImageWorker = new ImageWorker(overallROI.getAsImage());
        roiImageWorker.setRenderingHints(hints);

        PlanarImage alpha =
            roiImageWorker.forceComponentColorModel().retainFirstBand().getPlanarImage();
        if (!alpha.getBounds().equals(imageWorker.getPlanarImage().getBounds())) {
          // build final layout and use it for giving the alpha band a simil size and tiling
          // to the one of the image
          final ImageLayout layout =
              new ImageLayout(
                  mosaic.getMinX(), mosaic.getMinY(), mosaic.getWidth(), mosaic.getHeight());

          //
          final SampleModel sampleModel = mosaic.getSampleModel();
          layout.setTileHeight(sampleModel.getWidth()).setTileWidth(sampleModel.getHeight());
          hints.add(new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout));

          // correct bounds of the current image
          alpha =
              new ImageWorker(hints)
                  .mosaic(
                      new RenderedImage[] {alpha},
                      MosaicDescriptor.MOSAIC_TYPE_OVERLAY,
                      null,
                      new ROI[] {overallROI},
                      null,
                      null)
                  .getRenderedOperation();
        }
        imageWorker.addBand(alpha, false, true, null);
      }

      RenderedImage result = imageWorker.getRenderedImage();
      return result;
    }
Beispiel #9
0
  /**
   * Load a specified a raster as a portion of the granule describe by this {@link
   * GranuleDescriptor}.
   *
   * @param imageReadParameters the {@link ImageReadParam} to use for reading.
   * @param index the index to use for the {@link ImageReader}.
   * @param cropBBox the bbox to use for cropping.
   * @param mosaicWorldToGrid the cropping grid to world transform.
   * @param request the incoming request to satisfy.
   * @param hints {@link Hints} to be used for creating this raster.
   * @return a specified a raster as a portion of the granule describe by this {@link
   *     GranuleDescriptor}.
   * @throws IOException in case an error occurs.
   */
  public GranuleLoadingResult loadRaster(
      final ImageReadParam imageReadParameters,
      final int index,
      final ReferencedEnvelope cropBBox,
      final MathTransform2D mosaicWorldToGrid,
      final RasterLayerRequest request,
      final Hints hints)
      throws IOException {

    if (LOGGER.isLoggable(java.util.logging.Level.FINER)) {
      final String name = Thread.currentThread().getName();
      LOGGER.finer(
          "Thread:" + name + " Loading raster data for granuleDescriptor " + this.toString());
    }
    ImageReadParam readParameters = null;
    int imageIndex;
    final ReferencedEnvelope bbox =
        inclusionGeometry != null
            ? new ReferencedEnvelope(
                granuleBBOX.intersection(inclusionGeometry.getEnvelopeInternal()),
                granuleBBOX.getCoordinateReferenceSystem())
            : granuleBBOX;
    boolean doFiltering = false;
    if (filterMe) {
      doFiltering = Utils.areaIsDifferent(inclusionGeometry, baseGridToWorld, granuleBBOX);
    }

    // intersection of this tile bound with the current crop bbox
    final ReferencedEnvelope intersection =
        new ReferencedEnvelope(
            bbox.intersection(cropBBox), cropBBox.getCoordinateReferenceSystem());
    if (intersection.isEmpty()) {
      if (LOGGER.isLoggable(java.util.logging.Level.FINE)) {
        LOGGER.fine(
            new StringBuilder("Got empty intersection for granule ")
                .append(this.toString())
                .append(" with request ")
                .append(request.toString())
                .append(" Resulting in no granule loaded: Empty result")
                .toString());
      }
      return null;
    }

    ImageInputStream inStream = null;
    ImageReader reader = null;
    try {
      //
      // get info about the raster we have to read
      //

      // get a stream
      assert cachedStreamSPI != null : "no cachedStreamSPI available!";
      inStream =
          cachedStreamSPI.createInputStreamInstance(
              granuleUrl, ImageIO.getUseCache(), ImageIO.getCacheDirectory());
      if (inStream == null) return null;

      // get a reader and try to cache the relevant SPI
      if (cachedReaderSPI == null) {
        reader = ImageIOExt.getImageioReader(inStream);
        if (reader != null) cachedReaderSPI = reader.getOriginatingProvider();
      } else reader = cachedReaderSPI.createReaderInstance();
      if (reader == null) {
        if (LOGGER.isLoggable(java.util.logging.Level.WARNING)) {
          LOGGER.warning(
              new StringBuilder("Unable to get s reader for granuleDescriptor ")
                  .append(this.toString())
                  .append(" with request ")
                  .append(request.toString())
                  .append(" Resulting in no granule loaded: Empty result")
                  .toString());
        }
        return null;
      }
      // set input
      reader.setInput(inStream);

      // Checking for heterogeneous granules
      if (request.isHeterogeneousGranules()) {
        // create read parameters
        readParameters = new ImageReadParam();

        // override the overviews controller for the base layer
        imageIndex =
            ReadParamsController.setReadParams(
                request.getRequestedResolution(),
                request.getOverviewPolicy(),
                request.getDecimationPolicy(),
                readParameters,
                request.rasterManager,
                overviewsController);
      } else {
        imageIndex = index;
        readParameters = imageReadParameters;
      }

      // get selected level and base level dimensions
      final GranuleOverviewLevelDescriptor selectedlevel = getLevel(imageIndex, reader);

      // now create the crop grid to world which can be used to decide
      // which source area we need to crop in the selected level taking
      // into account the scale factors imposed by the selection of this
      // level together with the base level grid to world transformation
      AffineTransform2D cropWorldToGrid =
          new AffineTransform2D(selectedlevel.gridToWorldTransformCorner);
      cropWorldToGrid = (AffineTransform2D) cropWorldToGrid.inverse();
      // computing the crop source area which lives into the
      // selected level raster space, NOTICE that at the end we need to
      // take into account the fact that we might also decimate therefore
      // we cannot just use the crop grid to world but we need to correct
      // it.
      final Rectangle sourceArea =
          CRS.transform(cropWorldToGrid, intersection).toRectangle2D().getBounds();
      // gutter
      if (selectedlevel.baseToLevelTransform.isIdentity()) sourceArea.grow(2, 2);
      XRectangle2D.intersect(
          sourceArea,
          selectedlevel.rasterDimensions,
          sourceArea); // make sure roundings don't bother us
      // is it empty??
      if (sourceArea.isEmpty()) {
        if (LOGGER.isLoggable(java.util.logging.Level.FINE)) {
          LOGGER.fine(
              "Got empty area for granuleDescriptor "
                  + this.toString()
                  + " with request "
                  + request.toString()
                  + " Resulting in no granule loaded: Empty result");
        }
        return null;

      } else if (LOGGER.isLoggable(java.util.logging.Level.FINER)) {
        LOGGER.finer(
            "Loading level "
                + imageIndex
                + " with source region: "
                + sourceArea
                + " subsampling: "
                + readParameters.getSourceXSubsampling()
                + ","
                + readParameters.getSourceYSubsampling()
                + " for granule:"
                + granuleUrl);
      }

      // Setting subsampling
      int newSubSamplingFactor = 0;
      final String pluginName = cachedReaderSPI.getPluginClassName();
      if (pluginName != null && pluginName.equals(ImageUtilities.DIRECT_KAKADU_PLUGIN)) {
        final int ssx = readParameters.getSourceXSubsampling();
        final int ssy = readParameters.getSourceYSubsampling();
        newSubSamplingFactor = ImageIOUtilities.getSubSamplingFactor2(ssx, ssy);
        if (newSubSamplingFactor != 0) {
          if (newSubSamplingFactor > maxDecimationFactor && maxDecimationFactor != -1) {
            newSubSamplingFactor = maxDecimationFactor;
          }
          readParameters.setSourceSubsampling(newSubSamplingFactor, newSubSamplingFactor, 0, 0);
        }
      }

      // set the source region
      readParameters.setSourceRegion(sourceArea);
      final RenderedImage raster;
      try {
        // read
        raster =
            request
                .getReadType()
                .read(
                    readParameters,
                    imageIndex,
                    granuleUrl,
                    selectedlevel.rasterDimensions,
                    reader,
                    hints,
                    false);

      } catch (Throwable e) {
        if (LOGGER.isLoggable(java.util.logging.Level.FINE)) {
          LOGGER.log(
              java.util.logging.Level.FINE,
              "Unable to load raster for granuleDescriptor "
                  + this.toString()
                  + " with request "
                  + request.toString()
                  + " Resulting in no granule loaded: Empty result",
              e);
        }
        return null;
      }

      // use fixed source area
      sourceArea.setRect(readParameters.getSourceRegion());

      //
      // setting new coefficients to define a new affineTransformation
      // to be applied to the grid to world transformation
      // -----------------------------------------------------------------------------------
      //
      // With respect to the original envelope, the obtained planarImage
      // needs to be rescaled. The scaling factors are computed as the
      // ratio between the cropped source region sizes and the read
      // image sizes.
      //
      // place it in the mosaic using the coords created above;
      double decimationScaleX = ((1.0 * sourceArea.width) / raster.getWidth());
      double decimationScaleY = ((1.0 * sourceArea.height) / raster.getHeight());
      final AffineTransform decimationScaleTranform =
          XAffineTransform.getScaleInstance(decimationScaleX, decimationScaleY);

      // keep into account translation  to work into the selected level raster space
      final AffineTransform afterDecimationTranslateTranform =
          XAffineTransform.getTranslateInstance(sourceArea.x, sourceArea.y);

      // now we need to go back to the base level raster space
      final AffineTransform backToBaseLevelScaleTransform = selectedlevel.baseToLevelTransform;

      // now create the overall transform
      final AffineTransform finalRaster2Model = new AffineTransform(baseGridToWorld);
      finalRaster2Model.concatenate(CoverageUtilities.CENTER_TO_CORNER);
      final double x = finalRaster2Model.getTranslateX();
      final double y = finalRaster2Model.getTranslateY();

      if (!XAffineTransform.isIdentity(backToBaseLevelScaleTransform, Utils.AFFINE_IDENTITY_EPS))
        finalRaster2Model.concatenate(backToBaseLevelScaleTransform);
      if (!XAffineTransform.isIdentity(afterDecimationTranslateTranform, Utils.AFFINE_IDENTITY_EPS))
        finalRaster2Model.concatenate(afterDecimationTranslateTranform);
      if (!XAffineTransform.isIdentity(decimationScaleTranform, Utils.AFFINE_IDENTITY_EPS))
        finalRaster2Model.concatenate(decimationScaleTranform);

      // keep into account translation factors to place this tile
      finalRaster2Model.preConcatenate((AffineTransform) mosaicWorldToGrid);
      final Interpolation interpolation = request.getInterpolation();
      // paranoiac check to avoid that JAI freaks out when computing its internal layouT on images
      // that are too small
      Rectangle2D finalLayout =
          ImageUtilities.layoutHelper(
              raster,
              (float) finalRaster2Model.getScaleX(),
              (float) finalRaster2Model.getScaleY(),
              (float) finalRaster2Model.getTranslateX(),
              (float) finalRaster2Model.getTranslateY(),
              interpolation);
      if (finalLayout.isEmpty()) {
        if (LOGGER.isLoggable(java.util.logging.Level.INFO))
          LOGGER.info(
              "Unable to create a granuleDescriptor "
                  + this.toString()
                  + " due to jai scale bug creating a null source area");
        return null;
      }
      ROI granuleLoadingShape = null;
      if (granuleROIShape != null) {

        final Point2D translate =
            mosaicWorldToGrid.transform(new DirectPosition2D(x, y), (Point2D) null);
        AffineTransform tx2 = new AffineTransform();
        tx2.preConcatenate(
            AffineTransform.getScaleInstance(
                ((AffineTransform) mosaicWorldToGrid).getScaleX(),
                -((AffineTransform) mosaicWorldToGrid).getScaleY()));
        tx2.preConcatenate(
            AffineTransform.getScaleInstance(
                ((AffineTransform) baseGridToWorld).getScaleX(),
                -((AffineTransform) baseGridToWorld).getScaleY()));
        tx2.preConcatenate(
            AffineTransform.getTranslateInstance(translate.getX(), translate.getY()));
        granuleLoadingShape = (ROI) granuleROIShape.transform(tx2);
      }
      // apply the affine transform  conserving indexed color model
      final RenderingHints localHints =
          new RenderingHints(
              JAI.KEY_REPLACE_INDEX_COLOR_MODEL,
              interpolation instanceof InterpolationNearest ? Boolean.FALSE : Boolean.TRUE);
      if (XAffineTransform.isIdentity(finalRaster2Model, Utils.AFFINE_IDENTITY_EPS)) {
        return new GranuleLoadingResult(raster, granuleLoadingShape, granuleUrl, doFiltering);
      } else {
        //
        // In case we are asked to use certain tile dimensions we tile
        // also at this stage in case the read type is Direct since
        // buffered images comes up untiled and this can affect the
        // performances of the subsequent affine operation.
        //
        final Dimension tileDimensions = request.getTileDimensions();
        if (tileDimensions != null && request.getReadType().equals(ReadType.DIRECT_READ)) {
          final ImageLayout layout = new ImageLayout();
          layout.setTileHeight(tileDimensions.width).setTileWidth(tileDimensions.height);
          localHints.add(new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout));
        } else {
          if (hints != null && hints.containsKey(JAI.KEY_IMAGE_LAYOUT)) {
            final Object layout = hints.get(JAI.KEY_IMAGE_LAYOUT);
            if (layout != null && layout instanceof ImageLayout) {
              localHints.add(
                  new RenderingHints(JAI.KEY_IMAGE_LAYOUT, ((ImageLayout) layout).clone()));
            }
          }
        }
        if (hints != null && hints.containsKey(JAI.KEY_TILE_CACHE)) {
          final Object cache = hints.get(JAI.KEY_TILE_CACHE);
          if (cache != null && cache instanceof TileCache)
            localHints.add(new RenderingHints(JAI.KEY_TILE_CACHE, (TileCache) cache));
        }
        if (hints != null && hints.containsKey(JAI.KEY_TILE_SCHEDULER)) {
          final Object scheduler = hints.get(JAI.KEY_TILE_SCHEDULER);
          if (scheduler != null && scheduler instanceof TileScheduler)
            localHints.add(new RenderingHints(JAI.KEY_TILE_SCHEDULER, (TileScheduler) scheduler));
        }
        boolean addBorderExtender = true;
        if (hints != null && hints.containsKey(JAI.KEY_BORDER_EXTENDER)) {
          final Object extender = hints.get(JAI.KEY_BORDER_EXTENDER);
          if (extender != null && extender instanceof BorderExtender) {
            localHints.add(new RenderingHints(JAI.KEY_BORDER_EXTENDER, (BorderExtender) extender));
            addBorderExtender = false;
          }
        }
        // border extender
        if (addBorderExtender) {
          localHints.add(ImageUtilities.BORDER_EXTENDER_HINTS);
        }
        //                boolean hasScaleX=!(Math.abs(finalRaster2Model.getScaleX()-1) <
        // 1E-2/(raster.getWidth()+1-raster.getMinX()));
        //                boolean hasScaleY=!(Math.abs(finalRaster2Model.getScaleY()-1) <
        // 1E-2/(raster.getHeight()+1-raster.getMinY()));
        //                boolean hasShearX=!(finalRaster2Model.getShearX() == 0.0);
        //                boolean hasShearY=!(finalRaster2Model.getShearY() == 0.0);
        //                boolean hasTranslateX=!(Math.abs(finalRaster2Model.getTranslateX()) <
        // 0.01F);
        //                boolean hasTranslateY=!(Math.abs(finalRaster2Model.getTranslateY()) <
        // 0.01F);
        //                boolean isTranslateXInt=!(Math.abs(finalRaster2Model.getTranslateX() -
        // (int) finalRaster2Model.getTranslateX()) <  0.01F);
        //                boolean isTranslateYInt=!(Math.abs(finalRaster2Model.getTranslateY() -
        // (int) finalRaster2Model.getTranslateY()) <  0.01F);
        //
        //                boolean isIdentity = finalRaster2Model.isIdentity() &&
        // !hasScaleX&&!hasScaleY &&!hasTranslateX&&!hasTranslateY;

        //                // TODO how can we check that the a skew is harmelss????
        //                if(isIdentity){
        //                    // TODO check if we are missing anything like tiling or such that
        // comes from hints
        //                    return new GranuleLoadingResult(raster, granuleLoadingShape,
        // granuleUrl, doFiltering);
        //                }
        //
        //                // TOLERANCE ON PIXELS SIZE
        //
        //                // Check and see if the affine transform is in fact doing
        //                // a Translate operation. That is a scale by 1 and no rotation.
        //                // In which case call translate. Note that only integer translate
        //                // is applicable. For non-integer translate we'll have to do the
        //                // affine.
        //                // If the hints contain an ImageLayout hint, we can't use
        //                // TranslateIntOpImage since it isn't capable of dealing with that.
        //                // Get ImageLayout from renderHints if any.
        //                ImageLayout layout = RIFUtil.getImageLayoutHint(localHints);
        //                if ( !hasScaleX &&
        //                     !hasScaleY  &&
        //                      !hasShearX&&
        //                      !hasShearY&&
        //                      isTranslateXInt&&
        //                      isTranslateYInt&&
        //                    layout == null) {
        //                    // It's a integer translate
        //                    return new GranuleLoadingResult(new TranslateIntOpImage(raster,
        //                                                    localHints,
        //                                                   (int) finalRaster2Model.getShearX(),
        //                                                   (int)
        // finalRaster2Model.getShearY()),granuleLoadingShape, granuleUrl, doFiltering);
        //                }

        ImageWorker iw = new ImageWorker(raster);
        iw.setRenderingHints(localHints);
        iw.affine(finalRaster2Model, interpolation, request.getBackgroundValues());
        return new GranuleLoadingResult(
            iw.getRenderedImage(), granuleLoadingShape, granuleUrl, doFiltering);
      }

    } catch (IllegalStateException e) {
      if (LOGGER.isLoggable(java.util.logging.Level.WARNING)) {
        LOGGER.log(
            java.util.logging.Level.WARNING,
            new StringBuilder("Unable to load raster for granuleDescriptor ")
                .append(this.toString())
                .append(" with request ")
                .append(request.toString())
                .append(" Resulting in no granule loaded: Empty result")
                .toString(),
            e);
      }
      return null;
    } catch (org.opengis.referencing.operation.NoninvertibleTransformException e) {
      if (LOGGER.isLoggable(java.util.logging.Level.WARNING)) {
        LOGGER.log(
            java.util.logging.Level.WARNING,
            new StringBuilder("Unable to load raster for granuleDescriptor ")
                .append(this.toString())
                .append(" with request ")
                .append(request.toString())
                .append(" Resulting in no granule loaded: Empty result")
                .toString(),
            e);
      }
      return null;
    } catch (TransformException e) {
      if (LOGGER.isLoggable(java.util.logging.Level.WARNING)) {
        LOGGER.log(
            java.util.logging.Level.WARNING,
            new StringBuilder("Unable to load raster for granuleDescriptor ")
                .append(this.toString())
                .append(" with request ")
                .append(request.toString())
                .append(" Resulting in no granule loaded: Empty result")
                .toString(),
            e);
      }
      return null;

    } finally {
      try {
        if (request.getReadType() != ReadType.JAI_IMAGEREAD && inStream != null) {
          inStream.close();
        }
      } finally {
        if (request.getReadType() != ReadType.JAI_IMAGEREAD && reader != null) {
          reader.dispose();
        }
      }
    }
  }
    @Override
    public void sendContent(
        OutputStream out, Range range, final Map<String, String> params, String rContentType)
        throws IOException, BadRequestException {
      final int width = params.containsKey("width") ? Integer.parseInt(params.get("width")) : 300;
      final int height =
          params.containsKey("height") ? Integer.parseInt(params.get("height")) : 300;

      List<Job> jobs = new ArrayList();
      final Map<ILayer, Image> images = new HashMap();

      // run jobs for all layers
      WorkbenchState state = WorkbenchState.instance(SessionContext.current());
      final IMap map = state.getMap();
      if (map != null) {
        for (final ILayer layer : map.getLayers()) {
          if (layer.isVisible()) {
            UIJob job =
                new UIJob(getClass().getSimpleName() + ": " + layer.getLabel()) {
                  protected void runWithException(IProgressMonitor monitor) throws Exception {
                    try {
                      IGeoResource res = layer.getGeoResource();
                      if (res == null) {
                        throw new RuntimeException(
                            "Unable to find geo resource of layer: " + layer);
                      }
                      IService service = res.service(null);
                      Pipeline pipeline =
                          pipelineIncubator.newPipeline(
                              LayerUseCase.IMAGE, layer.getMap(), layer, service);
                      if (pipeline.length() == 0) {
                        throw new RuntimeException(
                            "Unable to build processor pipeline for layer: " + layer);
                      }

                      // processor request
                      GetMapRequest request =
                          new GetMapRequest(
                              null, // layers
                              map.getCRSCode(),
                              map.getExtent(),
                              contentType,
                              width,
                              height,
                              -1);

                      // process request
                      pipeline.process(
                          request,
                          new ResponseHandler() {
                            public void handle(ProcessorResponse pipeResponse) throws Exception {
                              Image image = ((ImageResponse) pipeResponse).getImage();
                              images.put(layer, image);
                            }
                          });
                    } catch (Exception e) {
                      // XXX put a special image in the map
                      log.warn("", e);
                      images.put(layer, null);
                      throw e;
                    }
                  }
                };
            jobs.add(job);
            job.schedule();
          }
        }

        // join jobs
        for (Job job : jobs) {
          try {
            job.join();
          } catch (InterruptedException e) {
            // XXX put a special image in the map
            log.warn("", e);
          }
        }
      }

      // put images together (MapContext order)
      Graphics2D g = null;
      try {
        // create image
        BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
        g = result.createGraphics();

        // rendering hints
        RenderingHints hints =
            new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        hints.add(
            new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
        hints.add(
            new RenderingHints(
                RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON));
        g.setRenderingHints(hints);

        if (map == null) {
          g.setFont(new Font("Serif", Font.PLAIN, 14));
          g.setColor(Color.RED);
          g.drawString("Melden Sie sich in der Workbench an, um hier eine Karte zu sehen!", 50, 50);
        }
        // FIXME honor layer.getOrderKey()
        for (Map.Entry<ILayer, Image> entry : images.entrySet()) {
          int rule = AlphaComposite.SRC_OVER;
          float alpha = ((float) entry.getKey().getOpacity()) / 100;

          g.setComposite(AlphaComposite.getInstance(rule, alpha));
          g.drawImage(entry.getValue(), 0, 0, null);
        }

        // encode image
        encodeImage(result, out);
      } finally {
        if (g != null) {
          g.dispose();
        }
      }
    }
    public PlanarImage setFront() {
      if (chroma_domain == 0 && chroma_range == 0 && luma_domain == 0 && luma_range == 0)
        return back;

      PlanarImage front = back;

      ColorScience.LinearTransform transform = new ColorScience.YST();

      double[][] rgb2yst = transform.fromRGB(back.getSampleModel().getDataType());
      double[][] yst2rgb = transform.toRGB(back.getSampleModel().getDataType());

      ParameterBlock pb = new ParameterBlock();
      pb.addSource(back);
      pb.add(rgb2yst);
      RenderedOp ystImage = JAI.create("BandCombine", pb, null);

      RenderingHints mfHints =
          new RenderingHints(
              JAI.KEY_BORDER_EXTENDER, BorderExtender.createInstance(BorderExtender.BORDER_COPY));

      if (chroma_domain != 0 && chroma_range != 0) {
        pb = new ParameterBlock();
        pb.addSource(ystImage);
        pb.add(chroma_domain * scale);
        pb.add(0.02f + 0.001f * chroma_domain);
        // pb.add(0.1f);
        ystImage = JAI.create("BilateralFilter", pb, mfHints);
        ystImage.setProperty(JAIContext.PERSISTENT_CACHE_TAG, Boolean.TRUE);
      }

      if (luma_domain != 0 && luma_range != 0) {
        pb = new ParameterBlock();
        pb.addSource(ystImage);
        pb.add(new int[] {0});
        RenderedOp y = JAI.create("bandselect", pb, null);

        pb = new ParameterBlock();
        pb.addSource(ystImage);
        pb.add(new int[] {1, 2});
        RenderedOp cc = JAI.create("bandselect", pb, JAIContext.noCacheHint);

        pb = new ParameterBlock();
        pb.addSource(y);
        pb.add((2 + luma_domain / 10f) * scale);
        pb.add(0.005f * luma_domain);
        y = JAI.create("BilateralFilter", pb, mfHints);

        RenderingHints layoutHints =
            new RenderingHints(JAI.KEY_IMAGE_LAYOUT, Functions.getImageLayout(ystImage));
        pb = new ParameterBlock();
        pb.addSource(y);
        pb.addSource(cc);
        layoutHints.add(JAIContext.noCacheHint);
        ystImage = JAI.create("BandMerge", pb, layoutHints);
      }

      pb = new ParameterBlock();
      pb.addSource(ystImage);
      pb.add(yst2rgb);
      front = JAI.create("BandCombine", pb, null);
      front.setProperty(JAIContext.PERSISTENT_CACHE_TAG, Boolean.TRUE);

      return front;
    }
  public void writeTo(final OutputStream out) throws ServiceException, IOException {
    Timer timer = new Timer();

    // single layer? -> request ENCODED_IMAGE
    if (mapContext.getLayerCount() == 1) {
      MapLayer mapLayer = mapContext.getLayers()[0];
      ILayer layer = loader.findLayer(mapLayer);
      try {
        Pipeline pipeline = loader.getOrCreatePipeline(layer, LayerUseCase.ENCODED_IMAGE);

        ProcessorRequest request = prepareProcessorRequest();
        pipeline.process(
            request,
            new ResponseHandler() {
              public void handle(ProcessorResponse pipeResponse) throws Exception {

                HttpServletResponse response = GeoServerWms.response.get();
                if (pipeResponse == EncodedImageResponse.NOT_MODIFIED) {
                  log.info("Response: 304!");
                  response.setStatus(304);
                } else {
                  long lastModified = ((EncodedImageResponse) pipeResponse).getLastModified();
                  // allow caches and browser clients to cache for 1h
                  // response.setHeader( "Cache-Control", "public,max-age=3600" );
                  if (lastModified > 0) {
                    response.setHeader("Cache-Control", "no-cache,must-revalidate");
                    response.setDateHeader("Last-Modified", lastModified);
                  } else {
                    response.setHeader("Cache-Control", "no-cache,must-revalidate");
                    response.setDateHeader("Expires", 0);
                  }

                  byte[] chunk = ((EncodedImageResponse) pipeResponse).getChunk();
                  int len = ((EncodedImageResponse) pipeResponse).getChunkSize();
                  out.write(chunk, 0, len);
                }
              }
            });
        log.debug("    flushing response stream. (" + timer.elapsedTime() + "ms)");
        out.flush();
      } catch (IOException e) {
        throw e;
      } catch (Exception e) {
        throw new IOException(e);
      }
    }

    // multiple layers -> render into one image
    else {
      List<Job> jobs = new ArrayList();
      final Map<MapLayer, Image> images = new HashMap();

      // run jobs for all layers
      for (final MapLayer mapLayer : mapContext.getLayers()) {
        final ILayer layer = loader.findLayer(mapLayer);
        // job
        UIJob job =
            new UIJob(getClass().getSimpleName() + ": " + layer.getLabel()) {
              protected void runWithException(IProgressMonitor monitor) throws Exception {
                try {
                  // XXX this excludes Cache304 (which supports EncodedImageResponse only)
                  Pipeline pipeline = loader.getOrCreatePipeline(layer, LayerUseCase.IMAGE);

                  GetMapRequest targetRequest = prepareProcessorRequest();
                  pipeline.process(
                      targetRequest,
                      new ResponseHandler() {
                        public void handle(ProcessorResponse pipeResponse) throws Exception {
                          Image layerImage = ((ImageResponse) pipeResponse).getImage();
                          images.put(mapLayer, layerImage);
                        }
                      });
                } catch (Exception e) {
                  // XXX put a special image in the map
                  log.warn("", e);
                  images.put(mapLayer, null);
                  throw e;
                }
              }
            };
        job.schedule();
        jobs.add(job);
      }

      // join jobs
      for (Job job : jobs) {
        try {
          job.join();
        } catch (InterruptedException e) {
          // XXX put a special image in the map
          log.warn("", e);
        }
      }

      // put images together (MapContext order)
      Graphics2D g = null;
      try {
        // result image
        BufferedImage result =
            ImageUtils.createImage(mapContext.getMapWidth(), mapContext.getMapHeight(), null, true);
        g = result.createGraphics();

        // rendering hints
        RenderingHints hints =
            new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        hints.add(
            new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
        hints.add(
            new RenderingHints(
                RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON));
        g.setRenderingHints(hints);

        for (MapLayer mapLayer : mapContext.getLayers()) {
          Image layerImage = images.get(mapLayer);

          // load image data
          //                  new javax.swing.ImageIcon( image ).getImage();

          ILayer layer = loader.findLayer(mapLayer);
          int rule = AlphaComposite.SRC_OVER;
          float alpha = ((float) layer.getOpacity()) / 100;

          g.setComposite(AlphaComposite.getInstance(rule, alpha));
          g.drawImage(layerImage, 0, 0, null);
        }
        encodeImage(result, out);
      } finally {
        if (g != null) {
          g.dispose();
        }
      }
    }
  }
  /**
   * Returns the specified property.
   *
   * @param name Property name.
   * @param opNode Operation node.
   */
  public Object getProperty(String name, Object opNode) {
    validate(name, opNode);

    if (opNode instanceof RenderedOp && name.equalsIgnoreCase("roi")) {
      RenderedOp op = (RenderedOp) opNode;

      ParameterBlock pb = op.getParameterBlock();

      // Retrieve the rendered source image and its ROI.
      RenderedImage src = pb.getRenderedSource(0);
      Object property = src.getProperty("ROI");
      if (property == null
          || property.equals(java.awt.Image.UndefinedProperty)
          || !(property instanceof ROI)) {
        return java.awt.Image.UndefinedProperty;
      }

      // Return undefined also if source ROI is empty.
      ROI srcROI = (ROI) property;
      if (srcROI.getBounds().isEmpty()) {
        return java.awt.Image.UndefinedProperty;
      }

      // Retrieve the Interpolation object.
      Interpolation interp = (Interpolation) pb.getObjectParameter(1);

      // Determine the effective source bounds.
      Rectangle srcBounds = null;
      PlanarImage dst = op.getRendering();
      if (dst instanceof GeometricOpImage && ((GeometricOpImage) dst).getBorderExtender() == null) {
        srcBounds =
            new Rectangle(
                src.getMinX() + interp.getLeftPadding(),
                src.getMinY() + interp.getTopPadding(),
                src.getWidth() - interp.getWidth() + 1,
                src.getHeight() - interp.getHeight() + 1);
      } else {
        srcBounds = new Rectangle(src.getMinX(), src.getMinY(), src.getWidth(), src.getHeight());
      }

      // If necessary, clip the ROI to the effective source bounds.
      if (!srcBounds.contains(srcROI.getBounds())) {
        srcROI = srcROI.intersect(new ROIShape(srcBounds));
      }

      // Retrieve the Warp object.
      Warp warp = (Warp) pb.getObjectParameter(0);

      // Setting constant image to be warped as a ROI
      Rectangle dstBounds = op.getBounds();

      // Setting layout of the constant image
      ImageLayout2 layout = new ImageLayout2();
      int minx = (int) srcBounds.getMinX();
      int miny = (int) srcBounds.getMinY();
      int w = (int) srcBounds.getWidth();
      int h = (int) srcBounds.getHeight();
      layout.setMinX(minx);
      layout.setMinY(miny);
      layout.setWidth(w);
      layout.setHeight(h);
      RenderingHints hints = op.getRenderingHints();
      hints.add(new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout));

      final PlanarImage constantImage =
          ConstantDescriptor.create(new Float(w), new Float(h), new Byte[] {(byte) 255}, hints);

      PlanarImage roiImage = null;

      // Make sure to specify tileCache, tileScheduler, tileRecyclier, by cloning hints.
      RenderingHints warpingHints = op.getRenderingHints();
      warpingHints.remove(JAI.KEY_IMAGE_LAYOUT);

      // Creating warped roi by the same way (Warp, Interpolation, source ROI) we warped the
      // input image.
      final ParameterBlock paramBlk = new ParameterBlock();
      paramBlk.addSource(constantImage);
      paramBlk.add(warp);
      paramBlk.add(interp);
      paramBlk.add(null);
      paramBlk.add(srcROI);

      // force in the image layout, this way we get exactly the same
      // as the affine we're eliminating
      Hints localHints = new Hints(op.getRenderingHints());
      localHints.remove(JAI.KEY_IMAGE_LAYOUT);
      ImageLayout il = new ImageLayout();
      il.setMinX(dstBounds.x);
      il.setMinY(dstBounds.y);
      il.setWidth(dstBounds.width);
      il.setHeight(dstBounds.height);
      localHints.put(JAI.KEY_IMAGE_LAYOUT, il);
      roiImage = JAI.create("Warp", paramBlk, localHints);
      ROI dstROI = new ROI(roiImage, 1);

      // If necessary, clip the warped ROI to the destination bounds.
      if (!dstBounds.contains(dstROI.getBounds())) {
        dstROI = dstROI.intersect(new ROIShape(dstBounds));
      }

      // Return the warped and possibly clipped ROI.
      return dstROI;
    }

    return java.awt.Image.UndefinedProperty;
  }
Beispiel #14
0
 public MacRenderer(RenderingHints rh, AffineTransform at) {
   renderingHints = new RenderingHints(null);
   renderingHints.add(rh);
   if (at == null) usr2dev = new AffineTransform();
   else usr2dev = new AffineTransform(at);
 }
Beispiel #15
0
 /** Constructs a new dynamic renderer with the specified buffer image. */
 public MacRenderer() {
   renderingHints = new RenderingHints(null);
   renderingHints.add(defaultRenderingHints);
   usr2dev = new AffineTransform();
 }