<commit_before>/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkOpenGLImageActor.cxx

  Copyright (c) <NAME>, <NAME>, <NAME>
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkOpenGLImageActor.h"

#include "vtkMapper.h"
#include "vtkObjectFactory.h"
#include "vtkOpenGLRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkImageData.h"
#include "vtkMatrix4x4.h"
#include "vtkOpenGLRenderWindow.h"

#include <math.h>

#include "vtkOpenGL.h"
#include "vtkgl.h" // vtkgl namespace

#ifndef VTK_IMPLEMENT_MESA_CXX
vtkStandardNewMacro(vtkOpenGLImageActor);
#endif

// Initializes an instance, generates a unique index.
vtkOpenGLImageActor::vtkOpenGLImageActor()
{
  this->Index = 0;
  this->RenderWindow = 0;
  this->TextureSize[0] = 0;
  this->TextureSize[1] = 0;
  this->TextureBytesPerPixel = 1;
}

vtkOpenGLImageActor::~vtkOpenGLImageActor()
{
  this->RenderWindow = NULL;
}

// Release the graphics resources used by this texture.  
void vtkOpenGLImageActor::ReleaseGraphicsResources(vtkWindow *renWin)
{
  if (this->Index && renWin && renWin->GetMapped())
    {
    static_cast<vtkRenderWindow *>(renWin)->MakeCurrent();
#ifdef GL_VERSION_1_1
    // free any textures
    if (glIsTexture(this->Index))
      {
      GLuint tempIndex;
      tempIndex = this->Index;
      // NOTE: Sun's OpenGL seems to require disabling of texture before delete
      glDisable(GL_TEXTURE_2D);
      glDeleteTextures(1, &tempIndex);
      }
#else
    if (glIsList(this->Index))
      {
      glDeleteLists(this->Index,1);
      }
#endif
    this->TextureSize[0] = 0;
    this->TextureSize[1] = 0;
    this->TextureBytesPerPixel = 1;
    }
  this->Index = 0;
  this->RenderWindow = NULL;
  this->Modified();
}

