~ubuntu-branches/ubuntu/jaunty/ghostscript/jaunty-updates

« back to all changes in this revision

Viewing changes to base/siinterp.c

  • Committer: Bazaar Package Importer
  • Author(s): Till Kamppeter
  • Date: 2009-01-20 16:40:45 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20090120164045-lnfhi0n30o5lwhwa
Tags: 8.64.dfsg.1~svn9377-0ubuntu1
* New upstream release (SVN rev 9377)
   o Fixes many bugs concerning PDF rendering, to make the PDF printing
     workflow correctly working.
   o Fixes long-standing bugs in many drivers, like input paper tray and
     duplex options not working for the built-in PCL 4, 5, 5c, 5e, and
     6/XL drivers, PDF input not working for bjc600, bjc800, and cups
     output devices, several options not working and uninitialized
     memory with cups output device.
   o Merged nearly all patches of the Ubuntu and Debian packages upstream.
   o Fixes LP: #317810, LP: #314439, LP: #314018.
* debian/patches/03_libpaper_support.dpatch,
  debian/patches/11_gs-cjk_font_glyph_handling_fix.dpatch,
  debian/patches/12_gs-cjk_vertical_writing_metrics_fix.dpatch,
  debian/patches/13_gs-cjk_cjkps_examples.dpatch,
  debian/patches/20_bbox_segv_fix.dpatch,
  debian/patches/21_brother_7x0_gdi_fix.dpatch,
  debian/patches/22_epsn_margin_workaround.dpatch,
  debian/patches/24_gs_man_fix.dpatch,
  debian/patches/25_toolbin_insecure_tmp_usage_fix.dpatch,
  debian/patches/26_assorted_script_fixes.dpatch,
  debian/patches/29_gs_css_fix.dpatch,
  debian/patches/30_ps2pdf_man_improvement.dpatch,
  debian/patches/31_fix-gc-sigbus.dpatch,
  debian/patches/34_ftbfs-on-hurd-fix.dpatch,
  debian/patches/35_disable_libcairo.dpatch,
  debian/patches/38_pxl-duplex.dpatch,
  debian/patches/39_pxl-resolution.dpatch,
  debian/patches/42_gs-init-ps-delaybind-fix.dpatch,
  debian/patches/45_bjc600-bjc800-pdf-input.dpatch,
  debian/patches/48_cups-output-device-pdf-duplex-uninitialized-memory-fix.dpatch,
  debian/patches/50_lips4-floating-point-exception.dpatch,
  debian/patches/52_cups-device-logging.dpatch,
  debian/patches/55_pcl-input-slot-fix.dpatch,
  debian/patches/57_pxl-input-slot-fix.dpatch,
  debian/patches/60_pxl-cups-driver-pdf.dpatch,
  debian/patches/62_onebitcmyk-pdf.dpatch,
  debian/patches/65_too-big-temp-files-1.dpatch,
  debian/patches/67_too-big-temp-files-2.dpatch,
  debian/patches/70_take-into-account-data-in-stream-buffer-before-refill.dpatch:
  Removed, applied upstream.
* debian/patches/01_docdir_fix_for_debian.dpatch,
  debian/patches/02_gs_man_fix_debian.dpatch,
  debian/patches/01_docdir-fix-for-debian.dpatch,
  debian/patches/02_docdir-fix-for-debian.dpatch: Renamed patches to
  make merging with Debian easier.
* debian/patches/32_improve-handling-of-media-size-changes-from-gv.dpatch, 
  debian/patches/33_bad-params-to-xinitimage-on-large-bitmaps.dpatch:
  regenerated for new source directory structure.
* debian/rules: Corrected paths to remove cidfmap (it is in Resource/Init/
  in GS 8.64) and to install headers (source paths are psi/ and base/ now).
* debian/rules: Remove all fontmaps, as DeFoMa replaces them.
* debian/local/pdftoraster/pdftoraster.c,
  debian/local/pdftoraster/pdftoraster.convs, debian/rules: Removed
  added pdftoraster filter and use the one which comes with Ghostscript.
* debian/ghostscript.links: s/8.63/8.64/

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2001-2006 Artifex Software, Inc.
 
2
   All Rights Reserved.
 
3
  
 
4
   This software is provided AS-IS with no warranty, either express or
 
