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

« back to all changes in this revision

Viewing changes to base/gsdevmem.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: gsdevmem.c 8022 2007-06-05 22:23:38Z giles $ */
 
15
/* Memory device creation for Ghostscript library */
 
16
#include "math_.h"              /* for fabs */
 
17
#include "memory_.h"
 
18
#include "gx.h"
 
19
#include "gserrors.h"
 
20
#include "gsdevice.h"           /* for prototypes */
 
21
#include "gxarith.h"
 
22
#include "gxdevice.h"
 
23
#include "gxdevmem.h"
 
24
 
 
25
/* Make a memory (image) device. */
 
26
/* If colors_size = -16, -24, or -32, this is a true-color device; */
 
27
/* otherwise, colors_size is the size of the palette in bytes */
 
28
/* (2^N for gray scale, 3*2^N for RGB color). */
 
29
/* We separate device allocation and initialization at customer request. */
 
30
int
 
31
gs_initialize_wordimagedevice(gx_device_memory * new_dev, const gs_matrix * pmat,
 
32
              uint width, uint height, const byte * colors, int colors_size,
 
33
                    bool word_oriented, bool page_device, gs_memory_t * mem)
 
34
{
 
35
    const gx_device_memory *proto_dev;
 
36
    int palette_count = colors_size;
 
37
    int num_components = 1;
 
38
    int pcount;
 
39
    int bits_per_pixel;
 
40
    float x_pixels_per_unit, y_pixels_per_unit;
 
41
    byte palette[256 * 3];
 
42
    bool has_color;
 
43
 
 
44
    switch (colors_size) {
 
45
        case 3 * 2:
 
46
            palette_count = 2;
 
47
            num_components = 3;
 
48
        case 2:
 
49
            bits_per_pixel = 1;
 
50
            break;
 
51
        case 3 * 4:
 
52
            palette_count = 4;
 
53
            num_components = 3;
 
54
        case 4:
 
55
            bits_per_pixel = 2;
 
56
            break;
 
57
        case 3 * 16:
 
58
            palette_count = 16;
 
59
            num_components = 3;
 
60
        case 16:
 
61
            bits_per_pixel = 4;
 
62
            break;
 
63
        case 3 * 256:
 
64
            palette_count = 256;
 
65
            num_components = 3;
 
66
        case 256:
 
67
            bits_per_pixel = 8;
 
68
            break;
 
69
        case -16:
 
70
            bits_per_pixel = 16;
 
71
            palette_count = 0;
 
72
            break;
 
73
        case -24:
 
74
            bits_per_pixel = 24;
 
75
            palette_count = 0;
 
76
            break;
 
77
        case -32:
 
78
            bits_per_pixel = 32;
 
79
            palette_count = 0;
 
80
            break;
 
81
        default:
 
82
            return_error(gs_error_rangecheck);
 
83
    }
 
84
    proto_dev = (word_oriented ?
 
85
                 gdev_mem_word_device_for_bits(bits_per_pixel) :
 
86
                 gdev_mem_device_for_bits(bits_per_pixel));
 
87
    if (proto_dev == 0)         /* no suitable device */
 
88
        return_error(gs_error_rangecheck);
 
89
    pcount = palette_count * 3;
 
90
    /* Check to make sure the palette contains white and black, */
 
91
    /* and, if it has any colors, the six primaries. */
 
92
    if (bits_per_pixel <= 8) {
 
93
        const byte *p;
 
94
        byte *q;
 
95
        int primary_mask = 0;
 
96
        int i;
 
97
 
 
98
        has_color = false;
 
99
        for (i = 0, p = colors, q = palette;
 
100
             i < palette_count; i++, q += 3
 
101
            ) {
 
102
            int mask = 1;
 
103
 
 
104
            switch (num_components) {
 
105
                case 1: /* gray */
 
106
                    q[0] = q[1] = q[2] = *p++;
 
107
                    break;
 
108
                default /* case 3 */ :          /* RGB */
 
109
                    q[0] = p[0], q[1] = p[1], q[2] = p[2];
 
110
                    p += 3;
 
111
            }
 
112
#define shift_mask(b,n)\
 
113
  switch ( b ) { case 0xff: mask <<= n; case 0: break; default: mask = 0; }
 
114
            shift_mask(q[0], 4);
 
115
            shift_mask(q[1], 2);
 
116
            shift_mask(q[2], 1);
 
117
#undef shift_mask
 
118
            primary_mask |= mask;
 
119
            if (q[0] != q[1] || q[0] != q[2])
 
120
                has_color = true;
 
121
        }
 
122
        switch (primary_mask) {
 
123
            case 129:           /* just black and white */
 
124
                if (has_color)  /* color but no primaries */
 
125
                    return_error(gs_error_rangecheck);
 
126
            case 255:           /* full color */
 
127
                break;
 
128
            default:
 
129
                return_error(gs_error_rangecheck);
 
130
        }
 
131
    } else
 
132
        has_color = true;
 
133
    /*
 
134
     * The initial transformation matrix must map 1 user unit to
 
135
     * 1/72".  Let W and H be the width and height in pixels, and
 
136
     * assume the initial matrix is of the form [A 0 0 B X Y].
 
137
     * Then the size of the image in user units is (W/|A|,H/|B|),
 
138
     * hence the size in inches is ((W/|A|)/72,(H/|B|)/72), so
 
139
     * the number of pixels per inch is
 
140
     * (W/((W/|A|)/72),H/((H/|B|)/72)), or (|A|*72,|B|*72).
 
141
     * Similarly, if the initial matrix is [0 A B 0 X Y] for a 90
 
142
     * or 270 degree rotation, the size of the image in user
 
143
     * units is (W/|B|,H/|A|), so the pixels per inch are
 
144
     * (|B|*72,|A|*72).  We forbid non-orthogonal transformation
 
145
     * matrices.
 
146
     */
 
147
    if (is_fzero2(pmat->xy, pmat->yx))
 
148
        x_pixels_per_unit = pmat->xx, y_pixels_per_unit = pmat->yy;
 
149
    else if (is_fzero2(pmat->xx, pmat->yy))
 
150
        x_pixels_per_unit = pmat->yx, y_pixels_per_unit = pmat->xy;
 
151
    else
 
152
        return_error(gs_error_undefinedresult);
 
153
    /* All checks done, initialize the device. */
 
154
    if (bits_per_pixel == 1) {
 
155
        /* Determine the polarity from the palette. */
 
156
        gs_make_mem_device(new_dev, proto_dev, mem,
 
157
                           (page_device ? 1 : -1), 0);
 
158
        /* This is somewhat bogus, but does the right thing */
 
159
        /* in the only cases we care about. */
 
160
        gdev_mem_mono_set_inverted(new_dev,
 
161
                               (palette[0] | palette[1] | palette[2]) != 0);
 
162
    } else {
 
163
        byte *dev_palette = gs_alloc_string(mem, pcount,
 
164
                                            "gs_makeimagedevice(palette)");
 
165
 
 
166
        if (dev_palette == 0)
 
167
            return_error(gs_error_VMerror);
 
168
        gs_make_mem_device(new_dev, proto_dev, mem,
 
169
                           (page_device ? 1 : -1), 0);
 
170
        new_dev->palette.size = pcount;
 
171
        new_dev->palette.data = dev_palette;
 
172
        memcpy(dev_palette, palette, pcount);
 
173
        if (!has_color) {
 
174
            new_dev->color_info.num_components = 1;
 
175
            new_dev->color_info.max_color = 0;
 
176
            new_dev->color_info.dither_colors = 0;
 
177
            new_dev->color_info.gray_index = 0;
 
178
        }
 
179
    }
 
180
    /* Memory defice is always initialised as an internal device but */
 
181
    /* this is an external device */
 
182
    new_dev->retained = true;
 
183
    rc_init(new_dev, new_dev->memory, 1);
 
184
 
 
185
    new_dev->initial_matrix = *pmat;
 
186
    new_dev->MarginsHWResolution[0] = new_dev->HWResolution[0] =
 
187
        fabs(x_pixels_per_unit) * 72;
 
188
    new_dev->MarginsHWResolution[1] = new_dev->HWResolution[1] =
 
189
        fabs(y_pixels_per_unit) * 72;
 
190
    gx_device_set_width_height((gx_device *) new_dev, width, height);
 
191
    /* Set the ImagingBBox so we get a correct clipping region. */
 
192
    {
 
193
        gs_rect bbox;
 
194
 
 
195
        bbox.p.x = 0;
 
196
        bbox.p.y = 0;
 
197
        bbox.q.x = width;
 
198
        bbox.q.y = height;
 
199
        gs_bbox_transform_inverse(&bbox, pmat, &bbox);
 
200
        new_dev->ImagingBBox[0] = bbox.p.x;
 
201
        new_dev->ImagingBBox[1] = bbox.p.y;
 
202
        new_dev->ImagingBBox[2] = bbox.q.x;
 
203
        new_dev->ImagingBBox[3] = bbox.q.y;
 
204
        new_dev->ImagingBBox_set = true;
 
205
    }
 
206
    /* The bitmap will be allocated when the device is opened. */
 
207
    new_dev->is_open = false;
 
208
    new_dev->bitmap_memory = mem;
 
209
    return 0;
 
210
}
 
211
 
 
212
int
 
213
gs_makewordimagedevice(gx_device ** pnew_dev, const gs_matrix * pmat,
 
214
               uint width, uint height, const byte * colors, int num_colors,
 
215
                    bool word_oriented, bool page_device, gs_memory_t * mem)
 
216
{
 
217
    int code;
 
218
    gx_device_memory *pnew =
 
219
    gs_alloc_struct(mem, gx_device_memory, &st_device_memory,
 
220
                    "gs_makeimagedevice(device)");
 
221
 
 
222
    if (pnew == 0)
 
223
        return_error(gs_error_VMerror);
 
224
    code = gs_initialize_wordimagedevice(pnew, pmat, width, height,
 
225
                                         colors, num_colors, word_oriented,
 
226
                                         page_device, mem);
 
227
    if (code < 0) {
 
228
        gs_free_object(mem, pnew, "gs_makeimagedevice(device)");
 
229
        return code;
 
230
    }
 
231
    *pnew_dev = (gx_device *) pnew;
 
232
    return 0;
 
233
}