Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (108)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  recalculating png crc  (Read 1674 times)
0 Members and 1 Guest are viewing this topic.
Offline dr_sulli

Junior Newbie





« Posted 2005-03-23 12:53:39 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
/**
          * 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;
   }
Offline Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #1 - Posted 2005-03-23 14:38:21 »

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

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline dr_sulli

Junior Newbie





« Reply #2 - Posted 2005-03-29 09:39:32 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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){}            
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #3 - Posted 2005-03-29 10:03:49 »

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.)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
278  
279  
280  
281  
282  
283  
284  
285  
286  
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);
            }
      }
}

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline dr_sulli

Junior Newbie





« Reply #4 - Posted 2005-03-29 10:44:47 »

Thanks, i'll give that a go.
dr_sulli
Offline Fraggle

Senior Newbie




Java is Great!


« Reply #5 - Posted 2005-03-31 22:13:25 »

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 Smiley It would take me ages Sad

Thanks,

Seb

Build multiplayers mobile java game for a living.
Offline dr_sulli

Junior Newbie





« Reply #6 - Posted 2005-04-04 16:20:48 »

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
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (17 views)
2014-07-30 21:08:39

Riven (23 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (43 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!