1
2/*
3 * Copyright 2010 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#ifndef SkPDFImage_DEFINED
11#define SkPDFImage_DEFINED
12
13#include "SkPicture.h"
14#include "SkPDFDevice.h"
15#include "SkPDFStream.h"
16#include "SkPDFTypes.h"
17#include "SkRefCnt.h"
18
19class SkBitmap;
20class SkData;
21class SkPDFCatalog;
22struct SkIRect;
23
24/**
25 *  Return the mose efficient availible encoding of the given bitmap.
26 *
27 *  If the bitmap has encoded JPEG data and that data can be embedded
28 *  into the PDF output stream directly, use that.  Otherwise, fall
29 *  back on SkPDFImage::CreateImage.
30 */
31SkPDFObject* SkPDFCreateImageObject(
32        const SkBitmap&, const SkIRect& subset, SkPicture::EncodeBitmap);
33
34/** \class SkPDFImage
35
36    An image XObject.
37*/
38
39// We could play the same trick here as is done in SkPDFGraphicState, storing
40// a copy of the Bitmap object (not the pixels), the pixel generation number,
41// and settings used from the paint to canonicalize image objects.
42class SkPDFImage : public SkPDFStream {
43public:
44    /** Create a new Image XObject to represent the passed bitmap.
45     *  @param bitmap   The image to encode.
46     *  @param srcRect  The rectangle to cut out of bitmap.
47     *  @param paint    Used to calculate alpha, masks, etc.
48     *  @return  The image XObject or NUll if there is nothing to draw for
49     *           the given parameters.
50     */
51    static SkPDFImage* CreateImage(const SkBitmap& bitmap,
52                                   const SkIRect& srcRect,
53                                   SkPicture::EncodeBitmap encoder);
54
55    virtual ~SkPDFImage();
56
57    /** Add a Soft Mask (alpha or shape channel) to the image.  Refs mask.
58     *  @param mask A gray scale image representing the mask.
59     *  @return The mask argument is returned.
60     */
61    SkPDFImage* addSMask(SkPDFImage* mask);
62
63    bool isEmpty() {
64        return fSrcRect.isEmpty();
65    }
66
67    // The SkPDFObject interface.
68    virtual void getResources(const SkTSet<SkPDFObject*>& knownResourceObjects,
69                              SkTSet<SkPDFObject*>* newResourceObjects);
70
71private:
72    SkBitmap fBitmap;
73    bool fIsAlpha;
74    SkIRect fSrcRect;
75    SkPicture::EncodeBitmap fEncoder;
76    bool fStreamValid;
77
78    SkTDArray<SkPDFObject*> fResources;
79
80    /** Create a PDF image XObject. Entries for the image properties are
81     *  automatically added to the stream dictionary.
82     *  @param stream     The image stream. May be NULL. Otherwise, this
83     *                    (instead of the input bitmap) will be used as the
84     *                    PDF's content stream, possibly with lossless encoding.
85     *  @param bitmap     The image. If a stream is not given, its color data
86     *                    will be used as the image. If a stream is given, this
87     *                    is used for configuration only.
88     *  @param isAlpha    Whether or not this is the alpha of an image.
89     *  @param srcRect    The clipping applied to bitmap before generating
90     *                    imageData.
91     *  @param encoder    A function used to encode the bitmap for compression.
92     *                    May be NULL.
93     */
94    SkPDFImage(SkStream* stream, const SkBitmap& bitmap, bool isAlpha,
95               const SkIRect& srcRect, SkPicture::EncodeBitmap encoder);
96
97    /** Copy constructor, used to generate substitutes.
98     *  @param image      The SkPDFImage to copy.
99     */
100    SkPDFImage(SkPDFImage& pdfImage);
101
102    // Populate the stream dictionary.  This method returns false if
103    // fSubstitute should be used.
104    virtual bool populate(SkPDFCatalog* catalog);
105
106    typedef SkPDFStream INHERITED;
107};
108
109#endif
110