~ubuntu-branches/ubuntu/oneiric/ghostscript/oneiric

« back to all changes in this revision

Viewing changes to base/gsbitcom.c

  • Committer: Bazaar Package Importer
  • Author(s): Till Kamppeter
  • Date: 2011-07-15 16:49:55 UTC
  • mfrom: (1.1.23 upstream)
  • Revision ID: james.westby@ubuntu.com-20110715164955-uga6qibao6kez05c
Tags: 9.04~dfsg~20110715-0ubuntu1
* New upstream release
   - GIT snapshot from Jult, 12 2011.
* debian/patches/020110406~a54df2d.patch,
  debian/patches/020110408~0791cc8.patch,
  debian/patches/020110408~507cbee.patch,
  debian/patches/020110411~4509a49.patch,
  debian/patches/020110412~78bb9a6.patch,
  debian/patches/020110418~a05ab8a.patch,
  debian/patches/020110420~20b6c78.patch,
  debian/patches/020110420~4ddefa2.patch: Removed upstream patches.
* debian/rules: Generate ABI version number (variable "abi") correctly,
  cutting off repackaging and pre-release parts.
* debian/rules: Added ./lcms2/ directory to DEB_UPSTREAM_REPACKAGE_EXCLUDES.
* debian/copyright: Added lcms2/* to the list of excluded files.
* debian/symbols.common: Updated for new upstream source. Applied patch
  which dpkg-gensymbols generated for debian/libgs9.symbols to this file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* Copyright (C) 2001-2006 Artifex Software, Inc.
2
2
   All Rights Reserved.
3
 
  
 
3
 
4
4
   This software is provided AS-IS with no warranty, either express or
5
5
   implied.
6
6
 
11
11
   San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
12
12
*/
13
13
 
14
 
/* $Id: gsbitcom.c 8022 2007-06-05 22:23:38Z giles $ */
 
14
/* $Id$ */
15
15
/* Oversampled bitmap compression */
16
16
#include "std.h"
17
17
#include "gstypes.h"
94
94
 */
95
95
void
96
96
bits_compress_scaled(const byte * src, int srcx, uint width, uint height,
97
 
                     uint sraster, byte * dest, uint draster,
98
 
                     const gs_log2_scale_point *plog2_scale, int log2_out_bits)
 
97
                     uint sraster, byte * dest, uint draster,
 
98
                     const gs_log2_scale_point *plog2_scale, int log2_out_bits)
