Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Increasing size of NSImage by adding padding



On 21 Oct, 2004, at 11:43 AM, Nicko van Someren wrote:

On 21 Oct 2004, at 17:27, Glen Simmons wrote:

OK, this seems like a really simple thing, but I'm not getting it. I have an NSImage that was created from some 1-bit bitmap data using that really long NSBitmapImageRep initWithBitmapDataPlanes... method. I need to pad the image with a couple of pixels worth of whitespace all the way around.
I tried:
1. create new NSImage that is 4 pixels larger in width and height;
2. lock focus on new image
3. tell original image to compositeToPoint 2,2 (also tried dissolveToPoint and drawAtPoint)

The problem with the above is that I end up with a fuzzy looking image, I guess due to anti-aliasing. I'm guessing (hoping?) there's a much better / cleaner way of doing this so that I end up with the same bitmap, just padded with some whitespace.

If you're not creating any specific representations in the new NSImage then lockFocus makes one with what it thinks are good parameters, where good tends to depend on the current screen settings. You might like to try generating an NSBitmapImageRep of the desired size and at 1BPP and then lock focus on that representation. Failing that, you could try calling setImageInterpolation on the current graphics context with a value of NSImageInterpolationNone before you composite the image.

Tried that, still fuzzy. Am I going to have to copy the bits into a buffer manually?

Here's the code:

NSSize theSize = [originalImage size];
NSRect borderRect;
NSImage *newImage;
NSBitmapImageRep *newBitmapRep;
NSImageRep *originalBitmapRep;
NSImageRep *tempRep;
NSArray *repsArray = [originalImage representations];
int i;

for (i = 0; i < [repsArray count]; i++) {
tempRep = [repsArray objectAtIndex:i];
if ([tempRep isKindOfClass:[NSBitmapImageRep class]]) {
originalBitmapRep = tempRep;
break;
}
}

theSize.height += 4.0;
theSize.width += 4.0;
newImage = [[NSImage alloc] initWithSize:theSize];
newBitmapRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:nil
pixelsWide:theSize.width
pixelsHigh:theSize.height
bitsPerSample:1
samplesPerPixel:1
hasAlpha:NO
isPlanar:NO
colorSpaceName:NSCalibratedBlackColorSpace
bytesPerRow:0
bitsPerPixel:0];
[newImage addRepresentation:newBitmapRep];
[newBitmapRep release];

[newImage lockFocusOnRepresentation:newBitmapRep];
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationNone];

borderRect = NSZeroRect;
borderRect.size = theSize;
[[NSColor whiteColor] set];
NSRectFill(borderRect);
[originalBitmapRep drawAtPoint:NSMakePoint(2.0, 2.0)];
borderRect = NSInsetRect(borderRect, 0.5, 0.5);
[NSBezierPath setDefaultLineWidth:1.0];
[[NSColor blackColor] set];
[NSBezierPath strokeRect:borderRect];
[newImage unlockFocus];

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >Increasing size of NSImage by adding padding (From: Glen Simmons <email@hidden>)
 >Re: Increasing size of NSImage by adding padding (From: Nicko van Someren <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.