Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions

iil4mitkImage Class Reference

#include <image.h>

Inheritance diagram for iil4mitkImage:
Inheritance graph
[legend]
Collaboration diagram for iil4mitkImage:
Collaboration graph
[legend]

List of all members.

Public Types

enum  {
  INTENSITY = 0, INTENSITY_ALPHA, COLOR, COLOR_ALPHA,
  RGB, RGBA
}
 

The supported color models.

More...

Public Member Functions

 iil4mitkImage (unsigned int size=128)
 The constructor.
virtual ~iil4mitkImage ()
 The destructor.
virtual void setImage (const unsigned int width, const unsigned int height, const int model, unsigned char *data)
 Sets the image.
unsigned int imageWidth () const
 Gets the image size.
unsigned int imageHeight () const
virtual void setRegion (const unsigned int x, const unsigned y, const unsigned w, const unsigned h)
 Limits the image to the specified region.
unsigned int regionX () const
 Gets the position and the size of the region.
unsigned int regionY () const
unsigned int regionWidth () const
unsigned int regionHeight () const
virtual void clear ()
 Clears the image.
int model () const
 Gets the maximal size of an image.
virtual void setInterpolation (const bool on=true)
 Switches interpolation for scaled images on or off.
bool interpolation () const
 Gets the interpolation method.
virtual void invalidateTextures ()
 Invalidates the content of the textures.
virtual float width () const
 Gets the size of the item.
virtual float height () const
virtual void display (iil4mitkWidget *widget)
 Displays the item.
virtual void remove (iil4mitkWidget *widget)
 Frees the resources of the item for the given widget.

Static Public Member Functions

static iil4mitkImagefind (const iil4mitkItem *item)
 Gets the first image of the item tree.

Protected Member Functions

void drawTextures (iil4mitkWidget *widget)
 Draws the image by means of the textures.
void updateTexture (_iil4mitkTexture *texture, unsigned int x, unsigned int y, unsigned int w, unsigned int h)
 Updates the content of the given texture.
virtual void copyImage (unsigned int x, unsigned int y, unsigned int w, unsigned int h, unsigned char *data, unsigned int width, unsigned int height, unsigned int xoffset, unsigned int yoffset)
 Copies a specified region of the image.
unsigned int bpe ()
 Sets the color model of the image.
unsigned char * pixels ()
 Gets the pixels of the image.

Detailed Description

Definition at line 14 of file image.h.


Member Enumeration Documentation

anonymous enum

The supported color models.

Enumerator:
INTENSITY 
INTENSITY_ALPHA 
COLOR 
COLOR_ALPHA 
RGB 
RGBA 

Definition at line 31 of file image.h.


Constructor & Destructor Documentation

iil4mitkImage::iil4mitkImage ( unsigned int  size = 128 )

The constructor.

Definition at line 21 of file image.cpp.

: _width (0), _height (0), _rx (0), _ry (0), _rw (0), _rh (0),
  _model (INTENSITY), _interpolation (false), _pixels (NULL),
  _internal (GL_LUMINANCE), _size(size)
{

}
iil4mitkImage::~iil4mitkImage (  ) [virtual]

The destructor.

Definition at line 29 of file image.cpp.

References clear().

{
  clear ();
}

Member Function Documentation

unsigned int iil4mitkImage::bpe (  ) [protected]

Sets the color model of the image.

Note:
method needed?

Gets the number of bits per element of the image.

Definition at line 391 of file image.cpp.

Referenced by iil4mitkPicImage::copyImage().

{
  return _bytes [_model] * 8;
}
void iil4mitkImage::clear (  ) [virtual]

Clears the image.

Reimplemented in iil4mitkPicImage.

Definition at line 98 of file image.cpp.

References GL_LUMINANCE, and INTENSITY.

Referenced by ~iil4mitkImage().