99
99
{
100
100
    int log2_x = plog2_scale->x, log2_y = plog2_scale->y;
101
101
    int xscale = 1 << log2_x;
110
110
    int input_byte_out_bits = out_bits << (3 - log2_x);
111
111
    byte input_byte_out_mask = (1 << input_byte_out_bits) - 1;
112
112
    const byte *table =
113
 
        compress_tables[log2_out_bits][log2_x + log2_y - 1];
 
113
        compress_tables[log2_out_bits][log2_x + log2_y - 1];
114
114
    uint sskip = sraster << log2_y;
115
115
    uint dwidth = (width >> log2_x) << log2_out_bits;
116
116
    uint dskip = draster - ((dwidth + 7) >> 3);
127
127
    uint h;
128
128
 
129
129
    for (h = height; h; srow += sskip, h -= yscale) {
130
 
        const byte *s = srow;
 
130
        const byte *s = srow;
131
131
 
132
132
#if ALPHA_LSB_FIRST
133
133
#  define out_shift_initial 0
136
136
#  define out_shift_initial (8 - out_bits)
137
137
#  define out_shift_update(out_shift, nbits) ((out_shift -= (nbits)) < 0)
138
138
#endif
139
 
        int out_shift = out_shift_initial;
140
 
        byte out = 0;
141
 
        int in_shift = in_shift_initial;
142
 
        int dw = 8 - (srcx & 7);
143
 
        int w;
144
 
 
145
 
        /* Loop over source bytes. */
146
 
        for (w = width; w > 0; w -= dw, dw = 8) {
147
 
            int index;
148
 
            int in_shift_final = (w >= dw ? 0 : dw - w);
149
 
 
150
 
            /*
151
 
             * Check quickly for all-0s or all-1s, but only if each
152
 
             * input byte generates no more than one output byte,
153
 
             * we're at an input byte boundary, and we're processing
154
 
             * an entire input byte (i.e., this isn't a final
155
 
             * partial byte.)
156
 
             */
157
 
            if (in_shift == in_shift_check && in_shift_final == 0)
158
 
                switch (*s) {
159
 
                    case 0:
160
 
                        for (index = sraster; index != sskip; index += sraster)
161
 
                            if (s[index] != 0)
162
 
                                goto p;
163
 
                        if (out_shift_update(out_shift, input_byte_out_bits))
164
 
                            *d++ = out, out_shift &= 7, out = 0;
165
 
                        s++;
166
 
                        continue;
 
139
        int out_shift = out_shift_initial;
 
140
        byte out = 0;
 
141
        int in_shift = in_shift_initial;
 
142
        int dw = 8 - (srcx & 7);
 
143
        int w;
 
144
 
 
145
        /* Loop over source bytes. */
 
146
        for (w = width; w > 0; w -= dw, dw = 8) {
 
147
            int index;
 
148
            int in_shift_final = (w >= dw ? 0 : dw - w);
 
149
 
 
150
            /*
 
151
             * Check quickly for all-0s or all-1s, but only if each
 
152
             * input byte generates no more than one output byte,
 
153
             * we're at an input byte boundary, and we're processing
 
154
             * an entire input byte (i.e., this isn't a final
 
155
             * partial byte.)
 
156
             */
 
157
            if (in_shift == in_shift_check && in_shift_final == 0)
 
158
                switch (*s) {
 
159
                    case 0:
 
160
                        for (index = sraster; index != sskip; index += sraster)
 
161
                            if (s[index] != 0)
 
162
                                goto p;
 
163
                        if (out_shift_update(out_shift, input_byte_out_bits))
 
164
                            *d++ = out, out_shift &= 7, out = 0;
 
165
                        s++;
 
166
                        continue;
167
167
#if !ALPHA_LSB_FIRST            /* too messy to make it work */
168
 
                    case 0xff:
169
 
                        for (index = sraster; index != sskip; index += sraster)
170
 
                            if (s[index] != 0xff)
171
 
                                goto p;
172
 
                        {
173
 
                            int shift =
174
 
                                (out_shift -= input_byte_out_bits) + out_bits;
 
168
                    case 0xff:
 
169
                        for (index = sraster; index != sskip; index += sraster)
 
170
                            if (s[index] != 0xff)
 
171
                                goto p;
 
172
                        {
 
173
                            int shift =
 
174
                                (out_shift -= input_byte_out_bits) + out_bits;
175
175
 
176
 
                            if (shift > 0)
177
 
                                out |= input_byte_out_mask << shift;
178
 
                            else {
179
 
                                out |= input_byte_out_mask >> -shift;
180
 
                                *d++ = out;
181
 
                                out_shift += 8;
182
 
                                out = input_byte_out_mask << (8 + shift);
183
 
                            }
184
 
                        }
185
 
                        s++;
186
 
                        continue;
 
176
                            if (shift > 0)
 
177
                                out |= input_byte_out_mask << shift;
 
178
                            else {
 
179
                                out |= input_byte_out_mask >> -shift;
 
180
                                *d++ = out;
 
181
                                out_shift += 8;
 
182
                                out = input_byte_out_mask << (8 + shift);
 
183
                            }
 
184
                        }
 
185
                        s++;
 
186
                        continue;
187
187
#endif
188
 
                    default:
189
 
                        ;
190
 
                }
191
 
          p:                    /* Loop over source pixels within a byte. */
192
 
            do {
193
 
                uint count;
194
 
 
195
 
                for (index = 0, count = 0; index != sskip;
196
 
                     index += sraster
197
 
                    )
198
 
                    count += half_byte_1s[(s[index] >> in_shift) & mask];
199
 
                if (count != 0 && table[count] == 0) {  /* Look at adjacent cells to help prevent */
200
 
                    /* dropouts. */
201
 
                    uint orig_count = count;
202
 
                    uint shifted_mask = mask << in_shift;
203
 
                    byte in;
204
 
 
205
 
                    if_debug3('B', "[B]count(%d,%d)=%d\n",
206
 
                              (width - w) / xscale,
207
 
                              (height - h) / yscale, count);
208
 
                    if (yscale > 1) {   /* Look at the next "lower" cell. */
209
 
                        if (h < height && (in = s[0] & shifted_mask) != 0) {
210
 
                            uint lower;
211
 
 
212
 
                            for (index = 0, lower = 0;
213
 
                                 -(index -= sraster) <= sskip &&
214
 
                                 (in &= s[index]) != 0;
215
 
                                )
216
 
                                lower += half_byte_1s[in >> in_shift];
217
 
                            if_debug1('B', "[B]  lower adds %d\n",
218
 
                                      lower);
219
 
                            if (lower <= orig_count)
220
 
                                count += lower;
221
 
                        }
222
 
                        /* Look at the next "higher" cell. */
223
 
                        if (h > yscale && (in = s[sskip - sraster] & shifted_mask) != 0) {
224
 
                            uint upper;
225
 
 
226
 
                            for (index = sskip, upper = 0;
227
 
                                 index < sskip << 1 &&
228
 
                                 (in &= s[index]) != 0;
229
 
                                 index += sraster
230
 
                                )
231
 
                                upper += half_byte_1s[in >> in_shift];
232
 
                            if_debug1('B', "[B]  upper adds %d\n",
233
 
                                      upper);
234
 
                            if (upper < orig_count)
235
 
                                count += upper;
236
 
                        }
237
 
                    }
238
 
                    if (xscale > 1) {
239
 
                        uint mask1 = (mask << 1) + 1;
240
 
 
241
 
                        /* Look at the next cell to the left. */
242
 
                        if (w < width) {
243
 
                            int lshift = in_shift + xscale - 1;
244
 
                            uint left;
245
 
 
246
 
                            for (index = 0, left = 0;
247
 
                                 index < sskip; index += sraster
248
 
                                ) {
249
 
                                uint bits =
250
 
                                ((s[index - 1] << 8) +
251
 
                                 s[index]) >> lshift;
252
 
 
253
 
                                left += bits5_trailing_1s[bits & mask1];
254
 
                            }
255
 
                            if_debug1('B', "[B]  left adds %d\n",
256
 
                                      left);
257
 
                            if (left < orig_count)
258
 
                                count += left;
259
 
                        }
260
 
                        /* Look at the next cell to the right. */
261
 
                        if (w > xscale) {
262
 
                            int rshift = in_shift - xscale + 8;
263
 
                            uint right;
264
 
 
265
 
                            for (index = 0, right = 0;
266
 
                                 index < sskip; index += sraster
267
 
                                ) {
268
 
                                uint bits =
269
 
                                ((s[index] << 8) +
270
 
                                 s[index + 1]) >> rshift;
271
 
 
272
 
                                right += bits5_leading_1s[(bits & mask1) << (4 - xscale)];
273
 
                            }
274
 
                            if_debug1('B', "[B]  right adds %d\n",
275
 
                                      right);
276
 
                            if (right <= orig_count)
277
 
                                count += right;
278
 
                        }
279
 
                    }
280
 
                    if (count > count_max)
281
 
                        count = count_max;
282
 
                }
283
 
                out += table[count] << out_shift;
284
 
                if (out_shift_update(out_shift, out_bits))
285
 
                    *d++ = out, out_shift &= 7, out = 0;
286
 
            }
287
 
            while ((in_shift -= xscale) >= in_shift_final);
288
 
            s++, in_shift += 8;
289
 
        }
290
 
        if (out_shift != out_shift_initial)
291
 
            *d++ = out;
292
 
        for (w = dskip; w != 0; w--)
293
 
            *d++ = 0;
 
188
                    default:
 
189
                        ;
 
190
                }
 
191
          p:                    /* Loop over source pixels within a byte. */
 
192
            do {
 
193
                uint count;
 
194
 
 
195
                for (index = 0, count = 0; index != sskip;
 
196
                     index += sraster
 
197
                    )
 
198
                    count += half_byte_1s[(s[index] >> in_shift) & mask];
 
199
                if (count != 0 && table[count] == 0) {  /* Look at adjacent cells to help prevent */
 
200
                    /* dropouts. */
 
201
                    uint orig_count = count;
 
202
                    uint shifted_mask = mask << in_shift;
 
203
                    byte in;
 
204
 
 
205
                    if_debug3('B', "[B]count(%d,%d)=%d\n",
 
206
                              (width - w) / xscale,
 
207
                              (height - h) / yscale, count);
 
208
                    if (yscale > 1) {   /* Look at the next "lower" cell. */
 
209
                        if (h < height && (in = s[0] & shifted_mask) != 0) {
 
210
                            uint lower;
 
211
 
 
212
                            for (index = 0, lower = 0;
 
213
                                 -(index -= sraster) <= sskip &&
 
214
                                 (in &= s[index]) != 0;
 
215
                                )
 
216
                                lower += half_byte_1s[in >> in_shift];
 
217
                            if_debug1('B', "[B]  lower adds %d\n",
 
218
                                      lower);
 
219
                            if (lower <= orig_count)
 
220
                                count += lower;
 
221
                        }
 
222
                        /* Look at the next "higher" cell. */
 
223
                        if (h > yscale && (in = s[sskip - sraster] & shifted_mask) != 0) {
 
224
                            uint upper;
 
225
 
 
226
                            for (index = sskip, upper = 0;
 
227
                                 index < sskip << 1 &&
 
228
                                 (in &= s[index]) != 0;
 
229
                                 index += sraster
 
230
                                )
 
231
                                upper += half_byte_1s[in >> in_shift];
 
232
                            if_debug1('B', "[B]  upper adds %d\n",
 
233
                                      upper);
 
234
                            if (upper < orig_count)
 
235
                                count += upper;
 
236
                        }
 
237
                    }
 
238
                    if (xscale > 1) {
 
239
                        uint mask1 = (mask << 1) + 1;
 
240
 
 
241
                        /* Look at the next cell to the left. */
 
242
                        if (w < width) {
 
243
                            int lshift = in_shift + xscale - 1;
 
244
                            uint left;
 
245
 
 
246
                            for (index = 0, left = 0;
 
247
                                 index < sskip; index += sraster
 
248
                                ) {
 
249
                                uint bits =
 
250
                                ((s[index - 1] << 8) +
 
251
                                 s[index]) >> lshift;
 
252
 
 
253
                                left += bits5_trailing_1s[bits & mask1];
 
254
                            }
 
255
                            if_debug1('B', "[B]  left adds %d\n",
 
256
                                      left);
 
257
                            if (left < orig_count)
 
258
                                count += left;
 
259
                        }
 
260
                        /* Look at the next cell to the right. */
 
261
                        if (w > xscale) {
 
262
                            int rshift = in_shift - xscale + 8;
 
263
                            uint right;
 
264
 
 
265
                            for (index = 0, right = 0;
 
266
                                 index < sskip; index += sraster
 
267
                                ) {
 
268
                                uint bits =
 
269
                                ((s[index] << 8) +
 
270
                                 s[index + 1]) >> rshift;
 
271
 
 
272
                                right += bits5_leading_1s[(bits & mask1) << (4 - xscale)];
 
273
                            }
 
274
                            if_debug1('B', "[B]  right adds %d\n",
 
275
                                      right);
 
276
                            if (right <= orig_count)
 
277
                                count += right;
 
278
                        }
 
279
                    }
 
280
                    if (count > count_max)
 
281
                        count = count_max;
 
282
                }
 
283
                out += table[count] << out_shift;
 
284
                if (out_shift_update(out_shift, out_bits))
 
285
                    *d++ = out, out_shift &= 7, out = 0;
 
286
            }
 
287
            while ((in_shift -= xscale) >= in_shift_final);
 
288
            s++, in_shift += 8;
 
289
        }
 
290
        if (out_shift != out_shift_initial)
 
291
            *d++ = out;
 
292
        for (w = dskip; w != 0; w--)
 
293
            *d++ = 0;
294
294
#undef out_shift_initial
295
295
#undef out_shift_update
296
296
    }