How to redraw an image?
How to redraw an image?
- Subject: How to redraw an image?
- From: "Fritz Isit" <email@hidden>
- Date: Wed, 15 Aug 2001 16:02:18 +0800
I wrote a small app that loads a picture file and renders it. NSImageView and NSObject is used. In MyObject I load the data into NSData. In MyImageView I try to render this data. Outleta of MyImageView are to a contrast slider and contrast textfield and an Outlet called myImageOutlet (for acessing data from MyObject).
The loadImage and saveImage methoide is connected to the Filemenue(load/save).
Whenever the app tries to redraw the image it crashes. I think it is not good to have [NSBitmapInageRep...] in the drawRect methode, but how do I change the bitmap bytes then ?
-------------------------------------------------------
#import <Cocoa/Cocoa.h>
@interface MyImageView : NSImageView
{
IBOutlet id myDisplayOutlet;
IBOutlet id myImageOutlet;
IBOutlet id myKontrastOutlet;
BOOL madefirstresponder;
int contrast;
}
- initWithFrame:(NSRect)frameRect;
- (void)drawRect:(NSRect)rects;
- (IBAction)myAction:(id)sender;
@end
#import "MyImageView.h"
#import "MyObject.h"
@implementation MyImageView
NSImage* myNSImage;
NSBitmapImageRep *myNSBitmapImageRep = nil;
NSSize imageSize={1340,1300};
- initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
[NSBezierPath setDefaultLineWidth:0.8];
madefirstresponder = NO;
/* calling a C-function filter(); */
return self;
}
- (void)drawRect:(NSRect)rects
{
NSRect myRect;
unsigned char *pictureData;
unsigned long *start;
unsigned long value;
unsigned long lhelp;
int i,j;
if (! madefirstresponder)
{
madefirstresponder = YES;
[[self window] makeFirstResponder:self];
[[self window] makeKeyAndOrderFront:self];
}
myRect = [self bounds];
[[NSColor whiteColor] set];
NSRectFill(myRect);
[[NSColor blackColor] set];
contrast = [myKontrastOutlet intValue];
[myDisplayOutlet setIntValue:contrast];
if ([myImageOutlet imageloaded] == YES)
{
myNSBitmapImageRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:imageSize.width
pixelsHigh:imageSize.height
bitsPerSample:8 // 8 bit per color component
samplesPerPixel:3 // rgb components
hasAlpha:NO // no alpha channel
isPlanar:NO // not interwoven
colorSpaceName:NSCalibratedRGBColorSpace //
bytesPerRow:0 // 0 = no empty space at the end
bitsPerPixel:24];
pictureData = [myNSBitmapImageRep bitmapData]; //delivers pointer where data are located
start = (unsigned long *)[[myImageOutlet getsubpicData] bytes];
NSLog(@"getsubpicData accessed");
if ((pictureData) && (start))
{
NSLog(@"Bitmap data allocated");
for(i=0;i<imageSize.height;i++)
{
for(j=0;j<imageSize.width;j++)
{
value = *(start++);
lhelp = ((value >> 8) & 0x0000ff00) + ((value >> 24) & 0x000000ff) +
((value << 8) & 0x00ff0000) + ((value << 24) & 0xff000000);
// only for longs:
value = lhelp/contrast;
if (value > 255)
value = 255;
pictureData[3*(i*(int)imageSize.width+j)]= value;
pictureData[3*(i*(int)imageSize.width+j)+1]= value;
pictureData[3*(i*(int)imageSize.width+j)+2]= value;
}
}
}
[myNSBitmapImageRep drawAtPoint:NSZeroPoint]; // render the bitmap
}
}
- (IBAction)myAction:(id)sender
{
[self display];
}
@end
---------------------------------------------------------
#import <Cocoa/Cocoa.h>
@interface MyObject : NSObject
{
NSData* picData;
NSData* subpicData;
}
- (BOOL) imageloaded;
- (NSData*) getsubpicData;
- (void) awakeFromNib;
- (IBAction)loadImage:(id)sender;
- (IBAction)saveImage:(id)sender;
@end
#import "MyObject.h"
#import "MyImageView.h"
@implementation MyObject
BOOL imageloaded;
- (void) awakeFromNib
{
imageloaded = NO;
}
- (IBAction)loadImage:(id)sender
{
NSOpenPanel* openPanel = [NSOpenPanel openPanel];
//NSArray* imageFileTypes = [NSImage imageFileTypes];
NSArray* fileTypes = [NSArray arrayWithObject:@"SPE"];
NSSize imageSize={1340,1300};
NSRange myRange = {4100, 1340*1300};
unsigned char header[100];
int res1,res2;
int result;
result = [openPanel runModalForDirectory:NSHomeDirectory() file:nil types:fileTypes];
if (result == NSOKButton)
{
NSString* path = [[openPanel filenames] objectAtIndex:0];
//NSImage* image = [[NSImage alloc] initWithContentsOfFile:path];
picData = [[NSData alloc] initWithContentsOfFile:path];
subpicData = [picData subdataWithRange:myRange];
[picData getBytes:header length:100];
res1 = 256 * header[7] + header[6];
res2 = 256 * header[19] + header[18];
NSLog(@"Resolution %d %d",res1,res2);
imageSize.width = res1;
imageSize.height = res2;
imageloaded = YES;
}
}
- (BOOL) imageloaded
{
return (imageloaded);
}
- (NSData*) getsubpicData // access to data
{
NSLog(@" in getsubpicData");
return (subpicData);
}
- (IBAction)saveImage:(id)sender
{
}
@end
--
Powered by Outblaze