Re: NSBitMapImageRep Woes

2010-01-07 Thread David Blanton

Thanks to all.

I have my bitmap being drawn but it is upside down.

Do isFlipped play any role in this / how do I get it to draw right  
side up without inverting my raw data?


For those interested this is the correct definition of NSBitMapImageRep:

m_ptrs[0] = (unsigned char*)m_bitmap.m_array;
m_NSBitmapImageRep = [[NSBitmapImageRep alloc]
  
initWithBitmapDataPlanes:m_ptrs
  pixelsWide:m_bitmap.m_pixelsx
  pixelsHigh:m_bitmap.m_pixelsy
  bitsPerSample:8
  samplesPerPixel:3
  hasAlpha:NO
  isPlanar:NO
  
colorSpaceName:NSCalibratedRGBColorSpace
  
bitmapFormat:NSAlphaNonpremultipliedBitmapFormat
  
bytesPerRow:(4*m_bitmap.m_pixelsx)
  bitsPerPixel:32];




On Jan 6, 2010, at 11:32 AM, David Duncan wrote:


On Jan 6, 2010, at 10:19 AM, David Blanton wrote:


Now, from David Duncan's comment.

My bitmap data does have alpha, i.e 4 samples per pixel ARGB. I   
thought kCGImageAlphaNoneSkipFirst says don't make an alpha plane,  
skip byte 1 and go to RED.


I am not sure how to specify this in the 'bitmapFormat' parameter.


AlphaNoneSkipFirst means you have no alpha, and skip any leading  
bytes per necessary to fill the pixel size. In your particular case  
since you have 8 bits per component and 3 components, that means  
skip the first 8 bits of each word then decode 8 bits for each of 3  
components.


When you create your NSBitmapImageRep however, you specify that  
there is alpha, which means that you are specifying the equivalent  
of kCGImageAlphaPremultipliedFirst (since you don't specify the  
NSAlphaNonpremultipliedBitmapFormat flag).  What you really want is  
to specify no alpha and 3 samples per pixel (if I'm not mistaken, I  
usually work in Quartz rather than NS for graphics). Specifying 32  
bits per pixel as you do should get the same interpretation that you  
are currently getting via Quartz.


Also keep in mind that your Quartz code is more work than you would  
actually need. Since you aren't drawing to your image data, you  
should create the CGImage directly via CGImageCreate rather than  
using CGBitmapContextCreate to then extract a CGImage – the  
performance difference is in the syscalls necessary to mark the  
memory for the bitmap context as copy-on-write.

--
David Duncan
Apple DTS Animation and Printing





___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSBitMapImageRep Woes

2010-01-07 Thread David Blanton

Answered my own question ... apologize for the noise!
On Jan 7, 2010, at 4:05 PM, David Blanton wrote:


Thanks to all.

I have my bitmap being drawn but it is upside down.

Do isFlipped play any role in this / how do I get it to draw right  
side up without inverting my raw data?


For those interested this is the correct definition of  
NSBitMapImageRep:


m_ptrs[0] = (unsigned char*)m_bitmap.m_array;
m_NSBitmapImageRep = [[NSBitmapImageRep alloc]
  
initWithBitmapDataPlanes:m_ptrs
  pixelsWide:m_bitmap.m_pixelsx
  pixelsHigh:m_bitmap.m_pixelsy
  bitsPerSample:8
  samplesPerPixel:3
  hasAlpha:NO
  isPlanar:NO
  
colorSpaceName:NSCalibratedRGBColorSpace
  
bitmapFormat:NSAlphaNonpremultipliedBitmapFormat
  
bytesPerRow:(4*m_bitmap.m_pixelsx)
  bitsPerPixel:32];




On Jan 6, 2010, at 11:32 AM, David Duncan wrote:


On Jan 6, 2010, at 10:19 AM, David Blanton wrote:


Now, from David Duncan's comment.

My bitmap data does have alpha, i.e 4 samples per pixel ARGB. I   
thought kCGImageAlphaNoneSkipFirst says don't make an alpha plane,  
skip byte 1 and go to RED.


I am not sure how to specify this in the 'bitmapFormat' parameter.


AlphaNoneSkipFirst means you have no alpha, and skip any leading  
bytes per necessary to fill the pixel size. In your particular case  
since you have 8 bits per component and 3 components, that means  
skip the first 8 bits of each word then decode 8 bits for each of 3  
components.