unsigned char *vtkOpenGLImageActor::MakeDataSuitable(int &xsize, int &ysize,
                                                     int &release,
                                                     int &reuseTexture)
{
  int contiguous = 0;
  unsigned short xs,ys;
  int powOfTwo = 0;
  int numComp = this->Input->GetNumberOfScalarComponents();
  int xdim, ydim;

  reuseTexture = 0;
  
  // it must be a power of two and contiguous
  // find the two used dimensions
  // this assumes a 2D image, no lines here folk
  if (this->ComputedDisplayExtent[0] != this->ComputedDisplayExtent[1])
    {
    xdim = 0;
    if (this->ComputedDisplayExtent[2] != this->ComputedDisplayExtent[3])
      {
      ydim = 1;
      }
    else
      {
      ydim = 2;
      }
    }
  else
    {
    xdim = 1;
    ydim = 2;
    }
  
  double *spacing = this->Input->GetSpacing();
  double *origin = this->Input->GetOrigin();
  
  // compute the world coordinates
  this->Coords[0] = this->ComputedDisplayExtent[0]*spacing[0] + origin[0];
  this->Coords[1] = this->ComputedDisplayExtent[2]*spacing[1] + origin[1];
  this->Coords[2] = this->ComputedDisplayExtent[4]*spacing[2] + origin[2];
  this->Coords[3] = this->ComputedDisplayExtent[1]*spacing[0] + origin[0];
  this->Coords[4] = 
    this->ComputedDisplayExtent[2 + (xdim == 1)]*spacing[1] + origin[1];
  this->Coords[5] = this->ComputedDisplayExtent[4]*spacing[2] + origin[2];
  this->Coords[6] = this->ComputedDisplayExtent[1]*spacing[0] + origin[0];
  this->Coords[7] = this->ComputedDisplayExtent[3]*spacing[1] + origin[1];
  this->Coords[8] = this->ComputedDisplayExtent[5]*spacing[2] + origin[2];
  this->Coords[9] = this->ComputedDisplayExtent[0]*spacing[0] + origin[0];
  this->Coords[10] = 
    this->ComputedDisplayExtent[2 + (ydim == 1)]*spacing[1] + origin[1];
  this->Coords[11] = this->ComputedDisplayExtent[5]*spacing[2] + origin[2];
  
  // now contiguous would require that xdim = 0 and ydim = 1
  // OR xextent = 1 pixel and xdim = 1 and ydim = 2 
  // OR xdim = 0 and ydim = 2 and yextent = i pixel. In addition
  // the corresponding x display extents must match the 
  // extent of the data
  int *ext = this->Input->GetExtent();
  
  if ( ( xdim == 0 && ydim == 1 && 
         this->ComputedDisplayExtent[0] == ext[0] && 
         this->ComputedDisplayExtent[1] == ext[1] )||
       ( ext[0] == ext[1] && xdim == 1 && 
         this->ComputedDisplayExtent[2] == ext[2] && 
         this->ComputedDisplayExtent[3] == ext[3] ) ||
       ( ext[2] == ext[3] && xdim == 0 && ydim == 2 &&
         this->ComputedDisplayExtent[0] == ext[0] && 
         this->ComputedDisplayExtent[1] == ext[1] ) )
    {
    contiguous = 1;
    }
      
  // if contiguous is it a pow of 2
  if (contiguous)
    {
    xsize = ext[xdim*2+1] - ext[xdim*2] + 1;
    // xsize and ysize must be a power of 2 in OpenGL
    xs = static_cast<unsigned short>(xsize);
    while (!(xs & 0x01))
      {
      xs = xs >> 1;
      }
    if (xs == 1)
      {
      powOfTwo = 1;
      }
    }
  
  if (contiguous && powOfTwo)
    {
    // can we make y a power of two also ?
    ysize = (this->ComputedDisplayExtent[ydim*2+1] -
             this->ComputedDisplayExtent[ydim*2] + 1);
    ys = static_cast<unsigned short>(ysize);
    while (!(ys & 0x01))
      {
      ys = ys >> 1;
      }
    // yes it is a power of two already
    if (ys == 1)
      {
      release = 0;
      this->TCoords[0] = (this->ComputedDisplayExtent[xdim*2] - 
                          ext[xdim*2] + 0.5)/xsize;
      this->TCoords[1] = 0.5/ysize;  
      this->TCoords[2] = (this->ComputedDisplayExtent[xdim*2+1] -
                          ext[xdim*2] + 0.5)/xsize;
      this->TCoords[3] = this->TCoords[1];  
      this->TCoords[4] = this->TCoords[2];
      this->TCoords[5] = 1.0 - 0.5/ysize;  
      this->TCoords[6] = this->TCoords[0];
      this->TCoords[7] = this->TCoords[5];

#ifdef GL_VERSION_1_1
      // if texture size hasn't changed, reuse old texture<commit_msg>BUG: Fix bug 0011613, infinite loop on empty extent.<commit_after>/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkOpenGLImageActor.cxx

  Copyright (c) <NAME>, <NAME>, <NAME>
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkOpenGLImageActor.h"

#include "vtkMapper.h"
#include "vtkObjectFactory.h"
#include "vtkOpenGLRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkImageData.h"
#include "vtkMatrix4x4.h"
#include "vtkOpenGLRenderWindow.h"

#include <math.h>

#include "vtkOpenGL.h"
#include "vtkgl.h" // vtkgl namespace

#ifndef VTK_IMPLEMENT_MESA_CXX
vtkStandardNewMacro(vtkOpenGLImageActor);
#endif

// Initializes an instance, generates a unique index.
vtkOpenGLImageActor::vtkOpenGLImageActor()
{
  this->Index = 0;
  this->RenderWindow = 0;
  this->TextureSize[0] = 0;
  this->TextureSize[1] = 0;
  this->TextureBytesPerPixel = 1;
}

vtkOpenGLImageActor::~vtkOpenGLImageActor()
{
  this->RenderWindow = NULL;
}

// Release the graphics resources used by this texture.  
void vtkOpenGLImageActor::ReleaseGraphicsResources(vtkWindow *renWin)
{
  if (this->Index && renWin && renWin->GetMapped())
    {
    static_cast<vtkRenderWindow *>(renWin)->MakeCurrent();
#ifdef GL_VERSION_1_1
    // free any textures
    if (glIsTexture(this->Index))
      {
      GLuint tempIndex;
      tempIndex = this->Index;
      // NOTE: Sun's OpenGL seems to require disabling of texture before delete
      glDisable(GL_TEXTURE_2D);
      glDeleteTextures(1, &tempIndex);
      }
#else
    if (glIsList(this->Index))
      {
      glDeleteLists(this->Index,1);
      }
#endif
    this->TextureSize[0] = 0;
    this->TextureSize[1] = 0;
    this->TextureBytesPerPixel = 1;
    }
  this->Index = 0;
  this->RenderWindow = NULL;
  this->Modified();
}

unsigned char *vtkOpenGLImageActor::MakeDataSuitable(int &xsize, int &ysize,
                                                     int &release,
                                                     int &reuseTexture)
{
  int contiguous = 0;
  int numComp = this->Input->GetNumberOfScalarComponents();
  int xdim, ydim;

  reuseTexture = 0;
  
  // it must be a power of two and contiguous
  // find the two used dimensions
  // this assumes a 2D image, no lines here folk
  if (this->ComputedDisplayExtent[0] != this->ComputedDisplayExtent[1])
    {
    xdim = 0;
    if (this->ComputedDisplayExtent[2] != this->ComputedDisplayExtent[3])
      {
      ydim = 1;
      }
    else
      {
      ydim = 2;
      }
    }
  else
    {
    xdim = 1;
    ydim = 2;
    }
  
  double *spacing = this->Input->GetSpacing();
  double *origin = this->Input->GetOrigin();
  
  // compute the world coordinates
  this->Coords[0] = this->ComputedDisplayExtent[0]*spacing[0] + origin[0];
  this->Coords[1] = this->ComputedDisplayExtent[2]*spacing[1] + origin[1];
  this->Coords[2] = this->ComputedDisplayExtent[4]*spacing[2] + origin[2];
  this->Coords[3] = this->ComputedDisplayExtent[1]*spacing[0] + origin[0];
  this->Coords[4] = 
    this->ComputedDisplayExtent[2 + (xdim == 1)]*spacing[1] + origin[1];
  this->Coords[5] = this->ComputedDisplayExtent[4]*spacing[2] + origin[2];
  this->Coords[6] = this->ComputedDisplayExtent[1]*spacing[0] + origin[0];
  this->Coords[7] = this->ComputedDisplayExtent[3]*spacing[1] + origin[1];
  this->Coords[8] = this->ComputedDisplayExtent[5]*spacing[2] + origin[2];
  this->Coords[9] = this->ComputedDisplayExtent[0]*spacing[0] + origin[0];
  this->Coords[10] = 
    this->ComputedDisplayExtent[2 + (ydim == 1)]*spacing[1] + origin[1];
  this->Coords[11] = this->ComputedDisplayExtent[5]*spacing[2] + origin[2];
  
  // now contiguous would require that xdim = 0 and ydim = 1
  // OR xextent = 1 pixel and xdim = 1 and ydim = 2 
  // OR xdim = 0 and ydim = 2 and yextent = i pixel. In addition
  // the corresponding x display extents must match the 
  // extent of the data
  int *ext = this->Input->GetExtent();
  
  if ( ( xdim == 0 && ydim == 1 && 
         this->ComputedDisplayExtent[0] == ext[0] && 
         this->ComputedDisplayExtent[1] == ext[1] )||
       ( ext[0] == ext[1] && xdim == 1 && 
         this->ComputedDisplayExtent[2] == ext[2] && 
         this->ComputedDisplayExtent[3] == ext[3] ) ||
       ( ext[2] == ext[3] && xdim == 0 && ydim == 2 &&
         this->ComputedDisplayExtent[0] == ext[0] && 
         this->ComputedDisplayExtent[1] == ext[1] ) )
    {
    contiguous = 1;
    }
      
  if (contiguous)
    {
    xsize = (this->ComputedDisplayExtent[xdim*2+1] -
             this->ComputedDisplayExtent[xdim*2] + 1);
    ysize = (this->ComputedDisplayExtent[ydim*2+1] -
             this->ComputedDisplayExtent[ydim*2] + 1);
    // are the dimensions a power of two already?
    if (xsize > 0 && (xsize & (xsize-1)) == 0 &&
        ysize > 0 && (ysize & (ysize-1)) == 0)
      {
      release = 0;
      this->TCoords[0] = (this->ComputedDisplayExtent[xdim*2] - 
                          ext[xdim*2] + 0.5)/xsize;
      this->TCoords[1] = 0.5/ysize;  
      this->TCoords[2] = (this->ComputedDisplayExtent[xdim*2+1] -
                          ext[xdim*2] + 0.5)/xsize;
      this->TCoords[3] = this->TCoords[1];  
      this->TCoords[4] = this->TCoords[2];
      this->TCoords[5] = 1.0 - 0.5/ysize;  
      this->TCoords[6] = this->TCoords[0];
      this->TCoords[7] = this->TCoords[5];

#ifdef GL_VERSION_1_1
      // if texture size hasn't changed, reuse old texture
{'fraction_non_alphanumeric': 0.16905082463816898, 'fraction_numerical': 0.027684281386738473, 'mean_word_length': 2.9380384360503644, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 177, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4917272', 'n_tokens_mistral': 4281, 'n_tokens_neox': 4077, 'n_words': 869}