Re: [Pyobjc-dev] Is it possible to create a CFData without copying bytes?
Brought to you by:
ronaldoussoren
|
From: Uliano G. <gue...@gm...> - 2007-11-11 15:31:04
|
Il giorno 10/nov/07, alle ore 16:24, Ronald Oussoren ha scritto:
>
> On 10 Nov, 2007, at 9:58, Uliano Guerrini wrote:
>
>> Hi,
>> this code:
>>
>> cf=Foundation.CFDataCreate(None,string,len)
>>
>> works as expected, while:
>>
>> cf=Foundation.CFDataCreateWithBytesNoCopy(None,string,len,None)
>>
>> generates crap data. How can I fix this?
>
> both seem to work as expected.
well now that I tried some simple typing on the terminal I have to agree
> What might bite you is that you must make sure that 'string' is kept
> alive as long as you might access 'cf', Python's garbage collector
> doesn't (and cannot) know that CFDataCreateWithBytesNoCopy sneeks a
> reference to part of a Python object in the object it creates.
>
> Could you please create a demo program that demonstrates the problem
> you're having?
the program for the moment is nonsensic experimenation, here are the
snippets causing the problem
def newf():
a=numpy.zeros((256,256),dtype='float32')
for x in range(0,256):
a[:,x]=x/255.0
return a.tostring()
def cff():
return Foundation.CFDataCreateWithBytesNoCopy(None,newf(),
65536*4,None)
def dpf():
return CGDataProviderCreateWithCFData(cff())
def imf():
colorspace=CGColorSpaceCreateDeviceGray()
c=kCGImageAlphaNone | kCGBitmapFloatComponents |
kCGBitmapByteOrder32Little
return CGImageCreate(256,256,32,32,256*4,colorspace,c,dpf(),None,
0,kCGRenderingIntentDefault)
def cii():
a=CIImage.imageWithCGImage_(imf())
return a
this core image is then plotted on a view with this method
def drawRect_(self, rect):
ciicontext=NSGraphicsContext.currentContext().CIContext()
ciicontext.drawImage_inRect_fromRect_(NumpyCFImage.cii(),self.bounds(),
((0,0),(256,256)))
changing between CFDataCreate and CFDataCreateWithBytesNoCopy makes
the difference from expected image and crapy noise
uliano
>
|