static void sunrast_image_write_image(AVCodecContext *avctx,

                                      const uint8_t *pixels,

                                      const uint32_t *palette_data,

                                      int linesize)

{

    SUNRASTContext *s = avctx->priv_data;

    const uint8_t *ptr;

    int len, alen, x;



    if (s->maplength) {     // palettized

        PutByteContext pb_r, pb_g;

        int len = s->maplength / 3;



        pb_r = s->p;

        bytestream2_skip_p(&s->p, len);

        pb_g = s->p;

        bytestream2_skip_p(&s->p, len);



        for (x = 0; x < len; x++) {

            uint32_t pixel = palette_data[x];



            bytestream2_put_byteu(&pb_r, (pixel >> 16) & 0xFF);

            bytestream2_put_byteu(&pb_g, (pixel >> 8)  & 0xFF);

            bytestream2_put_byteu(&s->p,  pixel        & 0xFF);

        }

    }



    len  = (s->depth * avctx->width + 7) >> 3;

    alen = len + (len & 1);

    ptr  = pixels;



     if (s->type == RT_BYTE_ENCODED) {

        uint8_t value, value2;

        int run;

        const uint8_t *end = pixels + avctx->height * linesize;



        ptr = pixels;



#define GET_VALUE ptr >= end ? 0 : x >= len ? ptr[len-1] : ptr[x]



        x = 0;

        value2 = GET_VALUE;

        while (ptr < end) {

            run = 1;

            value = value2;

            x++;

            if (x >= alen) {

                x = 0;

                ptr += linesize;

            }



            value2 = GET_VALUE;

            while (value2 == value && run < 256 && ptr < end) {

                x++;

                run++;

                if (x >= alen) {

                    x = 0;

                    ptr += linesize;

                }

                value2 = GET_VALUE;

            }



            if (run > 2 || value == RLE_TRIGGER) {

                bytestream2_put_byteu(&s->p, RLE_TRIGGER);

                bytestream2_put_byteu(&s->p, run - 1);

                if (run > 1)

                    bytestream2_put_byteu(&s->p, value);

            } else if (run == 1) {

                bytestream2_put_byteu(&s->p, value);

            } else

                bytestream2_put_be16u(&s->p, (value << 8) | value);

        }



        // update data length for header

        s->length = bytestream2_tell_p(&s->p) - 32 - s->maplength;

    } else {

        int y;

        for (y = 0; y < avctx->height; y++) {

            bytestream2_put_buffer(&s->p, ptr, len);

            if (len < alen)

                bytestream2_put_byteu(&s->p, 0);

            ptr += linesize;

        }

    }

}

static void sunrast_image_write_image(AVCodecContext *avctx,

                                      const uint8_t *pixels,

                                      const uint32_t *palette_data,

                                      int linesize)

{

    SUNRASTContext *s = avctx->priv_data;

    const uint8_t *ptr;

    int len, alen, x;



    if (s->maplength) {     

        PutByteContext pb_r, pb_g;

        int len = s->maplength / 3;



        pb_r = s->p;

        bytestream2_skip_p(&s->p, len);

        pb_g = s->p;

        bytestream2_skip_p(&s->p, len);



        for (x = 0; x < len; x++) {

            uint32_t pixel = palette_data[x];



            bytestream2_put_byteu(&pb_r, (pixel >> 16) & 0xFF);

            bytestream2_put_byteu(&pb_g, (pixel >> 8)  & 0xFF);

            bytestream2_put_byteu(&s->p,  pixel        & 0xFF);

        }

    }



    len  = (s->depth * avctx->width + 7) >> 3;

    alen = len + (len & 1);

    ptr  = pixels;



     if (s->type == RT_BYTE_ENCODED) {

        uint8_t value, value2;

        int run;

        const uint8_t *end = pixels + avctx->height * linesize;



        ptr = pixels;



#define GET_VALUE ptr >= end ? 0 : x >= len ? ptr[len-1] : ptr[x]



        x = 0;

        value2 = GET_VALUE;

        while (ptr < end) {

            run = 1;

            value = value2;

            x++;

            if (x >= alen) {

                x = 0;

                ptr += linesize;

            }



            value2 = GET_VALUE;

            while (value2 == value && run < 256 && ptr < end) {

                x++;

                run++;

                if (x >= alen) {

                    x = 0;

                    ptr += linesize;

                }

                value2 = GET_VALUE;

            }



            if (run > 2 || value == RLE_TRIGGER) {

                bytestream2_put_byteu(&s->p, RLE_TRIGGER);

                bytestream2_put_byteu(&s->p, run - 1);

                if (run > 1)

                    bytestream2_put_byteu(&s->p, value);

            } else if (run == 1) {

                bytestream2_put_byteu(&s->p, value);

            } else

                bytestream2_put_be16u(&s->p, (value << 8) | value);

        }



        

        s->length = bytestream2_tell_p(&s->p) - 32 - s->maplength;

    } else {

        int y;

        for (y = 0; y < avctx->height; y++) {

            bytestream2_put_buffer(&s->p, ptr, len);

            if (len < alen)

                bytestream2_put_byteu(&s->p, 0);

            ptr += linesize;

        }

    }

}

static void FUNC_0(AVCodecContext *VAR_0,

                                      const uint8_t *VAR_1,

                                      const uint32_t *VAR_2,

                                      int VAR_3)

