Results 1 to 10 of 10

Thread: JSK -JPEG Scan Killer- progressive JPEG explained in slowmo

  1. #1
    Member caveman's Avatar
    Join Date
    Jul 2009
    Location
    Strasbourg, France
    Posts
    190
    Thanks
    8
    Thanked 62 Times in 33 Posts

    JSK -JPEG Scan Killer- progressive JPEG explained in slowmo

    Hello,
    Colt McAnlis recently wrote "Image Compression for Web Developers", the article was rather good but I've found that the part concerning progressive images lacked details and was somewhat misleading.
    Explaining progressive JPEG is far from being easy since two mechanisms are involved: "spectral selection" and "successive approximation", thus I thought that it would be cool and enlightening to actually see what happens in the different scans (refinement passes).
    I quickly hacked together JSK -JPEG Scan Killer- as the name suggests it takes an existing (progressive or sequential) JPEG and progressively removes all the scans excepted the first one. This is done crudely by inserting an End Of Image marker and cutting the rest of the file, therefore the files produced by JSK are truncated JPEGs that do not comply with the specs but most decoders will do their best to display them, this means they will often blur the image as if it was still in-flight.
    Jpegtran can be used to turn them into standard JPEGs (will no longer be blurred when displayed) and PNGOUT does not complain when encoding them as PNGs (it has some flaws though). Such a series of PNGs can be turned in an animated PNG -APNG- with tools like apngasm or closely inspected at will.

    command line usage:
    jsk file.jpg
    will create files named scan-xxx.jpg

    Linux, Mac OS X binaries and C source in tarballs below.
    Windows binary in zip archive below.

    Now to clarify some points for those not familiar with JPEG inner workings, contrary to PNG with its Adam7 interlaced mode, there are tens of thousands different ways to produce progressive JPEGs, "progressive" is definitely not restricted to a single predefined way. For instance JPEGrescan conducts trials with different progressive configurations to reduce the file size, yes progressive JPEGs are often smaller than sequential ones. Even what is often called "baseline JPEG" ("sequential" is more appropriate) could be recorded in 13 different ways. 99.99% of sequential JPEGs use the single scan with 3 interleaved components scheme therefore JSK will only output a single scan_001.jpg file when it encounters one of these numerous sequential JPEG (it's not a bug, it's a sequential JPEG).
    JSK produces between 1 and 3 scan images when the input file was a sequential JPEG, 4 scan images and more mean the input file was a progressive JPEG.
    On this sample (click to enlarge) the images on the left represent one of the shortest progressive sequence and those on the right one of the longest sequential sequence.
    Click image for larger version. 