5
   implied.
 
6
 
 
7
   This software is distributed under license and may not be copied, modified
 
8
   or distributed except as expressly authorized under the terms of that
 
9
   license.  Refer to licensing information at http://www.artifex.com/
 
10
   or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
 
11
   San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
 
12
*/
 
13
 
 
14
/* $Id: siinterp.c 8250 2007-09-25 13:31:24Z giles $ */
 
15
/* Image interpolation filter */
 
16
#include "memory_.h"
 
17
#include "gxfixed.h"            /* for gxdda.h */
 
18
#include "gxdda.h"
 
19
#include "gxfrac.h"
 
20
#include "strimpl.h"
 
21
#include "siinterp.h"
 
22
 
 
23
/* ImageInterpolateEncode state */
 
24
typedef enum {
 
25
    SCALE_SAME = 0,
 
26
    SCALE_SAME_ALIGNED,
 
27
    SCALE_8_8,
 
28
    SCALE_8_8_ALIGNED,
 
29
    SCALE_8_16_BYTE2FRAC,
 
30
    SCALE_8_16_BYTE2FRAC_ALIGNED,
 
31
    SCALE_8_16_BYTE2FRAC_3,
 
32
    SCALE_8_16_BYTE2FRAC_3_ALIGNED,
 
33
    SCALE_8_16_GENERAL,
 
34
    SCALE_8_16_GENERAL_ALIGNED,
 
35
    SCALE_16_8,
 
36
    SCALE_16_8_ALIGNED,
 
37
    SCALE_16_16,
 
38
    SCALE_16_16_ALIGNED
 
39
} scale_case_t;
 
40
typedef struct stream_IIEncode_state_s {
 
41
    /* The client sets the params values before initialization. */
 
42
    stream_image_scale_state_common;  /* = state_common + params */
 
43
    /* The init procedure sets the following. */
 
44
    int sizeofPixelIn;          /* bytes per input pixel, 1 or 2 * Colors */
 
45
    int sizeofPixelOut;         /* bytes per output pixel, 1 or 2 * Colors */
 
46
    uint src_size;              /* bytes per row of input */
 
47
    uint dst_size;              /* bytes per row of output */
 
48
    void /*PixelOut */ *prev;   /* previous row of input data in output fmt, */
 
49
                                /* [WidthIn * sizeofPixelOut] */
 
50
    void /*PixelOut */ *cur;    /* current row of input data in output fmt, */
 
51
                                /* [WidthIn * sizeofPixelOut] */
 
52
    scale_case_t scale_case;
 
53
    /* The following are updated dynamically. */
 
54
    int dst_x;
 
55
    gx_dda_int_t dda_x;         /* DDA for dest X in current scan line */
 
56
    gx_dda_int_t dda_x_init;    /* initial setting of dda_x */
 
57
    int src_y, dst_y;
 
58
    gx_dda_int_t dda_y;         /* DDA for dest Y */
 
59
    int src_offset, dst_offset;
 
60
} stream_IIEncode_state;
 
61
 
 
62
gs_private_st_ptrs2(st_IIEncode_state, stream_IIEncode_state,
 
63
    "ImageInterpolateEncode state",
 
64
    iiencode_state_enum_ptrs, iiencode_state_reloc_ptrs,
 
65
    prev, cur);
 
66
 
 
67
/* Forward references */
 
68
static void s_IIEncode_release(stream_state * st);
 
69
 
 
70
/* Initialize the filter. */
 
71
static int
 
72
s_IIEncode_init(stream_state * st)
 
