Converting C++ code to swift (For imebra library integration)

541 views Asked by At

I really need some help to convert this c++ code to Swift. I am trying to integrate Imebra library into iOS, which has sample tutorial code in C++ as such (Source: Imebra integration tutorial)

// The transforms chain will contain all the transform that we want to
// apply to the image before displaying it
imebra::TransformsChain chain;

if(imebra::ColorTransformsFactory::isMonochrome(image.getColorSpace()))
{
    // Allocate a VOILUT transform. If the DataSet does not contain any pre-defined
    //  settings then we will find the optimal ones.
    std::shared_ptr<imebra::VOILUT> pVoilutTransform;

    // Retrieve the VOIs (center/width pairs)
    imebra::vois_t vois = loadedDataSet.getVOIs();

    // Retrieve the LUTs
    std::list<imebra::LUT> luts;
    for(size_t scanLUTs(0); ; ++scanLUTs)
    {
        try
        {
            luts.push_back(loadedDataSet.getLUT(imebra::TagId(imebra::tagId_t::VOILUTSequence_0028_3010), scanLUTs));
        }
        catch(const imebra::MissingDataElementError&)
        {
            break;
        }
    }

    if(!vois.empty())
    {
        pVoilutTransform.reset(new imebra::VOILUT(vois[0]));
    }
    else if(!luts.empty())
    {
        pVoilutTransform.reset(new imebra::VOILUT(luts.front()));
    }
    else
    {
        pVoilutTransform.reset(new imebra::VOILUT(imebra::VOILUT::getOptimalVOI(image, 0, 0, width, height)));
    }

    chain.addTransform(*pVoilutTransform);
}

// If the image is monochromatic then now chain contains the VOILUT transform

I need help converting especially these lines of code (from above snippet) into swift as follows:

 if(!vois.empty())
    {
        pVoilutTransform.reset(new imebra::VOILUT(vois[0]));
    }
    else if(!luts.empty())
    {
        pVoilutTransform.reset(new imebra::VOILUT(luts.front()));
    }
    else
    {
        pVoilutTransform.reset(new imebra::VOILUT(imebra::VOILUT::getOptimalVOI(image, 0, 0, width, height)));
    }

My Swift code as of now is like below: (Source: Genixtec Dicom IOS)

//  Created by Genix on 28/08/18.
//  Copyright © 2018 Genix. All rights reserved.
//

//.... Some code here

            if(ImebraColorTransformsFactory.isMonochrome(colorSpace)) {
                let voilutTransform = ImebraVOILUT()
                
                let vois: [ImebraVOIDescription] = try dataSet.getVOIs() as! [ImebraVOIDescription]
               
                var luts = [ImebraLUT]()
               
                var scanLuts: UInt32 = 0
               
                while(true){
                    do {
                        luts.append(try dataSet.getLUT(ImebraTagId(group: 0x0028, tag: 0x3010), item: scanLuts))
                        scanLuts = scanLuts + 1
                    } catch {
                        print("vois loop exit")
                        break;
                    }
                }
                if (!vois.isEmpty) {
                    //voilutTransform?.setCenter(vois[0].center, width: vois[0].width) --> deprecated code
                    //Need updated code here
                } else if (!luts.isEmpty) {
                    //voilutTransform?.setLUT(luts[0]) --> deprecated code
                    //Need updated code here
                } else {
                   // try voilutTransform?.applyOptimalVOI(image, inputTopLeftX: 0, inputTopLeftY: 0, inputWidth: width, inputHeight: height)  --> deprecated code
                   //Need updated code here 
                }
                chain?.add(voilutTransform)
            }
        } catch {
            print("Error occured")
        }
        return chain
    }
}

I need help exactly here:

 if (!vois.isEmpty) {
                    //voilutTransform?.setCenter(vois[0].center, width: vois[0].width) --> deprecated code
                    //Need updated code here
                } else if (!luts.isEmpty) {
                    //voilutTransform?.setLUT(luts[0]) --> deprecated code
                    //Need updated code here
                } else {
                   // try voilutTransform?.applyOptimalVOI(image, inputTopLeftX: 0, inputTopLeftY: 0, inputWidth: width, inputHeight: height)  --> deprecated code
                   //Need updated code here 
                }
0

There are 0 answers