about summary refs log tree commit diff
path: root/lib/pgm.h
blob: d4655239b7df58ca799bd22f4a2776e5a40910ac (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
/* pgm.h - header file for libpgm portable graymap library
*/

#ifndef _PGM_H_
#define _PGM_H_

#include <netpbm/pm.h>
#include <netpbm/pbm.h>

#ifdef __cplusplus
extern "C" {
#endif
#if 0
} /* to fake out automatic code indenters */
#endif

/* The following definition has nothing to do with the format of a PGM file */
typedef unsigned int gray;

/* Since April 2000, we are capable of reading and generating raw
   (binary) PGM files with maxvals up to 65535.  However, before that
   the maximum (as usually implemented) was 255, and people still want
   to generate files with a maxval of no more than 255 in most cases
   (because then old Netpbm programs can process them, and they're
   only half as big).

   So we keep PGM_MAXMAXVAL = 255, even though it's kind of a misnomer.

   Note that one could always write a file with maxval > PGM_MAXMAXVAL and
   it would just go into plain (text) format instead of raw (binary) format.
   Along with the expansion to 16 bit raw files, we took away that ability.
   Unless you specify 'forceplain' on the pgm_writepgminit() call, it will
   fail if you specify a maxval > PGM_OVERALLMAXVAL.  I made this design
   decision because I don't think anyone really wants to get a plain format
   file with samples larger than 65535 in it.  However, it should be possible
   just to increase PGM_OVERALLMAXVAL and get that old function back for
   maxvals that won't fit in 16 bits.  I think the only thing really
   constraining PGM_OVERALLMAXVAL is the size of the 'gray' data structure,
   which is generally 32 bits.
*/

#define PGM_OVERALLMAXVAL 65535
#define PGM_MAXMAXVAL 255

#define pgm_unnormalize(value, maxval) \
  ((gray)((value + 1e-6) * (maxval) + 0.5))

/* Magic constants. */

#define PGM_MAGIC1 'P'
#define PGM_MAGIC2 '2'
#define RPGM_MAGIC2 '5'
#define PGM_FORMAT (PGM_MAGIC1 * 256 + PGM_MAGIC2)
#define RPGM_FORMAT (PGM_MAGIC1 * 256 + RPGM_MAGIC2)
#define PGM_TYPE PGM_FORMAT

/* For the alpha-mask variant of PGM: */
#define PGM_TRANSPARENT 0

/* Macro for turning a format number into a type number. */

#define PGM_FORMAT_TYPE(f) ((f) == PGM_FORMAT || (f) == RPGM_FORMAT ? PGM_TYPE : PBM_FORMAT_TYPE(f))


/* Declarations of routines. */

void
pgm_init(int *   const argcP,
         char ** const argv);

gray *
pgm_allocrow(unsigned int const cols);

#define pgm_freerow(grayrow) pm_freerow(grayrow)

#define pgm_allocarray( cols, rows ) \
  ((gray**) pm_allocarray( cols, rows, sizeof(gray) ))
#define pgm_freearray( grays, rows ) pm_freearray( (char**) grays, rows )

gray **
pgm_readpgm(FILE * const file,
            int *  const colsP,
            int *  const rowsP,
            gray * const maxvalP);

void
pgm_readpgminit(FILE * const file,
                int *  const colsP,
                int *  const rowsP,
                gray * const maxvalP,
                int *  const formatP);

void
pgm_readpgmrow(FILE * const file,
               gray * const grayrow,
               int    const cols,
               gray   const maxval,
               int    const format);

void
pgm_writepgminit(FILE * const fileP,
                 int    const cols,
                 int    const rows,
                 gray   const maxval,
                 int    const forceplain);

void
pgm_writepgmrow(FILE *       const fileP,
                const gray * const grayrow,
                int          const cols,
                gray         const maxval,
                int          const forceplain);

void
pgm_writepgm(FILE *  const fileP,
             gray ** const grays,
             int     const cols,
             int     const rows,
             gray    const maxval,
             int     const forceplain);

void
pgm_nextimage(FILE * const file, int * const eofP);

void
pgm_check(FILE *               const file,
          enum pm_check_type   const check_type,
          int                  const format,
          int                  const cols,
          int                  const rows,
          gray                 const maxval,
          enum pm_check_code * const retval_p);

#ifdef __cplusplus
}
#endif


#endif /*_PGM_H_*/