View Javadoc
1   package com.github.celldynamics.quimp.plugin.utils;
2   
3   /**
4    * Basic interface for all family of filters based on moving window.
5    * 
6    * <p>Supports various methods of data padding
7    * 
8    * @author p.baniukiewicz
9    *
10   */
11  public interface IPadArray {
12    /**
13     * defines circular padding.
14     */
15    int CIRCULARPAD = 1;
16    /**
17     * defines symmetric padding.
18     */
19    int SYMMETRICPAD = 2;
20  
21    /**
22     * Helper method to pick values from X, Y arrays.
23     * 
24     * <p>It accepts negative indexes as well as larger than X.size() and does in-place padding.
25     * Returns new proper index for array that accounts padding e.g. for input = -2 it returns
26     * last+2 if padding is circular
27     * 
28     * <p>Do no check relations of window (provided \c index) to whole data size. May be unstable for
29     * certain cases.
30     * 
31     * @param dataLength Length of data
32     * @param index Index of element to get
33     * @param mode Method of padding. Available are: - \b CIRCULARPAD - as in Matlab padarray - \b
34     *        SYMMETRICPAD - as in Matlab padarray
35     * @return Proper index. If index is negative or larger than X,Y size returned value simulates
36     *         padding.
37     */
38    static int getIndex(int dataLength, int index, int mode) {
39  
40      switch (mode) {
41        case CIRCULARPAD:
42          if (index < 0) {
43            return (dataLength + index);
44          } // for -1 points last element
45          if (index >= dataLength) {
46            return (index - dataLength);
47          } // for after last points to first
48          break;
49        case SYMMETRICPAD:
50          if (index < 0) {
51            return -index - 1;
52          }
53          if (index >= dataLength) {
54            return (dataLength - (index - dataLength) - 1);
55          }
56          break;
57        default:
58          throw new IllegalArgumentException("Padding mode not supported");
59      }
60  
61      return index; // for all remaining cases
62    }
63  
64  }