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

« back to all changes in this revision

Viewing changes to base/sdctd.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: sdctd.c 8250 2007-09-25 13:31:24Z giles $ */
 
15
/* DCT decoding filter stream */
 
16
#include "memory_.h"
 
17
#include "stdio_.h"
 
18
#include "jpeglib_.h"
 
19
#include "jerror_.h"
 
20
#include "gdebug.h"
 
21
#include "gsmemory.h"
 
22
#include "strimpl.h"
 
23
#include "sdct.h"
 
24
#include "sjpeg.h"
 
25
 
 
26
/* ------ DCTDecode ------ */
 
27
 
 
28
/* JPEG source manager procedures */
 
29
static void
 
30
dctd_init_source(j_decompress_ptr dinfo)
 
31
{
 
32
}
 
33
static const JOCTET fake_eoi[2] =
 
34
{0xFF, JPEG_EOI};
 
35
static boolean
 
36
dctd_fill_input_buffer(j_decompress_ptr dinfo)
 
37
{
 
38
    jpeg_decompress_data *jddp =
 
39
    (jpeg_decompress_data *) ((char *)dinfo -
 
40
                              offset_of(jpeg_decompress_data, dinfo));
 
41
 
 
42
    if (!jddp->input_eod)
 
43
        return FALSE;           /* normal case: suspend processing */
 
44
    /* Reached end of source data without finding EOI */
 
45
    WARNMS(dinfo, JWRN_JPEG_EOF);
 
46
    /* Insert a fake EOI marker */
 
47
    dinfo->src->next_input_byte = fake_eoi;
 
48
    dinfo->src->bytes_in_buffer = 2;
 
49
    jddp->faked_eoi = true;     /* so process routine doesn't use next_input_byte */
 
50
    return TRUE;
 
51
}
 
52
static void
 
53
dctd_skip_input_data(j_decompress_ptr dinfo, long num_bytes)
 
54
{
 
55
    struct jpeg_source_mgr *src = dinfo->src;
 
56
    jpeg_decompress_data *jddp =
 
57
    (jpeg_decompress_data *) ((char *)dinfo -
 
58
                              offset_of(jpeg_decompress_data, dinfo));
 
59
 
 
60
    if (num_bytes > 0) {
 
61
        if (num_bytes > src->bytes_in_buffer) {
 
62
            jddp->skip += num_bytes - src->bytes_in_buffer;
 
63
            src->next_input_byte += src->bytes_in_buffer;
 
64
            src->bytes_in_buffer = 0;
 
65
            return;
 
66
        }
 
67
        src->next_input_byte += num_bytes;
 
68
        src->bytes_in_buffer -= num_bytes;
 
69
    }
 
70
}
 
71
static void
 
72
dctd_term_source(j_decompress_ptr dinfo)
 
73
{
 
74
}
 
75
 
 
76
/* Set the defaults for the DCTDecode filter. */
 
77
static void
 
78
s_DCTD_set_defaults(stream_state * st)
 
79
{
 
80
    s_DCT_set_defaults(st);
 
81
}
 
82
 
 
83
/* Initialize DCTDecode filter */
 
84
static int
 
85
s_DCTD_init(stream_state * st)
 
86
{
 
87
    stream_DCT_state *const ss = (stream_DCT_state *) st;
 
88
    struct jpeg_source_mgr *src = &ss->data.decompress->source;
 
89
 
 
90
    src->init_source = dctd_init_source;
 
91
    src->fill_input_buffer = dctd_fill_input_buffer;
 
92
    src->skip_input_data = dctd_skip_input_data;
 
93
    src->term_source = dctd_term_source;
 
94
    src->resync_to_restart = jpeg_resync_to_restart;    /* use default method */
 
95
    ss->data.common->memory = ss->jpeg_memory;
 
96
    ss->data.decompress->dinfo.src = src;
 
97
    ss->data.decompress->skip = 0;
 
98
    ss->data.decompress->input_eod = false;
 
99
    ss->data.decompress->faked_eoi = false;
 
100
    ss->phase = 0;
 
101
    return 0;
 
102
}
 
103
 
 
104
/* Process a buffer */
 
105
static int
 
106
s_DCTD_process(stream_state * st, stream_cursor_read * pr,
 
107
               stream_cursor_write * pw, bool last)
 
