Skip to content

Commit

Permalink
src/forcolormap_utils.f90: in test_colormap(), the encoding argument …
Browse files Browse the repository at this point in the history
…is optional and binary by default. And a comment was added in the PPM files.
  • Loading branch information
vmagnin committed Feb 22, 2024
1 parent 6b62dbb commit 690afa9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 8 additions & 8 deletions example/demo.f90
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -44,30 +44,30 @@ 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

! Cubehelix can also accept other parameters (varargs array):
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
16 changes: 8 additions & 8 deletions example/demo_reverse.f90
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -43,30 +43,30 @@ 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

! Cubehelix can also accept other parameters (varargs array):
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
17 changes: 12 additions & 5 deletions src/forcolormap_utils.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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))

Expand All @@ -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)

Expand Down

0 comments on commit 690afa9

Please sign in to comment.