/**
   * Write world image extensions : TFW, PRJ, TAB
   *
   * @param request
   * @param file
   * @param in
   * @throws IOException
   */
  private void writeWorldImageExt(WcsReaderRequest request, File file) throws IOException {

    String baseFilePath = file.getPath().substring(0, file.getPath().lastIndexOf('.'));

    // Compute image size and image transformed BBOX
    ReferencedEnvelope transformedBBox;
    AffineTransform transform;

    int width = -1;
    int height = -1;
    String ext = FileUtils.extension(file);

    try {
      final Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(ext.substring(1));

      while (readers.hasNext() && width < 0 && height < 0) {
        ImageInputStream stream = null;
        try {
          ImageReader reader = readers.next();
          stream = ImageIO.createImageInputStream(file.getAbsoluteFile());
          reader.setInput(stream, true, false);
          width = reader.getWidth(0);
          height = reader.getHeight(0);
          break;
        } catch (Exception e) {
          width = -1;
          height = -1;
          // try next reader;
        } finally {
          if (stream != null) {
            stream.close();
          }
        }
      }

      transformedBBox = request.requestBbox.transform(request.responseCRS, true, 10);
      if (width < 0) {
        width = (int) Math.round(transformedBBox.getWidth() / request.crsResolution());
      }

      if (height < 0) {
        height = (int) Math.round(transformedBBox.getHeight() / request.crsResolution());
      }

      Rectangle imageSize = new Rectangle(width, height);
      transform = RendererUtilities.worldToScreenTransform(transformedBBox, imageSize);
      transform.invert();

    } catch (Exception e) {
      throw new ExtractorException(e);
    }

    // Generate TFW TAB PRJ files
    createWorldFile(transform, ext, baseFilePath);
    createTABFile(transformedBBox, width, height, baseFilePath, ext);
    createPrjFile(request.responseCRS, baseFilePath);
  }
Esempio n. 2
0
  /**
   * Compute a transformation matrix that pass from graph units (user space) to pixel units (device
   * space) so that the whole graph is visible.
   *
   * @param g2 The Swing graphics.
   */
  protected void autoFitView(Graphics2D g2) {
    double sx, sy;
    double tx, ty;
    double padXgu = getPaddingXgu() * 2;
    double padYgu = getPaddingYgu() * 2;
    double padXpx = getPaddingXpx() * 2;
    double padYpx = getPaddingYpx() * 2;

    sx = (metrics.viewport[2] - padXpx) / (metrics.size.data[0] + padXgu); // Ratio
    // along
    // X
    sy = (metrics.viewport[3] - padYpx) / (metrics.size.data[1] + padYgu); // Ratio
    // along
    // Y
    tx = metrics.lo.x + (metrics.size.data[0] / 2); // Centre of graph in X
    ty = metrics.lo.y + (metrics.size.data[1] / 2); // Centre of graph in Y

    if (sx <= 0) {
      sx =
          (metrics.viewport[2] - Math.min(metrics.viewport[2] - 1, padXpx))
              / (metrics.size.data[0] + padXgu);
    }

    if (sy <= 0) {
      sy =
          (metrics.viewport[3] - Math.min(metrics.viewport[3] - 1, padYpx))
              / (metrics.size.data[1] + padYgu);
    }

    if (sx > sy) // The least ratio.
    sx = sy;
    else sy = sx;

    g2.translate(metrics.viewport[2] / 2, metrics.viewport[3] / 2);
    if (rotation != 0) g2.rotate(rotation / (180 / Math.PI));
    g2.scale(sx, -sy);
    g2.translate(-tx, -ty);

    Tx = g2.getTransform();
    xT = new AffineTransform(Tx);
    try {
      xT.invert();
    } catch (NoninvertibleTransformException e) {
      logger.warning("Cannot inverse gu2px matrix.");
    }

    zoom = 1;

    center.set(tx, ty, 0);
    metrics.setRatioPx2Gu(sx);
    metrics.loVisible.copy(metrics.lo);
    metrics.hiVisible.copy(metrics.hi);
  }
Esempio n. 3
0
  public void validateCompositeState(
      Composite comp, AffineTransform xform, Paint paint, SunGraphics2D sg2d) {
    boolean updatePaint = (paint != validatedPaint) || paint == null;

    // validate composite
    if ((comp != validatedComp)) {
      if (comp != null) {
        setComposite(comp);
      } else {
        comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER);
        setComposite(comp);
      }
      // the paint state is dependent on the composite state, so make
      // sure we update the color below
      updatePaint = true;
      validatedComp = comp;
    }

    if (sg2d != null && validatedPixel != sg2d.pixel) {
      validatedPixel = sg2d.pixel;
      setForeground(validatedPixel);
    }

    // validate paint
    if (updatePaint) {
      if (paint != null && sg2d != null && sg2d.paintState >= SunGraphics2D.PAINT_GRADIENT) {
        XRPaints.setPaint(sg2d, paint);
      } else {
        XRResetPaint();
      }
      validatedPaint = paint;
    }

    if (src != solidSrcPict) {
      AffineTransform at = (AffineTransform) xform.clone();
      try {
        at.invert();
      } catch (NoninvertibleTransformException e) {
        at.setToIdentity();
      }
      src.validateAsSource(at, -1, -1);
    }
  }
Esempio n. 4
0
  /**
   * Compute a transformation that pass from graph units (user space) to a pixel units (device
   * space) so that the view (zoom and centre) requested by the user is produced.
   *
   * @param g2 The Swing graphics.
   */
  protected void userView(Graphics2D g2) {
    double sx, sy;
    double tx, ty;
    double padXgu = getPaddingXgu() * 2;
    double padYgu = getPaddingYgu() * 2;
    double padXpx = getPaddingXpx() * 2;
    double padYpx = getPaddingYpx() * 2;
    double gw = gviewport != null ? gviewport[2] - gviewport[0] : metrics.size.data[0];
    double gh = gviewport != null ? gviewport[3] - gviewport[1] : metrics.size.data[1];

    sx = (metrics.viewport[2] - padXpx) / ((gw + padXgu) * zoom);
    sy = (metrics.viewport[3] - padYpx) / ((gh + padYgu) * zoom);
    tx = center.x;
    ty = center.y;

    if (sx > sy) // The least ratio.
    sx = sy;
    else sy = sx;

    g2.translate((metrics.viewport[2] / 2), (metrics.viewport[3] / 2));
    if (rotation != 0) g2.rotate(rotation / (180 / Math.PI));
    g2.scale(sx, -sy);
    g2.translate(-tx, -ty);

    Tx = g2.getTransform();
    xT = new AffineTransform(Tx);
    try {
      xT.invert();
    } catch (NoninvertibleTransformException e) {
      logger.log(Level.WARNING, "Cannot inverse gu2px matrix.", e);
    }

    metrics.setRatioPx2Gu(sx);

    double w2 = (metrics.viewport[2] / sx) / 2;
    double h2 = (metrics.viewport[3] / sx) / 2;

    metrics.loVisible.set(center.x - w2, center.y - h2);
    metrics.hiVisible.set(center.x + w2, center.y + h2);
  }