How to use the color palette to change the PNG Image Format

Source: Internet
Author: User
Tags format definition

This algorithm directly reads and modifies the color palette of images in PNG format by referring to an article from an expert, and then generates a new color palette to replace the original one.

In this way, the effects of common color changes in the game can be solved, and the game capacity is limited, so it cannot store too many genie images.

The specific process is not complex. You can search for information first and see the format definition of the PNG image. This algorithm finds the color palette and generates a new CRC check code based on the original format, and then replaces the original color palette. In this way, you can use a PNG Image to create multiple color copies.

 

Public class palettedimage {<br/> public image getpalettedimage (byte [] data, int [] originalcolors, <br/> int [] palettedcolors) {<br/> byte [] tempdata = new byte [data. length]; <br/> system. arraycopy (data, 0, tempdata, 0, Data. length); <br/> image IMG = NULL; <br/> int [] parameter = {<br/> 0, 0, 0 }; <br/> analyze (tempdata, parameter); <br/> for (INT I = 0; I <originalcolors. length; I ++) {<br/> r Eplacecolor (tempdata, parameter, originalcolors [I], <br/> palettedcolors [I]); <br/> <br/>}< br/> filldata (tempdata, parameter); <br/> try {<br/> IMG = image. createimage (tempdata, 0, Data. length); <br/> <br/>}< br/> catch (exception e) {<br/> system. out. println ("getpalettedimage &" + E. tostring (); <br/> <br/>}< br/> return IMG; <br/> <br/>}</P> <p> private void analyze (byte [] data, int [] Para) {<br/> int offset = 8; <br/> int chunklen = 0; <br/> while (data [Offset + 4]! = 0x50 | data [Offset + 5]! = 0x4c <br/> | data [Offset + 6]! = 0x54 | data [Offset + 7]! = 0x45) {<br/> chunklen = readint (data, offset); <br/> offset ++ = (4 + 4 + chunklen + 4 ); <br/> <br/>}< br/> chunklen = readint (data, offset); <br/> para [2] = chunklen/3; <br/> para [0] = offset + 8; <br/> para [1] = offset + 8 + chunklen; <br/> <br/>}</P> <p> private int readint (byte [] data, int offset) {<br/> return (data [offset] & 0xff) <24) <br/> | (data [Offset + 1] & 0xff) <16) <br/> | (data [Offset + 2] & 0xff) <8) | (data [Offset + 3] & 0xff ); <br/> <br/>}</P> <p> private void replacecolor (byte [] data, int [] para, int oldcolor, <br/> int newcolor) {<br/> byte RR = (byte) (oldcolor> 16) & 0xff); <br/> byte Gg = (byte) (oldcolor> 8) & 0xff); <br/> byte BB = (byte) (oldcolor & 0xff); <br/> for (INT I = 0, offset = para [0], temp = 0; I <para [2]; I ++, <br/> Offset + = 3) {<br/> If (RR = data [offset] & Gg = data [Offset + 1] <br/> & BB = data [Offset + 2]) {<br/> data [offset] = (byte) (newcolor> 16) & 0xff); <br/> data [Offset + 1] = (byte) (newcolor> 8) & 0xff); <br/> data [Offset + 2] = (byte) (newcolor & 0xff); <br/> break; <br/> <br/>}</P> <p> private void filldata (byte [] data, int [] para) {<br/> int checksum = update_crc (data, para [0]-4, para [2] * 3 + 4 ); <br/> data [para [1] = (byte) (checksum> 24) & 0xff ); <br/> data [para [1] + 1] = (byte) (checksum> 16) & 0xff ); <br/> data [para [1] + 2] = (byte) (checksum> 8) & 0xff ); <br/> data [para [1] + 3] = (byte) (checksum) & 0xff ); <br/> <br/>}</P> <p> private int update_crc (byte [] Buf, int off, int Len) {<br/> int C = 0 xffffffff; <br/> int N, K; <br/> int xx; <br/> int [] crc_table = new int [256]; <br/> for (n = 0; n <256; n ++) {<br/> xx = N; <br/> for (k = 0; k <8; k ++) {<br/> If (XX & 1) = 1) {<br/> xx = 0xedb88320 ^ (XX >>> 1 ); <br/> <br/>}< br/> else {<br/> xx = XX >>> 1; <br/> <br/>}< br/> crc_table [N] = xx; <br/> <br/>}< br/> for (n = off; n <Len + off; n ++) {<br/> C = crc_table [(C ^ Buf [N]) & 0xff] ^ (C >>> 8 ); <br/> <br/>}< br/> return (C ^ 0 xffffffff ); <br/> <br/>}</P> <p> <br/>}< br/>

 

The interface is the getpalettedimage () function. You only need to enter the byte array of the original image, the color value to replace the color, and the target color value. Because multiple colors can be replaced at the same time, the input parameter is an array representing the integer color. In short, make sure that the original color exactly corresponds to the target color. The method is simple and practical.

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.