about summary refs log tree commit diff
path: root/libpm.html
blob: 17e65c5b31ed440d833b69634aa174c4ebe509f5 (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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html> <head>
<title>Libnetbpm Utility Functions</title>
<meta name="manual_section" content="3">
</head>
<body>
<h1>Libnetpbm Utility Functions</h1>
Updated: 27 August 2006
<BR>
<p><A HREF="#toc">Table Of Contents</A>

<H2>NAME</H2>
libpm - netpbm utility functions
<H2>DESCRIPTION</H2>

<p>These library functions are part of <a href="index.html">Netpbm</a>.

<p>This page documents functions in the Netpbm subroutine library that
are not directly related to image data.

<p>For introductory and general information using <b>libnetpbm</b>, see
<a href="libnetpbm_ug.html">Libnetpbm User's Guide</a>.

<p>The most commonly used <b>libnetpbm</b> functions are those that
read and write and process Netpbm images.  Those are documented in <a
href="libnetpbm_image.html">Libnetpbm Netpbm Image Processing
Manual</a>

<p>To use these services, #include <b>pam.h</b>.

<H2 id="functions">Functions</H2>

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

<h4>Overview</h4>

<p>
<B>void pm_init(</B>
<B>int *</B><I>argcP</I><B>,</B>
<B>char *</B><I>argv</I><B>[]</B>
<B>);</B>

<h4>Description</h4>

<p>All Netpbm programs must call <b>pm_init()</b> just after
startup, before they process their arguments.  <b>pm_init()</b>,
among other things, processes Netpbm universal arguments and removes
them from the argument list.

<p>A program that isn't a Netpbm program, but just uses <b>libnetpbm</b>
services, need not invoke <b>pm_init</b>.

<h3 id="file">File Or Image Stream Access</h3>

<h4>Overview</h4>
<P>
<B>FILE *pm_openr(</b>
<b>char *</B> <I>name</I><B>)</B>

<P>
<B>FILE *pm_openw(</b>
<b>char *</B> <I>name</I>
<B>);</B>

<P>
<B>FILE *pm_openr_seekable(</b>
<b>const char *</B> <I>name</I>
<B>);</B>

<P>
<B>FILE *pm_close(</b>
<b>FILE *</B> <I>fp</I>
<B>);</B>

<P>
<B>void pm_tell2(</b>
<b>FILE * </B> <I>fileP</I><B>,</b>
<b>pm_filepos *</B> <I>fileposP</I><B>,</b>
<b>unsigned int</b> <i>fileposSize</i>
<B>);</B>

<P>
<B>unsigned int pm_tell(</b>
<b>FILE *</B> <I>fileP</I>
<B>);</B>

<P>
<B>void pm_seek2(</b>
<b>FILE * </B> <I>fileP</I><B>,</b>
<b>const pm_filepos *</B> <I>fileposP</I><B>,</b>
<b>unsigned int</b> <i>fileposSize</i>
<B>);</B>

<P>
<B>void pm_seek(</b>
<b>FILE * </B> <I>fileP</I><B>,</b>
<b>unsigned long</B> <I>filepos</I>
<B>);</B>

<P>
<B>char *pm_read_unknown_size(</b>
<b>FILE *</B> <I>fp</I><B>,</b>
<b>long *</B> <I>nread</I>
<B>);</B>


<h4>Description</h4>

<p>An image stream is just a file stream (represented in the standard C
library as type <b>FILE *</b>).

<p>These routines work on files &gt; 2 GiB if the underlying system does,
using the standard large file interface.  Before Netpbm 10.15 (April 2003),
though, they would fail to open any file that large or process any offset
in a file that could not be represented in 32 bits.

<p><B>pm_openr()</B> opens the given file for reading, with
appropriate error checking.  A filename of <B>-</B> is taken to mean
Standard Input.  <B>pm_openw()</B> opens the given file for writing,
with appropriate error checking.  <B>pm_close()</B> closes the file
descriptor, with appropriate error checking.

<P><B>pm_openr_seekable()</B> appears to open the file just like
<B>pm_openr()</B>, but the file thus opened is guaranteed to be
seekable (you can use ftell() and fseek() on it).
<B>pm_openr_seekable()</B> pulls this off by copying the entire file
to a temporary file and giving you the handle of the temporary file,
if it has to.  If the file you name is a regular file, it's already
seekable so <B>pm_openr_seekable()</B> just does the same thing as
<B>pm_openr()</B>.

But if it is, say, a pipe, it isn't seekable.  So
<B>pm_openr_seekable()</B> reads the pipe until EOF into a temporary
file, then opens that temporary file and returns the handle of the
temporary file.  The temporary file is seekable.  <P> The file
<B>pm_openr_seekable()</B> creates is one that the operating system
recognizes as temporary, so when you close the file, by any means, it
gets deleted.

<P>
You need a seekable file if you intend to make multiple passes through
the file.  The only alternative is to read the entire image into
memory and work from that copy.  That may use too much memory.  Note
that the image takes less space in the file cache than in a buffer in
memory.  As much as 96 times less space!  Each sample is an integer in
the buffer, which is usually 96 bits.  In the file, a sample may be as
small as 1 bit and rarely more than 8 bits.

<P><B>pm_tell2()</B> returns a handle for the current position of the
image stream (file), whether it be the header or a row of the raster.
Use the handle as an argument to <B>pm_seek2()</B> to reposition the
file there later.  The file must be seekable (which you can ensure by
opening it with <B>pm_openr_seekable()</B>) or this may fail.

<p>The file position handle is of type <b>pm_filepos</b>, which is
intended to be opaque, i.e. used only with these two functions.  In
practice, it is a file offset and is 32 bits or 64 bits depending upon
the capability of the underlying system.  For maximum backward and
forward compatibility, the functions that take or return a
<b>pm_filepos</b> have a <i>fileposSize</i> argument for the size of
the data structure.  In C, simply code <b>sizeof(pm_filepos)</b> for
that.

<p><b>pm_seek()</b> and <b>pm_tell</b> are for backward compatibility
only.  Do not use them in new code.  These functions are not capable of
handle positions in files whose byte offset cannot be represented in 32
bits.

<p><b>pm_tell2()</b> and <b>pm_seek2()</b> replaced <b>pm_tell()</b> and
<b>pm_seek()</b> in Netpbm 10.15 (April 2003).

<P><B>pm_read_unknown_size()</B> reads an entire file or input stream
of unknown size to a buffer.  It allocates more memory as needed.  The
calling routine has to free the allocated buffer with <B>free()</B>.

<P><B>pm_read_unknown_size()</B> returns a pointer to the allocated
buffer.  The <B>nread</B> argument returns the number of bytes read.


<a name="endian"></a>
<h3>Endian I/O</h3>

<h4>Entry Points</h4>

<P>
<B>void pm_readchar(</b>
<b>FILE *</B> <I>in</I><B>,</b>
<b>char *</B> <I>sP</I>
<B>);</B>

<P>
<B>void pm_writechar(</b>
<b>FILE *</B> <I>out</I><B>,</b>
<b>char</B> <I>s</I>
<B>);</B>

<P>
<B>int pm_readbigshort(</b>
<b>FILE *</B> <I>in</I><B>,</b>
<b>short *</B> <I>sP</I>
<B>);</B>

<P>
<B>int pm_writebigshort(</b>
<b>FILE *</B> <I>out</I><B>,</b>
<b>short</B> <I>s</I>
<B>);</B>

<P>
<B>int pm_readbiglong(</b>
<b>FILE *</B> <I>in</I><B>,</b>
<b>long *</B> <I>lP</I>
<B>);</B>

<P>
<B>int pm_writebiglong(</b>
<b>FILE *</B> <I>out</I><B>,</b>
<b>long</B> <I>l</I>
<B>);</B>

<P>
<B>int pm_readlittleshort(</b>
<b>FILE *</B> <I>in</I><B>,</b>
<b>short *</B> <I>sP</I>
<B>);</B>

<P>
<B>int pm_writelittleshort(</b>
<b>FILE *</B> <I>out</I><B>,</b>
<b>short</B> <I>s</I>
<B>);</B>

<P>
<B>int pm_readlittlelong(</b>
<b>FILE *</B> <I>in</I><B>,</b>
<b>long *</B> <I>lP</I>
<B>);</B>

<P>
<B>int pm_writelittlelong(</b>
<b>FILE *</B> <I>out</I><B>,</b>
<b>long</B> <I>l</I>
<B>);</B>

<P>
<B>void pm_readcharu(</b>
<b>FILE *</B> <I>in</I><B>,</b>
<b>char *</B> <I>sP</I>
<B>);</B>

<P>
<B>void pm_writecharu(</b>
<b>FILE *</B> <I>out</I><B>,</b>
<b>char</B> <I>s</I>
<B>);</B>

<P>
<B>int pm_readbigshortu(</b>
<b>FILE *</B> <I>in</I><B>,</b>
<b>short *</B> <I>sP</I>
<B>);</B>

<P>
<B>int pm_writebigshortu(</b>
<b>FILE *</B> <I>out</I><B>,</b>
<b>short</B> <I>s</I>
<B>);</B>

<P>
<B>int pm_readbiglongu(</b>
<b>FILE *</B> <I>in</I><B>,</b>
<b>long *</B> <I>lP</I>
<B>);</B>

<P>
<B>int pm_writebiglongu(</b>
<b>FILE *</B> <I>out</I><B>,</b>
<b>long</B> <I>l</I>
<B>);</B>

<P>
<B>int pm_readlittleshortu(</b>
<b>FILE *</B> <I>in</I><B>,</b>
<b>short *</B> <I>sP</I>
<B>);</B>

<P>
<B>int pm_writelittleshortu(</b>
<b>FILE *</B> <I>out</I><B>,</b>
<b>short</B> <I>s</I>
<B>);</B>

<P>
<B>int pm_readlittlelongu(</b>
<b>FILE *</B> <I>in</I><B>,</b>
<b>long *</B> <I>lP</I>
<B>);</B>

<P>
<B>int pm_writelittlelongu(</b>
<b>FILE *</B> <I>out</I><B>,</b>
<b>long</B> <I>l</I>
<B>);</B>

<h4>Description</h4>

<B>pm_readchar()</B>, <B>pm_writechar()</B>, <B>pm_readbigshort()</B>,
<B>pm_writebigshort()</B>, <B>pm_readbiglong()</B>,
<B>pm_writebiglong()</B>, <B>pm_readlittleshort()</B>,
<B>pm_writelittleshort()</B>, <B>pm_readlittlelong()</B>, and
<B>pm_writelittlelong()</B> are routines to read and write 1-byte,
2-byte, and 4-byte pure binary integers in either big- or
little-endian byte order.  Note that a &quot;long int&quot; C type might
be wider than 4 bytes, but the &quot;long&quot; routines still read and
write 4 bytes.

<p><b>pm_readbiglongu()</b>, etc. (names ending in <b>u</b>) are the same
except they work on unsigned versions of the type.

<P>The routines with declared return values always return 0.  Before
Netpbm 10.27 (March 2005), they returned -1 on failure, including EOF.
Now, they issue an error message to Standard Error and abort the program
if the I/O fails or encounters EOF.

<p>The 1-byte routines were new in Netpbm 10.27 (March 2005).
The unsigned versions were new somewhere around Netpbm 10.21 (2004).

<A NAME="maxval">&nbsp;</A>
<H3>Maxval Arithmetic</H3>

<h4>Entry Points</h4>
<P>
<B>int pm_maxvaltobits(</b>
<b>int</B> <I>maxval</I>
<B>);</B>

<P>
<B>int pm_bitstomaxval(</b>
<b>int</B> <I>bits</I>
<B>);</B>

<P>
<B>unsigned int pm_lcm(</b>
<b>unsigned int</B> <I>x</I><B>,</B>
<B>unsigned int</B> <I>y</I><B>,</B>
<B>unsigned int</B> <I>z</I><B>,</B>
<B>unsigned int</B> <I>limit</I>
<B>);</B>

<h4>Description</h4>

<p><B>pm_maxvaltobits()</B> and <B>pm_bitstomaxval()</B> convert
between a maxval and the minimum number of bits required to hold it.

<P><B>pm_lcm()</B> computes the least common multiple of 3 integers.
You also specify a limit and if the LCM would be higher than that
limit, <B>pm_lcm()</B> just returns that limit.

<H3 id="gamma">Gamma Arithmetic</H3>

<h4>Entry Points</h4>
<P>
<B>float pm_gamma(</b>
<b>float</B> <I>intensity</I>
<B>);</B>

<P>
<B>float pm_ungamma(</b>
<b>float</B> <I>brightness</I>
<B>);</B>


<h4>Description</h4>

<p>In graphics processing, there are two common ways of representing
numerically the intensity of a pixel, or a component of a pixel.

<p>The obvious way is with a number that is directly proportional to
the light intensity (e.g. 10 means twice as many milliwatts per square
centimeter as 5).  There are two problems with this:

<ul>
  <li>To the human eye, a 1 milliwatt per square centimeter difference
       in a bright image is much less apparent than a 1 milliwatt per
       square centimeter difference in a dark image.  So if you have
       a fixed number of bits in which to store the intensity value,
       you're wasting resolution at the bright end and skimping on it at
       the dark end.
  <li>Monitor inputs and camera outputs aren't directly proportional to
       the light intensity they project or detect.
</ul>

<p>For these reasons, light intensities are often represented in
graphics processing by an exponential scale.  The transfer function is
called a gamma function and the resulting numbers are called
gamma-corrected or gamma-adjusted.  There are various gamma functions.
The Netpbm formats specify that intensities are represented by
gamma-adjusted numbers of a particular gamma transfer function.

<p>These functions let you convert back and forth between these two
scales, using the same gamma transfer function that is specified in the
Netpbm format specifications.

<p><b>pm_gamma709</b> converts from an intensity-proportional intensity
value to a gamma-adjusted intensity value (roughly proportional to
brightness, which is the human subjective perception of intensity),
using the ITU-R Recommendation BT.709 gamma transfer function.

<P><B>pm_ungamma709</b> is the inverse of <b>pm_gamma709</b>.

<H3 id="message">Messages</H3>

<h4>Overview</h4>

<P>
<B>void pm_message(</b>
<b>char *</B> <I>fmt</I><B>,</B>
<B>... );</B>

<p>
<B>void pm_setusermessagefn(pm_usermessagefn *</B> <I>function</I><B>);</b>

<h4>Description</h4>

<p><B>pm_message()</B> is a <B>printf()</B> style routine to write an
informational message to the Standard Error file stream.
<B>pm_message()</B> suppresses the message, however, if the user
specified the <B>-quiet</B> option on the command line.  See the
initialization functions, e.g. <B>pnm_init()</B>, for information on
the <B>-quiet</B> option.  Note that Netpbm programs are often used
interactively, but also often used by programs.  In the interactive
case, it is nice to issue messages about what the program is doing,
but in the program case, such messages are usually undesirable.  By
using <B>pm_message()</B> for all your messages, you make your program
usable in both cases.  Without any effort on your part, program users
of your program can avoid the messages by specifying the <B>-quiet</B>
option.

<p>Netpbm distinguishes between error messages and information
messages; <b>pm_message()</b> is just for informational messages.  To
issue an error message, see
<a href="error.html#pm_errormsg"><b>pm_errormsg()</b></a>.

<p><b>pm_setusermessagefn</b> registers a handler for informational
messages, called a user message routine.  Any library function
(including <b>pm_message()</b>) that wants to issue an informational
message in the future will call that function with the message as an
argument instead of writing the message to Standard Error.

<p>The argument the user message routine gets is English text designed
for human reading.  It is just the text of the message; there is no
attempt at formatting in it (so you won't see any newline or tab
characters).

<p>To capture error messages in addition to informational messages,
see
<a href="error.html#pm_setusererrormsgfn"><b>pm_setusererrormsgfn()</b></a>.

<p>You can remove the user message routine, so that the library issues
future informational messages in its default way (write to Standard
Error) by specifying a null pointer for <i>function</i>.

Example:

<pre>
<code>
    static pm_usermessagefn logfilewrite;

    static void
    logfilewrite(const char * const msg) {
        fprintf(mymsglog, &quot;Netpbm message: %s&quot;, msg);
    }

    pm_setusermessagefn(&amp;logfilewrite);
    
    pm_message(&quot;Message for the message log&quot;);
</code>
</pre>


<h3 id="system">System Utilities</h3>

<ul>
<li><a href="libsystem.html">pm_system</a>
<li><a href="libtmpfile.html">pm_tmpfile</a>
</ul>

<H3 id="keyword">Keyword Matching</H3>

<h4>Entry Points</h4>

<P>
<B>void pm_keymatch();</b>

<h4>Description</h4>

<p>This subroutine is obsolete.  It used to be used for command line
option processing.  Today, you can do better option processing more
easily with the shhopt facility.  See any recent program in the Netpbm
package for an example.

<B>pm_keymatch()</B> does a case-insensitive match of <B>str</B>
against <B>keyword</B>.  <B>str</B> can be a leading substring of
<B>keyword</B>, but at least <B>minchars</B> must be present.


<HR>
<H2 id="toc">Table Of Contents</H2>

<ul>
  <li><a href="#functions">Functions</a>
  <ul>
    <li><a href="#initialization">Initialization</a>
    <li><a href="#file">File Or Image Stream Access</a>
    <li><a href="#endian">Endian I/O</a>
    <li><a href="#maxval">Maxval Arithmetic</a>
    <li><a href="#message">Messages</a>
    <li><a href="#keyword">Keyword</a>
    </ul>
  </ul>


</body> </html>