Learn how to convert black and white photos with Flash (64) color bitmap every day

Source: Internet
Author: User

I read an article from a foreign country today and found it interesting. So I will record it here. Convert a color chart to a black or white image. In PS, only simple operations are required to convert a color image into a black image or a black image. Flash processing is also very simple.

 

Select a favorite image and then process it. For example, this photo shows a pretty girl. Haha

 

Now, you can use a piece of code to convert a photo to a black-and-white image:

 

In this process, the modification of each pixel will produce this effect. We obtain the value of each pixel through scanning. Then, the bitmap API is used to modify each value to generate this result.

Test (); <br/> function test (): void <br/>{< br/> trace (0xff); <br/> trace (0 xffffff ); <br/> var bit: bitmap = new Bitmap (New myclass (0, 0); <br/> addchild (BIT); <br/> bit. width = 225; <br/> bit. height = 336; <br/> for (var I: uint = 0; I <bit. bitmapdata. height; I ++) <br/>{< br/> for (VAR J: uint = 0; j <bit. bitmapdata. width; j ++) <br/>{< br/> var pixelvalue: uint = bit. bitmapdata. getpixel (J, I); <br/> bit. bitmapdata. setpixel (J, I, (pixelvalue & 0xff) <16 | (pixelvalue & 0xff) <8 | (pixelvalue & 0xff )); </P> <p >}< br/>}

 

The key is

VaR pixelvalue: uint = bit. bitmapdata. getpixel (J, I); <br/> bit. bitmapdata. setpixel (J, I, (pixelvalue & 0xff) <16 | (pixelvalue & 0xff) <8 | (pixelvalue & 0xff ));

To obtain the value of each pixel. RGB value, and then determine the effect based on bit operations.

 

 

Main APIs:

Setpixel (X: int, Y: int, color: uint): void sets a single pixel of A bitmapdata object. Getpixel (X: int, Y: INT): uint returns an integer that represents the RGB pixel value of a bitmapdata object at a specific point (x, y. Other notes: if the image size is not modified, the original for (var I: uint = 0; I <bit. bitmapdata. height; I ++) can be changed to for (var I: uint = 0; I <bit. height; I ++) otherwise it will not be possible. 0xff and 0 xffffff were originally thought to be the same, but after testing, we found that 0xff represents the 0--255 color, which is also a common RPG value. 0 xfffff output 16777215. The results are obviously different. Let's test it. Interestingly, (pixelvalue & 0xff) <16. Why are the results produced by the bitwise operation and then the shift? What is the pixel value of the black/white effect? Let me know if you want. The following is an encapsulation call by extending the bitmap class. Package <br/> {<br/> Import flash. display. bitmap; <br/> Import flash. display. bitmapdata; </P> <p> public class bitmapex extends bitmap <br/> {</P> <p> Public Function bitmapex (bitmapdata: bitmapdata = NULL) <br/>{< br/> super (bitmapdata, "Auto", false ); <br/>}< br/> // convert a black/white image <br/> Public Function converttobw (width: Number, height: number ): void <br/> {<br/> for (var I: uint = 0; I <peight; I ++) <br/>{< br/> for (var j: uint = 0; j <width; j ++) <br/>{< br/> var pixelvalue: uint = This. bitmapdata. getpixel (J, I); <br/> This. bitmapdata. setpixel (J, I, (pixelvalue & 0xff) <16 | (pixelvalue & 0xff) <8 | (pixelvalue & 0xff )); </P> <p >}< br/>}</P> <p >}< br/>}Test: VaR bitmap: bitmapex = new bitmapex (New myclass (0, 0); <br/> bitmap. width = 225; <br/> bitmap. height = 336; <br/> addchild (Bitmap); <br/> bitmap. converttobw (bitmap. bitmapdata. width, bitmap. bitmapdata. height );

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.