ECEN 1200 - Telecommunications 1

Peter Mathys, Fall 2006, 10/13/06


Homework/Computer Lab 7: Characterization of Images, Image Compression and Formats, Including Images in Web Pages


How to Submit Solutions for this Homework/Computer Lab
Due Date: Friday 10-27-06
E-mail To: no-spam e-mail
Subject Line: HWCL 07
1'st Line: Your name and student number

This computer lab is written for Windows 9x/2000/NT/XT based PCs, with a Mozilla/Firefox Browser or an Internet Explorer. Those portions of the lab that are not platform-specific can also be run on a Mac.

Quick Links


Goals of this Lab

The goals of this computer lab are:


Programs to Download

7-Zip Icon 7-Zip is a file archiver with high compression ratio. It supports the 7z, ZIP, GZIP, BZIP2 and TAR formats for both packing and unpacking. Unpacking only is supported for the RAR, CAB, ISO, ARJ, LZH, CHM, Z, CPIO, RPM, DEB, and NSIS formats. The home page of 7-Zip is at http://www.7-zip.org.

Image Analyzer Icon Image Analyzer 1.26 is an image editing, enhancement and analysis program. It can read/write and convert between BMP, ICO, CUR, WMF, EMF, JPEG, PNG, PCX, JPEG2000, GIF, and TIF image formats. The home page of Image Analyzer is at http://meesoft.logicnet.dk.

LViewPro Icon LViewPro 1.D/32 is a very compact image viewer, editor, and file format converter. The home page of LViewPro is at http://www.lview.com.

Mona Lisa Icon images2.7z contains a set of 4 selected images in uncompressed TIFF (Tagged Image File Format) format with 24 bits per pixel.


How to Get Images, Introduction

Images that you want to use in your HTML documents may come from various sources such as a (digital) camera, a scanner, an e-mail attachment, etc. For our purposes, the most common method will probably be to locate images on the WWW (e.g., by using one of the search engines we studied earlier). When you find a suitable image, you just simply right-click on it and then select "Save Image as ..." (Firefox) or "Save Picture as ..." (Internet Explorer) to save it as a file on the computer on which you are currently working. To incorporate the image in an HTML document of your own, you use an <img> tag. Both the HTML file and the image file then need to be uploaded to your WebFiles account on a WWW server. Often you will not want an image in exactly the same form as you obtained it. You may want to change the size of the image, or only use a portion of it. Sometimes you also need to change the format in which the image is stored. And last but not least, how you place images within your HTML document can have a great impact on the perception of the images by a viewer. This computer lab is designed to give you an idea of what's possible and to introduce some useful tools that will help you implementing your own designs.


Images in HTML Documents

Note: Some of the img tag attributes shown in this section have been declared deprecated in HTML 4.01. Wherever possible, HTML should only be used to specify the structure of a document and its visual presentation should be controlled by CSS (cascading style sheets). CSS1 replacements for the deprecated HTML attributes are given below

An image can be inserted (almost) anywhere in an HTML document using an <img> tag, e.g., as shown in the following table.

HTML Element(s) Resulting Display
<img src="designs.png"
  alt="A Small Design">
A Small Design
<p align="center">
  <img src="designs.png"
  alt="A Small Design">
</p>

A Small Design

<p align="right">
  <img src="designs.png"
  alt="A Small Design">
</p>

A Small Design

<center>
  <img src="designs.png"
  alt="A Small Design">
</center>
A Small Design

The first row shows the basic tag that can be included anywhere in a text, e.g., A Small Design, as if it were another character. Note the alt attribute that is used to give an alternate description for browsers which do not or cannot display images. You can also make an image clickable, e.g., A Small Design, by including it inside an anchor. The HTML code for this example is

<a href="designs.png"><img src="designs.png" alt="A Small Design"></a>.

The other three examples in the above table show how to position an image by itself in a paragraph that is either left- or right-aligned, or centered.

