Re: Rasterizing an NSString to a bitmap
Re: Rasterizing an NSString to a bitmap
- Subject: Re: Rasterizing an NSString to a bitmap
- From: p3consulting <email@hidden>
- Date: Thu, 10 Jun 2004 21:03:27 +0200
>
Hold it. The -initWithFocusedViewRect: method is a documented and
>
reasonably efficient way to get rendered bitmap data. Even if it
>
isn't named brilliantly in relation to NSImage.
>
>
[image lockFocus];
>
[myObject draw];
>
bitmap = [[[NSBitmapImageRep alloc] initWithFocusedViewRect:myRect]
>
autorelease];
>
[image unlockFocus];
>
>
That's the canonical method of getting a bitmap of a rendered graphic
>
in Cocoa, as far as I am aware, and is independent of NSImage
>
implementation details. Another method is to get the
>
TIFFRepresentation, and then re-reading that, but that involves an
>
extra encoding/decoding step.
>
>
Nice, but the problem begin when you modify the NSBitmapImageRep by
accessing directly the pixels
and then try to make a new image from the modified pixels...
Using addRepresentaion with the NSBitmapRep may seem a solution at
first but as soon as you lockFocus
on the NSImage, a NSCachedImageRep is added and used instead of the
NSBitmapRep
In other words you never see the modified pixels even if they are
there... (exporting the image to TIFF and opening in Preview
will show you each representation)
Regarding the solution of Dietmar accessing the PixMapHandle, I have
done some testing and got strange result:
the PixBaseAddr is NOT the top-left pixel of the NSImage but the top
left pixel of the shared offscreen window
(using setCachedSeparately:YES doesn't guarantee a separate buffer)
this should not be a problem if the rectangle returned by GetPixBounds
was correct but it's not:
it's always at (top,left) = (0,0) ????
So I supposed there is something to do with the enclosing CGrafPtr
port's origin but for
an 512 x 512 offscreen I got port bounds = (0,0,512,512)
and rowbytes = 5120 !!! which is the size of my screen (1280 pixels x 4
bytes per pixel)
and trying to flood fill the image results in flood filling the menu
bar (nice effect but not what was expected !)
which obviously means pixBaseAddr was the base address of the screen
Any idea ????
However, I am still on the problem and may have found another solution
where, if I still have to play with
the ((graphicsContextInstance) [NSGraphicsContext
currentContext])->context trick,
this time it will be in a read only way (not perfect but much better...)
the idea (to be tested) is to draw the NSBitmapImageRep by using
CGImageCreate using a data provider supplying the bytes of the
NSBitmapImageRep
and then
CGContextDrawImage( ((graphicsContextInstance) [NSGraphicsContext
currentContext])->context, imageFrame, imageRef) ;
If this works, I will post the solution...
But I suspect (not sure yet) this may be slower than my original
"offending" trick...
Pascal Pochet
email@hidden
----------------------------------
PGP
KeyID: 0x208C5DBF
Fingerprint: 9BFB 245C 5BFE 7F1D 64B7 C473 ABB3 4E83 208C 5DBF
[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.