1 /* Copyright (C) 2001-2019 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,
8    modified or distributed except as expressly authorized under the terms
9    of the license contained in the file LICENSE in this distribution.
10 
11    Refer to licensing information at http://www.artifex.com or contact
12    Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
13    CA 94945, U.S.A., +1(415)492-9861, for further information.
14 */
15 
16 
17 /* CCITTFax filter state definition */
18 /* Requires strimpl.h */
19 
20 #ifndef scfx_INCLUDED
21 #  define scfx_INCLUDED
22 
23 #include "shc.h"
24 
25 /* Common state */
26 #define stream_CF_state_common\
27         stream_hc_state_common;\
28                 /* The client sets the following before initialization. */\
29         bool Uncompressed;\
30         int K;\
31         bool EndOfLine;\
32         bool EncodedByteAlign;\
33         int Columns;\
34         int Rows;\
35         bool EndOfBlock;\
36         bool BlackIs1;\
37         bool ErrsAsEOD;			/* true if we should treat errors as EOD */\
38         int DamagedRowsBeforeError;	/* (Decode only) */\
39         /*bool FirstBitLowOrder;*/	/* in stream_hc_state_common */\
40         int DecodedByteAlign;\
41                 /* The init procedure sets the following. */\
42         uint raster;\
43         byte *lbuf;		/* current scan line buffer */\
44                                 /* (only if decoding or 2-D encoding) */\
45         byte *lprev;		/* previous scan line buffer (only if 2-D) */\
46                 /* The following are updated dynamically. */\
47         int k_left		/* number of next rows to encode in 2-D */\
48                                 /* (only if K > 0) */
49 typedef struct stream_CF_state_s {
50     stream_CF_state_common;
51 } stream_CF_state;
52 
53 /* Define common default parameter setting. */
54 #define s_CF_set_defaults_inline(ss)\
55   ((ss)->Uncompressed = false,\
56    (ss)->K = 0,\
57    (ss)->EndOfLine = false,\
58    (ss)->EncodedByteAlign = false,\
59    (ss)->Columns = 1728,\
60    (ss)->Rows = 0,\
61    (ss)->EndOfBlock = true,\
62    (ss)->BlackIs1 = false,\
63                 /* Added by Adobe since the Red Book */\
64    (ss)->DamagedRowsBeforeError = 0, /* always set, for s_CF_get_params */\
65    (ss)->FirstBitLowOrder = false,\
66                 /* Added by us */\
67    (ss)->DecodedByteAlign = 1,\
68    (ss)->ErrsAsEOD = false,\
69         /* Clear pointers */\
70    (ss)->lbuf = 0, (ss)->lprev = 0,\
71         /* Clear errors */\
72    (ss)->error_string[0] = 0)
73 
74 /* CCITTFaxEncode */
75 typedef struct stream_CFE_state_s {
76     stream_CF_state_common;
77     /* The init procedure sets the following. */
78     int max_code_bytes;		/* max # of bytes for an encoded line */
79     byte *lcode;		/* buffer for encoded output line */
80     /* The following change dynamically. */
81     int read_count;		/* # of bytes to copy into lbuf */
82     int write_count;		/* # of bytes to copy out of lcode */
83     int code_bytes;		/* # of occupied bytes in lcode */
84 } stream_CFE_state;
85 
86 #define private_st_CFE_state()	/* in scfe.c */\
87   gs_private_st_ptrs3(st_CFE_state, stream_CFE_state, "CCITTFaxEncode state",\
88     cfe_enum_ptrs, cfe_reloc_ptrs, lbuf, lprev, lcode)
89 #define s_CFE_set_defaults_inline(ss)\
90   (s_CF_set_defaults_inline(ss), (ss)->lcode = 0)
91 extern const stream_template s_CFE_template;
92 
93 /* CCITTFaxDecode */
94 typedef struct stream_CFD_state_s {
95     stream_CF_state_common;
96     int cbit;			/* bits left to fill in current decoded
97                                    byte at lbuf[wpos] (0..7) */
98     int rows_left;		/* number of rows left */
99     int row;			/* current row, first is 0 */
100     int rpos;			/* rptr for copying lbuf to client */
101     int wpos;			/* rlimit/wptr for filling lbuf or
102                                    copying to client */
103     int eol_count;		/* number of EOLs seen so far */
104     byte invert;		/* current value of 'white' for 2-D decoding */
105     int run_color;		/* -1 if processing white run,
106                                    0 if between runs but white is next,
107                                    1 if between runs and black is next,
108                                    2 if processing black run */
109     int damaged_rows;		/* # of consecutive damaged rows preceding
110                                    the current row */
111     bool skipping_damage;	/* true if skipping a damaged row looking
112                                    for EOL */
113     /* The following are not used yet. */
114     int uncomp_run;		/* non-0 iff we are in an uncompressed
115                                    run straddling a scan line (-1 if white,
116                                    1 if black) */
117     int uncomp_left;		/* # of bits left in the run */
118     int uncomp_exit;		/* non-0 iff this is an exit run
119                                    (-1 if next run white, 1 if black) */
120 } stream_CFD_state;
121 
122 #define private_st_CFD_state()	/* in scfd.c */\
123   gs_private_st_ptrs2(st_CFD_state, stream_CFD_state, "CCITTFaxDecode state",\
124     cfd_enum_ptrs, cfd_reloc_ptrs, lbuf, lprev)
125 #define s_CFD_set_defaults_inline(ss)\
126   s_CF_set_defaults_inline(ss)
127 extern const stream_template s_CFD_template;
128 
129 #endif /* scfx_INCLUDED */
130