How is jpg so much smaller then bmp

Started by
59 comments, last by giant 20 years, 1 month ago
Hi. I am writing a platform game in DX8, and wanted to do it in 24bbp colour, so that the quality would be high. I was worried that the download would become huge, since a fullscreen bitmap at 24bpp is about 1MB on disk. I was talking to a friend and he said that .jpg is smaller and I should try it, so I ran an experiment. I had a desktop picture of space and stars with lens flare etc... quite a nice picture. It was something like 970kb in its 24bpp .bmp format. I then saved this as .jpg and looked at the file size. It was only 28kb. I opened both with IE, and the picture quality looked almost the same. Prehaps the bmp was better, I dont know. I might have been convincing myself that it should be considering the size difference. How is it that jpg can store a picture of aprox. the same quality in such a small size. Thanks Ciaran "Only two things are infinite, the universe and human stupidity, and I''m not sure about the former." --Albert Einstein
Advertisement
yeah right!
JPG''s use lossy compression. The more you compress it, the more artifacts you''ll see. A JPG will never have the quality of a BMP. However, depending on the image and what it''s used for, it might not matter that much.

Mark Fassett
Laughing Dragon Entertainment
http://www.laughing-dragon.com

Mark Fassett

Laughing Dragon Games

http://www.laughing-dragon.com

JPGs are compressed and lossy (information is discarded or changed). BMPs are uncompressed (in most cases) and are lossless. I would not use a JPG image for a texture, since while it looks ok when viewed at the original size on its own, as soon as you start stretching and titling it the compression artifacts will become very visible. Use a lossless compressed format instead (PNG, TGA, BMP, etc).

If you do want to use jpeg textures, make sure to keep a copy of the originals in a lossless format for editing. Trying to edit a jpeg image is, well, dumb.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
or maybe try one of the DXT formats for DX textures.
Hmm, I''ve used JPEGs as textures, and they looked fine (didn''t notice any artifacts in game).

Some tricks JPEGs use to save space:
(all of this is "AFAIK" information. I.e. at least partly incorrect)

DCT, discrete cosine transform, a form of fourier tranformation is the heart of JPEG. Think of a sin or cos wave. You could either save each point of that wave ("BMP") or you could save the mathematical constants that make that wave ("JPEG"). Eg. 2*cos(5*x+3). Obviously the latter goes to much smaller space as it only needs to save 3 variables (amplitude, phase and frequency of the wave) for infinitely long wave, and the former one needs to store all the discrete points you want to store.

What do cosine/sine waves have to do with images then? Well, if you start reading the image from left to right, top to bottom, you can read image''s values sequantically. So we''ve linearized the 2-dimensional problem (this is the part I''m most unsure of. JPEG probably uses some kind of 2-dimensional DCT instead of this kind of linearizing).

It might be that the values you now read form a pure cosine wave but that''s not usually the case . Then we need to sum up several waves in order to get a more correct representation of the original image. The more waves we use, the better the image quality.

But doing DCT for the whole image would lead to ugly results (and it would be slow) so JPEG divides the image in 8x8 pixel squares. DCT is done separately for each of these squares, and squares with less information can be saved in fewer bytes without affecting the rest of the image (e.g. a simple gradient can be presented with very few cosine waves).

The power of DCT comes from the fact that natural images have generally very smooth forms, as do cosine waves. By summing up several waves, finer details can also be represented.

But the fun doesn''t end. Normally you think of bitmaps in RGB format, but JPEG transforms images to a color format that has 2 channels for color and one channel for brightness (Hue, saturation, brightness perhaps). The trick is that human eyes don''t notice colors so well, but they see the variations of brightness accurately. JPEG thus stores the brightness-channel with higher quality than the colour channels.

you missed the actual compression part:

after the values for the block have been expressed in the frequency domain ( by the IDCT ), you quantize the values in the block ( ie mul by another block called the quantization block ). Basically this produces a bunch of zero''s in the 8x8 block ( hopefully ). This determines the quality. Then the block is traversed in a zigzag pattern ( defined by the standard or image ) then huffman RLE encoded. This is where all the compression happens, the DCT is just a way of easily detecting and removing the difference in frequency.

This happens for each 8x8 block and for each colour channel (YCbCr). Pending on the YCbCr mode you get more compression by subsampling the CbCr values. Ie in 420 format it''s one cbcr for 4 Y values. Interesting thing about YCbCr is that you can drop the CbCr values and get a greyscale image, this how a colour tv channel can be decoded by b&w tele''s.

BTW Mpeg is exactly the same but with motion compensation ( and some protocol stuff but it''s orthogonal to the compression code. )
whoops that should read DCT.
quote:Original post by civguy
Hmm, I''ve used JPEGs as textures, and they looked fine (didn''t notice any artifacts in game).


Looking fine and being fine are 2 seperate issues here. Granted jpegs look wonderful but often in 3d we are manipulating our textures on screen be it through mip-mapping, Anisotropic filtering, non true Full Screen AntiAliasing, etc. We are consistantly messing with these textures. Seeing as how just about every video card approaches these abilities differently. Using a lossy compression such as JPEG creates a problem in that it is very possible that the filtering implementations of a specific video card could adversaly effect the textures quality. Artifacting is a minor issue in comparison to some of the more severe image problems that can occur when mixing a lossy compression with video hardware that does not do true filtering. Bottom line things may look wonderful on your machine but on someone elses machine your texture may look like an aerial view of the local dump. It''s best to minimize problems like these by using non-lossy image formats such as BMP, TGA, or PNG.

This topic is closed to new replies.

Advertisement