Example #1
0
  /**
   * Scales the Pix to a specified width and height using a specified scaling type (fill, stretch,
   * etc.). Returns a scaled image or a clone of the Pix if no scaling is required.
   *
   * @param pixs
   * @param width
   * @param height
   * @param type
   * @return a scaled image or a clone of the Pix if no scaling is required
   */
  public static Pix scaleToSize(Pix pixs, int width, int height, ScaleType type) {
    if (pixs == null) throw new IllegalArgumentException("Source pix must be non-null");

    int pixWidth = pixs.getWidth();
    int pixHeight = pixs.getHeight();

    float scaleX = width / (float) pixWidth;
    float scaleY = height / (float) pixHeight;

    switch (type) {
      case FILL:
        // Retains default scaleX and scaleY values
        break;
      case FIT:
        scaleX = Math.min(scaleX, scaleY);
        scaleY = scaleX;
        break;
      case FIT_SHRINK:
        scaleX = Math.min(1.0f, Math.min(scaleX, scaleY));
        scaleY = scaleX;
        break;
    }

    return scale(pixs, scaleX, scaleY);
  }
Example #2
0
  /**
   * Scales the Pix to specified x and y scale. If no scaling is required, returns a clone of the
   * source Pix.
   *
   * @param pixs the source Pix
   * @param scaleX x-dimension (width) scaling factor
   * @param scaleY y-dimension (height) scaling factor
   * @return a Pix scaled according to the supplied factors
   */
  public static Pix scale(Pix pixs, float scaleX, float scaleY) {
    if (pixs == null) throw new IllegalArgumentException("Source pix must be non-null");
    if (scaleX <= 0.0f) throw new IllegalArgumentException("X scaling factor must be positive");
    if (scaleY <= 0.0f) throw new IllegalArgumentException("Y scaling factor must be positive");

    long nativePix = nativeScale(pixs.getNativePix(), scaleX, scaleY);

    if (nativePix == 0) throw new RuntimeException("Failed to natively scale pix");

    return new Pix(nativePix);
  }
Example #3
0
  /**
   * Performs a tophat-like operation.
   *
   * <p>Notes:
   *
   * <ol>
   *   <li>Don't be fooled. This is NOT a tophat. It is a tophat-like operation, where the result is
   *       similar to what you'd get if you used an erosion instead of an opening, or a dilation
   *       instead of a closing.
   *   <li>Instead of opening or closing at full resolution, it does a fast downscale/minmax
   *       operation, then a quick small smoothing at low res, a replicative expansion of the
   *       "background" to full res, and finally a removal of the background level from the input
   *       image. The smoothing step may not be important.
   *   <li>It does not remove noise as well as a tophat, but it is 5 to 10 times faster. If you need
   *       the preciseness of the tophat, don't use this.
   *   <li>The L_TOPHAT_WHITE flag emphasizes small bright regions, whereas the L_TOPHAT_BLACK flag
   *       emphasizes small dark regions.
   * </ol>
   *
   * @param pixs Source pix (8bpp)
   * @param xsize width of max/min op, smoothing; any integer >= 1
   * @param ysize height of max/min op, smoothing; any integer >= 1
   * @param type L_TOPHAT_WHITE: image - min, or L_TOPHAT_BLACK: max - image
   * @return a new Pix image
   */
  public static Pix pixFastTophat(Pix pixs, int xsize, int ysize, int type) {
    if (pixs == null) throw new IllegalArgumentException("Source pix must be non-null");
    if (pixs.getDepth() != 8) throw new IllegalArgumentException("Source pix depth must be 8bpp");
    if (xsize < 1 || ysize < 1) throw new IllegalArgumentException("size < 1");
    if (type < 0 || type > 1)
      throw new IllegalArgumentException("Type must be L_TOPHAT_BLACK or L_TOPHAT_WHITE");

    long nativePix = nativePixFastTophat(pixs.mNativePix, xsize, ysize, type);

    if (nativePix == 0) throw new RuntimeException("Failed to perform pixFastTophat on image");

    return new Pix(nativePix);
  }
Example #4
0
  /**
   * Scales the Pix to the specified scale without sharpening.
   *
   * @param pixs the source Pix (1, 2, 4, 8, 16 and 32 bpp)
   * @param scale scaling factor for both X and Y
   * @return a Pix scaled while maintaining its aspect ratio
   */
  public static Pix scaleWithoutSharpening(Pix pixs, float scale) {
    if (pixs == null) throw new IllegalArgumentException("Source pix must be non-null");
    if (scale <= 0.0f) throw new IllegalArgumentException("Scaling factor must be positive");

    return new Pix(nativeScaleGeneral(pixs.getNativePix(), scale, scale, 0f, 0));
  }