epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 2 | * Copyright 2013 The Android Open Source Project |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 8 | #include "SkErrorInternals.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | #include "SkImageDecoder.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkStream.h" |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 11 | #include "SkTRegistry.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 13 | // This file is used for registration of SkImageDecoders. It also holds a function |
| 14 | // for checking all the the registered SkImageDecoders for one that matches an |
| 15 | // input SkStream. |
| 16 | |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 17 | typedef SkTRegistry<SkImageDecoder*, SkStream*> DecodeReg; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | |
agl@chromium.org | 4f9cdcc | 2010-06-10 21:32:00 +0000 | [diff] [blame] | 19 | // N.B. You can't use "DecodeReg::gHead here" due to complex C++ |
| 20 | // corner cases. |
| 21 | template DecodeReg* SkTRegistry<SkImageDecoder*, SkStream*>::gHead; |
reed@android.com | 3c2b7e8 | 2009-01-27 01:43:36 +0000 | [diff] [blame] | 22 | |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 23 | SkImageDecoder* image_decoder_from_stream(SkStream*); |
| 24 | |
| 25 | SkImageDecoder* image_decoder_from_stream(SkStream* stream) { |
reed@android.com | dfee579 | 2010-04-15 14:24:50 +0000 | [diff] [blame] | 26 | SkImageDecoder* codec = NULL; |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 27 | const DecodeReg* curr = DecodeReg::Head(); |
| 28 | while (curr) { |
reed@android.com | dfee579 | 2010-04-15 14:24:50 +0000 | [diff] [blame] | 29 | codec = curr->factory()(stream); |
reed@android.com | a16cb97 | 2009-06-18 20:26:58 +0000 | [diff] [blame] | 30 | // we rewind here, because we promise later when we call "decode", that |
| 31 | // the stream will be at its beginning. |
djsollen@google.com | 5016652 | 2013-03-20 20:16:20 +0000 | [diff] [blame] | 32 | bool rewindSuceeded = stream->rewind(); |
| 33 | |
| 34 | // our image decoder's require that rewind is supported so we fail early |
| 35 | // if we are given a stream that does not support rewinding. |
| 36 | if (!rewindSuceeded) { |
| 37 | SkDEBUGF(("Unable to rewind the image stream.")); |
| 38 | SkDELETE(codec); |
| 39 | return NULL; |
| 40 | } |
| 41 | |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 42 | if (codec) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | return codec; |
| 44 | } |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 45 | curr = curr->next(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | } |
| 47 | return NULL; |
| 48 | } |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 49 | |
| 50 | typedef SkTRegistry<SkImageDecoder::Format, SkStream*> FormatReg; |
| 51 | |
| 52 | template FormatReg* SkTRegistry<SkImageDecoder::Format, SkStream*>::gHead; |
| 53 | |
| 54 | SkImageDecoder::Format SkImageDecoder::GetStreamFormat(SkStream* stream) { |
| 55 | const FormatReg* curr = FormatReg::Head(); |
| 56 | while (curr != NULL) { |
| 57 | Format format = curr->factory()(stream); |
| 58 | if (!stream->rewind()) { |
| 59 | SkErrorInternals::SetError(kInvalidOperation_SkError, |
| 60 | "Unable to rewind the image stream\n"); |
| 61 | return kUnknown_Format; |
| 62 | } |
| 63 | if (format != kUnknown_Format) { |
| 64 | return format; |
| 65 | } |
| 66 | curr = curr->next(); |
| 67 | } |
| 68 | return kUnknown_Format; |
| 69 | } |