When you create your NSBitmapImageRep however, you specify that  
there is alpha, which means that you are specifying the equivalent  
of kCGImageAlphaPremultipliedFirst (since you don't specify the  
NSAlphaNonpremultipliedBitmapFormat flag).  What you really want is  
to specify no alpha and 3 samples per pixel (if I'm not mistaken, I  
usually work in Quartz rather than NS for graphics). Specifying 32  
bits per pixel as you do should get the same interpretation that  
you are currently getting via Quartz.


Also keep in mind that your Quartz code is more work than you would  
actually need. Since you aren't drawing to your image data, you  
should create the CGImage directly via CGImageCreate rather than  
using CGBitmapContextCreate to then extract a CGImage – the  
performance difference is in the syscalls necessary to mark the  
memory for the bitmap context as copy-on-write.

--
David Duncan
Apple DTS Animation and Printing





___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/airedale%40tularosa.net

This email sent to aired...@tularosa.net




___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


NSBitMapImageRep Woes

2010-01-06 Thread David Blanton

Here is the code:

@interface MyDocumentView : NSView {
@public

NSBitmapImageRep*   m_NSBitmapImageRep;
NSRect  m_frameRect;
float   m_sz;
BBitmap m_bitmap;
unsigned char*  m_ptrs[1];
}

- (void)awakeFromNib {

m_ptrs[0] = (unsigned char*)m_bitmap.m_array;
m_NSBitmapImageRep = [[NSBitmapImageRep alloc]
   
initWithBitmapDataPlanes:m_ptrs
   
pixelsWide:m_frameRect.size.width
   
pixelsHigh:m_frameRect.size.height
   bitsPerSample:8
   samplesPerPixel:4
   hasAlpha:YES
   isPlanar:NO
   
colorSpaceName:NSCalibratedRGBColorSpace
   
bitmapFormat:NSAlphaFirstBitmapFormat
   bytesPerRow:(4 * 
m_frameRect.size.width)
   bitsPerPixel:32];
}

- (void)drawRect:(NSRect)dirtyRect {

[NSGraphicsContext saveGraphicsState];  
[m_NSBitmapImageRep drawInRect:dirtyRect];  
[NSGraphicsContext restoreGraphicsState];
}

m_bitmap.m_array is created in readFromData:ofType:fromFile

I know it is correct because if I use this code I get proper results: 
(in this case bitmap is a local var but is generated just m_bitmap is.


	context = CGBitmapContextCreate (bitmap.m_array, bitmap.m_pixelsx,  
bitmap.m_pixelsy, 8, bitmap.m_pixelsx * 4, colorSpace,  
kCGImageAlphaNoneSkipFirst|kCGBitmapByteOrder32Host);	

CGImageRelease(_cgImageRef);
_cgImageRef =  CGBitmapContextCreateImage (context);
CGImageRetain(_cgImageRef); 
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);


The problem is I just see garbage, no image.  Clearly I am doing  
something wrong with the NSBitMapImageRep but I have no idea what.


Please comment!

db


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSBitMapImageRep Woes

2010-01-06 Thread David Duncan
On Jan 6, 2010, at 9:21 AM, David Blanton wrote:

   m_NSBitmapImageRep = [[NSBitmapImageRep alloc]
  
 initWithBitmapDataPlanes:m_ptrs
  
 pixelsWide:m_frameRect.size.width
  
 pixelsHigh:m_frameRect.size.height
  bitsPerSample:8
  samplesPerPixel:4
  hasAlpha:YES
  isPlanar:NO
  
 colorSpaceName:NSCalibratedRGBColorSpace
  
 bitmapFormat:NSAlphaFirstBitmapFormat
  bytesPerRow:(4 * 
 m_frameRect.size.width)
  bitsPerPixel:32];

This code creates a bitmap with alpha.

   context = CGBitmapContextCreate (bitmap.m_array, bitmap.m_pixelsx, 
 bitmap.m_pixelsy, 8, bitmap.m_pixelsx * 4, colorSpace, 
 kCGImageAlphaNoneSkipFirst|kCGBitmapByteOrder32Host); 

