static int read_uncompressed_sgi(unsigned char* out_buf, uint8_t* out_end,

                const uint8_t *in_buf, const uint8_t *in_end, SgiState* s)

{

    int x, y, z;

    const uint8_t *ptr;

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



    /* Test buffer size. */

    if (offset * s->depth > in_end - in_buf) {

       return -1;

    }



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

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

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

            ptr = in_buf += s->bytes_per_channel;

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

                memcpy(out_end, ptr, s->bytes_per_channel);

                out_end += s->bytes_per_channel;

                ptr += offset;

            }

        }

    }

    return 0;

}

static int read_uncompressed_sgi(unsigned char* out_buf, uint8_t* out_end,

                const uint8_t *in_buf, const uint8_t *in_end, SgiState* s)

{

    int x, y, z;

    const uint8_t *ptr;

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



    

    if (offset * s->depth > in_end - in_buf) {

       return -1;

    }



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

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

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

            ptr = in_buf += s->bytes_per_channel;

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

                memcpy(out_end, ptr, s->bytes_per_channel);

                out_end += s->bytes_per_channel;

                ptr += offset;

            }

        }

    }

    return 0;

}

                const uint8_t *in_buf, const uint8_t *in_end, SgiState* s)     const uint8_t *ptr;     if (offset * s->depth > in_end - in_buf) {        return -1;         for (x = s->width; x > 0; x--) {             ptr = in_buf += s->bytes_per_channel;             for(z = 0; z < s->depth; z ++) {                 memcpy(out_end, ptr, s->bytes_per_channel);                 out_end += s->bytes_per_channel;                 ptr += offset;
static int FUNC_0(unsigned char* VAR_0, uint8_t* VAR_1,

                const uint8_t *VAR_2, const uint8_t *VAR_3, SgiState* VAR_4)

{

    int VAR_5, VAR_6, VAR_7;

    const uint8_t *VAR_8;

    unsigned int VAR_9 = VAR_4->height * VAR_4->width * VAR_4->bytes_per_channel;



    

    if (VAR_9 * VAR_4->depth > VAR_3 - VAR_2) {

       return -1;

    }



    for (VAR_6 = VAR_4->height - 1; VAR_6 >= 0; VAR_6--) {

        VAR_1 = VAR_0 + (VAR_6 * VAR_4->linesize);

        for (VAR_5 = VAR_4->width; VAR_5 > 0; VAR_5--) {

            VAR_8 = VAR_2 += VAR_4->bytes_per_channel;

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

                memcpy(VAR_1, VAR_8, VAR_4->bytes_per_channel);

                VAR_1 += VAR_4->bytes_per_channel;

                VAR_8 += VAR_9;

            }

        }

    }

    return 0;

}

static int FUNC_0(unsigned char* VAR_0, uint8_t* VAR_1,
const uint8_t *VAR_2, const uint8_t *VAR_3, SgiState* VAR_4)
{ int VAR_5, VAR_6, VAR_7; const uint8_t *VAR_8; unsigned int VAR_9 = VAR_4->height * VAR_4->width * VAR_4->bytes_per_channel; if (VAR_9 * VAR_4->depth > VAR_3 - VAR_2) { return -1; } for (VAR_6 = VAR_4->height - 1; VAR_6 >= 0; VAR_6--) { VAR_1 = VAR_0 + (VAR_6 * VAR_4->linesize); for (VAR_5 = VAR_4->width; VAR_5 > 0; VAR_5--) { VAR_8 = VAR_2 += VAR_4->bytes_per_channel; for(VAR_7 = 0; VAR_7 < VAR_4->depth; VAR_7 ++) { memcpy(VAR_1, VAR_8, VAR_4->bytes_per_channel); VAR_1 += VAR_4->bytes_per_channel; VAR_8 += VAR_9; } } } return 0; }