NSImage -> MacOSLIB : Copy Picture

I need to move a picture (actually copy it verbatim) into another picture object… and I’d like to do it faster than Drawpicture does it (which is faster, I’m hoping for faster).

I have perused MacOSLib … but my biggest problem is lack of adequate comments… tons of code, but unless you “know” what it does, you are hard pressed to figure out if it does what you want…

The NSImage code has a bunch of stuff using NSImage, CGImage etc…

I just need something that would do :

    myNewPicture=Copy_Of(Old_Picture)

and when complete I’d have TWO picture object (NOT two pointers to the same instance)

Right now I do it this way

  w=OldPicture.width
  h=OldPicture.height
  myNewPicture=New picture(w,h,32)
  g=myNewPicture.graphics
  // this is so the picture is not altered due to Cocoa interpolations
  Declare Sub CGContextSetInterpolationQuality Lib "Cocoa" ( context As Integer, quality As Integer )
  CGContextSetInterpolationQuality( g.handle( g.HandleTypeCGContextRef ),0 )
  g.DrawPicture old_picture,0,0,w,h,0,0,w,h

This seems to work (requires MacOSLIb)… and is slightly faster than above (0.15 vs 0.19)

Function CopyPicture(pict as picture) as picture
  #if TargetMacOS
    Dim cg_image As CGImage = CGImage.NewCGImage( pict )
    if cg_image<>nil then
      dim p as Picture = cg_image.MakePicture
      return   p
    else
      return  nil
    end if
  #endif
END FUNCTION

Are you building for Cocoa? Does changing

myNewPicture=New picture(w,h,32)

for

myNewPicture=New picture(w,h)

make any difference?

@Sam: I believe that all Cocoa pictures are 32 bit. If I’m not mistaken, the Picture constructor ignored the bit depth.

@Dave: Your Cocoa declare, while well intentioned, doesn’t appear to do anything because there is no scaling going on.

In fact, I would change your code thusly:

[code] w=OldPicture.width
h=OldPicture.height

myNewPicture=New picture(w,h,32)

g=myNewPicture.graphics

g.DrawPicture old_picture,0,0
[/code]

  • I removed the Cocoa declare.
  • I removed the extra parameters from the DrawPicture call. They aren’t required, because you’re not scaling the picture and you’re not drawing a sub-portion of it. It’s likely that there is an optimization in the framework (or Cocoa itself) when no scaling is going on.

I may have misunderstood then, because I thought that pictures declares without a bit depth were 32-Bit (8-bit alpha channel) images, where as with the bit depth specified, they were 32-Bit ignored alpha.

You’re both correct, pedantically speaking:

  • Passing an explicit bit depth results in a picture with a mask. The bit depth itself is ignored and the picture returned is always 32-bit (well, to be really pedantic it’s 24-bit because the alpha channel is unused).
  • Excluding the bit depth results in a 32-bit picture with an alpha channel.

There is a slight speed increase too, marginal.

With bit depth expressed, I get 20,008
Without bit depth, I get 16,916

here’s the code.

[code]
Dim n,l, itCount as integer
Dim p as new picture( 3000, 2000 )
Dim t as picture
Dim mTimer as double
Dim results( 0 ) as double
itCount = 10

// ------------------ With bit depth expressed

mTimer = microseconds
for l=0 to itCount
t = new picture( p.width, p.height, 32 )
t.graphics.drawpicture p, 0, 0
next

results.append microseconds - mTimer

// ------------------ Without bit depth expressed

mTimer = microseconds
for l=0 to itCount
t = new picture( p.width, p.height )
t.graphics.drawpicture p, 0, 0
next

results.append microseconds - mTimer

n = ubound( results )
for l=1 to n
listbox1.addrow format( results( l ) / itCount, “###,###,###,###,###,###” )
next[/code]

[quote=81439:@Sam Rowlands]There is a slight speed increase too, marginal.

With bit depth expressed, I get 20,008
Without bit depth, I get 16,916[/quote]

Yeah. You’re also going to notice a significant decrease in memory usage as there’s one less bitmap per Picture object. Pictures with alpha channels are also faster to draw because there’s no need to composite the image/mask together the first time it’s used (after being created/modified).