Converts a color image to greyscale. Only supported for PPM images.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(format_pnm), | intent(inout) | :: | this |
elemental pure subroutine greyscale(this) class(format_pnm), intent(inout) :: this integer :: i, j ! Check if the file is ppm if (this%file_format /= 'ppm') error stop 'greyscale: This function is only for ppm files.' do i = 1, this%height do j = 1, this%width ! Calculate the ITU Rec.709 weighted average of RGB channels to derive a greyscale value and assign it as an integer to all RGB channels. this%pixels(i, 3*j-2:3*j) = int(0.2126_rk * real(this%pixels(i, 3*j-2), kind=rk) + & 0.7152_rk * real(this%pixels(i, 3*j-1), kind=rk) + & 0.0722_rk * real(this%pixels(i, 3*j-0), kind=rk), kind=ik) end do end do call this%check_pixel_range(this%pixels) end subroutine greyscale