{
  _width = _height = 0;
  _rx = _ry = _rw = _rh = 0;
  _model = INTENSITY;
  _pixels = NULL;
  _internal = GL_LUMINANCE;

  std::map<void*,Textures*>::iterator i;
  for (i=_textures.begin(); i!=_textures.end(); ++i) {
    for (unsigned int j = 0; j < (*i).second->size (); j++) {
      delete (*(*i).second)[j];
    }
    delete i->second;
  }
  _textures.clear ();
}
void iil4mitkImage::copyImage ( unsigned int  x,
unsigned int  y,
unsigned int  w,
unsigned int  h,
unsigned char *  data,
unsigned int  width,
unsigned int  height,
unsigned int  xoffset,
unsigned int  yoffset 
) [protected, virtual]

Copies a specified region of the image.

Parameters:
x,y,w,hthe geometry of the region
datathe image data where the region is copied to
width,heightthe geometry of the image data
xoffset,yoffsetthe image point where the region is copied to

Reimplemented in iil4mitkPicImage.

Definition at line 374 of file image.cpp.

References iil4mitkItem::x().

Referenced by updateTexture().

{
  const unsigned int bytes = _bytes [_model];
  const unsigned char *src = _pixels + (y * _width + x) * bytes;
  unsigned char *dst = data + (yoffset * width + xoffset) * bytes;

  for (unsigned int i = 0; i < h; i++) 
  {
    memcpy (dst, src, w * bytes);
    src += _width * bytes;
    dst += width * bytes;
  }
}
void iil4mitkImage::display ( iil4mitkWidget widget ) [virtual]

Displays the item.

Note:
Make sure that the proper OpenGL context has been made current.

Implements iil4mitkItem.

Reimplemented in iil4mitkPicImage.

Definition at line 147 of file image.cpp.

References iil4mitkItem::alpha(), iil4mitkItem::blue(), COLOR_ALPHA, iil4mitkItem::constraint(), drawTextures(), GL_BLEND, GL_CLIP_PLANE4, GL_CLIP_PLANE5, GL_FALSE, GL_MODELVIEW, GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA, GL_TEXTURE_2D, glBlendFunc(), glClipPlane(), glColor4f(), glDisable(), glEnable(), glIsEnabled(), glMatrixMode(), glPopMatrix(), glPushMatrix(), glTranslatef(), iil4mitkItem::green(), INTENSITY_ALPHA, iil4mitkItem::red(), regionHeight(), regionWidth(), RGB, RGBA, and iil4mitkItem::visible().

{
  GLdouble planeX [] = {-1, 0, 0, regionWidth ()};
  GLdouble planeY [] = {0, -1, 0, regionHeight ()};

  if (!visible () 
    || (constraint () && (widget != constraint ())))
  {
    return;
  }

  if (_pixels)
  {
    assert (_rx + _rw <= _width);
    assert (_ry + _rh <= _height);

    GLboolean texturing = glIsEnabled (GL_TEXTURE_2D);
    GLboolean blending = glIsEnabled (GL_BLEND);

    glClipPlane (GL_CLIP_PLANE4, planeX);
    glEnable (GL_CLIP_PLANE4);
    glClipPlane (GL_CLIP_PLANE5, planeY);
    glEnable (GL_CLIP_PLANE5);

    if ((_model == INTENSITY_ALPHA) || (_model == COLOR_ALPHA) || (_model == RGB) || (_model == RGBA))
    {
      glEnable (GL_BLEND);
      glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    }

    glEnable (GL_TEXTURE_2D);
    glMatrixMode (GL_MODELVIEW);
    glPushMatrix ();
    glColor4f (red (), green (), blue (), alpha ());
    glTranslatef (x (), y (), 0.0);
    drawTextures (widget);
    glPopMatrix ();

    glDisable (GL_CLIP_PLANE4);
    glDisable (GL_CLIP_PLANE5);

    if (texturing == GL_FALSE) glDisable (GL_TEXTURE_2D);
    if (blending  == GL_FALSE) glDisable (GL_BLEND);
  }
}
void iil4mitkImage::drawTextures ( iil4mitkWidget widget ) [protected]

Draws the image by means of the textures.

