                 cudaKernelApplyComplexModulus<<<nBlocks,nThreads,0,rStream>>>
                     ( dpCurData, dpCurData, dpIntensity, nElements );
                 CUDA_ERROR( cudaPeekAtLastError() );
+                #if WRITE_OUT_SHRINKWRAP_DEBUG == 1
+                {
+                    std::stringstream fname;
+                    fname << "shrinkWrap_iC-" << iCycleShrinkWrap
+                          << "_iHio-" << iHioCycle << "-intensity.png";
+                    CUDA_ERROR( cudaStreamSynchronize( rStream ) );
+                    CUDA_ERROR( cudaMemcpy( pHostToWrite, dpCurData, 2*nElements *
+                        sizeof( pHostToWrite[0] ), cudaMemcpyDeviceToHost ) );
+                    for ( auto i = 0u; i < nElements; ++i )
+                    {
+                        pHostToWrite[i] = sqrtf( pHostToWrite[2*i+0]*pHostToWrite[2*i+0] +
+                                                 pHostToWrite[2*i+1]*pHostToWrite[2*i+1] );
+                    }
+                    imresh::io::writeOutFuncs::writeOutPNG(
+                        pHostToWrite, rImageWidth, rImageHeight, fname.str().c_str()
+                    );
+                }
+                #endif
 
                 CUFFT_ERROR( cufftExecC2C( ftPlan, dpCurData, dpCurData, CUFFT_INVERSE ) );
+                #if WRITE_OUT_SHRINKWRAP_DEBUG == 1
+                {
+                    std::stringstream fname;
+                    fname << "shrinkWrap_iC-" << iCycleShrinkWrap
+                          << "_iHio-" << iHioCycle << "-object.png";
+                    CUDA_ERROR( cudaStreamSynchronize( rStream ) );
+                    CUDA_ERROR( cudaMemcpy2D(
+                        pHostToWrite,              /* dest addr */
+                        sizeof( pHostToWrite[0] ), /* dest pitch */
+                        dpCurData,                 /* src addr */
+                        sizeof( dpCurData[0] ),    /* src pitch */
+                        sizeof( pHostToWrite[0] ), /* col width (bytes) */
+                        nElements,                 /* number of columns */
+                        cudaMemcpyDeviceToHost
+                    ) );
+
+                    imresh::io::writeOutFuncs::writeOutPNG(
+                        pHostToWrite, rImageWidth, rImageHeight, fname.str().c_str()
+                    );
+                }
+                #endif
             } // HIO loop
 
             /* check if we are done */
             const float currentError = cudaCalculateHioError( dpCurData /*g'*/, dpIsMasked, nElements, false /* don't invert mask */, rStream );
-#           ifdef IMRESH_DEBUG
+            #ifdef IMRESH_DEBUG
                 std::cout << "[Error " << currentError << "/" << rTargetError << "] "
                           << "[Cycle " << iCycleShrinkWrap << "/" << rnCycles-1 << "]"
                           << std::endl;
-#           endif
+            #endif
             if ( rTargetError > 0 && currentError < rTargetError )
                 break;
             if ( iCycleShrinkWrap >= rnCycles )
                 break;
+
+            #if WRITE_OUT_SHRINKWRAP_DEBUG == 1
+            {
+                std::stringstream fname;
+                fname << "shrinkWrap_iC-" << iCycleShrinkWrap
+                      << "-object.png";
+                CUDA_ERROR( cudaStreamSynchronize( rStream ) );
+                CUDA_ERROR( cudaMemcpy2D(
+                    pHostToWrite,              /* dest addr */
+                    sizeof( pHostToWrite[0] ), /* dest pitch */
+                    dpCurData,                 /* src addr */
+                    sizeof( dpCurData[0] ),    /* src pitch */
+                    sizeof( pHostToWrite[0] ), /* col width (bytes) */
+                    nElements,                 /* number of columns */
+                    cudaMemcpyDeviceToHost
+                ) );
+
+                imresh::io::writeOutFuncs::writeOutPNG(
+                    pHostToWrite, rImageWidth, rImageHeight, fname.str().c_str()
+                );
+            }
+            #endif
         } // shrink wrap loop
         cudaKernelCopyFromRealPart<<<nBlocks,nThreads,0,rStream>>>( dpIntensity, dpCurData, nElements );
         CUDA_ERROR( cudaPeekAtLastError() );

        CUDA_ERROR( cudaFree( dpgPrevious ) );
        CUDA_ERROR( cudaFree( dpIntensity ) );
        CUDA_ERROR( cudaFree( dpIsMasked  ) );

        return 0;
    }


#include "cudaShrinkWrap.hpp"

#ifndef NDEBUG // change the following, if you want to turn on debugging
#   define DEBUG_CUDASHRINKWRAP 1
#   define WRITE_OUT_SHRINKWRAP_DEBUG 1
#else   // leave this as it is
#   define DEBUG_CUDASHRINKWRAP 0
#   define WRITE_OUT_SHRINKWRAP_DEBUG 0
#endif

#include <iostream>
