static int read_uncompressed_sgi(unsigned char* out_buf, SgiState *s)

{

    int x, y, z;

    unsigned int offset = s->height * s->width * s->bytes_per_channel;

    GetByteContext gp[4];

    uint8_t *out_end;



    /* Test buffer size. */

    if (offset * s->depth > bytestream2_get_bytes_left(&s->g))

        return AVERROR_INVALIDDATA;



    /* Create a reader for each plane */

    for (z = 0; z < s->depth; z++) {

        gp[z] = s->g;

        bytestream2_skip(&gp[z], z * offset);

    }



    for (y = s->height - 1; y >= 0; y--) {

        out_end = out_buf + (y * s->linesize);

        if (s->bytes_per_channel == 1) {

            for (x = s->width; x > 0; x--) {

                bytestream2_get_bufferu(&gp[z], out_end, s->depth);

                out_end += s->depth;

            }

        } else {

            uint16_t *out16 = (uint16_t *)out_end;

            for (x = s->width; x > 0; x--)

                for (z = 0; z < s->depth; z++)

                    *out16++ = bytestream2_get_ne16u(&gp[z]);

        }

    }

    return 0;

}

static int read_uncompressed_sgi(unsigned char* out_buf, SgiState *s)

{

    int x, y, z;

    unsigned int offset = s->height * s->width * s->bytes_per_channel;

    GetByteContext gp[4];

    uint8_t *out_end;



    

    if (offset * s->depth > bytestream2_get_bytes_left(&s->g))

        return AVERROR_INVALIDDATA;



    

    for (z = 0; z < s->depth; z++) {

        gp[z] = s->g;

        bytestream2_skip(&gp[z], z * offset);

    }



    for (y = s->height - 1; y >= 0; y--) {

        out_end = out_buf + (y * s->linesize);

        if (s->bytes_per_channel == 1) {

            for (x = s->width; x > 0; x--) {

                bytestream2_get_bufferu(&gp[z], out_end, s->depth);

                out_end += s->depth;

            }

        } else {

            uint16_t *out16 = (uint16_t *)out_end;

            for (x = s->width; x > 0; x--)

                for (z = 0; z < s->depth; z++)

                    *out16++ = bytestream2_get_ne16u(&gp[z]);

        }

    }

    return 0;

}

            for (x = s->width; x > 0; x--) {                 bytestream2_get_bufferu(&gp[z], out_end, s->depth);                 out_end += s->depth;
static int FUNC_0(unsigned char* VAR_0, SgiState *VAR_1)

{

    int VAR_2, VAR_3, VAR_4;

    unsigned int VAR_5 = VAR_1->height * VAR_1->width * VAR_1->bytes_per_channel;

    GetByteContext gp[4];

    uint8_t *out_end;



    

    if (VAR_5 * VAR_1->depth > bytestream2_get_bytes_left(&VAR_1->g))

        return AVERROR_INVALIDDATA;



    

    for (VAR_4 = 0; VAR_4 < VAR_1->depth; VAR_4++) {

        gp[VAR_4] = VAR_1->g;

        bytestream2_skip(&gp[VAR_4], VAR_4 * VAR_5);

    }



    for (VAR_3 = VAR_1->height - 1; VAR_3 >= 0; VAR_3--) {

        out_end = VAR_0 + (VAR_3 * VAR_1->linesize);

        if (VAR_1->bytes_per_channel == 1) {

            for (VAR_2 = VAR_1->width; VAR_2 > 0; VAR_2--) {

                bytestream2_get_bufferu(&gp[VAR_4], out_end, VAR_1->depth);

                out_end += VAR_1->depth;

            }

        } else {

            uint16_t *out16 = (uint16_t *)out_end;

            for (VAR_2 = VAR_1->width; VAR_2 > 0; VAR_2--)

                for (VAR_4 = 0; VAR_4 < VAR_1->depth; VAR_4++)

                    *out16++ = bytestream2_get_ne16u(&gp[VAR_4]);

        }

    }

    return 0;

}

static int FUNC_0(unsigned char* VAR_0, SgiState *VAR_1)
{ int VAR_2, VAR_3, VAR_4; unsigned int VAR_5 = VAR_1->height * VAR_1->width * VAR_1->bytes_per_channel; GetByteContext gp[4]; uint8_t *out_end; if (VAR_5 * VAR_1->depth > bytestream2_get_bytes_left(&VAR_1->g))
return AVERROR_INVALIDDATA; for (VAR_4 = 0; VAR_4 < VAR_1->depth; VAR_4++) { gp[VAR_4] = VAR_1->g; bytestream2_skip(&gp[VAR_4], VAR_4 * VAR_5); } for (VAR_3 = VAR_1->height - 1; VAR_3 >= 0; VAR_3--) { out_end = VAR_0 + (VAR_3 * VAR_1->linesize); if (VAR_1->bytes_per_channel == 1) { for (VAR_2 = VAR_1->width; VAR_2 > 0; VAR_2--) { bytestream2_get_bufferu(&gp[VAR_4], out_end, VAR_1->depth); out_end += VAR_1->depth; } } else { uint16_t *out16 = (uint16_t *)out_end; for (VAR_2 = VAR_1->width; VAR_2 > 0; VAR_2--) for (VAR_4 = 0; VAR_4 < VAR_1->depth; VAR_4++) *out16++ = bytestream2_get_ne16u(&gp[VAR_4]); } } return 0; }