Definition at line 193 of file image.cpp.

References _iil4mitkTexture::bind(), GL_QUADS, glBegin(), glEnd(), glScalef(), glTexCoord2f(), glTranslatef(), glVertex3f(), int(), _iil4mitkTexture::isValid(), power2(), _iil4mitkTexture::setInterpolation(), _iil4mitkTexture::setModel(), _iil4mitkTexture::setSize(), and updateTexture().

Referenced by display().

{
  const unsigned int s = _size; // size of the tiles
  unsigned int n, m;  // number of the tiles

  n = (unsigned int) ceilf ((float) _rw / (float) (s - 2));
  m = (unsigned int) ceilf ((float) _rh / (float) (s - 2));

  /* Allocate memory for the textures */
  
  Textures* textures;
  {
    iil4mitkWidget* w;
    unsigned int available, total;
  
    //w = (widget->IsSharing () ? widget->SharedWidget () : widget);
    w = (false ? NULL : widget);
    std::map<void*,Textures*>::iterator tex_it = _textures.find (w);
    if(tex_it!=_textures.end())
      textures = tex_it->second;
    else
      textures = NULL;

    if (!textures) {
      textures = new Textures ();
      typedef std::pair <void*, std::vector<_iil4mitkTexture*>* > TexturePair;
      _textures.insert (TexturePair(w, textures));
    }
    available = textures->size (); 
    total = n * m;
    //textures->resize (total);
    int iii;
    for (unsigned int i = available; i < total; i++)
    {
      textures->push_back(new _iil4mitkTexture (w));
      iii=textures->size ();
    }
    widget->MakeCurrent ();
  }  

  /* Render the textures */

  glScalef ((float) (s-2), (float) (s-2), 1.0);
  for (unsigned int i = 0; i < n; i++)
  {
    unsigned int pos [2]; // left-top corner of the region
    unsigned int len [2]; // extent of the region
    unsigned int tex [2]; // size of the texture
    float res [2];        // resolution of the texture
    
    pos [0] = _rx+i*(s-2);
          len [0] = (pos[0]+(s-2)>_rw ? _rw-pos[0] : s-2);  
    tex [0] = power2(s);
    res [0] = 1.0f/tex[0];
    for (unsigned int j = 0; j < m; j++) 
    {
      _iil4mitkTexture* texture;
      
      texture = (*textures)[i*m+j];
      pos [1] = _ry+j*(s-2);
      len [1] = (pos[1]+(s-2)>_rh ? _rh-pos[1] : s-2);
      tex [1] = power2(s);
      res [1] = 1.0f/tex[1];
  
      //if (widget->isVisible (pos [0], pos [1], len [0], len [1])) {
      if (true) {
      // widget->makeCurrent ();  
      texture->bind ();
      // widget->makeCurrent ();  
      texture->setSize (tex[0], tex[1]);
      texture->setModel (_model);
      texture->setInterpolation (_interpolation);
      if (!texture->isValid ()) 
      {
        updateTexture (texture, pos[0], pos[1], len[0], len[1]);
      }  

      glBegin (GL_QUADS);
      glTexCoord2f (res[0], res[1]); glVertex3f (0.0, 0.0, 0.0);
      glTexCoord2f (1.0-res[0], res[1]); glVertex3f (1.0, 0.0, 0.0);
      glTexCoord2f (1.0-res[0], 1.0-res[1]); glVertex3f (1.0, 1.0, 0.0);
      glTexCoord2f (res[0], 1.0-res[1]); glVertex3f (0.0, 1.0, 0.0);
      glEnd ();
      }
      glTranslatef (0.0, 1.0, 0.0);
    }
    glTranslatef (1.0, -((float) m), 0.0);
  }
}
iil4mitkImage * iil4mitkImage::find ( const iil4mitkItem item ) [static]

Gets the first image of the item tree.

Reimplemented in iil4mitkPicImage.

Definition at line 403 of file image.cpp.

