recalculating png crc

Hi,
I’m currently working on a midp1 game that requires the palette of a png image to be modified and therefore requires the crc to be recalculated. When run on phones the IllegalArgumentException is thrown when the Image.createImage(byte[],int,int) method is called. I am using code taken from the png spec to recalculate the crc. I have posted it below.
Has anyone ever successfully implemented a function that achieves this sort of funcitonailty?
I would be very appreciative of any help.

Cheers,
dr_sulli


/**
          * Just build a plain old fashioned table based on good, old fashioned
          * values like the CRC32_POLYNOMIAL.  The table is of a fixed size.
          */
         private void buildCRCTable()
         {
            final int CRC32_POLYNOMIAL = 0xEDB88320;

            int i, j;
            int crc;

            CRCTable = new int[256];

            for (i = 0; i <= 255; i++)
            {
               crc = i;
               for (j = 8; j > 0; j--)
                  if ((crc & 1) == 1)
                     crc = (crc >>> 1) ^ CRC32_POLYNOMIAL;
                  else
                     crc >>>= 1;
               CRCTable[i] = crc;
            }
         }

 /**
          * General CRC generation function.
          *
          * @param buffer byte array to generate the CRC32
          * @param start byte start position
          * @param count number of byte's to include in CRC calculation
          * @param crc previously generated CRC32.
          *
          * @return 32 bit CRC
          */
         public int crc32(byte buffer[], int start, int count, int lastcrc)
         {
            int temp1, temp2;
            int i = start;

            int crc = lastcrc;

            while (count-- != 0)
            {
               temp1 = crc >>> 8;
               temp2 = CRCTable[(crc ^ buffer[i++]) & 0xFF];
               crc = temp1 ^ temp2;
            }

            return crc;
   }

When generating the crc for a chunk, make sure you have the start and end bytes correct.

The CRC should start on the 1st byte after the chunk length, and end on the last byte before the crc 4 bytes.

The PNG technical specification should have all the info. you need to write your own png reader/writer & manipulator.

http://download.mirror.ac.uk/sites/www.libpng.org/pub/png/spec/1.2/PNG-Structure.html#PNG-file-signature

I have re-read the png spec and made sure i’m not including the 4-byte length in the calculation. I’ve been checking the code by running the calculation on an unchanged chunk of the png and then comparing the old and new crc’s, which should be the same.
The only other area i can see could be causing a problem would be when i convert the crc from an int back into 4 bytes to be added to the newpng byte[]. The code i’m using is shown below, i don’t see how this can be wrong.

thanks for the help,
dr_sulli


int newCrc= crc32(chunk,0,chunk.length,0xFFFFFFFF);
byte_out = new ByteArrayOutputStream ();
data_out = new DataOutputStream (byte_out);

try{
      data_out.writeInt(newCrc);
}
catch(IOException ioe){
      System.out.println("caught exception at line 846");
}
byte[] crcBytes= byte_out.toByteArray();
int blen= crcBytes.length;
for(int i=0;i<blen;i++){
      newBuffer[(bl-4)+i]=crcBytes[i];
}
try{
      im= Image.createImage(newBuffer, 0, bl);
}
catch(IllegalArgumentException iae){}            

DUnno what you are doing wrong then, perhaps writing the CRC in the wrong byte order.

Heres a chunk of code that most definitely works.

The actual purpose of this code is rather irrelevant - its the crc generation and writing thats important in your situation.

(For the record, this was a little chunk of code I wrote to fix the palette chunk due to a bug in a particular phones png decoder.)


import java.util.*;
import java.io.*;

public class PNGColorFixer {
      public static void main(String [] args) throws Exception
      {
            File srcImage = new File(args[0]);

            byte [] data = new byte[(int)srcImage.length()];

            FileInputStream fis = new FileInputStream(srcImage);
            fis.read(data);
            fis.close();

            System.out.println(" ******* PAL FIX *******");
            data = fixPal(data);

            FileOutputStream fos = new FileOutputStream(args[0]);
            fos.write(data);
            fos.flush();
            fos.close();
      }

      static final int PNGHEADER_LENGTH = 8;

      static final int CHUNKLENGTH_LENGTH = 4;
      static final int CHUNKNAME_LENGTH = 4;
      static final int CHUNKCRC_LENGTH = 4;

