xref: /aosp_15_r20/external/webp/extras/webp_quality.c (revision b2055c353e87c8814eb2b6b1b11112a1562253bd)
1*b2055c35SXin Li // Simple tool to roughly evaluate the quality encoding of a webp bitstream
2*b2055c35SXin Li //
3*b2055c35SXin Li // Result is a *rough* estimation of the quality. You should just consider
4*b2055c35SXin Li // the bucket it's in (q > 80? > 50? > 20?) and not take it for face value.
5*b2055c35SXin Li /*
6*b2055c35SXin Li  gcc -o webp_quality webp_quality.c -O3 -I../ -L. -L../imageio \
7*b2055c35SXin Li     -limageio_util -lwebpextras -lwebp -lm -lpthread
8*b2055c35SXin Li */
9*b2055c35SXin Li 
10*b2055c35SXin Li #include <stdio.h>
11*b2055c35SXin Li #include <stdlib.h>
12*b2055c35SXin Li #include <string.h>
13*b2055c35SXin Li 
14*b2055c35SXin Li #include "extras/extras.h"
15*b2055c35SXin Li #include "imageio/imageio_util.h"
16*b2055c35SXin Li #include "../examples/unicode.h"
17*b2055c35SXin Li 
main(int argc,const char * argv[])18*b2055c35SXin Li int main(int argc, const char* argv[]) {
19*b2055c35SXin Li   int c;
20*b2055c35SXin Li   int quiet = 0;
21*b2055c35SXin Li   int ok = 1;
22*b2055c35SXin Li 
23*b2055c35SXin Li   INIT_WARGV(argc, argv);
24*b2055c35SXin Li 
25*b2055c35SXin Li   for (c = 1; ok && c < argc; ++c) {
26*b2055c35SXin Li     if (!strcmp(argv[c], "-quiet")) {
27*b2055c35SXin Li       quiet = 1;
28*b2055c35SXin Li     } else if (!strcmp(argv[c], "-help") || !strcmp(argv[c], "-h")) {
29*b2055c35SXin Li       printf("webp_quality [-h][-quiet] webp_files...\n");
30*b2055c35SXin Li       FREE_WARGV_AND_RETURN(0);
31*b2055c35SXin Li     } else {
32*b2055c35SXin Li       const char* const filename = (const char*)GET_WARGV(argv, c);
33*b2055c35SXin Li       const uint8_t* data = NULL;
34*b2055c35SXin Li       size_t data_size = 0;
35*b2055c35SXin Li       int q;
36*b2055c35SXin Li       ok = ImgIoUtilReadFile(filename, &data, &data_size);
37*b2055c35SXin Li       if (!ok) break;
38*b2055c35SXin Li       q = VP8EstimateQuality(data, data_size);
39*b2055c35SXin Li       if (!quiet) WPRINTF("[%s] ", (const W_CHAR*)filename);
40*b2055c35SXin Li       if (q < 0) {
41*b2055c35SXin Li         fprintf(stderr, "Not a WebP file, or not a lossy WebP file.\n");
42*b2055c35SXin Li         ok = 0;
43*b2055c35SXin Li       } else {
44*b2055c35SXin Li         if (!quiet) {
45*b2055c35SXin Li           printf("Estimated quality factor: %d\n", q);
46*b2055c35SXin Li         } else {
47*b2055c35SXin Li           printf("%d\n", q);   // just print the number
48*b2055c35SXin Li         }
49*b2055c35SXin Li       }
50*b2055c35SXin Li       free((void*)data);
51*b2055c35SXin Li     }
52*b2055c35SXin Li   }
53*b2055c35SXin Li   FREE_WARGV_AND_RETURN(ok ? 0 : 1);
54*b2055c35SXin Li }
55