Image compression

11.6.2 Image compression

Image compression is remarkably similar to audio compression, except that it works in two dimensions rather than one. There may not be the same obvious wave pattern in images, but in digital photographs the natural dith- ering in shades of color compresses very well when DCT/Huffman com- pression is applied.

During the JPEG compression process, the image is split into macrob- locks, or 8×8 blocks of pixels. Each macroblock is then compressed using

a two-dimensional DCT to isolate and reduce the number of color har-

11.6 Lossy compression 299

monics within each area of the picture. The idea of waves existing within an image may seem alien, but they exist everywhere in natural textures.

The two-dimensional DCT can be expressed mathematically as follows:

2 + 1 ( 2 y + 1 )πv Svu (,) =

Cv ------- ------- Cu

)πu

2 2 ∑ ∑ fyx ( , ) cos --------------------------- cos -------------------------- 16 16

Cu is equal to 0.7071 (the reciprocal of root 2); when u is zero, Cu is one for all u not equal to zero. The same applies to Cv.

This formula produces a two-dimensional array, which can be com- pressed by rounding the near-zero values of the array to zero, then using RLE compression followed by Huffman compression.

Luckily, you will probably never have to implement JPEG compression from scratch. .NET has native support for JPEG, along with plenty of other image formats, including PNG, TIFF, and GIF. The following sample pro- gram shows you how to compress a bitmap image into a JPEG.

Start a new project in Visual Studio .NET. Draw a picture box, named pictureBox onto the form. Draw two textboxes named tbInput and tbOutput on the form, with two corresponding buttons, btnBrowseInput and btnBrowseOutput . The two browse buttons should have corresponding File Open and File Save Dialog controls, named openFileDialog and saveFileDialog , respectively. Finally, a button named btnCompress is also required.

The first step is to tie the File Open and File Save dialog boxes to the buttons to make it easier for users to select the relevant files. The open file procedure will also load the new image into the picture box.

Click on the Browse button opposite the Input textbox and enter the following code:

C#

private void btnBrowseInput_Click(object sender, System.EventArgs e)

{ openFileDialog.ShowDialog(); tbInput.Text = openFileDialog.FileName; pictureBox.Image= Image.FromFile(openFileDialog.FileName);

Chapter 11

300 11.6 Lossy compression

VB.NET

Private Sub btnBrowseInput_Click(ByVal sender As Object, _

ByVal e As System.EventArgs) openFileDialog.ShowDialog() tbInput.Text = openFileDialog.FileName pictureBox.Image= Image.FromFile(openFileDialog.FileName)

End Sub

Click on the Browse button opposite the Output textbox and enter the following code:

C#

private void btnBrowseOutput_Click(object sender, System.EventArgs e)

saveFileDialog.ShowDialog(); tbOutput.Text = saveFileDialog.FileName;

VB.NET

Private Sub btnBrowseOutput_Click(ByVal sender As Object, _

ByVal e As System.EventArgs) saveFileDialog.ShowDialog() tbOutput.Text = saveFileDialog.FileName

End Sub

To save a JPEG from a loaded image, you may simply call the Save method. The method requires the image format and a stream as input parameters:

C#

private void btnCompress_Click(object sender, System.EventArgs e)

FileStream fs = new FileStream(tbOutput.Text,FileMode.CreateNew); PictureBox.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg); fs.Close();

11.6 Lossy compression 301

VB.NET

Private Sub btnCompress_Click(ByVal sender As Object, _

ByVal e As System.EventArgs) Dim fs As FileStream = New FileStream(tbOutput.Text, _ FileMode.CreateNew) PictureBox.Image.Save(fs, _ System.Drawing.Imaging.ImageFormat.Jpeg) fs.Close()

End Sub

To test this application, run it from Visual Studio .NET, press the Browse button next to the Input textbox, and choose a bitmap from your computer. Click the Browse button next to the Output textbox, and select a location to save the JPEG. Press Compress, and then locate the new saved JPEG on your computer; you should notice that it will have a smaller file size (Figure 11.4).

Figure 11.4

JPEG Compression application.

Chapter 11

302 11.6 Lossy compression

You may notice that it is possible to take a JPEG file as an input to this program. Although the application will allow you to do this, the end result will be a JPEG file of even lower quality than the original.