Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Reading image files

Dick_L_
Beginner
1,025 Views
I wonder what is the simplest image format to read into Intel Fortran? I have tried .raw, .tif, .pcx etc files produced by PaintshopPro but struggle to read the data. The purpose is to read image data into a Fortran Array for number crunching. Can anyone give me any clues as to the use of formatted or unformatted read. The INQUIRE statement says that the files are formatted, sequential. I use Windows XP.
Dick
0 Kudos
1 Reply
Steven_L_Intel1
Employee
1,025 Views
BMP is reasonably straightforward. INQUIRE won't tell you anything useful. Here's an excerpt of some code I use for reading uncompressed BMP files (this is not complete code - just a fragment that shows the structure declaration and how to read in the file.)


module logo
implicit none

!DEC$ OPTIONS /WARN=NOALIGN
type BITMAPFILEHEADER
sequence
character(2) bfType ! "BM"
integer(4) bfSize ! Size of file in bytes
integer(2) bfReserved1 ! Reserved, always 0
integer(2) bfReserved2 ! Reserved, always 0
integer(4) bfOffBits ! Offset to image in bytes
end type BITMAPFILEHEADER

type BITMAPINFOHEADER
sequence
integer(4) biSize ! Size of BITMAPINFOHEADER in bytes
integer(4) biWidth ! Width of image in pixels
integer(4) biHeight ! Height of image in pixels
integer(2) biPlanes ! Number of color planes (always 1)
integer(2) biBitCount ! Number of color bits
integer(4) biCompression ! Type of compression used
integer(4) biSizeImage ! Size of image in bytes
integer(4) biXPelsPerMeter ! Horizontal pixels per meter
integer(4) biYPelsPerMeter ! Vertical pixels per meter
integer(4) biClrUsed ! Number of colors used
integer(4) biClrImportant ! Number of "important" colors
end type BITMAPINFOHEADER

type(BITMAPFILEHEADER) :: bm_header,text_bm_header
type(BITMAPINFOHEADER) :: bm_info,text_bm_info
!DEC$ END OPTIONS


integer row_width, text_row_width
integer(1), allocatable :: bitmap(:), text_bitmap(:)
integer, dimension(8) :: date_values

contains

subroutine read_logo
implicit none

integer i,ios,totalsize
integer(1) ignore

open (unit=1,file='intlBlg.bmp', status='old',form='binary',action='read',iostat=ios)
if (ios /= 0) return
read(1,iostat=ios) bm_header
if (ios /= 0) return
if (bm_header%bfType /= "BM") return
read(1,iostat=ios) bm_info
if (ios /= 0) return

do i=1,(bm_header%bfOffBits - (sizeof(bm_header) + sizeof(bm_info)))
read (1) ignore
end do

totalsize = bm_info%biSizeImage
!if (totalsize == 0) &
! totalsize = (((bm_info%biWidth * bm_info%biBitcount) + 7) / 8) * bm_info%biHeight
allocate (bitmap(totalsize))
read (1, iostat=ios) bitmap
if (ios /= 0) deallocate(bitmap)
close (1)

row_width = (totalsize / bm_info%biHeight) / 3
0 Kudos
Reply