ios - How to draw cropped bitmap using the Metal API or Accelerate Framework? -
i'm implementing custom video compositor crops video frames. use core graphics this:
-(void)renderimage:(cgimageref)image inbuffer:(cvpixelbufferref)destination { cgrect croprect = // rect ... cgimageref currentrectimage = cgimagecreatewithimageinrect(photofullimage, croprect); size_t width = cvpixelbuffergetwidth(destination); size_t height = cvpixelbuffergetheight(destination); cgcontextref context = cgbitmapcontextcreate(cvpixelbuffergetbaseaddress(destination), // data width, height, 8, // bpp cvpixelbuffergetbytesperrow(destination), cgimagegetcolorspace(backimage), cgimagegetbitmapinfo(backimage)); cgrect frame = cgrectmake(0, 0, width, height); cgcontextdrawimage(context, frame, currentrectimage); cgcontextrelease(context); }
how can use metal api this? should faster, right? using accelerate framework (vimage specifically)? simpler?
ok, don't know wether useful you, still. check out following code:
- (void)captureoutput:(avcaptureoutput *)captureoutput didoutputsamplebuffer:(cmsamplebufferref)samplebuffer fromconnection:(avcaptureconnection *)connection { cvpixelbufferref pixelbuffer = cmsamplebuffergetimagebuffer(samplebuffer); id<mtltexture> texturey = nil; { size_t width = cvpixelbuffergetwidth(pixelbuffer); size_t height = cvpixelbuffergetheight(pixelbuffer); mtlpixelformat pixelformat = mtlpixelformatbgra8unorm; cvmetaltextureref texture = null; cvreturn status = cvmetaltexturecachecreatetexturefromimage(null, _texturecache, pixelbuffer, null, pixelformat, width, height, 0, &texture); if(status == kcvreturnsuccess) { texturey = cvmetaltexturegettexture(texture); if (self.delegate){ [self.delegate textureupdated:texturey]; } cfrelease(texture); } } }
i use code convert cvpixelbufferref
mtltexture. after should create blitcommandencoder
, use it's
func copyfromtexture(sourcetexture: mtltexture, sourceslice: int, sourcelevel: int, sourceorigin: mtlorigin, sourcesize: mtlsize, totexture destinationtexture: mtltexture, destinationslice: int, destinationlevel: int, destinationorigin: mtlorigin)
in it, can select cropped rectangle , copy other texture.
the next step convert generated mtltextures
cvpixelbufferrefs
, make video out of that, unfortunately don't know how that.
would hear came with. cheers.