Re: How can I convert bitmap representations to a "canonical" form
Re: How can I convert bitmap representations to a "canonical" form
- Subject: Re: How can I convert bitmap representations to a "canonical" form
- From: Nat! <email@hidden>
- Date: Thu, 23 Aug 2001 02:15:07 +0200
On Wednesday, August 22, 2001, at 01:09 AM, Erik M. Buck wrote:
Read a bitmap into an NSImage
Create an image rep with the pixel format desired
lock focus on the image rep
composite the NSImage into the rep (pixel conversions should happen
automatically)
unlock focus
OK I tried this in this demo program. The main window contains just two
ImageViews. "originalView" is editable and its action is hooked to
"action:". The other is plain. Now there is a second image displayed in
"destinationView", whenever an image is dragged into "originalView".
Unfortunately as can be seen by the output (comments), the
NSBitmapImageRep has been replaced by a NSCachedImageRep. Secondly and
more surprisingly (to me) the "destinationView" is rendered in color. I
was expecting a grayscale picture, but apparently it was really rendered
into the device format (?)
Did I do anything wrong ?
Nat!
--- Controller.h ---
#import <Cocoa/Cocoa.h>
@interface Controller : NSObject
{
IBOutlet NSImageView *destinationView;
IBOutlet NSImageView *originalView;
}
- (IBAction)action:(id)sender;
@end
--- Controller.m ---
#import "Controller.h"
#define UseColorSpace NSCalibratedWhiteColorSpace
#define UseSamplesPerPixel 1
@implementation Controller
- (IBAction)action:(id)sender
{
NSBitmapImageRep *repSrc;
NSBitmapImageRep *repDst;
NSData *bitmap;
NSImage *image;
NSImage *convertedImage;
image = [originalView image];
repSrc = [[image representations] lastObject];
bitmap = [[NSMutableData alloc] initWithLength:[image size].width *
[image size].height * UseSamplesPerPixel]; // leaks
repDst = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:[bitmap
bytes]
pixelsWide:[image
size].width
pixelsHigh:[image
size].height
bitsPerSample:8
samplesPerPixel:UseSamplesPerPixel
hasAlpha:NO
isPlanar:NO
colorSpaceName:UseColorSpace
bytesPerRow:UseSamplesPerPixel*[image size].width
bitsPerPixel:8*UseSamplesPerPixel];
convertedImage = [[[NSImage alloc] initWithSize:[image size]]
autorelease];
[convertedImage addRepresentation:repDst]; // must do this
NSLog( @"%@", convertedImage);
// Aug 23 02:04:48 CanonicalBitmap[821] NSImage 0x16a0ce0 Size={1280,
1024} Reps=(
// NSBitmapImageRep 0x16a0ca0 Size={1280, 1024}
ColorSpace=NSCalibratedWhiteColorSpace BPS=8 Pixels=1280x1024 // Alpha=NO
// )
[convertedImage lockFocusOnRepresentation:repDst];
NSLog( @"%@", convertedImage);
// Aug 23 02:04:48 CanonicalBitmap[821] NSImage 0x16a0ce0 Size={1280,
1024} Reps=(
// NSCachedImageRep 0x16a2990 Size={1280, 1024}
ColorSpace=NSCalibratedWhiteColorSpace BPS=8 Pixels=1280x1024 // Alpha=NO
// )
[image compositeToPoint:NSMakePoint( 0.0, 0.0)
operation:NSCompositeSourceOver];
[convertedImage unlockFocus];
NSLog( @"%@", convertedImage);
// Aug 23 02:04:48 CanonicalBitmap[821] NSImage 0x16a0ce0 Size={1280,
1024} Reps=(
// NSCachedImageRep 0x16a2990 Size={1280, 1024}
ColorSpace=NSCalibratedWhiteColorSpace BPS=8 Pixels=1280x1024 // Alpha=NO
// )
[destinationView setImage:convertedImage];
[destinationView setNeedsDisplay:YES];
}
@end