aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Pillow/py3/libImaging/EpsEncode.c
blob: 3f2cb33b2d20769555f5859dd4891c8f25a78a08 (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
/*
 * The Python Imaging Library.
 * $Id$
 *
 * encoder for EPS hex data
 *
 * history:
 * 96-04-19 fl created
 * 96-06-27 fl don't drop last block of encoded data
 *
 * notes:
 * FIXME: rename to HexEncode.c ??
 *
 * Copyright (c) Fredrik Lundh 1996.
 * Copyright (c) Secret Labs AB 1997.
 *
 * See the README file for information on usage and redistribution.
 */

#include "Imaging.h"

int
ImagingEpsEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) {
    enum { HEXBYTE = 1, NEWLINE };
    const char *hex = "0123456789abcdef";

    UINT8 *ptr = buf;
    UINT8 *in, i;

    if (!state->state) {
        state->state = HEXBYTE;
        state->xsize *= im->pixelsize; /* Hack! */
    }

    in = (UINT8 *)im->image[state->y];

    for (;;) {
        if (state->state == NEWLINE) {
            if (bytes < 1) {
                break;
            }
            *ptr++ = '\n';
            bytes--;
            state->state = HEXBYTE;
        }

        if (bytes < 2) {
            break;
        }

        i = in[state->x++];
        *ptr++ = hex[(i >> 4) & 15];
        *ptr++ = hex[i & 15];
        bytes -= 2;

        /* Skip junk bytes */
        if (im->bands == 3 && (state->x & 3) == 3) {
            state->x++;
        }

        if (++state->count >= 79 / 2) {
            state->state = NEWLINE;
            state->count = 0;
        }

        if (state->x >= state->xsize) {
            state->x = 0;
            if (++state->y >= state->ysize) {
                state->errcode = IMAGING_CODEC_END;
                break;
            }
            in = (UINT8 *)im->image[state->y];
        }
    }

    return ptr - buf;
}