summaryrefslogtreecommitdiff
path: root/base/gxdownscale.h
blob: fdbbad278f12476f410cee1495e4feddeff544d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* Copyright (C) 2001-2020 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
   CA 94945, U.S.A., +1(415)492-9861, for further information.
*/


#ifndef gxdownscale_INCLUDED
#define gxdownscale_INCLUDED

#include "ctype_.h"
#include "gsmemory.h"
#include "gstypes.h"
#include "gxdevcli.h"
#include "gxgetbit.h"

#include "claptrap.h"

/* Function to apply color management to a rectangle of data.
 *
 * dst/src point to arrays of pointers, 1 per component.
 * In the chunky case, only the first pointer is valid.
 * In the planar case, with n planes, the first n pointers
 * are valid.
 * w and h are the width and height of the rectangle of
 * data to be processed.
 * raster is the number of bytes to offset from the start
 * of one line to the next. This value should be ignored
 * in chunky cases.
 */
typedef int (gx_downscale_cm_fn)(void  *arg,
                                 byte **dst,
                                 byte **src,
                                 int    w,
                                 int    h,
                                 int    raster);

typedef struct gx_downscaler_ht_s
{
    int w;
    int h;
    int stride;
    int x_phase;
    int y_phase;
    byte *data;
} gx_downscaler_ht_t;

/* The following structure definitions should really be considered
 * private, and are exposed here only because it enables us to define
 * gx_downscaler_t's on the stack, thus avoiding mallocs.
 */

typedef struct gx_downscaler_s gx_downscaler_t;

/* Private function type for the core downscaler routines */
typedef void (gx_downscale_core)(gx_downscaler_t *ds,
                                 byte            *out_buffer,
                                 byte            *in_buffer,
                                 int              row,
                                 int              plane,
                                 int              span);

struct gx_downscaler_s {
    gx_device            *dev;        /* Device */
    int                   width;      /* Width (pixels) */
    int                   awidth;     /* Adjusted width (pixels) */
    int                   span;       /* Num bytes in unscaled scanline */
    int                   factor;     /* Factor to downscale */
    byte                 *mfs_data;   /* MinFeatureSize data */
    int                   src_bpc;    /* Source bpc */
    int                   dst_bpc;    /* Destination bpc */
    int                  *errors;     /* Error diffusion table */
    byte                 *scaled_data;/* Downscaled data (only used for non
                                       * integer downscales). */
    int                   scaled_span;/* Num bytes in scaled scanline */
    gx_downscale_core    *down_core;  /* Core downscaling function */
    gs_get_bits_params_t  params;     /* Params if in planar mode */
    int                   num_comps;  /* Number of components as rendered */
    int                   num_planes; /* Number of planes if planar, 0 otherwise */

    ClapTrap             *claptrap;   /* ClapTrap pointer (if trapping) */
    int                   claptrap_y; /* y pointer (if trapping) */
    gs_get_bits_params_t *claptrap_params; /* params (if trapping) */

    int                   early_cm;
    gx_downscale_cm_fn   *apply_cm;
    void                 *apply_cm_arg;
    int                   post_cm_num_comps;

    void                 *ets_config;
    gx_downscale_core    *ets_downscale;

    byte                 *pre_cm[GS_CLIENT_COLOR_MAX_COMPONENTS];
    byte                 *post_cm[GS_CLIENT_COLOR_MAX_COMPONENTS];

    gx_downscaler_ht_t   *ht;
    byte                 *htrow;
    byte                 *htrow_alloc;
    byte                 *inbuf;
    byte                 *inbuf_alloc;
};

/* To use the downscaler:
 *
 *  + define a gx_downscaler_t on the stack.
 *  + initialise it with gx_downscaler_init or gx_downscaler_init_planar
 *  + repeatedly call gx_downscaler_get_lines (or
 *    gx_downscaler_copy_scan_lines) (for chunky mode) or
 *    gx_downscaler_get_bits_rectangle (for planar mode)
 *  + finalise with gx_downscaler_fin
 */
 
