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

« back to all changes in this revision

Viewing changes to base/sidscale.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: sidscale.c 8784 2008-05-28 18:16:26Z giles $ */
 
15
/* Special Image downsample scaling filters for dithered devices */
 
16
#include "math_.h"
 
17
#include "memory_.h"
 
18
#include "stdio_.h"
 
19
#include "gdebug.h"
 
20
#include "gxfixed.h"            /* for gxdda.h */
 
21
#include "gxdda.h"
 
22
#include "gxfrac.h"
 
23
#include "strimpl.h"
 
24
#include "sidscale.h"
 
25
 
 
26
/* Temporary intermediate values */
 
27
typedef byte PixelTmp;
 
28
 
 
29
#define minPixelTmp 0
 
30
#define maxPixelTmp 255
 
31
#define unitPixelTmp 255
 
32
 
 
33
/* Max of all pixel sizes */
 
34
#define maxSizeofPixel 2
 
35
 
 
36
/* Auxiliary structures. */
 
37
 
 
38
/* ImageSpecialDownScaleEncode / ImageSpecialDownScaleDecode */
 
39
typedef struct stream_ISpecialDownScale_state_s {
 
40
    /* The client sets the params values before initialization. */
 
41
    stream_image_scale_state_common;  /* = state_common + params */
 
42
    /* The init procedure sets the following. */
 
43
    int sizeofPixelIn;          /* bytes per input value, 1 or 2 */
 
44
    int sizeofPixelOut;         /* bytes per output value, 1 or 2 */
 
45
    void /*PixelIn */  *src;
 
46
    void /*PixelOut */ *dst;
 
47
    void /*PixelIn */  *tmp;
 
48
    gx_dda_int_t dda_x_init;    /* initial setting of dda_x */
 
49
    /* The following are updated dynamically. */
 
50
    int dst_x;
 
51
    uint dst_offset, dst_size;
 
52
    gx_dda_int_t dda_x;         /* DDA for dest X in current scan line */
 
53
    int src_y;
 
54
    uint src_offset, src_size;
 
55
    int dst_y;
 
56
    gx_dda_int_t dda_y;         /* DDA for dest Y */
 
57
} stream_ISpecialDownScale_state;
 
58
 
 
59
gs_private_st_ptrs3(st_ISpecialDownScale_state, stream_ISpecialDownScale_state,
 
60
    "ImageSpecialDownScaleEncode/Decode state",
 
61
    isdscale_state_enum_ptrs, isdscale_state_reloc_ptrs,
 
62
    dst, src, tmp);
 
63
 
 
64
/* Apply filter to downscale horizontally from src to tmp. */
 
65
static void
 
66
idownscale_x(void /* PixelIn */ * tmp, const void /* PixelIn */ *src, stream_ISpecialDownScale_state *const ss)
 
