Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Read a .BMP into Fortran?

788 views
Skip to first unread message

David.P...@csiro.au

unread,
Mar 26, 2007, 6:32:48 PM3/26/07
to
I'm using a new Microsoft visual fortran at work and a really old
Compaq Visual Fortran Professional Edition 6.1.0 at home. I'd like to
do this on the Compaq fortran if possible.

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?

gary.l...@lmco.com

unread,
Mar 26, 2007, 6:38:18 PM3/26/07
to

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).

e p chandler

unread,
Mar 26, 2007, 7:26:39 PM3/26/07
to
> you want to perform the above transformations yourself).- Hide quoted text -
>
> - Show quoted text -

A newsgroup search seems to indicate the DISLIN has facilities to R/
W .bmp files and interconvert to RGB. HTH


David.P...@csiro.au

unread,
Mar 26, 2007, 8:45:41 PM3/26/07
to
> 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.

> DISLIN

I've downloaded it now. I'll unzip it in a minute.

Gary Scott

unread,
Mar 26, 2007, 8:53:39 PM3/26/07
to
David.P...@csiro.au wrote:

>>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

Edward N Bromhead

unread,
Mar 31, 2007, 7:55:35 AM3/31/07
to

<David.P...@csiro.au> wrote in message
news:1174948368.4...@e65g2000hsc.googlegroups.com...

> I'm using a new Microsoft visual fortran at work and a really old
> Compaq Visual Fortran Professional Edition 6.1.0 at home. I'd like to
> do this on the Compaq fortran if possible.
>
> 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.

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


rrr...@gmail.com

unread,
Apr 1, 2007, 4:44:47 AM4/1/07
to
On Mar 31, 4:55 am, "Edward N Bromhead"

> I only know this one, and can
> vouch that it will do what you want without any add-ons.
>

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

Lane Straatman

unread,
Apr 1, 2007, 4:09:55 PM4/1/07
to

<rrr...@gmail.com> wrote in message
news:1175417087....@b75g2000hsg.googlegroups.com...

> On Mar 31, 4:55 am, "Edward N Bromhead"
>> I only know this one, and can
>> vouch that it will do what you want without any add-ons.
>>
>
> Yes, I also confirm, this works fine. The compiler has help file and
> very effective examples how to read, write, [convert], manipulate in
> fortran and display bitmaps
Can you give more detail on this? I use silverfrost's IDE and have not
found any of the visual knink-knacks that I used to have with MS Visual
Studio.
--
LS


rrr...@gmail.com

unread,
Apr 4, 2007, 6:26:49 AM4/4/07
to
On Apr 1, 1:09 pm, "Lane Straatman" <inva...@invalid.net> wrote:
> <rrr7...@gmail.com> wrote in message

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

David.P...@csiro.au

unread,
Apr 23, 2007, 10:32:49 PM4/23/07
to
> How can I read a bitmap file into Fortran?

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

David.P...@csiro.au

unread,
Apr 25, 2007, 2:57:53 PM4/25/07
to
> ipad=(Maxwidth-iwidth)-((Maxwidth-iwidth)/4)*4

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

0 new messages