[HN Gopher] Jpeg2png: Silky smooth JPEG decoding - no more artif...
       ___________________________________________________________________
        
       Jpeg2png: Silky smooth JPEG decoding - no more artifacts (2016)
        
       Author : todsacerdoti
       Score  : 127 points
       Date   : 2020-07-10 10:19 UTC (12 hours ago)
        
 (HTM) web link (github.com)
 (TXT) w3m dump (github.com)
        
       | wtallis wrote:
       | This strikes me as conceptually similar to the problem of
       | printing floating point numbers. A naive algorithm will convert
       | the binary floating point to decimal and print that out, with
       | potentially many trailing digits that are more or less garbage.
       | The more human-friendly algorithms figure out the
       | shortest/simplest decimal that would be rounded to the same
       | binary floating point quantity, and prints that instead.
        
         | jandrese wrote:
         | Yes, I've long wanted a floating point print function that
         | understands that 3.999999999999 should be printed as "4".
        
           | colejohnson66 wrote:
           | My $.02: I'd prefer it say 3.9999999... than 4 so students
           | learn of the inaccuracy of floating point. If one wants a
           | rounded output, one should round before printing.
        
             | jandrese wrote:
             | Students can be taught using the standard library printf()
             | or whatever.
             | 
             | It's not as easy as "just round" though, because you don't
             | know where the digits start to repeat. A smart function
             | would also print 4.1249999999999 as 4.125.
             | 
             | A fancy unicode version could even include the mathematical
             | notation to print 3.333333333333 as 3.3 with a line over
             | the trailing 3. Or 4.2525252525 as 4.25 with the bar over
             | the 25. Might be asking too much of a simple print function
             | though, this might end up being an extension of the halting
             | problem.
        
               | moonchild wrote:
               | Sounds like you want a computer algebra system, not a
               | floating point format algorithm.
        
               | jandrese wrote:
               | I don't think I'm the only one. I was thinking as I wrote
               | the response that such a system might have to include a
               | whole lot more math than it looks at the surface, which
               | is probably why it doesn't exist as a general solution.
               | 
               | But maybe there could be some heuristics that get it
               | right 99% of the time (with a don't use this library for
               | serious number crunching disclaimer).
        
               | wtallis wrote:
               | I've used various HP scientific and graphing calculators
               | that have functions to convert a floating point number to
               | a symbolic representation of a rational number, or a
               | fraction of pi. These functions are approximate, and
               | their tolerance for how large the denominator of the
               | fraction may get is determined by the current setting for
               | how many decimal digits the display of numbers is rounded
               | to. In practice, this works pretty well, and they
               | implemented this capability long before they had a full
               | CAS on any of their calculators.
        
             | wtallis wrote:
             | 3.999... vs 4 is a bad example, because 4 is exactly
             | representable, and if your calculation gives you a result
             | of 3.999... [garbage digits omitted] then it means there
             | was roundoff error along the way and your computed result
             | is something that is strictly not equal to 4.
             | 
             | A better example would be something like 1/5, which for
             | almost every purpose is better printed as 0.2 instead of
             | 0.20000000298023223876953125. The latter is absurdly long
             | and gives the misleading impression of precision far in
             | excess of the ~7 decimal digits that single-precision
             | floats are capable of representing.
             | 
             | The difference between 0.2 and that long string is due to a
             | slightly different cause than a difference between 3.999...
             | and 4: the latter is likely due to information loss during
             | calculations and may be reduced and sometimes avoided
             | entirely with careful ordering of calculations and using
             | the right rounding modes. But 0.2 can never be exactly
             | represented, even as the input to a chain of calculations.
             | The loss of accuracy is an unavoidable first step of trying
             | to put 0.2 into a binary FPU register.
             | 
             | Students _should_ learn about the pitfalls of floating
             | point arithmetic. But preferably in a way that doesn 't
             | leave them with the impression that it is a non-
             | deterministic process that always leaves you with trailing
             | garbage that needs to be ignored.
        
             | 0x0 wrote:
             | But 3.9999999..... === 4. It is literally the same number.
        
               | wtallis wrote:
               | Your floating-point calculator won't give you infinite
               | trailing nines, but it may very well give you something
               | like 3.99999976158 (approximately the largest float below
               | exactly 4) or 3.99999999999999955591 (approximately the
               | largest double below 4).
        
         | Kednicma wrote:
         | You and the rest of this thread's participants may be
         | interested to know that the problem was solved a while ago; you
         | want the "Dragon4" algorithm from "How to Print Floating-Point
         | Numbers Accurately" [0]. Indeed, the problem is difficult, and
         | having implemented the algorithm, I can confirm that it has
         | earned its reputation. Numeric algorithms are hard and the only
         | reward is accuracy.
         | 
         | [0] https://lists.nongnu.org/archive/html/gcl-
         | devel/2012-10/pdfk... or use your search engine
        
       | karmakaze wrote:
       | For the first image I much prefer the jpeg decoded image to the
       | de-noised one. The de-noised one just looks like it was sent
       | through a low-pass/median filter and loses contrast, detail, and
       | texture. If you flip between the full-sized images the jpeg2png
       | looks like vaseline on the lens.
       | 
       | The naming is also odd. I don't imagine it would do well with
       | text (which are the sorts of images I associate with png) that
       | was jpeg encoded then decoded with it.
        
         | KarlKemp wrote:
         | It says it is _specifically targeted_ at images that should
         | have been png in the first place.
        
       | Scaevolus wrote:
       | Knusperli is similar: https://github.com/google/knusperli
       | 
       | Instead of trying to smooth the entire image, it reduces blocking
       | artifacts by finding plausible coefficients that reduce
       | discontinuities at block boundaries (jpegs are encoded as a
       | series of 8x8 blocks). "jpeg2png gives best results for pictures
       | that should never be saved as JPEG", but knusperli works well on
       | normal photographic content, since it only tries to remove
       | blocking artifacts.
       | 
       | > A JPEG encoder quantizes DCT coefficients by rounding
       | coefficients to the nearest multiple of the elements of the
       | quantization matrix. For every coefficient, there is an interval
       | of values that would round to the same multiple. A traditional
       | decoder uses the center of this interval to reconstruct the
       | image. Knusperli instead chooses the value in the interval that
       | reduces discontinuities at block boundaries. The coefficients
       | that Knusperli uses, would have rounded to the same values that
       | are stored in the JPEG image.
        
         | enriquto wrote:
         | > Instead of trying to smooth the entire image, it reduces
         | blocking artifacts by finding plausible coefficients that
         | reduce discontinuities at block boundaries
         | 
         | This is essentially the same thing, but using slightly
         | different regularizers. In both cases, an image is found that
         | whose jpeg compression is identical to the given one, by
         | selecting the "most regular" image among the large family of
         | such images. Jpeg2png minimizes the total variation over the
         | whole image, and knusperly only along the boundaries of the
         | blocks. The effect is quite similar at the end.
        
           | Scaevolus wrote:
           | The effects are quite different, since reducing total
           | variation also reduces texture, which is important to
           | perceived quality. Here's a direct comparison-- note how
           | knusperli retains slightly more detail instead of
           | oversmoothing:
           | https://mod.ifies.com/f/200710_lena_decode_comparison.png
        
       | crazygringo wrote:
       | This is really cool, but in other comments I'm reading that good
       | JPEG encoders and decoders _depend on knowing the other_.
       | 
       | That you can design a better JPEG encoder for a known reference
       | decoder (which decoded version best matches the original), and
       | you can likewise design a better JPEG decoder for a known
       | reference encoder.
       | 
       | But now that there are _both_ encoders _and_ decoders trying to
       | be more efficient and non-blocky respectively... it feels like a
       | mess. Even more so because a decoder never knows which encoder
       | was used, and an encoder never knows which decoder.
       | 
       | Is there any solution to this mess? Is there a best practice
       | here? Since most JPEG's are probably viewed on the web, do all
       | browsers share the same decoder, and so effort is best spent on
       | the encoder? Or even while encoder improvements are made
       | specifically for the algorithms browsers use, can improvements to
       | decoders still be made that remain improvements, instead of e.g.
       | "overfitting" and actually making things worse?
       | 
       | This just feels like a bit of an "improvements race" on both
       | sides which leaves me a little dizzy in trying to understand
       | it...
        
       | webmaven wrote:
       | _" jpeg2png gives best results for pictures that should never be
       | saved as JPEG. Examples are charts, logo's, and cartoon-style
       | digital drawings."_
       | 
       | Interesting value proposition.
       | 
       | There may be equivalents for other misused file types/formats.
       | The obvious one is the inverse case of photos saved as GIFs, but
       | I imagine audio and text (eg. PDF malapropisms?) are possible
       | too.
        
         | jandrese wrote:
         | I just tried it with a text heavy screenshot, a common scenario
         | where the JPEG encoding produces a poor result with lots of
         | speckle around each letter.
         | 
         | The result is very good. The speckling is effectively
         | eliminated without smearing the letters.
         | 
         | JPEG Encoded, 25% quality:
         | http://jubei.ceyah.org/overcompressed/crop-jpg.png
         | 
         | JPEG2PNG: http://jubei.ceyah.org/overcompressed/crop-
         | converted.png
        
           | fireattack wrote:
           | Why the whole picture shifted? This can't be good
        
           | indigo945 wrote:
           | I find the text on the reference decoded image easier to
           | read, despite the colorful speckles surrounding the words.
           | JPEG2PNG doesn't smear the letters a lot, but does appear to
           | slightly deform them. Consider the leading "a" in "add
           | license, readme", whose upper half seems to be closer
           | together after the JPEG2PNG run.
           | 
           | However, now I'm wondering whether my impression that the
           | reference decoded text is easier to read is a form of
           | overfitting. I have spent the past 20 years reading low-
           | quality JPEG screenshots on the internet, maybe the
           | perceptual centers of my brain have developed an unblocking
           | algorithm?
        
         | dmitriid wrote:
         | And then someone uploads this image to twitter/facebook, and it
         | gets triply re-encoded and resized with the worst algorithms
         | known to man :(
        
           | BlackLotus89 wrote:
           | https://mobile.twitter.com/NolanOBrien/status/12045574989900.
           | .. twitter now doesn't cripple uploaded images, but tries to
           | preserve all information except exiv data.
        
             | [deleted]
        
             | dmitriid wrote:
             | Kudos to Twitter!
             | 
             | Now if only they stopped cropping images to rand()xrand()
             | in timelines pretending they employ advanced ML algorithms
             | to do so :)
        
               | fireattack wrote:
               | It's not random though. They likely show most contrasted
               | area (with maybe some face detection).
        
         | makapuf wrote:
         | I d say the issue is the prpgram here, a compressor program
         | might have heuristic to detect png-favored images vs photos. I
         | cannot imagine we couldn't detect the easy cases reliably
         | (because I'd say often images are more in a category or
         | another). Crop the image to 64x64, try png and if the result is
         | sufficiently small continue, else convert to jpg ?
        
       | 0-_-0 wrote:
       | Now we just need an adversarially trained neural network to fill
       | in detail [1] and we have a 21st century JPEG decoder!
       | 
       | [1] Implemented for a custom compressor: https://hific.github.io/
        
         | lazyjones wrote:
         | And one day, a law enforcement officer will use it on a bad
         | quality JPEG image and one of the innocent persons whose photos
         | were used to train the NN will be arrested.
        
         | leeoniya wrote:
         | here's one that upscales extremely well:
         | 
         | https://github.com/TianZerL/Anime4KCPP
        
       | [deleted]
        
       | ris wrote:
       | Interesting project, but its name sells it incredibly short.
        
       | locofocos wrote:
       | "For pictures that have been reencoded to JPEG multiple times I
       | recommend shred."
       | (https://www.gnu.org/software/coreutils/manual/html_node/shre...)
       | 
       | That's the funniest thing I've read in a while.
        
       | etaioinshrdlu wrote:
       | The interesting thing is that advanced _encoders_ might create
       | JPEGs that produce odd results with this decoder.
        
         | a_e_k wrote:
         | Yes, this is definitely an interesting question. Earlier this
         | year I implemented a version of trellis quantization for some
         | compression experiments that I've been tinkering with in my
         | spare time. My code considers more possibilities than just
         | rounding to the nearest quantization step when it deems that
         | the bit rate savings in after entropy encoding may be worth the
         | additional image loss (e.g., it might even completely zero a
         | coefficient). That would violate this decoder's assumption that
         | the original DCT coefficient must have been within a limited
         | range around the quantization step.
         | 
         | I know that mozjpeg [1] features trellis quantization for JPEG
         | encoding. I wonder how this decoder would do with that?
         | 
         | [1] https://github.com/mozilla/mozjpeg
        
       | ajb wrote:
       | This is a poor-mans version of the deblocking filter which you
       | find in recent video standards, but jpeg was to early to get it.
       | 
       | The advantage of those over this is that the encoder knows that
       | the decoder is going to run the filter, and it can use that in
       | its perceptual model to choose an encoding which looks most like
       | the original _after deblocking_. Video codecs also use the
       | deblocked version as reference frames. So you see these artifacts
       | in mpeg-2 video and jpeg, but not more recent ones.
        
       | pents90 wrote:
       | It's long past time to retire Lena as the canonical test image in
       | CG.
        
         | pkulak wrote:
         | Lena and Big Buck Bunny for life!
        
         | enriquto wrote:
         | Why? It's like "lorem ipsum" at this point.
        
           | KarlKemp wrote:
           | Fish have no concept of water in very much the same way as
           | you not seeing the problem here.
        
             | flumpcakes wrote:
             | How about you respect women in tech more and stop assuming
             | they're so fragile that they need men to change an industry
             | so they don't get their feelings hurt.
             | 
             | Women are people, they don't care.
        
           | NoSorryCannot wrote:
           | If lorem ipsum were softcore erotica...
        
             | enriquto wrote:
             | What if I told you it actually is?
             | 
             | > Quis autem vel eum iure reprehenderit, qui inea voluptate
             | velit esse, quam nihil molestiae consequatur, vel illum,
             | qui dolorem eum fugiat, quo voluptas nulla pariatur?
             | 
             | Translation: Who has any right to find fault with a man who
             | chooses to enjoy a pleasure that has no annoying
             | consequences, or one who avoids a pain that produces no
             | resultant pleasure?
             | 
             | This text could very well be the first page of a softcore
             | erotica (or even hardcore!). The rest of the text is not
             | usually visible. But then again, neither is the rest of
             | lena!
        
               | YetAnotherNick wrote:
               | I knew other commenters would bash you for proving their
               | point, but this is quite the best comeback I have seen in
               | long time.
        
               | NoSorryCannot wrote:
               | This is a really big stretch in interpretation but also
               | in plausibility, since Latin is a dead language.
               | 
               | But if it were true, then you'd have it your way: it'd
               | also be inappropriate in professional settings that don't
               | naturally necessitate it.
        
               | 2038AD wrote:
               | > This is a really big stretch in interpretation
               | 
               | According to Wikipedia[0], "[t]he placeholder text is
               | taken from parts of the first book's discourse on
               | hedonism." The first book being the first book of
               | Cicero's _De finibus bonorum et malorum_.
               | 
               | [0] https://en.wikipedia.org/wiki/De_finibus_bonorum_et_m
               | alorum
        
               | NoSorryCannot wrote:
               | Least plausible of all is that someone would try to shore
               | up this argument in good faith.
        
               | 2038AD wrote:
               | To be honest I'm sure it isn't erotica just as the Holy
               | Bible is not despite verses like Ezekiel 23:20. I must
               | also confess I did find the revelation a little amusing.
               | It certainly says something about men (whether that's
               | just the men of 70s from when both originate).
        
               | crazygringo wrote:
               | Then... you'd be wrong.
               | 
               | It's a philosophical argument against Epicurianism. Which
               | has as much in common with softcare erotica as the phone
               | book.
               | 
               | Not sure what your point is.
        
         | crazygringo wrote:
         | Seriously. People wonder why there aren't enough women in CS.
         | 
         | Things like the canonical test image being a sexy "come hither"
         | look, coming from a Playboy centerfold, definitely don't help.
         | 
         | And it's not enough to say "well the rest of the centerfold
         | isn't shown". Come on. We all know the source of the image.
         | 
         | And "tradition" is far less important than CS being a welcoming
         | environment for all people.
         | 
         | Computer science is a profession. Let's use images that are
         | appropriate for a professional setting.
        
           | FriendlyNormie wrote:
           | Shut up you dumb fucking faggot.
        
           | hombre_fatal wrote:
           | The idea that women can't handle something like the Lena
           | lorem ipsum seems more potentially offputting to women, to
           | me.
           | 
           | Seems ironic that guys race over each other to claim a
           | picture of a woman is the shibboleth that keeps women out.
        
             | NoSorryCannot wrote:
             | Fortunate for us that there is a similar race to apologize
             | for erotic photographs in tech demos, as if it's not an
             | obvious anachronism.
             | 
             | Really, defending something so clearly out-of-place is
             | deeply suspicious. Maybe everyone can handle it but why are
             | we being asked to?
        
               | flumpcakes wrote:
               | If you find Lenna "erotic" and "out-of-place" then you
               | probably shouldn't watch TV or 90% of media.
               | 
               | It's a picture of a model's face. You're an adult, deal
               | with it.
        
               | NoSorryCannot wrote:
               | It is astonishing that we can't even agree that the photo
               | is erotic.
               | 
               | Or that there's a meaningful difference between media you
               | choose to consume for entertainment (which may be honest-
               | to-goodness porn and that's _fine_) and irrelevant
               | eroticism casually sprinkled in tech demos that don't
               | explicitly involve sex.
               | 
               | I wish we could at least be real about the facts in
               | evidence.
        
             | adrianmonk wrote:
             | OK, time for my pet theory on this.
             | 
             | It's not the picture itself that creates an obstacle. It's
             | that emotionally-aware people want to go into a career
             | where they like being around their co-workers.
             | 
             | So if they get the impression that many of the people in
             | the field are completely tone-deaf about basic stuff, they
             | start asking themselves questions like, "Since I have
             | limited time on this planet, why would I want to spend it
             | where a good percentage of the people around me are
             | insufferable asshats?" And then they pick some other field.
             | Not because they can't handle it. Because they know they
             | can do better for themselves.
        
             | crazygringo wrote:
             | Of course women can _handle_ it. We 're all adults.
             | 
             | But is it _appropriate in context_? Is it _welcoming_? If
             | you have a choice of jobs, is it the kind of environment
             | you _prefer_?
             | 
             | Or is it contributing to an image of "we're a bunch of
             | immature boys who think it's funny to use a Playboy
             | centerfold, har har, wait till you hear the sexist jokes we
             | tell at lunch".
             | 
             | It's not the end of the world. But a welcoming and
             | inclusive work environment is the sum of hundreds of little
             | things. This is one of those things.
             | 
             | And come on -- the old line "what's the matter, can't you
             | handle a joke?" is the oldest line in the book for
             | "defending" sexist behavior. Expecting women to just
             | "handle something" is not the right approach. We can be
             | better than that.
        
               | hombre_fatal wrote:
               | To put a finer point on my comment, I'm claiming that
               | it's not something that _needs_ to be handled. Women can
               | handle it in the same way they can handle a cloudy day or
               | a man not racing over to hold the door open.
               | 
               | I would suspect your shrill infantilization of women and
               | the vicarious outrage on the behalf of women is something
               | much harder to handle if you want to minimize the
               | handling women need to do.
               | 
               | How many people reading this even saw or knew about the
               | uncropped Lena picture until they saw men on HN/Reddit
               | nodding vigorously at each other over over how many women
               | pass on STEM because of it.
               | 
               | I see outrage like yours and wonder when was the last
               | time you actually polled a woman's opinion on something
               | like Lena. I'd wager 95+% of women roll their eyes that a
               | cropped Playboy shot even moves the needle.
        
               | repiret wrote:
               | My first programming job was for a media processing
               | company, and we used the Lena image, and it's origin was
               | well known, and we were mostly young men so we all found
               | copies of the entire shoot, and they were occasionally
               | involved in office pranks. None of us learned it from
               | HN/Reddit because those weren't things in 2000.
               | 
               | I cannot imagine it was a welcoming environment for
               | women.
        
               | tehjoker wrote:
               | >> a man not racing over to hold the door open.
               | 
               | It's telling that you think this is something that would
               | even annoy most women, especially in a professional non-
               | dating context. The women I know would feel creeped out
               | if someone did that at work. Honestly, even in dating if
               | you're "racing" to open the door it looks desperate. When
               | done naturally some women like it and some think it's
               | archaic.
        
               | bityard wrote:
               | I'm sorry you have to endure such an antagonistic social
               | environment. Where I live (midwest US), it's considered
               | polite for all people hold the door open for all others,
               | regardless of gender.
               | 
               | I'm a male and if I worked in a place where women sneered
               | at me for holding the door for them, I guess I would
               | start only holding the door for men.
        
               | crazygringo wrote:
               | Holding the door open for all genders is widely seen as
               | polite and fine, for whoever arrives at the door first,
               | man or woman. It's an optional courtesy.
               | 
               | Having a man "race over" to hold a door, and doing so
               | _because they 're a woman_ where he wouldn't for another
               | man, in a professional context, is creepy and weird.
               | 
               | Do you see the difference? What you're describing in the
               | midwest is fine. But it's not what the parent was
               | responding to. The "antagonistic social environment" with
               | "women sneering" you're describing is a total straw-man
               | of your own imagination.
        
               | flumpcakes wrote:
               | Thank you. This is very well put. It makes me quite angry
               | to see people racing to remove images or language that at
               | a push could be seen as offensive, to protect groups that
               | don't need or want it.
               | 
               | It's insulting to suggest your black co-worker can't use
               | the same language you've always used because you're now
               | saying they are more fragile.
               | 
               | It seems to be a race to the bottom in the name of
               | inclusivity and all we're doing is everything but
               | actually improving the opportunities of groups that are
               | "under represented" in CS.
               | 
               | Instead of "banning Lenna" how about we create free
               | resources teaching CS skills in a way that will interest
               | women/"others".
               | 
               | Does that mean making the slides pink?
               | 
               | Solving problems to do with manging boyfriends?
               | 
               | No. That's insulting, because women are people like
               | everyone else so stop treating them as fragile brainless
               | creatures that need to be wrapped in cotton wool just to
               | survive in the scary man's world.
        
               | crazygringo wrote:
               | I don't think _I 'm_ the one being shrill here. ;)
               | 
               | Unfortunately, your type of attitude is precisely the
               | problem. You deny a problem exists, attack anyone who
               | suggests fixing it, and resort to questioning character
               | and intelligence.
               | 
               | I'm sorry that this topic is making you angry. But
               | sometimes it's important to hear other voices. In your
               | case, instead of making a wager, why don't you simply
               | talk to some of your female friends. You don't need to
               | "poll" them, just have a friendly conversation. Don't ask
               | them whether they can "handle" it, but what they would
               | _prefer_.
               | 
               | Your eyes might be opened.
               | 
               | I'm talking from actual diverse workplace experiences
               | with issues like this. You don't appear to be, or else I
               | don't think you'd be saying the things you are.
        
               | ashearer wrote:
               | > Women can handle it in the same way they can handle a
               | cloudy day
               | 
               | Going with that, then why create that cloudy day when it
               | would take very little effort not to?
               | 
               | And what message does it send to actively defend creating
               | those cloudy days?
        
               | 2038AD wrote:
               | I guess the flip-side is that CS is so welcoming and
               | inclusive that tech conferences will invite former sex
               | workers and celebrate them. No slut-shaming from the
               | techies!
        
               | lazyjones wrote:
               | > is it appropriate in context?
               | 
               | Depends on whether you would consider an image very
               | similar to many, many others that get compressed to JPEG
               | is "appropriate" (i.e. relevant, representative), or
               | whether you are asking as a prude.
               | 
               | > Is it welcoming?
               | 
               | I can't see anything unwelcoming about that picture, at
               | least not in any other way than many people's flattering
               | profile pictures on social media. It's just a head of an
               | attractive woman, shot by a professional photographer.
        
         | rotskoff wrote:
         | Came here to make the exact same comment, agree completely!
        
       ___________________________________________________________________
       (page generated 2020-07-10 23:01 UTC)