private void printTarget(String kMsg, Texture kTarget) {
   m_pkRenderer.GetTexImage(kTarget);
   float[] afData = kTarget.GetImage().GetFloatData();
   System.err.println(
       kMsg
           + "   TEXTURE NAME = "
           + kTarget.GetName()
           + " size = "
           + kTarget.GetImage().GetBound(0)
           + " "
           + kTarget.GetImage().GetBound(1));
   int iSum = 0;
   float fMax = 0, fMin = 1;
   int iCount = 0;
   for (int i = 0; i < afData.length; i += 4) {
     // if ( afData[i] != 0.0f )
     {
       System.err.print(
           afData[i] + " " + afData[i + 1] + " " + afData[i + 2] + " " + afData[i + 3] + "  ");
       iCount++;
     }
     if (((iCount) % (m_iWidth) == 0)) {
       System.err.println("");
     }
     if (afData[i] > fMax) {
       fMax = afData[i];
     }
     if (afData[i] < fMin) {
       fMin = afData[i];
     }
     iSum += afData[i + 3];
   }
   // System.err.println( "TOTAL: " + iSum + " " + fMin + " " + fMax );
 }
  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] );
  }
  public void dispose(GLAutoDrawable arg0) {

    cleanUp();
    if (m_kTextureA != null) {
      m_kTextureA.Release();
      m_kTextureA.GetImage().dispose();
      m_kTextureA.dispose();
      m_kTextureA = null;
    }
    if (m_kTextureB != null) {
      m_kTextureB.Release();
      m_kTextureB.GetImage().dispose();
      m_kTextureB.dispose();
      m_kTextureB = null;
    }

    if (m_kBracketPoints != null) {
      m_pkRenderer.ReleaseResources(m_kBracketPoints);
      m_kBracketPoints.dispose();
      m_kBracketPoints = null;
    }
    if (m_kTransformPoints != null) {
      m_pkRenderer.ReleaseResources(m_kTransformPoints);
      m_kTransformPoints.dispose();
      m_kTransformPoints = null;
    }

    if (m_kImageEffectDual != null) {
      m_pkRenderer.ReleaseResources(m_kImageEffectDual);
      m_kImageEffectDual.dispose();
    }
    if (m_kImageLineMinDual != null) {
      m_pkRenderer.ReleaseResources(m_kImageLineMinDual);
      m_kImageLineMinDual.dispose();
    }
    if (m_kCalcTransform != null) {
      m_pkRenderer.ReleaseResources(m_kCalcTransform);
      m_kCalcTransform.dispose();
    }

    if (m_kBracketOut != null) {
      m_kBracketOut.GetTarget(0).GetImage().dispose();
      m_kBracketOut.GetTarget(0).dispose();
      m_kBracketOut.TerminateBuffer();
      m_kBracketOut.dispose();
    }
    if (m_kTransformOut != null) {
      m_kTransformOut.GetTarget(0).GetImage().dispose();
      m_kTransformOut.GetTarget(0).dispose();
      m_kTransformOut.TerminateBuffer();
      m_kTransformOut.dispose();
    }
    if (m_kBracketNewOut != null) {
      m_kBracketNewOut.GetTarget(0).GetImage().dispose();
      m_kBracketNewOut.GetTarget(0).dispose();
      m_kBracketNewOut.TerminateBuffer();
      m_kBracketNewOut.dispose();
    }

    ImageCatalog.GetActive().dispose();
    VertexProgramCatalog.GetActive().dispose();
    PixelProgramCatalog.GetActive().dispose();
    CompiledProgramCatalog.GetActive().dispose();

    super.dispose();
  }
  private void CreateImageTextures() {
    m_kTextureA = new Texture();
    m_kTextureA.SetImage(VolumeImage.UpdateData(m_kTarget, m_kNameA));
    m_kTextureA.SetShared(true);
    m_kTextureA.SetBorderColor(new ColorRGBA(0, 0, 0, 0));
    m_kTextureA.SetFilterType(Texture.FilterType.NEAREST);
    m_kTextureA.SetWrapType(0, Texture.WrapType.CLAMP_BORDER);
    m_kTextureA.SetWrapType(1, Texture.WrapType.CLAMP_BORDER);
    m_kTextureA.SetWrapType(2, Texture.WrapType.CLAMP_BORDER);
    if (!m_pkRenderer.LoadTexture(m_kTextureA)) {
      m_bStatus = false;
    }

    m_kTextureB = new Texture();
    m_kTextureB.SetImage(VolumeImage.UpdateData(m_kMoving, m_kNameB));
    m_kTextureB.SetShared(true);
    m_kTextureB.SetBorderColor(new ColorRGBA(0, 0, 0, 0));
    m_kTextureB.SetFilterType(Texture.FilterType.NEAREST);
    m_kTextureB.SetWrapType(0, Texture.WrapType.CLAMP_BORDER);
    m_kTextureB.SetWrapType(1, Texture.WrapType.CLAMP_BORDER);
    m_kTextureB.SetWrapType(2, Texture.WrapType.CLAMP_BORDER);
    if (!m_pkRenderer.LoadTexture(m_kTextureB)) {
      m_bStatus = false;
    }

    m_kImageEffectDual =
        new VolumeHistogramEffect(
            m_kTextureA,
            m_kTextureB,
            m_kTarget.min,
            m_kTarget.max,
            m_kMoving.min,
            m_kMoving.max,
            m_kTarget.extents[0],
            m_kTarget.extents[1],
            m_kImageTransform);
    /*
    m_kBracketOut = CreateRenderTargetInit( "bracketImage", 1, 3 );

    CreateTransformMesh();
    m_kTransformOut = CreateRenderTargetInit( "Transform", 1, 4 );
    //m_kTransformNewOut = CreateRenderTargetInit( "TransformNew", 1, 4 );
    m_kBracketOut.GetTarget(0).GetImage().SetData( m_afBracket, 1, 4 );
    m_kCalcTransform = new LineMinimizationEffect( m_kBracketOut.GetTarget(0),
            (m_kTarget.nDims == 2),
            m_kToOrigin, m_kFromOrigin,
            m_fRigid, m_fDim, m_afStartPoint, m_afPt, m_fPtLength,
            m_afUnitDirections, m_fUnitTolerance, m_fMinDist );
    m_kTransformPoints.AttachEffect(m_kCalcTransform);

    m_kImageLineMinDual = new LineMinimizationEffect( m_kTextureA, m_kTextureB, m_kTransformOut.GetTarget(0),
            m_kTarget.min, m_kTarget.max, m_kMoving.min, m_kMoving.max,
            m_kTarget.extents[0],m_kTarget.extents[1] );

    CreateBracketMesh();
    m_kBracketNewOut = CreateRenderTargetInit( "bracketNew", 1, 3 );
    */
  }