heifwriter/src/main/java/androidx/heifwriter/HeifEncoder.java
private static void copyOneTileYUV(
            ByteBuffer srcBuffer, Image dstImage,
            int srcWidth, int srcHeight,
            Rect srcRect, Rect dstRect) {
        if (srcRect.width() != dstRect.width() || srcRect.height() != dstRect.height()) {
            throw new IllegalArgumentException("src and dst rect size are different!");
        }
        if (srcWidth % 2 != 0      || srcHeight % 2 != 0      ||
                srcRect.left % 2 != 0  || srcRect.top % 2 != 0    ||
                srcRect.right % 2 != 0 || srcRect.bottom % 2 != 0 ||
                dstRect.left % 2 != 0  || dstRect.top % 2 != 0    ||
                dstRect.right % 2 != 0 || dstRect.bottom % 2 != 0) {
            throw new IllegalArgumentException("src or dst are not aligned!");
        }

        Image.Plane[] planes = dstImage.getPlanes();
        for (int n = 0; n < planes.length; n++) {
            ByteBuffer dstBuffer = planes[n].getBuffer();
            int colStride = planes[n].getPixelStride();
            int copyWidth = Math.min(srcRect.width(), srcWidth - srcRect.left);
            int copyHeight = Math.min(srcRect.height(), srcHeight - srcRect.top);
            int srcPlanePos = 0, div = 1;
            if (n > 0) {
                div = 2;
                srcPlanePos = srcWidth * srcHeight * (n + 3) / 4;
            }
            for (int i = 0; i < copyHeight / div; i++) {
                srcBuffer.position(srcPlanePos +
                        (i + srcRect.top / div) * srcWidth / div + srcRect.left / div);
                dstBuffer.position((i + dstRect.top / div) * planes[n].getRowStride()
                        + dstRect.left * colStride / div);

                for (int j = 0; j < copyWidth / div; j++) {
                    dstBuffer.put(srcBuffer.get());
                    if (colStride > 1 && j != copyWidth / div - 1) {
                        dstBuffer.position(dstBuffer.position() + colStride - 1);
                    }
                }
            }
        }
    }
/**
     * Copies from a rect from src buffer to dst image.
     * TOOD: This will be replaced by JNI.
     */
Copies from a rect from src buffer to dst image.
TOOD: This will be replaced by JNI.
Copies from a rect from src buffer to dst image . TOOD : This will be replaced by JNI .
private static void copyOneTileYUV(
            ByteBuffer srcBuffer, Image dstImage,
            int srcWidth, int srcHeight,
            Rect srcRect, Rect dstRect) {
        if (srcRect.width() != dstRect.width() || srcRect.height() != dstRect.height()) {
            throw new IllegalArgumentException("src and dst rect size are different!");
        }
        if (srcWidth % 2 != 0      || srcHeight % 2 != 0      ||
                srcRect.left % 2 != 0  || srcRect.top % 2 != 0    ||
                srcRect.right % 2 != 0 || srcRect.bottom % 2 != 0 ||
                dstRect.left % 2 != 0  || dstRect.top % 2 != 0    ||
                dstRect.right % 2 != 0 || dstRect.bottom % 2 != 0) {
            throw new IllegalArgumentException("src or dst are not aligned!");
        }
        Image.Plane[] planes = dstImage.getPlanes();
        for (int n = 0; n < planes.length; n++) {
            ByteBuffer dstBuffer = planes[n].getBuffer();
            int colStride = planes[n].getPixelStride();
            int copyWidth = Math.min(srcRect.width(), srcWidth - srcRect.left);
            int copyHeight = Math.min(srcRect.height(), srcHeight - srcRect.top);
            int srcPlanePos = 0, div = 1;
            if (n > 0) {
                div = 2;
                srcPlanePos = srcWidth * srcHeight * (n + 3) / 4;
            }
            for (int i = 0; i < copyHeight / div; i++) {
                srcBuffer.position(srcPlanePos +
                        (i + srcRect.top / div) * srcWidth / div + srcRect.left / div);
                dstBuffer.position((i + dstRect.top / div) * planes[n].getRowStride()
                        + dstRect.left * colStride / div);
                for (int j = 0; j < copyWidth / div; j++) {
                    dstBuffer.put(srcBuffer.get());
                    if (colStride > 1 && j != copyWidth / div - 1) {
                        dstBuffer.position(dstBuffer.position() + colStride - 1);
                    }
                }
            }
        }
    }
private static void copyOneTileYUV ( ByteBuffer srcBuffer , Image dstImage , int srcWidth , int srcHeight , Rect srcRect , Rect dstRect ) { if ( srcRect . width ( ) != dstRect . width ( ) || srcRect . height ( ) != dstRect . height ( ) ) { throw new IllegalArgumentException ( "src and dst rect size are different!" ) ; } if ( srcWidth % 2 != 0 || srcHeight % 2 != 0 || srcRect . left % 2 != 0 || srcRect . top % 2 != 0 || srcRect . right % 2 != 0 || srcRect . bottom % 2 != 0 || dstRect . left % 2 != 0 || dstRect . top % 2 != 0 || dstRect . right % 2 != 0 || dstRect . bottom % 2 != 0 ) { throw new IllegalArgumentException ( "src or dst are not aligned!" ) ; } Image . Plane [ ] planes = dstImage . getPlanes ( ) ; for ( int n = 0 ; n < planes . length ; n ++ ) { ByteBuffer dstBuffer = planes [ n ] . getBuffer ( ) ; int colStride = planes [ n ] . getPixelStride ( ) ; int copyWidth = Math . min ( srcRect . width ( ) , srcWidth - srcRect . left ) ; int copyHeight = Math . min ( srcRect . height ( ) , srcHeight - srcRect . top ) ; int srcPlanePos = 0 , div = 1 ; if ( n > 0 ) { div = 2 ; srcPlanePos = srcWidth * srcHeight * ( n + 3 ) / 4 ; } for ( int i = 0 ; i < copyHeight / div ; i ++ ) { srcBuffer . position ( srcPlanePos + ( i + srcRect . top / div ) * srcWidth / div + srcRect . left / div ) ; dstBuffer . position ( ( i + dstRect . top / div ) * planes [ n ] . getRowStride ( ) + dstRect . left * colStride / div ) ; for ( int j = 0 ; j < copyWidth / div ; j ++ ) { dstBuffer . put ( srcBuffer . get ( ) ) ; if ( colStride > 1 && j != copyWidth / div - 1 ) { dstBuffer . position ( dstBuffer . position ( ) + colStride - 1 ) ; } } } } }
Copies from a rect from src buffer to dst image.
Copies from a rect from src buffer to dst image .