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)  

libpbm2.c
Go to the documentation of this file.
1 /* libpbm2.c - pbm utility library part 2
2 **
3 ** Copyright (C) 1988 by Jef Poskanzer.
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 "libpbm.h"
15 
16 static bit pbm_getbit ARGS((FILE *));
17 static bit
19  FILE* file;
20  {
21  register char ch;
22 
23  do
24  {
25  ch = pbm_getc( file );
26  }
27  while ( ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' );
28 
29  if ( ch != '0' && ch != '1' )
30  pm_error( "junk in file where bits should be" );
31 
32  return ( ch == '1' ) ? 1 : 0;
33  }
34 
35 int
37  FILE* file;
38  {
39  int ich1, ich2;
40 
41  ich1 = getc( file );
42  if ( ich1 == EOF )
43  pm_error( "EOF / read error reading magic number" );
44  ich2 = getc( file );
45  if ( ich2 == EOF )
46  pm_error( "EOF / read error reading magic number" );
47  return ich1 * 256 + ich2;
48  }
49 
50 void
51 pbm_readpbminitrest( file, colsP, rowsP )
52  FILE* file;
53  int* colsP;
54  int* rowsP;
55  {
56  /* Read size. */
57  *colsP = pbm_getint( file );
58  *rowsP = pbm_getint( file );
59  }
60 
61 void
62 pbm_readpbminit( file, colsP, rowsP, formatP )
63  FILE* file;
64  int* colsP;
65  int* rowsP;
66  int* formatP;
67  {
68  /* Check magic number. */
69  *formatP = pbm_readmagicnumber( file );
70  switch ( PBM_FORMAT_TYPE(*formatP) )
71  {
72  case PBM_TYPE:
73  pbm_readpbminitrest( file, colsP, rowsP );
74  break;
75 
76  default:
77  pm_error( "bad magic number - not a pbm file" );
78  }
79  }
80 
81 void
83  FILE* file;
84  bit* bitrow;
85  int cols, format;
86  {
87  register int col, bitshift;
88  register bit* bP;
89 
90  switch ( format )
91  {
92  case PBM_FORMAT:
93  for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
94  *bP = pbm_getbit( file );
95  break;
96 
97  case RPBM_FORMAT: {
98  register unsigned char item;
99  bitshift = -1; item = 0; /* item's value is meaningless here */
100  for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
101  {
102  if ( bitshift == -1 )
103  {
104  item = pbm_getrawbyte( file );
105  bitshift = 7;
106  }
107  *bP = ( item >> bitshift ) & 1;
108  --bitshift;
109  }
110  }
111  break;
112 
113  default:
114  pm_error( "can't happen" );
115  }
116  }
117 
118 
119 
120 void
122  unsigned char * const packed_bits,
123  const int cols, const int format) {
124 
125  switch(format) {
126  case PBM_FORMAT: {
127  int col;
128  for (col = 0; col < cols; ++col) {
129  unsigned char mask;
130  mask = pbm_getbit(file) << (7 - col % 8);
131  packed_bits[col / 8] |= mask;
132  }
133  }
134  break;
135 
136  case RPBM_FORMAT: {
137  int bytes_read;
138  bytes_read = fread(packed_bits, 1, pbm_packed_bytes(cols), file);
139 
140  if (bytes_read < pbm_packed_bytes(cols)) {
141  if (feof(file))
142  if (bytes_read == 0)
143  pm_error("Attempt to read a raw PBM image row, but "
144  "no more rows left in file.");
145  else
146  pm_error("EOF in the middle of a raw PBM row.");
147  else
148  pm_error("I/O error reading raw PBM row");
149  }
150  }
151  break;
152 
153  default:
154  pm_error( "Internal error in pbm_readpbmrow_packed." );
155  }
156 }
157 
158 
159 
160 bit**
161 pbm_readpbm( file, colsP, rowsP )
162  FILE* file;
163  int* colsP;
164  int* rowsP;
165  {
166  register bit** bits;
167  int format, row;
168 
169  pbm_readpbminit( file, colsP, rowsP, &format );
170 
171  bits = pbm_allocarray( *colsP, *rowsP );
172 
173  for ( row = 0; row < *rowsP; ++row )
174  pbm_readpbmrow( file, bits[row], *colsP, format );
175 
176  return bits;
177  }
static int item
Definition: brushtopbm.c:66
static int bitshift
Definition: brushtopbm.c:66
#define fread
Definition: xxstdio.h:25
int col
Definition: gsftopk.c:443
unsigned char * bP
Definition: hbf2gf.c:371
static int pbm_getc(FILE *infile)
Definition: rdcolmap.c:121
#define bits
Definition: infblock.c:15
#define EOF
Definition: afmparse.c:59
#define getc
Definition: line.c:39
void pbm_readpbminitrest(FILE *file, int *colsP, int *rowsP)
Definition: libpbm2.c:51
void pbm_readpbmrow_packed(FILE *const file, unsigned char *const packed_bits, const int cols, const int format)
Definition: libpbm2.c:121
static bit pbm_getbit()
bit ** pbm_readpbm(FILE *file, int *colsP, int *rowsP)
Definition: libpbm2.c:161
void pbm_readpbminit(FILE *file, int *colsP, int *rowsP, int *formatP)
Definition: libpbm2.c:62
int pbm_readmagicnumber(FILE *file)
Definition: libpbm2.c:36
void pbm_readpbmrow(FILE *file, bit *bitrow, int cols, int format)
Definition: libpbm2.c:82
unsigned char pbm_getrawbyte(FILE *file)
Definition: libpbm4.c:51
int pbm_getint(FILE *file)
Definition: libpbm4.c:63
#define PBM_TYPE
Definition: pbm.h:21
#define RPBM_FORMAT
Definition: pbm.h:20
#define pbm_packed_bytes(cols)
Definition: pbm.h:39
unsigned char bit
Definition: pbm.h:9
#define pbm_allocarray(cols, rows)
Definition: pbm.h:34
#define PBM_FORMAT
Definition: pbm.h:19
#define PBM_FORMAT_TYPE(f)
Definition: pbm.h:26
static int format
Definition: pbmclean.c:15
static int cols
Definition: pbmmask.c:21
#define ARGS(alist)
Definition: pbmplus.h:235
#define pm_error
Definition: png22pnm.c:118
static int row
Definition: ps2pk.c:587
#define mask(n)
Definition: lbitlib.c:93
Definition: namelist.c:170
Definition: filedef.h:30
#define FILE
Definition: t1stdio.h:34
#define feof(f)
Definition: t1stdio.h:109
ch
Definition: t4ht.c:1443
char * file
Definition: t4ht.c:931