about summary refs log tree commit diff
path: root/converter/pbm/pbmtomacp.c
blob: 600cc407de4b41e14c1b452b9cc569f8bc2843d4 (plain) (blame)
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
/* pbmtomacp.c - read a portable bitmap and produce a MacPaint bitmap file
**
** Copyright (C) 1988 by Douwe vand der Schaaf.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation.  This software is provided "as is" without express or
** implied warranty.
*/

#include <string.h>

#include "pm_c_util.h"
#include "pbm.h"
#include "macp.h"

#define TRUE		1
#define FALSE		0
#define EQUAL		1
#define UNEQUAL		0

#define MIN3(a,b,c)     (MIN((MIN((a),(b))),(c)))

static void fillbits ARGS(( bit **bits, bit **bitsr, int top, int left, int bottom, int right ));
static void writemacp ARGS(( bit **bits ));
static int packit ARGS(( bit *pb, bit *bits ));
static void filltemp ARGS(( bit *dest, bit *src ));
static void sendbytes ARGS(( bit *pb, register int npb ));
static void header ARGS(( void ));

static FILE *fdout;

int
main(argc, argv)
int argc;
char *argv[];
{ FILE *ifp;
  register bit **bits, **bitsr;
  int argn, rows, cols;
  int left,bottom,right,top;
  int lflg, rflg, tflg, bflg;
  const char * const usage = "[-l left] [-r right] [-b bottom] [-t top] [pbmfile]";


  pbm_init( &argc, argv );

  argn = 1;
  fdout = stdout;
  lflg = rflg = tflg = bflg = 0;
  left = right = top = bottom = 0;  /* To quiet compiler warning */

  while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
  { switch ( argv[argn][1] )
    { case 'l':
      lflg++;
      argn++;
      left = atoi( argv[argn] );
      break;

      case 'r':
      rflg++;
      argn++;
      right = atoi( argv[argn] );
      break;

      case 't':
      tflg++;
      argn++;
      top = atoi( argv[argn] );
      break;

      case 'b':
      bflg++;
      argn++;
      bottom = atoi( argv[argn] );
      break;

      case '?':
      default:
      pm_usage( usage );
    }
    ++argn;
  }

  if ( argn == argc )
  { ifp = stdin;
  }
  else
  { ifp = pm_openr( argv[argn] );
    ++argn;
  }

  if ( argn != argc )
    pm_usage( usage );

  bitsr = pbm_readpbm( ifp, &cols, &rows );

  pm_close( ifp );

  bits = pbm_allocarray( MAX_COLS, MAX_LINES );

  if( !lflg )
    left = 0;

  if( rflg )
    right = MIN3( right, cols - 1, left + MAX_COLS - 1 );
  else
    right = MIN( cols - 1,  left + MAX_COLS - 1 );

  if( !tflg )
    top = 0;

  if( bflg )
    bottom = MIN3( bottom, rows - 1, top + MAX_LINES - 1);
  else
    bottom = MIN( rows - 1, top + MAX_LINES - 1 );

    if( right <= left || left < 0 || right - left + 1 > MAX_COLS )
      pm_error("error in right (= %d) and/or left (=%d)",right,left );
    if( bottom <= top || top < 0 || bottom - top + 1 > MAX_LINES )
      pm_error("error in bottom (= %d) and/or top (=%d)",bottom,top );

  fillbits( bits, bitsr, top, left, bottom, right );

  writemacp( bits );

  exit( 0 );

}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - */

/* centreer het over te zenden plaatje in het MacPaint document
 *
 * Het plaatje wordt vanaf al of niet opgegeven (left, bottom)
 * in een pbm bitmap van de juist macpaint afmetingen gezet,
 * en eventueel afgekapt.
 */
static void
fillbits( bits, bitsr, top, left, bottom, right )
bit **bits, **bitsr;
int top, left, bottom, right;
{ register bit *bi, *bir;
  register int i, j;
  register int bottomr, leftr, topr, rightr;
  int width, height;

  width = right - left + 1;
  leftr = (MAX_COLS - width) / 2;
  rightr = leftr + width - 1;

  height = bottom - top + 1;
  topr = ( MAX_LINES - height ) / 2;
  bottomr = topr + height - 1;

  for( i = 0; i < topr; i++ )
  { bi = bits[i];
    for( j = 0; j < MAX_COLS; j++ )
      *bi++ = 0;
  }

  for( i = topr; i <= bottomr; i++ )
  { bi = bits[i];
    { for( j = 0; j < leftr; j++ )
	*bi++ = 0;
      bir = bitsr[ i - topr + top ];
      for( j = leftr; j <= rightr; j++ )
	*bi++ = bir[j - leftr + left];
      for( j = rightr + 1; j < MAX_COLS; j++ )
	*bi++ = 0;
  } }

  for( i = bottomr + 1; i < MAX_LINES; i++ )
  { bi = bits[i];
    for( j = 0; j < MAX_COLS; j++ )
      *bi++ = 0;
  }
} /* fillbits */
      
/* - - - - - - - - - - - - - - - - - - - - - - - - - - */

static void
writemacp( bits )
bit **bits;
{ register int i;
  bit pb[MAX_COLS * 2];
  int npb;

  header();
  for( i=0; i < MAX_LINES; i++ )
  { npb = packit( pb, bits[i] );
    sendbytes( pb, npb );
  }
} /* writemacp */

/* - - - - - - - - - - - - - - - - - - - - - - - - - - */

/* pack regel van MacPaint doc in Apple's format
 * return value = # of bytes in pb 
 */
static int
packit( pb, bits )
     bit *pb, *bits;
{ register int charcount, npb, newcount, flg;
  bit temp[72];
  bit *count, *srcb, *destb, save;

  srcb = bits; destb = temp;
  filltemp( destb, srcb );
  srcb = temp;
  destb = pb;
  npb = 0;
  charcount = BYTES_WIDE;
  flg = EQUAL;
  while( charcount ) { 
      save = *srcb++;
      charcount--;
      newcount = 1;
      while( charcount && (*srcb == save) ) { 
          srcb++;
          newcount++;
          charcount--;
      }
      if( newcount > 2 ) { 
          count = destb++;
          *count = 257 - newcount;
          *destb++ = save;
          npb += 2;
          flg = EQUAL;
      } else { 
          if( flg == EQUAL ) { 
              count = destb++;
              *count = newcount - 1;
              npb++;
          } else
            *count += newcount;
          while( newcount-- ) { 
              *destb++ = save;
              npb++;
          }
          flg = UNEQUAL;
      } 
  }
  return npb;
} /* packit */

/* - - - - - - - - - - - - - - - - - - - - - - - - - - */

static void
filltemp( dest, src )
bit *dest, *src;
{ register unsigned char ch, zero, acht;
  register int i, j;

  zero = '\0';
  acht = 8;
  i = BYTES_WIDE;
  while( i-- )
  { ch = zero; 
    j = acht;
    while( j-- )
    { ch <<= 1;
      if( *src++ )
	ch++;
    }
    *dest++ = ch;
  }
} /* filltemp */

/* - - - - - - - - - - - - - - - - - - - - - - - - - - */

static void
sendbytes( pb, npb )
bit *pb;
register int npb;
{ register bit *b;

  b = pb;
  while( npb-- )
    (void) putc( *b++, fdout );
} /* sendbytes */

/* - - - - - - - - - - - - - - - - - - - - - - - - - - */

static void
header()
{ register int i;
  register char ch;

  /* header contains nothing ... */
  ch = '\0';
  for(i = 0; i < HEADER_LENGTH; i++ )
    (void) putc( ch, fdout );
} /* header */