SkDocument::setDCTEncoder() for old versions of webkit
Review URL: https://codereview.chromium.org/1505763003
diff --git a/tests/PDFDocumentTest.cpp b/tests/PDFDocumentTest.cpp
index 04c3ede..c3a5f0e 100644
--- a/tests/PDFDocumentTest.cpp
+++ b/tests/PDFDocumentTest.cpp
@@ -6,10 +6,12 @@
*/
#include "Test.h"
+#include "Resources.h"
#include "SkCanvas.h"
#include "SkDocument.h"
#include "SkOSFile.h"
#include "SkStream.h"
+#include "SkPixelSerializer.h"
static void test_empty(skiatest::Reporter* reporter) {
SkDynamicMemoryWStream stream;
@@ -110,3 +112,45 @@
test_file(reporter);
test_close(reporter);
}
+
+namespace {
+class JPEGSerializer final : public SkPixelSerializer {
+ bool onUseEncodedData(const void*, size_t) override { return true; }
+ SkData* onEncode(const SkPixmap& pixmap) override {
+ SkBitmap bm;
+ return bm.installPixels(pixmap.info(),
+ pixmap.writable_addr(),
+ pixmap.rowBytes(),
+ pixmap.ctable(),
+ nullptr, nullptr)
+ ? SkImageEncoder::EncodeData(bm, SkImageEncoder::kJPEG_Type, 85)
+ : nullptr;
+ }
+};
+} // namespace
+
+size_t count_bytes(const SkBitmap& bm, bool useDCT) {
+ SkDynamicMemoryWStream stream;
+ SkAutoTUnref<SkDocument> doc;
+ if (useDCT) {
+ SkAutoTUnref<SkPixelSerializer> serializer(new JPEGSerializer);
+ doc.reset(SkDocument::CreatePDF(
+ &stream, SK_ScalarDefaultRasterDPI, serializer));
+ } else {
+ doc.reset(SkDocument::CreatePDF(&stream));
+ }
+ SkCanvas* canvas = doc->beginPage(64, 64);
+ canvas->drawBitmap(bm, 0, 0);
+ doc->endPage();
+ doc->close();
+ return stream.bytesWritten();
+}
+
+DEF_TEST(document_dct_encoder, r) {
+ REQUIRE_PDF_DOCUMENT(document_dct_encoder, r);
+ SkBitmap bm;
+ if (GetResourceAsBitmap("mandrill_64.png", &bm)) {
+ // Lossy encoding works better on photographs.
+ REPORTER_ASSERT(r, count_bytes(bm, true) < count_bytes(bm, false));
+ }
+}