reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame^] | 1 | /* libs/graphics/ports/SkImageDecoder_Factory.cpp |
| 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include "SkImageDecoder.h" |
| 19 | #include "SkMovie.h" |
| 20 | #include "SkStream.h" |
| 21 | |
| 22 | extern SkImageDecoder* SkImageDecoder_GIF_Factory(SkStream*); |
| 23 | extern SkImageDecoder* SkImageDecoder_BMP_Factory(SkStream*); |
| 24 | extern SkImageDecoder* SkImageDecoder_ICO_Factory(SkStream*); |
| 25 | extern SkImageDecoder* SkImageDecoder_PNG_Factory(SkStream*); |
| 26 | extern SkImageDecoder* SkImageDecoder_WBMP_Factory(SkStream*); |
| 27 | extern SkImageDecoder* SkImageDecoder_JPEG_Factory(SkStream*); |
| 28 | |
| 29 | typedef SkImageDecoder* (*SkImageDecoderFactoryProc)(SkStream*); |
| 30 | |
| 31 | struct CodecFormat { |
| 32 | SkImageDecoderFactoryProc fProc; |
| 33 | SkImageDecoder::Format fFormat; |
| 34 | }; |
| 35 | |
| 36 | static const CodecFormat gPairs[] = { |
| 37 | { SkImageDecoder_GIF_Factory, SkImageDecoder::kGIF_Format }, |
| 38 | { SkImageDecoder_PNG_Factory, SkImageDecoder::kPNG_Format }, |
| 39 | { SkImageDecoder_ICO_Factory, SkImageDecoder::kICO_Format }, |
| 40 | { SkImageDecoder_WBMP_Factory, SkImageDecoder::kWBMP_Format }, |
| 41 | { SkImageDecoder_BMP_Factory, SkImageDecoder::kBMP_Format }, |
| 42 | { SkImageDecoder_JPEG_Factory, SkImageDecoder::kJPEG_Format } |
| 43 | }; |
| 44 | |
| 45 | SkImageDecoder* SkImageDecoder::Factory(SkStream* stream) { |
| 46 | for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) { |
| 47 | SkImageDecoder* codec = gPairs[i].fProc(stream); |
| 48 | stream->rewind(); |
| 49 | if (NULL != codec) { |
| 50 | return codec; |
| 51 | } |
| 52 | } |
| 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | bool SkImageDecoder::SupportsFormat(Format format) { |
| 57 | for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) { |
| 58 | if (gPairs[i].fFormat == format) { |
| 59 | return true; |
| 60 | } |
| 61 | } |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | ///////////////////////////////////////////////////////////////////////// |
| 66 | |
| 67 | typedef SkMovie* (*SkMovieFactoryProc)(SkStream*); |
| 68 | |
| 69 | extern SkMovie* SkMovie_GIF_Factory(SkStream*); |
| 70 | |
| 71 | static const SkMovieFactoryProc gMovieProcs[] = { |
| 72 | SkMovie_GIF_Factory |
| 73 | }; |
| 74 | |
| 75 | SkMovie* SkMovie::DecodeStream(SkStream* stream) { |
| 76 | for (unsigned i = 0; i < SK_ARRAY_COUNT(gMovieProcs); i++) { |
| 77 | SkMovie* movie = gMovieProcs[i](stream); |
| 78 | if (NULL != movie) { |
| 79 | return movie; |
| 80 | } |
| 81 | stream->rewind(); |
| 82 | } |
| 83 | return NULL; |
| 84 | } |
| 85 | |
| 86 | ///////////////////////////////////////////////////////////////////////// |
| 87 | |
| 88 | #ifdef SK_SUPPORT_IMAGE_ENCODE |
| 89 | |
| 90 | extern SkImageEncoder* SkImageEncoder_JPEG_Factory(); |
| 91 | extern SkImageEncoder* SkImageEncoder_PNG_Factory(); |
| 92 | |
| 93 | SkImageEncoder* SkImageEncoder::Create(Type t) { |
| 94 | switch (t) { |
| 95 | case kJPEG_Type: |
| 96 | return SkImageEncoder_JPEG_Factory(); |
| 97 | case kPNG_Type: |
| 98 | return SkImageEncoder_PNG_Factory(); |
| 99 | default: |
| 100 | return NULL; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | #endif |