Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code to change a JPG file to a BMP file

Status
Not open for further replies.

gullipe

Programmer
Oct 5, 2006
139
IS
Hi there

Has anybody come a cross a Fortran code (or subroutine) to change a JPG (JPEG) file to a BMP file? I have found such code in C and in Pascal somewhere in the Internet, but not in Fortran.
 
Haven't come across any but you could always translate it.
 
Hi, xwb

The problem is that the code (in C and Pascal) seems to be 50-60 files, each one several pages (including comments though)! This means many thousand programming lines!
 
I guess, translating something so big from C or from Pascal to Fortran would be a real pain
:)

IMHO, the simplest way is to use a ready command line utility for conversion.
Look at the graphical program of your choice if it has a such utility. Or look at ImageMagick - it has a convert command which seems to be usable:

Then call the command line utility from your fortran program using the system subroutine as follows
Code:
call system ("convert image.jpg image.bmp")
 
Hi, mikrom

Thank you for the reply. I will look into this. I have used a program called DJPEG.exe (C code available on to change a JPG file to a BMP file by calling it from my Fortran program, but I wanted to do this by a simple call to a subroutine (in Fortran of course!) and not to use a help from an external program.
 
Hi gullipe,

If you have the C-source, you can try to call the C-function from Fortran.
1. modify the C-source, so that you wrap the main()-functionality into other function
2. compile it and link with fortran.

Example
functions.c
Code:
[COLOR=#0000ff]/*[/color][COLOR=#0000ff] Compile:[/color]
[COLOR=#0000ff] *   gcc -c functions.c [/color][COLOR=#0000ff]*/[/color]
[COLOR=#a020f0]#include [/color][COLOR=#ff00ff]<stdio.h>[/color]
[COLOR=#a020f0]#include [/color][COLOR=#ff00ff]<stdlib.h>[/color]

[COLOR=#2e8b57][b]int[/b][/color] sys_call__()
[COLOR=#0000ff]/*[/color][COLOR=#0000ff] When Compiling & Linking the Fortran Caller with g95, [/color]
[COLOR=#0000ff] * the name of the function should be[/color]
[COLOR=#0000ff] *   sys_call__[/color]
[COLOR=#0000ff] *[/color]
[COLOR=#0000ff] * When Compiling & Linking with gfortran[/color]
[COLOR=#0000ff] * the name of the function should be[/color]
[COLOR=#0000ff] *   sys_call_[/color]
[COLOR=#0000ff] [/color][COLOR=#0000ff]*/[/color]
{
  [COLOR=#2e8b57][b]int[/b][/color] i;
  [COLOR=#0000ff]// Executing program/command[/color]
  i=system ([COLOR=#ff00ff]"ls -la *.f*"[/color]);
  printf ([COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]Returned Value = [/color][COLOR=#6a5acd]%d[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color],i);
  [COLOR=#804040][b]return[/b][/color] [COLOR=#ff00ff]0[/color];
}

c_call.f90
Code:
[COLOR=#0000ff]! Compile the C functions:[/color]
[COLOR=#0000ff]!   gcc -c functions.c[/color]
[COLOR=#0000ff]![/color]
[COLOR=#0000ff]! Compile & Link Fortran:[/color]
[COLOR=#0000ff]!   gfortran c_call.f90 functions.o -o c_call[/color]
[COLOR=#0000ff]!   or[/color]
[COLOR=#0000ff]!   g95 c_call.f90 functions.o -o c_call[/color]

[COLOR=#a020f0]program[/color] c_call
  [COLOR=#2e8b57][b]integer[/b][/color] :: res
  [COLOR=#804040][b]print[/b][/color] [COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]"* Calling C function from Fortran..."[/color]
  [COLOR=#a020f0]call[/color] sys_call()
  [COLOR=#804040][b]print[/b][/color] [COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]"* Back in Fortran..."[/color]
[COLOR=#a020f0]end program[/color] c_call

Compiling & Linking:
Code:
$ gcc -c functions.c
$ g95 c_call.f90 functions.o -o c_call

Running:
Code:
$ c_call
 * Calling C function from Fortran...
-rw-r--r--    1 Roman    Administ      338 Jan 24 05:53 c_call.f90
-rw-r--r--    1 Roman    Administ     1456 Jan 24 05:53 c_call.f90.html
-rw-r--r--    1 Roman    Administ      338 Jan 24 05:52 c_call.f90~
-rw-r--r--    1 Roman    Administ      201 Nov 30 11:41 f01.f
-rw-r--r--    1 Roman    Administ      201 Nov 30 11:41 f01.f90
-rw-r--r--    1 Roman    Administ      201 Nov 27 00:06 f01.f90~
-rw-r--r--    1 Roman    Administ      252 Nov 26 23:44 myfunctions.f
-rw-r--r--    1 Roman    Administ      345 Nov 26 23:38 myfunctions.f90
-rw-r--r--    1 Roman    Administ      394 Nov 26 23:16 test_myfunctions.f90
 * Back in Fortran...

Returned Value = 0

But here is little issue: Why Returned Value = 0 is printed after * Back in Fortran... ?
 
Hi, mikrom

Thank you very much again. I will try to follow your advise.
 
Hi gullipe,

But if you want to try this way be ready for some problems:
1. They may be problems with mixed-language-programming. What I showed you was very trivial example.
2. If you create your own version, you probably have to maintain it, e.g. if the conversion utility will be upgraded from the author to the newer version, you have to bother with the C-source once again and again...

The simplest way for you would be still what I recommended first: to call the ready conversion utility (DJPEG.exe or something other) using the Fortran's system() command.
 
Hi, xwb and mikrom

Well, I downloaded C2F and tried it on some of the DJPEG routines. First I tried to trantlate "example.c" (which actually is a demonstration program, that "does nothing useful") and got 2 errors, without any explanation of the errors. Then I tried to translate djpeg.c and got about 13 errors (without explanations) until I allowed the inclusion of jpeglib.h (which is needed). Then C2F crashed!

I probably still have to live with the simple solution that mikrom suggested, just to call a conversion utility from by Fortran program ... until some Fortran programming enthusiast takes on the task to make a program to convert JPG to BMP, preferably in F77 (which I mainly use).

Meanwhile I will possibly try to figure out which of the C routines in the DJPEG library are necessary just to convert JPG to BMP.
 
I can remember as I was student, I should write something for numerical mathematics in C. I found such a ready procedure in Fortran and tried to convert it to C with f2c. The conversion worked but the resulting C-source was in comparition to the original Fortran-source so complex, that I couldn't understand it and therefore it was unusable for me. So I wrote rather my own whenever not so professional procedure in C.
That's my experience with conversions :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top