|
| 1 | +module bmp_m |
| 2 | + use iso_fortran_env, only: i2 => int16, i4 => int32 |
| 3 | + use iso_c_binding, only: c_char |
| 4 | + implicit none |
| 5 | + private |
| 6 | + public :: save_bmp |
| 7 | + |
| 8 | + integer(kind=i2), parameter :: BMP_HEADER_SIGNATURE = int(z'4D42', i2) ! 'BM' in ASCII |
| 9 | + |
| 10 | + type :: bmp_file_head |
| 11 | + integer(kind=i4) :: file_size ! size of BMP file, in byte (54 + pixel array) |
| 12 | + integer(kind=i2) :: reserve1 ! not used |
| 13 | + integer(kind=i2) :: reserve2 ! not used |
| 14 | + integer(kind=i4) :: offset_pixel ! starting position of pixel array |
| 15 | + end type |
| 16 | + |
| 17 | + type :: dib_head |
| 18 | + integer(kind=i4) :: dib_size ! size of DIB, 40 bytes |
| 19 | + integer(kind=i4) :: bmp_width ! image width, in pixel |
| 20 | + integer(kind=i4) :: bmp_height ! image height, in pixel |
| 21 | + integer(kind=i2) :: num_planes |
| 22 | + integer(kind=i2) :: bit_per_pixel ! only 24 is available |
| 23 | + integer(kind=i4) :: compress ! 0, do not compress |
| 24 | + integer(kind=i4) :: image_size ! size of BMP image, in byte |
| 25 | + integer(kind=i4) :: x_ppm ! x pixel per meter |
| 26 | + integer(kind=i4) :: y_ppm ! y pixel per meter |
| 27 | + integer(kind=i4) :: num_colors ! colors in color table |
| 28 | + integer(kind=i4) :: important_color |
| 29 | + end type |
| 30 | + |
| 31 | +contains |
| 32 | + |
| 33 | + function save_bmp(image, file_name) result(stat) |
| 34 | + character(kind=c_char), dimension(:,:,:), intent(in) :: image |
| 35 | + character(len=*), intent(in) :: file_name |
| 36 | + logical :: stat |
| 37 | + !=================================================== |
| 38 | + type(bmp_file_head) :: header |
| 39 | + type(dib_head) :: dib |
| 40 | + |
| 41 | + integer(kind=i4) :: row_size |
| 42 | + integer :: unit, ierr, i |
| 43 | + |
| 44 | + stat = .false. |
| 45 | + |
| 46 | + if (size(image, dim=1) /= 3) return |
| 47 | + |
| 48 | + header%offset_pixel = 54 |
| 49 | + header%reserve1 = 0 |
| 50 | + header%reserve2 = 0 |
| 51 | + |
| 52 | + dib%bit_per_pixel = 24 |
| 53 | + dib%bmp_width = size(image, dim=2) |
| 54 | + dib%bmp_height = size(image, dim=3) |
| 55 | + dib%compress = 0 |
| 56 | + dib%dib_size = 40 |
| 57 | + |
| 58 | + dib%important_color = 0 |
| 59 | + dib%num_colors = 0 |
| 60 | + dib%num_planes = 1 |
| 61 | + dib%x_ppm = 3780 |
| 62 | + dib%y_ppm = 3780 |
| 63 | + |
| 64 | + row_size = ceiling(dib%bit_per_pixel * dib%bmp_width / 32., kind=i4) * 4 |
| 65 | + dib%image_size = dib%bmp_height * row_size |
| 66 | + header%file_size = 54 + dib%image_size |
| 67 | + |
| 68 | + open(newunit=unit, file=file_name, iostat=ierr, access="stream", & |
| 69 | + form="unformatted", action="write", status="unknown") |
| 70 | + if (ierr /= 0) return |
| 71 | + |
| 72 | + write(unit) BMP_HEADER_SIGNATURE |
| 73 | + write(unit) header |
| 74 | + write(unit) dib |
| 75 | + |
| 76 | + do i = 0, dib%bmp_height-1 |
| 77 | + write(unit, pos=header%offset_pixel+i*row_size+1) image(:,:,i+1) |
| 78 | + end do |
| 79 | + |
| 80 | + if (mod(dib%bmp_width, 4) /= 0) then |
| 81 | + write(unit, pos=header%offset_pixel+dib%bmp_height*row_size) char(0, c_char) |
| 82 | + end if |
| 83 | + |
| 84 | + close(unit) |
| 85 | + |
| 86 | + stat = .true. |
| 87 | + |
| 88 | + end function save_bmp |
| 89 | + |
| 90 | +end module bmp_m |
0 commit comments