![]() |
Multimedia »
General Graphics »
Image classes
Intermediate
A C# MPEG1 Image Compression ClassBy Mike DonovanThis article shows how to compress an image using the MPEG1 format |
C#, Windows, .NET 1.0, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

There is a lot of support in C# for image conversion and compression, but I have never found a good tool to convert images into the MPEG format. I wish someone had done it for me, but since they didn't I wrote a class called MPEGFunctions that provides the functions you need to convert a bitmap image to an MPEG1 I Frame. The code is functional but definitely not optimized or efficient. I think the MPEGFunctions class and the provided sample application are a pretty good learning tool if you want to learn more about MPEG image compression.
There are a couple reasons why I needed a program like this. The first is that I have some MPEG animations on my web site, and I wanted to put up a short title screen at the beginning of the video clips. I can create a title screen using something like Microsoft Paint, convert it to an MPEG sequence of 2 second duration, and then play it with Windows Media Player (Media Player won't display JPEGs but will display my MPEG sequences). Another reason is I work with digital set top boxes, and I needed a way to create MPEG still frames that the boxes could display. Set top boxes are good at rendering MPEG but usually can't display JPEGs.
The MPEG1 standard (ISO/IEC 11172 Part 2) describes the process used to encode and compress an image into the MPEG1 format. A good tutorial link I found on the web that helped me a lot can be found here:
or do a Google search on: jpeg dct cmpt365.
The source code for the MPEGFunctions class is in the file named Class1.cs. The source code for the ImageFunctions class is in the ImageFunctions.cs file.
The simplest MPEG frame type is the I Frame. To convert an image to an MPEG I Frame you need to start with an RGB bitmap and go through the following steps:
ImageFunctions.padWidth and ImageFunctions.padHeight functions.
CBblock. Convert the RGB values for the pixels into Y, CR, and CB color components. Use the MPEGFunctions.getYMatrix, MPEGFunctions.getCRMatrix, and MPEGFunctions.getCBMatrix functions.
MPEGFunctions.calculateDCT function.
MPEGFunctions.Quantize function.
MPEGFunctions.Zigzag function.
MPEGFunctions.DCHuffmanEncode function.
MPEGFunctions.ACHuffmanEncode function.
The sample application shows how to use the MPEGFunctions class.
MPEG I Frames are very similar to JPEG images, in fact the process to create a JPEG is virtually identical to MPEG. The syntax of the final bitstream is different, but with some simple changes you could turn the MPEGFunctions class into a JPEGFunctions class. However, in C# it is so simple to save images as JPEGs that it really isn't necessary. You can see how simple it is to convert a bitmap to JPEG in my sample application in the UserControl1.WriteJPEG function.
MPEG is not really designed to be a format for single image compression. It is actually supposed to be used for video compression, and a lot of its usefulness comes from using previous and future video frames to make predictions for what any single video frame will look like. These are called P and B frames and allow video to be compressed even more efficiently than if only I Frames are used.
So far I can only do MPEG I frames, but I hope to keep going with this project and add the functions needed to create MPEG P and B frames. I will also create a JPEGFunctions class someday that is similar to the MPEGFunctions class. Hopefully in 3 months or so there will be an update to this project.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 22 Jan 2004 Editor: Nishant Sivakumar |
Copyright 2004 by Mike Donovan Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |