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)  

libpgm1.c
Go to the documentation of this file.
1 /* libpgm1.c - pgm utility library part 1
2 **
3 ** Copyright (C) 1989 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 "pgm.h"
14 #include "libpgm.h"
15 #include "pbm.h"
16 #include "libpbm.h"
17 
18 void
19 pgm_init( argcP, argv )
20  int* argcP;
21  char* argv[];
22  {
23  pbm_init( argcP, argv );
24  }
25 
26 void
27 pgm_readpgminitrest( file, colsP, rowsP, maxvalP )
28  FILE* file;
29  int* colsP;
30  int* rowsP;
31  gray* maxvalP;
32  {
33  int maxval;
34 
35  /* Read size. */
36  *colsP = pbm_getint( file );
37  *rowsP = pbm_getint( file );
38 
39  /* Read maxval. */
40  maxval = pbm_getint( file );
41  if ( maxval > PGM_OVERALLMAXVAL )
42  pm_error( "maxval is too large. The largest we can handle is %d.",
44  *maxvalP = maxval;
45  }
46 
48 
49 void
50 pgm_readpgminit( file, colsP, rowsP, maxvalP, formatP )
51  FILE* file;
52  int* colsP;
53  int* rowsP;
54  int* formatP;
55  gray* maxvalP;
56  {
57  /* Check magic number. */
58  *formatP = pbm_readmagicnumber( file );
59  switch ( PGM_FORMAT_TYPE(*formatP) )
60  {
61  case PGM_TYPE:
62  pgm_readpgminitrest( file, colsP, rowsP, maxvalP );
63  break;
64 
65  case PBM_TYPE:
66  pbm_readpbminitrest( file, colsP, rowsP );
67  *maxvalP = pgm_pbmmaxval;
68  break;
69 
70  default:
71  pm_error( "bad magic number - not a pgm or pbm file" );
72  }
73  }
74 
75 gray
77 
78  if (maxval < 256) {
79  /* The sample is just one byte. Read it. */
80  return(pbm_getrawbyte(file));
81  } else {
82  /* The sample is two bytes. Read both. */
83  unsigned char byte_pair[2];
84  size_t pairs_read;
85 
86  pairs_read = fread(&byte_pair, 2, 1, file);
87  if (pairs_read == 0)
88  pm_error("EOF /read error while reading a long sample");
89  /* This could be a few instructions faster if exploited the internal
90  format (i.e. endianness) of a pixval. Then we might be able to
91  skip the shifting and oring.
92  */
93  return((byte_pair[0]<<8) | byte_pair[1]);
94  }
95 }
96 
97 
98 
99 #if __STDC__
100 void
102 #else /*__STDC__*/
103 void
105  FILE* file;
106  gray* grayrow;
107  int cols;
108  gray maxval;
109  int format;
110 #endif /*__STDC__*/
111  {
112  register int col;
113  register gray* gP;
114  bit* bitrow;
115  register bit* bP;
116 
117  switch ( format )
118  {
119  case PGM_FORMAT:
120  for ( col = 0, gP = grayrow; col < cols; ++col, ++gP )
121  {
122  *gP = pbm_getint( file );
123 #ifdef DEBUG
124  if ( *gP > maxval )
125  pm_error( "value out of bounds (%u > %u)", *gP, maxval );
126 #endif /*DEBUG*/
127  }
128  break;
129 
130  case RPGM_FORMAT:
131  for ( col = 0; col < cols; ++col) {
133 #ifdef DEBUG
134  if ( grayrow[col] > maxval )
135  pm_error( "value out of bounds (%u > %u)", grawrow[col], maxval );
136 #endif /*DEBUG*/
137  }
138  break;
139 
140  case PBM_FORMAT:
141  case RPBM_FORMAT:
142  bitrow = pbm_allocrow( cols );
143  pbm_readpbmrow( file, bitrow, cols, format );
144  for ( col = 0, gP = grayrow, bP = bitrow; col < cols; ++col, ++gP, ++bP )
145  *gP = ( *bP == PBM_WHITE ) ? maxval : 0;
146  pbm_freerow( bitrow );
147  break;
148 
149  default:
150  pm_error( "can't happen" );
151  }
152  }
153 
154 gray**
155 pgm_readpgm( file, colsP, rowsP, maxvalP )
156  FILE* file;
157  int* colsP;
158  int* rowsP;
159  gray* maxvalP;
160  {
161  gray** grays;
162  int row;
163  int format;
164 
165  pgm_readpgminit( file, colsP, rowsP, maxvalP, &format );
166 
167  grays = pgm_allocarray( *colsP, *rowsP );
168 
169  for ( row = 0; row < *rowsP; ++row )
170  pgm_readpgmrow( file, grays[row], *colsP, *maxvalP, format );
171 
172  return grays;
173  }
static gray maxval
Definition: asciitopgm.c:38
#define fread
Definition: xxstdio.h:25
int col
Definition: gsftopk.c:443
unsigned char * bP
Definition: hbf2gf.c:371
long grayrow[1023]
Definition: hbf2gf.c:383
void pbm_init(int *argcP, argv)
Definition: libpbm1.c:332
void pbm_readpbminitrest(FILE *file, int *colsP, int *rowsP)
Definition: libpbm2.c:51
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_allocrow(cols)
Definition: pbm.h:36
unsigned char bit
Definition: pbm.h:9
#define PBM_FORMAT
Definition: pbm.h:19
#define pbm_freerow(bitrow)
Definition: pbm.h:38
#define PBM_WHITE
Definition: pbm.h:10
static int format
Definition: pbmclean.c:15
static int cols
Definition: pbmmask.c:21
gray ** pgm_readpgm(FILE *file, int *colsP, int *rowsP, gray *maxvalP)
Definition: libpgm1.c:155
gray pgm_getrawsample(FILE *file, const gray maxval)
Definition: libpgm1.c:76
gray pgm_pbmmaxval
Definition: libpgm1.c:47
void pgm_readpgminit(FILE *file, int *colsP, int *rowsP, gray *maxvalP, int *formatP)
Definition: libpgm1.c:50
void pgm_init(int *argcP, argv)
Definition: libpgm1.c:19
void pgm_readpgminitrest(FILE *file, int *colsP, int *rowsP, gray *maxvalP)
Definition: libpgm1.c:27
void pgm_readpgmrow(FILE *file, gray *grayrow, int cols, gray maxval, int format)
Definition: libpgm1.c:104
#define pgm_allocarray(cols, rows)
Definition: pgm.h:57
#define RPGM_FORMAT
Definition: pgm.h:44
#define PGM_FORMAT_TYPE(f)
Definition: pgm.h:50
#define PGM_FORMAT
Definition: pgm.h:43
#define PGM_TYPE
Definition: pgm.h:45
unsigned int gray
Definition: pgm.h:10
#define PGM_OVERALLMAXVAL
Definition: pgm.h:34
#define pm_error
Definition: png22pnm.c:118
static int row
Definition: ps2pk.c:587
Definition: filedef.h:30
#define FILE
Definition: t1stdio.h:34
char * file
Definition: t4ht.c:931
#define argv
Definition: xmain.c:270