Coverage Report

Created: 2025-07-01 06:35

/src/libwebp/imageio/image_dec.c
Line
Count
Source (jump to first uncovered line)
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 <stddef.h>
13
14
#include "./image_dec.h"
15
#include "./metadata.h"
16
#include "webp/encode.h"
17
#include "webp/types.h"
18
19
0
const char* WebPGetEnabledInputFileFormats(void) {
20
0
  return "WebP"
21
0
#ifdef WEBP_HAVE_JPEG
22
0
         ", JPEG"
23
0
#endif
24
0
#ifdef WEBP_HAVE_PNG
25
0
         ", PNG"
26
0
#endif
27
0
         ", PNM (PGM, PPM, PAM)"
28
#ifdef WEBP_HAVE_TIFF
29
         ", TIFF"
30
#endif
31
#ifdef HAVE_WINCODEC_H
32
         ", Windows Imaging Component (WIC)"
33
#endif
34
0
         "";
35
0
}
36
37
0
static WEBP_INLINE uint32_t GetBE32(const uint8_t buf[]) {
38
0
  return ((uint32_t)buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
39
0
}
40
41
WebPInputFileFormat WebPGuessImageType(const uint8_t* const data,
42
0
                                       size_t data_size) {
43
0
  WebPInputFileFormat format = WEBP_UNSUPPORTED_FORMAT;
44
0
  if (data != NULL && data_size >= 12) {
45
0
    const uint32_t magic1 = GetBE32(data + 0);
46
0
    const uint32_t magic2 = GetBE32(data + 8);
47
0
    if (magic1 == 0x89504E47U) {
48
0
      format = WEBP_PNG_FORMAT;
49
0
    } else if (magic1 >= 0xFFD8FF00U && magic1 <= 0xFFD8FFFFU) {
50
0
      format = WEBP_JPEG_FORMAT;
51
0
    } else if (magic1 == 0x49492A00 || magic1 == 0x4D4D002A) {
52
0
      format = WEBP_TIFF_FORMAT;
53
0
    } else if (magic1 == 0x52494646 && magic2 == 0x57454250) {
54
0
      format = WEBP_WEBP_FORMAT;
55
0
    } else if (((magic1 >> 24) & 0xff) == 'P') {
56
0
      const int type = (magic1 >> 16) & 0xff;
57
      // we only support 'P5 -> P7' for now.
58
0
      if (type >= '5' && type <= '7') format = WEBP_PNM_FORMAT;
59
0
    }
60
0
  }
61
0
  return format;
62
0
}
63
64
static int FailReader(const uint8_t* const data, size_t data_size,
65
                      struct WebPPicture* const pic,
66
0
                      int keep_alpha, struct Metadata* const metadata) {
67
0
  (void)data;
68
0
  (void)data_size;
69
0
  (void)pic;
70
0
  (void)keep_alpha;
71
0
  (void)metadata;
72
0
  return 0;
73
0
}
74
75
0
WebPImageReader WebPGetImageReader(WebPInputFileFormat format) {
76
0
  switch (format) {
77
0
    case WEBP_PNG_FORMAT: return ReadPNG;
78
0
    case WEBP_JPEG_FORMAT: return ReadJPEG;
79
0
    case WEBP_TIFF_FORMAT: return ReadTIFF;
80
0
    case WEBP_WEBP_FORMAT: return ReadWebP;
81
0
    case WEBP_PNM_FORMAT: return ReadPNM;
82
0
    default: return FailReader;
83
0
  }
84
0
}
85
86
WebPImageReader WebPGuessImageReader(const uint8_t* const data,
87
0
                                     size_t data_size) {
88
0
  return WebPGetImageReader(WebPGuessImageType(data, data_size));
89
0
}