67
{
 
68
    int c, i;
 
69
    int Colors = ss->params.Colors;
 
70
    int WidthIn = ss->params.WidthIn;
 
71
    int prev_y = dda_previous(ss->dda_y);
 
72
    int cur_y = dda_next(ss->dda_y);
 
73
    bool firstline = prev_y != cur_y;   /* at the start of a new group of lines */
 
74
    bool polarity_additive = ss->params.ColorPolarityAdditive;
 
75
 
 
76
        /* The following could be a macro, BUT macro's with control */
 
77
        /* are not good style and hard to debug */
 
78
    if (ss->sizeofPixelIn == 1) {
 
79
        for (c = 0; c < Colors; ++c) {
 
80
            byte *tp = (byte *)tmp + c;         /* destination */
 
81
            const byte *pp =  (const byte *)src + c;
 
82
 
 
83
            ss->dda_x = ss->dda_x_init;
 
84
            if_debug1('W', "[W]idownscale_x color %d:", c);
 
85
 
 
86
            for ( i = 0; i < WidthIn; tp += Colors) {
 
87
                int endx = dda_next(ss->dda_x);
 
88
                if (firstline)
 
89
                    *tp = *pp;
 
90
                else {
 
91
                    if ((polarity_additive && (*pp < *tp)) ||
 
92
                        (!polarity_additive && (*pp > *tp)) )
 
93
                        *tp = *pp;
 
94
                }
 
95
                i++; pp += Colors;
 
96
                while (i < endx) {
 
97
                   if (*pp < *tp)
 
98
                        *tp = *pp;
 
99
                   i++; pp += Colors;
 
100
                }
 
101
                if_debug1('W', " %d", *tp);
 
102
            }
 
103
            if_debug0('W', "\n");
 
104
        }
 
105
    } else {            /* sizeofPixelIn == 2 */
 
106
        for (c = 0; c < Colors; ++c) {
 
107
            bits16 *tp = (bits16 *)tmp + c;             /* destination */
 
108
            const bits16 *pp =  (const bits16 *)src + c;
 
109
 
 
110
            ss->dda_x = ss->dda_x_init;
 
111
            if_debug1('W', "[W]idownscale_x color %d:", c);
 
112
 
 
113
            for ( i = 0; i < WidthIn; tp += Colors) {
 
114
                int endx = dda_next(ss->dda_x);
 
115
                if (firstline)
 
116
                    *tp = *pp;
 
117
                else {
 
118
                    if ((polarity_additive && (*pp < *tp)) ||
 
119
                        (!polarity_additive && (*pp > *tp)) )
 
120
                        *tp = *pp;
 
121
                }
 
122
                i++; pp += Colors;
 
123
                while (i < endx) {
 
124
                   if (*pp < *tp)
 
125
                        *tp = *pp;
 
126
                   i++; pp += Colors;
 
127
                }
 
128
                if_debug1('W', " %d", *tp);
 
129
            }
 
130
            if_debug0('W', "\n");
 
131
        }
 
132
    }
 
133
}
 
134
 
 
135
 
 
136
/*
 
137
 * Copy from tmp to dst, taking into account PixelOut vs. PixelIn sizes
 
138
 * We do the conversion from PixelIn to PixelOut here (if needed)
 
139
 * since if the Y is scaled down we will convert less often.
 
140
 * This is simpler because we can treat all columns identically
 
141
 * without regard to the number of samples per pixel.
 
142
 */
 
143
static void
 
144
idownscale_y(void /*PixelOut */ *dst, const void /* PixelIn */ *tmp,
 
145
             stream_ISpecialDownScale_state *const ss)
 
146
{
 
147
    int kn = ss->params.WidthOut * ss->params.Colors;
 
148
    int kc;
 
149
 
 
150
    if_debug0('W', "[W]idownscale_y: ");
 
151
 
 
152
    if (ss->sizeofPixelOut == 1) {
 
153
        if (ss->sizeofPixelIn == 1) {
 
154
            const byte *pp = (byte *)tmp;
 
155
 
 
156
            for ( kc = 0; kc < kn; ++kc, pp++ ) {
 
157
                if_debug1('W', " %d", *pp);
 
158
                ((byte *)dst)[kc] = *pp;
 
159
            }
 
160
        } else {        /* sizeofPixelIn == 2 */
 
161
            const bits16 *pp = (bits16 *)tmp;
 
162
 
 
163
            for ( kc = 0; kc < kn; ++kc, pp++ ) {
 
164
                if_debug1('W', " %d", *pp);
 
165
                ((byte *)dst)[kc] = frac2byte(*pp);
 
166
            }
 
167
        }
 
168
    } else {            /* sizeofPixelOut == 2 */
 
169
        if (ss->sizeofPixelIn == 1) {
 
170
            const byte *pp = (byte *)tmp;
 
171
 
 
172
            for ( kc = 0; kc < kn; ++kc, pp++ ) {
 
173
                if_debug1('W', " %d", *pp);
 
174
                ((bits16 *)dst)[kc] = byte2frac(*pp);
 
175
            }
 
176
        } else {        /* sizeofPixelIn == 2 */
 
177
            const bits16 *pp = (bits16 *)tmp;
 
178
 
 
179
            for ( kc = 0; kc < kn; ++kc, pp++ ) {
 
180
                if_debug1('W', " %d", *pp);
 
181
                ((bits16 *)dst)[kc] = *pp;
 
182
            }
 
183
        }
 
184
    }
 
185
    if_debug0('W', "n");
 
186
}
 
