Image processing in iOS (iii)--mixed operation

Source: Internet
Author: User

Sometimes it is difficult or impossible to work on an image alone to get the effect we want. The birth of a good filter effect, often through a lot of complex steps, fine-tuning, picture application effect observation and a lot of layer overlay.

I found some common blending algorithms on jswidget, which correspond to some common blending modes, and through these blend modes, we can specify how the two images are blended.

But before we do, we need a plain color image and a gradient image to help:

[CPP]View Plaincopy
  1. + (UIImage *) Imagewithcolor: (Uicolor *) color size: (cgsize) size
  2. {
  3. //HTTP://STACKOVERFLOW.COM/QUESTIONS/1213790/HOW-TO-GET-A-COLOR-IMAGE-IN-IPHONE-SDK
  4. //create A context of the appropriate size
  5. Uigraphicsbeginimagecontext (size);
  6. Cgcontextref CurrentContext = Uigraphicsgetcurrentcontext ();
  7. //build A rect of appropriate size at Origin 0,0
  8. CGRect fillRect = CGRectMake (0, 0, size.width, size.height);
  9. //set the Fill color
  10. Cgcontextsetfillcolorwithcolor (CurrentContext, color. Cgcolor);
  11. //fill The color
  12. Cgcontextfillrect (CurrentContext, fillRect);
  13. //snap The picture and close the context
  14. UIImage *colorimage = Uigraphicsgetimagefromcurrentimagecontext ();
  15. Uigraphicsendimagecontext ();
  16. return colorimage;
  17. }

[CPP]View Plaincopy
  1. + (UIImage *) Imagewithgradient: (UIImage *) image startcolor: (Uicolor *) StartColor EndColor: (Uicolor *) EndColor
  2. {
  3. Uigraphicsbeginimagecontextwithoptions (Image.size, NO, Image.scale);
  4. Cgcontextref context = Uigraphicsgetcurrentcontext ();
  5. CGCONTEXTTRANSLATECTM (context, 0, image.size.height);
  6. CGCONTEXTSCALECTM (Context, 1.0,-1.0);
  7. Cgcontextsetblendmode (context, kcgblendmodenormal);
  8. CGRect rect = CGRectMake (0, 0, image.size.width, image.size.height);
  9. Cgcontextdrawimage (context, rect, image. Cgimage);
  10. //Create gradient
  11. Nsarray *colors = [Nsarray arraywithobjects: (ID) endcolor.cgcolor, (ID) startcolor.cgcolor, nil];
  12. Cgcolorspaceref space = Cgcolorspacecreatedevicergb ();
  13. Cggradientref gradient = cggradientcreatewithcolors (space, (CFARRAYREF) colors, NULL);
  14. //Apply gradient
  15. Cgcontextcliptomask (context, rect, image. Cgimage);
  16. Cgcontextdrawlineargradient (context, gradient, Cgpointmake (0,0), Cgpointmake (0, Image.size.height), 0);
  17. UIImage *gradientimage = Uigraphicsgetimagefromcurrentimagecontext ();
  18. Uigraphicsendimagecontext ();
  19. Cggradientrelease (gradient);
  20. Cgcolorspacerelease (space);
  21. return gradientimage;
  22. }

And the transparency filter mentioned in the first article (on the alpha value of the scope pixel) is ineffective and can be achieved by quartz 2D: [CPP]View Plaincopy
  1. -(UIImage *) Setalpha: (cgfloat) Alpha
  2. {
  3. //Http://stackoverflow.com/questions/5084845/how-to-set-the-opacity-alpha-of-a-uiimage
  4. Uigraphicsbeginimagecontextwithoptions (Self.size, NO, 0.0f);
  5. Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
  6. CGRect area = CGRectMake (0, 0, self.size.width, self.size.height);
  7. CGCONTEXTSCALECTM (CTX, 1,-1);
  8. CGCONTEXTTRANSLATECTM (CTX, 0,-area.size.height);
  9. Cgcontextsetblendmode (CTX, kcgblendmodemultiply);
  10. Cgcontextsetalpha (CTX, Alpha);
  11. Cgcontextdrawimage (CTX, area, self.) Cgimage);
  12. UIImage *newimage = Uigraphicsgetimagefromcurrentimagecontext ();
  13. Uigraphicsendimagecontext ();
  14. return newimage;
  15. }

On this basis, through the following four lines of code, you can get four different effects: [CPP]View Plaincopy
    1. return [[UIImage Imagewithcolor:[uicolor Purplecolor] size:originImage.size] changeopacitybyfactor:0.5];;

[CPP]View Plaincopy
    1. return [UIImage imagewithgradient:originimage startcolor:[uicolor Whitecolor] Endcolor:[uicolor YellowColor]];

[CPP]View Plaincopy
    1. return [[Originimage Tintwithmaxrgba: (RGBA) {Minrgba, overlaywithimage:[[uiimage): (RGBA) {[] Imagewithcolor:[uicolor Purplecolor] size:originImage.size] changeopacitybyfactor:0.3];

[CPP]View Plaincopy
    1. return [Originimage softlightwithimage:[[uiimage imagewithcolor:[uicolor Yellowcolor] size:originImage.size] changeopacitybyfactor:0.8]];



Among them, the overlay algorithm is as follows:

[CPP]View Plaincopy
  1. Double Calcoverlay (float B, float t)
  2. {
  3. return (b > 128.0f) 255.0f-2.0f * (255.0f-t) * (255.0f-b)/255.0f: (b * t * 2.0f)/255.0f;
  4. }
  5. void Filteroverlay (UInt8 *pixelbuf, UInt8 *pixedblendbuf, UInt32 offset, void *context)
  6. {
  7. int r = offset;
  8. int g = offset+1;
  9. int b = offset+2;
  10. int red = Pixelbuf[r];
  11. int green = Pixelbuf[g];
  12. int blue = pixelbuf[b];
  13. int blendred = Pixedblendbuf[r];
  14. int blendgreen = pixedblendbuf[g];
  15. int blendblue = pixedblendbuf[b];
  16. Pixelbuf[r] = Safecolor (Calcoverlay (red, blendred));
  17. PIXELBUF[G] = Safecolor (Calcoverlay (green, blendgreen));
  18. PIXELBUF[B] = Safecolor (Calcoverlay (blue, blendblue));
  19. }



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Image processing in iOS (iii)--mixed operation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.