c++ - Get image pixels using mask -
so have original image.
i need , display part of image using specific mask. mask not rectangle or shape. contains different polygons , shapes.
are there methods or tutorials how implement that? or start make this? shall write small shader or compare appropriate pixels of images or this?
will glad advises.
thank you
you can put image in uiimageview , add mask image view like
myimageview.layer.mask = mymasklayer;
to draw custom shapes, mymasklayer should instance of own subclass of calayer implements drawincontext
method. method should draw areas shown in image full alpha , areas hidden 0 alpha (and can use in between alphas if like). here's example implementation of calayer subclass that, when used mask on image view, cause oval area in top left corner of image shown:
@interface mymasklayer : calayer @end @implementation mymasklayer - (void)drawincontext:(cgcontextref)ctx { static cgcolorspaceref rgbcolorspace; static dispatch_once_t oncetoken; dispatch_once(&oncetoken, ^{ rgbcolorspace = cgcolorspacecreatedevicergb(); }); const cgrect selfbounds = self.bounds; cgcontextsetfillcolorspace(ctx, rgbcolorspace); cgcontextsetfillcolor(ctx, (cgfloat[4]){0.0, 0.0, 0.0, 0.0}); cgcontextfillrect(ctx, selfbounds); cgcontextsetfillcolor(ctx, (cgfloat[4]){0.0, 0.0, 0.0, 1.0}); cgcontextfillellipseinrect(ctx, selfbounds); } @end
to apply such mask layer image view, might use code this
mymasklayer *masklayer = [[mymasklayer alloc] init]; masklayer.bounds = self.view.bounds; [masklayer setneedsdisplay]; self.imageview.layer.mask = masklayer;
now, if want pixel data rendering it's best render cgcontext backed bitmap control pixel buffer. can inspect pixel data in buffer after rendering. here's example create bitmap context , render layer , mask bitmap context. since supply own buffer pixel data, can go poking around in buffer rgba values after rendering.
const cgrect imageviewbounds = self.imageview.bounds; const size_t imagewidth = cgrectgetwidth(imageviewbounds); const size_t imageheight = cgrectgetheight(imageviewbounds); const size_t bytesperpixel = 4; // allocate own buffer bitmap data. can pass null // cgbitmapcontextcreate , quartz allocate memory you, // won't have pointer resulting pixel data want inspect! unsigned char *bitmapdata = (unsigned char *)malloc(imagewidth * imageheight * bytesperpixel); cgcontextref context = cgbitmapcontextcreate(bitmapdata, imagewidth, imageheight, 8, bytesperpixel * imagewidth, rgbcolorspace, kcgimagealphapremultipliedlast); // render image bitmap context. [self.imageview.layer renderincontext:context]; // set blend mode destination in, pixels become "destination * source alpha" cgcontextsetblendmode(context, kcgblendmodedestinationin); // render mask bitmap context. [self.imageview.layer.mask renderincontext:context]; // whatever here, chose middle of image. const size_t x = imagewidth / 2; const size_t y = imageheight / 2; const size_t pixelindex = y * imagewidth + x; const unsigned char red = bitmapdata[pixelindex]; const unsigned char green = bitmapdata[pixelindex + 1]; const unsigned char blue = bitmapdata[pixelindex + 2]; const unsigned char alpha = bitmapdata[pixelindex + 3]; nslog(@"rgba: {%u, %u, %u, %u}", red, green, blue, alpha);
Comments
Post a Comment