Plan 9 from Bell Labs’s /usr/web/sources/contrib/pac/sys/doc/netpbm/ms/libnetpbm_ug.ms

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


.TL
Libnetpbm Image Processing Manual
.SH 1
Libnetpbm User's Guide
.LP
<?makeman .SH NAME ?>
<?makeman libnetpbm_ug \- netpbm sample code ?>
.LP
The Libnetpbm programming library is part of Netpbm.
.SH 2
Example
.LP
.LP
Here is an example of a C program that uses \fBlibnetpbm\fR to read a
Netpbm image input and produce a Netpbm image output.
.DS L
   /* 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 <pam.h>
   struct pam inpam, outpam;
   unsigned int row;
   pnm_init(&argc, argv);
   pnm_readpaminit(stdin, &inpam, PAM_STRUCT_SIZE(tuple_type));
   outpam = inpam; outpam.file = stdout;
   pnm_writepaminit(&outpam);
   tuplerow = pnm_allocpamrow(&inpam);
   for (row = 0; row < inpam.height; row++) {
       unsigned int column;
       pnm_readpamrow(&inpam, tuplerow);
       for (column = 0; column < inpam.width; ++column) {
           unsigned int plane;
           for (plane = 0; plane < inpam.depth; ++plane) {
               grand_total += tuplerow[column][plane];
           }
       }
       pnm_writepamrow(&outpam, tuplerow); }
   pnm_freepamrow(tuplerow);
.DE
.SH 2
Guide To Using Libnetpbm
.LP
.SH 3
\fBlibnetpbm classes\fR
.LP
.LP
In this section, we cover only the PAM functions in
\fBlibnetpbm\fR.  As described in the
introduction to \fBlibnetpbm\fR, 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: PBM Function Manual,
PGM Function Manual,PPM Function Manual, and PNM Function Manual.
.LP
In case you're wondering, what makes the PAM functions easier to use
is:
.IP \(bu
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.
.IP \(bu
The PAM function parameter lists convey most information about the
image with which you're working with a single \fBpam\fR 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.
.LP
.SH 3
THE pam STRUCTURE
.LP
.LP
The PAM functions take most of their arguments in the form of a
single \fBpam\fR 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 \fBpnm_readpaminit()\fR or
\fBpnm_writepaminit()\fR to set the fields in the \fBpam\fR
structure before calling any other pam functions, and then just to
pass the structure unchanged in all future calls to pam functions.
.LP
The fields are:
.RS
.IP "\fBsize\fR"
The storage size in bytes of this entire structure.
.IP "\fBlen\fR"
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 \fBsize\fR.  \fBsize\fR and \fBlen\fR can be used
to make programs compatible with newer and older versions of the
Netpbm libraries.
.IP "\fBfile\fR"
The file.
.IP "\fBformat\fR"
The format code of the image.  This is \fBPAM_FORMAT\fR
unless the PAM image is really a view of a PBM, PGM, or PPM image.
Then it's \fBPBM_FORMAT\fR, \fBRPBM_FORMAT\fR, etc.
.LP
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, \fBlibnetpbm\fR ignores the
plainness part and instead takes the plainness from the
\fBplainformat\fR member.  So \fBPBM_FORMAT\fR and
\fBRPBM_FORMAT\fR are identical when writing.
.LP
This quirk exists for historical purposes; it's necessary for consistency
with the older functions such as \fBpnm_writepnmrow()\fR whose
\fIformat\fR and \fIforceplain\fR arguments are analogous.
.LP
Before Netpbm 10.32 (February 2006), \fBlibnetpbm\fR 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.
.LP
A common way to use this member is to copy it and the
\fBplainformat\fR 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.
.IP "\fBplainformat\fR"
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 \fBformat\fR as oppposed to the
raw (binary) version.  Note that the format code in \fBformat\fR
would appear to completely specify the format, making
\fBplainformat\fR redundant.  But see the description of
\fBformat\fR for why that isn't true.
.LP
Until Netpbm 10.32 (Februrary 2006), this was defined a little differently.
The \fBformat\fR member did in fact completely identify the format and
\fBplainformat\fR was redundant and existed as a separate member only
for computational speed.  But this was inconsistent with the older
\fBlibnetpbm\fR interface (e.g. \fBpnm_writepnm()\fR, and it made it
difficult to write backward compatible programs.  Before Netpbm 10.32,
it affected reading as well as writing.
.LP
\fBlibnetpbm\fR image reading functions set this field to false, for your
convenience in building an output image pam from an input image pam.
.IP "\fBheight\fR"
The height of the image in rows.
.IP "\fBwidth\fR"
The width of the image in number of columns (tuples per row).
.IP "\fBdepth\fR"
The depth of the image (degree of or number of samples in each tuple).
.IP "\fBmaxval\fR"
The maxval of the image.  See definitions in pam.
.IP "\fBbytes_per_sample\fR"
The number of bytes used to represent each sample in the image
file.  See the format definition in pam.  This
is entirely redundant with \fBmaxval\fR.  It exists as a separate
member for computational speed.
.IP "\fBtuple_type\fR"
The tuple type of the image.  See definitions in pam.  Netpbm does not define any values for this
except the following, which are used for a PAM image which is really a
view of a PBM, PGM, or PPM image: \fBPAM_PBM_TUPLETYPE\fR,
\fBPAM_PGM_TUPLETYPE\fR, \fBPAM_PPM_TUPLETYPE\fR.
.IP "\fBallocation_depth\fR"
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.
.LP
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.
.LP
0 means the allocation depth is the same as the image depth.
.IP "\fBcomments_p\fR"
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 "#"
at the beginning of a PAM header line that indicates the line is a comment
is not part of the comment.
.LP
On output, NULL means no comments.
.LP
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.
.LP
Examples:
.DS L
    const char * comments;
    ...
    pam.comment_p = &comments;
    pnm_readpaminit(fileP, &pam, PAM_STRUCT_SIZE(comment_p));
    printf("The comments are:\n");
    printf("%s", comments)
    free(comments);
.DE
.DS L
    const char * comments;
    ...
    comments = strdup("This is a comment 1\nThis is comment 2\n");
    pam.comment_p = &comments;
    pnm_writepaminit(&pam);
    free(comments);
.DE
.LP
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.
.LP
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
\fBPAM_HAVE_COMMENT_P\fR is defined in \fBpam.h\fR where the member
exists.
.RE
.SH 3
PLAIN VERSUS RAW FORMAT
.LP
.LP
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.
.LP
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.
.LP
The pnm functions choose between plain and raw based on the
\fIforceplain\fR 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.
.LP
We are trying to stamp out the older plain formats, so it would be
a wise choice not to write a program that sets \fIforceplain\fR true
under any circumstance.  A user who needs a plain format can use the
\fBpnmtoplainpnm\fR program to convert the output of your program to
plain format.
.SH 3
Reference
.LP
.LP
The Libnetpbm Netpbm Image
Processing Manual describes the the \fBlibnetpbm\fR functions for
processing image data.
.LP
The Libnetpbm Utility Manual
describes the functions that are not specifically related to the Netpbm
image formats.

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.