Compressing Pixel Differences

Since compression algorithms such as lzw or deflate work by reducing redundancy, I came up with this idea to make photographic images compress better with them. Unfortunately, I didn't realize that the PNG file format already does at least part of this :). Anyway, my two ideas were this: 1) instead of compressing the colors themselves, compress the differences in color between pixels next to each other and 2) compress red, green, and blue parts of a pixel seperate from each other. The theory behind compressing the differences in colors is in a photographic image, there are a lot of gradients. Anyway, if you had an image where the pixel values from left to right were (120,122,124,126) if these values were "compressed" with lzw or such, the file size could not be made smaller. However, if the differences in pixel values were compressed (-120,2,2,2) this has a lot of redundancy. Now the reason for compressing the red, green, and blue parts of a color image separate.... maybe I'll explain my idea on that later. Either way, I tried it on these two images: alligator.jpeg and alligator-gray.jpeg. This image was originally a large jpeg from a digital camera that I shrunk down to 400x240 and turned into a bmp. I also turned it into a PNG and GIF to compare file sizes with those. Anyway, here are the results:

Update: I changed compression algorithms from lzw to zlib's deflate (using the default compression of 6)


CONTENTS

Home
Updates
Software
Electronics
Music
Resume
Contact


YouTube
Twitter
GitHub
LinkedIn


Compressing Pixel Differences

Graphics: SSE Image Processing, GIF, TIFF, BMP/RLE, JPEG, AVI, kunzip, gif2avi, Ringtone Tools, yuv2rgb, RTSP
RAW BMP PNG RAW-LZW RAW-DIFF-LZW RAW-ZLIB RAW-DIFF-ZLIB GIF
alligator
(24 bit)
288000 288054 214659 324454 305521 252653 231603 n/a
alligator
(8 bit)
96000 97078 84811 107505 101514 83617 77158 108638

The results of this test prove that my theory does work on at least this image. The RAW-DIFF column creates a file smaller than the original. However, it's interesting to note that the "compressed" image is bigger than the RAW (or BMP) image. Also interesting to note that the PNG images are smaller than the RAW or BMP. When I saw this, I wondered if my LZW routines were flawed so I made a GIF from the gray-scale image which ended up to be a similar size as the non-diff version. One thing to note: the LZW I used for here currently is identical to GIF except I removed a stupidity in GIF which wraps the compressed data in 255 byte frames (thus should reduce the file size by around 1/255 :). Anyway, I think this test also shows that DEFLATE/INFLATE (the compression used in PNG) is far superior to LZW.

Copyright 1997-2024 - Michael Kohn