187
 
 
188
/* ------ Stream implementation ------ */
 
189
 
 
190
 
 
191
/* Forward references */
 
192
static void s_ISpecialDownScale_release(stream_state * st);
 
193
 
 
194
/* Set default parameter values (actually, just clear pointers). */
 
195
static void
 
196
s_ISpecialDownScale_set_defaults(stream_state * st)
 
197
{
 
198
    stream_ISpecialDownScale_state *const ss = (stream_ISpecialDownScale_state *) st;
 
199
 
 
200
    ss->src = 0;
 
201
    ss->dst = 0;
 
202
    ss->tmp = 0;
 
203
}
 
204
 
 
205
/* Initialize the filter. */
 
206
static int
 
207
s_ISpecialDownScale_init(stream_state * st)
 
208
{
 
209
    stream_ISpecialDownScale_state *const ss = (stream_ISpecialDownScale_state *) st;
 
210
    gs_memory_t *mem = ss->memory;
 
211
 
 
212
    ss->sizeofPixelIn = ss->params.BitsPerComponentIn / 8;
 
213
    ss->sizeofPixelOut = ss->params.BitsPerComponentOut / 8;
 
214
 
 
215
    ss->src_size = ss->params.WidthIn * ss->sizeofPixelIn * ss->params.Colors;
 
216
    ss->dst_size = ss->params.WidthOut * ss->sizeofPixelOut * ss->params.Colors;
 
217
 
 
218
    /* Initialize destination DDAs. */
 
219
    ss->dst_x = 0;
 
220
    ss->src_offset = ss->dst_offset = 0;
 
221
    dda_init(ss->dda_x, 0, ss->params.WidthIn, ss->params.WidthOut);
 
222
    ss->dda_x_init = ss->dda_x;
 
223
    ss->src_y = ss->dst_y = 0;
 
224
    dda_init(ss->dda_y, 0, ss->params.HeightOut, ss->params.HeightIn);
 
225
 
 
226
 
 
227
    /* create intermediate image to hold horizontal zoom */
 
228
    ss->tmp = gs_alloc_byte_array(mem, ss->params.WidthOut * ss->params.Colors,
 
229
                                  ss->sizeofPixelIn, "image_scale tmp");
 
230
    /* Allocate buffers for 1 row of source and destination. */
 
231
    ss->dst = gs_alloc_byte_array(mem, ss->params.WidthOut * ss->params.Colors,
 
232
                                  ss->sizeofPixelOut, "image_scale dst");
 
233
    ss->src = gs_alloc_byte_array(mem, ss->params.WidthIn * ss->params.Colors,
 
234
                                  ss->sizeofPixelIn, "image_scale src");
 
235
    if (ss->tmp == 0 || ss->dst == 0 || ss->src == 0) {
 
236
        s_ISpecialDownScale_release(st);
 
237
        return ERRC;
 
238
/****** WRONG ******/
 
239
    }
 
240
 
 
241
    return 0;
 
242
 
 
243
}
 
244
 
 
245
/* Process a buffer.  Note that this handles Encode and Decode identically. */
 
246
static int
 
247
s_ISpecialDownScale_process(stream_state * st, stream_cursor_read * pr,
 
248
                 stream_cursor_write * pw, bool last)
 
