/** Test for chain binding */
  @Test
  public void chainTest() {
    TestModel model = new TestModel();

    new PropertyBindingLink(
        new ChainedBindingSource(model, "currentSelection", "currentName"),
        new PropertyBindingTarget() {

          @Override
          public void updateTarget(Object newValue) {
            receptedValue = newValue;
          }
        });
    // awaiting null
    Assert.assertEquals(null, receptedValue);

    // setting the first chain element
    TestSelection testSelection1 = new TestSelection();
    model.setCurrentSelection(testSelection1);

    // no update
    Assert.assertEquals(null, receptedValue);

    testSelection1.setCurrentName("my.test.1");
    // new value : my.test.1
    Assert.assertEquals("my.test.1", receptedValue);

    // changing model selection
    TestSelection testSelection2 = new TestSelection();
    testSelection2.setCurrentName("my.test.2");
    model.setCurrentSelection(testSelection2);
    Assert.assertEquals("my.test.2", receptedValue);

    // verifying terminated links
    testSelection1.setCurrentName("changed.test.1");
    Assert.assertNotSame("changed.test1", receptedValue);

    // verifying link termination
    model.setCurrentSelection(null);
    Assert.assertEquals(null, receptedValue);
  }
Пример #2
0
  public void testRescale(
      RenderedImage source,
      boolean roiUsed,
      boolean noDataUsed,
      boolean useRoiAccessor,
      TestSelection select) {

    // The precalculated roi is used, if selected by the related boolean.
    ROI roiData;

    if (roiUsed) {
      roiData = roi;
    } else {
      roiData = null;
    }

    // The precalculated NoData Range is used, if selected by the related boolean.
    Range noDataRange;
    // Image data type
    int dataType = source.getSampleModel().getDataType();

    if (noDataUsed) {

      switch (dataType) {
        case DataBuffer.TYPE_BYTE:
          noDataRange = noDataByte;
          break;
        case DataBuffer.TYPE_USHORT:
          noDataRange = noDataUShort;
          break;
        case DataBuffer.TYPE_SHORT:
          noDataRange = noDataShort;
          break;
        case DataBuffer.TYPE_INT:
          noDataRange = noDataInt;
          break;
        case DataBuffer.TYPE_FLOAT:
          noDataRange = noDataFloat;
          break;
        case DataBuffer.TYPE_DOUBLE:
          noDataRange = noDataDouble;
          break;
        default:
          throw new IllegalArgumentException("Wrong data type");
      }
    } else {
      noDataRange = null;
    }

    // Rescale operation
    PlanarImage rescaled =
        RescaleDescriptor.create(
            source, scales, offsets, roiData, noDataRange, useRoiAccessor, destNoData, null);

    // Display Image
    if (INTERACTIVE && TEST_SELECTOR == select.getType()) {
      RenderedImageBrowser.showChain(rescaled, false, roiUsed);
      try {
        System.in.read();
      } catch (IOException e) {
        e.printStackTrace();
      }

    } else {
      // Calculation of all the image tiles
      rescaled.getTiles();
    }

    // Rescale control on the first band
    int tileMinX = rescaled.getMinTileX();
    int tileMinY = rescaled.getMinTileY();
    // Selection of the source and destination first tile
    Raster tileDest = rescaled.getTile(tileMinX, tileMinY);
    Raster tileSource = source.getTile(tileMinX, tileMinY);

    int tileMinXpix = tileDest.getMinX();
    int tileMinYpix = tileDest.getMinY();

    int tileMaxXpix = tileDest.getWidth() + tileMinXpix;
    int tileMaxYpix = tileDest.getHeight() + tileMinYpix;

    double scaleFactor = scales[0];
    double offset = offsets[0];
    // loop through the tile pixels
    for (int i = tileMinXpix; i < tileMaxXpix; i++) {
      for (int j = tileMinYpix; j < tileMaxYpix; j++) {

        switch (dataType) {
          case DataBuffer.TYPE_BYTE:
            // selection of the rescaled pixel
            byte destValueB = (byte) tileDest.getSample(i, j, 0);
            // rescale operation on the source pixel
            int srcValueB = tileSource.getSample(i, j, 0) & 0xFF;
            byte calculationB = ImageUtil.clampRoundByte(srcValueB * scaleFactor + offset);
            // comparison
            if (roiUsed && noDataUsed) {
              if (roiBounds.contains(i, j) && !noDataRange.contains((byte) srcValueB)) {
                assertEquals(calculationB, destValueB);
              }
            } else if (roiUsed) {
              if (roiBounds.contains(i, j)) {
                assertEquals(calculationB, destValueB);
              }
            } else if (noDataUsed) {
              if (!noDataRange.contains((byte) srcValueB)) {
                assertEquals(calculationB, destValueB);
              }
            } else {
              assertEquals(calculationB, destValueB);
            }
            break;
          case DataBuffer.TYPE_USHORT:
            short destValueU = (short) tileDest.getSample(i, j, 0);
            int srcValueU = tileSource.getSample(i, j, 0) & 0xFFFF;
            short calculationU = ImageUtil.clampRoundUShort(srcValueU * scaleFactor + offset);
            if (roiUsed && noDataUsed) {
              if (roiBounds.contains(i, j) && !noDataRange.contains((short) srcValueU)) {
                assertEquals(calculationU, destValueU);
              }
            } else if (roiUsed) {
              if (roiBounds.contains(i, j)) {
                assertEquals(calculationU, destValueU);
              }
            } else if (noDataUsed) {
              if (!noDataRange.contains((short) srcValueU)) {
                assertEquals(calculationU, destValueU);
              }
            } else {
              assertEquals(calculationU, destValueU);
            }
            break;
          case DataBuffer.TYPE_SHORT:
            short destValueS = (short) tileDest.getSample(i, j, 0);
            short srcValueS = (short) tileSource.getSample(i, j, 0);
            short calculationS = ImageUtil.clampRoundShort(srcValueS * scaleFactor + offset);
            if (roiUsed && noDataUsed) {
              if (roiBounds.contains(i, j) && !noDataRange.contains(srcValueS)) {
                assertEquals(calculationS, destValueS);
              }
            } else if (roiUsed) {
              if (roiBounds.contains(i, j)) {
                assertEquals(calculationS, destValueS);
              }
            } else if (noDataUsed) {
              if (!noDataRange.contains(srcValueS)) {
                assertEquals(calculationS, destValueS);
              }
            } else {
              assertEquals(calculationS, destValueS);
            }
            break;
          case DataBuffer.TYPE_INT:
            int destValueI = tileDest.getSample(i, j, 0);
            int srcValueI = tileSource.getSample(i, j, 0);
            int calculationI = ImageUtil.clampRoundInt(srcValueI * scaleFactor + offset);
            if (roiUsed && noDataUsed) {
              if (roiBounds.contains(i, j) && !noDataRange.contains(srcValueI)) {
                assertEquals(calculationI, destValueI);
              }
            } else if (roiUsed) {
              if (roiBounds.contains(i, j)) {
                assertEquals(calculationI, destValueI);
              }
            } else if (noDataUsed) {
              if (!noDataRange.contains(srcValueI)) {
                assertEquals(calculationI, destValueI);
              }
            } else {
              assertEquals(calculationI, destValueI);
            }
            break;
          case DataBuffer.TYPE_FLOAT:
            float destValueF = tileDest.getSampleFloat(i, j, 0);
            float srcValueF = tileSource.getSampleFloat(i, j, 0);
            float calculationF = (float) ((srcValueF * scaleFactor) + offset);
            if (roiUsed && noDataUsed) {
              if (roiBounds.contains(i, j) && !noDataRange.contains(srcValueF)) {
                assertEquals(calculationF, destValueF, TOLERANCE);
              }
            } else if (roiUsed) {
              if (roiBounds.contains(i, j)) {
                assertEquals(calculationF, destValueF, TOLERANCE);
              }
            } else if (noDataUsed) {
              if (!noDataRange.contains(srcValueF)) {
                assertEquals(calculationF, destValueF, TOLERANCE);
              }
            } else {
              assertEquals(calculationF, destValueF, TOLERANCE);
            }
            break;
          case DataBuffer.TYPE_DOUBLE:
            double destValueD = tileDest.getSampleDouble(i, j, 0);
            double srcValueD = tileSource.getSampleDouble(i, j, 0);
            double calculationD = ((srcValueD * scaleFactor) + offset);
            if (roiUsed && noDataUsed) {
              if (roiBounds.contains(i, j) && !noDataRange.contains(srcValueD)) {
                assertEquals(calculationD, destValueD, TOLERANCE);
              }
            } else if (roiUsed) {
              if (roiBounds.contains(i, j)) {
                assertEquals(calculationD, destValueD, TOLERANCE);
              }
            } else if (noDataUsed) {
              if (!noDataRange.contains(srcValueD)) {
                assertEquals(calculationD, destValueD, TOLERANCE);
              }
            } else {
              assertEquals(calculationD, destValueD, TOLERANCE);
            }
            break;
          default:
            throw new IllegalArgumentException("Wrong data type");
        }
      }
    }
  }