/* For chunky mode, currently only:
 *   src_bpc ==  8 && dst_bpc ==  1 && num_comps == 1
 *   src_bpc ==  8 && dst_bpc ==  8 && num_comps == 1
 *   src_bpc ==  8 && dst_bpc ==  8 && num_comps == 3
 *   src_bpc == 16 && dst_bpc == 16 && num_comps == 1
 * are supported. mfs is ignored for all except the first of these.
 * For planar mode, currently only:
 *   src_bpp ==  8 && dst_bpp ==  1
 *   src_bpp ==  8 && dst_bpp ==  8
 *   src_bpp == 16 && dst_bpp == 16
 * are supported.
 */
int gx_downscaler_init(gx_downscaler_t   *ds,
                       gx_device         *dev,
                       int                src_bpc,
                       int                dst_bpc,
                       int                num_comps,
                       int                factor,
                       int                mfs,
                       int              (*adjust_width_proc)(int, int),
                       int                adjust_width);

int gx_downscaler_init_ets(gx_downscaler_t   *ds,
                           gx_device         *dev,
                           int                src_bpc,
                           int                dst_bpc,
                           int                num_comps,
                           int                factor,
                           int                mfs,
                           int              (*adjust_width_proc)(int, int),
                           int                adjust_width,
                           int                ets);

int gx_downscaler_init_trapped(gx_downscaler_t   *ds,
                               gx_device         *dev,
                               int                src_bpc,
                               int                dst_bpc,
                               int                num_comps,
                               int                factor,
                               int                mfs,
                               int              (*adjust_width_proc)(int, int),
                               int                adjust_width,
                               int                trap_w,
                               int                trap_h,
                               const int         *comp_order);

int gx_downscaler_init_trapped_ets(gx_downscaler_t   *ds,
                                   gx_device         *dev,
                                   int                src_bpc,
                                   int                dst_bpc,
                                   int                num_comps,
                                   int                factor,
                                   int                mfs,
                                   int              (*adjust_width_proc)(int, int),
                                   int                adjust_width,
                                   int                trap_w,
                                   int                trap_h,
                                   const int         *comp_order,
                                   int                ets);

int gx_downscaler_init_cm(gx_downscaler_t    *ds,
                          gx_device          *dev,
                          int                 src_bpc,
                          int                 dst_bpc,
                          int                 num_comps,
                          int                 factor,
                          int                 mfs,
                          int               (*adjust_width_proc)(int, int),
                          int                 adjust_width,
                          gx_downscale_cm_fn *apply_cm,
                          void               *apply_cm_arg,
                          int                 post_cm_num_comps);

int gx_downscaler_init_cm_ets(gx_downscaler_t    *ds,
                              gx_device          *dev,
                              int                 src_bpc,
                              int                 dst_bpc,
                              int                 num_comps,
                              int                 factor,
                              int                 mfs,
                              int               (*adjust_width_proc)(int, int),
                              int                 adjust_width,
                              gx_downscale_cm_fn *apply_cm,
                              void               *apply_cm_arg,
                              int                 post_cm_num_comps,
                              int                 ets);

int gx_downscaler_init_trapped_cm(gx_downscaler_t    *ds,
                                  gx_device          *dev,
                                  int                 src_bpc,
                                  int                 dst_bpc,
                                  int                 num_comps,
                                  int                 factor,
                                  int                 mfs,
                                  int               (*adjust_width_proc)(int, int),
                                  int                 adjust_width,
                                  int                 trap_w,
                                  int                 trap_h,
                                  const int          *comp_order,
                                  gx_downscale_cm_fn *apply_cm,
                                  void               *apply_cm_arg,
                                  int                 post_cm_num_comps);

int gx_downscaler_init_trapped_cm_ets(gx_downscaler_t    *ds,
                                      gx_device          *dev,
                                      int                 src_bpc,
                                      int                 dst_bpc,
                                      int                 num_comps,
                                      int                 factor,
                                      int                 mfs,
                                      int               (*adjust_width_proc)(int, int),
                                      int                 adjust_width,
                                      int                 trap_w,
                                      int                 trap_h,
                                      const int          *comp_order,
                                      gx_downscale_cm_fn *apply_cm,
                                      void               *apply_cm_arg,
                                      int                 post_cm_num_comps,
                                      int                 ets);