{

    SUNRASTContext *s = VAR_0->priv_data;

    const uint8_t *VAR_4;

    int VAR_8, VAR_6, VAR_7;



    if (s->maplength) {     

        PutByteContext pb_r, pb_g;

        int VAR_8 = s->maplength / 3;



        pb_r = s->p;

        bytestream2_skip_p(&s->p, VAR_8);

        pb_g = s->p;

        bytestream2_skip_p(&s->p, VAR_8);



        for (VAR_7 = 0; VAR_7 < VAR_8; VAR_7++) {

            uint32_t pixel = VAR_2[VAR_7];



            bytestream2_put_byteu(&pb_r, (pixel >> 16) & 0xFF);

            bytestream2_put_byteu(&pb_g, (pixel >> 8)  & 0xFF);

            bytestream2_put_byteu(&s->p,  pixel        & 0xFF);

        }

    }



    VAR_8  = (s->depth * VAR_0->width + 7) >> 3;

    VAR_6 = VAR_8 + (VAR_8 & 1);

    VAR_4  = VAR_1;



     if (s->type == RT_BYTE_ENCODED) {

        uint8_t value, value2;

        int VAR_8;

        const uint8_t *VAR_9 = VAR_1 + VAR_0->height * VAR_3;



        VAR_4 = VAR_1;



#define GET_VALUE VAR_4 >= VAR_9 ? 0 : VAR_7 >= VAR_8 ? VAR_4[VAR_8-1] : VAR_4[VAR_7]



        VAR_7 = 0;

        value2 = GET_VALUE;

        while (VAR_4 < VAR_9) {

            VAR_8 = 1;

            value = value2;

            VAR_7++;

            if (VAR_7 >= VAR_6) {

                VAR_7 = 0;

                VAR_4 += VAR_3;

            }



            value2 = GET_VALUE;

            while (value2 == value && VAR_8 < 256 && VAR_4 < VAR_9) {

                VAR_7++;

                VAR_8++;

                if (VAR_7 >= VAR_6) {

                    VAR_7 = 0;

                    VAR_4 += VAR_3;

                }

                value2 = GET_VALUE;

            }



            if (VAR_8 > 2 || value == RLE_TRIGGER) {

                bytestream2_put_byteu(&s->p, RLE_TRIGGER);

                bytestream2_put_byteu(&s->p, VAR_8 - 1);

                if (VAR_8 > 1)

                    bytestream2_put_byteu(&s->p, value);

            } else if (VAR_8 == 1) {

                bytestream2_put_byteu(&s->p, value);

            } else

                bytestream2_put_be16u(&s->p, (value << 8) | value);

        }



        

        s->length = bytestream2_tell_p(&s->p) - 32 - s->maplength;

    } else {

        int VAR_10;

        for (VAR_10 = 0; VAR_10 < VAR_0->height; VAR_10++) {

            bytestream2_put_buffer(&s->p, VAR_4, VAR_8);

            if (VAR_8 < VAR_6)

                bytestream2_put_byteu(&s->p, 0);

            VAR_4 += VAR_3;

        }

    }

}

static void FUNC_0(AVCodecContext *VAR_0,
const uint8_t *VAR_1,
const uint32_t *VAR_2,
int VAR_3)
{ SUNRASTContext *s = VAR_0->priv_data; const uint8_t *VAR_4; int VAR_8, VAR_6, VAR_7; if (s->maplength) { PutByteContext pb_r, pb_g; int VAR_8 = s->maplength / 3; pb_r = s->p; bytestream2_skip_p(&s->p, VAR_8); pb_g = s->p; bytestream2_skip_p(&s->p, VAR_8); for (VAR_7 = 0; VAR_7 < VAR_8; VAR_7++) { uint32_t pixel = VAR_2[VAR_7]; bytestream2_put_byteu(&pb_r, (pixel >> 16) & 0xFF); bytestream2_put_byteu(&pb_g, (pixel >> 8)  & 0xFF); bytestream2_put_byteu(&s->p,  pixel        & 0xFF); } } VAR_8  = (s->depth * VAR_0->width + 7) >> 3; VAR_6 = VAR_8 + (VAR_8 & 1); VAR_4  = VAR_1; if (s->type == RT_BYTE_ENCODED) { uint8_t value, value2; int VAR_8; const uint8_t *VAR_9 = VAR_1 + VAR_0->height * VAR_3; VAR_4 = VAR_1; #define GET_VALUE VAR_4 >= VAR_9 ? 0 : VAR_7 >= VAR_8 ? VAR_4[VAR_8-1] : VAR_4[VAR_7]
VAR_7 = 0; value2 = GET_VALUE; while (VAR_4 < VAR_9) { VAR_8 = 1; value = value2; VAR_7++; if (VAR_7 >= VAR_6) { VAR_7 = 0; VAR_4 += VAR_3; } value2 = GET_VALUE; while (value2 == value && VAR_8 < 256 && VAR_4 < VAR_9) { VAR_7++; VAR_8++; if (VAR_7 >= VAR_6) { VAR_7 = 0; VAR_4 += VAR_3; } value2 = GET_VALUE; } if (VAR_8 > 2 || value == RLE_TRIGGER) { bytestream2_put_byteu(&s->p, RLE_TRIGGER); bytestream2_put_byteu(&s->p, VAR_8 - 1); if (VAR_8 > 1)
bytestream2_put_byteu(&s->p, value); } else if (VAR_8 == 1) { bytestream2_put_byteu(&s->p, value); } else bytestream2_put_be16u(&s->p, (value << 8) | value); } s->length = bytestream2_tell_p(&s->p) - 32 - s->maplength; } else { int VAR_10; for (VAR_10 = 0; VAR_10 < VAR_0->height; VAR_10++) { bytestream2_put_buffer(&s->p, VAR_4, VAR_8); if (VAR_8 < VAR_6)
bytestream2_put_byteu(&s->p, 0); VAR_4 += VAR_3; } } }