Debian Codegraph @

src/write_pfm.f90

raw
1 !-----------------------------------------------------------------
2 !
3 ! This file is (or was) part of SPLASH, a visualisation tool
4 ! for Smoothed Particle Hydrodynamics written by Daniel Price:
5 !
6 ! http://users.monash.edu.au/~dprice/splash
7 !
8 ! SPLASH comes with ABSOLUTELY NO WARRANTY.
9 ! This is free software; and you are welcome to redistribute
10 ! it under the terms of the GNU General Public License
11 ! (see LICENSE file for details) and the provision that
12 ! this notice remains intact. If you modify this file, please
13 ! note section 2a) of the GPLv2 states that:
14 !
15 ! a) You must cause the modified files to carry prominent notices
16 ! stating that you changed the files and the date of any change.
17 !
18 ! Copyright (C) 2005-2020 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22 !
23 ! module containing routines to read/write images in .pfm format
24 ! Info below taken from: http://www.pauldebevec.com/Research/HDR/PFM/
25 !
26 ! The Portable FloatMap format was designed as a floating-point image format.
27 ! The format begins with three lines of text specifying the image size and type,
28 ! and then continues with raw binary image data for the rest of the file.
29 ! The text header of a .pfm file takes the following form:
30 ! [type]
31 ! [xres] [yres]
32 ! [byte_order]
33 !
34 ! Each of the three lines of text ends with a 1-byte Unix-style carriage return.
35 ! "[type]" is one of "PF" for a 3-channel RGB color image, or "Pf" for a monochrome single-channel image.
36 ! "[xres] [yres]" indicates the x and y resolutions of the image.
37 ! "[byte_order]" is a number used to indicate the byte order within the file.
38 ! A positive number (e.g. "1.0") indicates big-endian, with the most significant byte of each 4-byte float first.
39 ! If the number is negative (e.g. "-1.0") this indicates little-endian, with the least significant byte first. There are no comments in these files. For example:
40 !
41 ! PF
42 ! 768 512
43 ! -1.0
44 ! Indicates an RGB color image, 768 by 512 pixels, with little-endian byte order.
45 ! After the final carriage return the file proceeds with a series of three 4-byte IEEE 754 single
46 ! precision floating point numbers for each pixel, specified in left to right, bottom to top order.
47 !
48 !-----------------------------------------------------------------
49 module write_pfm
50 implicit none
51
52 contains
53
54 subroutine write_pixmap_pfm(filename,nx,ny,datpix,ierr)
55 use iso_c_binding, only:c_float
56 character(len=*), intent(in) :: filename
57 integer, intent(in) :: nx,ny
58 real(c_float), intent(in) :: datpix(nx,ny)
59 integer, intent(out) :: ierr
60 integer :: iunit
61 logical :: bigendian
62
63 bigendian = IACHAR(TRANSFER(1,"a")) == 0
64
65 ! write formatted header
66 open(newunit=iunit,file=filename,action='write',status='replace',iostat=ierr)
67 if (ierr /= 0) return
68 write(iunit,"(a)",iostat=ierr) 'Pf'
69 write(iunit,*,iostat=ierr) nx,ny
70 if (bigendian) then
71 write(iunit,*,iostat=ierr) 1.0
72 else
73 write(iunit,*,iostat=ierr) -1.0
74 endif
75 close(iunit)
76
77 ! write unformatted data
78 open(newunit=iunit,file=filename,action='write',status='old',position='append',form='unformatted',iostat=ierr)
79 write(iunit) datpix(1:nx,1:ny)
80 close(iunit)
81
82 end subroutine write_pixmap_pfm
83
84 end module write_pfm