You can also wrap text around images by using the align="left" or the align="right" attribute specifications in the <img> tag as shown in the following table (note that align="center" does not exist).

HTML Element(s) Resulting Display
<img src="fairy.png" alt="Fairy"
  align="left" hspace="10" />
<p>The inscription read:
  Here lies the Lovely Fairy
  with Azure Hair who died of
  grief when abandoned by her
  little brother Pinocchio.
</p>
Fairy

The inscription read: Here lies the Lovely Fairy with Azure Hair who died of grief when abandoned by her little brother Pinocchio.

<img src="fairy.png" alt="Fairy"
  align="right" hspace="10" />
<p>The inscription read:
  Here lies the Lovely Fairy
  with Azure Hair who died of
  grief when abandoned by her
  little brother Pinocchio.
</p>
Fairy

The inscription read: Here lies the Lovely Fairy with Azure Hair who died of grief when abandoned by her little brother Pinocchio.

To stop text from flowing around a left-aligned image use the <br clear="left"> tag. Similary, <br clear="right"> stops text from flowing around a right-aligned image. You can also use <br clear="all"> to stop text flowing around both left- and right-aligned images.

The next table shows some of the attributes that can be used in the <img> tag.

Attribute Attribute Values Purpose
align left, right Aligns image with respect to the surrounding text
alt description Describes the image during loading and for text-only browsers
border 0,1,2,... Makes border of specified width around image
height 1,2,3,... Specifies height (in pixels) of the window in which the image is placed
hspace 0,1,2,... Specifies space (in pixels) around image in horizontal direction
src url Specifies the URL of the image file
vspace 0,1,2,... Specifies space (in pixels) around image in vertical direction
width 1,2,3,... Specifies width (in pixels) of the window in which the image is placed

An example of an HTML document that contains images and uses many of these tags is the Balloon Fiesta page.

Important: Note that the HTML document with the text and the image file are

two separate files.

To display the text and the image properly on the WWW, both files must be present in your public_html directory (or a subdirectory of it).


CSS1 Replacement of Deprecated HTML 4

Definition: Deprecated. A deprecated HTML element or attribute is one that has been outdated by newer constructs. Browsers will generally still support deprecated HTML for backward compatibility, but new HTML documents should avoid the use of tags and/or attributes that have been marked as deprecated.

Deprecated img tag attributes for "stand-alone" image placement:

Deprecated HTML Examples
HTML Element(s) Resulting Display
<p align="left">
  <img src="designs.png"
  alt="A Small Design">
</p>

A Small Design

<p align="center">
  <img src="designs.png"
  alt="A Small Design">
</p>

A Small Design

<center>
  <img src="designs.png"
  alt="A Small Design">
</center>
A Small Design
<p align="right">
  <img src="designs.png"
  alt="A Small Design">
</p>

A Small Design

CSS1 img tag attribute replacements for "stand-alone" image placement:

CSS1 Replacements for Deprecated HTML Examples
HTML/CSS Element(s) Resulting Display
<div style="text-align: left;">
  <img src="designs.png"
  alt="A Small Design" />
</div>
A Small Design
<div style="text-align: center;">
  <img src="designs.png"
  alt="A Small Design" />
</div>
A Small Design
<div style="text-align: right;">
  <img src="designs.png"
  alt="A Small Design" />
</div>
A Small Design


Deprecated img tag attributes for floating image placement:

Deprecated HTML Examples
HTML Element(s) Resulting Display
<img src="fairy.png" alt="Fairy"
  align="left" hspace="10" />
<p>The inscription read:
  Here lies the Lovely Fairy
  with Azure Hair who died of
  grief when abandoned by her
  little brother Pinocchio.
</p>
Fairy

The inscription read: Here lies the Lovely Fairy with Azure Hair who died of grief when abandoned by her little brother Pinocchio.

<img src="fairy.png" alt="Fairy"
  align="right" hspace="10" />
