michael@0: /* michael@0: * Copyright 2013 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkErrorInternals.h" michael@0: #include "SkImageDecoder.h" michael@0: #include "SkStream.h" michael@0: #include "SkTRegistry.h" michael@0: michael@0: // This file is used for registration of SkImageDecoders. It also holds a function michael@0: // for checking all the the registered SkImageDecoders for one that matches an michael@0: // input SkStreamRewindable. michael@0: michael@0: template SkImageDecoder_DecodeReg* SkImageDecoder_DecodeReg::gHead; michael@0: michael@0: SkImageDecoder* image_decoder_from_stream(SkStreamRewindable*); michael@0: michael@0: SkImageDecoder* image_decoder_from_stream(SkStreamRewindable* stream) { michael@0: SkImageDecoder* codec = NULL; michael@0: const SkImageDecoder_DecodeReg* curr = SkImageDecoder_DecodeReg::Head(); michael@0: while (curr) { michael@0: codec = curr->factory()(stream); michael@0: // we rewind here, because we promise later when we call "decode", that michael@0: // the stream will be at its beginning. michael@0: bool rewindSuceeded = stream->rewind(); michael@0: michael@0: // our image decoder's require that rewind is supported so we fail early michael@0: // if we are given a stream that does not support rewinding. michael@0: if (!rewindSuceeded) { michael@0: SkDEBUGF(("Unable to rewind the image stream.")); michael@0: SkDELETE(codec); michael@0: return NULL; michael@0: } michael@0: michael@0: if (codec) { michael@0: return codec; michael@0: } michael@0: curr = curr->next(); michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: template SkImageDecoder_FormatReg* SkImageDecoder_FormatReg::gHead; michael@0: michael@0: SkImageDecoder::Format SkImageDecoder::GetStreamFormat(SkStreamRewindable* stream) { michael@0: const SkImageDecoder_FormatReg* curr = SkImageDecoder_FormatReg::Head(); michael@0: while (curr != NULL) { michael@0: Format format = curr->factory()(stream); michael@0: if (!stream->rewind()) { michael@0: SkErrorInternals::SetError(kInvalidOperation_SkError, michael@0: "Unable to rewind the image stream\n"); michael@0: return kUnknown_Format; michael@0: } michael@0: if (format != kUnknown_Format) { michael@0: return format; michael@0: } michael@0: curr = curr->next(); michael@0: } michael@0: return kUnknown_Format; michael@0: }