public byte[] compress(int width,int[] previousFrame,List<Integer> colorPalette) throws IOException {
  ByteArrayOutputStream outputArray=new ByteArrayOutputStream();
  DataOutputStream output=new DataOutputStream(outputArray);
  int rowCount=previousFrame.length / width;
  for (int row=0; row < rowCount; row++) {
    int offset=row * width;
    for (int iter=offset; iter < offset + width; iter++) {
      if (previousFrame[iter] != imageData[iter]) {
        output.writeShort(row);
        for (int rowIter=0; rowIter < width; rowIter++) {
          output.writeByte(getPaletteIndex(colorPalette,imageData[offset + rowIter]));
        }
        break;
      }
    }
  }
  output.writeShort(-1);
  output.close();
  byte[] bytes=outputArray.toByteArray();
  if (bytes.length < imageData.length / 10 * 7) {
    return bytes;
  }
  return null;
}

public byte [ ] compress ( int width , int [ ] previousFrame , List < Integer > colorPalette ) throws IOException { ByteArrayOutputStream outputArray = new ByteArrayOutputStream ( ) ; DataOutputStream output = new DataOutputStream ( outputArray ) ; int rowCount = previousFrame . length / width ; for ( int row = 0 ; row < rowCount ; row ++ ) { int offset = row * width ; for ( int iter = offset ; iter < offset + width ; iter ++ ) { if ( previousFrame [ iter ] != imageData [ iter ] ) { output . writeShort ( row ) ; for ( int rowIter = 0 ; rowIter < width ; rowIter ++ ) { output . writeByte ( getPaletteIndex ( colorPalette , imageData [ offset + rowIter ] ) ) ; } break ; } } } output . writeShort ( - 1 ) ; output . close ( ) ; byte [ ] bytes = outputArray . toByteArray ( ) ; if ( bytes . length < imageData . length / 10 * 7 ) { return bytes ; } return null ; }
try to compress the frame data , if this fails then return null and a keyframe would be inserted . failure is defined by compression coming out as more than the size of the original or close enough .
