about summary refs log tree commit diff
path: root/lib/pm.h
blob: 47cbfe837da53c9e168f689c6c8fb34033066953 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/* pm.h - interface to format-independent part of libpbm.
**
** Copyright (C) 1988, 1989, 1991 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.
*/

#ifndef PM_H_INCLUDED
#define PM_H_INCLUDED

#include <netpbm/pm_config.h>

#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <errno.h>
#include <setjmp.h>
#include <sys/stat.h>
#include <fcntl.h>

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


/* Definitions to make Netpbm programs work with either ANSI C or C
   Classic.

   This is obsolete, as all compilers recognize the ANSI syntax now.

   We are slowly removing all the ARGS invocations from the programs
   (and replacing them with explicit ANSI syntax), but we have a lot
   of programs where we have removed ARGS from the definition but not
   the prototype, and we have discovered that the Sun compiler
   considers the resulting mismatch between definition and prototype
   to be an error.  So we make ARGS create the ANSI syntax
   unconditionally to avoid having to fix all those mismatches.  */

#if 0
#if __STDC__
#define ARGS(alist) alist
#else /*__STDC__*/
#define ARGS(alist) ()
#define const
#endif /*__STDC__*/
#endif
#define ARGS(alist) alist


/* PM_GNU_PRINTF_ATTR lets the GNU compiler check pm_message() and pm_error()
   calls to be sure the arguments match the format string, thus preventing
   runtime segmentation faults and incorrect messages.
*/
#ifdef __GNUC__
#define PM_GNU_PRINTF_ATTR(a,b) __attribute__ ((format (printf, a, b)))
#else
#define PM_GNU_PRINTF_ATTR(a,b)
#endif


/* PURE_FN_ATTR is the attribute you add to a function declaration
   that indicates it's a true function -- has no side effects and return
   value is not influenced by anything except its arguments.
*/
#ifdef __GNUC__
#define PURE_FN_ATTR __attribute__ ((const))
#else
#define PURE_FN_ATTR
#endif


/* S_IRUSR is POSIX, defined in <sys/stat.h> Some old BSD systems and Windows
   systems have S_IREAD instead.  Most Unix today (2011) has both.  In 2011,
   Android has S_IRUSR and not S_IREAD.

   Some Windows has _S_IREAD.

   We're ignoring S_IREAD now to see if anyone misses it.  If there are still
   users that need it, we can handle it here.
*/
#if MSVCRT
  #define PM_S_IWUSR _S_IWRITE
  #define PM_S_IRUSR _S_IREAD
#else
  #define PM_S_IWUSR S_IWUSR
  #define PM_S_IRUSR S_IRUSR
#endif



typedef struct {
    /* Coordinates of a pixel within an image.  Row 0 is the top row.
       Column 0 is the left column.
    */
    unsigned int row;
    unsigned int col;
} pm_pixelcoord;

extern int pm_plain_output;
    /* Output functions are to produce plain (as opposed to raw) format
       regardless of their 'plainformat' arguments.
    */
extern const char * pm_progname;

void 
pm_init(const char * const progname, unsigned int const flags);

void 
pm_proginit(int * const argcP, const char * argv[]);

void
pm_setMessage(int const newState, int * const oldStateP);

int
pm_getMessage(void);

FILE * 
pm_tmpfile(void);

int
pm_tmpfile_fd(void);

void
pm_make_tmpfile(FILE **       const filePP,
                const char ** const filenameP);

void
pm_make_tmpfile_fd(int *         const fdP,
                   const char ** const filenameP);

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

/* Variable-sized arrays definitions. */

char** 
pm_allocarray (int const cols, int const rows, int const size );

void * 
pm_allocrow(unsigned int const cols,
            unsigned int const size);

void 
pm_freearray (char** const its, int const rows);

void 
pm_freerow(void * const row);


/* Obsolete -- use shhopt instead */
int 
pm_keymatch(const char * const str,
            const char * const keyword,
            int          const minchars);


int PURE_FN_ATTR
pm_maxvaltobits(int const maxval);

int PURE_FN_ATTR
pm_bitstomaxval(int const bits);

unsigned int PURE_FN_ATTR
pm_lcm (unsigned int const x, 
        unsigned int const y,
        unsigned int const z,
        unsigned int const limit);

void
pm_setjmpbuf(jmp_buf * const jmpbufP);

void
pm_setjmpbufsave(jmp_buf *  const jmpbufP,
                 jmp_buf ** const oldJmpbufPP);

void
pm_longjmp(void);

void
pm_fork(int *         const iAmParentP,
        pid_t *       const childPidP,
        const char ** const errorP);

void
pm_waitpid(pid_t         const pid,
           int *         const statusP,
           int           const options,
           pid_t *       const exitedPidP,
           const char ** const errorP);


void
pm_waitpidSimple(pid_t const pid);

typedef void pm_usermessagefn(const char * msg);

void
pm_setusermessagefn(pm_usermessagefn * fn);

typedef void pm_usererrormsgfn(const char * msg);

void
pm_setusererrormsgfn(pm_usererrormsgfn * fn);