<p>The inscription read:
  Here lies the Lovely Fairy
  with Azure Hair who died of
  grief when abandoned by her
  little brother Pinocchio.
</p>
Fairy

The inscription read: Here lies the Lovely Fairy with Azure Hair who died of grief when abandoned by her little brother Pinocchio.

CSS1 img tag attribute replacements for floating image placement:

CSS1 Replacements for Deprecated HTML Examples
HTML/CSS Element(s) Resulting Display
<img src="fairy.png" alt="Fairy"
  style="float:left;
  margin:0 10px 2px 0;"
/>
<p>The inscription read:
  Here lies the Lovely Fairy
  with Azure Hair who died of
  grief when abandoned by her
  little brother Pinocchio.
</p>
Fairy

The inscription read: Here lies the Lovely Fairy with Azure Hair who died of grief when abandoned by her little brother Pinocchio.

<img src="fairy.png" alt="Fairy"
  style="float:right;
  margin:0 10px 2px 0;"
/>
<p>The inscription read:
  Here lies the Lovely Fairy
  with Azure Hair who died of
  grief when abandoned by her
  little brother Pinocchio.
</p>
Fairy

The inscription read: Here lies the Lovely Fairy with Azure Hair who died of grief when abandoned by her little brother Pinocchio.

An example of an HTML document that contains images and uses CSS for image placement is the Balloon Fiesta (using CSS) page.


Bitmapped and Vector Graphics, Image Resolution

Two different methods

Vector graphics: Example

Bitmap graphics: Example

Vector graphics: Resizing

Bitmap graphics: Resizing

Vector graphics

Bitmap graphics

Bitmap vs vector graphics


Image Formats, Compression

Images are an important medium for communication and you are all familiar with the saying that "an image is worth a thousand words". When using electronic means of communication, the big question is: How are images represented electronically such that they can be distributed universally and economically? In practice there is no single universal format for images that satisfies all requirements for all images. Because different types of images have different properties and different applications have different requirements and, last but not least, politics are an important consideration for many companies, there exist quite a few different image file formats. Some of the most important ones are:

In this lab you will use the GIF, JPEG, PNG, and (uncompressed) TIFF formats. In the next section, you will use the Image Analyzer program to convert between these file formats and you will look at the compression ratios that can be achieved using the GIF, JPEG, and PNG file formats for different types of images.


Cropping, Resizing, and Converting Images

If necessary, download and install the Image Analyzer program on your computer (use 7-Zip to unpack the archive). The first step for all operations is to open an image in Image Analyzer as shown in the following screen snapshot.

Opening an image file in Image Analyzer

To open TIFF files in Image Analyzer, make sure you have the TIFFRead.dll plugin saved in the same (sub)directory as the Analyzer.exe file.

To crop an image, e.g., color.tif, use the "Rectangle Select Tool" to specify an area within the picture. The result is shown in the following figure.

Selecting an image area in Image Analyzer

Then select "Image" and "Crop" as shown next.

Cropping an image in Image Analyzer

At this point the image is cropped, but not yet written to a file. If you don't like the outcome of the cropping, you can select "Edit" and then "Undo" to restore the original image. To save a cropped image under a new filename, choose "File" and then "Save As" as shown in the following screen snapshot.

Saving an image in Image Analyzer

Make sure you select the right image format (at the bottom of the window shown above) in which you want to save the edited image (e.g., JPEG, PNG, or GIF).

To resize an image, first load the image as shown earlier, and then select "Image;" and "Resize" as shown next.

Resizing an image in Image Analyzer

You can either enter the new image dimensions in pixels, or select them in the form of a percentage of the current size. If the box labeled "Maintain aspect ratio" is checked, then the horizontal and vertical size changes are coupled so that the aspect ratio of the image does not change. If you unclick this box, you can change the horizontal and vertical sizes independently, e.g., to achieve effects like the following:

Resizing an image with variable aspect ratio in Image Analyzer

