summaryrefslogtreecommitdiff
path: root/gs/src/siinterp.c
blob: aae78b139d3bc6f00f6abb33da65a9e1a698c9dc (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* Copyright (C) 2001-2006 artofcode LLC.
   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 that
   license.  Refer to licensing information at http://www.artifex.com/
   or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
   San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
*/

/* $Id$ */
/* Image interpolation filter */
#include "memory_.h"
#include "gxfixed.h"		/* for gxdda.h */
#include "gxdda.h"
#include "gxfrac.h"
#include "strimpl.h"
#include "siinterp.h"

/* ImageInterpolateEncode state */
typedef struct gx_dda_int_s {
    dda_state_struct(ia_, int, uint) state;
    dda_step_struct(ie_, int, uint) step;
} gx_dda_int_t;
typedef enum {
    SCALE_SAME = 0,
    SCALE_SAME_ALIGNED,
    SCALE_8_8,
    SCALE_8_8_ALIGNED,
    SCALE_8_16_BYTE2FRAC,
    SCALE_8_16_BYTE2FRAC_ALIGNED,
    SCALE_8_16_BYTE2FRAC_3,
    SCALE_8_16_BYTE2FRAC_3_ALIGNED,
    SCALE_8_16_GENERAL,
    SCALE_8_16_GENERAL_ALIGNED,
    SCALE_16_8,
    SCALE_16_8_ALIGNED,
    SCALE_16_16,
    SCALE_16_16_ALIGNED
} scale_case_t;
typedef struct stream_IIEncode_state_s {
    /* The client sets the params values before initialization. */
    stream_image_scale_state_common;  /* = state_common + params */
    /* The init procedure sets the following. */
    int sizeofPixelIn;		/* bytes per input pixel, 1 or 2 * Colors */
    int sizeofPixelOut;		/* bytes per output pixel, 1 or 2 * Colors */
    uint src_size;		/* bytes per row of input */
    uint dst_size;		/* bytes per row of output */
    void /*PixelOut */ *prev;	/* previous row of input data in output fmt, */
				/* [WidthIn * sizeofPixelOut] */
    void /*PixelOut */ *cur;	/* current row of input data in output fmt, */
				/* [WidthIn * sizeofPixelOut] */
    scale_case_t scale_case;
    /* The following are updated dynamically. */
    int dst_x;
    gx_dda_int_t dda_x;		/* DDA for dest X in current scan line */
    gx_dda_int_t dda_x_init;	/* initial setting of dda_x */
    int src_y, dst_y;
    gx_dda_int_t dda_y;		/* DDA for dest Y */
    int src_offset, dst_offset;
} stream_IIEncode_state;

gs_private_st_ptrs2(st_IIEncode_state, stream_IIEncode_state,
    "ImageInterpolateEncode state",
    iiencode_state_enum_ptrs, iiencode_state_reloc_ptrs,
    prev, cur);

/* Forward references */
private void s_IIEncode_release(stream_state * st);

/* Initialize the filter. */
private int
s_IIEncode_init(stream_state * st)
{
    stream_IIEncode_state *const ss = (stream_IIEncode_state *) st;
    gs_memory_t *mem = ss->memory;

    ss->sizeofPixelIn =
	ss->params.BitsPerComponentIn / 8 * ss->params.Colors;
    ss->sizeofPixelOut =
	ss->params.BitsPerComponentOut / 8 * ss->params.Colors;
    ss->src_size = ss->sizeofPixelIn * ss->params.WidthIn;
    ss->dst_size = ss->sizeofPixelOut * ss->params.WidthOut;

    /* Initialize destination DDAs. */
    ss->dst_x = 0;
    ss->src_offset = ss->dst_offset = 0;
    dda_init(ss->dda_x, 0, ss->params.WidthIn, ss->params.WidthOut);
    ss->dda_x_init = ss->dda_x;
    ss->src_y = ss->dst_y = 0;
    dda_init(ss->dda_y, 0, ss->params.HeightOut, ss->params.HeightIn);

    /* Allocate buffers for 2 rows of input data. */
    ss->prev = gs_alloc_byte_array(mem, ss->params.WidthIn,
				   ss->sizeofPixelOut, "IIEncode prev");
    ss->cur = gs_alloc_byte_array(mem, ss->params.WidthIn,
				  ss->sizeofPixelOut, "IIEncode cur");
    if (ss->prev == 0 || ss->cur == 0) {
	s_IIEncode_release(st);
	return ERRC;	/****** WRONG ******/
    }

    /* Determine the case for the inner loop. */
    ss->scale_case =
	(ss->params.BitsPerComponentIn == 8 ?
	 (ss->params.BitsPerComponentOut == 8 ?
	  (ss->params.MaxValueIn == ss->params.MaxValueOut ?
	   SCALE_SAME : SCALE_8_8) :
	  (ss->params.MaxValueIn == 255 && ss->params.MaxValueOut == frac_1 ?
	   (ss->params.Colors == 3 ? SCALE_8_16_BYTE2FRAC_3 :
	    SCALE_8_16_BYTE2FRAC) :
	   SCALE_8_16_GENERAL)) :
	 (ss->params.BitsPerComponentOut == 8 ? SCALE_16_8 :
	  ss->params.MaxValueIn == ss->params.MaxValueOut ?
	  SCALE_SAME : SCALE_16_16));

    return 0;
}

/* Process a buffer. */
private int
s_IIEncode_process(stream_state * st, stream_cursor_read * pr,
		   stream_cursor_write * pw, bool last)
{
    stream_IIEncode_state *const ss = (stream_IIEncode_state *) st;
    const scale_case_t scale_case = ss->scale_case +
	ALIGNMENT_MOD(pw->ptr, 2); /* ptr odd => buffer is aligned */
    byte *out = pw->ptr + 1;
    /****** WRONG, requires an entire output pixel ******/
    byte *limit = pw->limit + 1 - ss->sizeofPixelOut;

    /* Check whether we need to deliver any output. */

top:
    if (dda_current(ss->dda_y) > ss->dst_y) {
	/* Deliver some or all of the current scaled row. */
	while (ss->dst_x < ss->params.WidthOut) {
	    uint sx = dda_current(ss->dda_x) * ss->sizeofPixelIn;
	    const byte *in = (const byte *)ss->cur + sx;
	    int c;

	    if (out > limit) {
		pw->ptr = out - 1;
		return 1;
	    }
	    switch (scale_case) {
	    case SCALE_SAME:
	    case SCALE_SAME_ALIGNED:
		memcpy(out, in, ss->sizeofPixelIn);
		out += ss->sizeofPixelIn;
		break;
	    case SCALE_8_8:
	    case SCALE_8_8_ALIGNED:
		for (c = ss->params.Colors; --c >= 0; ++in, ++out)
		    *out = (byte)(*in * ss->params.MaxValueOut /
				  ss->params.MaxValueIn);
		break;
	    case SCALE_8_16_BYTE2FRAC:
	    case SCALE_8_16_BYTE2FRAC_ALIGNED: /* could be optimized */
	    case SCALE_8_16_BYTE2FRAC_3: /* could be optimized */
		for (c = ss->params.Colors; --c >= 0; ++in, out += 2) {
		    uint b = *in;
		    uint value = byte2frac(b);

		    out[0] = (byte)(value >> 8), out[1] = (byte)value;
		}
		break;
	    case SCALE_8_16_BYTE2FRAC_3_ALIGNED:
		{
		    uint b = in[0];

		    ((bits16 *)out)[0] = byte2frac(b);
		    b = in[1];
		    ((bits16 *)out)[1] = byte2frac(b);
		    b = in[2];
		    ((bits16 *)out)[2] = byte2frac(b);
		}
		out += 6;
		break;
	    case SCALE_8_16_GENERAL:
	    case SCALE_8_16_GENERAL_ALIGNED: /* could be optimized */
		for (c = ss->params.Colors; --c >= 0; ++in, out += 2) {
		    uint value = *in * ss->params.MaxValueOut /
			ss->params.MaxValueIn;

		    out[0] = (byte)(value >> 8), out[1] = (byte)value;
		}
		break;
	    case SCALE_16_8:
	    case SCALE_16_8_ALIGNED:
		for (c = ss->params.Colors; --c >= 0; in += 2, ++out)
		    *out = (byte)(*(const bits16 *)in *
				  ss->params.MaxValueOut /
				  ss->params.MaxValueIn);
		break;
	    case SCALE_16_16:
	    case SCALE_16_16_ALIGNED: /* could be optimized */
		for (c = ss->params.Colors; --c >= 0; in += 2, out += 2) {
		    uint value = *(const bits16 *)in *
			ss->params.MaxValueOut / ss->params.MaxValueIn;

		    out[0] = (byte)(value >> 8), out[1] = (byte)value;
		}
	    }
	    dda_next(ss->dda_x);
	    ss->dst_x++;
	}
	ss->dst_x = 0;
	ss->dst_y++;
	ss->dda_x = ss->dda_x_init;
	goto top;
    }
    pw->ptr = out - 1;
    if (ss->dst_y >= ss->params.HeightOut)
	return EOFC;

    if (ss->src_offset < ss->src_size) {
	uint count = min(ss->src_size - ss->src_offset, pr->limit - pr->ptr);

	if (count == 0)
	    return 0;
	memcpy((byte *)ss->cur + ss->src_offset, pr->ptr + 1, count);
	ss->src_offset += count;
	pr->ptr += count;
	if (ss->src_offset < ss->src_size)
	    return 0;
    }
    ss->src_offset = 0;
    ss->dst_x = 0;
    ss->dda_x = ss->dda_x_init;
    dda_next(ss->dda_y);
    goto top;
}

/* Release the filter's storage. */
private void
s_IIEncode_release(stream_state * st)
{
    stream_IIEncode_state *const ss = (stream_IIEncode_state *) st;
    gs_memory_t *mem = ss->memory;

    gs_free_object(mem, ss->cur, "IIEncode cur");
    ss->cur = 0;
    gs_free_object(mem, ss->prev, "IIEncode prev");
    ss->prev = 0;
}

/* Stream template */
const stream_template s_IIEncode_template = {
    &st_IIEncode_state, s_IIEncode_init, s_IIEncode_process, 1, 1,
    s_IIEncode_release
};