libjxl

FORK: libjxl patches used on blog
git clone https://git.neptards.moe/blog/libjxl.git
Log | Files | Refs | Submodules | README | LICENSE

squeeze.h (3446B)


      1 // Copyright (c) the JPEG XL Project Authors. All rights reserved.
      2 //
      3 // Use of this source code is governed by a BSD-style
      4 // license that can be found in the LICENSE file.
      5 
      6 #ifndef LIB_JXL_MODULAR_TRANSFORM_SQUEEZE_H_
      7 #define LIB_JXL_MODULAR_TRANSFORM_SQUEEZE_H_
      8 
      9 // Haar-like transform: halves the resolution in one direction
     10 // A B   -> (A+B)>>1              in one channel (average)  -> same range as
     11 // original channel
     12 //          A-B - tendency        in a new channel ('residual' needed to make
     13 //          the transform reversible)
     14 //                                        -> theoretically range could be 2.5
     15 //                                        times larger (2 times without the
     16 //                                        'tendency'), but there should be lots
     17 //                                        of zeroes
     18 // Repeated application (alternating horizontal and vertical squeezes) results
     19 // in downscaling
     20 //
     21 // The default coefficient ordering is low-frequency to high-frequency, as in
     22 // M. Antonini, M. Barlaud, P. Mathieu and I. Daubechies, "Image coding using
     23 // wavelet transform", IEEE Transactions on Image Processing, vol. 1, no. 2, pp.
     24 // 205-220, April 1992, doi: 10.1109/83.136597.
     25 
     26 #include <stdlib.h>
     27 
     28 #include "lib/jxl/base/data_parallel.h"
     29 #include "lib/jxl/modular/modular_image.h"
     30 #include "lib/jxl/modular/transform/transform.h"
     31 
     32 #define JXL_MAX_FIRST_PREVIEW_SIZE 8
     33 
     34 namespace jxl {
     35 
     36 /*
     37         int avg=(A+B)>>1;
     38         int diff=(A-B);
     39         int rA=(diff+(avg<<1)+(diff&1))>>1;
     40         int rB=rA-diff;
     41 
     42 */
     43 //         |A B|C D|E F|
     44 //           p   a   n             p=avg(A,B), a=avg(C,D), n=avg(E,F)
     45 //
     46 // Goal: estimate C-D (avoiding ringing artifacts)
     47 // (ensuring that in smooth areas, a zero residual corresponds to a smooth
     48 // gradient)
     49 
     50 // best estimate for C: (B + 2*a)/3
     51 // best estimate for D: (n + 3*a)/4
     52 // best estimate for C-D:  4*B - 3*n - a /12
     53 
     54 // avoid ringing by 1) only doing this if B <= a <= n  or  B >= a >= n
     55 // (otherwise, this is not a smooth area and we cannot really estimate C-D)
     56 //                  2) making sure that B <= C <= D <= n  or B >= C >= D >= n
     57 
     58 inline pixel_type_w SmoothTendency(pixel_type_w B, pixel_type_w a,
     59                                    pixel_type_w n) {
     60   pixel_type_w diff = 0;
     61   if (B >= a && a >= n) {
     62     diff = (4 * B - 3 * n - a + 6) / 12;
     63     //      2C = a<<1 + diff - diff&1 <= 2B  so diff - diff&1 <= 2B - 2a
     64     //      2D = a<<1 - diff - diff&1 >= 2n  so diff + diff&1 <= 2a - 2n
     65     if (diff - (diff & 1) > 2 * (B - a)) diff = 2 * (B - a) + 1;
     66     if (diff + (diff & 1) > 2 * (a - n)) diff = 2 * (a - n);
     67   } else if (B <= a && a <= n) {
     68     diff = (4 * B - 3 * n - a - 6) / 12;
     69     //      2C = a<<1 + diff + diff&1 >= 2B  so diff + diff&1 >= 2B - 2a
     70     //      2D = a<<1 - diff + diff&1 <= 2n  so diff - diff&1 >= 2a - 2n
     71     if (diff + (diff & 1) < 2 * (B - a)) diff = 2 * (B - a) - 1;
     72     if (diff - (diff & 1) < 2 * (a - n)) diff = 2 * (a - n);
     73   }
     74   return diff;
     75 }
     76 
     77 void DefaultSqueezeParameters(std::vector<SqueezeParams> *parameters,
     78                               const Image &image);
     79 
     80 Status CheckMetaSqueezeParams(const SqueezeParams &parameter, int num_channels);
     81 
     82 Status MetaSqueeze(Image &image, std::vector<SqueezeParams> *parameters);
     83 
     84 Status InvSqueeze(Image &input, const std::vector<SqueezeParams> &parameters,
     85                   ThreadPool *pool);
     86 
     87 }  // namespace jxl
     88 
     89 #endif  // LIB_JXL_MODULAR_TRANSFORM_SQUEEZE_H_