3737
3838#include " itksys/SystemTools.hxx"
3939#include " itksys/Base64.h"
40+ #include " itkMakeUniqueForOverwrite.h"
4041
4142#include " gdcmImageHelper.h"
4243#include " gdcmFileExplicitFilter.h"
@@ -367,7 +368,7 @@ GDCMImageIO::Read(void * pointer)
367368
368369 if (m_SingleBit)
369370 {
370- auto * copy = new unsigned char [len] ;
371+ const auto copy = make_unique_for_overwrite< unsigned char []>(len) ;
371372 unsigned char * t = reinterpret_cast <unsigned char *>(pointer);
372373 size_t j = 0 ;
373374 for (size_t i = 0 ; i < len / 8 ; ++i)
@@ -383,8 +384,7 @@ GDCMImageIO::Read(void * pointer)
383384 copy[j + 7 ] = (c & 0x80 ) ? 255 : 0 ;
384385 j += 8 ;
385386 }
386- memcpy ((char *)pointer, copy, len);
387- delete[] copy;
387+ memcpy ((char *)pointer, copy.get (), len);
388388 }
389389 else
390390 {
@@ -405,10 +405,9 @@ GDCMImageIO::Read(void * pointer)
405405 r.SetSlope (m_RescaleSlope);
406406 r.SetPixelFormat (pixeltype);
407407 gdcm::PixelFormat outputpt = r.ComputeInterceptSlopePixelType ();
408- auto * copy = new char [len];
409- memcpy (copy, (char *)pointer, len);
410- r.Rescale ((char *)pointer, copy, len);
411- delete[] copy;
408+ const auto copy = make_unique_for_overwrite<char []>(len);
409+ memcpy (copy.get (), (char *)pointer, len);
410+ r.Rescale ((char *)pointer, copy.get (), len);
412411 // WARNING: sizeof(Real World Value) != sizeof(Stored Pixel)
413412 len = len * outputpt.GetPixelSize () / pixeltype.GetPixelSize ();
414413 }
@@ -756,15 +755,14 @@ GDCMImageIO::InternalReadImageInformation()
756755 int encodedLengthEstimate = 2 * bv->GetLength ();
757756 encodedLengthEstimate = ((encodedLengthEstimate / 4 ) + 1 ) * 4 ;
758757
759- auto * bin = new char [encodedLengthEstimate] ;
760- auto encodedLengthActual =
758+ const auto bin = make_unique_for_overwrite< char []>(encodedLengthEstimate) ;
759+ auto encodedLengthActual =
761760 static_cast <unsigned int >(itksysBase64_Encode ((const unsigned char *)bv->GetPointer (),
762761 static_cast <SizeValueType>(bv->GetLength ()),
763- (unsigned char *)bin,
762+ (unsigned char *)bin. get () ,
764763 0 ));
765- std::string encodedValue (bin, encodedLengthActual);
764+ std::string encodedValue (bin. get () , encodedLengthActual);
766765 EncapsulateMetaData<std::string>(dico, tag.PrintAsPipeSeparatedString (), encodedValue);
767- delete[] bin;
768766 }
769767 }
770768 }
@@ -883,16 +881,16 @@ GDCMImageIO::Write(const void * buffer)
883881 {
884882 // Custom VR::VRBINARY
885883 // convert value from Base64
886- auto * bin = new uint8_t [value.size ()] ;
887- auto decodedLengthActual =
884+ const auto bin = make_unique_for_overwrite< uint8_t []>( value.size ()) ;
885+ auto decodedLengthActual =
888886 static_cast <unsigned int >(itksysBase64_Decode ((const unsigned char *)value.c_str (),
889887 static_cast <SizeValueType>(0 ),
890- (unsigned char *)bin,
888+ (unsigned char *)bin. get () ,
891889 static_cast <SizeValueType>(value.size ())));
892890 if (/* tag.GetGroup() != 0 ||*/ tag.GetElement () != 0 ) // ?
893891 {
894892 gdcm::DataElement de (tag);
895- de.SetByteValue ((char *)bin, decodedLengthActual);
893+ de.SetByteValue ((char *)bin. get () , decodedLengthActual);
896894 de.SetVR (dictEntry.GetVR ());
897895 if (tag.GetGroup () == 0x2 )
898896 {
@@ -903,7 +901,6 @@ GDCMImageIO::Write(const void * buffer)
903901 header.Insert (de);
904902 }
905903 }
906- delete[] bin;
907904 }
908905 else // VRASCII
909906 {
@@ -1297,11 +1294,10 @@ GDCMImageIO::Write(const void * buffer)
12971294
12981295 image.SetIntercept (m_RescaleIntercept);
12991296 image.SetSlope (m_RescaleSlope);
1300- auto * copyBuffer = new char [len] ;
1297+ const auto copyBuffer = make_unique_for_overwrite< char []>(len) ;
13011298 const auto * inputBuffer = static_cast <const char *>(buffer);
1302- ir.InverseRescale (copyBuffer, inputBuffer, numberOfBytes);
1303- pixeldata.SetByteValue (copyBuffer, static_cast <uint32_t >(len));
1304- delete[] copyBuffer;
1299+ ir.InverseRescale (copyBuffer.get (), inputBuffer, numberOfBytes);
1300+ pixeldata.SetByteValue (copyBuffer.get (), static_cast <uint32_t >(len));
13051301 }
13061302 else
13071303 {
0 commit comments