aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Pillow/py2/libImaging/RawDecode.c
blob: c069bdb8864cc6cdf7c997c6e11f9c88c1379566 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 * The Python Imaging Library.
 * $Id$
 *
 * decoder for raw (uncompressed) image data
 *
 * history:
 *	96-03-07 fl	rewritten
 *
 * Copyright (c) Fredrik Lundh 1996.
 * Copyright (c) Secret Labs AB 1997.
 *
 * See the README file for information on usage and redistribution.
 */


#include "Imaging.h"

#include "Raw.h"


int
ImagingRawDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t bytes)
{
    enum { LINE = 1, SKIP };
    RAWSTATE* rawstate = state->context;

    UINT8* ptr;

    if (state->state == 0) {

	/* Initialize context variables */

	/* get size of image data and padding */
	state->bytes = (state->xsize * state->bits + 7) / 8;
	if (rawstate->stride) {
	    rawstate->skip = rawstate->stride - state->bytes;
	    if (rawstate->skip < 0) {
	        state->errcode = IMAGING_CODEC_CONFIG;
	        return -1;
	    }
	} else {
	    rawstate->skip = 0;
	}

	/* check image orientation */
	if (state->ystep < 0) {
	    state->y = state->ysize-1;
	    state->ystep = -1;
	} else
	    state->ystep = 1;

	state->state = LINE;

    }

    ptr = buf;

    for (;;) {

	if (state->state == SKIP) {

	    /* Skip padding between lines */

	    if (bytes < rawstate->skip)
		return ptr - buf;

	    ptr += rawstate->skip;
	    bytes -= rawstate->skip;

	    state->state = LINE;

	}

	if (bytes < state->bytes)
	    return ptr - buf;

	/* Unpack data */
	state->shuffle((UINT8*) im->image[state->y + state->yoff] +
		       state->xoff * im->pixelsize, ptr, state->xsize);

	ptr += state->bytes;
	bytes -= state->bytes;

	state->y += state->ystep;

	if (state->y < 0 || state->y >= state->ysize) {
	    /* End of file (errcode = 0) */
	    return -1;
	}

	state->state = SKIP;

    }

}