Exemplo n.º 1
0
public class CropOp extends AbstractOp {
  private static final Logger LOGGER = LoggerFactory.getLogger(CropOp.class);

  public static final String OP_NAME = Messages.getString("CropOperation.name"); // $NON-NLS-1$

  /**
   * Set the area to crop (Required parameter).
   *
   * <p>java.awt.Rectangle value.
   */
  public static final String P_AREA = "area"; // $NON-NLS-1$

  /**
   * Whether or not the image origin is shift after cropping.
   *
   * <p>Boolean value. Default value is false (keep the original image referential).
   */
  public static final String P_SHIFT_TO_ORIGIN = "shift.origin"; // $NON-NLS-1$

  public CropOp() {
    setName(OP_NAME);
  }

  @Override
  public void process() throws Exception {
    RenderedImage source = (RenderedImage) params.get(INPUT_IMG);
    RenderedImage result = source;
    Rectangle area = (Rectangle) params.get(P_AREA);

    if (area == null) {
      LOGGER.warn("Cannot apply \"{}\" because a parameter is null", OP_NAME); // $NON-NLS-1$
    } else {
      area =
          area.intersection(
              new Rectangle(
                  source.getMinX(), source.getMinY(), source.getWidth(), source.getHeight()));
      if (area.width > 1 && area.height > 1) {
        ParameterBlock pb = new ParameterBlock();
        pb.addSource(source);
        pb.add((float) area.x).add((float) area.y);
        pb.add((float) area.width).add((float) area.height);
        result = JAI.create("crop", pb, null); // $NON-NLS-1$

        if (JMVUtils.getNULLtoFalse(params.get(P_SHIFT_TO_ORIGIN))) {
          float diffw = source.getMinX() - result.getMinX();
          float diffh = source.getMinY() - result.getMinY();
          if (diffw != 0.0f || diffh != 0.0f) {
            pb = new ParameterBlock();
            pb.addSource(result);
            pb.add(diffw);
            pb.add(diffh);
            result = JAI.create("translate", pb, null); // $NON-NLS-1$
          }
        }
      }
    }
    params.put(OUTPUT_IMG, result);
  }
}
Exemplo n.º 2
0
public enum Unit {
  PIXEL(
      -5,
      Messages.getString("Unit.pix"),
      Messages.getString("Unit.pix_s"),
      1.0), //$NON-NLS-1$ //$NON-NLS-2$
  /*
   * The prefix "nano" means 1 x 10-9, so one nanometer = 0.000000001 meters. Alternatively, 1 meter contains
   * 1,000,000,000 nanometers. Visible light contains wavelengths from roughly 300 to 800 nm. To convert this
   * wavelength to energy.
   */
  NANOMETER(
      -2,
      Messages.getString("Unit.nano"),
      Messages.getString("Unit.nano_S"),
      1.0E-09), //$NON-NLS-1$ //$NON-NLS-2$
  /*
   * Also known a micron. The prefix "micro" means 1 x 10-6, so one micrometer = 0.000001 meters. Alternatively, 1
   * meter contains 1,000,000 micrometers. 1 micrometer = 10,000 Angstroms = 0.001 millimeters = 0.0009843 inches.
   */
  MICROMETER(
      -1,
      Messages.getString("Unit.micro"),
      Messages.getString("Unit.micro_S"),
      1.0E-06), //$NON-NLS-1$ //$NON-NLS-2$
  /*
   * The prefix "milli" means 1/1000, so one millimeter = 0.001 meters. Alternatively, 1 meter contains 1000
   * millimeters. 1 millimeter = 0.1 cm = 0.0394 inches.
   */
  MILLIMETER(
      0,
      Messages.getString("Unit.milli"),
      Messages.getString("Unit.milli_s"),
      1.0E-03), //$NON-NLS-1$ //$NON-NLS-2$
  /*
   * The prefix "centi" means 1/100, so one centimeter = 0.01 meters. Alternatively, 1 meter contains 100 cm. 1 cm =
   * 10 mm = 0.3937 inches.
   */
  CENTIMETER(
      1,
      Messages.getString("Unit.cent"),
      Messages.getString("Unit.cent_s"),
      1.0E-02), //$NON-NLS-1$ //$NON-NLS-2$
  /*
   * Defined as the length of 1,650,763.73 wavelengths of the orange-red radiation of 86Kr in a vacuum, one meter
   * contains 100 cm = 1000 mm. There are 1000 m in a kilometer. One meter = 39.37 inches = 3.28 feet = 1.09 yards.
   */
  METER(
      2,
      Messages.getString("Unit.meter"),
      Messages.getString("Unit.meter_s"),
      1.0), //$NON-NLS-1$ //$NON-NLS-2$
  /*
   * The prefix "kilo" means 1000, so one kilometer = 1,000 meters. 1 kilometer = 3,280.84 feet = 1,093.6 yards =
   * 0.6214 miles.
   */
  KILOMETER(
      3,
      Messages.getString("Unit.kilo"),
      Messages.getString("Unit.kilo_s"),
      1.0E+03), //$NON-NLS-1$ //$NON-NLS-2$

  // micro and mili iches are not official units because micro and mili comes from metric unit
  MICROINCH(
      9,
      Messages.getString("Unit.minch"),
      Messages.getString("Unit.minch_s"),
      2.54E-08), //$NON-NLS-1$ //$NON-NLS-2$

  MILLIINCH(10, "milli-inches", "mil", 2.54E-05),

  INCH(
      11,
      Messages.getString("Unit.inch"),
      Messages.getString("Unit.inch_s"),
      2.54E-02), //$NON-NLS-1$ //$NON-NLS-2$

  FEET(
      12,
      Messages.getString("Unit.feet"),
      Messages.getString("Unit.feet_s"),
      3.048E-01), //$NON-NLS-1$ //$NON-NLS-2$