{
  iil4mitkImage* result = NULL;

  if (!item) return (iil4mitkImage *) NULL;

  if ( dynamic_cast<const iil4mitkImage*>(item)!=NULL ) {
      result = const_cast<iil4mitkImage*>(dynamic_cast<const iil4mitkImage*>(item));
  }
  return (iil4mitkImage *) result;
}
float iil4mitkImage::height (  ) const [virtual]

Reimplemented from iil4mitkItem.

Definition at line 123 of file image.cpp.

Referenced by mitk::ImageMapperGL2D::Paint(), and setImage().

{
  return (float) _rh;
}
unsigned int iil4mitkImage::imageHeight (  ) const

Definition at line 55 of file image.cpp.

{
  return _height;
}
unsigned int iil4mitkImage::imageWidth (  ) const

Gets the image size.

Definition at line 49 of file image.cpp.

{
  return _width;
}
bool iil4mitkImage::interpolation (  ) const

Gets the interpolation method.

Definition at line 141 of file image.cpp.

{
  return _interpolation;
}
void iil4mitkImage::invalidateTextures (  ) [virtual]

Invalidates the content of the textures.

Definition at line 297 of file image.cpp.

Referenced by iil4mitkPicImage::setBinary(), iil4mitkPicImage::setExtrema(), setImage(), iil4mitkPicImage::setMask(), iil4mitkPicImage::setOpacityExtrema(), iil4mitkPicImage::setOpacityWindow(), setRegion(), and iil4mitkPicImage::setWindow().

{
  std::map<void*,Textures*>::iterator i;
  for (i=_textures.begin(); i!=_textures.end(); ++i) 
  {
    for (unsigned int j = 0; j < (*i).second->size (); j++) 
    {
      (*(*i).second)[j]->invalidate ();
    }  
  }
}
int iil4mitkImage::model (  ) const

Gets the maximal size of an image.

Gets the color model of the image.

Definition at line 129 of file image.cpp.

Referenced by iil4mitkPicImage::copyImage(), and setImage().

{
  return _model;
}
unsigned char * iil4mitkImage::pixels (  ) [protected]

Gets the pixels of the image.

Definition at line 397 of file image.cpp.

{
  return _pixels;
}
unsigned int iil4mitkImage::regionHeight (  ) const

Definition at line 92 of file image.cpp.

Referenced by display().

{
  return _rh;
}
unsigned int iil4mitkImage::regionWidth (  ) const

Definition at line 86 of file image.cpp.

Referenced by display().

{
  return _rw;
}
unsigned int iil4mitkImage::regionX (  ) const

Gets the position and the size of the region.

Definition at line 74 of file image.cpp.

{
  return _rx;
}
unsigned int iil4mitkImage::regionY (  ) const

Definition at line 80 of file image.cpp.

{
  return _ry;
}
void iil4mitkImage::remove ( iil4mitkWidget widget ) [virtual]

Frees the resources of the item for the given widget.

Note:
Make sure that the proper OpenGL context has been made current.

Reimplemented from iil4mitkItem.

Definition at line 284 of file image.cpp.

{
  std::map<void*,Textures*>::iterator i =
   _textures.find (widget);
  for (unsigned int j = 0; j < (*i).second->size (); j++) 
  {
    delete (*(*i).second)[j];
  }  
  delete i->second;
  _textures.erase(i);
}
void iil4mitkImage::setImage ( const unsigned int  width,
const unsigned int  height,
const int  model,
unsigned char *  data 
) [virtual]

Sets the image.

Parameters:
width,heightthe image size. Make sure that the dimensions have a power of two. Note that the image size is limited (see maxDimensions).
modelthe color model
datathe data
Note:
Make sure that the data keeps valid until this instance is destroyed.

Definition at line 35 of file image.cpp.

References height(), invalidateTextures(), model(), and width().

{
  assert (width > 0);
  assert (height > 0);

  _width = width;
  _height = height;
  _model = model;
  _pixels = data;
  invalidateTextures ();
}
void iil4mitkImage::setInterpolation ( const bool  on = true ) [virtual]

