2014-08-27 20:00:54 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SkDocument.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkImageGenerator.h"
|
|
|
|
#include "SkData.h"
|
|
|
|
#include "SkStream.h"
|
|
|
|
|
|
|
|
#include "Resources.h"
|
|
|
|
#include "Test.h"
|
|
|
|
|
|
|
|
// Returned bitmap is lazy. Only lazy bitmaps hold onto the original data.
|
|
|
|
static SkBitmap bitmap_from_data(SkData* data) {
|
|
|
|
SkASSERT(data);
|
|
|
|
SkBitmap bm;
|
2015-09-25 13:56:57 +00:00
|
|
|
SkDEPRECATED_InstallDiscardablePixelRef(data, &bm);
|
2014-08-27 20:00:54 +00:00
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_subset_of(SkData* smaller, SkData* larger) {
|
|
|
|
SkASSERT(smaller && larger);
|
|
|
|
if (smaller->size() > larger->size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
size_t size = smaller->size();
|
|
|
|
size_t size_diff = larger->size() - size;
|
|
|
|
for (size_t i = 0; i <= size_diff; ++i) {
|
|
|
|
if (0 == memcmp(larger->bytes() + i, smaller->bytes(), size)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-02 21:40:46 +00:00
|
|
|
static sk_sp<SkData> load_resource(
|
2014-08-27 20:00:54 +00:00
|
|
|
skiatest::Reporter* r, const char* test, const char* filename) {
|
|
|
|
SkString path(GetResourcePath(filename));
|
2016-08-02 21:40:46 +00:00
|
|
|
sk_sp<SkData> data(SkData::MakeFromFileName(path.c_str()));
|
2016-02-25 01:59:16 +00:00
|
|
|
if (!data) {
|
|
|
|
INFOF(r, "\n%s: Resource '%s' can not be found.\n",
|
|
|
|
test, filename);
|
2014-08-27 20:00:54 +00:00
|
|
|
}
|
2015-08-27 14:41:13 +00:00
|
|
|
return data; // May return nullptr.
|
2014-08-27 20:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that for Jpeg files that use the JFIF colorspace, they are
|
|
|
|
* directly embedded into the PDF (without re-encoding) when that
|
|
|
|
* makes sense.
|
|
|
|
*/
|
|
|
|
DEF_TEST(PDFJpegEmbedTest, r) {
|
|
|
|
const char test[] = "PDFJpegEmbedTest";
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkData> mandrillData(load_resource(r, test, "mandrill_512_q075.jpg"));
|
|
|
|
sk_sp<SkData> cmykData(load_resource(r, test, "CMYK.jpg"));
|
2014-08-27 20:00:54 +00:00
|
|
|
if (!mandrillData || !cmykData) {
|
|
|
|
return;
|
|
|
|
}
|
2015-10-01 14:28:13 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2014-08-27 20:00:54 +00:00
|
|
|
SkDynamicMemoryWStream pdf;
|
2016-04-27 14:45:18 +00:00
|
|
|
sk_sp<SkDocument> document(SkDocument::MakePDF(&pdf));
|
2014-08-27 20:00:54 +00:00
|
|
|
SkCanvas* canvas = document->beginPage(642, 1028);
|
|
|
|
|
|
|
|
canvas->clear(SK_ColorLTGRAY);
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
SkBitmap bm1(bitmap_from_data(mandrillData.get()));
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->drawBitmap(bm1, 65.0, 0.0, nullptr);
|
2016-03-17 17:51:11 +00:00
|
|
|
SkBitmap bm2(bitmap_from_data(cmykData.get()));
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->drawBitmap(bm2, 0.0, 512.0, nullptr);
|
2014-08-27 20:00:54 +00:00
|
|
|
|
|
|
|
canvas->flush();
|
|
|
|
document->endPage();
|
|
|
|
document->close();
|
2016-08-03 20:32:32 +00:00
|
|
|
sk_sp<SkData> pdfData(pdf.copyToData());
|
2014-08-27 20:00:54 +00:00
|
|
|
SkASSERT(pdfData);
|
|
|
|
pdf.reset();
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
REPORTER_ASSERT(r, is_subset_of(mandrillData.get(), pdfData.get()));
|
2014-08-27 20:00:54 +00:00
|
|
|
|
2015-10-01 14:28:13 +00:00
|
|
|
// This JPEG uses a nonstandard colorspace - it can not be
|
|
|
|
// embedded into the PDF directly.
|
2016-03-17 17:51:11 +00:00
|
|
|
REPORTER_ASSERT(r, !is_subset_of(cmykData.get(), pdfData.get()));
|
2015-10-01 14:28:13 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
pdf.reset();
|
2016-04-27 14:45:18 +00:00
|
|
|
document = SkDocument::MakePDF(&pdf);
|
2015-10-01 14:28:13 +00:00
|
|
|
canvas = document->beginPage(642, 1028);
|
|
|
|
|
|
|
|
canvas->clear(SK_ColorLTGRAY);
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> im1(SkImage::MakeFromEncoded(mandrillData));
|
|
|
|
canvas->drawImage(im1.get(), 65.0, 0.0, nullptr);
|
|
|
|
sk_sp<SkImage> im2(SkImage::MakeFromEncoded(cmykData));
|
|
|
|
canvas->drawImage(im2.get(), 0.0, 512.0, nullptr);
|
2015-10-01 14:28:13 +00:00
|
|
|
|
|
|
|
canvas->flush();
|
|
|
|
document->endPage();
|
|
|
|
document->close();
|
|
|
|
pdfData.reset(pdf.copyToData());
|
|
|
|
SkASSERT(pdfData);
|
|
|
|
pdf.reset();
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
REPORTER_ASSERT(r, is_subset_of(mandrillData.get(), pdfData.get()));
|
2015-10-01 14:28:13 +00:00
|
|
|
|
2014-08-27 20:00:54 +00:00
|
|
|
// This JPEG uses a nonstandard colorspace - it can not be
|
|
|
|
// embedded into the PDF directly.
|
2016-03-17 17:51:11 +00:00
|
|
|
REPORTER_ASSERT(r, !is_subset_of(cmykData.get(), pdfData.get()));
|
2014-08-27 20:00:54 +00:00
|
|
|
}
|
2015-05-07 18:46:59 +00:00
|
|
|
|
|
|
|
#include "SkJpegInfo.h"
|
|
|
|
|
|
|
|
DEF_TEST(JpegIdentification, r) {
|
|
|
|
static struct {
|
|
|
|
const char* path;
|
|
|
|
bool isJfif;
|
|
|
|
SkJFIFInfo::Type type;
|
|
|
|
} kTests[] = {{"CMYK.jpg", false, SkJFIFInfo::kGrayscale},
|
|
|
|
{"color_wheel.jpg", true, SkJFIFInfo::kYCbCr},
|
|
|
|
{"grayscale.jpg", true, SkJFIFInfo::kGrayscale},
|
|
|
|
{"mandrill_512_q075.jpg", true, SkJFIFInfo::kYCbCr},
|
|
|
|
{"randPixels.jpg", true, SkJFIFInfo::kYCbCr}};
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(kTests); ++i) {
|
2016-08-02 21:40:46 +00:00
|
|
|
sk_sp<SkData> data(load_resource(r, "JpegIdentification", kTests[i].path));
|
2015-05-07 18:46:59 +00:00
|
|
|
if (!data) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
SkJFIFInfo info;
|
2016-08-02 21:40:46 +00:00
|
|
|
bool isJfif = SkIsJFIF(data.get(), &info);
|
2015-05-07 18:46:59 +00:00
|
|
|
if (isJfif != kTests[i].isJfif) {
|
|
|
|
ERRORF(r, "%s failed isJfif test", kTests[i].path);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!isJfif) {
|
|
|
|
continue; // not applicable
|
|
|
|
}
|
|
|
|
if (kTests[i].type != info.fType) {
|
|
|
|
ERRORF(r, "%s failed jfif type test", kTests[i].path);
|
|
|
|
continue;
|
|
|
|
}
|
2016-02-25 01:59:16 +00:00
|
|
|
INFOF(r, "\nJpegIdentification: %s [%d x %d]\n", kTests[i].path,
|
|
|
|
info.fSize.width(), info.fSize.height());
|
2015-05-07 18:46:59 +00:00
|
|
|
}
|
|
|
|
}
|