void PM_GNU_PRINTF_ATTR(1,2)
pm_message (const char format[], ...);     

void PM_GNU_PRINTF_ATTR(1,2)
pm_errormsg(const char format[], ...);

void PM_GNU_PRINTF_ATTR(1,2)
pm_error (const char reason[], ...);       

int
pm_have_float_format(void);

/* Obsolete - use shhopt and user's manual instead */
void 
pm_usage (const char usage[]);             

FILE* 
pm_openr (const char* const name);
         
FILE*    
pm_openw (const char* const name);
         
FILE *
pm_openr_seekable(const char name[]);

void     
pm_close (FILE* const f);

void 
pm_closer (FILE* const f);
          
void      
pm_closew (FILE* const f);



void
pm_readchar(FILE * const ifP,
            char * const cP);

static __inline__ void
pm_readcharu(FILE *          const ifP,
             unsigned char * const cP) {
    pm_readchar(ifP, (char *) cP);
}

void
pm_writechar(FILE * const ofP,
             char   const c);

static __inline__ void
pm_writecharu(FILE *        const ofP,
              unsigned char const c) {
    pm_writechar(ofP, (char) c);
}

int
pm_readbigshort(FILE *  const ifP, 
                short * const sP);

static __inline__ int
pm_readbigshortu(FILE*            const ifP, 
                 unsigned short * const sP) {
    return pm_readbigshort(ifP, (short *) sP);
}

int
pm_writebigshort(FILE * const ofP,
                 short  const s);

static __inline__ int
pm_writebigshortu(FILE *          const ofP,
                  unsigned short  const s) {
    return pm_writebigshort(ofP, (short) s);
}

int
pm_readbiglong(FILE * const ifP, 
               long * const lP);

static __inline__ int
pm_readbiglongu(FILE *          const ifP,
                unsigned long * const lP) {
    return pm_readbiglong(ifP, (long *) lP);
}

int
pm_readbiglong2(FILE * const ifP, 
                int32_t * const lP);

static __inline__ int
pm_readbiglongu2(FILE *     const ifP,
                 uint32_t * const lP) {
    return pm_readbiglong2(ifP, (int32_t *) lP);
}

int
pm_writebiglong(FILE * const ofP,
                long   const l);

static __inline__ int
pm_writebiglongu(FILE *        const ofP,
                 unsigned long const l) {
    return pm_writebiglong(ofP, (long) l);
}

int
pm_readlittleshort(FILE  * const ifP,
                   short * const sP);

static __inline__ int
pm_readlittleshortu(FILE  *          const ifP,
                    unsigned short * const sP) {
    return pm_readlittleshort(ifP, (short *) sP);
}

int
pm_writelittleshort(FILE * const ofP,
                    short  const s);

static __inline__ int
pm_writelittleshortu(FILE *          const ofP,
                     unsigned short  const s) {
    return pm_writelittleshort(ofP, (short) s);
}

int
pm_readlittlelong(FILE * const ifP,
                  long * const lP);

static __inline__ int
pm_readlittlelongu(FILE *          const ifP,
                   unsigned long * const lP) {
    return pm_readlittlelong(ifP, (long *) lP);
}

int
pm_readlittlelong2(FILE *    const ifP,
                   int32_t * const lP);

static __inline__ int
pm_readlittlelong2u(FILE *     const ifP,
                    uint32_t * const lP) {
    return pm_readlittlelong2(ifP, (int32_t *) lP);
}

int
pm_writelittlelong(FILE * const ofP,
                   long   const l);

static __inline__ int
pm_writelittlelongu(FILE *        const ofP,
                    unsigned long const l) {
    return pm_writelittlelong(ofP, (long) l);
}

int 
pm_readmagicnumber(FILE * const ifP);

char* 
pm_read_unknown_size(FILE * const ifP, 
                     long * const buf);

short
pm_bs_short(short const s);

long
pm_bs_long(long const l);

unsigned int
pm_tell(FILE * const fileP);

void
pm_tell2(FILE *       const fileP, 
         void *       const fileposP,
         unsigned int const fileposSize);

void
pm_seek2(FILE *             const fileP, 
         const pm_filepos * const fileposP,
         unsigned int       const fileposSize);

void
pm_seek(FILE * const fileP, unsigned long filepos);

enum pm_check_code {
    PM_CHECK_OK,
    PM_CHECK_UNKNOWN_TYPE,
    PM_CHECK_TOO_LONG,
    PM_CHECK_UNCHECKABLE,
    PM_CHECK_TOO_SHORT
};

enum pm_check_type {
    PM_CHECK_BASIC
};

void
pm_check(FILE *               const file, 
         enum pm_check_type   const check_type, 
         pm_filepos           const need_raster_size,
         enum pm_check_code * const retval_p);

void
pm_drain(FILE *         const fileP,
         unsigned int   const limit,
         unsigned int * const bytesReadP);

char *
pm_arg0toprogname(const char arg0[]);

unsigned int
pm_randseed(void);

unsigned int
pm_parse_width(const char * const arg);

unsigned int
pm_parse_height(const char * const arg);

#ifdef __cplusplus
}
#endif


#endif