From 3072e599f7e320068f088faed6789fd6ac407bb8 Mon Sep 17 00:00:00 2001 From: vmagnin Date: Wed, 21 Feb 2024 10:50:12 +0100 Subject: [PATCH 1/4] src/forcolormap_utils.f90: contains the test_colormap() subroutine used in several examples --- CHANGELOG.md | 2 +- ROADMAP.md | 2 +- example/demo.f90 | 39 +------------------ example/demo_reverse.f90 | 39 +------------------ example/example1.f90 | 43 ++------------------- src/CMakeLists.txt | 3 +- src/README.md | 1 + src/forcolormap_utils.f90 | 79 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 91 insertions(+), 117 deletions(-) create mode 100644 src/forcolormap_utils.f90 diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a77ece..cb4ded9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. @@ -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. diff --git a/ROADMAP.md b/ROADMAP.md index 7690fe4..4fba704 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -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 diff --git a/example/demo.f90 b/example/demo.f90 index d6b71b5..afc01b0 100644 --- a/example/demo.f90 +++ b/example/demo.f90 @@ -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 @@ -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 diff --git a/example/demo_reverse.f90 b/example/demo_reverse.f90 index 505cf46..11e9c8b 100644 --- a/example/demo_reverse.f90 +++ b/example/demo_reverse.f90 @@ -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 @@ -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 diff --git a/example/example1.f90 b/example/example1.f90 index 1d5e77b..75ea102 100644 --- a/example/example1.f90 +++ b/example/example1.f90 @@ -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 @@ -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 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index cb51bea..2a7b97a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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) \ No newline at end of file +set(FORCOLORMAP_SOURCES ${FORCOLORMAP_SOURCES} PARENT_SCOPE) diff --git a/src/README.md b/src/README.md index ed578f5..8ad76f0 100644 --- a/src/README.md +++ b/src/README.md @@ -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` diff --git a/src/forcolormap_utils.f90 b/src/forcolormap_utils.f90 new file mode 100644 index 0000000..e72d327 --- /dev/null +++ b/src/forcolormap_utils.f90 @@ -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 From 51e47fcea976de0a2bc759b16f8f052a00015c27 Mon Sep 17 00:00:00 2001 From: vmagnin Date: Wed, 21 Feb 2024 10:51:12 +0100 Subject: [PATCH 2/4] .gitignore: added .bak --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index d2f33c9..6045c84 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ *.ppm # Backups +*.bak *.backup .gitignore~ @@ -43,4 +44,4 @@ # Directories build/ -doc/ \ No newline at end of file +doc/ From 6b62dbb5f1c499e3433132e391df20cd9ea156a6 Mon Sep 17 00:00:00 2001 From: vmagnin Date: Wed, 21 Feb 2024 10:54:07 +0100 Subject: [PATCH 3/4] src/forcolormap_utils.f90: copyright updated --- src/forcolormap_utils.f90 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/forcolormap_utils.f90 b/src/forcolormap_utils.f90 index e72d327..045110b 100644 --- a/src/forcolormap_utils.f90 +++ b/src/forcolormap_utils.f90 @@ -1,6 +1,6 @@ ! The MIT License (MIT) ! -! Copyright (c) 2024 vmagnin +! Copyright (c) 2024 vmagnin, gha3mi ! ! Permission is hereby granted, free of charge, to any person obtaining a copy ! of this software and associated documentation files (the "Software"), to deal @@ -20,7 +20,7 @@ ! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE ! SOFTWARE. !------------------------------------------------------------------------------- -! Contributed by vmagnin: 2024-02-21 +! Contributed by vmagnin & gha3mi: 2024-02-21 ! Last modification: vmagnin 2024-02-21 !------------------------------------------------------------------------------- From 690afa9f6179d923c6fc6a3ecb91d69399ae57d2 Mon Sep 17 00:00:00 2001 From: vmagnin Date: Thu, 22 Feb 2024 10:45:17 +0100 Subject: [PATCH 4/4] src/forcolormap_utils.f90: in test_colormap(), the encoding argument is optional and binary by default. And a comment was added in the PPM files. --- CHANGELOG.md | 1 + example/demo.f90 | 16 ++++++++-------- example/demo_reverse.f90 | 16 ++++++++-------- src/forcolormap_utils.f90 | 17 ++++++++++++----- 4 files changed, 29 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb4ded9..e0c7228 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ All notable changes to the gtk-fortran project are documented in this file. The * A `colorbar()` type-bound procedure to write a PPM file with the colorbar. ### Changed +* Subroutine `test_colormap()`: the `encoding` argument is now optional (binary by default). * The colormap `inverted_rainbow` was renamed `inv_rainbow`. * `TODO.md` was refactored and renamed `ROADMAP.md`. * Code refactoring. diff --git a/example/demo.f90 b/example/demo.f90 index afc01b0..872fedc 100644 --- a/example/demo.f90 +++ b/example/demo.f90 @@ -1,6 +1,6 @@ ! The MIT License (MIT) ! -! Copyright (c) 2023 Vincent Magnin +! Copyright (c) 2023-2024 Vincent Magnin ! ! Permission is hereby granted, free of charge, to any person obtaining a copy ! of this software and associated documentation files (the "Software"), to deal @@ -21,7 +21,7 @@ ! SOFTWARE. !------------------------------------------------------------------------------- ! Contributed by vmagnin: 2023-09-26 -! Last modification: gha3mi 2024-01-28, vmagnin 2024-02-21 +! Last modification: gha3mi 2024-01-28, vmagnin 2024-02-22 !------------------------------------------------------------------------------- program demo @@ -44,12 +44,12 @@ program demo 255, 255, 255 ], & shape(my_colormap), order = [2, 1] ) - ! Let's create PPM files for each built-in colormap. - ! The built-in z=f(x,y) test function is in the [0, 2] range: + !> We create PPM files (binary encoded by default) for each built-in colormap. + !> The built-in z=f(x,y) test function is in the [0, 2] range: do i = 1, size(colormaps_list) call cmap%set(trim(colormaps_list(i)), 0.0_wp, 2.0_wp) call cmap%colorbar(trim(colormaps_list(i))//'_colorbar') - call test_colormap(cmap, trim(colormaps_list(i))//'_test', encoding='binary') + call test_colormap(cmap, trim(colormaps_list(i))//'_test') print '("Colormap ", A30, " has ", I0, " levels")', trim(cmap%get_name()), cmap%get_levels() end do @@ -57,17 +57,17 @@ program demo call cmap%set("cubehelix", 0.0_wp, 2.0_wp, 1024, [0.5_wp, -1.0_wp, 1.0_wp, 1.0_wp]) ! We change the name for the output test files: call cmap%colorbar('cubehelix_customized_colorbar') - call test_colormap(cmap, 'cubehelix_customized_test', encoding='binary') + call test_colormap(cmap, 'cubehelix_customized_test') ! You can create your own colormap defined in an array: call custom_cmap%create('discrete', 0.0_wp, 2.0_wp, my_colormap) call custom_cmap%colorbar('discrete_colorbar') - call test_colormap(custom_cmap, 'discrete_test', encoding='binary') + call test_colormap(custom_cmap, 'discrete_test') ! Or you can download it from a .txt file: call custom_cmap%load("test_map_to_load.txt", 0.0_wp, 2.0_wp) call custom_cmap%colorbar('a_loaded_colorbar') - call test_colormap(custom_cmap, 'a_loaded_colormap_test', encoding='binary') + call test_colormap(custom_cmap, 'a_loaded_colormap_test') call custom_cmap%print() end program demo diff --git a/example/demo_reverse.f90 b/example/demo_reverse.f90 index 11e9c8b..d9d61a1 100644 --- a/example/demo_reverse.f90 +++ b/example/demo_reverse.f90 @@ -1,6 +1,6 @@ ! The MIT License (MIT) ! -! Copyright (c) 2023 Vincent Magnin +! Copyright (c) 2023-2024 Vincent Magnin ! ! Permission is hereby granted, free of charge, to any person obtaining a copy ! of this software and associated documentation files (the "Software"), to deal @@ -21,7 +21,7 @@ ! SOFTWARE. !------------------------------------------------------------------------------- ! Contributed by vmagnin: 2023-09-26 -! Last modification: gha3mi 2023-11-01, vmagnin 2024-02-21 +! Last modification: gha3mi 2023-11-01, vmagnin 2024-02-22 !------------------------------------------------------------------------------- program demo_reverse @@ -43,12 +43,12 @@ program demo_reverse 255, 255, 255 ], & shape(my_colormap), order = [2, 1] ) - ! Let's create PPM files for each built-in colormap. - ! The built-in z=f(x,y) test function is in the [0, 2] range: + !> We create PPM files (binary encoded by default) for each built-in colormap. + !> The built-in z=f(x,y) test function is in the [0, 2] range: do i = 1, size(colormaps_list) call cmap%set(trim(colormaps_list(i)), 0.0_wp, 2.0_wp, reverse=.true.) call cmap%colorbar(trim(colormaps_list(i))//'_reverse_colorbar') - call test_colormap(cmap, trim(colormaps_list(i))//'_reverse_test', encoding='binary') + call test_colormap(cmap, trim(colormaps_list(i))//'_reverse_test') print '("Colormap ", A30, " has ", I0, " levels")', trim(cmap%get_name()), cmap%get_levels() end do @@ -56,17 +56,17 @@ program demo_reverse call cmap%set("cubehelix", 0.0_wp, 2.0_wp, 1024, [0.5_wp, -1.0_wp, 1.0_wp, 1.0_wp], reverse=.true.) ! We change the name for the output test files: call cmap%colorbar('cubehelix_customized_reverse_colorbar') - call test_colormap(cmap, 'cubehelix_customized_reverse_test', encoding='binary') + call test_colormap(cmap, 'cubehelix_customized_reverse_test') ! You can create your own colormap defined in an array: call custom_cmap%create('discrete', 0.0_wp, 2.0_wp, my_colormap, reverse=.true.) call custom_cmap%colorbar('discrete_reverse_colorbar') - call test_colormap(custom_cmap, 'discrete_reverse_test', encoding='binary') + call test_colormap(custom_cmap, 'discrete_reverse_test') ! Or you can download it from a .txt file: call custom_cmap%load("test_map_to_load.txt", 0.0_wp, 2.0_wp, reverse=.true.) call custom_cmap%colorbar('a_loaded_reverse_colorbar') - call test_colormap(custom_cmap, 'a_loaded_reverse_colormap_test', encoding='binary') + call test_colormap(custom_cmap, 'a_loaded_reverse_colormap_test') call custom_cmap%print() end program demo_reverse diff --git a/src/forcolormap_utils.f90 b/src/forcolormap_utils.f90 index 045110b..18c7fa1 100644 --- a/src/forcolormap_utils.f90 +++ b/src/forcolormap_utils.f90 @@ -21,7 +21,7 @@ ! SOFTWARE. !------------------------------------------------------------------------------- ! Contributed by vmagnin & gha3mi: 2024-02-21 -! Last modification: vmagnin 2024-02-21 +! Last modification: vmagnin 2024-02-22 !------------------------------------------------------------------------------- !> This module contains miscellaneous procedures and functions. @@ -37,7 +37,8 @@ module forcolormap_utils contains !> This procedure computes a default z=f(x,y) function and plot it - !> in a .ppm file using the specified colormap. + !> in a .ppm file using the specified colormap. That function is defined + !> in the [0, 2] range. subroutine test_colormap(self, filename, encoding) use forimage, only: format_pnm type(Colormap), intent(inout) :: self @@ -49,7 +50,7 @@ subroutine test_colormap(self, filename, encoding) integer :: red, green, blue real(wp) :: z type(format_pnm) :: ppm - character(*), intent(in) :: encoding + character(*), intent(in), optional :: encoding !> Default is binary allocate(rgb_image(pixheight,pixwidth*3)) @@ -65,12 +66,18 @@ subroutine test_colormap(self, filename, encoding) end do end do - call ppm%set_pnm(encoding = encoding,& + if (present(encoding)) then + call ppm%set_format(encoding) + else + call ppm%set_format('binary') + end if + + call ppm%set_pnm(encoding = ppm%get_format(),& file_format = 'ppm',& width = pixwidth,& height = pixheight,& max_color = 255,& - comment = 'comment',& + comment = 'Test generated by ForColormap',& pixels = rgb_image) call ppm%export_pnm(filename)