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