Recent

Author Topic: [COCOA API] How to draw bitmap through TCocoaContext and from a NSBitmapImageRep  (Read 454 times)

BeanzMaster

  • Sr. Member
  • ****
  • Posts: 268
Hi to all, i'm updating my library to be compatible with MacOS

I'd like to draw directly a bitmap from a pointer buffer directly on a canvas

This my code

Code: Pascal  [Select][+][-]
  1. procedure TBZCustomBitmap.DrawToCanvas(Const ACanvas: TCanvas; Const ARect: TRect; Const IsOpaque: Boolean; Const ClearBK: Boolean);
  2. Var
  3.   NSImageRep : NSBitmapImageRep;
  4.   dstRect, srcRect : NSRect;
  5.   //TmpBmp : Graphics.TBitmap;
  6.   ctx :  TCocoaContext;
  7.   BitmapFormat: NSBitmapFormat;
  8.   BitsPtr: Pointer;
  9. Begin
  10.  
  11.     Self.PreMultiplyAlpha;
  12.  
  13.     BitmapFormat := 0;
  14.     BitmapFormat := BitmapFormat or NSAlphaFirstBitmapFormat;
  15.  
  16.     getMem(BitsPtr, Self.Size);
  17.     move(Self.getSurfaceBuffer^, BitsPtr^, Self.Size);
  18.  
  19.     NSImageRep := NSBitmapImageRep(NSBitmapImageRep.alloc.initWithBitmapDataPlanes_pixelsWide_pixelsHigh__colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel(
  20.         //Pointer(Self.getSurfaceBuffer), // planes, BitmapDataPlanes
  21.         @(BitsPtr),
  22.         Self.Width, // width, pixelsWide
  23.         Self.Height,// height, PixelsHigh
  24.         8,// bitsPerSample, bps
  25.         4, // samplesPerPixel, spp
  26.         Not(IsOpaque), // hasAlpha
  27.         False, // isPlanar
  28.         NSDeviceRGBColorSpace, // colorSpaceName
  29.         BitmapFormat, // bitmapFormat
  30.         Self.ImageDescription.BytesPerLine, // bytesPerRow
  31.         32 //bitsPerPixel
  32.         ));
  33.  
  34.     dstRect.origin.x := ARect.Left;
  35.     dstRect.origin.y := ARect.Top;
  36.     dstRect.size.width := ARect.Right - ARect.Left;
  37.     dstRect.size.height := ARect.Bottom - ARect.Top;
  38.  
  39.     srcRect.origin.x := 0;
  40.     srcRect.origin.y := 0;
  41.     srcRect.size.width := Self.Width;
  42.     srcRect.size.height := Self.Height;
  43.  
  44.     ctx := CheckDC(ACanvas.Handle);
  45.  
  46.     ctx.DrawImageRep(dstRect, srcRect, NSImageRep);
  47.  
  48.     NSImageRep.release;
  49.     NSImageRep := nil;
  50.  
  51.     FreeMem(BitsPtr);
  52.  
  53.   // work but a little bit slow
  54.   //  Try
  55.   //    TmpBmp := Self.ExportToBitmap;
  56.   //    ACanvas.StretchDraw(ARect,TmpBmp);
  57.   //  finally
  58.   //    TmpBmp.Free;
  59.   //  end;
  60.  
  61.   //Canvas.Draw(0,0, ImageBitmap);
  62.   //Canvas.Draw(R.Left, R.Top, ImageBitmap);
  63. End;    

It compile but my bitmap is not displayed

Have you some suggestions

Thanks in advance

 

TinyPortal © 2005-2018