Lossless JPEG RotationSince JPEG compression is "lossy" any edits or modifications to a JPEG image cause an accumulation of loss (i.e. quality degradation) when the modified image is saved again in JPEG format (see JPEG Compression - Quality & Loss and Generational Loss). However, there are some useful exceptions to this rule. Lossless JPEG Rotation allows a JPEG image to be rotated in 90 degree increments without requiring re-compression (thereby avoiding the loss). This is particularly useful for rotating digital photos taken in portrait orientation or correcting the orientation of scans. However, there are certain conditions attached... JPEG Blocks: The MCU Size
JPEG compresses images in blocks (tiles) that are usually 8 x 8 or 16 x 16 pixels, known technically as the "MCU" size (Minimum Coded Unit). If the image width or height are not exact multiples of the MCU size then "Partial MCUs" are added. Partial MCUs are actually full-sized blocks, but when the image is decoded the surplus pixels can be discarded from the right and bottom edges of the image, since the overall image dimensions are known. Note that most digital cameras produce images with dimensions that are exact multiples of the MCU size. JPEG "MCU" Blocks, Visible in a Highly Compressed Image
Actual Size | 3x Zoom | | |
|
|
|
Lossless Rotation and MCU's
If the image width and height are exact multiples of the MCU size then Lossless JPEG Rotation can proceeed without futher complication. Consequently, when resizing, resampling or cropping images it is a good idea to choose image sizes that result in whole MCU multiples (the table below can help to choose suitable sizes for various aspect-ratios and MCU's).
If either of the dimensions are not exact MCU multiples then special handling is required, and there are various choices: - 1) Trim the Image to the Nearest MCU Boundary.
Only complete MCU's are rotated, and partial MCU's are discarded (i.e. trimmed/cropped). This is technically not lossless, since a few pixels are discarded and hence the operation is not fully reversible, but it is often visually preferable to option 2, and it does retain the original quality in the remaining part of the image.
- 2) Don't Trim - Leave Partial MCU's "Unrotated".
This results in an odd-looking strip of pixels on one or two sides of the image, however it is reversible (and therefore technically lossless) and retains the overall dimensions of the original image.
- 3) Fall-Back to "Lossy" Rotation.
If the width and height need to be retained, and the odd-looking "strip" produced by option 2 is not visually acceptible, then the only option is to rotate the uncompressed image, then re-compress to JPEG format, causing a (usually slight) loss of quality due to generational loss.
The following examples illustrate the results of lossless JPEG rotation for a 'worst-case' image, i.e. with partial MCU's at both the right and bottom edges, both of which are the maximum width/height for a partial MCU (in this case 15 pixels - one pixel less that the full MCU size of 16 x 16). Original Image
Unrotated | Partial MCU's | | |
|
|
|
90 CW Rotation
Un-rotatable Area | Result: Trimmed | Result: Un-Trimmed | | | |
|
|
|
180 Rotation
Un-rotatable Area | Result: Trimmed | Result: Un-Trimmed | | | |
|
|
|
90 CCW Rotation
Un-rotatable Area | Result: Trimmed | Result: Un-Trimmed | | | |
|
|
|
Lossless JPEG Rotation Implementation in DBPix
The DBPix image control supports rotation and mirroring, either initiated by the user (via the "Rotate/Mirror" context-menu), or programmatically using the "ImageRotate" method. Lossless JPEG rotation will always be used when rotating a JPEG image with dimensions that are exact multiples of the MCU size. If either dimension is not an exact MCU multiple the developer can determine the behaviour by setting the "JPEGRotateWarning" property: - Prompt (default):
The user is prompted to choose between lossy or lossless rotation, and in the case of lossless whether to trim the image to the MCU size, or leave unrotated pixels. The user can also Cancel to abort the rotation. See the dialog screenshot below.
- Trim:
Lossless rotation is used, and the image is trimmed to the MCU size.
- Ignore:
Lossless rotation is used, and partial MCU's are left unrotated.
- Lossy:
Lossy rotation is used.
- Abort:
Aborts the operation and does not rotate the image.
DBPix Lossless JPEG Rotation Options Dialog
JPEG MCU "HotSpots"
The following table lists dimensions for JPEG images that are exact multiples of MCU size for common aspect-ratios and MCU sizes. These are therefore good choices when resizing, resampling or cropping images that may need subsequent Lossless JPEG Rotation.
Aspect Ratio 3:2 | Aspect Ratio 4:3 | MCU 8 x 8 | MCU 16 x 16 | MCU 8 x 8 | MCU 16 x 16 | 24 | x | 16 | 48 | x | 32 | 72 | x | 48 | 96 | x | 64 | 120 | x | 80 | 144 | x | 96 | 168 | x | 112 | 192 | x | 128 | 216 | x | 144 | 240 | x | 160 | 264 | x | 176 | 288 | x | 192 | 312 | x | 208 | 336 | x | 224 | 360 | x | 240 | 384 | x | 256 | 408 | x | 272 | 432 | x | 288 | 456 | x | 304 | 480 | x | 320 | 504 | x | 336 | 528 | x | 352 | 552 | x | 368 | 576 | x | 384 | 600 | x | 400 | 624 | x | 416 | 648 | x | 432 | 672 | x | 448 | 696 | x | 464 | 720 | x | 480 | 744 | x | 496 | 768 | x | 512 | 792 | x | 528 | 816 | x | 544 | 840 | x | 560 | 864 | x | 576 | 888 | x | 592 | 912 | x | 608 | 936 | x | 624 | 960 | x | 640 | 984 | x | 656 | 1008 | x | 672 | 1032 | x | 688 | 1056 | x | 704 | 1080 | x | 720 | 1104 | x | 736 | 1128 | x | 752 | 1152 | x | 768 | 1176 | x | 784 | 1200 | x | 800 | 1224 | x | 816 | 1248 | x | 832 | 1272 | x | 848 | 1296 | x | 864 | 1320 | x | 880 | 1344 | x | 896 | 1368 | x | 912 | 1392 | x | 928 | 1416 | x | 944 | 1440 | x | 960 | 1464 | x | 976 | 1488 | x | 992 | 1512 | x | 1008 | 1536 | x | 1024 | 1560 | x | 1040 | 1584 | x | 1056 | 1608 | x | 1072 | 1632 | x | 1088 | 1656 | x | 1104 | 1680 | x | 1120 | 1704 | x | 1136 | 1728 | x | 1152 | 1752 | x | 1168 | 1776 | x | 1184 | 1800 | x | 1200 | 1824 | x | 1216 | 1848 | x | 1232 | 1872 | x | 1248 | 1896 | x | 1264 | 1920 | x | 1280 | 1944 | x | 1296 | 1968 | x | 1312 | 1992 | x | 1328 | 2016 | x | 1344 | 2040 | x | 1360 | 2064 | x | 1376 | 2088 | x | 1392 | 2112 | x | 1408 | 2136 | x | 1424 | 2160 | x | 1440 | 2184 | x | 1456 | 2208 | x | 1472 | 2232 | x | 1488 | 2256 | x | 1504 | 2280 | x | 1520 | 2304 | x | 1536 | 2328 | x | 1552 | 2352 | x | 1568 | 2376 | x | 1584 | 2400 | x | 1600 |
| 48 | x | 32 | 96 | x | 64 | 144 | x | 96 | 192 | x | 128 | 240 | x | 160 | 288 | x | 192 | 336 | x | 224 | 384 | x | 256 | 432 | x | 288 | 480 | x | 320 | 528 | x | 352 | 576 | x | 384 | 624 | x | 416 | 672 | x | 448 | 720 | x | 480 | 768 | x | 512 | 816 | x | 544 | 864 | x | 576 | 912 | x | 608 | 960 | x | 640 | 1008 | x | 672 | 1056 | x | 704 | 1104 | x | 736 | 1152 | x | 768 | 1200 | x | 800 | 1248 | x | 832 | 1296 | x | 864 | 1344 | x | 896 | 1392 | x | 928 | 1440 | x | 960 | 1488 | x | 992 | 1536 | x | 1024 | 1584 | x | 1056 | 1632 | x | 1088 | 1680 | x | 1120 | 1728 | x | 1152 | 1776 | x | 1184 | 1824 | x | 1216 | 1872 | x | 1248 | 1920 | x | 1280 | 1968 | x | 1312 | 2016 | x | 1344 | 2064 | x | 1376 | 2112 | x | 1408 | 2160 | x | 1440 | 2208 | x | 1472 | 2256 | x | 1504 | 2304 | x | 1536 | 2352 | x | 1568 | 2400 | x | 1600 | 2448 | x | 1632 | 2496 | x | 1664 | 2544 | x | 1696 | 2592 | x | 1728 | 2640 | x | 1760 | 2688 | x | 1792 | 2736 | x | 1824 | 2784 | x | 1856 | 2832 | x | 1888 | 2880 | x | 1920 | 2928 | x | 1952 | 2976 | x | 1984 | 3024 | x | 2016 | 3072 | x | 2048 | 3120 | x | 2080 | 3168 | x | 2112 | 3216 | x | 2144 | 3264 | x | 2176 | 3312 | x | 2208 | 3360 | x | 2240 | 3408 | x | 2272 | 3456 | x | 2304 | 3504 | x | 2336 | 3552 | x | 2368 | 3600 | x | 2400 | 3648 | x | 2432 | 3696 | x | 2464 | 3744 | x | 2496 | 3792 | x | 2528 | 3840 | x | 2560 | 3888 | x | 2592 | 3936 | x | 2624 | 3984 | x | 2656 | 4032 | x | 2688 | 4080 | x | 2720 | 4128 | x | 2752 | 4176 | x | 2784 | 4224 | x | 2816 | 4272 | x | 2848 | 4320 | x | 2880 | 4368 | x | 2912 | 4416 | x | 2944 | 4464 | x | 2976 | 4512 | x | 3008 | 4560 | x | 3040 | 4608 | x | 3072 | 4656 | x | 3104 | 4704 | x | 3136 | 4752 | x | 3168 | 4800 | x | 3200 |
| 32 | x | 24 | 64 | x | 48 | 96 | x | 72 | 128 | x | 96 | 160 | x | 120 | 192 | x | 144 | 224 | x | 168 | 256 | x | 192 | 288 | x | 216 | 320 | x | 240 | 352 | x | 264 | 384 | x | 288 | 416 | x | 312 | 448 | x | 336 | 480 | x | 360 | 512 | x | 384 | 544 | x | 408 | 576 | x | 432 | 608 | x | 456 | 640 | x | 480 | 672 | x | 504 | 704 | x | 528 | 736 | x | 552 | 768 | x | 576 | 800 | x | 600 | 832 | x | 624 | 864 | x | 648 | 896 | x | 672 | 928 | x | 696 | 960 | x | 720 | 992 | x | 744 | 1024 | x | 768 | 1056 | x | 792 | 1088 | x | 816 | 1120 | x | 840 | 1152 | x | 864 | 1184 | x | 888 | 1216 | x | 912 | 1248 | x | 936 | 1280 | x | 960 | 1312 | x | 984 | 1344 | x | 1008 | 1376 | x | 1032 | 1408 | x | 1056 | 1440 | x | 1080 | 1472 | x | 1104 | 1504 | x | 1128 | 1536 | x | 1152 | 1568 | x | 1176 | 1600 | x | 1200 | 1632 | x | 1224 | 1664 | x | 1248 | 1696 | x | 1272 | 1728 | x | 1296 | 1760 | x | 1320 | 1792 | x | 1344 | 1824 | x | 1368 | 1856 | x | 1392 | 1888 | x | 1416 | 1920 | x | 1440 | 1952 | x | 1464 | 1984 | x | 1488 | 2016 | x | 1512 | 2048 | x | 1536 | 2080 | x | 1560 | 2112 | x | 1584 | 2144 | x | 1608 | 2176 | x | 1632 | 2208 | x | 1656 | 2240 | x | 1680 | 2272 | x | 1704 | 2304 | x | 1728 | 2336 | x | 1752 | 2368 | x | 1776 | 2400 | x | 1800 | 2432 | x | 1824 | 2464 | x | 1848 | 2496 | x | 1872 | 2528 | x | 1896 | 2560 | x | 1920 | 2592 | x | 1944 | 2624 | x | 1968 | 2656 | x | 1992 | 2688 | x | 2016 | 2720 | x | 2040 | 2752 | x | 2064 | 2784 | x | 2088 | 2816 | x | 2112 | 2848 | x | 2136 | 2880 | x | 2160 | 2912 | x | 2184 | 2944 | x | 2208 | 2976 | x | 2232 | 3008 | x | 2256 | 3040 | x | 2280 | 3072 | x | 2304 | 3104 | x | 2328 | 3136 | x | 2352 | 3168 | x | 2376 | 3200 | x | 2400 |
| 64 | x | 48 | 128 | x | 96 | 192 | x | 144 | 256 | x | 192 | 320 | x | 240 | 384 | x | 288 | 448 | x | 336 | 512 | x | 384 | 576 | x | 432 | 640 | x | 480 | 704 | x | 528 | 768 | x | 576 | 832 | x | 624 | 896 | x | 672 | 960 | x | 720 | 1024 | x | 768 | 1088 | x | 816 | 1152 | x | 864 | 1216 | x | 912 | 1280 | x | 960 | 1344 | x | 1008 | 1408 | x | 1056 | 1472 | x | 1104 | 1536 | x | 1152 | 1600 | x | 1200 | 1664 | x | 1248 | 1728 | x | 1296 | 1792 | x | 1344 | 1856 | x | 1392 | 1920 | x | 1440 | 1984 | x | 1488 | 2048 | x | 1536 | 2112 | x | 1584 | 2176 | x | 1632 | 2240 | x | 1680 | 2304 | x | 1728 | 2368 | x | 1776 | 2432 | x | 1824 | 2496 | x | 1872 | 2560 | x | 1920 | 2624 | x | 1968 | 2688 | x | 2016 | 2752 | x | 2064 | 2816 | x | 2112 | 2880 | x | 2160 | 2944 | x | 2208 | 3008 | x | 2256 | 3072 | x | 2304 | 3136 | x | 2352 | 3200 | x | 2400 | 3264 | x | 2448 | 3328 | x | 2496 | 3392 | x | 2544 | 3456 | x | 2592 | 3520 | x | 2640 | 3584 | x | 2688 | 3648 | x | 2736 | 3712 | x | 2784 | 3776 | x | 2832 | 3840 | x | 2880 | 3904 | x | 2928 | 3968 | x | 2976 | 4032 | x | 3024 | 4096 | x | 3072 | 4160 | x | 3120 | 4224 | x | 3168 | 4288 | x | 3216 | 4352 | x | 3264 | 4416 | x | 3312 | 4480 | x | 3360 | 4544 | x | 3408 | 4608 | x | 3456 | 4672 | x | 3504 | 4736 | x | 3552 | 4800 | x | 3600 | 4864 | x | 3648 | 4928 | x | 3696 | 4992 | x | 3744 | 5056 | x | 3792 | 5120 | x | 3840 | 5184 | x | 3888 | 5248 | x | 3936 | 5312 | x | 3984 | 5376 | x | 4032 | 5440 | x | 4080 | 5504 | x | 4128 | 5568 | x | 4176 | 5632 | x | 4224 | 5696 | x | 4272 | 5760 | x | 4320 | 5824 | x | 4368 | 5888 | x | 4416 | 5952 | x | 4464 | 6016 | x | 4512 | 6080 | x | 4560 | 6144 | x | 4608 | 6208 | x | 4656 | 6272 | x | 4704 | 6336 | x | 4752 | 6400 | x | 4800 |
|
|