      static final int CHUNKNAME_IHDR = ((byte)'I' <<24) | ((byte)'H' <<16) | ((byte)'D' <<8) | ((byte)'R');
      static final int CHUNKNAME_IDAT = ((byte)'I' <<24) | ((byte)'D' <<16) | ((byte)'A' <<8) | ((byte)'T');
      static final int CHUNKNAME_PLTE = ((byte)'P' <<24) | ((byte)'L' <<16) | ((byte)'T' <<8) | ((byte)'E');
      static final int CHUNKNAME_tRNS = ((byte)'t' <<24) | ((byte)'R' <<16) | ((byte)'N' <<8) | ((byte)'S');

      static int [] CRC_TABLE;



      //changes the 1st color in the palette to pink (fix for the SHITE E710)
      // index 0 will almost always be occupied by the transparent color for palettized PNGs
      public static byte [] fixPal(byte [] srcData)
      {
            System.out.println(" ******* srcData length = " + srcData.length + " *******");

            /* lazy creation of the CRC_TABLE */

            int [] crcTable = CRC_TABLE;


            if(crcTable==null)
            {
                   crcTable = new int [256];
                int c;
                for (int n = 0; n < 256; n++)
                {
                        c = n;
                        for (int k = 0; k < 8; k++)
                        {
                              if ((c & 1) != 0)
                              {
                                    c = ((int) 0xedb88320) ^ (c >>> 1);
                              }
                              else
                              {
                                    c >>>= 1;
                              }
                        }
                        crcTable[n] = c;
                 }
                 CRC_TABLE = crcTable;
             }



            int palChunkStart =0; //start of the data section of the chunk
            int palChunkLength =0;

            boolean hasAlpha = false;

            // This code assumes the PLTE chunk will appear before the tRNS chunk

            out:
            for(int i = PNGHEADER_LENGTH;;)
            {
                  int chunkLength = MSB.readInt(srcData,i);

                  System.out.println("chunkLength=" + chunkLength);

                  i+=CHUNKLENGTH_LENGTH;

                  int chunkType = MSB.readInt(srcData,i);

                  System.out.println("srcChunkType=" + chunkType);

                  i+=CHUNKNAME_LENGTH;

                  switch(chunkType)
                  {
                        case CHUNKNAME_IDAT:
                        break out;

                        case CHUNKNAME_tRNS:
                        hasAlpha = true;
                        break;

                        case CHUNKNAME_PLTE:
                        palChunkStart = i;
                        palChunkLength = chunkLength;
                        break;
                  }
                  i+=chunkLength+CHUNKCRC_LENGTH;
            }


            //if there is no alpha, return with no changes
            if(!hasAlpha) return srcData;

            MSB.writeByte(255, srcData,palChunkStart);
            MSB.writeByte(0, srcData,palChunkStart+1);
            MSB.writeByte(255, srcData,palChunkStart+2);


            int crc = -1;

            //regenerate the CRC for the palette chunk
            for(int i = palChunkStart-CHUNKNAME_LENGTH;i < (palChunkStart+palChunkLength);i++)
            {
                  crc = crcTable[(crc ^ srcData[i]) & 0x00ff] ^ (crc >>> 8);
            }

            crc ^= -1;

            MSB.writeInt(crc, srcData, palChunkStart+palChunkLength);
            return srcData;
      }



      public static class LSB
      {
            public static int readInt(byte [] src, int index)
            {
                  int retValue =  ((src[index]&0xFF)) |
                                          ((src[index+1]&0xFF) << 8) |
                                          ((src[index+2]&0xFF) << 16) |
                                          ((src[index+3]&0xFF) << 24);

                  System.out.println("LSB.readInt @" + index +" ; " + Integer.toHexString(index));
                  System.out.println("value = " + retValue + "; " + Integer.toHexString(retValue));
                  return retValue;
            }

            public static int readShort(byte [] src, int index)
            {
                  int retValue = (
                                          (src[index]&0xFF) |
                                        ((src[index+1]&0xFF) << 8)
                                        )&0xFFFF;

                  System.out.println("LSB.readShort @" + index +" ; " + Integer.toHexString(index));
                  System.out.println("value = " + retValue + "; " + Integer.toHexString(retValue));
                  return retValue;
            }

            public static int readByte(byte [] src, int index)
            {
                  int retValue = src[index]&0xFF;
                  System.out.println("LSB.readByte @" + index +" ; " + Integer.toHexString(index));
                  System.out.println("value = " + retValue + "; " + Integer.toHexString(retValue));
                  return retValue;
            }

