void DataManager::GenerateLookupTable()
{
  // compute vtkLookupTable colors for labels, first color is black for background
  // rest are pre-calculated by Build() based on the number of labels
  double rgba[4]{ 0.05, 0.05, 0.05, HIGHLIGHT_ALPHA };
  auto labels = m_labelMap->GetNumberOfLabelObjects();
  Q_ASSERT(0 != labels);

  m_lookupTable->Allocate();
  m_lookupTable->SetNumberOfTableValues(labels + 1);
  m_lookupTable->SetTableRange(0, labels);
  m_lookupTable->SetTableValue(0, rgba);

  auto temporal_table = vtkSmartPointer<vtkLookupTable>::New();
  temporal_table->SetNumberOfTableValues(labels);
  temporal_table->SetRange(0, labels-1);
  temporal_table->Build();

  for (unsigned int index = 0; index < labels; ++index)
  {
    temporal_table->GetTableValue(index, rgba);
    m_lookupTable->SetTableValue(index + 1, rgba[0], rgba[1], rgba[2], DIM_ALPHA);
  }
}
Ejemplo n.º 2
0
bool Exporter::exportImageFormat(ImageDataObject & image, const QString & fileName)
{
    QString ext = QFileInfo(fileName).suffix().toLower();

    vtkSmartPointer<vtkImageWriter> writer;

    if (ext == "png")
    {
        writer = vtkSmartPointer<vtkPNGWriter>::New();
    }
    else if (ext == "jpg" || ext == "jpeg")
    {
        writer = vtkSmartPointer<vtkJPEGWriter>::New();
    }
    else if (ext == "bmp")
    {
        writer = vtkSmartPointer<vtkBMPWriter>::New();
    }

    if (!writer)
    {
        return false;
    }

    const auto scalars = image.dataSet()->GetPointData()->GetScalars();
    if (!scalars)
    {
        return false;
    }

    const auto components = scalars->GetNumberOfComponents();
    if (components != 1 && components != 3 && components != 4)
    {
        return false;
    }

    if (scalars->GetDataType() == VTK_UNSIGNED_CHAR)
    {
        writer->SetInputData(image.dataSet());
    }
    else
    {
        auto toUChar = vtkSmartPointer<ImageMapToColors>::New();
        toUChar->SetInputData(image.dataSet());

        auto lut = vtkSmartPointer<vtkLookupTable>::New();
        lut->SetNumberOfTableValues(0xFF);
        lut->SetHueRange(0, 0);
        lut->SetSaturationRange(0, 0);
        lut->SetValueRange(0, 1);

        ValueRange<> totalRange;
            
        for (int c = 0; c < components; ++c)
        {
            ValueRange<> range;
            scalars->GetRange(range.data(), c);
            totalRange.add(range);
        }

        toUChar->SetOutputFormat(
            components == 3 ? VTK_RGB :
            (components == 4 ? VTK_RGBA :
                VTK_LUMINANCE));

        toUChar->SetLookupTable(lut);

        writer->SetInputConnection(toUChar->GetOutputPort());
    }

    writer->SetFileName(fileName.toUtf8().data());
    writer->Write();

    return true;
}