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.
This type is designed to store and manipulate PNM (Portable Any Map) image files.
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 ( |
|||
character(len=3), | private | :: | file_format |
File format of the PNM image ( |
|||
integer, | private | :: | height |
Height (number of rows) of the image. |
|||
character(len=2), | private | :: | magic_number |
Magic number representing the PNM image type ( |
|||
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. |
procedure, public :: brighten | Adjust the brightness and darkness of the image. |
procedure, public :: crop | Crop the image to a specified region. |
procedure, public :: export_pnm | Write an image to a file. |
procedure, public :: finalize => deallocate_pnm | Clean up allocated memory for the PNM image. |
procedure, public :: flip_horizontal | Flip the image horizontally. |
procedure, public :: flip_vertical | Flip the image vertically. |
procedure, public :: get_format | Get the encoding of the PNM image. |
procedure, public :: greyscale | Convert a color image to greyscale. |
procedure, public :: import_pnm | Read an image from a file. |
procedure, public :: negative | Invert the colors of the image. |
procedure, public :: print_info | Display information about the image (dimensions, aspect ratio, etc.). |
procedure, public :: remove_channels | Remove one or more RGB channels from the image. |
procedure, public :: resize | Resize the image to a specified size. |
procedure, public :: rotate | Rotate the image by a specified angle. |
procedure, public :: set_format | Set the encoding of the PNM image. |
procedure, public :: set_pnm | Set the attributes of the PNM image. |
procedure, public :: swap_channels | Swap the RGB channels of the 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_comment | Set a comment for the PNM image. |
procedure, private :: set_file_format | Set the file format of the PNM image. |
procedure, private :: set_header | Set the header of the PNM image. |
procedure, private :: set_height | Set the height of the PNM image. |
procedure, private :: set_magicnumber | Set the magic number of the PNM image. |
procedure, private :: set_max_color | Set the maximum color value of the PNM image. |
procedure, private :: set_pixels | Set the pixel values of the PNM image. |
procedure, private :: set_width | Set the width of the PNM image. |
Gets the encoding of the PNM image.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(in) | :: | this |
Allocates memory for the pixels of the PNM image.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(inout) | :: | this |
Calculates the aspect ratio of the image. Required for print_info
method.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(in) | :: | this | |||
real(kind=rk), | intent(out) | :: | ratio |
Calculates the average color values of the image. Required for print_info
method.
Type | Intent | Optional | 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 |
Brightens or darkens the image. Only supported for PGM and PPM images.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(inout) | :: | this | |||
integer, | intent(in) | :: | factor |
Checks if the pixel values are within the valid range.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(inout) | :: | this | |||
integer(kind=ik), | intent(in), | dimension(:,:) | :: | pixels |
Crops the image to a specified region.
Type | Intent | Optional | 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 |
Deallocates memory for the PNM image.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(inout) | :: | this |
Writes the PNM image to a file.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(inout) | :: | this | |||
character(len=*), | intent(in) | :: | file_name | |||
character(len=*), | intent(in), | optional | :: | encoding |
Flips the image horizontally.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(inout) | :: | this |
Flips the image vertically.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(inout) | :: | this |
Converts a color image to greyscale. Only supported for PPM images.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(inout) | :: | this |
Reads a PNM image from a file.
Type | Intent | Optional | 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 |
Inverts the colors of the image.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(inout) | :: | this |
Calculates imgae size in KB and MB. Required for print_info
method.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(in) | :: | this | |||
real(kind=rk), | intent(out) | :: | pixel_size_kb | |||
real(kind=rk), | intent(out) | :: | pixel_size_mb |
Displays information about the image e.g. dimensions, aspect ratio, etc.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(in) | :: | this |
Reads the header of the PNM image from a file. Required before reading the pixels from the file.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(inout) | :: | this | |||
integer, | intent(in) | :: | nunit | |||
integer, | intent(out) | :: | pos |
Removes one or more RGB channels from the image. Only supported for PPM images.
Type | Intent | Optional | 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 |
Resizes the image to specified dimensions.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(inout) | :: | this | |||
integer, | intent(in) | :: | new_height | |||
integer, | intent(in) | :: | new_width |
Rotates the image by a specified angle. Supported angles are 90, 180, 270, -90, -180, -270.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(inout) | :: | this | |||
integer, | intent(in) | :: | angle |
Sets a comment for the PNM image.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(inout) | :: | this | |||
character(len=*), | intent(in) | :: | comment |
Sets the file format of the PNM image. Supported values are pbm
, pgm
, and ppm
.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(inout) | :: | this | |||
character(len=3), | intent(in) | :: | file_format |
Sets the encoding of the PNM image. Supported values are ascii
or plain
and binary
or raw
.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(inout) | :: | this | |||
character(len=*), | intent(in) | :: | encoding |
Sets the header of the PNM image. The header includes the magic number, width, height, comment, and max_color.
Type | Intent | Optional | 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 |
Sets the height of the PNM image.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(inout) | :: | this | |||
integer, | intent(in) | :: | height |
Sets the magic number of the PNM image. Supported values are P1
, P2
, P3
, P4
, P5
, and P6
.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(inout) | :: | this | |||
character(len=*), | intent(in) | :: | magic_number |
Sets the maximum color value of the PNM image. Only required for PGM and PPM images
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(inout) | :: | this | |||
integer, | intent(in) | :: | max_color |
Sets the pixel values of the PNM image.
Type | Intent | Optional | 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 |
Sets the pixel values of the PNM image.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(inout) | :: | this | |||
integer(kind=ik), | intent(in), | dimension(:,:) | :: | pixels |
Sets the attributes of the PNM image.
Type | Intent | Optional | 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 |
Sets the width of the PNM image.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(inout) | :: | this | |||
integer, | intent(in) | :: | width |
Swaps the RGB channels of the image. Only supported for PPM images.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(inout) | :: | this | |||
character(len=*), | intent(in) | :: | swap |
Writes the header of the PNM image to a file. Required before writing the pixels to the file.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(format_pnm), | intent(in) | :: | this | |||
integer, | intent(in) | :: | nunit |