# JPEG Compression#

This application note explains the use of the Basler JPEG compression operators (Application Note SAN013).

You get information about the functionality of the operators. Using a Visual Applets design, a JPEG compression application is shown. An SDK project is presented which generates JPEG data files.

For JPEG compression in VisualApplets two operators are required. These are the `ImageBuffer_JPEG_Gray` and the `JPEG_Encoder_Gray`. The image buffer is used to buffer the image data in frame grabber memory and is used for a specific reordering of the pixels. The JPEG encoder performs the JPEG compression itself.

The reordering of the pixels is required because for JPEG compression blocks of eight by eight pixels are used. The following figure illustrates the reordering.

A reordered image will look like the following. The operator keeps the image dimensions of the original image.

After the reordering the JPEG compression can be performed using operator `JPEG_Encoder_Gray`. The operator's output is a Huffman stream without JPEG headers. The compression rate depends on the selected quantization table which is changeable during runtime. For Huffman coding the standard luminance table is used.

Operator Documentation

Refer to the Operator Reference for a detailed description of parameters, input links, and operator functionality.

## JPEG Compression Design Example#

The following design example will show a Visual Applets design which performs a JPEG compression. The design is required to perform the following specifications:

• Grab images from a camera-link base area camera.
• Buffer the image data and perform the JPEG compression.
• Add a JPEG header to the image in software and write selected images to file.
• The image height of the camera is not constant. The varying height of the image has to be added to the JPEG header.

### VisualApplets Design#

The following design is used to meet the specifications.

This simple design is sufficient to meet all specificationsa. The image is grabbed using a base area camera operator. Next, the image is split into two paths. The first path buffers the image and performs the pixel reordering using operator ImageBuffer_JPEG_Gray. The JPEG_Encoder_Gray is used afterwards for JPEG compression. The JPEG data stream is transferred into host PC memory via a DMA channel. In the second path, the original image data is buffered and transferred to the host PC using a second DMA channel.

### Simulation of the Visual Applets Design#

The design can be fully simulated. By placing simulation sources and probes on the data links the respective steps of the JPEG compression can be monitored. The figure below shows the design with simulation modules. As can be seen, the pixels of the original image (simModule0) are reordered (simModule1) and the JPEG data stream is generated (simModule2).

When looking at the JPEG data the length of the stream can be determined (see figure below). If the quality in the JPEG encoder is changed, the data length varies respectively.

### Runtime Test in microDisplay#

Any applet including a JPEG Encoder can directly be used in microDisplay. For the given example the applet results in one image window for each DMA channel. DMA number one shows the original image grabbed, while the first DMA channel shows the encoded image data along with the DMA length.

### SDK Project for JPEG File Generation#

A design file of the example together with a SDK example project can be found in folder `examples\processing\advanced\JPEG\JPEG_gray` in the VisualApplets installation path.

The SDK project basically grabs the original image and the JPEG data. The original image is directly output to display. A JPEG header is generated and added to the JPEG data. This completes the JPEG file. The user has the possibility to write a JPEG snapshot to file. Furthermore, the encoded JPEG data is decoded using the simple C-code decoder `nanojpeg`. The decoded image is displayed.

The user has the possibilities to resize the image, change the JPEG quality, write jpeg snapshot files or write JPEG data hex file dumps.

Note that the header generation is only an example. Users of the Visual Applets JPEG operators should write their own header generators adapted for their applications.

JPEG decoder information:

``````NanoJPEG -- KeyJ's Tiny Baseline JPEG Decoder

Version 1.0 (2009-04-29)

by Martin J. Fiedler <martin.fiedler@gmx.net>
``````

1. The operator `ImageBuffer_JPEG_Gray` is only available for the platform microEnable IV.