format_pnm Derived Type

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.

  • 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

procedure, public :: crop

Crop the image to a specified region.

  • 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

procedure, public :: export_pnm

Write an image to a file.

  • 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

procedure, public :: finalize => deallocate_pnm

Clean up allocated memory for the PNM image.

  • 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

procedure, public :: flip_horizontal

Flip the image horizontally.

  • 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

procedure, public :: flip_vertical

Flip the image vertically.

  • 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

procedure, public :: get_format

Get the encoding of the PNM image.

  • 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

procedure, public :: greyscale

Convert a color image to greyscale.

  • 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

procedure, public :: import_pnm

Read an image from a file.

  • 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

procedure, public :: negative

Invert the colors of the image.

  • 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

procedure, public :: print_info

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

  • 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

procedure, public :: remove_channels

Remove one or more RGB channels from the image.

  • 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

procedure, public :: resize

Resize the image to a specified size.

  • 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

procedure, public :: rotate

Rotate the image by a specified angle.

  • 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

procedure, public :: set_format

Set the encoding of the PNM image.

  • 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

procedure, public :: set_pnm

Set the attributes of the PNM image.

  • 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

procedure, public :: swap_channels

Swap the RGB channels of the image.

  • 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

procedure, private :: allocate_pixels

Allocate memory for the pixels of the PNM image.

  • 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

procedure, private :: check_pixel_range

Check if the pixel values are within the valid range.

  • 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

procedure, private :: set_comment

Set a comment for the PNM image.

  • 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

procedure, private :: set_file_format

Set the file format of the PNM image.

  • 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

procedure, private :: set_header

Set the header of the PNM image.

  • 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

procedure, private :: set_height

Set the height of the PNM image.

  • 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

procedure, private :: set_magicnumber

Set the magic number of the PNM image.

  • 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

procedure, private :: set_max_color

Set the maximum color value of the PNM image.

  • 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

procedure, private :: set_pixels

Set the pixel values of the PNM image.

  • 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

procedure, private :: set_width

Set the width of the PNM image.

  • 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

Source Code

   type format_pnm
      character(2)                             , private :: magic_number !! Magic number representing the PNM image type (`P1`, `P2`, `P3`, `P4`, `P5`, `P6`).
      integer                                  , private :: width        !! Width (number of columns) of the image.
      integer                                  , private :: height       !! Height (number of rows) of the image.
      character(:), allocatable                , private :: comment      !! Optional comment associated with the image.
      integer                                  , private :: max_color    !! Maximum color value of the image. Used for PGM and PPM images.
      integer(ik), dimension(:,:), allocatable :: pixels                 !! Pixel values of the image.
      character(3)                             , private :: file_format  !! File format of the PNM image (`pbm`, `pgm`, `ppm`).
      character(6)                             , private :: encoding     !! Encoding of the PNM image (`ascii` or `plain`, `binary` or `raw`).
   contains
      ! Procedures for setting individual attributes
      procedure :: set_format                 !!> Set the encoding of the PNM image.
      procedure, private :: set_file_format   !!> Set the file format of the PNM image.
      procedure, private :: set_magicnumber   !!> Set the magic number of the PNM image.
      procedure, private :: set_width         !!> Set the width of the PNM image.
      procedure, private :: set_height        !!> Set the height of the PNM image.
      procedure, private :: set_comment       !!> Set a comment for the PNM image.
      procedure, private :: set_max_color     !!> Set the maximum color value of the PNM image.
      procedure, private :: set_header        !!> Set the header of the PNM image.
      procedure, private :: allocate_pixels   !!> Allocate memory for the pixels of the PNM image.
      procedure, private :: check_pixel_range !!> Check if the pixel values are within the valid range.
      procedure, private :: set_pixels        !!> Set the pixel values of the PNM image.
      
      ! Procedures for setting individual attributes
      procedure :: get_format                 !!> Get the encoding of the PNM image.

      ! High-level procedures for working with PNM images
      procedure :: set_pnm                    !!> Set the attributes of the PNM image.
      procedure :: print_info                 !!> Display information about the image (dimensions, aspect ratio, etc.).
      procedure :: import_pnm                 !!> Read an image from a file.
      procedure :: export_pnm                 !!> Write an image to a file.
      procedure :: finalize => deallocate_pnm !!> Clean up allocated memory for the PNM image.

      ! Image manipulation procedures
      procedure :: negative                   !!> Invert the colors of the image.             
      procedure :: brighten                   !!> Adjust the brightness and darkness of the image.
      procedure :: swap_channels              !!> Swap the RGB channels of the image.
      procedure :: remove_channels            !!> Remove one or more RGB channels from the image.
      procedure :: greyscale                  !!> Convert a color image to greyscale.
      procedure :: rotate                     !!> Rotate the image by a specified angle.
      procedure :: flip_horizontal            !!> Flip the image horizontally.
      procedure :: flip_vertical              !!> Flip the image vertically.
      procedure :: crop                       !!> Crop the image to a specified region.
      procedure :: resize                     !!> Resize the image to a specified size.
   end type format_pnm