249
{
 
250
    stream_ISpecialDownScale_state *const ss = (stream_ISpecialDownScale_state *) st;
 
251
    uint cur_y = dda_current(ss->dda_y);
 
252
 
 
253
    /* Check whether we need to deliver any output. */
 
254
 
 
255
top:
 
256
    if (cur_y > ss->dst_y) {
 
257
        /* Deliver some or all of the current scaled row. */
 
258
        /* to generate a vertically scaled output row. */
 
259
        uint wleft = pw->limit - pw->ptr;
 
260
 
 
261
        if (ss->dst_y == ss->params.HeightOut)
 
262
            return EOFC;
 
263
        if (wleft == 0)
 
264
            return 1;
 
265
        if (ss->dst_offset == 0) {
 
266
            byte *row;
 
267
 
 
268
            if (wleft >= ss->dst_size) {        /* We can scale the row directly into the output. */
 
269
                row = pw->ptr + 1;
 
270
                pw->ptr += ss->dst_size;
 
271
            } else {            /* We'll have to buffer the row. */
 
272
                row = ss->dst;
 
273
            }
 
274
            /* Apply filter to zoom vertically from tmp to dst. */
 
275
            idownscale_y(row, ss->tmp, ss);
 
276
            /* Idiotic C coercion rules allow T* and void* to be */
 
277
            /* inter-assigned freely, but not compared! */
 
278
            if ((void *)row != ss->dst)         /* no buffering */
 
279
                goto adv;
 
280
        } {                     /* We're delivering a buffered output row. */
 
281
            uint wcount = ss->dst_size - ss->dst_offset;
 
282
            uint ncopy = min(wleft, wcount);
 
283
 
 
284
            memcpy(pw->ptr + 1, (byte *) ss->dst + ss->dst_offset, ncopy);
 
285
            pw->ptr += ncopy;
 
286
            ss->dst_offset += ncopy;
 
287
            if (ncopy != wcount)
 
288
                return 1;
 
289
            ss->dst_offset = 0;
 
290
        }
 
291
        /* Advance to the next output row. */
 
292
adv:    ++(ss->dst_y);
 
293
    }
 
294
 
 
295
    /* Read input data and scale horizontally into tmp. */
 
296
 
 
297
    {
 
298
        uint rleft = pr->limit - pr->ptr;
 
299
        uint rcount = ss->src_size - ss->src_offset;
 
300
 
 
301
        if (rleft == 0)
 
302
            return 0;           /* need more input */
 
303
        if (ss->src_y >= ss->params.HeightIn)
 
304
            return ERRC;
 
305
        if (rleft >= rcount) {  /* We're going to fill up a row. */
 
306
            const byte *row;
 
307
 
 
308
            if (ss->src_offset == 0) {  /* We have a complete row.  Read the data */
 
309
                /* directly from the input. */
 
310
                row = pr->ptr + 1;
 
311
            } else {            /* We're buffering a row in src. */
 
312
                row = ss->src;
 
313
                memcpy((byte *) ss->src + ss->src_offset, pr->ptr + 1,
 
314
                       rcount);
 
315
                ss->src_offset = 0;
 
316
            }
 
317
            /* Apply filter to zoom horizontally from src to tmp. */
 
318
            if_debug2('w', "[w]idownscale_x y = %d to tmp row %d\n",
 
319
                      ss->src_y, (ss->src_y % MAX_ISCALE_SUPPORT));
 
320
            idownscale_x(ss->tmp, row, ss);
 
321
            pr->ptr += rcount;
 
322
            ++(ss->src_y);
 
323
            cur_y = dda_next(ss->dda_y);
 
324
            goto top;
 
325
        } else {                /* We don't have a complete row.  Copy data to src buffer. */
 
326
            memcpy((byte *) ss->src + ss->src_offset, pr->ptr + 1, rleft);
 
327
            ss->src_offset += rleft;
 
328
            pr->ptr += rleft;
 
329
            return 0;
 
330
        }
 
331
    }
 
332
}
 
333
 
 
334
/* Release the filter's storage. */
 
335
static void
 
336
s_ISpecialDownScale_release(stream_state * st)
 
337
{
 
338
    stream_ISpecialDownScale_state *const ss = (stream_ISpecialDownScale_state *) st;
 
339
    gs_memory_t *mem = ss->memory;
 
340
 
 
341
    gs_free_object(mem, (void *)ss->src, "image_scale src");    /* no longer const */
 
342
    ss->src = 0;
 
343
    gs_free_object(mem, ss->dst, "image_scale dst");
 
344
    ss->dst = 0;
 
345
    gs_free_object(mem, ss->tmp, "image_scale tmp");
 
346
    ss->tmp = 0;
 
347
}
 
348
 
 
349
/* Stream template */
 
350
const stream_template s_ISpecialDownScale_template = {
 
351
    &st_ISpecialDownScale_state, s_ISpecialDownScale_init, s_ISpecialDownScale_process, 1, 1,
 
352
    s_ISpecialDownScale_release, s_ISpecialDownScale_set_defaults
 
353
};