summaryrefslogtreecommitdiff
path: root/image
diff options
context:
space:
mode:
authorwolfbeast <mcwerewolf@gmail.com>2017-07-24 13:31:07 +0200
committerwolfbeast <mcwerewolf@gmail.com>2018-02-03 10:05:56 +0100
commit9371852bb9f01c3263bb718655fa616c94061979 (patch)
treee883818cae40f0d53b458d1cd60ea659a69f958a /image
parent3e0351aa49be56434df121d9b4d9f283c422a670 (diff)
downloaduxp-9371852bb9f01c3263bb718655fa616c94061979.tar.gz
Add WebP content sniffing code.
Diffstat (limited to 'image')
-rw-r--r--image/imgLoader.cpp5
-rw-r--r--image/test/gtest/TestLoader.cpp84
-rw-r--r--image/test/gtest/moz.build1
3 files changed, 90 insertions, 0 deletions
diff --git a/image/imgLoader.cpp b/image/imgLoader.cpp
index 3545c5be27..5e5ee78296 100644
--- a/image/imgLoader.cpp
+++ b/image/imgLoader.cpp
@@ -2558,6 +2558,11 @@ imgLoader::GetMimeTypeFromContent(const char* aContents,
!memcmp(aContents, "\000\000\002\000", 4))) {
aContentType.AssignLiteral(IMAGE_ICO);
+ // WebPs always begin with RIFF, a 32-bit length, and WEBP.
+ } else if (aLength >= 12 && !memcmp(aContents, "RIFF", 4) &&
+ !memcmp(aContents + 8, "WEBP", 4)) {
+ aContentType.AssignLiteral(IMAGE_WEBP);
+
} else {
/* none of the above? I give up */
return NS_ERROR_NOT_AVAILABLE;
diff --git a/image/test/gtest/TestLoader.cpp b/image/test/gtest/TestLoader.cpp
new file mode 100644
index 0000000000..5551f3f053
--- /dev/null
+++ b/image/test/gtest/TestLoader.cpp
@@ -0,0 +1,84 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "gtest/gtest.h"
+
+#include "Common.h"
+#include "imgLoader.h"
+#include "nsMimeTypes.h"
+#include "nsString.h"
+
+using namespace mozilla;
+using namespace mozilla::image;
+
+static void
+CheckMimeType(const char* aContents, size_t aLength, const char* aExpected)
+{
+ nsAutoCString detected;
+ nsresult rv = imgLoader::GetMimeTypeFromContent(aContents, aLength, detected);
+ if (aExpected) {
+ ASSERT_TRUE(NS_SUCCEEDED(rv));
+ EXPECT_TRUE(detected.EqualsASCII(aExpected));
+ } else {
+ ASSERT_TRUE(NS_FAILED(rv));
+ EXPECT_TRUE(detected.IsEmpty());
+ }
+}
+
+class ImageLoader : public ::testing::Test
+{
+protected:
+ AutoInitializeImageLib mInit;
+};
+
+TEST_F(ImageLoader, DetectGIF)
+{
+ const char buffer[] = "GIF87a";
+ CheckMimeType(buffer, sizeof(buffer), IMAGE_GIF);
+}
+
+TEST_F(ImageLoader, DetectPNG)
+{
+ const char buffer[] = "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A";
+ CheckMimeType(buffer, sizeof(buffer), IMAGE_PNG);
+}
+
+TEST_F(ImageLoader, DetectJPEG)
+{
+ const char buffer[] = "\xFF\xD8\xFF";
+ CheckMimeType(buffer, sizeof(buffer), IMAGE_JPEG);
+}
+
+TEST_F(ImageLoader, DetectART)
+{
+ const char buffer[] = "\x4A\x47\xFF\xFF\x00";
+ CheckMimeType(buffer, sizeof(buffer), IMAGE_ART);
+}
+
+TEST_F(ImageLoader, DetectBMP)
+{
+ const char buffer[] = "BM";
+ CheckMimeType(buffer, sizeof(buffer), IMAGE_BMP);
+}
+
+TEST_F(ImageLoader, DetectICO)
+{
+ const char buffer[] = "\x00\x00\x01\x00";
+ CheckMimeType(buffer, sizeof(buffer), IMAGE_ICO);
+}
+
+TEST_F(ImageLoader, DetectWebP)
+{
+ const char buffer[] = "RIFF\xFF\xFF\xFF\xFFWEBPVP8L";
+ CheckMimeType(buffer, sizeof(buffer), IMAGE_WEBP);
+}
+
+TEST_F(ImageLoader, DetectNone)
+{
+ const char buffer[] = "abcdefghijklmnop";
+ CheckMimeType(buffer, sizeof(buffer), nullptr);
+}
+
diff --git a/image/test/gtest/moz.build b/image/test/gtest/moz.build
index 5cf6d5116f..3548eaecc0 100644
--- a/image/test/gtest/moz.build
+++ b/image/test/gtest/moz.build
@@ -13,6 +13,7 @@ UNIFIED_SOURCES = [
'TestDecoders.cpp',
'TestDecodeToSurface.cpp',
'TestDeinterlacingFilter.cpp',
+ 'TestLoader.cpp',
'TestMetadata.cpp',
'TestRemoveFrameRectFilter.cpp',
'TestSourceBuffer.cpp',