------------------------------------------------------------------------------
| 1855 | |
| 1856 | //------------------------------------------------------------------------------ |
| 1857 | vtkImageData* vtkPDFContextDevice2D::PrepareImageData(vtkImageData* in) |
| 1858 | { |
| 1859 | int numComps = in->GetNumberOfScalarComponents(); |
| 1860 | |
| 1861 | // We'll only handle RGB / RGBA: |
| 1862 | if (numComps != 3 && numComps != 4) |
| 1863 | { |
| 1864 | vtkWarningMacro("Images with " << numComps << " components not supported."); |
| 1865 | return nullptr; |
| 1866 | } |
| 1867 | |
| 1868 | // Need to convert scalar type? |
| 1869 | if (in->GetScalarType() != VTK_UNSIGNED_CHAR) |
| 1870 | { |
| 1871 | vtkNew<vtkImageCast> cast; |
| 1872 | cast->SetInputData(in); |
| 1873 | cast->SetOutputScalarTypeToUnsignedChar(); |
| 1874 | cast->Update(); |
| 1875 | in = cast->GetOutput(); |
| 1876 | in->Register(this); |
| 1877 | } |
| 1878 | else |
| 1879 | { |
| 1880 | in->Register(this); // Keep refcounts consistent |
| 1881 | } |
| 1882 | |
| 1883 | if (in->GetNumberOfScalarComponents() == 4) |
| 1884 | { // If RGBA, blend into brush color -- Haru doesn't support RGBA. |
| 1885 | |
| 1886 | vtkNew<vtkImageData> background; |
| 1887 | { // Fill the background image with brush color, saturate alpha |
| 1888 | std::array<unsigned char, 4> bgColor; |
| 1889 | this->Brush->GetColor(bgColor.data()); |
| 1890 | bgColor[3] = 255; // Saturate alpha |
| 1891 | background->SetExtent(in->GetExtent()); |
| 1892 | background->AllocateScalars(VTK_UNSIGNED_CHAR, 4); |
| 1893 | auto* scalars = vtkUnsignedCharArray::SafeDownCast(background->GetPointData()->GetScalars()); |
| 1894 | for (int comp = 0; comp < 4; ++comp) |
| 1895 | { |
| 1896 | scalars->FillComponent(comp, bgColor[comp]); |
| 1897 | } |
| 1898 | } |
| 1899 | |
| 1900 | // Blend the input image over the background color: |
| 1901 | vtkNew<vtkImageBlend> blender; |
| 1902 | blender->AddInputData(0, background); |
| 1903 | blender->AddInputData(0, in); |
| 1904 | in->UnRegister(this); // Remove ref++ from above |
| 1905 | blender->SetBlendModeToNormal(); |
| 1906 | |
| 1907 | vtkNew<vtkImageExtractComponents> extract; |
| 1908 | extract->SetInputConnection(blender->GetOutputPort(0)); |
| 1909 | extract->SetComponents(0, 1, 2); |
| 1910 | extract->Update(); |
| 1911 | in = extract->GetOutput(); |
| 1912 | in->Register(this); |
| 1913 | } |
| 1914 |
no test coverage detected