Name:	smiles.jpg 
Views:	1027 
Size:	118.3 KB 
ID:	2500
    A sequential JPEG can be turned into a progressive one losslessly (and the other way around) using jpegtran. Most of the progressive sequences shown here have been crafted for educational purpose, you'll find "custom_something_scans" text files in each xxx_ball.zip archive these were used to produce the specific sequence the sample illustrates, feel free to reuse them (-scans option in cjpeg/jpegtran, syntax of scan files is explained in the wizard.txt file).


    Commented JSKs output
    The arrays representing JPEG "data units" on the right were hand added, they reflect the various DC/AC coefficients available so far for each YCbCr component. Only 5 data units per component are fully displayed, a 272 by 176 pixel image is actually made of 34 x 22 = 748 data units per component (when chroma sub-sampling kicks in this figure is usually halved or quartered for Cb and Cr components). If you are not familiar with YCbCr components and chroma sub-sampling I've started to write a .pdf document (yet another work in progress) called "JPEG for the horseshoe crabs" meant to expose JPEGs inner workings, read it first and get back.

    - "spectral selection" is represented by filled cells (Y: light gray, Cb: yellow-green, Cr: violet) along the zigzag path.


    - "successive approximation" is represented by partially filled cells (starting at sample 3).



    1) Fish sample
    - The initial scan is very common, it's an interleaved scan holding the DC coefficient of the 3 components.
    This results in the typical mosaic image made of 8 by 8 pixel squares.

    - The second scan holds the 9 first (in the zigzag order) AC coefficients of the Y component.
    The dorsal and caudal fins take form and the eye is now round. The red colored blocks are clearly twice as big as the initial 8 by 8 mosaic this reveals that 4:2:0 (2x2) chroma sub-sampling has been applied.

    - The third scan holds the 9 first (in the zigzag order) AC coefficients of the Cb component.
    Some temporary green and purple spots appear this is due to the unbalanced quantity of information now available in the Cb and Cr components.

    - The forth scan holds the 9 first (in the zigzag order) AC coefficients of the Cr component.
    Chroma is now well balanced, a lot of jaggies remain especially near the fish/background border.

    - The fifth scan holds 18 AC coefficients of the Y component.
    The overall image quality jumps suddenly, this illustrates the importance of the luma signal compared to chroma and also shows that the first half of the AC coefficients is usually more important than the later ones.

    - The last scans will only slightly improve the image quality, some JPEG compression artifacts remain even after the last scan this is due to the lossy aspect of JPEG compression.


    2) Snap sample
    - The initial scan only holds the DC coefficient of the Y component.
    Since there's no chroma signal the mosaic is in grayscale.

    - The second scan holds the 9 first (in the zigzag order) AC coefficients of the Y component.
    Still no chroma, the overall shape of the windows appears but texts look totally weird.

    - The third scan is an interleaved scan holding the DC coefficient of the Cb and Cr components.
    This brings some color.

    - The forth scan holds 18 AC coefficients of the Y component.
    Texts are now readable but there's a lot of image noise around them. Horizontal window edges are plagued by ringing artifacts (lighter ghost lines).

    - The fifth and sixth scans hold 9 AC coefficients of the Cb and Cr component respectively.
    This improves color accuracy and reduces smearing around color spots.
    The same DC/AC coefficients are now available for this image as on the fifth scan of the fish image (at this point the fish looked rather good) the artificial nature of the snap image makes it harder to compress, JPEG has been tuned (quantization tables, zigzag order) for continuous tone images which makes the sharp edges in this image harder to compress whithout artifacts (that's why some images -say cartoon like- should definitely be saved as PNGs and never as JPEGs).

    The fish_ball.zip and snap_ball.zip archives hold the original JPEG image, a custom scan file (-scans option in cjpeg/jpegtran), the 8 or 9 scans as raw JSK outputs and as PNG files and finally an animated PNG (use Firefox or Opera to watch it).
    The animations are also available independently, click on the thumbnails below to watch them (a new scan is displayed every 1.5 or 1.8 seconds, be patient).
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	fish_scan.jpg 
Views:	886 
Size:	774.3 KB 
ID:	2475   Click image for larger version. 

Name:	anim_fish.png 
Views:	536 
Size:	279.4 KB 
ID:	2476   Click image for larger version. 

Name:	snap_scan.jpg 
Views:	662 
Size:	781.7 KB 
ID:	2478   Click image for larger version. 

Name:	anim_snap.png 
Views:	454 
Size:	122.6 KB 
ID:	2479  
    Attached Files Attached Files
    Last edited by caveman; 11th June 2014 at 01:15. Reason: Added some JPEG background informations

  2. The Following 4 Users Say Thank You to caveman For This Useful Post:

    Biozynotiker (6th October 2013),Jaff (7th October 2013),lorents17 (28th December 2013),Mangix (30th September 2013)

  3. #2
    Member caveman's Avatar
    Join Date
    Jul 2009
    Location
    Strasbourg, France
    Posts
    190
    Thanks
    8
    Thanked 62 Times in 33 Posts
    3) Lizard sample (successive approximation)

    More samples to come (Photoshop and jpegtran/cjpeg default progressive modes compared).
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	lizard_sa_luma_first.jpg 
Views:	550 
Size:	866.9 KB 
ID:	2495   Click image for larger version. 

Name:	lizard_sa_chroma_first.jpg 
Views:	535 
Size:	947.3 KB 
ID:	2494   Click image for larger version. 

