w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

pbmtomacp.c
Go to the documentation of this file.
1 /* pbmtomacp.c - read a portable bitmap and produce a MacPaint bitmap file
2 **
3 ** Copyright (C) 1988 by Douwe vand der Schaaf.
4 **
5 ** Permission to use, copy, modify, and distribute this software and its
6 ** documentation for any purpose and without fee is hereby granted, provided
7 ** that the above copyright notice appear in all copies and that both that
8 ** copyright notice and this permission notice appear in supporting
9 ** documentation. This software is provided "as is" without express or
10 ** implied warranty.
11 */
12 
13 #include "pbm.h"
14 #include "macp.h"
15 
16 #define TRUE 1
17 #define FALSE 0
18 #define EQUAL 1
19 #define UNEQUAL 0
20 
21 static void fillbits ARGS(( bit **bits, bit **bitsr, int top, int left, int bottom, int right ));
22 static void writemacp ARGS(( bit **bits ));
23 static int packit ARGS(( bit *pb, bit *bits ));
24 static void filltemp ARGS(( bit *dest, bit *src ));
25 static void sendbytes ARGS(( bit *pb, register int npb ));
26 static void header ARGS(( void ));
27 
28 static FILE *fdout;
29 
30 int
32 int argc;
33 char *argv[];
34 { FILE *ifp;
35  register bit **bits, **bitsr;
36  int argn, rows, cols;
37  int left,bottom,right,top;
38  int lflg, rflg, tflg, bflg;
39  char name[100];
40  char *usage = "[-l left] [-r right] [-b bottom] [-t top] [pbmfile]";
41 
42 
43  pbm_init( &argc, argv );
44 
45  argn = 1;
46  fdout = stdout;
47  lflg = rflg = tflg = bflg = 0;
48  left = right = top = bottom = 0; /* To quiet compiler warning */
49 
50  while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
51  { switch ( argv[argn][1] )
52  { case 'l':
53  lflg++;
54  argn++;
55  left = atoi( argv[argn] );
56  break;
57 
58  case 'r':
59  rflg++;
60  argn++;
61  right = atoi( argv[argn] );
62  break;
63 
64  case 't':
65  tflg++;
66  argn++;
67  top = atoi( argv[argn] );
68  break;
69 
70  case 'b':
71  bflg++;
72  argn++;
73  bottom = atoi( argv[argn] );
74  break;
75 
76  case '?':
77  default:
78  pm_usage( usage );
79  }
80  ++argn;
81  }
82 
83  if ( argn == argc )
84  { ifp = stdin;
85  strcpy( name, "noname" );
86  }
87  else
88  { ifp = pm_openr( argv[argn] );
89  strcpy( name, argv[argn] );
90  ++argn;
91  }
92 
93  if ( argn != argc )
94  pm_usage( usage );
95 
96  bitsr = pbm_readpbm( ifp, &cols, &rows );
97 
98  pm_close( ifp );
99 
101 
102  if( !lflg )
103  left = 0;
104 
105  if( rflg )
106  { if( right - left >= MAX_COLS )
107  right = left + MAX_COLS - 1;
108  }
109  else
110  right = ( left + MAX_COLS > cols ) ? ( cols - 1 ) : ( left + MAX_COLS - 1 );
111 
112  if( !tflg )
113  top = 0;
114 
115  if( bflg )
116  { if( bottom - top >= MAX_LINES )
117  bottom = top + MAX_LINES - 1;
118  }
119  else
120  bottom = ( top + MAX_LINES > rows ) ?
121  ( rows - 1 ) : ( top + MAX_LINES - 1 );
122 
124  pm_error("error in right (= %d) and/or left (=%d)",right,left );
125  if( bottom <= top || top < 0 || bottom - top + 1 > MAX_LINES )
126  pm_error("error in bottom (= %d) and/or top (=%d)",bottom,top );
127 
128  fillbits( bits, bitsr, top, left, bottom, right );
129 
130  writemacp( bits );
131 
132  exit( 0 );
133 
134 }
135 
136 /* - - - - - - - - - - - - - - - - - - - - - - - - - - */
137 
138 /* centreer het over te zenden plaatje in het MacPaint document
139  *
140  * Het plaatje wordt vanaf al of niet opgegeven (left, bottom)
141  * in een pbm bitmap van de juist macpaint afmetingen gezet,
142  * en eventueel afgekapt.
143  */
144 static void
146 bit **bits, **bitsr;
147 int top, left, bottom, right;
148 { register bit *bi, *bir;
149  register int i, j;
150  register int bottomr, leftr, topr, rightr;
151  int width, height;
152 
153  width = right - left + 1;
154  leftr = (MAX_COLS - width) / 2;
155  rightr = leftr + width - 1;
156 
157  height = bottom - top + 1;
158  topr = ( MAX_LINES - height ) / 2;
159  bottomr = topr + height - 1;
160 
161  for( i = 0; i < topr; i++ )
162  { bi = bits[i];
163  for( j = 0; j < MAX_COLS; j++ )
164  *bi++ = 0;
165  }
166 
167  for( i = topr; i <= bottomr; i++ )
168  { bi = bits[i];
169  { for( j = 0; j < leftr; j++ )
170  *bi++ = 0;
171  bir = bitsr[ i - topr + top ];
172  for( j = leftr; j <= rightr; j++ )
173  *bi++ = bir[j - leftr + left];
174  for( j = rightr + 1; j < MAX_COLS; j++ )
175  *bi++ = 0;
176  } }
177 
178  for( i = bottomr + 1; i < MAX_LINES; i++ )
179  { bi = bits[i];
180  for( j = 0; j < MAX_COLS; j++ )
181  *bi++ = 0;
182  }
183 } /* fillbits */
184 
185 /* - - - - - - - - - - - - - - - - - - - - - - - - - - */
186 
187 static void
189 bit **bits;
190 { register int i;
191  bit pb[MAX_COLS * 2];
192  int npb;
193 
194  header();
195  for( i=0; i < MAX_LINES; i++ )
196  { npb = packit( pb, bits[i] );
197  sendbytes( pb, npb );
198  }
199 } /* writemacp */
200 
201 /* - - - - - - - - - - - - - - - - - - - - - - - - - - */
202 
203 /* pack regel van MacPaint doc in Apple's format
204  * return value = # of bytes in pb
205  */
206 static int
207 packit( pb, bits )
208  bit *pb, *bits;
209 { register int charcount, npb, newcount, flg;
210  bit temp[72];
211  bit *count, *srcb, *destb, save;
212 
213  srcb = bits; destb = temp;
214  filltemp( destb, srcb );
215  srcb = temp;
216  destb = pb;
217  npb = 0;
219  flg = EQUAL;
220  while( charcount ) {
221  save = *srcb++;
222  charcount--;
223  newcount = 1;
224  while( (*srcb == save) && charcount ) {
225  srcb++;
226  newcount++;
227  charcount--;
228  }
229  if( newcount > 2 ) {
230  count = destb++;
231  *count = 257 - newcount;
232  *destb++ = save;
233  npb += 2;
234  flg = EQUAL;
235  } else {
236  if( flg == EQUAL ) {
237  count = destb++;
238  *count = newcount - 1;
239  npb++;
240  } else
241  *count += newcount;
242  while( newcount-- ) {
243  *destb++ = save;
244  npb++;
245  }
246  flg = UNEQUAL;
247  }
248  }
249  return npb;
250 } /* packit */
251 
252 /* - - - - - - - - - - - - - - - - - - - - - - - - - - */
253 
254 static void
256 bit *dest, *src;
257 { register unsigned char ch, zero, acht;
258  register int i, j;
259 
260  zero = '\0';
261  acht = 8;
262  i = BYTES_WIDE;
263  while( i-- )
264  { ch = zero;
265  j = acht;
266  while( j-- )
267  { ch <<= 1;
268  if( *src++ )
269  ch++;
270  }
271  *dest++ = ch;
272  }
273 } /* filltemp */
274 
275 /* - - - - - - - - - - - - - - - - - - - - - - - - - - */
276 
277 static void
278 sendbytes( pb, npb )
279 bit *pb;
280 register int npb;
281 { register bit *b;
282 
283  b = pb;
284  while( npb-- )
285  (void) putc( *b++, fdout );
286 } /* sendbytes */
287 
288 /* - - - - - - - - - - - - - - - - - - - - - - - - - - */
289 
290 static void
292 { register int i;
293  register char ch;
294 
295  /* header contains nothing ... */
296  ch = '\0';
297  for(i = 0; i < HEADER_LENGTH; i++ )
298  (void) putc( ch, fdout );
299 } /* header */
int charcount
Definition: afm2pfm.c:108
#define width(a)
Definition: aptex-macros.h:198
#define count(a)
Definition: aptex-macros.h:781
#define name
#define height(a)
Definition: aptex-macros.h:200
#define b
Definition: jpegint.h:372
char * strcpy()
char * temp
Definition: dvidvi.c:137
static void
Definition: fpif.c:118
static char usage[]
Definition: giftopnm.c:59
#define putc
Definition: jbib.h:20
small capitals from c petite p scientific i
Definition: afcover.h:80
#define bits
Definition: infblock.c:15
void exit()
@ right
Definition: annotate.c:15
int int double double double char double char * top
Definition: gdfx.h:19
int int double double double char double char char * bottom
Definition: gdfx.h:20
int atoi(const char *)
#define dest
#define MAX_LINES
Definition: macp.h:8
#define MAX_COLS
Definition: macp.h:10
#define BYTES_WIDE
Definition: macp.h:9
#define HEADER_LENGTH
Definition: macp.h:7
void pm_usage(char *usage)
Definition: libpbm1.c:343
void pbm_init(int *argcP, argv)
Definition: libpbm1.c:332
FILE * pm_openr(char *name)
Definition: libpbm1.c:600
bit ** pbm_readpbm(FILE *file, int *colsP, int *rowsP)
Definition: libpbm2.c:161
unsigned char bit
Definition: pbm.h:9
#define pbm_allocarray(cols, rows)
Definition: pbm.h:34
static int rows
Definition: pbmclean.c:15
static int cols
Definition: pbmmask.c:21
#define ARGS(alist)
Definition: pbmplus.h:235
static FILE * fdout
Definition: pbmtomacp.c:28
#define EQUAL
Definition: pbmtomacp.c:18
static void fillbits()
#define UNEQUAL
Definition: pbmtomacp.c:19
static void header()
Definition: pbmtomacp.c:291
static void writemacp()
static void sendbytes()
static int packit()
static void filltemp()
int main(int argc, argv)
Definition: pbmtomacp.c:31
#define zero
#define pm_error
Definition: png22pnm.c:118
#define pm_close(file)
Definition: png22pnm.c:120
lft_cell * left
Definition: routines.h:73
Definition: namelist.c:170
static unsigned char * save
Definition: t1disasm.c:278
#define FILE
Definition: t1stdio.h:34
ch
Definition: t4ht.c:1443
int j
Definition: t4ht.c:1589
FILE * ifp
Definition: t1asm.c:88
#define argv
Definition: xmain.c:270
#define argc
Definition: xmain.c:269
#define argn