protected void CreateScene(GLAutoDrawable arg0) {
    int iWidth = m_iWidth;
    int iHeight = m_iHeight;

    int iDepth = m_kImageA.nDims == 3 ? m_kImageA.extents[2] : 1;
    m_kHistogramOutput = CreateRenderTarget(arg0, "Histogram2D", iWidth, iHeight);
    m_kHistogramOutputB = CreateRenderTarget(arg0, "Histogram2DB", iWidth, iHeight);
    m_kEntropyOut = CreateRenderTarget(arg0, "EntropyOut", 1, 1);

    if (CreateImageMesh(m_kImageA.extents[0], m_kImageA.extents[1], iDepth) == null) {
      CreateImageMesh(m_kImageA.extents[0], m_kImageA.extents[1], 1);
      m_iRenderLoops = iDepth;
      System.err.println("Switching to 2.5D");
    }
    m_kImagePointsDual.AttachEffect(m_kImageEffectDual);
    m_kImageEffectDual.SetImageSize(m_kImageA.extents[0], m_kImageA.extents[1], iDepth);

    CreateHistogramMesh(m_iWidth, m_iHeight);
    m_akCollapse2D =
        new VolumeHistogramEffect(m_kHistogramOutput.GetTarget(0), VolumeHistogramEffect.NONE);
    m_akCollapseColumns =
        new VolumeHistogramEffect(
            m_kHistogramOutput.GetTarget(0), VolumeHistogramEffect.COLLAPSE_COLUMNS);
    m_akCollapseRows =
        new VolumeHistogramEffect(
            m_kHistogramOutput.GetTarget(0), VolumeHistogramEffect.COLLAPSE_ROWS);

    m_kHistogramPoints2D.AttachEffect(m_akCollapse2D);
    AlphaState pkAState = m_akCollapse2D.GetBlending(0);
    pkAState.BlendEnabled = true;
    pkAState.SrcBlend = AlphaState.SrcBlendMode.SBF_ONE;
    pkAState.DstBlend = AlphaState.DstBlendMode.DBF_ONE;

    m_kHistogramPoints2D.AttachEffect(m_akCollapseColumns);
    pkAState = m_akCollapseColumns.GetBlending(0);
    pkAState.BlendEnabled = true;
    pkAState.SrcBlend = AlphaState.SrcBlendMode.SBF_ONE;
    pkAState.DstBlend = AlphaState.DstBlendMode.DBF_ONE;

    m_kHistogramPoints2D.AttachEffect(m_akCollapseRows);
    pkAState = m_akCollapseRows.GetBlending(0);
    pkAState.BlendEnabled = true;
    pkAState.SrcBlend = AlphaState.SrcBlendMode.SBF_ONE;
    pkAState.DstBlend = AlphaState.DstBlendMode.DBF_ONE;

    double dSize = m_kImageA.dataSize;
    m_akImageReduceEntropy = new ImageReduceEffect(m_kHistogramOutputB.GetTarget(0), dSize);
    m_kEntropyPoints2D.AttachEffect(m_akImageReduceEntropy);
    /*
          m_kImageLineMinPass2a = new LineMinimizationEffect( m_kBracketOut.GetTarget(0), m_kEntropyOut.GetTarget(0), m_fMinDist, (float)dSize, m_kImageA.nDims  );
          m_kImageLineMinDual.SetImageSize( m_kImageA.extents[0],m_kImageA.extents[1],iDepth );
    */
  }
  private void cleanUp() {
    // System.err.println( "VolumeImageViewerPoint dispose()" );
    if (m_kHistogramPoints2D != null) {
      m_pkRenderer.ReleaseResources(m_kHistogramPoints2D);
      m_kHistogramPoints2D.dispose();
      m_kHistogramPoints2D = null;
    }
    if (m_kEntropyPoints2D != null) {
      m_pkRenderer.ReleaseResources(m_kEntropyPoints2D);
      m_kEntropyPoints2D.dispose();
      m_kEntropyPoints2D = null;
    }
    if (m_kImagePointsDual != null) {
      m_kImagePointsDual.DetachAllEffects();
      m_pkRenderer.ReleaseResources(m_kImagePointsDual);
      m_kImagePointsDual.dispose();
      m_kImagePointsDual = null;
    }

    if (m_kHistogramOutput != null) {
      m_kHistogramOutput.GetTarget(0).GetImage().dispose();
      m_kHistogramOutput.GetTarget(0).dispose();
      m_kHistogramOutput.TerminateBuffer();
      m_kHistogramOutput.dispose();
    }
    if (m_kHistogramOutputB != null) {
      m_kHistogramOutputB.GetTarget(0).GetImage().dispose();
      m_kHistogramOutputB.GetTarget(0).dispose();
      m_kHistogramOutputB.TerminateBuffer();
      m_kHistogramOutputB.dispose();
    }
    if (m_kEntropyOut != null) {
      m_kEntropyOut.GetTarget(0).GetImage().dispose();
      m_kEntropyOut.GetTarget(0).dispose();
      m_kEntropyOut.TerminateBuffer();
      m_kEntropyOut.dispose();
    }
    if (m_akImageReduceEntropy != null) {
      m_pkRenderer.ReleaseResources(m_akImageReduceEntropy);
      m_akImageReduceEntropy.dispose();
    }
    if (m_akCollapse2D != null) {
      m_pkRenderer.ReleaseResources(m_akCollapse2D);
      m_akCollapse2D.dispose();
    }
    if (m_akCollapseColumns != null) {
      m_pkRenderer.ReleaseResources(m_akCollapseColumns);
      m_akCollapseColumns.dispose();
    }
    if (m_akCollapseRows != null) {
      m_pkRenderer.ReleaseResources(m_akCollapseRows);
      m_akCollapseRows.dispose();
    }
  }
  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 OpenGLFrameBuffer CreateRenderTarget(
      GLAutoDrawable arg0, String kImageName, int iWidth, int iHeight) {
    // System.err.println( kImageName + " " + iWidth + " " + iHeight );
    float[] afData = new float[iWidth * iHeight * 4];
    GraphicsImage pkSceneImage =
        new GraphicsImage(GraphicsImage.FormatMode.IT_RGBA32, iWidth, iHeight, afData, kImageName);

    Texture[] akSceneTarget = new Texture[1];
    akSceneTarget[0] = new Texture();
    akSceneTarget[0].SetImage(pkSceneImage);
    akSceneTarget[0].SetShared(false);
    akSceneTarget[0].SetFilterType(Texture.FilterType.NEAREST);
    akSceneTarget[0].SetWrapType(0, Texture.WrapType.CLAMP);
    akSceneTarget[0].SetWrapType(1, Texture.WrapType.CLAMP);
    m_pkRenderer.LoadTexture(akSceneTarget[0]);
    OpenGLFrameBuffer kFB =
        new OpenGLFrameBuffer(
            m_eFormat, m_eDepth, m_eStencil, m_eBuffering, m_eMultisampling, m_pkRenderer, arg0);
    kFB.InitializeBuffer(akSceneTarget);
    return kFB;
  }
  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 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 initLineMin(
      Matrix4f kToOrigin,
      Matrix4f kFromOrigin,
      float rigid,
      float dim,
      double[] startPoint,
      double[] pt,
      int ptLength,
      double[] unitDirections,
      double unit_tolerance,
      double fMinDist,
      double bracketA,
      double functionA,
      double bracketB,
      double functionB,
      double bracketC,
      double functionC) {
    m_fUnitTolerance = (float) unit_tolerance;
    m_kToOrigin = convertTo4D(kToOrigin);
    m_kFromOrigin = convertTo4D(kFromOrigin);
    m_fRigid = rigid;
    m_fDim = dim;

    for (int i = 0; i < startPoint.length; i++) {
      m_afStartPoint[i] = (float) startPoint[i];
    }
    for (int i = 0; i < unitDirections.length; i++) {
      m_afUnitDirections[i] = (float) unitDirections[i];
    }
    for (int i = 0; i < pt.length; i++) {
      m_afPt[i] = (float) pt[i];
    }
    m_fPtLength = ptLength;
    m_fMinDist = (float) fMinDist;

    if (m_kImageLineMinDual != null) {
      m_kImageLineMinDual.updateParameters(
          m_kToOrigin,
          m_kFromOrigin,
          m_fRigid,
          m_fDim,
          m_afStartPoint,
          m_afPt,
          m_fPtLength,
          m_afUnitDirections,
          m_fUnitTolerance,
          m_fMinDist);
    }
    if (m_kCalcTransform != null) {
      m_kCalcTransform.updateParameters(
          m_kToOrigin,
          m_kFromOrigin,
          m_fRigid,
          m_fDim,
          m_afStartPoint,
          m_afPt,
          m_fPtLength,
          m_afUnitDirections,
          m_fUnitTolerance,
          m_fMinDist);
    }
    if (m_kImageLineMinPass2a != null) {
      m_kImageLineMinPass2a.setMinDist(m_fMinDist);
    }

    int index = 0;
    m_afBracket[index++] = (float) bracketA;
    m_afBracket[index++] = (float) functionA;
    m_afBracket[index++] = -1f;
    m_afBracket[index++] = 0f;

    m_afBracket[index++] = (float) bracketB;
    m_afBracket[index++] = (float) functionB;
    m_afBracket[index++] = 0f;
    m_afBracket[index++] = 0f;

    m_afBracket[index++] = (float) bracketC;
    m_afBracket[index++] = (float) functionC;
    m_afBracket[index++] = 0f;
    m_afBracket[index++] = 0f;

    if (m_kBracketOut != null) {
      if (m_kBracketOut.GetTarget(0) != null) {
        if (m_kBracketOut.GetTarget(0).GetImage() != null) {
          m_kBracketOut.GetTarget(0).GetImage().SetData(m_afBracket, 1, 3);
          m_kBracketOut.GetTarget(0).Reload(true);
        }
      }
    }
  }
  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();
  }