1*b2055c35SXin Li // Copyright 2016 Google Inc. All Rights Reserved.
2*b2055c35SXin Li //
3*b2055c35SXin Li // Use of this source code is governed by a BSD-style license
4*b2055c35SXin Li // that can be found in the COPYING file in the root of the source
5*b2055c35SXin Li // tree. An additional intellectual property rights grant can be found
6*b2055c35SXin Li // in the file PATENTS. All contributing project authors may
7*b2055c35SXin Li // be found in the AUTHORS file in the root of the source tree.
8*b2055c35SXin Li // -----------------------------------------------------------------------------
9*b2055c35SXin Li //
10*b2055c35SXin Li // Simple tool to load two webp/png/jpg/tiff files and compute PSNR/SSIM.
11*b2055c35SXin Li // This is mostly a wrapper around WebPPictureDistortion().
12*b2055c35SXin Li //
13*b2055c35SXin Li /*
14*b2055c35SXin Li gcc -o get_disto get_disto.c -O3 -I../ -L../examples -L../imageio \
15*b2055c35SXin Li -lexample_util -limageio_util -limagedec -lwebp -L/opt/local/lib \
16*b2055c35SXin Li -lpng -lz -ljpeg -ltiff -lm -lpthread
17*b2055c35SXin Li */
18*b2055c35SXin Li //
19*b2055c35SXin Li // Author: Skal ([email protected])
20*b2055c35SXin Li
21*b2055c35SXin Li #include <assert.h>
22*b2055c35SXin Li #include <stdio.h>
23*b2055c35SXin Li #include <stdlib.h>
24*b2055c35SXin Li #include <string.h>
25*b2055c35SXin Li
26*b2055c35SXin Li #include "webp/encode.h"
27*b2055c35SXin Li #include "imageio/image_dec.h"
28*b2055c35SXin Li #include "imageio/imageio_util.h"
29*b2055c35SXin Li #include "../examples/unicode.h"
30*b2055c35SXin Li
ReadPicture(const char * const filename,WebPPicture * const pic,int keep_alpha)31*b2055c35SXin Li static size_t ReadPicture(const char* const filename, WebPPicture* const pic,
32*b2055c35SXin Li int keep_alpha) {
33*b2055c35SXin Li const uint8_t* data = NULL;
34*b2055c35SXin Li size_t data_size = 0;
35*b2055c35SXin Li WebPImageReader reader = NULL;
36*b2055c35SXin Li int ok = ImgIoUtilReadFile(filename, &data, &data_size);
37*b2055c35SXin Li if (!ok) goto End;
38*b2055c35SXin Li
39*b2055c35SXin Li pic->use_argb = 1; // force ARGB
40*b2055c35SXin Li
41*b2055c35SXin Li #ifdef HAVE_WINCODEC_H
42*b2055c35SXin Li // Try to decode the file using WIC falling back to the other readers for
43*b2055c35SXin Li // e.g., WebP.
44*b2055c35SXin Li ok = ReadPictureWithWIC(filename, pic, keep_alpha, NULL);
45*b2055c35SXin Li if (ok) goto End;
46*b2055c35SXin Li #endif
47*b2055c35SXin Li reader = WebPGuessImageReader(data, data_size);
48*b2055c35SXin Li ok = reader(data, data_size, pic, keep_alpha, NULL);
49*b2055c35SXin Li
50*b2055c35SXin Li End:
51*b2055c35SXin Li if (!ok) {
52*b2055c35SXin Li WFPRINTF(stderr, "Error! Could not process file %s\n",
53*b2055c35SXin Li (const W_CHAR*)filename);
54*b2055c35SXin Li }
55*b2055c35SXin Li free((void*)data);
56*b2055c35SXin Li return ok ? data_size : 0;
57*b2055c35SXin Li }
58*b2055c35SXin Li
RescalePlane(uint8_t * plane,int width,int height,int x_stride,int y_stride,int max)59*b2055c35SXin Li static void RescalePlane(uint8_t* plane, int width, int height,
60*b2055c35SXin Li int x_stride, int y_stride, int max) {
61*b2055c35SXin Li const uint32_t factor = (max > 0) ? (255u << 16) / max : 0;
62*b2055c35SXin Li int x, y;
63*b2055c35SXin Li for (y = 0; y < height; ++y) {
64*b2055c35SXin Li uint8_t* const ptr = plane + y * y_stride;
65*b2055c35SXin Li for (x = 0; x < width * x_stride; x += x_stride) {
66*b2055c35SXin Li const uint32_t diff = (ptr[x] * factor + (1 << 15)) >> 16;
67*b2055c35SXin Li ptr[x] = diff;
68*b2055c35SXin Li }
69*b2055c35SXin Li }
70*b2055c35SXin Li }
71*b2055c35SXin Li
72*b2055c35SXin Li // Return the max absolute difference.
DiffScaleChannel(uint8_t * src1,int stride1,const uint8_t * src2,int stride2,int x_stride,int w,int h,int do_scaling)73*b2055c35SXin Li static int DiffScaleChannel(uint8_t* src1, int stride1,
74*b2055c35SXin Li const uint8_t* src2, int stride2,
75*b2055c35SXin Li int x_stride, int w, int h, int do_scaling) {
76*b2055c35SXin Li int x, y;
77*b2055c35SXin Li int max = 0;
78*b2055c35SXin Li for (y = 0; y < h; ++y) {
79*b2055c35SXin Li uint8_t* const ptr1 = src1 + y * stride1;
80*b2055c35SXin Li const uint8_t* const ptr2 = src2 + y * stride2;
81*b2055c35SXin Li for (x = 0; x < w * x_stride; x += x_stride) {
82*b2055c35SXin Li const int diff = abs(ptr1[x] - ptr2[x]);
83*b2055c35SXin Li if (diff > max) max = diff;
84*b2055c35SXin Li ptr1[x] = diff;
85*b2055c35SXin Li }
86*b2055c35SXin Li }
87*b2055c35SXin Li
88*b2055c35SXin Li if (do_scaling) RescalePlane(src1, w, h, x_stride, stride1, max);
89*b2055c35SXin Li return max;
90*b2055c35SXin Li }
91*b2055c35SXin Li
92*b2055c35SXin Li //------------------------------------------------------------------------------
93*b2055c35SXin Li // SSIM calculation. We re-implement these functions here, out of dsp/, to avoid
94*b2055c35SXin Li // breaking the library's hidden visibility. This code duplication avoids the
95*b2055c35SXin Li // bigger annoyance of having to open up internal details of libdsp...
96*b2055c35SXin Li
97*b2055c35SXin Li #define SSIM_KERNEL 3 // total size of the kernel: 2 * SSIM_KERNEL + 1
98*b2055c35SXin Li
99*b2055c35SXin Li // struct for accumulating statistical moments
100*b2055c35SXin Li typedef struct {
101*b2055c35SXin Li uint32_t w; // sum(w_i) : sum of weights
102*b2055c35SXin Li uint32_t xm, ym; // sum(w_i * x_i), sum(w_i * y_i)
103*b2055c35SXin Li uint32_t xxm, xym, yym; // sum(w_i * x_i * x_i), etc.
104*b2055c35SXin Li } DistoStats;
105*b2055c35SXin Li
106*b2055c35SXin Li // hat-shaped filter. Sum of coefficients is equal to 16.
107*b2055c35SXin Li static const uint32_t kWeight[2 * SSIM_KERNEL + 1] = { 1, 2, 3, 4, 3, 2, 1 };
108*b2055c35SXin Li
SSIMCalculation(const DistoStats * const stats)109*b2055c35SXin Li static WEBP_INLINE double SSIMCalculation(const DistoStats* const stats) {
110*b2055c35SXin Li const uint32_t N = stats->w;
111*b2055c35SXin Li const uint32_t w2 = N * N;
112*b2055c35SXin Li const uint32_t C1 = 20 * w2;
113*b2055c35SXin Li const uint32_t C2 = 60 * w2;
114*b2055c35SXin Li const uint32_t C3 = 8 * 8 * w2; // 'dark' limit ~= 6
115*b2055c35SXin Li const uint64_t xmxm = (uint64_t)stats->xm * stats->xm;
116*b2055c35SXin Li const uint64_t ymym = (uint64_t)stats->ym * stats->ym;
117*b2055c35SXin Li if (xmxm + ymym >= C3) {
118*b2055c35SXin Li const int64_t xmym = (int64_t)stats->xm * stats->ym;
119*b2055c35SXin Li const int64_t sxy = (int64_t)stats->xym * N - xmym; // can be negative
120*b2055c35SXin Li const uint64_t sxx = (uint64_t)stats->xxm * N - xmxm;
121*b2055c35SXin Li const uint64_t syy = (uint64_t)stats->yym * N - ymym;
122*b2055c35SXin Li // we descale by 8 to prevent overflow during the fnum/fden multiply.
123*b2055c35SXin Li const uint64_t num_S = (2 * (uint64_t)(sxy < 0 ? 0 : sxy) + C2) >> 8;
124*b2055c35SXin Li const uint64_t den_S = (sxx + syy + C2) >> 8;
125*b2055c35SXin Li const uint64_t fnum = (2 * xmym + C1) * num_S;
126*b2055c35SXin Li const uint64_t fden = (xmxm + ymym + C1) * den_S;
127*b2055c35SXin Li const double r = (double)fnum / fden;
128*b2055c35SXin Li assert(r >= 0. && r <= 1.0);
129*b2055c35SXin Li return r;
130*b2055c35SXin Li }
131*b2055c35SXin Li return 1.; // area is too dark to contribute meaningfully
132*b2055c35SXin Li }
133*b2055c35SXin Li
SSIMGetClipped(const uint8_t * src1,int stride1,const uint8_t * src2,int stride2,int xo,int yo,int W,int H)134*b2055c35SXin Li static double SSIMGetClipped(const uint8_t* src1, int stride1,
135*b2055c35SXin Li const uint8_t* src2, int stride2,
136*b2055c35SXin Li int xo, int yo, int W, int H) {
137*b2055c35SXin Li DistoStats stats = { 0, 0, 0, 0, 0, 0 };
138*b2055c35SXin Li const int ymin = (yo - SSIM_KERNEL < 0) ? 0 : yo - SSIM_KERNEL;
139*b2055c35SXin Li const int ymax = (yo + SSIM_KERNEL > H - 1) ? H - 1 : yo + SSIM_KERNEL;
140*b2055c35SXin Li const int xmin = (xo - SSIM_KERNEL < 0) ? 0 : xo - SSIM_KERNEL;
141*b2055c35SXin Li const int xmax = (xo + SSIM_KERNEL > W - 1) ? W - 1 : xo + SSIM_KERNEL;
142*b2055c35SXin Li int x, y;
143*b2055c35SXin Li src1 += ymin * stride1;
144*b2055c35SXin Li src2 += ymin * stride2;
145*b2055c35SXin Li for (y = ymin; y <= ymax; ++y, src1 += stride1, src2 += stride2) {
146*b2055c35SXin Li for (x = xmin; x <= xmax; ++x) {
147*b2055c35SXin Li const uint32_t w = kWeight[SSIM_KERNEL + x - xo]
148*b2055c35SXin Li * kWeight[SSIM_KERNEL + y - yo];
149*b2055c35SXin Li const uint32_t s1 = src1[x];
150*b2055c35SXin Li const uint32_t s2 = src2[x];
151*b2055c35SXin Li stats.w += w;
152*b2055c35SXin Li stats.xm += w * s1;
153*b2055c35SXin Li stats.ym += w * s2;
154*b2055c35SXin Li stats.xxm += w * s1 * s1;
155*b2055c35SXin Li stats.xym += w * s1 * s2;
156*b2055c35SXin Li stats.yym += w * s2 * s2;
157*b2055c35SXin Li }
158*b2055c35SXin Li }
159*b2055c35SXin Li return SSIMCalculation(&stats);
160*b2055c35SXin Li }
161*b2055c35SXin Li
162*b2055c35SXin Li // Compute SSIM-score map. Return -1 in case of error, max diff otherwise.
SSIMScaleChannel(uint8_t * src1,int stride1,const uint8_t * src2,int stride2,int x_stride,int w,int h,int do_scaling)163*b2055c35SXin Li static int SSIMScaleChannel(uint8_t* src1, int stride1,
164*b2055c35SXin Li const uint8_t* src2, int stride2,
165*b2055c35SXin Li int x_stride, int w, int h, int do_scaling) {
166*b2055c35SXin Li int x, y;
167*b2055c35SXin Li int max = 0;
168*b2055c35SXin Li uint8_t* const plane1 = (uint8_t*)malloc(2 * w * h * sizeof(*plane1));
169*b2055c35SXin Li uint8_t* const plane2 = plane1 + w * h;
170*b2055c35SXin Li if (plane1 == NULL) return -1;
171*b2055c35SXin Li
172*b2055c35SXin Li // extract plane
173*b2055c35SXin Li for (y = 0; y < h; ++y) {
174*b2055c35SXin Li for (x = 0; x < w; ++x) {
175*b2055c35SXin Li plane1[x + y * w] = src1[x * x_stride + y * stride1];
176*b2055c35SXin Li plane2[x + y * w] = src2[x * x_stride + y * stride2];
177*b2055c35SXin Li }
178*b2055c35SXin Li }
179*b2055c35SXin Li for (y = 0; y < h; ++y) {
180*b2055c35SXin Li for (x = 0; x < w; ++x) {
181*b2055c35SXin Li const double ssim = SSIMGetClipped(plane1, w, plane2, w, x, y, w, h);
182*b2055c35SXin Li int diff = (int)(255 * (1. - ssim));
183*b2055c35SXin Li if (diff < 0) {
184*b2055c35SXin Li diff = 0;
185*b2055c35SXin Li } else if (diff > max) {
186*b2055c35SXin Li max = diff;
187*b2055c35SXin Li }
188*b2055c35SXin Li src1[x * x_stride + y * stride1] = (diff > 255) ? 255u : (uint8_t)diff;
189*b2055c35SXin Li }
190*b2055c35SXin Li }
191*b2055c35SXin Li free(plane1);
192*b2055c35SXin Li
193*b2055c35SXin Li if (do_scaling) RescalePlane(src1, w, h, x_stride, stride1, max);
194*b2055c35SXin Li return max;
195*b2055c35SXin Li }
196*b2055c35SXin Li
197*b2055c35SXin Li // Convert an argb picture to luminance.
ConvertToGray(WebPPicture * const pic)198*b2055c35SXin Li static void ConvertToGray(WebPPicture* const pic) {
199*b2055c35SXin Li int x, y;
200*b2055c35SXin Li assert(pic != NULL);
201*b2055c35SXin Li assert(pic->use_argb);
202*b2055c35SXin Li for (y = 0; y < pic->height; ++y) {
203*b2055c35SXin Li uint32_t* const row = &pic->argb[y * pic->argb_stride];
204*b2055c35SXin Li for (x = 0; x < pic->width; ++x) {
205*b2055c35SXin Li const uint32_t argb = row[x];
206*b2055c35SXin Li const uint32_t r = (argb >> 16) & 0xff;
207*b2055c35SXin Li const uint32_t g = (argb >> 8) & 0xff;
208*b2055c35SXin Li const uint32_t b = (argb >> 0) & 0xff;
209*b2055c35SXin Li // We use BT.709 for converting to luminance.
210*b2055c35SXin Li const uint32_t Y = (uint32_t)(0.2126 * r + 0.7152 * g + 0.0722 * b + .5);
211*b2055c35SXin Li row[x] = (argb & 0xff000000u) | (Y * 0x010101u);
212*b2055c35SXin Li }
213*b2055c35SXin Li }
214*b2055c35SXin Li }
215*b2055c35SXin Li
Help(void)216*b2055c35SXin Li static void Help(void) {
217*b2055c35SXin Li fprintf(stderr,
218*b2055c35SXin Li "Usage: get_disto [-ssim][-psnr][-alpha] compressed.webp orig.webp\n"
219*b2055c35SXin Li " -ssim ..... print SSIM distortion\n"
220*b2055c35SXin Li " -psnr ..... print PSNR distortion (default)\n"
221*b2055c35SXin Li " -alpha .... preserve alpha plane\n"
222*b2055c35SXin Li " -h ........ this message\n"
223*b2055c35SXin Li " -o <file> . save the diff map as a WebP lossless file\n"
224*b2055c35SXin Li " -scale .... scale the difference map to fit [0..255] range\n"
225*b2055c35SXin Li " -gray ..... use grayscale for difference map (-scale)\n"
226*b2055c35SXin Li "\nSupported input formats:\n %s\n",
227*b2055c35SXin Li WebPGetEnabledInputFileFormats());
228*b2055c35SXin Li }
229*b2055c35SXin Li
main(int argc,const char * argv[])230*b2055c35SXin Li int main(int argc, const char* argv[]) {
231*b2055c35SXin Li WebPPicture pic1, pic2;
232*b2055c35SXin Li size_t size1 = 0, size2 = 0;
233*b2055c35SXin Li int ret = 1;
234*b2055c35SXin Li float disto[5];
235*b2055c35SXin Li int type = 0;
236*b2055c35SXin Li int c;
237*b2055c35SXin Li int help = 0;
238*b2055c35SXin Li int keep_alpha = 0;
239*b2055c35SXin Li int scale = 0;
240*b2055c35SXin Li int use_gray = 0;
241*b2055c35SXin Li const char* name1 = NULL;
242*b2055c35SXin Li const char* name2 = NULL;
243*b2055c35SXin Li const char* output = NULL;
244*b2055c35SXin Li
245*b2055c35SXin Li INIT_WARGV(argc, argv);
246*b2055c35SXin Li
247*b2055c35SXin Li if (!WebPPictureInit(&pic1) || !WebPPictureInit(&pic2)) {
248*b2055c35SXin Li fprintf(stderr, "Can't init pictures\n");
249*b2055c35SXin Li FREE_WARGV_AND_RETURN(1);
250*b2055c35SXin Li }
251*b2055c35SXin Li
252*b2055c35SXin Li for (c = 1; c < argc; ++c) {
253*b2055c35SXin Li if (!strcmp(argv[c], "-ssim")) {
254*b2055c35SXin Li type = 1;
255*b2055c35SXin Li } else if (!strcmp(argv[c], "-psnr")) {
256*b2055c35SXin Li type = 0;
257*b2055c35SXin Li } else if (!strcmp(argv[c], "-alpha")) {
258*b2055c35SXin Li keep_alpha = 1;
259*b2055c35SXin Li } else if (!strcmp(argv[c], "-scale")) {
260*b2055c35SXin Li scale = 1;
261*b2055c35SXin Li } else if (!strcmp(argv[c], "-gray")) {
262*b2055c35SXin Li use_gray = 1;
263*b2055c35SXin Li } else if (!strcmp(argv[c], "-h")) {
264*b2055c35SXin Li help = 1;
265*b2055c35SXin Li ret = 0;
266*b2055c35SXin Li } else if (!strcmp(argv[c], "-o")) {
267*b2055c35SXin Li if (++c == argc) {
268*b2055c35SXin Li fprintf(stderr, "missing file name after %s option.\n", argv[c - 1]);
269*b2055c35SXin Li goto End;
270*b2055c35SXin Li }
271*b2055c35SXin Li output = (const char*)GET_WARGV(argv, c);
272*b2055c35SXin Li } else if (name1 == NULL) {
273*b2055c35SXin Li name1 = (const char*)GET_WARGV(argv, c);
274*b2055c35SXin Li } else {
275*b2055c35SXin Li name2 = (const char*)GET_WARGV(argv, c);
276*b2055c35SXin Li }
277*b2055c35SXin Li }
278*b2055c35SXin Li if (help || name1 == NULL || name2 == NULL) {
279*b2055c35SXin Li if (!help) {
280*b2055c35SXin Li fprintf(stderr, "Error: missing arguments.\n");
281*b2055c35SXin Li }
282*b2055c35SXin Li Help();
283*b2055c35SXin Li goto End;
284*b2055c35SXin Li }
285*b2055c35SXin Li size1 = ReadPicture(name1, &pic1, 1);
286*b2055c35SXin Li size2 = ReadPicture(name2, &pic2, 1);
287*b2055c35SXin Li if (size1 == 0 || size2 == 0) goto End;
288*b2055c35SXin Li
289*b2055c35SXin Li if (!keep_alpha) {
290*b2055c35SXin Li WebPBlendAlpha(&pic1, 0x00000000);
291*b2055c35SXin Li WebPBlendAlpha(&pic2, 0x00000000);
292*b2055c35SXin Li }
293*b2055c35SXin Li
294*b2055c35SXin Li if (!WebPPictureDistortion(&pic1, &pic2, type, disto)) {
295*b2055c35SXin Li fprintf(stderr, "Error while computing the distortion.\n");
296*b2055c35SXin Li goto End;
297*b2055c35SXin Li }
298*b2055c35SXin Li printf("%u %.2f %.2f %.2f %.2f %.2f [ %.2f bpp ]\n",
299*b2055c35SXin Li (unsigned int)size1,
300*b2055c35SXin Li disto[4], disto[0], disto[1], disto[2], disto[3],
301*b2055c35SXin Li 8.f * size1 / pic1.width / pic1.height);
302*b2055c35SXin Li
303*b2055c35SXin Li if (output != NULL) {
304*b2055c35SXin Li uint8_t* data = NULL;
305*b2055c35SXin Li size_t data_size = 0;
306*b2055c35SXin Li if (pic1.use_argb != pic2.use_argb) {
307*b2055c35SXin Li fprintf(stderr, "Pictures are not in the same argb format. "
308*b2055c35SXin Li "Can't save the difference map.\n");
309*b2055c35SXin Li goto End;
310*b2055c35SXin Li }
311*b2055c35SXin Li if (pic1.use_argb) {
312*b2055c35SXin Li int n;
313*b2055c35SXin Li fprintf(stderr, "max differences per channel: ");
314*b2055c35SXin Li for (n = 0; n < 3; ++n) { // skip the alpha channel
315*b2055c35SXin Li const int range = (type == 1) ?
316*b2055c35SXin Li SSIMScaleChannel((uint8_t*)pic1.argb + n, pic1.argb_stride * 4,
317*b2055c35SXin Li (const uint8_t*)pic2.argb + n, pic2.argb_stride * 4,
318*b2055c35SXin Li 4, pic1.width, pic1.height, scale) :
319*b2055c35SXin Li DiffScaleChannel((uint8_t*)pic1.argb + n, pic1.argb_stride * 4,
320*b2055c35SXin Li (const uint8_t*)pic2.argb + n, pic2.argb_stride * 4,
321*b2055c35SXin Li 4, pic1.width, pic1.height, scale);
322*b2055c35SXin Li if (range < 0) fprintf(stderr, "\nError computing diff map\n");
323*b2055c35SXin Li fprintf(stderr, "[%d]", range);
324*b2055c35SXin Li }
325*b2055c35SXin Li fprintf(stderr, "\n");
326*b2055c35SXin Li if (use_gray) ConvertToGray(&pic1);
327*b2055c35SXin Li } else {
328*b2055c35SXin Li fprintf(stderr, "Can only compute the difference map in ARGB format.\n");
329*b2055c35SXin Li goto End;
330*b2055c35SXin Li }
331*b2055c35SXin Li #if !defined(WEBP_REDUCE_CSP)
332*b2055c35SXin Li data_size = WebPEncodeLosslessBGRA((const uint8_t*)pic1.argb,
333*b2055c35SXin Li pic1.width, pic1.height,
334*b2055c35SXin Li pic1.argb_stride * 4,
335*b2055c35SXin Li &data);
336*b2055c35SXin Li if (data_size == 0) {
337*b2055c35SXin Li fprintf(stderr, "Error during lossless encoding.\n");
338*b2055c35SXin Li goto End;
339*b2055c35SXin Li }
340*b2055c35SXin Li ret = ImgIoUtilWriteFile(output, data, data_size) ? 0 : 1;
341*b2055c35SXin Li WebPFree(data);
342*b2055c35SXin Li if (ret) goto End;
343*b2055c35SXin Li #else
344*b2055c35SXin Li (void)data;
345*b2055c35SXin Li (void)data_size;
346*b2055c35SXin Li fprintf(stderr, "Cannot save the difference map. Please recompile "
347*b2055c35SXin Li "without the WEBP_REDUCE_CSP flag.\n");
348*b2055c35SXin Li #endif // WEBP_REDUCE_CSP
349*b2055c35SXin Li }
350*b2055c35SXin Li ret = 0;
351*b2055c35SXin Li
352*b2055c35SXin Li End:
353*b2055c35SXin Li WebPPictureFree(&pic1);
354*b2055c35SXin Li WebPPictureFree(&pic2);
355*b2055c35SXin Li FREE_WARGV_AND_RETURN(ret);
356*b2055c35SXin Li }
357