libpbm2.c
2.69 KB
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
/* libpbm2.c - pbm utility library part 2
**
** Copyright (C) 1988 by Jef Poskanzer.
**
** 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 "pbm.h"
#include "libpbm.h"
static bit pbm_getbit ARGS((FILE *));
static bit
pbm_getbit( file )
FILE* file;
{
register char ch;
do
{
ch = pbm_getc( file );
}
while ( ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' );
if ( ch != '0' && ch != '1' )
pm_error( "junk in file where bits should be" );
return ( ch == '1' ) ? 1 : 0;
}
int
pbm_readmagicnumber( file )
FILE* file;
{
int ich1, ich2;
ich1 = getc( file );
if ( ich1 == EOF )
pm_error( "EOF / read error reading magic number" );
ich2 = getc( file );
if ( ich2 == EOF )
pm_error( "EOF / read error reading magic number" );
return ich1 * 256 + ich2;
}
void
pbm_readpbminitrest( file, colsP, rowsP )
FILE* file;
int* colsP;
int* rowsP;
{
/* Read size. */
*colsP = pbm_getint( file );
*rowsP = pbm_getint( file );
}
void
pbm_readpbminit( file, colsP, rowsP, formatP )
FILE* file;
int* colsP;
int* rowsP;
int* formatP;
{
/* Check magic number. */
*formatP = pbm_readmagicnumber( file );
switch ( PBM_FORMAT_TYPE(*formatP) )
{
case PBM_TYPE:
pbm_readpbminitrest( file, colsP, rowsP );
break;
default:
pm_error( "bad magic number - not a pbm file" );
}
}
void
pbm_readpbmrow( file, bitrow, cols, format )
FILE* file;
bit* bitrow;
int cols, format;
{
register int col, bitshift;
register unsigned char item;
register bit* bP;
switch ( format )
{
case PBM_FORMAT:
for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
*bP = pbm_getbit( file );
break;
case RPBM_FORMAT:
bitshift = -1;
for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
{
if ( bitshift == -1 )
{
item = pbm_getrawbyte( file );
bitshift = 7;
}
*bP = ( item >> bitshift ) & 1;
--bitshift;
}
break;
default:
pm_error( "can't happen" );
}
}
bit**
pbm_readpbm( file, colsP, rowsP )
FILE* file;
int* colsP;
int* rowsP;
{
register bit** bits;
int format, row;
pbm_readpbminit( file, colsP, rowsP, &format );
bits = pbm_allocarray( *colsP, *rowsP );
for ( row = 0; row < *rowsP; ++row )
pbm_readpbmrow( file, bits[row], *colsP, format );
return bits;
}