title: Replace Part of Pixel Buffer with White Pixels in iOS
body: <p>I am using the iPhone camera to capture live video and feeding the pixel buffer to a network that does some object recognition. Here is the relevant code: (I won't post the code for setting up the <code>AVCaptureSession</code>
  etc. as this is pretty standard.)</p>
  
  <pre><code>- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
      CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
  
      OSType sourcePixelFormat = CVPixelBufferGetPixelFormatType( pixelBuffer );
      int doReverseChannels;
      if ( kCVPixelFormatType_32ARGB == sourcePixelFormat ) {
          doReverseChannels = 1;
      } else if ( kCVPixelFormatType_32BGRA == sourcePixelFormat ) {
          doReverseChannels = 0;
      } else {
          assert(false);
      }
  
      const int sourceRowBytes = (int)CVPixelBufferGetBytesPerRow( pixelBuffer );
      const int width = (int)CVPixelBufferGetWidth( pixelBuffer );
      const int fullHeight = (int)CVPixelBufferGetHeight( pixelBuffer );
      CVPixelBufferLockBaseAddress( pixelBuffer, 0 );
      unsigned char* sourceBaseAddr = CVPixelBufferGetBaseAddress( pixelBuffer );
      int height;
      unsigned char* sourceStartAddr;
      if (fullHeight &lt;= width) {
          height = fullHeight;
          sourceStartAddr = sourceBaseAddr;
      } else {
          height = width;
          const int marginY = ((fullHeight - width) / 2);
          sourceStartAddr = (sourceBaseAddr + (marginY * sourceRowBytes));
      }
  }
  </code></pre>
  
  <p>The network then takes <code>sourceStartAddr</code>, <code>width</code>, <code>height</code>, <code>sourceRowBytes</code> &amp; <code>doReverseChannels</code> as inputs.</p>
  
  <p>My question is the following: What would be the simplest and/or most efficient way to replace or delete a part of the image data with all white 'pixels'? Is it possible to directly overwrite e portion of the pixel buffer data and if yes how?</p>
  
  <p>I only have a very rudimentary understanding of how this pixel buffer works, so I apologize if I'm missing something very basic here. The question most closely related to mine I found on Stackoverflow was <a href="https://stackoverflow.com/questions/21753926/avfoundation-add-text-to-the-cmsamplebufferref-video-frame">this one</a>, where a <code>EAGLContext</code> is used to add text to a video frame. While this would actually work for my objective which only needs this replacement for single images, I assume this step would kill performance if applied to every video frame, and I would like to find out if there is another method. Any help here would be appreciated.</p>