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 |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame^] | 15 | // input SkStreamRewindable. |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 16 | |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 17 | template SkImageDecoder_DecodeReg* SkImageDecoder_DecodeReg::gHead; |
reed@android.com | 3c2b7e8 | 2009-01-27 01:43:36 +0000 | [diff] [blame] | 18 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame^] | 19 | SkImageDecoder* image_decoder_from_stream(SkStreamRewindable*); |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 20 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame^] | 21 | SkImageDecoder* image_decoder_from_stream(SkStreamRewindable* stream) { |
reed@android.com | dfee579 | 2010-04-15 14:24:50 +0000 | [diff] [blame] | 22 | SkImageDecoder* codec = NULL; |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 23 | const SkImageDecoder_DecodeReg* curr = SkImageDecoder_DecodeReg::Head(); |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 24 | while (curr) { |
reed@android.com | dfee579 | 2010-04-15 14:24:50 +0000 | [diff] [blame] | 25 | codec = curr->factory()(stream); |
reed@android.com | a16cb97 | 2009-06-18 20:26:58 +0000 | [diff] [blame] | 26 | // we rewind here, because we promise later when we call "decode", that |
| 27 | // the stream will be at its beginning. |
djsollen@google.com | 5016652 | 2013-03-20 20:16:20 +0000 | [diff] [blame] | 28 | bool rewindSuceeded = stream->rewind(); |
| 29 | |
| 30 | // our image decoder's require that rewind is supported so we fail early |
| 31 | // if we are given a stream that does not support rewinding. |
| 32 | if (!rewindSuceeded) { |
| 33 | SkDEBUGF(("Unable to rewind the image stream.")); |
| 34 | SkDELETE(codec); |
| 35 | return NULL; |
| 36 | } |
| 37 | |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 38 | if (codec) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | return codec; |
| 40 | } |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 41 | curr = curr->next(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | } |
| 43 | return NULL; |
| 44 | } |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 45 | |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 46 | template SkImageDecoder_FormatReg* SkImageDecoder_FormatReg::gHead; |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 47 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame^] | 48 | SkImageDecoder::Format SkImageDecoder::GetStreamFormat(SkStreamRewindable* stream) { |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 49 | const SkImageDecoder_FormatReg* curr = SkImageDecoder_FormatReg::Head(); |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 50 | while (curr != NULL) { |
| 51 | Format format = curr->factory()(stream); |
| 52 | if (!stream->rewind()) { |
| 53 | SkErrorInternals::SetError(kInvalidOperation_SkError, |
| 54 | "Unable to rewind the image stream\n"); |
| 55 | return kUnknown_Format; |
| 56 | } |
| 57 | if (format != kUnknown_Format) { |
| 58 | return format; |
| 59 | } |
| 60 | curr = curr->next(); |
| 61 | } |
| 62 | return kUnknown_Format; |
| 63 | } |