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