Name:	anim_lizard.png 
Views:	449 
Size:	975.9 KB 
ID:	2496  
    Attached Files Attached Files
    Last edited by caveman; 29th September 2013 at 06:00.

  4. The Following 2 Users Say Thank You to caveman For This Useful Post:

    lorents17 (12th June 2015),porneL (1st October 2013)

  5. #3
    Member
    Join Date
    Jun 2013
    Location
    USA
    Posts
    98
    Thanks
    4
    Thanked 14 Times in 12 Posts
    nice tool. also nice that it works with arithmetic jpegs as well.

    it's a bit funny that the last scan doesn't really look better than the second-to-last on most images that i've tested. Some potential for lossy compression there I guess.

  6. #4
    Member
    Join Date
    Apr 2012
    Location
    Stuttgart
    Posts
    437
    Thanks
    1
    Thanked 96 Times in 57 Posts
    Quote Originally Posted by Mangix View Post
    nice tool. also nice that it works with arithmetic jpegs as well.

    it's a bit funny that the last scan doesn't really look better than the second-to-last on most images that i've tested. Some potential for lossy compression there I guess.
    It depends on how the scans were organized, of course. Typically, the last scan only covers the LSBs of the AC coefficients, which are mostly noise. If you don't see any difference, it only means that you could double the quantization parameters for the AC coefficients without causing asubjective quality degration. IOW, the quantization matrix of the original image was overly fine, and the image was undercompressed to begin with.

  7. #5
    Member
    Join Date
    Apr 2011
    Location
    Russia
    Posts
    168
    Thanks
    163
    Thanked 9 Times in 8 Posts
    Good day!
    Tell me, is it possible to make such a program only for jpeg images optimize / Baseline ?

  8. #6
    Member
    Join Date
    Apr 2012
    Location
    Stuttgart
    Posts
    437
    Thanks
    1
    Thanked 96 Times in 57 Posts
    Quote Originally Posted by lorents17 View Post
    Good day!
    Tell me, is it possible to make such a program only for jpeg images optimize / Baseline ?
    You mean a program that requantizes JPEG files to compress them more? yes, certainly. Actually, there are quite some programs/solutions out in the web you can try from, including online solutions - it's certainly not a new or novel feature.

  9. #7
    Member
    Join Date
    Apr 2011
    Location
    Russia
    Posts
    168
    Thanks
    163
    Thanked 9 Times in 8 Posts
    meant a little more. simple application that simplifies the download of jpeg frames.

  10. #8
    Member
    Join Date
    Apr 2012
    Location
    Stuttgart
    Posts
    437
    Thanks
    1
    Thanked 96 Times in 57 Posts
    Quote Originally Posted by lorents17 View Post
    meant a little more. simple application that simplifies the download of jpeg frames.
    Hmm. Specify "simplify". Downloading a JPEG is as simple as it gets, the browsers support it natively, so there is nothing to worry about.

  11. #9
    Member
    Join Date
    Apr 2011
    Location
    Russia
    Posts
    168
    Thanks
    163
    Thanked 9 Times in 8 Posts
    del
    Last edited by lorents17; 11th June 2015 at 23:21.

  12. #10
    Member
    Join Date
    Apr 2011
    Location
    Russia
    Posts
    168
    Thanks
    163
    Thanked 9 Times in 8 Posts
    Please explain, how does the technology -scans file in libjpeg?
    Last edited by lorents17; 11th June 2015 at 23:36.

Similar Threads

  1. JPEG Huffman Coding
    By lorents17 in forum Data Compression
    Replies: 26
    Last Post: 16th December 2013, 00:51
  2. Unoptimizing? a JPEG file
    By Mangix in forum Data Compression
    Replies: 7
    Last Post: 7th September 2013, 11:55
  3. jpeg to zpaq lossy compression
    By toi007 in forum Data Compression
    Replies: 11
    Last Post: 24th January 2013, 04:20
  4. Test JPEG Standards Online
    By thorfdbg in forum Data Compression
    Replies: 0
    Last Post: 27th November 2012, 22:14
  5. Detector for ex-JPEG images
    By Alexander Rhatushnyak in forum Data Compression
    Replies: 22
    Last Post: 13th September 2012, 03:18

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •