blob: ac05644032502e23cf2f6a146e9325ffebfc592c [file] [log] [blame]
Pascal Massiminoae2a7222016-05-30 21:45:38 -07001// 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
14static WEBP_INLINE uint32_t GetBE32(const uint8_t buf[]) {
15 return ((uint32_t)buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
16}
17
18WebPInputFileFormat WebPGuessImageType(const uint8_t* const data,
19 size_t data_size) {
20 WebPInputFileFormat format = WEBP_UNSUPPORTED_FORMAT;
21 if (data != NULL && data_size >= 12) {
22 const uint32_t magic1 = GetBE32(data + 0);
23 const uint32_t magic2 = GetBE32(data + 8);
24 if (magic1 == 0x89504E47U) {
25 format = WEBP_PNG_FORMAT;
26 } else if (magic1 >= 0xFFD8FF00U && magic1 <= 0xFFD8FFFFU) {
27 format = WEBP_JPEG_FORMAT;
28 } else if (magic1 == 0x49492A00 || magic1 == 0x4D4D002A) {
29 format = WEBP_TIFF_FORMAT;
30 } else if (magic1 == 0x52494646 && magic2 == 0x57454250) {
31 format = WEBP_WEBP_FORMAT;
32 }
33 }
34 return format;
35}
Pascal Massiminod77b8772016-06-01 20:55:29 +020036
37WebPImageReader WebPGuessImageReader(const uint8_t* const data,
38 size_t data_size) {
39 switch (WebPGuessImageType(data, data_size)) {
40 case WEBP_PNG_FORMAT: return ReadPNG;
41 case WEBP_JPEG_FORMAT: return ReadJPEG;
42 case WEBP_TIFF_FORMAT: return ReadTIFF;
43 case WEBP_WEBP_FORMAT: return ReadWebP;
44 default: return NULL;
45 }
46}