During lock down I thought I would have some fun in my spare time and see if it was possible to create an LC APNG player. I have found an online APNG decoder and the APNG spec which I was following. However I have had minimal success. I can export the images but they are corrupted. I'm thinking it has something to do with the CRC check. Maybe if you guys with much more experience than me have the time you can correct my mistakes and get it going.
The idea was to break up a APNG file and push it through an image object using the APNG parameters.
If anyone has a little spare time and wants to have a bit of fun please download and have a look. But please don't judge the code I was just experimenting.
Thanks
https://stackoverflow.com/questions/187 ... s-with-php
Code: Select all
on mouseUp pMouseButton
   put img "elephant.png" into test1
     
   put empty into tPic
   
   put byte 1 to 8 in test1 into tSig
   put 8 into toffset
   put the len of test1 into tSize
   --hex 76f6b354 7796403
   
   repeat until toffset >= tSize
      --error break
      add 1 to tcounter
      put byte toffset+1 to (toffset+4) in test1 into tLength
      add 4 to toffset
      
      put byte toffset+1 to (toffset+4) in test1 into tType
      add 4 to toffset
      
      // Unpack the length and read the chunk data including 4 byte CRC
      --$ilength = unpack('Nlength', $length); --output binary into array $length (convert decimal to binary array)
      --$ilength = $ilength['length']; --put the $length into array $ilength['length']
      --$chunk = substr($data, $offset, $ilength+4); --get the frame data $offset(from) (to)=($ilength+4) incl 4 byte CRC (remove first 4 bytes from chunk as it is sequence data)
      --$offset += $ilength+4; --adds 4 to the offset (why?)(maybe (maybe CRC)get ready for next chunk?)
      breakpoint
      put binaryDecode("M",tLength,sLength) into pLength --get the chunk lenght of frame
      --put tLength into tLength['length']
      if tType contains "fdAT" then
         
         put byte toffset+1 to ((toffset+1)+sLength) in test1 into tChunk --get location of frame and save to chunk
         
         --put byte 1 to 4 in tChunk into tChunkSequence
         --put binaryDecode("M",tChunkSequence,tChunkSequence) into pLength
         --put return & tChunkSequence after fld "seq"
         --breakpoint
         put byte +5 to -1 in tChunk into tChunk --remove 4 bytes sequence from start of chunk
         put sLength-4 into pLength --remove 4 byte sequnce from decimal length 
         
         put byte (toffset+1+sLength) to (toffset+sLength+4) in test1 into tCRCTest --test crc
      else
         put byte toffset+1 to (toffset+sLength+4) in test1 into tChunk --get location of frame and save to chunk
      end if
      
      --add 4 to toffset
      put toffset + (sLength+4) into toffset
      
      switch
         case tType contains "IHDR"
            --header
            --$header = $length . $type . $chunk;
            
            put byte -4 to -1 in tChunk into tCRC
            put binaryDecode("M",tCRC,sCRC) into pCRC
            
            put tLength & tType & tChunk into tHeader
            --breakpoint
            break
         case tType contains "IDAT"
            put tLength & tType & tChunk into tIDAT
            break
         case tType contains "IEND"
            breakpoint
            put tLength & tType & tChunk into tIEND
            break
         case tType contains "fdAT"
            
            --put char toffset to toffset+sLength in test1 into tChunk --just data and type should get a crc generated
            put "IDAT" before tChunk
            --generate crc
            put Crc32Str(tChunk)into scrc --hex
            --put "0x" before tcrc
            --breakpoint
            --put binaryDecode("M",tCRCTest,sCRCTest) into pCRCTest
            --breakpoint
            --put baseConvert(sCRCTest,10,10) into sCRCTest
            
            put baseConvert(scrc,16,10) into tcrc --hex to decimal
            
            put binaryEncode("M",tcrc) into tcrc --deciaml to binary
            
            --put char 11 to -9 of compress(tcrc) into tcrc
            --breakpoint
            put tcrc after tChunk
            --put char -4 to -1 of tChunk into tChunk --remove crc
            if fts is "65" then
               breakpoint
            end if
            put binaryEncode("M",pLength) into pLength --encode new length for file
            
            put pLength & tChunk into tPic --minus 4 from the length then convert to binary
            --$length . $type . $chunk; (parts)
            --$signature . $header . $parts[$i] . $end; (file)
            break
         case tType contains "fcTL"
            --breakpoint
            put byte 9 to 12 of tChunk into tw --width 480
            put binaryDecode("M",tw,fw) into ptw
            
            put byte 5 to 8 of tChunk into th --height 400
            put binaryDecode("M",th,fh) into pth
            
            put byte 1 to 4 of tChunk into ts --sequence 1
            put binaryDecode("M",ts,fts) into pts
            
            if fts is "65" then
               --breakpoint
            end if
            
            put byte 21 to 22 of tChunk into tfnum
            put binaryDecode("m",tfnum,fnum) into pfnum
            
            put byte 23 to 24 of tChunk into tdnum
            put binaryDecode("m",tdnum,dnum) into pdnum
            break
      end switch
      if tcounter >= 1000 then
         exit repeat
      end if
   end repeat
   breakpoint
   --$signature . $header . $parts[$i] . $end; 
   put tSig & tHeader & tPic & tIEND into sPic
   set the text of img "test3" to sPic --into img "test3"
   
   put specialfolderpath("desktop") & "/image.png" into theFilepath
   open file theFilepath for binary write ## you might also use 'binary update' here to modify part of file
   write sPic to file theFilePath
   close file theFilePath 
   end mouseup 

 
 

