Import From buffer

I would like to import an image from buffer; is it right?

const unsigned short numberOfPixels = pow(size_import_buffer[0] , 3);
auto * localBuffer = new PixelType[numberOfPixels];
PixelType *  it = localBuffer;

for(int z= 0 ; z < size_for_dimensions[2]; z++)
    for(int y= 0 ; y < size_for_dimensions[2]; y++)
        for(int x= 0 ; x < size_for_dimensions[2]; x++)
		{
			const ImageType::IndexType pixelIndex_Buffer_1 = {{ x, y, z }}; // Position of {X,Y,Z}
			std::cerr<<"HI: "<<std::endl;
		
			*it++ = Flipped_Trachea -> GetPixel(pixelIndex_Buffer_1);
        }

const bool importImageFilterWillOwnTheBuffer = true;
importFilter->SetImportPointer(localBuffer, numberOfPixels, importImageFilterWillOwnTheBuffer);

auto writerTraqueia = WriterTypeTraqueia::New();
writerTraqueia->SetFileName("TRACHEA AIRWAY SEG.dcm");
writerTraqueia->SetInput(importFilter->GetOutput());
writerTraqueia->SetImageIO(gdcmIO);
writerTraqueia->Update();

Based on your code sample, I don’t know what is the purpose of localBuffer? I think you could achieve the same effect with:

const unsigned short numberOfPixels = pow(size_import_buffer[0] , 3);
PixelType *  it = Flipped_Trachea->GetBufferPointer();
// we have direct pointer to pixel buffer
// we don't use it, as there is no need to iterate

auto writerTraqueia = WriterTypeTraqueia::New();
writerTraqueia->SetFileName("TRACHEA AIRWAY SEG.dcm");
writerTraqueia->SetInput(Flipped_Trachea);
writerTraqueia->SetImageIO(gdcmIO);
writerTraqueia->Update();