blob: 3550bb183108ebc21b7a52c7b5eca2b5be67cce1 [file] [log] [blame]
scroggof24f2242015-03-03 08:59:20 -08001/*
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"
scroggofffeede2015-03-18 10:50:37 -070014#include "SkStream.h"
scroggof24f2242015-03-03 08:59:20 -080015#include "SkTemplates.h"
16#include "SkTypes.h"
17
18class SkData;
scroggof24f2242015-03-03 08:59:20 -080019
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 */
scroggofffeede2015-03-18 10:50:37 -070049 SkISize getScaledDimensions(float desiredScale) const {
50 return this->onGetScaledDimensions(desiredScale);
51 }
scroggof24f2242015-03-03 08:59:20 -080052
53protected:
54 SkCodec(const SkImageInfo&, SkStream*);
55
56 /**
57 * The SkAlphaType is a conservative answer. i.e. it is possible that it
58 * initially returns a non-opaque answer, but completing the decode
59 * reveals that the image is actually opaque.
60 */
reed3ef71e32015-03-19 08:31:14 -070061#ifdef SK_SUPPORT_LEGACY_BOOL_ONGETINFO
scroggof24f2242015-03-03 08:59:20 -080062 bool onGetInfo(SkImageInfo* info) SK_OVERRIDE {
63 *info = fInfo;
64 return true;
65 }
reed3ef71e32015-03-19 08:31:14 -070066#endif
scroggof24f2242015-03-03 08:59:20 -080067
68 // Helper for subclasses.
69 const SkImageInfo& getOriginalInfo() { return fInfo; }
70
71 virtual SkISize onGetScaledDimensions(float /* desiredScale */) const {
72 // By default, scaling is not supported.
73 return fInfo.dimensions();
74 }
75
76 /**
77 * If the stream was previously read, attempt to rewind.
78 * @returns:
79 * true
80 * - if the stream needed to be rewound, and the rewind
81 * succeeded.
82 * - if the stream did not need to be rewound.
83 * false
84 * - if the stream needed to be rewound, and rewind failed.
85 * Subclasses MUST call this function before reading the stream (e.g. in
86 * onGetPixels). If it returns false, onGetPixels should return
87 * kCouldNotRewind.
88 */
89 bool SK_WARN_UNUSED_RESULT rewindIfNeeded();
90
msarett74114382015-03-16 11:55:18 -070091 /*
92 *
93 * Get method for the input stream
94 *
95 */
96 SkStream* stream() {
97 return fStream.get();
98 }
99
scroggof24f2242015-03-03 08:59:20 -0800100private:
scroggo95526622015-03-17 05:02:17 -0700101 const SkImageInfo fInfo;
scroggof24f2242015-03-03 08:59:20 -0800102 SkAutoTDelete<SkStream> fStream;
scroggo95526622015-03-17 05:02:17 -0700103 bool fNeedsRewind;
reed3ef71e32015-03-19 08:31:14 -0700104
105 typedef SkImageGenerator INHERITED;
scroggof24f2242015-03-03 08:59:20 -0800106};
107#endif // SkCodec_DEFINED