summary refs log tree commit diff
path: root/libpm.html
blob: a60d3fe0c5a6a7cb342f43313aac73f469d79a58 (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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.3//EN">
<html> <head>
<title>Libnetbpm Utility Functions</title>
<meta name="manual_section" content="3">
</head>
<body>
<h1>Libnetpbm Utility Functions</h1>

<p>Updated: 08 March 2024

<p><a href="#toc">Table Of Contents</a>

<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>const char * </b><i>progname</i><b>,</b>
<b>unsigned int </b><i>flags</i>
<b>);</b>

<p>
<b>void pm_proginit(</b>
<b>int * </b><i>argcP</i><b>,</b>
<b>const char ** </b><i>argv</i><b></b>
<b>);</b>

<h4>Description</h4>

<p>All Netpbm programs must call <b>pm_proginit()</b> just after
startup, before they process their arguments.  <b>pm_proginit()</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_proginit</b>.  But such a program
must invoke <b>pm_init()</b>.

<p>By "Netpbm program," we mean a program that is part of
the Netpbm package or is intended to act like one.  <b>pm_proginit()</b>
does things that all Netpbm programs do by convention.  For example,
it scans the argument list for <a href="index.html#commonoptions">
common options</a>, handles them, and removes them from the argument
list.  Ergo, if you want your program to have the same common options
as those in the Netpbm package, you might use <b>pm_proginit()</b>,
and if you don't, you must not.

<p><strong>Note:</strong> The <i>argv</i> argument is supposed to be the POSIX
program arguments.  The conventional way for a program to declare its
arguments is with <b>char **</b>, as opposed to <b>pm_proginit</b>&apos;s
<b>const char **</b> and other functions the program calls (for example,
<b>getopt</b>) may expect them to be declared that way.  Because of
limitations of type compatibility in C, you may get a compiler warning if
you pass a value declared as <b>char **</b> as <b>pm_proginit</b>&apos;s
<i>argv</i> argument.  To avoid that, either declare the &quot;argv&quot;
argument of your program&apos;s main function as <b>const char **</b> (as
Netpbm programs do) or cast it to that (e.g. <kbd>(const char **)argv</kbd>)
before passing it to <b>pm_proginit</b>.
  
<p><b>pm_proginit()</b> is primarily intended for Netpbm developers,
so you should not expect it to have stable function across releases,
and you must go to the comments in the source code to see exactly what
it does.


  
<p>Any program that uses <b>libnetpbm</b> but does not call
<b>pm_proginit</b> (i.e. is not a Netpbm program) must call
<b>pm_init()</b>.  The conventional place to do this is at the very
beginning of the program.  This sets up some program-global variables
for use by the <b>libnetpbm</b> functions.

<p>The <i>progname</i> argument is the program name for
<b>libnetpbm</b> functions to use in messages they issue.  Normally,
you would use argv[0] for this.

<p><i>flags</i> is meaningless, but for forward compatibility, you
must set it to zero.

<p><b>pm_init()</b> and <b>pm_proginit()</b> have been around at least
since Netpbm 9.25 (March 2002).  Another function named
<b>pm_init()</b> exists in older Netpbm, but was for internal use.
Netpbm programs of that era use <b>pbm_init()</b>, etc to do what
<b>pm_proginit()</b> does today.  Today, <b>pbm_init()</b>, etc. exist
for backward compatibility and are identical the <b>pm_proginit()</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.


<h3 id="endian">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 "long int" C type might
be wider than 4 bytes, but the "long" 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).

<h3 id="maxval">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_gamma709(</b>
<b>float</b> <i>intensity</i>
<b>);</b>

<p>
<b>float pm_ungamma709(</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> <a href="index.html#commonoptions">common
option</a> on the command line.  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="liberror.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="liberror.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, "Netpbm message: %s", msg);
    }

    pm_setusermessagefn(&amp;logfilewrite);
    
    pm_message("Message for the message log");
</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="#gamma">Gamma Arithmetic</a>
    <li><a href="#message">Messages</a>
    <li><a href="#system">System Utilities</a>
    <li><a href="#keyword">Keyword Matching</a>
    </ul>
  </ul>


</body> </html>