summaryrefslogtreecommitdiff
path: root/base/spwgd.c
blob: 48ea90965b56d9f1fe6c7e8890d903472341f6f4 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* Copyright (C) 2017-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/


/* PWGDecode filter */
#include "stdio_.h"		/* includes std.h */
#include "memory_.h"
#include "strimpl.h"
#include "spwgx.h"

/* ------ RunLengthDecode ------ */

private_st_PWGD_state();

/* Set defaults */
static void
s_PWGD_set_defaults(stream_state * st)
{
    stream_PWGD_state *const ss = (stream_PWGD_state *) st;

    (ss)->bpp = PWG_default_bpp;
    (ss)->width = PWG_default_width;
}

/* Initialize */
static int
s_PWGD_init(stream_state * st)
{
    stream_PWGD_state *const ss = (stream_PWGD_state *) st;

    (ss)->line_pos = 0;
    (ss)->line_rep = 0;
    (ss)->state = 0;
    (ss)->line_buffer = NULL;

    return 0;
}

/* Refill the buffer */
static int
s_PWGD_process(stream_state * st, stream_cursor_read * pr,
               stream_cursor_write * pw, bool last)
{
    stream_PWGD_state *const ss = (stream_PWGD_state *) st;
    register const byte *p = pr->ptr;
    register byte *q = pw->ptr;
    const byte *rlimit = pr->limit;
    byte *wlimit = pw->limit;
    int bpp = (ss->bpp+7)>>3;
    int wb = ss->width * bpp;
    int line_pos = ss->line_pos;

    if (ss->line_buffer == NULL) {
        ss->line_buffer =
                    gs_alloc_bytes_immovable(gs_memory_stable(ss->memory),
                                             wb,
                                             "s_PWGD_process(line_buffer)");
        if (ss->line_buffer == NULL)
            return ERRC;
    }

    while (1) {
        if (ss->state == 0) {
            /* Copy any buffered data out */
            if (ss->line_rep > 0) {
                int avail = wb - line_pos;
                if (avail > wlimit - q)
                    avail = wlimit - q;
                if (avail != 0)
                    memcpy(q+1, &ss->line_buffer[line_pos], avail);
                line_pos += avail;
                q += avail;
                if (line_pos == wb) {
                    line_pos = 0;
                    ss->line_rep--;
                }
                goto data_produced;
            }
            /* Now unpack data into the line buffer */
            /* Awaiting line repeat value */
            if (p == rlimit)
                goto need_data;
            ss->line_rep = (*++p) + 1;
            ss->state = 1; /* Wait for pixel repeat */
        }
        if (ss->state == 1) {
            int rep;
            /* Awaiting pixel repeat value */
            if (p == rlimit)
                goto need_data;
            rep = *++p;
            if (rep < 0x80) {
                /* Repeat the next pixel multiple times */
                ss->state = (rep+1) * bpp + 1;
                if (line_pos + ss->state - 1 > wb)
                    /* Too many repeats for this line! */
                    goto error;
            } else {
                /* Copy colors */
                ss->state = -(257 - rep) * bpp;
                if (line_pos + -ss->state > wb)
                    /* Too many pixels for this line! */
                    goto error;
            }
        }
        if (ss->state > 1) {
            /* Repeating a single pixel */
            int pixel_pos = line_pos % bpp;
            int avail = bpp - pixel_pos;
            if (avail > rlimit - p)
                avail = rlimit - p;
            if (avail != 0)
                memcpy(&ss->line_buffer[line_pos], p+1, avail);
            p += avail;
            line_pos += avail;
            pixel_pos += avail;
            ss->state -= avail;
            if (pixel_pos != bpp)
                goto need_data;
            while (ss->state > 1) {
                memcpy(&ss->line_buffer[line_pos], &ss->line_buffer[line_pos - bpp], bpp);
                line_pos += bpp;
                ss->state -= bpp;
            }
            if (line_pos == wb) {
                line_pos = 0;
                ss->state = 0;
            } else
                ss->state = 1;
        }
        if (ss->state < 0) {
            /* Copying literals */
            int avail = -ss->state;
            if (avail > rlimit - p)
                avail = rlimit - p;
            memcpy(&ss->line_buffer[line_pos], p + 1, avail);
            p += avail;
            ss->state += avail;
            line_pos += avail;
            if (ss->state)
                goto need_data;
            ss->state = 1;
        }
    }
need_data:
    {
        int status = 0; /* Need input data */
        if (0) {
error:
            status = ERRC;
        } else if (0) {
data_produced:
            status = 1; /* Need output space */
        }
        pr->ptr = p;
        pw->ptr = q;
        ss->line_pos = line_pos;
        return status;
    }
}

static void
s_PWGD_release(stream_state * st)
{
    stream_PWGD_state *const ss = (stream_PWGD_state *) st;

    gs_free_object(st->memory, ss->line_buffer, "PWGD(close)");
    ss->line_buffer = NULL;
}

/* Stream template */
const stream_template s_PWGD_template = {
    &st_PWGD_state, s_PWGD_init, s_PWGD_process, 1, 1, s_PWGD_release,
    s_PWGD_set_defaults
};