This code creates a bitmap without alpha.

 The problem is I just see garbage, no image.  Clearly I am doing something 
 wrong with the NSBitMapImageRep but I have no idea what.


Hence garbage.
--
David Duncan
Apple DTS Animation and Printing

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSBitMapImageRep Woes

2010-01-06 Thread Ken Thomases
On Jan 6, 2010, at 11:21 AM, David Blanton wrote:

 - (void)drawRect:(NSRect)dirtyRect {
   
   [NSGraphicsContext saveGraphicsState];  
   [m_NSBitmapImageRep drawInRect:dirtyRect];  
   [NSGraphicsContext restoreGraphicsState];
 }

The docs for -drawInRect: say it does nothing if you haven't set the size of 
the image.  I know the size is (potentially) different from the 
pixel-width/height, although I don't know if NSBitmapImageRep assumes an 
initial size based on the pixel dimensions.

Also, you probably don't want to draw to the dirty rect.  You want to draw to 
the view's bounds, presumably.

Regards,
Ken

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSBitMapImageRep Woes

2010-01-06 Thread Henry McGilton (Boulevardier)

On Jan 6, 2010, at 9:21 AM, David Blanton wrote:

 Here is the code:
 
 @interface MyDocumentView : NSView {
 @public
   
   NSBitmapImageRep*   m_NSBitmapImageRep;
   NSRect  m_frameRect;
   float   m_sz;
   BBitmap m_bitmap;
   unsigned char*  m_ptrs[1];
 }
 
 - (void)awakeFromNib {
   
   m_ptrs[0] = (unsigned char*)m_bitmap.m_array;
   m_NSBitmapImageRep = [[NSBitmapImageRep alloc]
  
 initWithBitmapDataPlanes:m_ptrs
  
 pixelsWide:m_frameRect.size.width
  
 pixelsHigh:m_frameRect.size.height
  bitsPerSample:8
  samplesPerPixel:4
  hasAlpha:YES
  isPlanar:NO
  
 colorSpaceName:NSCalibratedRGBColorSpace
  
 bitmapFormat:NSAlphaFirstBitmapFormat
  bytesPerRow:(4 * 
 m_frameRect.size.width)
  bitsPerPixel:32];
 }

Two questions:

1.where ism_frameRectinitialised ?

2.and are the width and height initialised to the precise number of  bits 
per row and number of rows
of whatever m_ptrs[0] is pointing at ?

