Imagine, there is a game, there are many colors, the picture is exactly the same, just the color of character clothes. For example, a true red Ken and fake cyan ken in the streets, their graphics are exactly the same, but the color is changed. What do you do at this time? How many pictures painted? Please, all in the 21st century, don't do this, don't work, no technical content is good? Smart, you will think that if you can "replace" in the red "replace" into a cyan. OK, then let's replace it. A lot of pictures such as GIF, PNG, are used to record colors with a palette. For example, the record 3 color is 0xff0000 red, then we change the 3 color to cyan code, and all the regions of all markers 3 colors become cyan. how about it? It seems simple to say it? ^ _ ^ Next we use the most popular PNG format in J2ME mobile phone to complete this work. First we have to clear the format of the PNG picture. The first is the PNG flag of 8 byte. Second, several blocks, each block has the following structure: 4 Byte Length Block LENGTH4 BYTE TYPE block Type Length Byte Data Block Data4 Byte CRC block type and Data two districts a total of Length 4 bytes of CRC The blocks we are interested in the palette sector. The content of the type area is 'p', 'l', 't', 'e' four bytes, and the DATA area is all colors in the format of 0XRGGBB, Length The value of the area is the number of colors * 3. OK, the basic knowledge is ready. (CRC checksum algorithm and PNG structure details can be referred to http://www.w3.org/tr/png-structure.html) Next Design Our Super Bull X's PalettedImage class, first provide two factories Method, one creates a picture from the package via a file name and is created from the Byte array. After creating, the Analyze method is executed immediately, obtain the number of color numbers, palette offset, CRC check code (for a picture is constant). You can replace some color with SetColor or replace all the color values of the entire palette with SetPalette. Remember to regenerate the correct CRC check and recreate the image after each replacement colors.
The advantage of this class is that it is not necessary to carry a lot of picture resources, but only one picture and several set tones are available. A disadvantage is that it takes up twice the memory of a picture (IMGDATA array and image object), but you can release the PALETTEDIMAGE after getting new Image.
With a simple example (click here to download), press 5 key KEN to change clothes.
Here is the rendering: