pnm Module

This module defines the format_pnm type for handling PNM (Portable Any Map) image files. PNM files include both ASCII and binary representations for various image types, such as PBM, PGM, and PPM. The module offers functionalities to manipulate, import, and export PNM images, along with image processing.


Uses

  • module~~pnm~~UsesGraph module~pnm pnm module~forimage_parameters forimage_parameters module~pnm->module~forimage_parameters iso_fortran_env iso_fortran_env module~forimage_parameters->iso_fortran_env

Used by

  • module~~pnm~~UsedByGraph module~pnm pnm module~forcolor forcolor module~forcolor->module~pnm module~forimage forimage module~forimage->module~pnm module~forimage->module~forcolor program~demo_color demo_color program~demo_color->module~forimage program~demo_ppm demo_ppm program~demo_ppm->module~forimage program~example26 example26 program~example26->module~forimage program~example27 example27 program~example27->module~forimage program~example28 example28 program~example28->module~forimage program~example29 example29 program~example29->module~forimage program~test test program~test->module~forimage program~test1 test1 program~test1->module~forimage program~test10 test10 program~test10->module~forimage program~test11 test11 program~test11->module~forimage program~test12 test12 program~test12->module~forimage program~test13 test13 program~test13->module~forimage program~test14 test14 program~test14->module~forimage program~test15 test15 program~test15->module~forimage program~test16 test16 program~test16->module~forimage program~test17 test17 program~test17->module~forimage program~test18 test18 program~test18->module~forimage program~test19 test19 program~test19->module~forimage program~test2 test2 program~test2->module~forimage program~test20 test20 program~test20->module~forimage program~test21 test21 program~test21->module~forimage program~test22 test22 program~test22->module~forimage program~test23 test23 program~test23->module~forimage program~test24 test24 program~test24->module~forimage program~test25 test25 program~test25->module~forimage program~test3 test3 program~test3->module~forimage program~test4 test4 program~test4->module~forimage program~test6 test6 program~test6->module~forimage program~test7 test7 program~test7->module~forimage program~test8 test8 program~test8->module~forimage program~test9 test9 program~test9->module~forimage

Derived Types

type, public ::  format_pnm

This type is designed to store and manipulate PNM (Portable Any Map) image files.

Components

Type Visibility Attributes Name Initial
integer(kind=ik), public, dimension(:,:), allocatable :: pixels

Pixel values of the image.

character(len=:), private, allocatable :: comment

Optional comment associated with the image.

character(len=6), private :: encoding

Encoding of the PNM image (ascii or plain, binary or raw).

character(len=3), private :: file_format

File format of the PNM image (pbm, pgm, ppm).

integer, private :: height

Height (number of rows) of the image.

character(len=2), private :: magic_number

Magic number representing the PNM image type (P1, P2, P3, P4, P5, P6).

integer, private :: max_color

Maximum color value of the image. Used for PGM and PPM images.

integer, private :: width

Width (number of columns) of the image.

Type-Bound Procedures

procedure, public :: brighten

Adjust the brightness and darkness of the image.

Read more…
procedure, public :: crop

Crop the image to a specified region.

Read more…
procedure, public :: export_pnm

Write an image to a file.

Read more…
procedure, public :: finalize => deallocate_pnm

Clean up allocated memory for the PNM image.

Read more…
procedure, public :: flip_horizontal

Flip the image horizontally.

Read more…
procedure, public :: flip_vertical

Flip the image vertically.

Read more…
procedure, public :: get_format

Get the encoding of the PNM image.

Read more…
procedure, public :: greyscale

Convert a color image to greyscale.

Read more…
procedure, public :: import_pnm

Read an image from a file.

Read more…
procedure, public :: negative

Invert the colors of the image.

Read more…
procedure, public :: print_info

Display information about the image (dimensions, aspect ratio, etc.).

Read more…
procedure, public :: remove_channels

Remove one or more RGB channels from the image.

Read more…
procedure, public :: resize

Resize the image to a specified size.

Read more…
procedure, public :: rotate

Rotate the image by a specified angle.

Read more…
procedure, public :: set_format

Set the encoding of the PNM image.

Read more…
procedure, public :: set_pnm

Set the attributes of the PNM image.

Read more…
procedure, public :: swap_channels

Swap the RGB channels of the image.

Read more…
procedure, private :: allocate_pixels

Allocate memory for the pixels of the PNM image.

Read more…
procedure, private :: check_pixel_range

Check if the pixel values are within the valid range.

Read more…
procedure, private :: set_comment

Set a comment for the PNM image.

Read more…
procedure, private :: set_file_format

Set the file format of the PNM image.

Read more…
procedure, private :: set_header

Set the header of the PNM image.

Read more…
procedure, private :: set_height

Set the height of the PNM image.

Read more…
procedure, private :: set_magicnumber

Set the magic number of the PNM image.

Read more…
procedure, private :: set_max_color

Set the maximum color value of the PNM image.

Read more…
procedure, private :: set_pixels

Set the pixel values of the PNM image.

Read more…
procedure, private :: set_width

Set the width of the PNM image.

Read more…

Functions

private pure function get_format(this) result(encoding)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Gets the encoding of the PNM image.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(in) :: this

Return Value character(len=:), allocatable


Subroutines

private pure elemental subroutine allocate_pixels(this)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Allocates memory for the pixels of the PNM image.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this

private pure elemental subroutine aspect_ratio(this, ratio)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Calculates the aspect ratio of the image. Required for print_info method.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(in) :: this
real(kind=rk), intent(out) :: ratio

private pure elemental subroutine average_colors(this, avg, avg_red, avg_green, avg_blue)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Calculates the average color values of the image. Required for print_info method.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(in) :: this
real(kind=rk), intent(out), optional :: avg
real(kind=rk), intent(out), optional :: avg_red
real(kind=rk), intent(out), optional :: avg_green
real(kind=rk), intent(out), optional :: avg_blue

private pure elemental subroutine brighten(this, factor)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Brightens or darkens the image. Only supported for PGM and PPM images.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this
integer, intent(in) :: factor

private pure subroutine check_pixel_range(this, pixels)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Checks if the pixel values are within the valid range.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this
integer(kind=ik), intent(in), dimension(:,:) :: pixels

private pure elemental subroutine crop(this, start_row, end_row, start_col, end_col)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Crops the image to a specified region.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this
integer, intent(in) :: start_row
integer, intent(in) :: end_row
integer, intent(in) :: start_col
integer, intent(in) :: end_col

private pure elemental subroutine deallocate_pnm(this)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Deallocates memory for the PNM image.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this

private impure subroutine export_pnm(this, file_name, encoding)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Writes the PNM image to a file.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this
character(len=*), intent(in) :: file_name
character(len=*), intent(in), optional :: encoding

private pure elemental subroutine flip_horizontal(this)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Flips the image horizontally.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this

private pure elemental subroutine flip_vertical(this)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Flips the image vertically.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this

private pure elemental subroutine greyscale(this)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Converts a color image to greyscale. Only supported for PPM images.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this

private impure subroutine import_pnm(this, file_name, file_format, encoding)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Reads a PNM image from a file.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this
character(len=*), intent(in) :: file_name
character(len=3), intent(in) :: file_format
character(len=*), intent(in) :: encoding

private pure elemental subroutine negative(this)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Inverts the colors of the image.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this

private pure elemental subroutine pixel_size(this, pixel_size_kb, pixel_size_mb)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Calculates imgae size in KB and MB. Required for print_info method.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(in) :: this
real(kind=rk), intent(out) :: pixel_size_kb
real(kind=rk), intent(out) :: pixel_size_mb

private impure elemental subroutine print_info(this)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Displays information about the image e.g. dimensions, aspect ratio, etc.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(in) :: this

private subroutine read_header(this, nunit, pos)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Reads the header of the PNM image from a file. Required before reading the pixels from the file.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this
integer, intent(in) :: nunit
integer, intent(out) :: pos

private pure elemental subroutine remove_channels(this, remove_r, remove_g, remove_b)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Removes one or more RGB channels from the image. Only supported for PPM images.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this
logical, intent(in), optional :: remove_r
logical, intent(in), optional :: remove_g
logical, intent(in), optional :: remove_b

private pure elemental subroutine resize(this, new_height, new_width)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Resizes the image to specified dimensions.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this
integer, intent(in) :: new_height
integer, intent(in) :: new_width

private pure elemental subroutine rotate(this, angle)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Rotates the image by a specified angle. Supported angles are 90, 180, 270, -90, -180, -270.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this
integer, intent(in) :: angle

private pure elemental subroutine set_comment(this, comment)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Sets a comment for the PNM image.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this
character(len=*), intent(in) :: comment

private pure elemental subroutine set_file_format(this, file_format)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Sets the file format of the PNM image. Supported values are pbm, pgm, and ppm.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this
character(len=3), intent(in) :: file_format

private pure elemental subroutine set_format(this, encoding)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Sets the encoding of the PNM image. Supported values are ascii or plain and binary or raw.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this
character(len=*), intent(in) :: encoding

private pure elemental subroutine set_header(this, magic_number, width, height, comment, max_color)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Sets the header of the PNM image. The header includes the magic number, width, height, comment, and max_color.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this
character(len=*), intent(in) :: magic_number
integer, intent(in) :: width
integer, intent(in) :: height
character(len=*), intent(in) :: comment
integer, intent(in), optional :: max_color

private pure elemental subroutine set_height(this, height)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Sets the height of the PNM image.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this
integer, intent(in) :: height

private pure elemental subroutine set_magicnumber(this, magic_number)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Sets the magic number of the PNM image. Supported values are P1, P2, P3, P4, P5, and P6.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this
character(len=*), intent(in) :: magic_number

private pure elemental subroutine set_max_color(this, max_color)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Sets the maximum color value of the PNM image. Only required for PGM and PPM images

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this
integer, intent(in) :: max_color

private pure elemental subroutine set_pixel(this, grey, r, g, b, i, j)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Sets the pixel values of the PNM image.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this
integer, intent(in), optional :: grey
integer, intent(in), optional :: r
integer, intent(in), optional :: g
integer, intent(in), optional :: b
integer, intent(in) :: i
integer, intent(in) :: j

private pure subroutine set_pixels(this, pixels)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Sets the pixel values of the PNM image.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this
integer(kind=ik), intent(in), dimension(:,:) :: pixels

private pure subroutine set_pnm(this, encoding, file_format, width, height, max_color, comment, pixels)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Sets the attributes of the PNM image.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this
character(len=*), intent(in) :: encoding
character(len=3), intent(in) :: file_format
integer, intent(in) :: width
integer, intent(in) :: height
integer, intent(in), optional :: max_color
character(len=*), intent(in) :: comment
integer(kind=ik), intent(in), dimension(:,:) :: pixels

private pure elemental subroutine set_width(this, width)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Sets the width of the PNM image.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this
integer, intent(in) :: width

private pure elemental subroutine swap_channels(this, swap)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Swaps the RGB channels of the image. Only supported for PPM images.

Arguments

Type IntentOptional Attributes Name
class(format_pnm), intent(inout) :: this
character(len=*), intent(in) :: swap

private subroutine write_header(this, nunit)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Writes the header of the PNM image to a file. Required before writing the pixels to the file.

Arguments

Type IntentOptional Attributes Name
type(format_pnm), intent(in) :: this
integer, intent(in) :: nunit