[The OS/2 Supersite: The Starting Point for Warped Web Surfing.]

[Previous]
the REXX Files- by Dr. Dirk Terrell
 [Next]

Yesterday, while working on some new pages for the OS/2 Supersite, I came across something that I needed a Rexx program for. The program I was working on was one to generate some HTML files from a text file that contained all the images, hyperlinks, etc. for the web page. I wanted the program to generate the HEIGHT and WIDTH parameters for the <IMG=""> tags automatically.

If you've used my HTML Wizard program, you probably know that it automatically generates these parameters when you create an IMG tag. It's really not too difficult once you know a little bit about the structure of GIF and JPEG images.

Let's start off with GIF images this month. A GIF file begins with a header block as follows:

  1. A six-byte identifier, either GIF87a or GIF89a
  2. A two byte width field
  3. A two byte height field
  4. A one byte flag field
  5. A one byte background color field
  6. A one byte aspect ratio field

The identifier field makes it possible to detect a GIF file and since it is a string value, it is very simple to read it with a Rexx program. Since we want to read the file byte by byte, we'll use the charin function. Let's say that our image file (which, for now, we know is a GIF image) is test.gif. To read in the GIF identifier, use:

ID=Charin("test.gif",1,6)

Which means "starting at the first byte of the file test.gif, read six bytes and place them into the variable ID". With the above, we could write a very simple program to take as input a GIF file and print its identifier field:

/* Prints out the identifier field of a GIF file */
Parse Arg F
ID=Charin(F,1,6)
rc=Stream(F,"C","Close")
Say F "is in the" ID "format."
Exit
Now, on to the width and height fields. Technically, these are not the height and width of the image, but the height and width of the screen that was used to initially view the GIF file being created. However, I have found that most images have these values set to the width and height of the image. The actual dimensions of an image are to be found farther into the GIF file, and it takes a bit more work to get them out. Let's assume, for the time being, that the width and height fields in the header are the same as the image height and width. What we have is a two-byte field that we need to convert into an integer number of pixels. If you just read two more bytes after reading the identifier field and print out the result:
Width=Charin("test.gif",,2)
Say "Width is" Width "pixels."
you will probably see something that looks nothing like a number which, when run on a sample GIF file, returned:
Width is #! pixels.
The problem is that we have to convert the character string into an integer. With a two-byte integer, we have the "low" and "high" bytes that go together to represent the number. Recall that the eight bits that make up a byte can represent 2^8, or 256, numbers. With two bytes (16 bits), we can have 2^16, or 65,536 numbers. In a GIF file, the low byte comes first and then the high byte. So, to read them in, use something like this:
F="test.gif"      /* Saves some typing */
ID=Charin(F,1,6)
ByteLow=Charin(F,,1)
ByteHigh=Charin(F,,1)
By leaving the second parameter of Charin blank, we read from the position where we left off after the previous read.

Now we have the low and high bytes of the width field, but how do we convert the two bytes into one integer? That's where the C2D, or Character to Decimal, function comes in. C2D converts character strings into decimals. If you use C2D on a letter, it returns the ASCII code for that letter. For example:

Say C2D("A")
returns:
65
For our image width, we just concatenate the low and high bytes and pass that to C2D:
ImageWidth=c2d(ByteH||ByteL)
Let's modify our simple program above to print out not only the GIF identifier, but also the width and height of the image:
/* Prints out the identifier field of a GIF file and the image dimensions*/
Parse Arg F
ID=Charin(F,1,6)
WidthLow=Charin(F,,1)
WidthHigh=Charin(F,,1)
ImageWidth=c2d(WidthHigh||WidthLow)
HeightLow=Charin(F,,1)
HeightHigh=Charin(F,,1)
ImageHeight=c2d(HeightHigh||HeightLow)
rc=Stream(F,"C","Close")
Say F "is in the" ID "format."
Say "Width is" ImageWidth "pixels."
Say "Height is" ImageHeight "pixels."
Exit
Now you have a handy little program for printing out information about GIF files. You can also turn this code into a subroutine that could be called from within other Rexx programs and have it return the GIF type and image dimensions. If you generate HTML files with Rexx programs, you can now add the WIDTH and HEIGHT parameters to your IMG tags automatically, and that will probably save you a lot of time. Next month we'll look at the more complicated JPEG image format.

* * *

Dr. Dirk Terrell is an astronomer at the University of Florida specializing in interacting binary stars. His hobbies include cave diving, martial arts, painting and writing OS/2 software such as HTML Wizard.


 [® Previous]
[Index]
 [Feedback]
 [Next ¯]

[Our Sponsor: Sundial Systems Corporation - Productivity: Relish; Mesa 2; Clearlook; DBExpert.]

Copyright © 1997 - Falcon Networking ISSN 1203-5696