73
{
 
74
    stream_IIEncode_state *const ss = (stream_IIEncode_state *) st;
 
75
    gs_memory_t *mem = ss->memory;
 
76
 
 
77
    ss->sizeofPixelIn =
 
78
        ss->params.BitsPerComponentIn / 8 * ss->params.Colors;
 
79
    ss->sizeofPixelOut =
 
80
        ss->params.BitsPerComponentOut / 8 * ss->params.Colors;
 
81
    ss->src_size = ss->sizeofPixelIn * ss->params.WidthIn;
 
82
    ss->dst_size = ss->sizeofPixelOut * ss->params.WidthOut;
 
83
 
 
84
    /* Initialize destination DDAs. */
 
85
    ss->dst_x = 0;
 
86
    ss->src_offset = ss->dst_offset = 0;
 
87
    dda_init(ss->dda_x, 0, ss->params.WidthIn, ss->params.WidthOut);
 
88
    ss->dda_x_init = ss->dda_x;
 
89
    ss->src_y = ss->dst_y = 0;
 
90
    dda_init(ss->dda_y, 0, ss->params.HeightOut, ss->params.HeightIn);
 
91
 
 
92
    /* Allocate buffers for 2 rows of input data. */
 
93
    ss->prev = gs_alloc_byte_array(mem, ss->params.WidthIn,
 
94
                                   ss->sizeofPixelOut, "IIEncode prev");
 
95
    ss->cur = gs_alloc_byte_array(mem, ss->params.WidthIn,
 
96
                                  ss->sizeofPixelOut, "IIEncode cur");
 
97
    if (ss->prev == 0 || ss->cur == 0) {
 
98
        s_IIEncode_release(st);
 
99
        return ERRC;    /****** WRONG ******/
 
100
    }
 
101
 
 
102
    /* Determine the case for the inner loop. */
 
103
    ss->scale_case =
 
104
        (ss->params.BitsPerComponentIn == 8 ?
 
105
         (ss->params.BitsPerComponentOut == 8 ?
 
106
          (ss->params.MaxValueIn == ss->params.MaxValueOut ?
 
107
           SCALE_SAME : SCALE_8_8) :
 
108
          (ss->params.MaxValueIn == 255 && ss->params.MaxValueOut == frac_1 ?
 
109
           (ss->params.Colors == 3 ? SCALE_8_16_BYTE2FRAC_3 :
 
110
            SCALE_8_16_BYTE2FRAC) :
 
111
           SCALE_8_16_GENERAL)) :
 
112
         (ss->params.BitsPerComponentOut == 8 ? SCALE_16_8 :
 
113
          ss->params.MaxValueIn == ss->params.MaxValueOut ?
 
114
          SCALE_SAME : SCALE_16_16));
 
115
 
 
116
    return 0;
 
117
}
 
118
 
 
119
/* Process a buffer. */
 
120
static int
 
121
s_IIEncode_process(stream_state * st, stream_cursor_read * pr,
 
122
                   stream_cursor_write * pw, bool last)
 