108
{
 
109
    stream_DCT_state *const ss = (stream_DCT_state *) st;
 
110
    jpeg_decompress_data *jddp = ss->data.decompress;
 
111
    struct jpeg_source_mgr *src = jddp->dinfo.src;
 
112
    int code;
 
113
 
 
114
    if_debug3('w', "[wdd]process avail=%u, skip=%u, last=%d\n",
 
115
              (uint) (pr->limit - pr->ptr), (uint) jddp->skip, last);
 
116
    if (jddp->skip != 0) {
 
117
        long avail = pr->limit - pr->ptr;
 
118
 
 
119
        if (avail < jddp->skip) {
 
120
            jddp->skip -= avail;
 
121
            pr->ptr = pr->limit;
 
122
            if (!last)
 
123
                return 0;       /* need more data */
 
124
            jddp->skip = 0;     /* don't skip past input EOD */
 
125
        }
 
126
        pr->ptr += jddp->skip;
 
127
        jddp->skip = 0;
 
128
    }
 
129
    src->next_input_byte = pr->ptr + 1;
 
130
    src->bytes_in_buffer = pr->limit - pr->ptr;
 
131
    jddp->input_eod = last;
 
132
    switch (ss->phase) {
 
133
        case 0:         /* not initialized yet */
 
134
            /*
 
135
             * Adobe implementations seem to ignore leading garbage bytes,
 
136
             * even though neither the standard nor Adobe's own
 
137
             * documentation mention this.
 
138
             */
 
139
            while (pr->ptr < pr->limit && pr->ptr[1] != 0xff)
 
140
                pr->ptr++;
 
141
            if (pr->ptr == pr->limit)
 
142
                return 0;
 
143
            src->next_input_byte = pr->ptr + 1;
 
144
            src->bytes_in_buffer = pr->limit - pr->ptr;
 
145
            ss->phase = 1;
 
146
            /* falls through */
 
147
        case 1:         /* reading header markers */
 
148
            if ((code = gs_jpeg_read_header(ss, TRUE)) < 0)
 
149
                return ERRC;
 
150
            pr->ptr =
 
151
                (jddp->faked_eoi ? pr->limit : src->next_input_byte - 1);
 
152
            switch (code) {
 
153
                case JPEG_SUSPENDED:
 
154
                    return 0;
 
155
                    /*case JPEG_HEADER_OK: */
 
156
            }
 
157
            /* If we have a ColorTransform parameter, and it's not
 
158
             * overridden by an Adobe marker in the data, set colorspace.
 
159
             */
 
160
            if (ss->ColorTransform >= 0 &&
 
161
                !jddp->dinfo.saw_Adobe_marker) {
 
162
                switch (jddp->dinfo.num_components) {
 
163
                    case 3:
 
164
                        jddp->dinfo.jpeg_color_space =
 
165
                            (ss->ColorTransform ? JCS_YCbCr : JCS_RGB);
 
166
                        /* out_color_space will default to JCS_RGB */
 
167
                        break;
 
168
                    case 4:
 
169
                        jddp->dinfo.jpeg_color_space =
 
170
                            (ss->ColorTransform ? JCS_YCCK : JCS_CMYK);
 
171
                        /* out_color_space will default to JCS_CMYK */
 
172
                        break;
 
173
                }
 
174
            }
 
175
            ss->phase = 2;
 
176
            /* falls through */
 
177
        case 2:         /* start_decompress */
 
178
            if ((code = gs_jpeg_start_decompress(ss)) < 0)
 
179
                return ERRC;
 
180
            pr->ptr =
 
181
                (jddp->faked_eoi ? pr->limit : src->next_input_byte - 1);
 
182
            if (code == 0)
 
183
                return 0;
 
184
            ss->scan_line_size =
 
185
                jddp->dinfo.output_width * jddp->dinfo.output_components;
 
186
            if_debug4('w', "[wdd]width=%u, components=%d, scan_line_size=%u, min_out_size=%u\n",
 
187
                      jddp->dinfo.output_width,
 
188
                      jddp->dinfo.output_components,
 
189
                      ss->scan_line_size, jddp->template.min_out_size);
 
190
            if (ss->scan_line_size > (uint) jddp->template.min_out_size) {
 
191
                /* Create a spare buffer for oversize scanline */
 
192
                jddp->scanline_buffer =
 
193
                    gs_alloc_bytes_immovable(gs_memory_stable(jddp->memory),
 
194
                                             ss->scan_line_size,
 
195
                                         "s_DCTD_process(scanline_buffer)");
 
196
                if (jddp->scanline_buffer == NULL)
 
197
                    return ERRC;
 
198
            }
 
199
            jddp->bytes_in_scanline = 0;
 
200
            ss->phase = 3;
 
201
            /* falls through */
 
202
        case 3:         /* reading data */
 
203
          dumpbuffer:
 
204
            if (jddp->bytes_in_scanline != 0) {
 
205
                uint avail = pw->limit - pw->ptr;
 
206
                uint tomove = min(jddp->bytes_in_scanline,
 
207
                                  avail);
 
208
 
 
209
                if_debug2('w', "[wdd]moving %u/%u\n",
 
210
                          tomove, avail);
 
211
                memcpy(pw->ptr + 1, jddp->scanline_buffer +
 
212
                       (ss->scan_line_size - jddp->bytes_in_scanline),
 
213
                       tomove);
 
214
                pw->ptr += tomove;
 
215
                jddp->bytes_in_scanline -= tomove;
 
216
                if (jddp->bytes_in_scanline != 0)
 
217
                    return 1;   /* need more room */
 
218
            }
 
219
            while (jddp->dinfo.output_height > jddp->dinfo.output_scanline) {
 
220
                int read;
 
221
                byte *samples;
 
222
 
 
223
                if (jddp->scanline_buffer != NULL)
 
224
                    samples = jddp->scanline_buffer;
 
225
                else {
 
226
                    if ((uint) (pw->limit - pw->ptr) < ss->scan_line_size)
 
227
                        return 1;       /* need more room */
 
228
                    samples = pw->ptr + 1;
 
229
                }
 
230
                read = gs_jpeg_read_scanlines(ss, &samples, 1);
 
231
                if (read < 0)
 
232
                    return ERRC;
 
233
                if_debug3('w', "[wdd]read returns %d, used=%u, faked_eoi=%d\n",
 
234
                          read,
 
235
                          (uint) (src->next_input_byte - 1 - pr->ptr),
 
236
                          (int)jddp->faked_eoi);
 
237
                pr->ptr =
 
238
                    (jddp->faked_eoi ? pr->limit : src->next_input_byte - 1);
 
239
                if (!read)
 
240
                    return 0;   /* need more data */
 
241
                if (jddp->scanline_buffer != NULL) {
 
242
                    jddp->bytes_in_scanline = ss->scan_line_size;
 
243
                    goto dumpbuffer;
 
244
                }
 
245
                pw->ptr += ss->scan_line_size;
 
246
            }
 
247
            ss->phase = 4;
 
248
            /* falls through */
 
249
        case 4:         /* end of image; scan for EOI */
 
250
            if ((code = gs_jpeg_finish_decompress(ss)) < 0)
 
251
                return ERRC;
 
252
            pr->ptr =
 
253
                (jddp->faked_eoi ? pr->limit : src->next_input_byte - 1);
 
254
            if (code == 0)
 
255
                return 0;
 
256
            ss->phase = 5;
 
257
            /* falls through */
 
258
        case 5:         /* we are DONE */
 
259
            return EOFC;
 
260
    }
 
261
    /* Default case can't happen.... */
 
262
    return ERRC;
 
263
}
 
264
 
 
265
/* Release the stream */
 
266
static void
 
267
s_DCTD_release(stream_state * st)
 
268
{
 
269
    stream_DCT_state *const ss = (stream_DCT_state *) st;
 
270
 
 
271
    gs_jpeg_destroy(ss);
 
272
    if (ss->data.decompress->scanline_buffer != NULL)
 
273
        gs_free_object(gs_memory_stable(ss->data.common->memory),
 
274
                       ss->data.decompress->scanline_buffer,
 
275
                       "s_DCTD_release(scanline_buffer)");
 
276
    gs_free_object(ss->data.common->memory, ss->data.decompress,
 
277
                   "s_DCTD_release");
 
278
    /* Switch the template pointer back in case we still need it. */
 
279
    st->template = &s_DCTD_template;
 
280
}
 
281
 
 
282
/* Stream template */
 
283
const stream_template s_DCTD_template =
 
284
{&st_DCT_state, s_DCTD_init, s_DCTD_process, 2000, 4000, s_DCTD_release,
 
285
 s_DCTD_set_defaults
 
286
};