Re: How to quickly paint to a Cocoa view from a bitmap in memory
Re: How to quickly paint to a Cocoa view from a bitmap in memory
- Subject: Re: How to quickly paint to a Cocoa view from a bitmap in memory
- From: koko <email@hidden>
- Date: Mon, 26 Sep 2011 14:46:43 -0600
I alos forgot to say that x and y below are pixels x and pixels y of the bitmap, i.e. its size.
On Sep 26, 2011, at 12:28 PM, koko wrote:
> I too am working in a cross platform environment and use bitmaps in an NSView. I use the code below and it is fast enough to pan , scroll etc with no sense of sluggishness.
> My code looks a little different than what you have submitted. You might re-factor to my model.
>
>
> CGColorSpaceRef colorSpace;
> colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
> provider;
> CGImageRef bitmap;
>
> provider = CGDataProviderCreateWithData (NULL,m_main.m_bitmap.m_array, 4*x*y, NULL);
> bitmap = CGImageCreate(x, y, 8, 32, 4*x, colorSpace, kCGImageAlphaNoneSkipFirst|kCGBitmapByteOrder32Host,provider, NULL,true, kCGRenderingIntentDefault);
>
> CGRect r;
> if(bitmap)
> {
> NSGraphicsContext *graphicsContext = [NSGraphicsContext currentContext];
> CGContextRef context = (CGContextRef)[graphicsContext graphicsPort];
> r = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
> CGContextDrawImage(context, r, bitmap);
> }
>
> CGColorSpaceRelease(colorSpace);
> CGDataProviderRelease(provider);
> CGImageRelease(bitmap);
>
> On Sep 24, 2011, at 12:30 PM, Vojtěch Meluzín wrote:
>
>> Hi folks,
>>
>> I'm implementing a crossplatform (not-only GUI) library, where everything is
>> drawn internally to an RGBA bitmap and then just copied to screen. On
>> Windows it works like charm especially since there is a support for
>> "in-memory" bitmaps. But I don't see such a feature on Mac. I ended up with
>> this code:
>>
>> CGColorSpaceRef colorSpace =
>> CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
>> CGContextRef bmpcontext =
>> CGBitmapContextCreate((void*)desc.Bitmap.GetData(),
>> sz.X, sz.Y, 8, sz.X
>> * 4, colorSpace, kCGImageAlphaNoneSkipLast);
>> CGColorSpaceRelease(colorSpace);
>>
>> CGContextSaveGState(context);
>> CGRect cliprect = { r.X1, sz.Y - r.Y2, r.GetWidth(), r.GetHeight()
>> };
>> CGContextClipToRect (context, cliprect);
>> CGContextSetShouldAntialias (context, false);
>>
>> HIRect bounds;
>> bounds.origin.x = 0;
>> bounds.origin.y = 0;
>> bounds.size.width = sz.X;
>> bounds.size.height = sz.Y;
>>
>> CGImageRef temp = CGBitmapContextCreateImage (bmpcontext);
>> CGContextDrawImage(context, bounds, temp);
>>
>> CGImageRelease(temp);
>> CGContextRelease (bmpcontext);
>>
>> CGContextRestoreGState (context);
>>
>>
>> It basically converts the whole bitmap into CGImageRef and then copies a
>> part of it into the NSView's context. But it's quite slow. Is there a better
>> way to do this? And is there a way to create the in-memory images like they
>> are on Windows?
>>
>> One more thing - I do the same thing in Carbon (I just need Cocoa because of
>> x64...) and I noticed that the coordinates for the CGContext is inverted
>> vertically, is that correct?? I mean the same context object type, but
>> different Y handling...
>>
>> Thanks in advance.
>> Vojtech
>> _______________________________________________
>>
>> Cocoa-dev mailing list (email@hidden)
>>
>> Please do not post admin requests or moderator comments to the list.
>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>
>> Help/Unsubscribe/Update your Subscription:
>>
>> This email sent to email@hidden
>>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden