Re: cut strech new Bitmap
Re: cut strech new Bitmap
- Subject: Re: cut strech new Bitmap
- From: Alastair Houghton <email@hidden>
- Date: Mon, 3 Dec 2007 14:44:12 +0000
On 3 Dec 2007, at 13:04, email@hidden wrote:
+ (NSBitmapImageRep *) createCharacterImage:(NSBitmapImageRep
*)image rect:(NSRect)rect {
NSBitmapImageRep *targetBitmapRep;
NSRect targetRect = NSMakeRect(0, 0, rect.size.width,
rect.size.height);
NSImage *scaledImage = [[NSImage alloc] initWithSize: targetSize];
[sourceImage addRepresentation:image];
[image lockFocus];
targetBitmapRep = [[NSBitmapImageRep alloc]
initWithFocusedViewRect:targetRect];
[image unlockFocus];
NSGraphicsContext *bitmapGraphicsContext = [NSGraphicsContext
graphicsContextWithBitmapImageRep:targetBitmapRep];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:bitmapGraphicsContext];
[image drawInRect:targetRect fromRect:rect
operation:NSCompositeCopy fraction:1.0];
[image drawInRect:]
[NSGraphicsContext restoreGraphicsState];
return targetBitmapRep;
}
There seems to be a lot of unnecessary rubbish in your function, which
is probably why it's slow. Here's a shorter version:
+ (NSBitmapImageRep *)characterImage:(NSImage *)image inRect:
(NSRect)rect
{
NSRect targetRect = { NSZeroPoint, targetSize };
NSBitmapImageRep *result = [[[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:NULL
pixelsWide:ceil(NSWidth(targetRect))
pixelsHigh:ceil(NSHeight(targetRect))
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace
bitmapFormat:0
bytesPerRow:0
pixelBits:0] autorelease];
NSGraphicsContext *gc = [NSGraphicsContext
graphicsContextWithBitmapImageRep:result];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:gc];
[image drawInRect:targetRect fromRect:rect
operation:NSCompositeCopy fraction:1.0];
[NSGraphicsContext restoreGraphicsState];
return result;
}
Note that I'm assuming that you have an NSSize variable called
"targetSize" that says how large you want the NSBitmapImageRep to be.
I've also assumed that you want premultiplied RGBA data with eight
bits per channel. You might want to change that, I don't know.
Additionally, I've assumed that the source image is in an NSImage,
because the NSImageRep class doesn't provide a way to scale just part
of the image. You could still use NSImageRep, but if you do so you'd
need to calculate the necessary rectangle co-ordinates to achieve the
scaling *and* clipping that you require.
The usual warning goes with this code; it was typed in Mail.app, and
probably doesn't work.
One other comment: I'm not sure whether this is really the right
approach for the application you're talking about. Firstly, I'm not
convinced that splitting the image up into lots of little bitmaps is
going to work very well (since you're going to end up misaligned) and
second I'm not sure that relying on the system's scaling to
appropriately sample the input pixels is necessarily the best thing to
do here. You may find that you need rather more control, in which
case, particularly if your network has a fairly small input layer, you
may be better off sampling the original image yourself (which will
give you a lot more control over how you do the sampling anyway).
Kind regards,
Alastair.
--
http://alastairs-place.net
_______________________________________________
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