            public static void writeInt(int val, byte [] dst, int index)
            {
                  System.out.println("LSB.writeInt @" + index +" ; " + Integer.toHexString(index));
                  System.out.println("value = " + val + "; " + Integer.toHexString(val));

                  for(int k = 0;k<4;k++)
                  {
                        dst[index+k] = (byte)val;
                        val >>=8;
                  }
            }

            public static void writeShort(int val, byte [] dst, int index)
            {
                  System.out.println("LSB.writeShort @" + index +" ; " + Integer.toHexString(index));
                  System.out.println("value = " + val + "; " + Integer.toHexString(val));

                  for(int k = 0;k<2;k++)
                  {
                        dst[index+k] = (byte)val;
                        val >>=8;
                  }
            }

            public static void writeByte(int val, byte [] dst, int index)
            {
                  System.out.println("LSB.writeByte @" + index +" ; " + Integer.toHexString(index));
                  System.out.println("value = " + val + "; " + Integer.toHexString(val));
                  dst[index] = (byte)val;
            }

            public static boolean sanityCheck()
            {
                  byte [] testData = new byte[4];

                  int testValue = 1;

                  writeInt(testValue,testData,0);

                  int testValue2 = readInt(testData,0);
                  return (testValue==testValue2);
            }
      }

      public static class MSB
      {
            public static int readInt(byte [] src, int index)
            {
                  int retValue = ((src[index]&0xFF) << 24) | ((src[index+1]&0xFF) << 16) | ((src[index+2]&0xFF) << 8) | ((src[index+3]&0xFF));
                  System.out.println("MSB.readInt @" + index +" ; " + Integer.toHexString(index));
                  System.out.println("value = " + retValue + "; " + Integer.toHexString(retValue));
                  return retValue;
            }

            public static int readShort(byte [] src, int index)
            {
                  int retValue = (((src[index]&0xFF) << 8) | ((src[index+1]&0xFF)))&0xFFFF;
                  System.out.println("MSB.readShort @" + index +" ; " + Integer.toHexString(index));
                  System.out.println("value = " + retValue + "; " + Integer.toHexString(retValue));
                  return retValue;
            }

            public static int readByte(byte [] src, int index)
            {
                  int retValue = src[index]&0xFF;
                  System.out.println("MSB.readByte @" + index +" ; " + Integer.toHexString(index));
                  System.out.println("value = " + retValue + "; " + Integer.toHexString(retValue));
                  return retValue;
            }

            public static void writeInt(int val, byte [] dst, int index)
            {
                  System.out.println("MSB.writeInt @" + index +" ; " + Integer.toHexString(index));
                  System.out.println("value = " + val + "; " + Integer.toHexString(val));

                  for(int k = 3;k>=0;k--)
                  {
                        dst[index+k] = (byte)val;
                        val >>=8;
                  }
            }

            public static void writeShort(int val, byte [] dst, int index)
            {
                  System.out.println("MSB.writeShort @" + index +" ; " + Integer.toHexString(index));
                  System.out.println("value = " + val + "; " + Integer.toHexString(val));
                  for(int k = 1;k>=0;k--)
                  {
                        dst[index+k] = (byte)val;
                        val >>=8;
                  }
            }

            public static void writeByte(int val, byte [] dst, int index)
            {
                  System.out.println("MSB.writeByte @" + index +" ; " + Integer.toHexString(index));
                  System.out.println("value = " + val + "; " + Integer.toHexString(val));
                  dst[index] = (byte)val;
            }


            public static boolean sanityCheck()
            {
                  byte [] testData = new byte[4];

                  int testValue = 1;

                  writeInt(testValue,testData,0);

                  int testValue2 = readInt(testData,0);
                  return (testValue==testValue2);
            }
      }
}

Thanks, i’ll give that a go.
dr_sulli

Could you share the final code for palette change in png?
If someone has it for gif too… :-p

We were thinking of doing the same thing here, but image file format manipulations is definitly not my strongest skill :slight_smile: It would take me ages :frowning:

Thanks,

Seb

i’m still having trouble with certain devices. It seems to run on some of the sony ericsson’s but not others.
very frustrating.
probably a platform issue.

will post it if i get it working in full.

dr_sulli