I need to read a .BMP or other bitmap file into Fortran to do image
manipulation on it eg. conversion to and from LAB or HSV,
autocorrelation, convolution & deconvolution, edge detection, fft,
discrete and continuous optimisation.
How can I read a bitmap file into Fortran?
>From "USE DFLIB",
LOADIMAGE will read a .bmp and display it to screen
SAVEIMAGE will screendump to a .bmp
but neither will allow the image to be saved and manipulated within
the program.
GETIMAGE will screendump to a buffer but I've no idea what format the
buffer is in or whether it can be used. Can I use the buffer?
Perhaps I can use "USE DFOPNGL", but how?
You need to locate the bmp file format and craft derived types
suitable for reading the bmp file content. You'll probably want to
use the "binary" file I/O capability. Method is (or can be) the same
whether MS FPS (there was no MS Visual Fortran) or CVF (or IVF). You
don't technically need any OS APIs to read in the raw data (assuming
you want to perform the above transformations yourself).
A newsgroup search seems to indicate the DISLIN has facilities to R/
W .bmp files and interconvert to RGB. HTH
Code for reading .bmp files in C use a "struct" containing "unsigned
short" and "unsigned int" data types. How do these translate into
Fortran? If the .bmp is compressed then it becomes more difficult.
> DISLIN
I've downloaded it now. I'll unzip it in a minute.
>>locate the bmp file format
>
>
> Code for reading .bmp files in C use a "struct" containing "unsigned
> short" and "unsigned int" data types. How do these translate into
> Fortran? If the .bmp is compressed then it becomes more difficult.
read them into signed integers of the same length (same number of bytes
or bits). works just fine. do it all the time. With CVF/IVF a derived
type can be made interoperable with a c struct.
>
>
>>DISLIN
>
>
> I've downloaded it now. I'll unzip it in a minute.
>
--
Gary Scott
mailto:garylscott@sbcglobal dot net
Fortran Library: http://www.fortranlib.com
Support the Original G95 Project: http://www.g95.org
-OR-
Support the GNU GFortran Project: http://gcc.gnu.org/fortran/index.html
If you want to do the impossible, don't hire an expert because he knows
it can't be done.
-- Henry Ford
You seem to be using a Windows platform. Both MS visual fortran and the
compaq fortran are now rather old.
You can download a free personal edition version of the salford/silverfrost
FTN95 from www.silverfrost.com.
It appears that various graphics file formats (not just BMP, but JPEG and
possibly others) can be read into
a "device independent bitmap" format (essentially an array) and processed
from there. You can at least try it
(for free) and see if it works for you. I suspect that the advantage of
importing and converting to the DIB format
is that you can import various other graphics formats and process them all
using the same code.
I'd be prepared to bet that other up-to-date fortrans offer similar
facilities, although I only know this one, and can
vouch that it will do what you want without any add-ons. The DIB and BMP
formats are written up on the
MSDN website.
EB
Yes, I also confirm, this works fine. The compiler has help file and
very effective examples how to read, write, covert, manipulate in
fortran and display bitmaps
Send me message with your email and I will try to prepare some
relatively small programs as a prototypes.
Some things like image scalings, comparison, distortions, animations,
highlighting part of screen with mouse, cut, paste and save in
several
graphics formats etc are hard already to extract out of my code...
I am very happy with that my application-oriented little personal
"Photoshop" and find this very convenient
The following Fortran code fragment will read a 24 bit .bmp file. On
my compiler I also need the switch "/assume:byterecl"
integer Maxwidth,Maxheight,irec,iwidth,iheight,i,j,ipad
parameter(Maxwidth=3220,Maxheight=2415)
character header(54),ch
integer image(Maxheight,Maxwidth,3)
open(1,file='..\..
\cow_small.bmp',form='unformatted',access='direct',recl=1)
do irec=1,54
read(1,rec=irec) header(irec)
end do
if(ichar(header(11)).ne.54.or.ichar(header(29)).ne.
24.or.ichar(header(31)).ne.0) then
print*,'sorry, can not handle this file'
end if
! get image height and width
iheight=ichar(header(23))+256*(ichar(header(24))
+256*(ichar(header(25))+256*ichar(header(26))))
iwidth=ichar(header(19))+256*(ichar(header(20))
+256*(ichar(header(21))+256*ichar(header(22))))
print*,'iheight,iwidth=',iheight,iwidth
ipad=(Maxwidth-iwidth)-((Maxwidth-iwidth)/4)*4
irec=54
do i=1,iheight
do j=1,iwidth
irec=irec+1
read(1,rec=irec) ch
image(i,j,3)=ichar(ch)
irec=irec+1
read(1,rec=irec) ch
image(i,j,2)=ichar(ch)
irec=irec+1
read(1,rec=irec) ch
image(i,j,1)=ichar(ch)
end do
irec=irec+ipad
end do
Oops,
ipad=(Maxwidth-iwidth)*3-((Maxwidth-iwidth)*3/4)*4
Maxwidth has to be divisible by 4. On writing the .bmp bitmap out
again use:
do ip=1,ipad
irec=irec+1
write(2,rec=irec) char(0)
end do