Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Imaging/libImaging/MspDecode.c
blob: af2b842461b81c36ce72110a1a700f840d38cbd3 (plain)
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
/*
 * The Python Imaging Library.
 * $Id: MspDecode.c 2134 2004-10-06 08:55:20Z fredrik $
 *
 * decoder for MSP version 2 data.
 *
 * history:
 *	97-01-03 fl	Created
 *
 * Copyright (c) Fredrik Lundh 1997.
 * Copyright (c) Secret Labs AB 1997.
 *
 * See the README file for information on usage and redistribution.
 */


#include "Imaging.h"


int
ImagingMspDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
{
    int n;
    UINT8* ptr;

    ptr = buf;

    for (;;) {

	if (bytes < 1)
	    return ptr - buf;

	if (ptr[0] == 0) {

	    /* Run (3 bytes block) */
	    if (bytes < 3)
		break;

	    n = ptr[1];

	    if (state->x + n > state->bytes) {
		state->errcode = IMAGING_CODEC_OVERRUN;
		return -1;
	    }

	    memset(state->buffer + state->x, ptr[2], n);

	    ptr += 3;
	    bytes -= 3;

	} else {

	    /* Literal (1+n bytes block) */
	    n = ptr[0];

	    if (bytes < 1 + n)
		break;

	    if (state->x + n > state->bytes) {
		state->errcode = IMAGING_CODEC_OVERRUN;
		return -1;
	    }

	    memcpy(state->buffer + state->x, ptr + 1, n);

	    ptr += 1 + n;
	    bytes -= 1 + n;

	}

	state->x += n;

	if (state->x >= state->bytes) {

	    /* Got a full line, unpack it */
	    state->shuffle((UINT8*) im->image[state->y + state->yoff] +
			   state->xoff * im->pixelsize, state->buffer,
			   state->xsize);

	    state->x = 0;

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

    }

    return ptr - buf;
}