0

I am working on encoder decoder for animated webp image using pure java. I have to write static webp image from each from of animated webp image. It works fine but for the image having transparancy in image. reader gets flags for animation and alpha but when I write the flag for alpha (not setting animation as creating static image of given frame). But surprisingly it does not set the flag at all. I tried different indexes but not working. The output image have white background rather transparent bg.

code:

  public byte[] encodeWebPFrame(Frame frame, WebpImage webpImage) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        
        baos.write(riff); // 4
        baos.write(intToByteArray(getFileSize(frame))); // 4
        baos.write(webp); // 4

        baos.write(vp8x); // 4
        baos.write(intToByteArray(10)); // 4
        BitSet bs = new BitSet(32);
        bs.set(1, false); // A hasAnim
        bs.set(2, false); // X hasXmp
        bs.set(3, false); // E hasExif
        bs.set(4, true);  // L hasalpha
        bs.set(5, false); // I hasIccp
        baos.write(bitSetToBytes(bs,4)); // 4
        baos.write(intTo3ByteArray(frame.width)); // 3
        baos.write(intTo3ByteArray(frame.height)); // 3

        // alpha data, bitStream
        if(frame.hasVP8chunk) {
            if(frame.hasALPHchunk) {
                baos.write(alph); // 4
                baos.write(intToByteArray(frame.alphaData.length)); // 4
                baos.write(frame.alphaData); // frame.alphaData.length
            }
            baos.write(vp8); // 4
            baos.write(intToByteArray(frame.bitStream.length)); // 4
            baos.write(frame.bitStream); // frame.bitStream.length
        }else{
//            Logs.i(this,"write byte[] -> VP8L "+frame.bitStream.length+" | baso - "+baos.size());
            baos.write(vp8l); // 4
            baos.write(intToByteArray(frame.bitStream.length)); // 4
            baos.write(frame.bitStream); // frame.bitStream.length
        }
        baos.close();
        return baos.toByteArray();
    }


bs.set(4, true); // L hasalpha Here I am setting the alpha flag. If I set it to false then output is incorrect image. same logic I used to write to stream and it works fine but here I am getting error in writing to bytearrayoutputstream. Any idea?

CrackerKSR
  • 1,380
  • 1
  • 11
  • 29

0 Answers0