aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/pdf/SkPDFImageStream.cpp
blob: 7130f2b7cab3cbcebfb2ea620cd1b010b5dce78b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * Copyright 2013 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */


#include "SkData.h"
#include "SkFlate.h"
#include "SkPDFCatalog.h"
#include "SkPDFImageStream.h"
#include "SkStream.h"

#define kNoColorTransform 0

static bool skip_compression(SkPDFCatalog* catalog) {
    return SkToBool(catalog->getDocumentFlags() &
                    SkPDFDocument::kFavorSpeedOverSize_Flags);
}

// TODO(edisonn): Use SkData (after removing deprecated constructor in SkPDFStream).
SkPDFImageStream::SkPDFImageStream(SkStream* stream,
                                   const SkBitmap& bitmap,
                                   const SkIRect& srcRect,
                                   EncodeToDCTStream encoder)
    : SkPDFStream(stream),
      fBitmap(bitmap),
      fSrcRect(srcRect),
      fEncoder(encoder) {
}

SkPDFImageStream::SkPDFImageStream(const SkPDFImageStream& pdfStream)
        : SkPDFStream(pdfStream),
          fBitmap(pdfStream.fBitmap),
          fSrcRect(pdfStream.fSrcRect),
          fEncoder(pdfStream.fEncoder) {
}

SkPDFImageStream::~SkPDFImageStream() {}

bool SkPDFImageStream::populate(SkPDFCatalog* catalog) {
    if (getState() == kUnused_State) {
        if (!skip_compression(catalog)) {
            SkDynamicMemoryWStream dctCompressedWStream;
            if (!fEncoder || !fEncoder(&dctCompressedWStream, fBitmap, fSrcRect)) {
                return INHERITED::populate(catalog);
            }

            if (dctCompressedWStream.getOffset() < getData()->getLength()) {
                SkData* data = dctCompressedWStream.copyToData();
                SkMemoryStream* stream = SkNEW_ARGS(SkMemoryStream, (data));
                setData(stream);
                stream->unref();
                if (data) {
                    // copyToData and new SkMemoryStream both call ref(), supress one.
                    data->unref();
                }

                insertName("Filter", "DCTDecode");
                insertInt("ColorTransform", kNoColorTransform);
                setState(kCompressed_State);
            }
        }
        setState(kNoCompression_State);
        insertInt("Length", getData()->getLength());
    } else if (getState() == kNoCompression_State && !skip_compression(catalog) &&
               (SkFlate::HaveFlate() || fEncoder)) {
        // Compression has not been requested when the stream was first created.
        // But a new Catalog would want it compressed.
        if (!getSubstitute()) {
            SkPDFImageStream* substitute = SkNEW_ARGS(SkPDFImageStream, (*this));
            setSubstitute(substitute);
            catalog->setSubstitute(this, substitute);
        }
        return false;
    }
    return true;
}