3.(I said previously I can't count), are there potential endian issues 
involved ? 


 
 - (void)drawRect:(NSRect)dirtyRect {
   
   [NSGraphicsContext saveGraphicsState];  
   [m_NSBitmapImageRep drawInRect:dirtyRect];  
   [NSGraphicsContext restoreGraphicsState];
 }
 
 m_bitmap.m_array is created in readFromData:ofType:fromFile
 
 I know it is correct because if I use this code I get proper results:(in this 
 case bitmap is a local var but is generated just m_bitmap is.
 
   context = CGBitmapContextCreate (bitmap.m_array, bitmap.m_pixelsx, 
 bitmap.m_pixelsy, 8, bitmap.m_pixelsx * 4, colorSpace, 
 kCGImageAlphaNoneSkipFirst|kCGBitmapByteOrder32Host); 
   CGImageRelease(_cgImageRef);
   _cgImageRef =  CGBitmapContextCreateImage (context);
   CGImageRetain(_cgImageRef); 
   CGContextRelease(context);
   CGColorSpaceRelease(colorSpace);
 
 
 The problem is I just see garbage, no image.  Clearly I am doing something 
 wrong with the NSBitMapImageRep but I have no idea what.
 
 Please comment!
 



=
iPhone App Development and Developer Education . . .
Visit  www.nonatomic-retain.com

Mac OSX Application Development, Plus a Great Deal More . . .
Visit  www.trilithon.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSBitMapImageRep Woes

2010-01-06 Thread David Blanton

OK.

So, from other comments I made some changes.

1. Correctly set the dimensions by using m_bitmap.m_pixelsx and  
m_bitmap.m_pixelsy (these are members of our portable bitmap class).



m_NSBitmapImageRep = [[NSBitmapImageRep alloc]
  
initWithBitmapDataPlanes:m_ptrs
  pixelsWide:m_bitmap.m_pixelsx
  pixelsHigh:m_bitmap.m_pixelsy
  bitsPerSample:8
  samplesPerPixel:4
  hasAlpha:YES
  isPlanar:NO
  
colorSpaceName:NSCalibratedRGBColorSpace
  
bitmapFormat:NSAlphaFirstBitmapFormat
  bytesPerRow:(4 * 
m_bitmap.m_pixelsx)
  bitsPerPixel:32];


And changed the drawInRect to draw in the views bounds

- (void)drawRect:(NSRect)dirtyRect {

NSRect bounds = [self bounds];
[NSGraphicsContext saveGraphicsState];  
[m_NSBitmapImageRep drawInRect:bounds]; 
[NSGraphicsContext restoreGraphicsState];
}

Now, from David Duncan's comment.

My bitmap data does have alpha, i.e 4 samples per pixel ARGB. I   
thought kCGImageAlphaNoneSkipFirst says don't make an alpha plane,  
skip byte 1 and go to RED.


I am not sure how to specify this in the 'bitmapFormat' parameter.


On Jan 6, 2010, at 10:35 AM, David Duncan wrote:


On Jan 6, 2010, at 9:21 AM, David Blanton wrote:


m_NSBitmapImageRep = [[NSBitmapImageRep alloc]
   
initWithBitmapDataPlanes:m_ptrs
   
pixelsWide:m_frameRect.size.width
   
pixelsHigh:m_frameRect.size.height
   bitsPerSample:8
   samplesPerPixel:4
   hasAlpha:YES
   isPlanar:NO
   
colorSpaceName:NSCalibratedRGBColorSpace
   
bitmapFormat:NSAlphaFirstBitmapFormat
   bytesPerRow:(4 * 
m_frameRect.size.width)
   bitsPerPixel:32];


This code creates a bitmap with alpha.

	context = CGBitmapContextCreate (bitmap.m_array, bitmap.m_pixelsx,  
bitmap.m_pixelsy, 8, bitmap.m_pixelsx * 4, colorSpace,  
kCGImageAlphaNoneSkipFirst|kCGBitmapByteOrder32Host);	


This code creates a bitmap without alpha.

The problem is I just see garbage, no image.  Clearly I am doing  
something wrong with the NSBitMapImageRep but I have no idea what.



Hence garbage.
--
David Duncan
Apple DTS Animation and Printing





___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSBitMapImageRep Woes

2010-01-06 Thread David Duncan
On Jan 6, 2010, at 10:19 AM, David Blanton wrote:

 Now, from David Duncan's comment.
 
 My bitmap data does have alpha, i.e 4 samples per pixel ARGB. I  thought 
 kCGImageAlphaNoneSkipFirst says don't make an alpha plane, skip byte 1 and go 
 to RED.
 
 I am not sure how to specify this in the 'bitmapFormat' parameter.

AlphaNoneSkipFirst means you have no alpha, and skip any leading bytes per 
necessary to fill the pixel size. In your particular case since you have 8 bits 
per component and 3 components, that means skip the first 8 bits of each word 
then decode 8 bits for each of 3 components.

When you create your NSBitmapImageRep however, you specify that there is alpha, 
which means that you are specifying the equivalent of 
kCGImageAlphaPremultipliedFirst (since you don't specify the 
NSAlphaNonpremultipliedBitmapFormat flag).  What you really want is to specify 
no alpha and 3 samples per pixel (if I'm not mistaken, I usually work in Quartz 
rather than NS for graphics). Specifying 32 bits per pixel as you do should get 
the same interpretation that you are currently getting via Quartz.

Also keep in mind that your Quartz code is more work than you would actually 
need. Since you aren't drawing to your image data, you should create the 
CGImage directly via CGImageCreate rather than using CGBitmapContextCreate to 
then extract a CGImage – the performance difference is in the syscalls 
necessary to mark the memory for the bitmap context as copy-on-write.
--
David Duncan
Apple DTS Animation and Printing

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: NSBitMapImageRep Woes

2010-01-06 Thread Graham Cox

On 07/01/2010, at 4:21 AM, David Blanton wrote:

  bytesPerRow:(4 * 
 m_frameRect.size.width)
  bitsPerPixel:32];


Apart from what others have said, you can pass 0 for these parameters and they 
get calculated for you. That's usually safest because any inconsistency among 
the parameters throws an exception.

--Graham


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com