If you are satisfied with the result of your resizing operation, then save the resulting image as described above (make sure you select the desired output format). Otherwise choose "Edit" and then "Undo" to restore the original image.

To convert an image from one format to another, first load the image (make sure you select the desired input format), and then, without cropping or resizing or any other editing, select "File" and "Save As". Select the desired output format to which you want to convert at the bottom of the "Save As" window, and then click on "Save". Note: When you convert images to the GIF format, the image cannot have more than 256 colors (or 256 gray levels for black & white images). To reduce the number of colors go to "Image" and then select "Convert to 256 Colors" as shown below.

Reducing the number of colors to 256

There are different methods that can be used to reduce the number of colors in an image. One method, called dithering, attempts to approximate the original color by using a pattern of pixels, selected from a set (or palette) of 256 colors. This is widely used in the printing industry. For Web pages on computer monitors, where the image resolution is much less than for printed media, it usually works better to use the "nearest color" method shown in the following screen snapshot.

Color reduction: Selection of Conversion Method

The effect of the color reduction is shown in the next figure.

Image after color reduction using nearest color method

Huffman Coding

The file size of a bitmapped image grows very quickly as image size and color resolution increase. The general formula is

total # bits = width x height x bits/pixel.

A one megapixel digital camera (with an image quality roughly equivalent to a 3.75 x 5 inch print), for instance, produces images that have 1280 x 960 = 1,228,800 pixels and 24 bits per pixel (corresponding to 224 = 16,777,216 colors). Thus

total # bits = 1280 x 960 x 24 = 29,491,200.

After dividing by 8, this yields an uncompressed file size of approximately 3.7 MB (megabytes). Even under the best of circumstances, this would take about 9 minutes to download over a telephone modem.

To keep the required storage space small and download times short, images are generally compressed, using either lossless compression (e.g., for GIF or PNG files) or lossy compression (e.g., for JPG or JPEG files). Lossless compression schemes are generally based on the idea of using short binary strings for representing features that occur frequently in an image and longer strings for features that occur more infrequently.

As an example, consider the "w.w.w" bitmapped image shown (enlarged) below.

A bitmap image with 512 pixels

The image is 32 pixels wide and 16 pixels high, resulting in a total of 512 pixels. There are 7 different colors, and thus the straightforward fixed length binary representation of each pixel requires 3 bits (23 = 8 > 7), e.g., using a code like the following:

   color   | binary code
   --------+------------
   black   |    000
   magenta |    001
   purple  |    010          This uses a total of  512 x 3 = 1536 bits
   white   |    011          or an average of  3 bits/pixel
   navy    |    100
   lime    |    101
   yellow  |    110

Obviously, black occurs most frequently (256 pixels), followed by magenta (128 pixels). These two colors should therefore have shorter binary representations than, for instance, yellow which occurs much less frequently (16 pixels). But how many bits should be used in each case and how should the bit values be assigned so that the image can be restored again later without ambiguity? Fortunately, there is a fairly simple procedure, known as Huffman coding, that answers this question quite easily. It consists of generating a binary tree, starting from the leaves of the tree and systematically adding internal nodes until the root (usually drawn at the top) of the tree is reached.

The starting point to build a Huffman code is to list all items of interest in decreasing order of their probability of occurrence. For the "w.w.w" image the items of interest are the seven colors

   black (256 pixels), magenta (128 pixels), purple (32 pixels), white (32 pixels),
   navy (32 pixels), lime (16 pixels), yellow (16 pixels).

To obtain the probability of occurrence, the number of pixels for each color is divided by the total number of pixels, e.g., 128/512 = 1/4, and thus the probability of a magenta pixel is 1/4. The next figure shows all colors in the "w.w.w" image together with their probabilities, ordered such that the lowest values are on the right.

Huffman coding of 512 pixel image, step 1

To make a binary code, the next step is to interpret the two colors with smallest probabilities (pick any two if there are more than two with the same probability) as being descendants of the same parent node, as shown in the next figure.