  YARD(
      13,
      Messages.getString("Unit.yard"),
      Messages.getString("Unit.yard_s"),
      9.144E-01), //$NON-NLS-1$ //$NON-NLS-2$

  MILE(
      14,
      Messages.getString("Unit.mile"),
      Messages.getString("Unit.mile_s"),
      1.609344E+03); //$NON-NLS-1$ //$NON-NLS-2$

  private final int id;
  private final String fullName;
  private final String abbreviation;
  private final double convFactor;

  Unit(int id, String fullName, String abbreviation, double convFactor) {
    this.id = id;
    this.fullName = fullName;
    this.abbreviation = abbreviation;
    this.convFactor = convFactor;
  }

  public int getId() {
    return id;
  }

  public String getFullName() {
    return fullName;
  }

  public double getConvFactor() {
    return convFactor;
  }

  public String getAbbreviation() {
    return abbreviation;
  }

  @Override
  public String toString() {
    return fullName;
  }

  public double getConversionRatio(double calibRatio) {
    return calibRatio / convFactor;
  }

  public static ArrayList<AbbreviationUnit> getAbbreviationUnits() {
    ArrayList<AbbreviationUnit> units = new ArrayList<AbbreviationUnit>();
    for (Unit u : Unit.values()) {
      if (u.getId() != Unit.PIXEL.getId()) {
        units.add(new AbbreviationUnit(u));
      }
    }
    return units;
  }

  public static Unit getCurrentIdUnit(int id) {
    for (Unit u : Unit.values()) {
      if (id == u.getId()) {
        return u;
      }
    }
    return Unit.PIXEL;
  }

  public static final ArrayList<String> getUnitsName() {
    ArrayList<String> list = new ArrayList<String>();
    for (Unit u : Unit.values()) {
      list.add(u.getFullName());
    }
    return list;
  }

  public Unit getUpUnit() {
    for (Unit u : Unit.values()) {
      if (u.getId() - getId() == 1) {
        return u;
      }
    }
    return null;
  }

  public Unit getDownUnit() {
    for (Unit u : Unit.values()) {
      if (getId() - u.getId() == 1) {
        return u;
      }
    }
    return null;
  }

  public static ArrayList<Unit> getUnitExceptPixel() {
    ArrayList<Unit> list = new ArrayList<Unit>();
    for (Unit u : Unit.values()) {
      if (u.getId() != Unit.PIXEL.getId()) {
        list.add(u);
      }
    }
    return list;
  }
}
Exemplo n.º 3
0
public class RotationOperation extends AbstractOperation {
  private static final Logger LOGGER = LoggerFactory.getLogger(RotationOperation.class);

  public static final String name = Messages.getString("RotationOperation.title"); // $NON-NLS-1$
  public static final double epsilon = 1e-5;

  @Override
  public RenderedImage getRenderedImage(RenderedImage source, ImageOperation imageOperation) {
    Integer rotationAngle = (Integer) imageOperation.getActionValue(ActionW.ROTATION.cmd());
    if (rotationAngle == null) {
      result = source;
      LOGGER.warn("Cannot apply \"{}\" because a parameter is null", name); // $NON-NLS-1$
    } else if (rotationAngle == 0 || rotationAngle == 360) {
      result = source;
    } else {
      // optimize rotation by right angles
      TransposeType rotOp = null;

      if (Math.abs(rotationAngle - 90) < epsilon) {
        // 90 degree
        rotOp = TransposeDescriptor.ROTATE_90;
      } else if (Math.abs(rotationAngle - 180) < epsilon) {
        // 180 degree
        rotOp = TransposeDescriptor.ROTATE_180;
      } else if (Math.abs(rotationAngle - 270) < epsilon) {
        // 270 degree
        rotOp = TransposeDescriptor.ROTATE_270;
      }
      if (rotOp != null) {
        // use Transpose operation
        ParameterBlock pb = new ParameterBlock();
        pb.addSource(source);
        pb.add(rotOp);
        result = JAI.create("transpose", pb, ImageToolkit.NOCACHE_HINT); // $NON-NLS-1$
        // Handle non square images. Translation is necessary because the transpose operator keeps
        // the same
        // origin (top left not the center of the image)
        float diffw = source.getWidth() / 2.0f - result.getWidth() / 2.0f;
        float diffh = source.getHeight() / 2.0f - result.getHeight() / 2.0f;
        if (diffw != 0.0f || diffh != 0.0f) {
          pb = new ParameterBlock();
          pb.addSource(result);
          pb.add(diffw);
          pb.add(diffh);
          result = JAI.create("translate", pb, ImageToolkit.NOCACHE_HINT); // $NON-NLS-1$
        }

      } else {
        ParameterBlock pb = new ParameterBlock();
        pb.addSource(source);
        pb.add(source.getWidth() / 2.0f);
        pb.add(source.getHeight() / 2.0f);
        pb.add((float) (rotationAngle * Math.PI / 180.0));
        pb.add(new InterpolationBilinear());
        result = JAI.create("rotate", pb, ImageToolkit.NOCACHE_HINT); // $NON-NLS-1$

        // // Untile this rotate node so that when compute the next node,
        // // no extra memory and time are used in PlanarImage.getExtendedData().
        // ImageLayout il = new ImageLayout();
        // il.setTileWidth(result.getWidth());
        // il.setTileHeight(result.getHeight());
        // il.setTileGridXOffset(result.getMinX());
        // il.setTileGridYOffset(result.getMinY());
        // RenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, il);
        //
        // result = JAI.create("rotate", pb, hints);
      }
    }
    return result;
  }

  @Override
  public String getOperationName() {
    return name;
  }
}