Bitmap - debenbraveheart/Mac-OSX---Cocoa-quartz GitHub Wiki

inside - (void)drawRect of a NSView

{

CGContextRef ref = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];

CGContextRef refff = CreateBitmap(300,300);
CGContextSetRGBFillColor (refff, 0, 0, 0, 1);
CGContextFillRect (refff, CGRectMake (0, 0, 300, 300 ));
CGContextSetRGBFillColor (refff, 1, 0, 0, 1);
CGContextFillRect (refff, CGRectMake (0, 0, 200, 100 ));
CGContextSetRGBFillColor (refff, 0, 0, 1, .5);
CGContextFillRect (refff, CGRectMake (0, 0, 100, 200 ));

CGImageRef myImage = CGBitmapContextCreateImage (refff);

CGRect myBoundingBox;
myBoundingBox = CGRectMake (0, 0, 300, 300);

CGContextDrawImage(ref, myBoundingBox, myImage);
char *bitmapData = (char*)CGBitmapContextGetData(refff);
CGContextRelease (refff);
if (bitmapData) free(bitmapData);
CGImageRelease(myImage);

}

//Bitmap.hpp #include <stdio.h>

#import <Foundation/Foundation.h> #include <Cocoa/Cocoa.h>

CGContextRef CreateBitmap(int height, int width);

//Bitmap.cpp

#include "Bitmap.hpp"

int fun() { return 0; }

CGContextRef CreateBitmap(int height, int width) {

CGContextRef    context = NULL;

CGColorSpaceRef colorSpace;

void *          bitmapData;

int             bitmapByteCount;

int             bitmapBytesPerRow;



bitmapBytesPerRow   = (width * 4);// 1

bitmapByteCount     = (bitmapBytesPerRow * height);



colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);

bitmapData = malloc( bitmapByteCount );



if (bitmapData == NULL)
    
{
    
    fprintf (stderr, "Memory not allocated!");
    
    return NULL;
    
}



context = CGBitmapContextCreate (bitmapData,// 4
                                 
                                 width,
                                 
                                 height,
                                 
                                 8,      // bits per component
                                 
                                 bitmapBytesPerRow,
                                 
                                 colorSpace,
                                 
                                 kCGImageAlphaPremultipliedLast);

if (context== NULL)
    
{
    
    free (bitmapData);// 5
    
    fprintf (stderr, "Context not created!");
    
    return NULL;
    
}

CGColorSpaceRelease( colorSpace );// 6



return context;

}

⚠️ **GitHub.com Fallback** ⚠️