Create a PNG Image class for free color change

Source: Internet
Author: User

Imagine that there is a game in which there are many different colors. The pictures are the same, but the colors of character clothes are different. For example, the Red Ken and the false blue Ken in the street overlord are identical, but the color is changed.
What will you do now? How many images are drawn? Come on, it's 21 century. Don't do this kind of work without any technical skills, OK? If you are smart, you must think that you can replace the red color with blue. OK, then we will replace it.
Images in many formats such as GIF and PNG are recorded in the color palette. For example, if the color of "3" is 0xff0000 red, we will change the color of "3" to the blue code. All the areas marked as "3" in the image will become blue. How is it? Is it easy to say? ^_^ Below we will use the most PNG format images used by cell phones for this purpose.
First, we need to understand the PNG image format.
The first is the PNG flag of 8 bytes. There are several blocks. Each block has the following structure:
4 Byte Length
4 byte type block type
Length byte data block data
The 4-byte CRC block type and data have a total length + 4-byte CRC checksum.
We are interested in the color palette. The content of the type partition is four bytes: 'P', 'l', 't', and 'E, in the data area, all colors are arranged in the format of 0xrrggbb, and the value in the length area is the number of colors * 3. OK. The basic knowledge is ready. (CRC checksum algorithm and PNG structure details can refer to the http://www.w3.org/TR/PNG-Structure.html)
Next we will design the palettedimage class of super ox X. First we will provide two factory methods, one for creating images from the package through the file name, and the other for creating images directly from the byte array. Execute the analyze method immediately after creation to obtain the values of the color number, color palette offset, and CRC verification code offset (for a picture, these values remain unchanged ). In the future, you can use setcolor to replace a certain color or use setpalette to replace all the color values of the entire palette. After each color replacement, remember to generate the correct CRC checksum and recreate the image.

The advantage of this class is that you don't have to carry a lot of image resources, but you only need one image and several sets of color palette information. The disadvantage is that it will occupy twice the memory of an image (imgdata array and image object), but you can release the palettedimage after obtaining a new image.

With a simple example (Click here to download), press the 5-key Ken to change your clothes.

Here is:

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.