1/* 2 * Copyright 2014 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8#include "SkCanvas.h" 9#include "SkData.h" 10#include "SkDocument.h" 11#include "SkImageGenerator.h" 12#include "SkStream.h" 13 14#include "Resources.h" 15#include "Test.h" 16 17static bool is_subset_of(SkData* smaller, SkData* larger) { 18 SkASSERT(smaller && larger); 19 if (smaller->size() > larger->size()) { 20 return false; 21 } 22 size_t size = smaller->size(); 23 size_t size_diff = larger->size() - size; 24 for (size_t i = 0; i <= size_diff; ++i) { 25 if (0 == memcmp(larger->bytes() + i, smaller->bytes(), size)) { 26 return true; 27 } 28 } 29 return false; 30} 31 32 33static sk_sp<SkData> load_resource( 34 skiatest::Reporter* r, const char* test, const char* filename) { 35 SkString path(GetResourcePath(filename)); 36 sk_sp<SkData> data(SkData::MakeFromFileName(path.c_str())); 37 if (!data) { 38 INFOF(r, "\n%s: Resource '%s' can not be found.\n", 39 test, filename); 40 } 41 return data; // May return nullptr. 42} 43 44/** 45 * Test that for Jpeg files that use the JFIF colorspace, they are 46 * directly embedded into the PDF (without re-encoding) when that 47 * makes sense. 48 */ 49DEF_TEST(SkPDF_JpegEmbedTest, r) { 50 REQUIRE_PDF_DOCUMENT(SkPDF_JpegEmbedTest, r); 51 const char test[] = "SkPDF_JpegEmbedTest"; 52 sk_sp<SkData> mandrillData(load_resource(r, test, "mandrill_512_q075.jpg")); 53 sk_sp<SkData> cmykData(load_resource(r, test, "CMYK.jpg")); 54 if (!mandrillData || !cmykData) { 55 return; 56 } 57 //////////////////////////////////////////////////////////////////////////// 58 SkDynamicMemoryWStream pdf; 59 sk_sp<SkDocument> document(SkDocument::MakePDF(&pdf)); 60 SkCanvas* canvas = document->beginPage(642, 1028); 61 62 canvas->clear(SK_ColorLTGRAY); 63 64 sk_sp<SkImage> im1(SkImage::MakeFromEncoded(mandrillData)); 65 canvas->drawImage(im1.get(), 65.0, 0.0, nullptr); 66 sk_sp<SkImage> im2(SkImage::MakeFromEncoded(cmykData)); 67 canvas->drawImage(im2.get(), 0.0, 512.0, nullptr); 68 69 canvas->flush(); 70 document->endPage(); 71 document->close(); 72 sk_sp<SkData> pdfData = pdf.detachAsData(); 73 SkASSERT(pdfData); 74 75 REPORTER_ASSERT(r, is_subset_of(mandrillData.get(), pdfData.get())); 76 77 // This JPEG uses a nonstandard colorspace - it can not be 78 // embedded into the PDF directly. 79 REPORTER_ASSERT(r, !is_subset_of(cmykData.get(), pdfData.get())); 80} 81 82#ifdef SK_SUPPORT_PDF 83 84#include "SkJpegInfo.h" 85 86DEF_TEST(SkPDF_JpegIdentification, r) { 87 static struct { 88 const char* path; 89 bool isJfif; 90 SkJFIFInfo::Type type; 91 } kTests[] = {{"CMYK.jpg", false, SkJFIFInfo::kGrayscale}, 92 {"color_wheel.jpg", true, SkJFIFInfo::kYCbCr}, 93 {"grayscale.jpg", true, SkJFIFInfo::kGrayscale}, 94 {"mandrill_512_q075.jpg", true, SkJFIFInfo::kYCbCr}, 95 {"randPixels.jpg", true, SkJFIFInfo::kYCbCr}}; 96 for (size_t i = 0; i < SK_ARRAY_COUNT(kTests); ++i) { 97 sk_sp<SkData> data(load_resource(r, "JpegIdentification", kTests[i].path)); 98 if (!data) { 99 continue; 100 } 101 SkJFIFInfo info; 102 bool isJfif = SkIsJFIF(data.get(), &info); 103 if (isJfif != kTests[i].isJfif) { 104 ERRORF(r, "%s failed isJfif test", kTests[i].path); 105 continue; 106 } 107 if (!isJfif) { 108 continue; // not applicable 109 } 110 if (kTests[i].type != info.fType) { 111 ERRORF(r, "%s failed jfif type test", kTests[i].path); 112 continue; 113 } 114 INFOF(r, "\nJpegIdentification: %s [%d x %d]\n", kTests[i].path, 115 info.fSize.width(), info.fSize.height()); 116 } 117 118 // Test several malformed jpegs. 119 SkJFIFInfo info; 120 { 121 static const char goodJpeg[] = 122 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7" 123 "\6\5\10\7\7\7\t\t\10\n\14\24\r\14\13\13\14\31\22\23\17\24\35\32\37" 124 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t" 125 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222" 126 "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001"; 127 size_t goodJpegLength = 177; 128 auto data = SkData::MakeWithoutCopy(goodJpeg, goodJpegLength); 129 REPORTER_ASSERT(r, SkIsJFIF(data.get(), &info)); 130 REPORTER_ASSERT(r, info.fSize == SkISize::Make(512, 512)); 131 REPORTER_ASSERT(r, info.fType == SkJFIFInfo::kYCbCr); 132 133 // Not long enough to read first (SOI) segment marker. 134 data = SkData::MakeWithoutCopy(goodJpeg, 1); 135 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); 136 137 // Not long enough to read second segment (APP0) marker. 138 data = SkData::MakeWithoutCopy(goodJpeg, 3); 139 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); 140 141 // Not long enough to read second segment's length. 142 data = SkData::MakeWithoutCopy(goodJpeg, 5); 143 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); 144 145 // APP0 segment is truncated. 146 data = SkData::MakeWithoutCopy(goodJpeg, 7); 147 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); 148 149 // Missing SOF segment. 150 data = SkData::MakeWithoutCopy(goodJpeg, 89); 151 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); 152 } 153 { 154 // JFIF tag missing. 155 static const char jpeg[] = 156 "\377\330\377\340\0\20JFIX\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7" 157 "\6\5\10\7\7\7\t\t\10\n\14\24\r\14\13\13\14\31\22\23\17\24\35\32\37" 158 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t" 159 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222" 160 "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001"; 161 size_t jpegLength = 177; 162 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength); 163 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); 164 } 165 { 166 // APP0 segment short (byte 6 changed). 167 static const char jpeg[] = 168 "\377\330\377\340\0\5JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7" 169 "\6\5\10\7\7\7\t\t\10\n\14\24\r\14\13\13\14\31\22\23\17\24\35\32\37" 170 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t" 171 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222" 172 "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001"; 173 size_t jpegLength = 177; 174 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength); 175 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); 176 } 177 { 178 // SOF segment short. ('\21' replaced with '\5') 179 static const char jpeg[] = 180 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7" 181 "\6\5\10\7\7\7\t\t\10\n\14\24\r\14\13\13\14\31\22\23\17\24\35\32\37" 182 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t" 183 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222" 184 "22222222222222\377\300\0\5\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001"; 185 size_t jpegLength = 177; 186 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength); 187 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); 188 } 189 { 190 // Unsupported 12-bit components. ('\10' replaced with '\14') 191 static const char jpeg[] = 192 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7" 193 "\6\5\10\7\7\7\t\t\10\n\14\24\r\14\13\13\14\31\22\23\17\24\35\32\37" 194 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t" 195 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222" 196 "22222222222222\377\300\0\21\14\2\0\2\0\3\1\"\0\2\21\1\3\21\001"; 197 size_t jpegLength = 177; 198 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength); 199 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); 200 } 201 { 202 // Two color channels. ('\3' replaced with '\2') 203 static const char jpeg[] = 204 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7" 205 "\6\5\10\7\7\7\t\t\10\n\14\24\r\14\13\13\14\31\22\23\17\24\35\32\37" 206 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t" 207 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222" 208 "22222222222222\377\300\0\21\10\2\0\2\0\2\1\"\0\2\21\1\3\21\001"; 209 size_t jpegLength = 177; 210 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength); 211 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); 212 } 213} 214#endif 215