private void calcEntropy(ModelSimpleImage kImage, double dNumSamples) {
    m_kImagePointsDual.DetachAllEffects();
    m_kImagePointsDual.AttachEffect(m_kImageEffectDual);
    m_pkRenderer.Resize(
        m_kHistogramOutput.GetTarget(0).GetImage().GetBound(0),
        m_kHistogramOutput.GetTarget(0).GetImage().GetBound(1));
    m_kHistogramOutput.Enable();
    m_pkRenderer.ClearColorDepth();
    // m_pkRenderer.ClearBuffers();
    int iDepth = m_kImageA.nDims == 3 ? m_kImageA.extents[2] : 1;
    float fInv2 = 1.0f / (iDepth);
    float fW;
    for (int i = 0; i < m_iRenderLoops; i++) {
      if (m_iRenderLoops == 1) {
        m_kImageEffectDual.ZSlice(0f);
      } else {
        fW = i * fInv2;
        m_kImageEffectDual.ZSlice(((2.0f * fW - 1.0f)));
        m_kImageEffectDual.UseZSlice();
      }
      m_pkRenderer.Draw(m_kImagePointsDual);
    }
    // writeImage();
    m_kHistogramOutput.Disable();
    // printTarget( "HistoOut" , m_kHistogramOutput.GetTarget(0) );

    m_pkRenderer.Resize(
        m_kHistogramOutputB.GetTarget(0).GetImage().GetBound(0),
        m_kHistogramOutputB.GetTarget(0).GetImage().GetBound(1));
    m_kHistogramOutputB.Enable();
    m_pkRenderer.ClearColorDepth();
    // m_pkRenderer.ClearBuffers();

    // m_kHistogramPoints2D.DetachAllEffects();
    // m_kHistogramPoints2D.AttachEffect(m_akCollapse2D);
    m_pkRenderer.Draw(m_kHistogramPoints2D);

    // m_kHistogramPoints2D.DetachAllEffects();
    // m_kHistogramPoints2D.AttachEffect(m_akCollapseColumns);
    // m_pkRenderer.Draw(m_kHistogramPoints2D);

    // m_kHistogramPoints2D.DetachAllEffects();
    // m_kHistogramPoints2D.AttachEffect(m_akCollapseRows);
    // m_pkRenderer.Draw(m_kHistogramPoints2D);

    // m_pkRenderer.Draw(m_kHistogramPoints2D);
    m_kHistogramOutputB.Disable();
    // printTarget( "HistoOut" , m_kHistogramOutputB.GetTarget(0) );
    ReduceDualA(dNumSamples);
  }
  private void ReduceDualA(double dNumSamples) {
    Texture kTarget = null;
    m_pkRenderer.Resize(
        m_kEntropyOut.GetTarget(0).GetImage().GetBound(0),
        m_kEntropyOut.GetTarget(0).GetImage().GetBound(1));
    m_kEntropyOut.Enable();
    m_pkRenderer.ClearColorDepth();
    // m_pkRenderer.ClearBuffers();
    m_pkRenderer.Draw(m_kEntropyPoints2D);
    m_kEntropyOut.Disable();
    kTarget = m_kEntropyOut.GetTarget(0);

    double dEntropyDual = 0;
    double dOverlap = 0;
    double dEntropyX = 0, dEntropyY = 0;
    if (kTarget != null) {
      int iIndex = 0;
      int iStep = (int) Math.max(1.0, (int) (kTarget.GetImage().GetBytesPerPixel() / 4.0f));
      m_pkRenderer.GetTexImage(kTarget);

      // System.err.println( "Entropy = " + kTarget.GetImage().GetFloatData()[0] + " "
      //        + kTarget.GetImage().GetFloatData()[1] + " "
      //        + kTarget.GetImage().GetFloatData()[2] + " "
      //        + kTarget.GetImage().GetFloatData()[3] );

      for (int i = 0; i < kTarget.GetImage().GetBound(1); i++) {
        for (int j = 0; j < kTarget.GetImage().GetBound(0); j++) {
          dEntropyDual += kTarget.GetImage().GetFloatData()[iIndex];
          dEntropyY += kTarget.GetImage().GetFloatData()[iIndex + 1];
          dEntropyX += kTarget.GetImage().GetFloatData()[iIndex + 2];
          dOverlap += kTarget.GetImage().GetFloatData()[iIndex + 3];
          iIndex += iStep;
        }
      }
    }
    dEntropyX = dEntropyX / dNumSamples;
    dEntropyY = dEntropyY / dNumSamples;

    m_dOverlap = dOverlap;
    m_dHx = dEntropyX;
    m_dHy = dEntropyY;
    m_dHxy = dEntropyDual / dNumSamples;
    // System.err.println( "GPU: " + m_iWidth + " " + dNumSamples + " " + m_dOverlap + " " + m_dHx +
    // " " + m_dHy + " " + m_dHxy );
  }
  private void calcLineMin() {
    m_kImagePointsDual.DetachAllEffects();
    m_kImagePointsDual.AttachEffect(m_kImageLineMinDual);
    boolean bEarly = false;
    Texture kTarget = null;
    OpenGLFrameBuffer kCurrentBracket = m_kBracketOut;
    OpenGLFrameBuffer kNewBracket = m_kBracketNewOut;
    OpenGLFrameBuffer kTempBracket;
    for (int i = 0; i < 100; i++) {
      m_kCalcTransform.SetTexture(kCurrentBracket.GetTarget(0), 0, 0);
      m_kImageLineMinPass2a.SetTexture(kCurrentBracket.GetTarget(0), 0, 0);

      // 1. Create the transform matrix based on the current bracket.
      m_pkRenderer.Resize(
          m_kTransformOut.GetTarget(0).GetImage().GetBound(0),
          m_kTransformOut.GetTarget(0).GetImage().GetBound(1));
      m_kTransformOut.Enable();
      m_pkRenderer.ClearColorDepth();
      // m_pkRenderer.ClearBuffers();
      m_pkRenderer.Draw(m_kTransformPoints);
      m_kTransformOut.Disable();
      /*
      kTarget = m_kTransformOut.GetTarget(0);
      m_pkRenderer.GetTexImage( kTarget );
      for ( int j = 0; j < kTarget.GetImage().GetFloatData().length; j++ )
      {
          System.err.print( kTarget.GetImage().GetFloatData()[j] + " " );
      }
      System.err.println( " " ); */

      // 1. Render all image points w/LineMin1 shaders
      m_pkRenderer.Resize(
          m_kHistogramOutput.GetTarget(0).GetImage().GetBound(0),
          m_kHistogramOutput.GetTarget(0).GetImage().GetBound(1));
      m_kHistogramOutput.Enable();
      m_pkRenderer.ClearColorDepth();
      // m_pkRenderer.ClearBuffers();
      m_pkRenderer.Draw(m_kImagePointsDual);
      m_kHistogramOutput.Disable();

      // 2. Render Histogram points
      m_pkRenderer.Resize(
          m_kHistogramOutputB.GetTarget(0).GetImage().GetBound(0),
          m_kHistogramOutputB.GetTarget(0).GetImage().GetBound(1));
      m_kHistogramOutputB.Enable();
      m_pkRenderer.ClearColorDepth();
      // m_pkRenderer.ClearBuffers();
      m_pkRenderer.Draw(m_kHistogramPoints2D);
      m_kHistogramOutputB.Disable();

      // 3. Render Entropy points w/LineMin1 shader
      m_pkRenderer.Resize(
          m_kEntropyOut.GetTarget(0).GetImage().GetBound(0),
          m_kEntropyOut.GetTarget(0).GetImage().GetBound(1));
      m_kEntropyOut.Enable();
      m_pkRenderer.ClearColorDepth();
      // m_pkRenderer.ClearBuffers();
      m_pkRenderer.Draw(m_kEntropyPoints2D);
      m_kEntropyOut.Disable();

      // 4. Render bracket points w/LineMin2 shader
      m_kBracketPoints.DetachAllEffects();
      m_kBracketPoints.AttachEffect(m_kImageLineMinPass2a);
      m_pkRenderer.Resize(
          kNewBracket.GetTarget(0).GetImage().GetBound(0),
          kNewBracket.GetTarget(0).GetImage().GetBound(1));
      kNewBracket.Enable();
      m_pkRenderer.ClearColorDepth();
      // m_pkRenderer.ClearBuffers();
      m_pkRenderer.Draw(m_kBracketPoints);
      kNewBracket.Disable();
      kTempBracket = kCurrentBracket;
      kCurrentBracket = kNewBracket;
      kNewBracket = kTempBracket;

      if ((i % 6) == 0) {
        // kTarget = m_kBracketOut.GetTarget(0);
        kTarget = kCurrentBracket.GetTarget(0);
        m_pkRenderer.GetTexImage(kTarget);
        /*
        System.err.println("");
        System.err.println("");
        System.err.println( "GPU BracketA = " + kTarget.GetImage().GetFloatData()[0] + " " + kTarget.GetImage().GetFloatData()[1]);
        System.err.println( "GPU BracketB = " + kTarget.GetImage().GetFloatData()[4] + " " + kTarget.GetImage().GetFloatData()[5]);
        System.err.println( "GPU BracketC = " + kTarget.GetImage().GetFloatData()[8] + " " + kTarget.GetImage().GetFloatData()[9]);
        System.err.println( "   " + i + " xNew = " + kTarget.GetImage().GetFloatData()[3] + " yNew = " +
                kTarget.GetImage().GetFloatData()[6]  + " case: " + kTarget.GetImage().GetFloatData()[7] + " " +
                kTarget.GetImage().GetFloatData()[10] + " " +
                kTarget.GetImage().GetFloatData()[11]);
                */
        if ((Math.abs(kTarget.GetImage().GetFloatData()[8] - kTarget.GetImage().GetFloatData()[0])
            <= m_fUnitTolerance)) {
          m_afBracketB[0] = kTarget.GetImage().GetFloatData()[4];
          m_afBracketB[1] = kTarget.GetImage().GetFloatData()[5];
          bEarly = true;
          break;
        }
        if (kTarget.GetImage().GetFloatData()[3] == 0) {
          m_afBracketB[0] = kTarget.GetImage().GetFloatData()[4];
          m_afBracketB[1] = kTarget.GetImage().GetFloatData()[5];
          bEarly = true;
          System.err.println("BREAK EARLY BAD");
          break;
        }
      }
    }
    if (!bEarly) {
      // kTarget = m_kBracketOut.GetTarget(0);
      kTarget = kCurrentBracket.GetTarget(0);
      m_pkRenderer.GetTexImage(kTarget);
      m_afBracketB[0] = kTarget.GetImage().GetFloatData()[4];
      m_afBracketB[1] = kTarget.GetImage().GetFloatData()[5];
      /*
          System.err.println("");
          System.err.println("");
          System.err.println( "GPU BracketA = " + kTarget.GetImage().GetFloatData()[0] + " " + kTarget.GetImage().GetFloatData()[1]);
          System.err.println( "GPU BracketB = " + kTarget.GetImage().GetFloatData()[4] + " " + kTarget.GetImage().GetFloatData()[5]);
          System.err.println( "GPU BracketC = " + kTarget.GetImage().GetFloatData()[8] + " " + kTarget.GetImage().GetFloatData()[9]);
      */
    }
    // System.err.println( m_afBracketB[0] + " " + m_afBracketB[1] );
  }