Coverage Report

Created: 2026-02-14 07:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebp/imageio/image_dec.c
Line
Count
Source
1
// Copyright 2016 Google Inc. All Rights Reserved.
2
//
3
// Use of this source code is governed by a BSD-style license
4
// that can be found in the COPYING file in the root of the source
5
// tree. An additional intellectual property rights grant can be found
6
// in the file PATENTS. All contributing project authors may
7
// be found in the AUTHORS file in the root of the source tree.
8
// -----------------------------------------------------------------------------
9
//
10
// Generic image-type guessing.
11
12
#include "./image_dec.h"
13
14
#include <stddef.h>
15
16
#include "./metadata.h"
17
#include "webp/encode.h"
18
#include "webp/types.h"
19
20
0
const char* WebPGetEnabledInputFileFormats(void) {
21
0
  return "WebP"
22
0
#ifdef WEBP_HAVE_JPEG
23
0
         ", JPEG"
24
0
#endif
25
0
#ifdef WEBP_HAVE_PNG
26
0
         ", PNG"
27
0
#endif
28
0
         ", PNM (PGM, PPM, PAM)"
29
#ifdef WEBP_HAVE_TIFF
30
         ", TIFF"
31
#endif
32
#ifdef HAVE_WINCODEC_H
33
         ", Windows Imaging Component (WIC)"
34
#endif
35
0
         "";
36
0
}
37
38
4.84k
static WEBP_INLINE uint32_t GetBE32(const uint8_t buf[]) {
39
4.84k
  return ((uint32_t)buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
40
4.84k
}
41
42
WebPInputFileFormat WebPGuessImageType(const uint8_t* const data,
43
2.47k
                                       size_t data_size) {
44
2.47k
  WebPInputFileFormat format = WEBP_UNSUPPORTED_FORMAT;
45
2.47k
  if (data != NULL && data_size >= 12) {
46
2.42k
    const uint32_t magic1 = GetBE32(data + 0);
47
2.42k
    const uint32_t magic2 = GetBE32(data + 8);
48
2.42k
    if (magic1 == 0x89504E47U) {
49
16
      format = WEBP_PNG_FORMAT;
50
2.40k
    } else if (magic1 >= 0xFFD8FF00U && magic1 <= 0xFFD8FFFFU) {
51
12
      format = WEBP_JPEG_FORMAT;
52
2.39k
    } else if (magic1 == 0x49492A00 || magic1 == 0x4D4D002A) {
53
8
      format = WEBP_TIFF_FORMAT;
54
2.38k
    } else if (magic1 == 0x52494646 && magic2 == 0x57454250) {
55
40
      format = WEBP_WEBP_FORMAT;
56
2.34k
    } else if (((magic1 >> 24) & 0xff) == 'P') {
57
1.26k
      const int type = (magic1 >> 16) & 0xff;
58
      // we only support 'P5 -> P7' for now.
59
1.26k
      if (type >= '5' && type <= '7') format = WEBP_PNM_FORMAT;
60
1.26k
    }
61
2.42k
  }
62
2.47k
  return format;
63
2.47k
}
64
65
static int FailReader(const uint8_t* const data, size_t data_size,
66
                      struct WebPPicture* const pic, int keep_alpha,
67
573
                      struct Metadata* const metadata) {
68
573
  (void)data;
69
573
  (void)data_size;
70
573
  (void)pic;
71
573
  (void)keep_alpha;
72
573
  (void)metadata;
73
573
  return 0;
74
573
}
75
76
10.3k
WebPImageReader WebPGetImageReader(WebPInputFileFormat format) {
77
10.3k
  switch (format) {
78
259
    case WEBP_PNG_FORMAT:
79
259
      return ReadPNG;
80
206
    case WEBP_JPEG_FORMAT:
81
206
      return ReadJPEG;
82
113
    case WEBP_TIFF_FORMAT:
83
113
      return ReadTIFF;
84
6.90k
    case WEBP_WEBP_FORMAT:
85
6.90k
      return ReadWebP;
86
2.30k
    case WEBP_PNM_FORMAT:
87
2.30k
      return ReadPNM;
88
590
    default:
89
590
      return FailReader;
90
10.3k
  }
91
10.3k
}
92
93
WebPImageReader WebPGuessImageReader(const uint8_t* const data,
94
1.23k
                                     size_t data_size) {
95
1.23k
  return WebPGetImageReader(WebPGuessImageType(data, data_size));
96
1.23k
}