title: Inverting a bitmap in Embarcadero C++Builder
body: <p>The same problem happens in Borland C++Builder 6 and Embarcadero C++Builder 2010.</p>
  <p>When I try to invert an image, the form freezes for about 5 seconds and nothing happens. When I click again, the image is inverted within a blink of an eye, and again, again...</p>
  <p>To reproduce, create a form with a <code>TImage</code> and two <code>TButton</code>'s using this code:</p>
  <pre><code>//---------------------------------------------------------------------------
  #include &lt;vcl.h&gt;
  #include &lt;jpeg.hpp&gt;
  #pragma hdrstop
  #pragma package(smart_init)
  #pragma resource &quot;*.dfm&quot;
  #include &quot;Unit1.h&quot;
  TForm1 *Form1;
  Graphics::TBitmap *CurrentBitmap;
  bool bLoaded = false;
  //---------------------------------------------------------------------------
  __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) {
      CurrentBitmap = new Graphics::TBitmap;
  }
  //---------------------------------------------------------------------------
  bool __fastcall Invert( TImage *img ) {
      if( bLoaded ) {
          double Scale, ScaleW, ScaleH;
          Graphics::TBitmap *bmp = new Graphics::TBitmap;
          bmp-&gt;Assign( CurrentBitmap );
          DWORD **pixel = new DWORD*[bmp-&gt;Height];
          for( long y=0; y&lt;bmp-&gt;Height; y++ ) {
              pixel[y] = (DWORD*)(bmp-&gt;ScanLine[y]);
              for( long x=0; x&lt;bmp-&gt;Width; x++ ) {
                  if( pixel[y][x] == clBlack ) {
                      pixel[y][x] = clWhite;
                  } else if( pixel[y][x] == clWhite ) {
                      pixel[y][x] = clBlack;
                  }
              }
  
          }
          delete[] pixel;
          ScaleW = (double)bmp-&gt;Width / img-&gt;ClientWidth;
          ScaleH = (double)bmp-&gt;Height / img-&gt;ClientHeight;
          if( ScaleW &gt; ScaleH ) {
              Scale = ScaleW;
          } else {
              Scale = ScaleH;
          }
          CurrentBitmap-&gt;Assign( bmp );
          img-&gt;Picture-&gt;Bitmap-&gt;Canvas-&gt;StretchDraw(Rect(0, 0, bmp-&gt;Width/Scale, bmp-&gt;Height/Scale), bmp );
          delete bmp;
          return true;
      } else {
          return false;
      }
  }
  //---------------------------------------------------------------------------
  void __fastcall TForm1::Button1Click(TObject *Sender) {
      TJPEGImage *jpg = new TJPEGImage();
      jpg-&gt;LoadFromFile( &quot;V:\\CBuilder\\BCB10\\GerberTest\\Testdata\\GerberTest.dpi2400.mskcmp.jpg&quot; );
      CurrentBitmap-&gt;Width = jpg-&gt;Width;
      CurrentBitmap-&gt;Height = jpg-&gt;Height;
      CurrentBitmap-&gt;Canvas-&gt;StretchDraw(Rect(0, 0, jpg-&gt;Width, jpg-&gt;Height), jpg );
      bLoaded = true;
      double Scale, ScaleW, ScaleH;
      ScaleW = (double)jpg-&gt;Width / Image1-&gt;ClientWidth;
      ScaleH = (double)jpg-&gt;Height / Image1-&gt;ClientHeight;
      if( ScaleW &gt; ScaleH ) {
          Scale = ScaleW;
      } else {
          Scale = ScaleH;
      }
      Image1-&gt;Canvas-&gt;StretchDraw(Rect(0, 0, jpg-&gt;Width/Scale, jpg-&gt;Height/Scale), jpg );
  }
  //---------------------------------------------------------------------------
  void __fastcall TForm1::Button2Click(TObject *Sender) {
      Invert( Image1 );
  }
  </code></pre>
  <p>I cannot explain why the application freezes, or what the code is doing during the 5 second delay.</p>