Huffman coding of 512 pixel image, step 2

The parent node is then labeled with the sum of the probabilities of its children. Now, the problem is reduced to finding a Huffman code for the new parent node and all other nodes that are not descendants of this new node. Among all these nodes pick again the two with the smallest probabilities (pick any two if there are more than two with the same probability), and interpret them as the children of a parent node, as shown below for the example considered here.

Huffman coding of 512 pixel image, step 3

Note that since there were 4 nodes with probability 1/16 each to choose from, the above choice is not unique. Each of the other possible choices will lead to a different Huffman code, but all these codes will compress the data by the same amount. The following 4 pictures show the completion of the recursive procedure to build the Huffman code tree until the root (with a probability value of 1) is reached.

Huffman coding of 512 pixel image, step 4

Huffman coding of 512 pixel image, step 5

Huffman coding of 512 pixel image, step 6

Huffman coding of 512 pixel image, step 7

Once the location of the root is known, the code for each item (color in this example) can easily be read off from the labeling along the links between the nodes. The result for encoding the "w.w.w" image is:

   color   | binary code
   --------+------------
   black   |   0
   magenta |   10            This uses a total of
   purple  |   1100          256 x 1 + 128 x 2 + 3 x 32 x 4 + 2 x 16 x 5 = 1056 bits
   white   |   1101          or an average of  2.0625 bits/pixel
   navy    |   1110          ==> Compression ratio  1.45:1
   lime    |   11110
   yellow  |   11111

Obviously, the choice of labeling left branches in the tree with 0 and right branches with 1 is arbitrary and any other labeling (as long as it assigns different bit values for the descendants of the same parent) would have worked just as well. The Huffman coding scheme in this simple example achieved a reduction in the number of bits used for the image by about 1/3. Instead of a 9 minute download time this would correspond to a 6 minute download time. To achieve more compression, more complicated schemes are needed which not only take into account the probability with which each color occurs, but also the geometric arrangement of pixels and possibly also the physiology of the human vision system.


Transmission Speeds, Image Load Time

Images (and movie/sound files) take by far the longest to download when you access Web pages. The following table lists typical connection speeds for various Internet connections.

Typical Transmission Speeds
Name Speed Description
V.34 28.8 kbit/sec Full-duplex telephone modem
V.90 56 kbit/sec Asymmetric telephone modem (56k download, 28.8k upload))
Ethernet 10 10 Mbit/sec 10 Mb Ethernet LAN (bandwidth shared with other users)
Ethernet 100 100 Mbit/sec 100 Mb Ethernet LAN (bandwidth shared with other users)
T 1 1.544 Mbit/sec High Speed WAN (Telephone Network) Access
OC-12 622.08 Mbit/sec Internet Backbone, Optical Carrier

Note that all speeds are given based on bits whereas file sizes are usually given based on bytes (1 byte equals 8 bits). Thus, a 3.7 MByte image has 29.6 Mbits and takes 29600/28.8=1028 sec to download on a V.34 modem. If you are the only one using a 100 Mbit/sec Ethernet, then the download time reduces to 29.6/100=0.3 sec. But if 20 users use the same Ethernet simultaneously (e.g., in the computer lab), then the download time increases to 6 sec. It turns out that the vast majority of Internet users are still connected using telephone modems, and if you want to achieve reasonable download times (e.g., less than a minute) for these users, you have to be very cautious to select images that are small enough in (compressed) file size.


Uploading Images to your WebFiles Account

The following screen snapshot shows an HTML page that contains image tags, just after it has been created (e.g., in Notepad) and saved on a local computer under the name lakeside.html.

Page source of lakeside.html page

The next figure shows the HTML file and the associated image files in Windows Explorer.

Files to be uploaded to WebFiles server

Altogether there are 4 files for this Web page and all of them need to be uploaded into the www directory on the WebFiles server. The following picture shows the preparation for uploading all 4 files.

