1 /**
2  * pnmio.h
3  * Simple I/O interface to the Portable Any Map (PNM) image file format.
4  * Version 0.3, Release 2008-Jun-15.
5  *
6  * Copyright (C) 2002-2008 Cosmin Truta.
7  *
8  * This software is provided 'as-is', without any express or implied
9  * warranty.  In no event will the author(s) be held liable for any damages
10  * arising from the use of this software.
11  *
12  * Permission is granted to anyone to use this software for any purpose,
13  * including commercial applications, and to alter it and redistribute it
14  * freely, subject to the following restrictions:
15  *
16  * 1. The origin of this software must not be misrepresented; you must not
17  *    claim that you wrote the original software.  If you use this software
18  *    in a product, an acknowledgment in the product documentation would be
19  *    appreciated but is not required.
20  * 2. Altered source versions must be plainly marked as such, and must not be
21  *    misrepresented as being the original software.
22  * 3. This notice may not be removed or altered from any source distribution.
23  *
24  **/
25 
26 
27 #ifndef PNMIO_H
28 #define PNMIO_H
29 
30 #include <limits.h>
31 #include <stddef.h>
32 #include <stdio.h>
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 
39 /**
40  * PNM format codes
41  **/
42 enum
43 {
44     PNM_P1 = 1,  /* plain PBM */
45     PNM_P2 = 2,  /* plain PGM */
46     PNM_P3 = 3,  /* plain PPM */
47     PNM_P4 = 4,  /* raw PBM */
48     PNM_P5 = 5,  /* raw PGM */
49     PNM_P6 = 6,  /* raw PPM */
50     PNM_P7 = 7   /* PAM (only partially implemented) */
51 };
52 
53 
54 /**
55  * PNM info structure
56  **/
57 typedef struct pnm_struct
58 {
59     unsigned int format;
60     unsigned int depth;
61     unsigned int width;
62     unsigned int height;
63     unsigned int maxval;
64 } pnm_struct;
65 
66 
67 /**
68  * PNM input functions
69  **/
70 int pnm_fget_header(pnm_struct *pnm_ptr,
71                     FILE *stream);
72 int pnm_fget_values(const pnm_struct *pnm_ptr,
73                     unsigned int *sample_values,
74                     unsigned int num_rows,
75                     FILE *stream);
76 int pnm_fget_bytes(const pnm_struct *pnm_ptr,
77                     unsigned char *sample_bytes,
78                     size_t sample_size,
79                     unsigned int num_rows,
80                     FILE *stream);
81 
82 
83 /**
84  * PNM output functions
85  **/
86 int pnm_fput_header(const pnm_struct *pnm_ptr,
87                     FILE *stream);
88 int pnm_fput_values(const pnm_struct *pnm_ptr,
89                     const unsigned int *sample_values,
90                     unsigned int num_rows,
91                     FILE *stream);
92 int pnm_fput_bytes(const pnm_struct *pnm_ptr,
93                     const unsigned char *sample_bytes,
94                     size_t sample_size,
95                     unsigned int num_rows,
96                     FILE *stream);
97 
98 
99 /**
100  * PNM utility functions
101  **/
102 int pnm_is_valid(const pnm_struct *pnm_ptr);
103 size_t pnm_raw_sample_size(const pnm_struct *pnm_ptr);
104 size_t pnm_mem_size(const pnm_struct *pnm_ptr,
105                     size_t sample_size,
106                     unsigned int num_rows);
107 
108 
109 /**
110  * PNM limits
111  **/
112 #define PNM_UCHAR_BIT 8
113 #define PNM_UCHAR_MAX 0xffU
114 #if UINT_MAX < 0xffffffffUL
115 #define PNM_UINT_BIT 16
116 #define PNM_UINT_MAX 0xffffU
117 #else
118 #define PNM_UINT_BIT 32
119 #define PNM_UINT_MAX 0xffffffffU
120 #endif
121 
122 
123 #ifdef __cplusplus
124 }  /* extern "C" */
125 #endif
126 
127 #endif  /* PNMIO_H */
128