summaryrefslogtreecommitdiff
path: root/graphics/sam2p/testfax_main.c
blob: 7b07f59951dd708e4d433386cfc8e6cd4767958b (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
/*
 * testfax_main.c -- CCITTFax(En|De)code usage sample
 * by pts@fazekas.hu at Sat Jul  6 20:03:35 CEST 2002
 * Sun Jul  7 22:25:55 CEST 2002
 *
 * Note that this sample is incomplete since the parameters (such as /K)
 * cannot be set from the command line.
 */

#include "pts_fax.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>

#ifdef __GNUC__
#ifndef __clang__
#pragma implementation
#endif
#endif

#if OBJDEP
#  warning PROVIDES: testfax_main
#endif

static void* gsfax_xalloc(unsigned n) {
  void *ret;
  if ((ret=malloc(n))==0) abort();
  return ret;
}
static void gsfax_free(void *ptr) {
  free(ptr);
}
static void gsfax_memset(void *s, int c, unsigned n) {
  /*return*/ memset(s,c,n);
}
static void gsfax_memcpy(void *dest, const void *src, unsigned n) {
  /*return*/ memcpy(dest, src, n);
}



#if 0
CCITTFaxEncode:

bool Uncompressed=false;
int  K=0;
bool EndOfLine=false;
bool EncodedByteAlign=false;
int  Columns=1728;
int  Rows=0; // Decode only
bool EndOfBlock=true;
bool BlackIs1=false;
int  DamagedRowsBeforeError=0; // Decode only
bool FirstBitLowOrder=false; // GS only
int  DecodedByteAlign=1; // GS only
bool CloseSource;
bool CloseTarget;
#endif

/* GS expected
 * min_left=0
 * bits_left=32
 *
 */

static void work(FILE *f, stream_template const*template_, stream_state *state) {
  unsigned got, pog;
  stream_cursor_read r;
  stream_cursor_write w;
  unsigned char rbuf[4096], wbuf[1000], *hard, *rlimit;
  bool last;

  #if __CHECKER__
    memset(&r, 0, sizeof(r));
    memset(&w, 0, sizeof(w));
  #endif

  template_->init(state);

  r.ptr=rlimit=rbuf-1;
  hard=rbuf+sizeof(rbuf)-1;
  assert(hard-r.ptr>=(int)template_->min_in_size);
  last=false;


  while (1) {
    assert(r.ptr==rbuf-1);
    if (last==false) {
      if (0==(got=fread(rlimit+1, 1, hard-rlimit, f))) last=true;
      rlimit+=got;
    }
    /* if (r.ptr==rlimit) break; */
    w.ptr=wbuf-1;
    w.limit=wbuf+sizeof(wbuf)-1;
    assert(w.limit-w.ptr>=(int)template_->min_out_size);
    r.limit=rlimit;
    pog=template_->process(state, &r, &w, last);
    fprintf(stderr, "pog=%d write=%d last=%d\n", pog, w.ptr-(wbuf-1), last);
    if ((int)pog==PTSFAX_ERRC) {
      fprintf(stderr, "syntax error!\n");
      abort();
    }
    /* assert(last || r.ptr>=rbuf); */
    fwrite(wbuf, 1, w.ptr-(wbuf-1), stdout);
    if (last && (pog==0 || (int)pog==PTSFAX_EOFC)) break;
    if (r.ptr!=rbuf-1) {
      fprintf(stderr, "limit=%d\n", rlimit-r.ptr);
      memmove(rbuf, r.ptr+1, rlimit-r.ptr);
      rlimit=rbuf-1+(rlimit-r.ptr);
      r.ptr=rbuf-1;
    }
  }

  template_->release(state);
}

int main(int argc, char **argv) {
  FILE *f;

  (void)argc;
  (void)argv;
/*
-* Define a limit on the Rows parameter, close to max_int. *-
#define cf_max_height 32000
    if (code >= 0 &&
	(state.K < -cf_max_height || state.K > cf_max_height ||
	 state.Columns < 0 || state.Columns > cfe_max_width ||
	 state.Rows < 0 || state.Rows > cf_max_height ||
	 state.DamagedRowsBeforeError < 0 ||
	 state.DamagedRowsBeforeError > cf_max_height ||
	 state.DecodedByteAlign < 1 || state.DecodedByteAlign > 16 ||
	 (state.DecodedByteAlign & (state.DecodedByteAlign - 1)) != 0)
	)
*/

  f=stdin;
  /* f=fopen("stdpre.x","rb"); */
  /* f=fopen("216","rb"); */
  assert(f!=0);

  if (0) {
  #if USE_BUILTIN_FAXE
  #if OBJDEP
  #  warning REQUIRES: pts_faxe
  #endif
  } else if (argc==2 && 0==strcmp(argv[1], "encode")) {
    stream_CFE_state sCFEs;
    sCFEs.memset_=gsfax_memset;
    sCFEs.xalloc_=gsfax_xalloc;
    sCFEs.free_=gsfax_free;
    sCFEs.memcpy_=gsfax_memcpy;
    s_CFE_template.set_defaults((stream_state*)&sCFEs);
    /* sCFEs.K=-1; ... */
    work(f, (stream_template const*)&s_CFE_template, (stream_state*)&sCFEs);
  #endif
  #if USE_BUILTIN_FAXD
  #if OBJDEP
  #  warning REQUIRES: pts_faxd
  #endif
  } else if (argc==2 && 0==strcmp(argv[1], "decode")) {
    stream_CFD_state sCFDs;
    sCFDs.memset_=gsfax_memset;
    sCFDs.xalloc_=gsfax_xalloc;
    sCFDs.free_=gsfax_free;
    sCFDs.memcpy_=gsfax_memcpy;
    s_CFD_template.set_defaults((stream_state*)&sCFDs);
    /* sCFEs.K=-1; ... */
    work(f, (stream_template const*)&s_CFD_template, (stream_state*)&sCFDs);
  #endif
  } else {
    fprintf(stderr, "Usage: %s encode|decode <INFILE >OUTFILE\n", argv[0]);
    return 2;
  }
  fclose(f);

  return 0;
}