123
{
 
124
    stream_IIEncode_state *const ss = (stream_IIEncode_state *) st;
 
125
    const scale_case_t scale_case = ss->scale_case +
 
126
        ALIGNMENT_MOD(pw->ptr, 2); /* ptr odd => buffer is aligned */
 
127
    byte *out = pw->ptr + 1;
 
128
    /****** WRONG, requires an entire output pixel ******/
 
129
    byte *limit = pw->limit + 1 - ss->sizeofPixelOut;
 
130
 
 
131
    /* Check whether we need to deliver any output. */
 
132
 
 
133
top:
 
134
    if (dda_current(ss->dda_y) > ss->dst_y) {
 
135
        /* Deliver some or all of the current scaled row. */
 
136
        while (ss->dst_x < ss->params.WidthOut) {
 
137
            uint sx = dda_current(ss->dda_x) * ss->sizeofPixelIn;
 
138
            const byte *in = (const byte *)ss->cur + sx;
 
139
            int c;
 
140
 
 
141
            if (out > limit) {
 
142
                pw->ptr = out - 1;
 
143
                return 1;
 
144
            }
 
145
            switch (scale_case) {
 
146
            case SCALE_SAME:
 
147
            case SCALE_SAME_ALIGNED:
 
148
                memcpy(out, in, ss->sizeofPixelIn);
 
149
                out += ss->sizeofPixelIn;
 
150
                break;
 
151
            case SCALE_8_8:
 
152
            case SCALE_8_8_ALIGNED:
 
153
                for (c = ss->params.Colors; --c >= 0; ++in, ++out)
 
154
                    *out = (byte)(*in * ss->params.MaxValueOut /
 
155
                                  ss->params.MaxValueIn);
 
156
                break;
 
157
            case SCALE_8_16_BYTE2FRAC:
 
158
            case SCALE_8_16_BYTE2FRAC_ALIGNED: /* could be optimized */
 
159
            case SCALE_8_16_BYTE2FRAC_3: /* could be optimized */
 
160
                for (c = ss->params.Colors; --c >= 0; ++in, out += 2) {
 
161
                    uint b = *in;
 
162
                    uint value = byte2frac(b);
 
163
 
 
164
                    out[0] = (byte)(value >> 8), out[1] = (byte)value;
 
165
                }
 
166
                break;
 
167
            case SCALE_8_16_BYTE2FRAC_3_ALIGNED:
 
168
                {
 
169
                    uint b = in[0];
 
170
 
 
171
                    ((bits16 *)out)[0] = byte2frac(b);
 
172
                    b = in[1];
 
173
                    ((bits16 *)out)[1] = byte2frac(b);
 
174
                    b = in[2];
 
175
                    ((bits16 *)out)[2] = byte2frac(b);
 
176
                }
 
177
                out += 6;
 
178
                break;
 
179
            case SCALE_8_16_GENERAL:
 
180
            case SCALE_8_16_GENERAL_ALIGNED: /* could be optimized */
 
181
                for (c = ss->params.Colors; --c >= 0; ++in, out += 2) {
 
182
                    uint value = *in * ss->params.MaxValueOut /
 
183
                        ss->params.MaxValueIn;
 
184
 
 
185
                    out[0] = (byte)(value >> 8), out[1] = (byte)value;
 
186
                }
 
187
                break;
 
188
            case SCALE_16_8:
 
189
            case SCALE_16_8_ALIGNED:
 
190
                for (c = ss->params.Colors; --c >= 0; in += 2, ++out)
 
191
                    *out = (byte)(*(const bits16 *)in *
 
192
                                  ss->params.MaxValueOut /
 
193
                                  ss->params.MaxValueIn);
 
194
                break;
 
195
            case SCALE_16_16:
 
196
            case SCALE_16_16_ALIGNED: /* could be optimized */
 
197
                for (c = ss->params.Colors; --c >= 0; in += 2, out += 2) {
 
198
                    uint value = *(const bits16 *)in *
 
199
                        ss->params.MaxValueOut / ss->params.MaxValueIn;
 
200
 
 
201
                    out[0] = (byte)(value >> 8), out[1] = (byte)value;
 
202
                }
 
203
            }
 
204
            dda_next(ss->dda_x);
 
205
            ss->dst_x++;
 
206
        }
 
207
        ss->dst_x = 0;
 
208
        ss->dst_y++;
 
209
        ss->dda_x = ss->dda_x_init;
 
210
        goto top;
 
211
    }
 
212
    pw->ptr = out - 1;
 
213
    if (ss->dst_y >= ss->params.HeightOut)
 
214
        return EOFC;
 
215
 
 
216
    if (ss->src_offset < ss->src_size) {
 
217
        uint count = min(ss->src_size - ss->src_offset, pr->limit - pr->ptr);
 
218
 
 
219
        if (count == 0)
 
220
            return 0;
 
221
        memcpy((byte *)ss->cur + ss->src_offset, pr->ptr + 1, count);
 
222
        ss->src_offset += count;
 
223
        pr->ptr += count;
 
224
        if (ss->src_offset < ss->src_size)
 
225
            return 0;
 
226
    }
 
227
    ss->src_offset = 0;
 
228
    ss->dst_x = 0;
 
229
    ss->dda_x = ss->dda_x_init;
 
230
    dda_next(ss->dda_y);
 
231
    goto top;
 
232
}
 
233
 
 
234
/* Release the filter's storage. */
 
235
static void
 
236
s_IIEncode_release(stream_state * st)
 
237
{
 
238
    stream_IIEncode_state *const ss = (stream_IIEncode_state *) st;
 
239
    gs_memory_t *mem = ss->memory;
 
240
 
 
241
    gs_free_object(mem, ss->cur, "IIEncode cur");
 
242
    ss->cur = 0;
 
243
    gs_free_object(mem, ss->prev, "IIEncode prev");
 
244
    ss->prev = 0;
 
245
}
 
246
 
 
247
/* Stream template */
 
248
const stream_template s_IIEncode_template = {
 
249
    &st_IIEncode_state, s_IIEncode_init, s_IIEncode_process, 1, 1,
 
250
    s_IIEncode_release
 
251
};