Switches interpolation for scaled images on or off.

Definition at line 135 of file image.cpp.

Referenced by mitk::ImageMapperGL2D::GenerateData(), and mitk::ImageMapperGL2D::Paint().

{
  _interpolation = on;
}
void iil4mitkImage::setRegion ( const unsigned int  x,
const unsigned  y,
const unsigned  w,
const unsigned  h 
) [virtual]

Limits the image to the specified region.

Parameters:
x,y,w,hthe position and size of the region

Definition at line 61 of file image.cpp.

References invalidateTextures(), iil4mitkItem::x(), and iil4mitkItem::y().

Referenced by mitk::ImageMapperGL2D::GenerateData().

{
  assert (x + w <= _width);
  assert (y + h <= _height);

  if ((_rx != x) || (_ry != y) || (_rw != w) || (_rh != h)) {
    _rx = x; _ry = y; _rw = w; _rh = h;
    invalidateTextures ();
  }
}
void iil4mitkImage::updateTexture ( _iil4mitkTexture texture,
unsigned int  x,
unsigned int  y,
unsigned int  w,
unsigned int  h 
) [protected]

Updates the content of the given texture.

Definition at line 310 of file image.cpp.

References copyImage(), _iil4mitkTexture::height(), _iil4mitkTexture::setData(), and _iil4mitkTexture::width().

Referenced by drawTextures().

{
  unsigned int p2w = texture->width ();
  unsigned int p2h = texture->height ();
  unsigned int size = p2w * p2h * _bytes [_model];
  unsigned char *region = (unsigned char *) malloc (size * sizeof (unsigned char));
  unsigned int left = (x == 0 ? 0 : 1);
  unsigned int right = (x+w == _rw ? 0 : 1);
  unsigned int top = (y == 0 ? 0 : 1);
  unsigned int bottom = (y + h == _rh ? 0 : 1);

  if (x == 0) 
  {
    copyImage (x, y-top, 1, h+top+bottom, region, p2w, p2h, 0, 1-top);
  }
  else 
  {
    copyImage (x-1, y-top, 1, h+top+bottom, region, p2w, p2h, 0, 1-top);
  }
  if (x+w == _rw) 
  {
    copyImage (x+w-1, y-top, 1, h+top+bottom, region, p2w, p2h, w+1, 1-top);
  }
  else
  {
    copyImage (x+w, y-top, 1, h+top+bottom, region, p2w, p2h, w+1, 1-top);
  }
  if (y == 0) 
  {
    copyImage (x-left, y, w+left+right, 1, region, p2w, p2h, 1-left, 0);
  }
  else
  {
    copyImage (x-left, y-1, w+left+right, 1, region, p2w, p2h, 1-left, 0);
  }
  if (y+h == _rh)
  {
    copyImage (x-left, y+h-1, w+left+right, 1, region, p2w, p2h, 1-left, h+1);
  }
  else 
  {
    copyImage (x-left, y+h, w+left+right, 1, region, p2w, p2h, 1-left, h+1);
  }
  copyImage (x, y, w, h, region, p2w, p2h, 1, 1);
  texture->setData (region);

  /*
  char filename [255];
  sprintf (filename, "tile_%u_%u.pic", x, y);
  mitkIpPicDescriptor* pic = mitkIpPicNew ();
  pic->type = mitkIpPicUInt;
  pic->bpe = 8;
  pic->dim = 2;
  pic->n [0] = p2w;
  pic->n [1] = p2h;
  pic->data = region;
  mitkIpPicPut (filename, pic);
  pic->data = NULL;
  mitkIpPicFree (pic);
  */

  free (region);
}
float iil4mitkImage::width (  ) const [virtual]

Gets the size of the item.

Note:
The method may be helpful for picking.

Reimplemented from iil4mitkItem.

Definition at line 117 of file image.cpp.

Referenced by mitk::ImageMapperGL2D::Paint(), setImage(), and iil4mitkPicImage::setOutlineWidth().

{
  return (float) _rw;
}

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines