summary refs log tree commit diff
path: root/libnetpbm_ug.html
blob: 51da49de8b35441cf19676d9a7367db8630b5a0f (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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>Libnetpbm Image Processing Manual</TITLE>
<META NAME="manual_section" CONTENT="3">
</HEAD>
<BODY>
<H1>Libnetpbm User's Guide</H1>
<?makeman .SH NAME ?>
<?makeman libnetpbm_ug - netpbm sample code ?>

<?makeman .SH DESCRIPTION ?>
<p>The Libnetpbm programming library is part of <a
href="index.html">Netpbm</a>.

<H2>Example</H2>

<p>Here is an example of a C program that uses <b>libnetpbm</b> to read a
Netpbm image input and produce a Netpbm image output.

<pre>
   /* Example program fragment to read a PAM or PNM image
      from stdin, add up the values of every sample in it
      (I don't know why), and write the image unchanged to
      stdout. */

   #include &lt;netpbm/pam.h&gt;

   struct pam inpam, outpam;
   tuple * tuplerow;
   unsigned int row;

   pm_init(argv[0], 0);

   pnm_readpaminit(stdin, &amp;inpam, PAM_STRUCT_SIZE(tuple_type));

   outpam = inpam; outpam.file = stdout;

   pnm_writepaminit(&amp;outpam);

   tuplerow = pnm_allocpamrow(&amp;inpam);

   for (row = 0; row &lt; inpam.height; row++) {
       unsigned int column;
       pnm_readpamrow(&amp;inpam, tuplerow);
       for (column = 0; column &lt; inpam.width; ++column) {
           unsigned int plane;
           for (plane = 0; plane &lt; inpam.depth; ++plane) {
               grand_total += tuplerow[column][plane];
           }
       }
       pnm_writepamrow(&amp;outpam, tuplerow); }

   pnm_freepamrow(tuplerow);

</pre>

<H2>Guide To Using Libnetpbm</H2>

<h3><b>libnetpbm classes</b></h3>

<p>In this section, Guide To Using Libnetpbm, we cover only the PAM functions
in <b>libnetpbm</b>.  As described in <a href="libnetpbm.html">the
introduction to <b>libnetpbm</b></a>, there are four other classes of
image processing functions (PBM, PGM, PPM, PNM).  They are less
important, since you can do everything more easily with the PAM
functions, but if you're working on old programs or need the extra
efficiency those older functions can sometimes provide, you can find
them documented as here: <a href="libpbm.html">PBM Function Manual</a>,
<a href="libpgm.html">PGM Function Manual</a>, <a
href="libppm.html">PPM Function Manual</a>, and <a
href="libpnm.html">PNM Function Manual</a>.

<p>In case you're wondering, what makes the PAM functions easier to use
is:
<ul>
<li>Each function handles all the formats.  It does so without converting
to a common format, so your program can treat the different formats 
differently if it wants.  However, the interface makes it easy for your
program to ignore the differences between the formats if that's what you
want.

<li>The PAM function parameter lists convey most information about the
image with which you're working with a single <b>pam</b> structure,
which you can build once and use over and over, whereas the older
functions require you to pass up to 5 pieces of image information
(height, width, etc.) as separate arguments to every function.
</ul>


<h3 id="initialization">Library Initialization</h3>

<p>Every program that uses the library must initialize the library, i.e. set
up the process to use the library, as described in <a href="libpm.html#initialization">Initialization</a>.  That is the purpose of the call to
<b>pm_init()</b> in the example above.



<H3 id="pamstruct">THE pam STRUCTURE</H3>

<P>The PAM functions take most of their arguments in the form of a
single <B>pam</B> structure.  This is not an opaque object, but just a
convenient way to organize the information upon which most the
functions depend.  So you are free to access or set the elements of
the structure however you want.  But you will find in most cases it is
most convenient to call <B>pnm_readpaminit()</B> or
<B>pnm_writepaminit()</B> to set the members in the <B>pam</B>
structure before calling any other pam functions, and then just to
pass the structure unchanged in all future calls to pam functions.

<p>It depends upon the function to which you pass the structure what members
are inputs, what members are outputs, and what members are irrelevant.

<p>It is possible for a <b>pam</b> structure <em>not to specify</em> some
members, by operation of its <b>len</b> member.  When you supply a <b>pam</b>
structure as an argument to a function, the function has default behavior
defined for unspecified members.  All the functions require that you specify
at least up through <b>maxval</b>, and some require more.

<p>Likewise, a function the returns a <b>pam</b> structure can return only
a subset of the members defined here, according to its setting of the
<b>len</b> member.  But this normally happens only because the library is old
and predates the existence of the omitted members.

<P>The members are:

<DL COMPACT>
<DT><B>size</B>
<DD>
The storage size in bytes of this entire structure.

<DT><B>len</B>

<DD>The length, in bytes, of the information in this structure.  The
information starts in the first byte and is contiguous.  This cannot
be greater than <B>size</B>.  <B>size</B> and <B>len</B> can be used
to make programs compatible with newer and older versions of the
Netpbm libraries.

<DT><B>file</B>

<DD>The file.

<DT><B>format</B>

<DD>The format code of the image, which tells which of the various Netpbm
image formats is being processed.  The following macros stand for those
format codes:

<dl>

<dt><b>PAM_FORMAT</b>

<dd>PAM

<dt><b>RPBM_FORMAT</b>

<dd>raw PBM format

<dt><b>RPGM_FORMAT</b>

<dd>raw PGM format

<dt><b>RPPM_FORMAT</b>

<dd>raw PPM format

<dt><b>PBM_FORMAT</b>

<dd>plain PBM format

<dt><b>PGM_FORMAT</b>

<dd>plain PGM format

<dt><b>PPM_FORMAT</b>

<dd>plain PPM format

</dl>

<p>
There is an important quirk in the meaning of this member when you use
the pam structure to write an image: Only the type portion of it is
meaningful.  A Netpbm format code conveys two pieces of information:
The format type (PBM, PGM, PPM, or PAM) and the plainness (plain PBM
vs raw PBM, etc.).  But when writing, <b>libnetpbm</b> ignores the
plainness part and instead takes the plainness from the
<b>plainformat</b> member.  So <B>PBM_FORMAT</B> and
<B>RPBM_FORMAT</B> are identical when writing.
<p>
This quirk exists for historical purposes; it's necessary for consistency
with the older functions such as <b>pnm_writepnmrow()</b> whose
<i>format</i> and <i>forceplain</i> arguments are analogous.
<p>
Before Netpbm 10.32 (February 2006), <b>libnetpbm</b> did not ignore the
plainness.  This caused many programs to behave poorly, producing plain
format output when they should, for backward compatibility at the very
least, produce raw format output.
<p>
A common way to use this member is to copy it and the
<b>plainformat</b> member from a pam for an input image to a pam for
an output image.  When you do that, your output image will be raw
format regardless of whether your input image was plain or raw, and
this is the conventional behavior of Netpbm programs.

<DT><B>plainformat</B>

<DD>This is a boolean value (0 = false, 1 = true), meaningful only
when writing an image file.  It means to write in the plain (text)
version of the format indicated by <b>format</b> as opposed to the
raw (binary) version.  Note that the format code in <b>format</b>
would appear to completely specify the format, making
<b>plainformat</b> redundant.  But see the description of
<b>format</b> for why that isn't true.
<p>
Until Netpbm 10.32 (February 2006), this was defined a little differently.
The <b>format</b> member did in fact completely identify the format and
<b>plainformat</b> was redundant and existed as a separate member only
for computational speed.  But this was inconsistent with the older
<b>libnetpbm</b> interface (e.g. <b>pnm_writepnm()</b>, and it made it
difficult to write backward compatible programs.  Before Netpbm 10.32,
it affected reading as well as writing.
<p>
<b>libnetpbm</b> image reading functions set this member to false, for your
convenience in building an output image pam from an input image pam.

<DT><B>height</B>

<DD>The height of the image in rows.

<DT><B>width</B>

<DD>The width of the image in number of columns (tuples per row).

<DT><B>depth</B>

<DD>The depth of the image (degree of or number of samples in each tuple).

<DT><B>maxval</B>

<DD>The maxval of the image.  See definitions in <A HREF="pam.html">pam</A>.

<DT><B>bytes_per_sample</B>

<DD>The number of bytes used to represent each sample in the image
file.  See the format definition in <A HREF="pam.html">pam</A>.  This
is entirely redundant with <B>maxval</B>.  It exists as a separate
member for computational speed.

<DT><B>tuple_type</B>

<DD>The tuple type of the image.  See definitions in <A
HREF="pam.html">pam</A>.  Netpbm defines values for the most common
types of visual images, but any value is legal.  There are macros for
these values:

<dl>
<dt><b>PAM_PBM_TUPLETYPE</b>
<dd>black and white image, such as would alternatively be represented by a PBM
image.
<dt><b>PAM_PGM_TUPLETYPE</b>
<dd>grayscale image, such as would alternatively be represented by a PGM
image.
<dt><b>PAM_PPM_TUPLETYPE</b>
<dd>color image, such as would alternatively be represented by a PPM image.
<dt><b>PAM_PBM_ALPHA_TUPLETYPE</b>
<dd>black and white with a transparency (alpha) information.
<dt><b>PAM_PGM_ALPHA_TUPLETYPE</b>
<dd>grayscale with a transparency (alpha) information.
<dt><b>PAM_PPM_ALPHA_TUPLETYPE</b>
<dd>color with a transparency (alpha) information.
</dl>

<DT><B>allocation_depth</B>

<dd>The number of samples for which memory is allocated for any tuple
associated with this PAM structure.  This must be at least as great as
'depth'.  Only the first 'depth' of the samples of a tuple are
meaningful.

<p>The purpose of this is to make it possible for a program to change
the type of a tuple to one with more or fewer planes.

<p>0 means the allocation depth is the same as the image depth.

<dt><b>comment_p</b>

<dd>Pointer to a pointer to a NUL-terminated ASCII string of comments.
When reading an image, this contains the comments from the image's PAM
header; when writing, the image gets these as comments, right after
the magic number line.  The individual comments are delimited by
newlines and are in the same order as in the PAM header.  The &quot;#&quot;
at the beginning of a PAM header line that indicates the line is a comment
is not part of the comment.

<p>On output, NULL means no comments.

<p>On input, libnetpbm mallocs storage for the comments and placed the
pointer at *comment_p.  Caller must free it.  NULL means libnetpbm
does not return comments and does not allocate any storage.

<p>Examples:

<pre>
<code>
    const char * comments;
    ...
    pam.comment_p = &amp;comments;
    pnm_readpaminit(fileP, &amp;pam, PAM_STRUCT_SIZE(comment_p));
    printf(&quot;The comments are:\n&quot;);
    printf(&quot;%s&quot;, comments)
    free(comments);
</code>
</pre>

<pre>
<code>
    const char * comments;
    ...
    comments = strdup(&quot;This is a comment 1\nThis is comment 2\n&quot;);
    pam.comment_p = &amp;comments;
    pnm_writepaminit(&amp;pam);
    free(comments);
</code>
</pre>

<p>This works only for PAM images.  If you read a PNM image, you
always get back a null string.  If you write a PNM image, you always get
an image that contains no comments.

<p>This member does not exist before Netpbm 10.35 (August 2006).  Before that,
there is no way with libnetpbm to get or set comments.  The macro
<B>PAM_HAVE_COMMENT_P</B> is defined in <b>pam.h</b> where the member
exists.

</DL>



<H3 id="plainvsraw">PLAIN VERSUS RAW FORMAT</H3>

<P>The PNM formats each come in two varieties: the older plain (text)
format and the newer raw (binary) format.  There are different format
codes for the plain and raw formats, but which of the two formats the
pnm and pam functions write is independent of the format code you pass
to them.

<P>The pam functions always write raw formats.  If you specify the format
code for a plain format, a pam function assumes instead the raw
version of that format.

<P>The pnm functions choose between plain and raw based on the
<I>forceplain</I> parameter that every write-type pnm function has.
If this boolean value is true, the function writes the plain version
of the format specified by the format code.  If it is false, the
function writes the raw version of the format specified by the format
code.

<P>We are trying to stamp out the older plain formats, so it would be
a wise choice not to write a program that sets <I>forceplain</I> true
under any circumstance.  A user who needs a plain format can use the
<B>pnmtoplainpnm</B> program to convert the output of your program to
plain format.

<H3>Reference</h3>

<p>The <a href="libnetpbm_image.html">Libnetpbm Netpbm Image
Processing Manual</a> describes the the <b>libnetpbm</b> functions for
processing image data.

<p>The <a href="libpm.html">Libnetpbm Utility Manual</a>
describes the functions that are not specifically related to the Netpbm
image formats.

</BODY>
</HTML>