LIBNETPBM_UG(3)

okay bestie, welcome to libnetpbm_ug(3) β€” the manual for netpbm sample code, and I'm gonna make it make sense, fr fr πŸ’–

πŸŽ€ NAME πŸŽ€

  • libnetpbm_ug β€” netpbm sample code πŸ’…

✨ DESCRIPTION ✨

so the Libnetpbm programming library is literally part of Netpbm(1) 🫢

πŸ“š Contents πŸ“š

here's the vibe check of what's inside, bestie:

  • Example ✨
  • libnetpbm Classes πŸ’…
  • Library Initialization classes πŸŽ€
  • The pam Structure 🧚
  • Plain versus Raw Format 🌈
  • Reference πŸ“–

🌟 Example 🌟

okay so here's an example of a C program that uses libnetpbm to read a Netpbm image input and produce a Netpbm image output. it's giving real main character energy πŸ”₯

          /* 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 <netpbm/pam.h>

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

          pm_init(argv[0], 0);

          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);

πŸ’… libnetpbm Classes πŸ’…

so in this section β€” Guide To Using Libnetpbm β€” we're only covering the PAM functions in libnetpbm, bestie. as described in the introduction to libnetpbm" (1), there are four other classes of image processing functions (PBM, PGM, PPM, PNM). they're lowkey 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 here: PBM Function Manual(1), PGM Function Manual(1), PPM Function Manual(1), and PNM Function Manual(1) πŸ’…

and in case you're wondering, what makes the PAM functions easier to use is:

  • 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 πŸ¦‹
  • the PAM function parameter lists convey most information about the image with which you're working with a single pam 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 ✨

πŸ‘‘ Library Initialization πŸ‘‘

every program that uses the library must initialize the library β€” like, set up the process to use the library β€” as described in Initialization . that's literally the purpose of the call to pm_init() in the example above 🌸

πŸŽ€ The pam Structure πŸŽ€

okay so bestie, the PAM functions are obsessed with taking almost all their arguments as one single pam structure. And the slay part? It's not some opaque mystery object β€” it's literally just a convenient lil organizer for all the info most of the functions depend on. So you can access or set its elements however you want, no gatekeeping. But ngl, in most cases the move is to call pnm_readpaminit() or pnm_writepaminit() first to set the members up, and then just pass the structure along unchanged to every future pam function call. ✨

Which members are inputs, which are outputs, and which are just kinda there vibing? Depends on the function you hand it to. That's the tea. β˜•

It's possible for a pam structure to not specify some members, and that all happens via its len member. When you pass a pam structure to a function, the function has default behavior defined for unspecified members. All the functions require that you specify at least up through maxval, and some require more than that. πŸ’…

Same deal in reverse: a function returning a pam structure can return only a subset of the members defined here, according to its setting of len. But this normally happens only because the library is old and predates the existence of the omitted members. #vintage

The members are:

  • size β€” The storage size in bytes of this entire structure.
  • len β€” 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 size. size and len can be used to make programs compatible with newer and older versions of the Netpbm libraries. 🫢
  • file β€” The file.
  • format β€” 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:
    • PAM_FORMAT β€” PAM
    • RPBM_FORMAT β€” raw PBM format
    • RPGM_FORMAT β€” raw PGM format
    • RPPM_FORMAT β€” raw PPM format
    • PBM_FORMAT β€” plain PBM format
    • PGM_FORMAT β€” plain PGM format
    • PPM_FORMAT β€” plain PPM format

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, libnetpbm ignores the plainness part and instead takes the plainness from the plainformat member. So PBM_FORMAT and RPBM_FORMAT are identical when writing. 😭

This quirk exists for historical purposes; it's necessary for consistency with the older functions such as pnm_writepnmrow() whose format and forceplain arguments are analogous.

Before Netpbm 10.32 (February 2006), libnetpbm 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. πŸ’€

A common way to use this member is to copy it and the plainformat 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. ✨

  • plainformat β€” 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 format as opposed to the raw (binary) version. Note that the format code in format would appear to completely specify the format, making plainformat redundant. But see the description of format for why that isn't true. πŸ‘€

Until Netpbm 10.32 (February 2006), this was defined a little differently. The format member did in fact completely identify the format and plainformat was redundant and existed as a separate member only for computational speed. But this was inconsistent with the older libnetpbm interface (e.g. pnm_writepnm(), and it made it difficult to write backward compatible programs. Before Netpbm 10.32, it affected reading as well as writing.

libnetpbm image reading functions set this member to false, for your convenience in building an output image pam from an input image pam. 🎁

  • height β€” The height of the image in rows.
  • width β€” The width of the image in number of columns (tuples per row).
  • depth β€” The depth of the image (degree of or number of samples in each tuple).
  • maxval β€” The maxval of the image. See definitions in pam(1).
  • bytes_per_sample β€” The number of bytes used to represent each sample in the image file. See the format definition in pam(1). This is entirely redundant with maxval. It exists as a separate member for computational speed. ⚑
  • tuple_type β€” The tuple type of the image. See definitions in pam(1). Netpbm defines values for the most common types of visual images, but any value is legal. There are macros for these values:
    • PAM_PBM_TUPLETYPE β€” black and white image, such as would alternatively be represented by a PBM image.
    • PAM_PGM_TUPLETYPE β€” grayscale image, such as would alternatively be represented by a PGM image.
    • PAM_PPM_TUPLETYPE β€” color image, such as would alternatively be represented by a PPM image.
    • PAM_PBM_ALPHA_TUPLETYPE β€” black and white with a transparency (alpha) information.
    • PAM_PGM_ALPHA_TUPLETYPE β€” grayscale with a transparency (alpha) information.
    • PAM_PPM_ALPHA_TUPLETYPE β€” color with a transparency (alpha) information.
  • allocation_depth β€” 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.

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.

0 means the allocation depth is the same as the image depth.

  • comment_p β€” 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.

On output, NULL means no comments.

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.

Examples:

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);
const char * comments;
...
comments = strdup("This is a comment 1\nThis is comment 2\n");
pam.comment_p = &comments;
pnm_writepaminit(&pam);
free(comments);

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.

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 PAM_HAVE_COMMENT_P is defined in pam.h where the member exists. πŸ¦‹


🌈 Plain Versus Raw Format 🌈

ok so 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. 🀯

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. periodt.

The pnm functions choose between plain and raw based on the forceplain 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. 🫑

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

fr fr bestie, don't set forceplain true. just don't. let the raw formats live. 🌸

πŸ‘‘ Reference πŸ‘‘

The "Libnetpbm Netpbm Image Processing Manual" (1) describes the the libnetpbm functions for processing image data. πŸ“Έ

The Libnetpbm Utility Manual(1) describes the functions that are not specifically related to the Netpbm image formats. 🧰


πŸ“œ DOCUMENT SOURCE πŸ“œ

This manual page was generated by the Netpbm tool makeman from HTML source. The master documentation is at

http://netpbm.sourceforge.net/doc/libnetpbm_ug.html

and that's the whole thing, bestie 🌟 this little love letter came to you straight from the netpbm documentation, living in the Libnetpbm ...sing Manual(3) β€” go off, read the other parts, drink some water, and remember: raw format supremacy, MWAH πŸ’–πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from