Debian Codegraph @

examples/benchmark.cc

raw
1 /*
2 * HEIF codec.
3 * Copyright (c) 2022 Dirk Farin <[email protected]>
4 *
5 * This file is part of libheif.
6 *
7 * libheif is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * libheif is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with libheif. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "benchmark.h"
22 #include "libheif/heif.h"
23 #include <math.h>
24
25
26 double compute_psnr(heif_image* original_image, const std::string& encoded_file)
27 {
28 double psnr = 0.0;
29
30 // read encoded image
31
32 struct heif_context* ctx = nullptr;
33 struct heif_image_handle* handle = nullptr;
34 struct heif_image* image = nullptr;
35 heif_error err{};
36
37 int orig_stride = 0;
38 const uint8_t* orig_p = nullptr;
39 int compressed_stride = 0;
40 const uint8_t* compressed_p = nullptr;
41
42 int w = 0, h = 0;
43 double mse = 0.0;
44
45
46 if (heif_image_get_colorspace(original_image) != heif_colorspace_YCbCr &&
47 heif_image_get_colorspace(original_image) != heif_colorspace_monochrome) {
48 fprintf(stderr, "Benchmark can only be computed on YCbCr or monochrome images\n");
49 goto cleanup;
50 }
51
52
53 ctx = heif_context_alloc();
54
55 err = heif_context_read_from_file(ctx, encoded_file.c_str(), nullptr);
56 if (err.code) {
57 fprintf(stderr, "Error reading encoded file: %s\n", err.message);
58 goto cleanup;
59 }
60
61 err = heif_context_get_primary_image_handle(ctx, &handle);
62 if (err.code) {
63 fprintf(stderr, "Error getting primary image handle: %s\n", err.message);
64 goto cleanup;
65 }
66
67 err = heif_decode_image(handle, &image,
68 heif_image_get_colorspace(original_image),
69 heif_image_get_chroma_format(original_image),
70 nullptr);
71 if (err.code) {
72 fprintf(stderr, "Error decoding image: %s\n", err.message);
73 goto cleanup;
74 }
75
76 w = heif_image_get_width(original_image, heif_channel_Y);
77 h = heif_image_get_height(original_image, heif_channel_Y);
78
79 orig_p = heif_image_get_plane_readonly(original_image, heif_channel_Y, &orig_stride);
80 compressed_p = heif_image_get_plane_readonly(image, heif_channel_Y, &compressed_stride);
81
82 for (int y = 0; y < h; y++) {
83 for (int x = 0; x < w; x++) {
84 int d = orig_p[y * orig_stride + x] - compressed_p[y * compressed_stride + x];
85 mse += d * d;
86 }
87 }
88
89 mse /= w * h;
90
91 psnr = 10 * log10(255.0 * 255.0 / mse);
92
93 cleanup:
94 heif_image_release(image);
95 heif_image_handle_release(handle);
96 heif_context_free(ctx);
97
98 return psnr;
99 }