Skip to content

Commit

Permalink
src/forcolormap_utils.f90: contains the test_colormap() subroutine us…
Browse files Browse the repository at this point in the history
…ed in several examples
  • Loading branch information
vmagnin committed Feb 21, 2024
1 parent 91a4037 commit 3072e59
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 117 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ All notable changes to the gtk-fortran project are documented in this file. The
* A `reverse` boolean option was added to the methods `set`, `create`, and `load` to reverse a colormap.
* An `example/demo_reverse.f90` example.
* The "magma", "inferno","plasma", "viridis" matplotlib colormaps in a `matplotlib_colormaps` module.
* A `src/forcolormap_utils.f90` module with the subroutine `test_colormap()`.
* A `colormap_parameters` module.
* A `miscellaneous_colormaps` module.
* A `colorbar()` type-bound procedure to write a PPM file with the colorbar.
Expand All @@ -37,7 +38,6 @@ All notable changes to the gtk-fortran project are documented in this file. The
* The colormap `inverted_rainbow` was renamed `inv_rainbow`.
* `TODO.md` was refactored and renamed `ROADMAP.md`.
* Code refactoring.
* Moved `test` subroutine to demo and example1.
* Renamed `get_current()` to `get_name()`.
* For writing PPM files, the project [ForImage](https://github.com/gha3mi/forimage) by @gha3mi is now used as a fpm dependency. The related example must now be launched with `fpm run --example example1` and the ForColormap demo by `fpm run --example demo`.
* `src/colormap_class.f90`: `private` statement is now the default.
Expand Down
2 changes: 1 addition & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The main goal of v0.9 is to offer a usable library, sufficiently friendly, with
- [x] Add Interpolation functions.
- [x] Lagrange
- [x] Bezier
- [ ] Add a `colormap_utils.f90` module where the subroutine `test_colormap()` will be moved.
- [x] Add a `src/forcolormap_utils.f90` module where the subroutine `test_colormap()` will be moved.

### Examples

Expand Down
39 changes: 2 additions & 37 deletions example/demo.f90
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
! SOFTWARE.
!-------------------------------------------------------------------------------
! Contributed by vmagnin: 2023-09-26
! Last modification: gha3mi 2024-01-28, vmagnin 2024-02-15
! Last modification: gha3mi 2024-01-28, vmagnin 2024-02-21
!-------------------------------------------------------------------------------

program demo
use forcolormap, only: Colormap, colormaps_list, wp
use forcolormap_utils, only: test_colormap
implicit none

integer :: i
Expand Down Expand Up @@ -69,40 +70,4 @@ program demo
call test_colormap(custom_cmap, 'a_loaded_colormap_test', encoding='binary')
call custom_cmap%print()

contains

subroutine test_colormap(self, filename, encoding)
use forimage, only: format_pnm
type(Colormap), intent(inout) :: self
character(*), intent(in) :: filename
integer :: k, j ! Pixbuffer coordinates
integer, parameter :: pixwidth = 600
integer, parameter :: pixheight = 600
integer, dimension(pixheight,pixwidth*3) :: rgb_image
integer :: red, green, blue
real(wp) :: z
type(format_pnm) :: ppm
character(*), intent(in) :: encoding

do k = 0, pixwidth-1
do j = 0, pixheight-1
! Computing a z=f(x,y) function:
z = 1.0_wp + sin(k*j/10000.0_wp) * cos(j/100.0_wp)
! The corresponding RGB values in our colormap:
call self%compute_RGB(z, red, green, blue)
rgb_image(pixheight-j, 3*(k+1)-2) = red
rgb_image(pixheight-j, 3*(k+1)-1) = green
rgb_image(pixheight-j, 3*(k+1)) = blue
end do
end do

call ppm%set_pnm(encoding = encoding,&
file_format = 'ppm',&
width = pixwidth,&
height = pixheight,&
max_color = 255,&
comment = 'comment',&
pixels = rgb_image)
call ppm%export_pnm(filename)
end subroutine test_colormap
end program demo
39 changes: 2 additions & 37 deletions example/demo_reverse.f90
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
! SOFTWARE.
!-------------------------------------------------------------------------------
! Contributed by vmagnin: 2023-09-26
! Last modification: gha3mi 2023-11-01, vmagnin 2024-02-15
! Last modification: gha3mi 2023-11-01, vmagnin 2024-02-21
!-------------------------------------------------------------------------------

program demo_reverse
use forcolormap, only: Colormap, colormaps_list, wp
use forcolormap_utils, only: test_colormap
implicit none

integer :: i
Expand Down Expand Up @@ -68,40 +69,4 @@ program demo_reverse
call test_colormap(custom_cmap, 'a_loaded_reverse_colormap_test', encoding='binary')
call custom_cmap%print()

contains

subroutine test_colormap(self, filename, encoding)
use forimage, only: format_pnm
type(Colormap), intent(inout) :: self
character(*), intent(in) :: filename
integer :: k, j ! Pixbuffer coordinates
integer, parameter :: pixwidth = 600
integer, parameter :: pixheight = 600
integer, dimension(pixheight,pixwidth*3) :: rgb_image
integer :: red, green, blue
real(wp) :: z
type(format_pnm) :: ppm
character(*), intent(in) :: encoding

do k = 0, pixwidth-1
do j = 0, pixheight-1
! Computing a z=f(x,y) function:
z = 1.0_wp + sin(k*j/10000.0_wp) * cos(j/100.0_wp)
! The corresponding RGB values in our colormap:
call self%compute_RGB(z, red, green, blue)
rgb_image(pixheight-j, 3*(k+1)-2) = red
rgb_image(pixheight-j, 3*(k+1)-1) = green
rgb_image(pixheight-j, 3*(k+1)) = blue
end do
end do

call ppm%set_pnm(encoding = encoding,&
file_format = 'ppm',&
width = pixwidth,&
height = pixheight,&
max_color = 255,&
comment = 'comment',&
pixels = rgb_image)
call ppm%export_pnm(filename)
end subroutine test_colormap
end program demo_reverse
43 changes: 3 additions & 40 deletions example/example1.f90
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
! SOFTWARE.
!-------------------------------------------------------------------------------
! Contributed by gha3mi: 2023-10-26
! Last modification: gha3mi 2024-01-06
! Last modification: gha3mi 2024-01-06, vmagnin 2024-02-21
!-------------------------------------------------------------------------------

! This example demonstrates how ForImage can be used to import/export PPM files.
!> This example demonstrates how ForImage can be used to import/export PPM files.
program example1
use forcolormap
use forcolormap_utils, only: test_colormap
use forimage
implicit none

Expand Down Expand Up @@ -55,42 +56,4 @@ program example1
call ex1_colormap%finalize()
call ex1_colorbar%finalize()

contains

subroutine test_colormap(self, filename, encoding)
use forimage, only: format_pnm
type(Colormap), intent(inout) :: self
character(*), intent(in) :: filename
integer :: k, j ! Pixbuffer coordinates
integer, parameter :: pixwidth = 600
integer, parameter :: pixheight = 600
integer, dimension(:,:), allocatable :: rgb_image
integer :: red, green, blue
real(wp) :: z
type(format_pnm) :: ppm
character(*), intent(in) :: encoding

allocate(rgb_image(pixheight,pixwidth*3))

do k = 0, pixwidth-1
do j = 0, pixheight-1
! Computing a z=f(x,y) function:
z = 1.0_wp + sin(k*j/10000.0_wp) * cos(j/100.0_wp)
! The corresponding RGB values in our colormap:
call self%compute_RGB(z, red, green, blue)
rgb_image(pixheight-j, 3*(k+1)-2) = red
rgb_image(pixheight-j, 3*(k+1)-1) = green
rgb_image(pixheight-j, 3*(k+1)) = blue
end do
end do

call ppm%set_pnm(encoding = encoding,&
file_format = 'ppm',&
width = pixwidth,&
height = pixheight,&
max_color = 255,&
comment = 'comment',&
pixels = rgb_image)
call ppm%export_pnm(filename)
end subroutine test_colormap
end program example1
3 changes: 2 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ set(dir ${CMAKE_CURRENT_SOURCE_DIR})

set(FORCOLORMAP_SOURCES
${dir}/colormap_class.f90
${dir}/forcolormap_utils.f90
${dir}/colormap_parameters.f90
${dir}/colormaps_info.f90
${dir}/matplotlib_colormaps.f90
${dir}/miscellaneous_colormaps.f90
${dir}/scientific_colour_maps.f90
)
set(FORCOLORMAP_SOURCES ${FORCOLORMAP_SOURCES} PARENT_SCOPE)
set(FORCOLORMAP_SOURCES ${FORCOLORMAP_SOURCES} PARENT_SCOPE)
1 change: 1 addition & 0 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* `colormap_class.f90`: the Colormap class and the `colormaps_list`.
* `colormap_parameters.f90`: a few parameters used in several modules of the library.
* `forcolormap_utils.f90`: contains miscellaneous procedures and functions.
* `colormaps_info.f90`: the Colormaps_info class offers information about each colormap.
* The modules containing the different collections of colormaps:
* `scientific_colour_maps.f90`
Expand Down
79 changes: 79 additions & 0 deletions src/forcolormap_utils.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
! The MIT License (MIT)
!
! Copyright (c) 2024 vmagnin
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in all
! copies or substantial portions of the Software.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.
!-------------------------------------------------------------------------------
! Contributed by vmagnin: 2024-02-21
! Last modification: vmagnin 2024-02-21
!-------------------------------------------------------------------------------

!> This module contains miscellaneous procedures and functions.
module forcolormap_utils
use forcolormap, only: Colormap, wp

implicit none

private

public :: test_colormap

contains

!> This procedure computes a default z=f(x,y) function and plot it
!> in a .ppm file using the specified colormap.
subroutine test_colormap(self, filename, encoding)
use forimage, only: format_pnm
type(Colormap), intent(inout) :: self
character(*), intent(in) :: filename
integer :: k, j ! Pixbuffer coordinates
integer, parameter :: pixwidth = 600
integer, parameter :: pixheight = 600
integer, dimension(:,:), allocatable :: rgb_image
integer :: red, green, blue
real(wp) :: z
type(format_pnm) :: ppm
character(*), intent(in) :: encoding

allocate(rgb_image(pixheight,pixwidth*3))

do k = 0, pixwidth-1
do j = 0, pixheight-1
! Computing a z=f(x,y) function:
z = 1.0_wp + sin(k*j/10000.0_wp) * cos(j/100.0_wp)
! The corresponding RGB values in our colormap:
call self%compute_RGB(z, red, green, blue)
rgb_image(pixheight-j, 3*(k+1)-2) = red
rgb_image(pixheight-j, 3*(k+1)-1) = green
rgb_image(pixheight-j, 3*(k+1)) = blue
end do
end do

call ppm%set_pnm(encoding = encoding,&
file_format = 'ppm',&
width = pixwidth,&
height = pixheight,&
max_color = 255,&
comment = 'comment',&
pixels = rgb_image)
call ppm%export_pnm(filename)

end subroutine test_colormap

end module forcolormap_utils

0 comments on commit 3072e59

Please sign in to comment.