int gx_downscaler_init_trapped_cm_halftone(gx_downscaler_t    *ds,
                                           gx_device          *dev,
                                           int                 src_bpc,
                                           int                 dst_bpc,
                                           int                 num_comps,
                                           int                 factor,
                                           int                 mfs,
                                           int               (*adjust_width_proc)(int, int),
                                           int                 adjust_width,
                                           int                 trap_w,
                                           int                 trap_h,
                                           const int          *comp_order,
                                           gx_downscale_cm_fn *apply_cm,
                                           void               *apply_cm_arg,
                                           int                 post_cm_num_comps,
                                           gx_downscaler_ht_t *ht);

int gx_downscaler_init_planar(gx_downscaler_t      *ds,
                              gx_device            *dev,
                              gs_get_bits_params_t *params,
                              int                   num_comps,
                              int                   factor,
                              int                   mfs,
                              int                   src_bpc,
                              int                   dst_bpc);

int gx_downscaler_init_planar_trapped(gx_downscaler_t      *ds,
                                      gx_device            *dev,
                                      gs_get_bits_params_t *params,
                                      int                   num_comps,
                                      int                   factor,
                                      int                   mfs,
                                      int                   src_bpc,
                                      int                   dst_bpc,
                                      int                   trap_w,
                                      int                   trap_h,
                                      const int            *comp_order);

int gx_downscaler_init_planar_cm(gx_downscaler_t      *ds,
                                 gx_device            *dev,
                                 gs_get_bits_params_t *params,
                                 int                   num_comps,
                                 int                   factor,
                                 int                   mfs,
                                 int                   src_bpc,
                                 int                   dst_bpc,
                                 gx_downscale_cm_fn   *apply_cm,
                                 void                 *apply_cm_arg,
                                 int                   post_cm_num_comps);

int gx_downscaler_init_planar_trapped_cm(gx_downscaler_t      *ds,
                                         gx_device            *dev,
                                         gs_get_bits_params_t *params,
                                         int                   num_comps,
                                         int                   factor,
                                         int                   mfs,
                                         int                   src_bpc,
                                         int                   dst_bpc,
                                         int                   trap_w,
                                         int                   trap_h,
                                         const int            *comp_order,
                                         gx_downscale_cm_fn   *apply_cm,
                                         void                 *apply_cm_arg,
                                         int                   post_cm_num_comps);

int gx_downscaler_getbits(gx_downscaler_t *ds,
                          byte            *out_data,
                          int              row);

int gx_downscaler_get_bits_rectangle(gx_downscaler_t      *ds,
                                     gs_get_bits_params_t *params,
                                     int                   row);

/* Must only fin a device that has been inited (though you can safely
 * fin several times) */
void gx_downscaler_fin(gx_downscaler_t *ds);

int
gx_downscaler_scale(int width, int factor);

int
gx_downscaler_scale_rounded(int width, int factor);

int gx_downscaler_adjust_bandheight(int factor, int band_height);

/* Downscaling call to the process_page (downscaler also works on the
 * rendering threads and chains calls through to supplied options functions).
 */
int gx_downscaler_process_page(gx_device                 *dev,
                               gx_process_page_options_t *options,
                               int                        factor);

/* The following structure is used to hold the configuration
 * parameters for the downscaler.
 */
typedef struct gx_downscaler_params_s
{
    int downscale_factor;
    int min_feature_size;
    int trap_w;
    int trap_h;
    int trap_order[GS_CLIENT_COLOR_MAX_COMPONENTS];
    int ets;
} gx_downscaler_params;

#define GX_DOWNSCALER_PARAMS_DEFAULTS \
{ 1, 0, 0, 0, { 3, 1, 0, 2 } }

enum {
    GX_DOWNSCALER_PARAMS_MFS = 1,
    GX_DOWNSCALER_PARAMS_TRAP = 2,
    GX_DOWNSCALER_PARAMS_ETS = 4
};

int gx_downscaler_read_params(gs_param_list        *plist,
                              gx_downscaler_params *params,
                              int                   features);

int gx_downscaler_write_params(gs_param_list        *plist,
                               gx_downscaler_params *params,
                               int                   features);

#endif