Home | History | Annotate | Download | only in tests
      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 "SkDocument.h"
      9 #include "SkCanvas.h"
     10 #include "SkImageGenerator.h"
     11 #include "SkData.h"
     12 #include "SkStream.h"
     13 
     14 #include "Resources.h"
     15 #include "Test.h"
     16 
     17 // Returned bitmap is lazy.  Only lazy bitmaps hold onto the original data.
     18 static SkBitmap bitmap_from_data(SkData* data) {
     19     SkASSERT(data);
     20     SkBitmap bm;
     21     SkDEPRECATED_InstallDiscardablePixelRef(data, &bm);
     22     return bm;
     23 }
     24 
     25 static bool is_subset_of(SkData* smaller, SkData* larger) {
     26     SkASSERT(smaller && larger);
     27     if (smaller->size() > larger->size()) {
     28         return false;
     29     }
     30     size_t size = smaller->size();
     31     size_t size_diff = larger->size() - size;
     32     for (size_t i = 0; i <= size_diff; ++i) {
     33         if (0 == memcmp(larger->bytes() + i, smaller->bytes(), size)) {
     34             return true;
     35         }
     36     }
     37     return false;
     38 }
     39 
     40 
     41 static SkData* load_resource(
     42         skiatest::Reporter* r, const char* test, const char* filename) {
     43     SkString path(GetResourcePath(filename));
     44     SkData* data = SkData::NewFromFileName(path.c_str());
     45     if (!data) {
     46         INFOF(r, "\n%s: Resource '%s' can not be found.\n",
     47               test, filename);
     48     }
     49     return data;  // May return nullptr.
     50 }
     51 
     52 /**
     53  *  Test that for Jpeg files that use the JFIF colorspace, they are
     54  *  directly embedded into the PDF (without re-encoding) when that
     55  *  makes sense.
     56  */
     57 DEF_TEST(PDFJpegEmbedTest, r) {
     58     const char test[] = "PDFJpegEmbedTest";
     59     SkAutoTUnref<SkData> mandrillData(
     60             load_resource(r, test, "mandrill_512_q075.jpg"));
     61     SkAutoTUnref<SkData> cmykData(load_resource(r, test, "CMYK.jpg"));
     62     if (!mandrillData || !cmykData) {
     63         return;
     64     }
     65     ////////////////////////////////////////////////////////////////////////////
     66     SkDynamicMemoryWStream pdf;
     67     SkAutoTUnref<SkDocument> document(SkDocument::CreatePDF(&pdf));
     68     SkCanvas* canvas = document->beginPage(642, 1028);
     69 
     70     canvas->clear(SK_ColorLTGRAY);
     71 
     72     SkBitmap bm1(bitmap_from_data(mandrillData));
     73     canvas->drawBitmap(bm1, 65.0, 0.0, nullptr);
     74     SkBitmap bm2(bitmap_from_data(cmykData));
     75     canvas->drawBitmap(bm2, 0.0, 512.0, nullptr);
     76 
     77     canvas->flush();
     78     document->endPage();
     79     document->close();
     80     SkAutoTUnref<SkData> pdfData(pdf.copyToData());
     81     SkASSERT(pdfData);
     82     pdf.reset();
     83 
     84     REPORTER_ASSERT(r, is_subset_of(mandrillData, pdfData));
     85 
     86     // This JPEG uses a nonstandard colorspace - it can not be
     87     // embedded into the PDF directly.
     88     REPORTER_ASSERT(r, !is_subset_of(cmykData, pdfData));
     89     ////////////////////////////////////////////////////////////////////////////
     90     pdf.reset();
     91     document.reset(SkDocument::CreatePDF(&pdf));
     92     canvas = document->beginPage(642, 1028);
     93 
     94     canvas->clear(SK_ColorLTGRAY);
     95 
     96     SkAutoTUnref<SkImage> im1(SkImage::NewFromEncoded(mandrillData));
     97     canvas->drawImage(im1, 65.0, 0.0, nullptr);
     98     SkAutoTUnref<SkImage> im2(SkImage::NewFromEncoded(cmykData));
     99     canvas->drawImage(im2, 0.0, 512.0, nullptr);
    100 
    101     canvas->flush();
    102     document->endPage();
    103     document->close();
    104     pdfData.reset(pdf.copyToData());
    105     SkASSERT(pdfData);
    106     pdf.reset();
    107 
    108     REPORTER_ASSERT(r, is_subset_of(mandrillData, pdfData));
    109 
    110     // This JPEG uses a nonstandard colorspace - it can not be
    111     // embedded into the PDF directly.
    112     REPORTER_ASSERT(r, !is_subset_of(cmykData, pdfData));
    113 }
    114 
    115 #include "SkJpegInfo.h"
    116 
    117 DEF_TEST(JpegIdentification, r) {
    118     static struct {
    119         const char* path;
    120         bool isJfif;
    121         SkJFIFInfo::Type type;
    122     } kTests[] = {{"CMYK.jpg", false, SkJFIFInfo::kGrayscale},
    123                   {"color_wheel.jpg", true, SkJFIFInfo::kYCbCr},
    124                   {"grayscale.jpg", true, SkJFIFInfo::kGrayscale},
    125                   {"mandrill_512_q075.jpg", true, SkJFIFInfo::kYCbCr},
    126                   {"randPixels.jpg", true, SkJFIFInfo::kYCbCr}};
    127     for (size_t i = 0; i < SK_ARRAY_COUNT(kTests); ++i) {
    128         SkAutoTUnref<SkData> data(
    129                 load_resource(r, "JpegIdentification", kTests[i].path));
    130         if (!data) {
    131             continue;
    132         }
    133         SkJFIFInfo info;
    134         bool isJfif = SkIsJFIF(data, &info);
    135         if (isJfif != kTests[i].isJfif) {
    136             ERRORF(r, "%s failed isJfif test", kTests[i].path);
    137             continue;
    138         }
    139         if (!isJfif) {
    140             continue;  // not applicable
    141         }
    142         if (kTests[i].type != info.fType) {
    143             ERRORF(r, "%s failed jfif type test", kTests[i].path);
    144             continue;
    145         }
    146         INFOF(r, "\nJpegIdentification: %s [%d x %d]\n", kTests[i].path,
    147               info.fSize.width(), info.fSize.height());
    148     }
    149 }
    150