blob: d557087c01879f257fd64a2d8e7400cce920b765 [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#include "SkCodec.h"
9#include "SkData.h"
msarett74114382015-03-16 11:55:18 -070010#include "SkCodec_libbmp.h"
scroggof24f2242015-03-03 08:59:20 -080011#include "SkCodec_libpng.h"
12#include "SkStream.h"
13
msarett74114382015-03-16 11:55:18 -070014struct DecoderProc {
15 bool (*IsFormat)(SkStream*);
16 SkCodec* (*NewFromStream)(SkStream*);
17};
18
19static const DecoderProc gDecoderProcs[] = {
20 { SkPngCodec::IsPng, SkPngCodec::NewFromStream },
21 { SkBmpCodec::IsBmp, SkBmpCodec::NewFromStream }
22};
23
scroggof24f2242015-03-03 08:59:20 -080024SkCodec* SkCodec::NewFromStream(SkStream* stream) {
25 if (!stream) {
26 return NULL;
27 }
msarett74114382015-03-16 11:55:18 -070028 for (uint32_t i = 0; i < SK_ARRAY_COUNT(gDecoderProcs); i++) {
29 DecoderProc proc = gDecoderProcs[i];
30 const bool correctFormat = proc.IsFormat(stream);
31 if (!stream->rewind()) {
32 return NULL;
33 }
34 if (correctFormat) {
35 return proc.NewFromStream(stream);
36 }
scroggof24f2242015-03-03 08:59:20 -080037 }
scroggof24f2242015-03-03 08:59:20 -080038 return NULL;
39}
40
41SkCodec* SkCodec::NewFromData(SkData* data) {
42 if (!data) {
43 return NULL;
44 }
45 return NewFromStream(SkNEW_ARGS(SkMemoryStream, (data)));
46}
47
48SkCodec::SkCodec(const SkImageInfo& info, SkStream* stream)
49 : fInfo(info)
50 , fStream(stream)
51 , fNeedsRewind(false)
52{}
53
54bool SkCodec::rewindIfNeeded() {
55 // Store the value of fNeedsRewind so we can update it. Next read will
56 // require a rewind.
57 const bool neededRewind = fNeedsRewind;
58 fNeedsRewind = true;
59 return !neededRewind || fStream->rewind();
60}