File: LabelToRGBAFilter.h

package info (click to toggle)
itksnap 3.6.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 22,132 kB
  • sloc: cpp: 91,089; ansic: 1,994; sh: 327; makefile: 16
file content (126 lines) | stat: -rw-r--r-- 4,068 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: LabelToRGBAFilter.h,v $
  Language:  C++
  Date:      $Date: 2009/10/26 16:22:52 $
  Version:   $Revision: 1.2 $
  Copyright (c) 2003 Insight Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/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 notices for more information.
=========================================================================*/
#ifndef __LabelToRGBAFilter_h_
#define __LabelToRGBAFilter_h_

#include "SNAPCommon.h"
#include "itkImage.h"
#include "itkRGBAPixel.h"
#include "itkImageToImageFilter.h"
#include "ColorLabelTable.h"

#include <itkRGBAPixel.h>
#include <itkNumericTraitsRGBAPixel.h>

/**
 * \class LabelToRGBAFilter
 * \brief Simple filter that maps label image to RGB color image
 */
class LabelToRGBAFilter: 
  public itk::ImageToImageFilter<
  itk::Image<LabelType, 2> , itk::Image<itk::RGBAPixel<unsigned char>,2> >
{
public:
  
  /** Pixel Type of the input image */
  typedef LabelType                                      InputPixelType;
  typedef itk::Image<InputPixelType, 2>                  InputImageType;
  typedef itk::SmartPointer<InputImageType>           InputImagePointer;

  /** Pixel Type of the output image */
  typedef itk::RGBAPixel<unsigned char>                 OutputPixelType;
  typedef itk::Image<OutputPixelType, 2>                OutputImageType;
  typedef itk::SmartPointer<OutputImageType>         OutputImagePointer;

  /** Standard class typedefs. */
  typedef LabelToRGBAFilter                                        Self;
  typedef itk::ImageToImageFilter<InputImageType,OutputImageType>  Superclass;
  typedef itk::SmartPointer<Self>                               Pointer;
  typedef itk::SmartPointer<const Self>                    ConstPointer;  
  
  /** Method for creation through the object factory. */
  itkNewMacro(Self)
    
  /** Image dimension. */
  itkStaticConstMacro(ImageDimension, unsigned int,
                      InputImageType::ImageDimension);

  /** Set color table macro */
  void SetColorTable(ColorLabelTable *table)
  {
    m_ColorTable = table;
    this->SetNthInput(1, table);
  }
  
  /** Get color table */
  ColorLabelTable *GetColorTable()
  {
    return m_ColorTable;
  }

protected:

  void PrintSelf(std::ostream& os, itk::Indent indent) const
    { os << indent << "LabelToRGBAFilter"; }
  
  /** Generate Data */
  void GenerateData( void )
    {
    // Here's the input and output
    InputImageType::ConstPointer inputPtr = this->GetInput();
    OutputImageType::Pointer outputPtr = this->GetOutput();

    // Get the number of pixels in the input
    size_t n = inputPtr->GetBufferedRegion().GetNumberOfPixels();

    // Allocate output if needed
    if(outputPtr->GetBufferedRegion().GetNumberOfPixels() != n)
      {
      outputPtr->SetBufferedRegion(inputPtr->GetBufferedRegion());
      outputPtr->Allocate();
      }

    // Get the clear label
    const ColorLabel &clear = m_ColorTable->GetColorLabel(0);
    const ColorLabel *cllast = &clear;

    // Keep track of the last pixel - this reduces the amount of color label
    // lookups, taking advantage of the fact that segmentations are homogeneous
    InputPixelType last_pixel = 0;

    // Simple loop
    const LabelType *xin = inputPtr->GetBufferPointer(), *xinend = xin + n;
    OutputPixelType *xout = outputPtr->GetBufferPointer();
    for(; xin < xinend; ++xin, ++xout)
      {
      if(*xin != last_pixel)
        {
        last_pixel = *xin;
        const ColorLabel &cl = m_ColorTable->GetColorLabel(last_pixel);
        if(cl.IsVisible())
          cllast = &cl;
        else
          cllast = &clear;
        }

      cllast->GetRGBAVector(xout->GetDataPointer());
      }
    }

private:
  ColorLabelTable *m_ColorTable;
};

#endif