Select HTML and image files for uploading

After uploading the files you need to check the Share Permissions. When the files are uploaded for the first time, they are stored as Not Shared by default, as shown below.

Check Sharing Permissions after upload

To be accessible over the WWW, all files, HTML and image files, must have the Public Permission set as shown next for the image file lakeside_tower.jpg.

Images must have Public read permission too

The next screen snapshot shows what happens if not all images have their permission set to public.

HTML page with only one image made Public

And here is the final result after all Share Permissions are set correctly.

HTML page after all share permissions are correct

To be sure that a Web page is accessible to the general public, use a different computer than the one from which you uploaded the files, and type in the URL of your Web page.


Your Task

To obtain credit for this homework/computer lab you need to answer the questions stated below. Send your solution to no-spam e-mail.

Rules for the Submission of Homework/Computerlab Solutions

Format: E-mail your solution as a plain text (ASCII) file. Do not use word processor files like Microsoft Word or "rich-text" HTML files.

Corrections: If you need to make corrections after you submitted your solution, resubmit all your answers (not only the ones that changed) since only your last submission of each homework/computer lab will be graded.

Teamwork: Teamwork is fine for the homework/computer labs, but the solutions must be turned in individually. In particular, copy and paste of entire solutions from other students is not acceptable.

Questions:

  1. The pdf (portable document format) file format allows text and images to be stored either in bitmapped or in vector format (with a preference for the latter when possible). Use the Adobe Acrobat reader (download from www.adobe.com if necessary) to look at the documents in test01.pdf and in test02.pdf. Which of the two is stored in bitmapped format and which is in vector format? How can you tell?
  2. Download the images in the images2 archive, and unzip them. Then use Image Analyzer to convert all 4 images first to PNG format, then to JPEG format, and then to GIF format (use "nearest color" method to reduce image to 256 colors). Open up a Windows Explorer (with detail view) so that you see the file sizes of both the original and the converted files. Determine the compression ratios (compared to the original TIFF files) for the PNG, the GIF, and the JPEG versions of the image files. Find out which format works best for which image in terms of how the image looks and in terms of the resulting file size. Can you deduce general criteria for deciding when to use PNG, JPEG, and GIF compression?
  3. How much difference on the file size does color make? To find out, start from the MonaLisa.tif image. Then, under "Image", select "Convert to Grayscale" to obtain a black & white image. Convert this image first to PNG format, then to JPEG format, and finally to GIF format. Compute the compression ratios for the three files (compared to the original TIFF file) and compare with the results you obtained for the colored image. What conclusions can you draw?
  4. Find a Huffman code to represent the pixel colors for the image below as efficiently as possible in the form of variable length binary strings.

    Taxi Image

    In your solution state the binary strings that are used for each color explicitly and compute the compression ratio for your code.
  5. Look at the Balloon Fiesta page and list all image attributes that were used. Also look at the Balloon Fiesta (using CSS) page and show which image attributes in your list were replaced by CSS style/class declarations and how these styles/classes were defined. Then compute how much time it takes for a browser to download this page (all images and text) over a telephone modem with 28.8 kbit/sec transmission speed.
  6. Make your own Web page with text and images and send the URL in your solution. You can either add to your existing homepage or make a new HTML document. The conditions for this page are as follows:
    1. You can choose any subject that you like and either use your own images or images from the WWW.
    2. The page must load in one minute or less over a telephone modem with 28.8 kbit/sec speed.
    3. The page must have at least two images, one with text flowing around, and one which is in a paragraph of its own (no text to the left or right).
    You can use either (deprecated) HTML attributes or the (recommended) CSS style declarations for the image placement. Look at the Balloon Fiesta or the Balloon Fiesta (using CSS) page for examples. Note: In order for images to show up on a Web page, they must have public read permission. Check the share properties of your image files and, if necessary, set them to public. Check that your Web page can be viewed on a computer different from the one which you used for uploading.