SkCodec.h revision ca358852b4fed656d11107b2aaf28318a4518b49
1/*
2 * Copyright 2015 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#ifndef SkCodec_DEFINED
9#define SkCodec_DEFINED
10
11#include "SkImageGenerator.h"
12#include "SkImageInfo.h"
13#include "SkSize.h"
14#include "SkTemplates.h"
15#include "SkTypes.h"
16
17class SkData;
18class SkStream;
19
20/**
21 *  Abstraction layer directly on top of an image codec.
22 */
23class SkCodec : public SkImageGenerator {
24public:
25    /**
26     *  If this stream represents an encoded image that we know how to decode,
27     *  return an SkCodec that can decode it. Otherwise return NULL.
28     *
29     *  If NULL is returned, the stream is deleted immediately. Otherwise, the
30     *  SkCodec takes ownership of it, and will delete it when done with it.
31     */
32    static SkCodec* NewFromStream(SkStream*);
33
34    /**
35     *  If this data represents an encoded image that we know how to decode,
36     *  return an SkCodec that can decode it. Otherwise return NULL.
37     *
38     *  Will take a ref if it returns a codec, else will not affect the data.
39     */
40    static SkCodec* NewFromData(SkData*);
41
42    /**
43     *  Return a size that approximately supports the desired scale factor.
44     *  The codec may not be able to scale efficiently to the exact scale
45     *  factor requested, so return a size that approximates that scale.
46     *
47     *  FIXME: Move to SkImageGenerator?
48     */
49    SkISize getScaledDimensions(float desiredScale) const;
50
51protected:
52    SkCodec(const SkImageInfo&, SkStream*);
53
54    /**
55     *  The SkAlphaType is a conservative answer. i.e. it is possible that it
56     *  initially returns a non-opaque answer, but completing the decode
57     *  reveals that the image is actually opaque.
58     */
59    bool onGetInfo(SkImageInfo* info) SK_OVERRIDE {
60        *info = fInfo;
61        return true;
62    }
63
64    // Helper for subclasses.
65    const SkImageInfo& getOriginalInfo() { return fInfo; }
66
67    virtual SkISize onGetScaledDimensions(float /* desiredScale */) const {
68        // By default, scaling is not supported.
69        return fInfo.dimensions();
70    }
71
72    /**
73     *  If the stream was previously read, attempt to rewind.
74     *  @returns:
75     *      true
76     *       - if the stream needed to be rewound, and the rewind
77     *         succeeded.
78     *       - if the stream did not need to be rewound.
79     *      false
80     *       - if the stream needed to be rewound, and rewind failed.
81     *  Subclasses MUST call this function before reading the stream (e.g. in
82     *  onGetPixels). If it returns false, onGetPixels should return
83     *  kCouldNotRewind.
84     */
85    bool SK_WARN_UNUSED_RESULT rewindIfNeeded();
86
87private:
88    const SkImageInfo fInfo;
89    SkAutoTDelete<SkStream> fStream;
90    bool fNeedsRewind;
91};
92#endif // SkCodec_DEFINED
93