Edit: A newer version with full compression/decompression capability can be found later in this thread, here.
Warning: If you're not a math geek, this will probably not interest you!
This expression simulates part of the JPEG image format compression algorithm called the Discrete Cosine Transform (DCT). I built it to see how hard the math would be. You can find out what's involved in the complete algorithm by reading this article:
JPEG - Wikipedia, the free encyclopedia
This expression cannot read from or write to real .JPG files, nor does it do the entropy encoding to actually compress the data. It just does block splitting, two-dimensional DCT, quantization, and inverse DCT to get the original image back with some added noise. For a complete compression/decompression simulation, see my later post.
To start with, the expression uses a ranger camera similar to my E2 webcam to capture an image of the player standing in front of the chip. It's been overclocked using the same technique as the webcam to 2144 Hz, and can take regular or anti-aliased pictures at about 2 seconds per 64x64 pic.
Once the picture has been stored in an array, it is then processed block by block. Typical block size is 8x8 pixels, although any block size is valid as long as the screen dimensions are a multiple of the block size and both are square shaped. Set the SRes value in the expression for the screen resolution and the Res value for the block size. A 64x64 pic with block size of 8 will be processed in 4 seconds and scales up linearly with the total number of pixels.
The first pass takes the one-dimensional DCT of each horizontal row of pixels in the block, puts those results in a 8x8 block in a new array, then moves on to the next block. The second pass does the same one-dimensional DCT on each vertical column of results from the first pass and begins as soon as the first pass finishes a block. The third and fourth passes do the inverse vertical DCT and inverse horizontal DCT respectively, and all four passes overlap eachother to complete in almost the same time as it takes to do a single pass.
In between the second and third pass is the quantization step. This is the point where the results from the DCT are normally divided (rounded in my example) by given quantization amounts so that the final block has a lot of zeros that represent visual frequencies with such low power that they can be eliminated from the image without much noticeable effect. The size of the quantization divisors determine the final quality of the image - low numbers give a high image quality but lower chance for compression, and high numbers give a low image quality and higher compressibility. A good reference on quantization can be found here.
I used a diagonally gradiated quantization table based approximately on the JPEG standard table and also negatively scaled it to resemble the JPEG "Quality" setting of 0-100 (0 for very low quality and 100 for almost perfect quality). You can actually set it to a number below zero to keep decreasing the quality (and theoretically further improving compressibility).
What the digital screens in the screenshots represent:
The screenshots attached show the simulated compression and decompression of a 64x64 image that has been separated into 8x8 blocks.
Screen 1 is the original image as seen by the chip. The only effect applied to it is optional anti-aliasing, which can be toggled by pressing the num key 1.
Screen 2 shows a summarization of the DCT table for each block of the image at that quality level. The upper left corner of each block normally equals the average grey value (0-255) of the entire block. I've colored it red if it's greater than zero and greyish if it equals zero, mostly to show the separation of the blocks. All the other dots within each block represent the various combinations of vertical and horizontal frequencies and are white if non-zero and black if zero. With lower quality settings there will be more values that round down to zero and therefore less white dots.
Screen 3 is what the compressed image would look like. It will contain noticeable visual artifacts at lower quality settings.
Screen 4 is the absolute difference between the pixels of screens 1 and 3, and is exaggerated.
The info screens show the quality and AA status, as well as a crude estimate of possible compression ratio (number of non-zero pixels in DCT table / total number of pixels) and the average absolute per-pixel error of the final image. An error of 1 means that on average each pixel is off by 1 shade of grey in its range of 0-255.
How to use it:
Spawn the "jpeg simulator" file
Press the +/- keys to adjust the quality
Press 1 to toggle AA
Press 0 to take a picture of yourself
Press Enter to compress it
Press period (.) to reset the expression and screens in case something breaks.


LinkBack URL
About LinkBacks




Reply With Quote








). In theory, the final image should look exactly the same as the simulated one in Screen 3, as there is no further loss of information after the DCT stage.

Bookmarks