| OLD | NEW |
| (Empty) |
| 1 /* libs/graphics/images/SkImageDecoder.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 "SkStream.h" | |
| 20 #include "SkTemplates.h" | |
| 21 | |
| 22 static SkBitmap::Config gDeviceConfig = SkBitmap::kNo_Config; | |
| 23 | |
| 24 SkBitmap::Config SkImageDecoder::GetDeviceConfig() | |
| 25 { | |
| 26 return gDeviceConfig; | |
| 27 } | |
| 28 | |
| 29 void SkImageDecoder::SetDeviceConfig(SkBitmap::Config config) | |
| 30 { | |
| 31 gDeviceConfig = config; | |
| 32 } | |
| 33 | |
| 34 /////////////////////////////////////////////////////////////////////////////// | |
| 35 | |
| 36 SkImageDecoder::SkImageDecoder() | |
| 37 : fPeeker(NULL), fChooser(NULL), fAllocator(NULL), fSampleSize(1), | |
| 38 fDitherImage(true) { | |
| 39 } | |
| 40 | |
| 41 SkImageDecoder::~SkImageDecoder() { | |
| 42 fPeeker->safeUnref(); | |
| 43 fChooser->safeUnref(); | |
| 44 fAllocator->safeUnref(); | |
| 45 } | |
| 46 | |
| 47 SkImageDecoder::Format SkImageDecoder::getFormat() const { | |
| 48 return kUnknown_Format; | |
| 49 } | |
| 50 | |
| 51 SkImageDecoder::Peeker* SkImageDecoder::setPeeker(Peeker* peeker) { | |
| 52 SkRefCnt_SafeAssign(fPeeker, peeker); | |
| 53 return peeker; | |
| 54 } | |
| 55 | |
| 56 SkImageDecoder::Chooser* SkImageDecoder::setChooser(Chooser* chooser) { | |
| 57 SkRefCnt_SafeAssign(fChooser, chooser); | |
| 58 return chooser; | |
| 59 } | |
| 60 | |
| 61 SkBitmap::Allocator* SkImageDecoder::setAllocator(SkBitmap::Allocator* alloc) { | |
| 62 SkRefCnt_SafeAssign(fAllocator, alloc); | |
| 63 return alloc; | |
| 64 } | |
| 65 | |
| 66 void SkImageDecoder::setSampleSize(int size) { | |
| 67 if (size < 1) { | |
| 68 size = 1; | |
| 69 } | |
| 70 fSampleSize = size; | |
| 71 } | |
| 72 | |
| 73 bool SkImageDecoder::chooseFromOneChoice(SkBitmap::Config config, int width, | |
| 74 int height) const { | |
| 75 Chooser* chooser = fChooser; | |
| 76 | |
| 77 if (NULL == chooser) { // no chooser, we just say YES to decoding :) | |
| 78 return true; | |
| 79 } | |
| 80 chooser->begin(1); | |
| 81 chooser->inspect(0, config, width, height); | |
| 82 return chooser->choose() == 0; | |
| 83 } | |
| 84 | |
| 85 bool SkImageDecoder::allocPixelRef(SkBitmap* bitmap, | |
| 86 SkColorTable* ctable) const { | |
| 87 return bitmap->allocPixels(fAllocator, ctable); | |
| 88 } | |
| 89 | |
| 90 /////////////////////////////////////////////////////////////////////////////// | |
| 91 | |
| 92 bool SkImageDecoder::decode(SkStream* stream, SkBitmap* bm, | |
| 93 SkBitmap::Config pref, Mode mode) { | |
| 94 SkBitmap tmp; | |
| 95 | |
| 96 // we reset this to false before calling onDecode | |
| 97 fShouldCancelDecode = false; | |
| 98 | |
| 99 // pass a temporary bitmap, so that if we return false, we are assured of | |
| 100 // leaving the caller's bitmap untouched. | |
| 101 if (this->onDecode(stream, &tmp, pref, mode)) { | |
| 102 /* We operate on a tmp bitmap until we know we succeed. This way | |
| 103 we're sure we don't change the caller's bitmap and then later | |
| 104 return false. Returning false must mean that their parameter | |
| 105 is unchanged. | |
| 106 */ | |
| 107 bm->swap(tmp); | |
| 108 return true; | |
| 109 } | |
| 110 return false; | |
| 111 } | |
| 112 | |
| 113 /////////////////////////////////////////////////////////////////////////////// | |
| 114 | |
| 115 bool SkImageDecoder::DecodeFile(const char file[], SkBitmap* bm, | |
| 116 SkBitmap::Config pref, Mode mode) { | |
| 117 SkASSERT(file); | |
| 118 SkASSERT(bm); | |
| 119 | |
| 120 SkFILEStream stream(file); | |
| 121 return stream.isValid() && | |
| 122 SkImageDecoder::DecodeStream(&stream, bm, pref, mode); | |
| 123 } | |
| 124 | |
| 125 bool SkImageDecoder::DecodeMemory(const void* buffer, size_t size, SkBitmap* bm, | |
| 126 SkBitmap::Config pref, Mode mode) { | |
| 127 if (0 == size) { | |
| 128 return false; | |
| 129 } | |
| 130 SkASSERT(buffer); | |
| 131 | |
| 132 SkMemoryStream stream(buffer, size); | |
| 133 return SkImageDecoder::DecodeStream(&stream, bm, pref, mode); | |
| 134 } | |
| 135 | |
| 136 bool SkImageDecoder::DecodeStream(SkStream* stream, SkBitmap* bm, | |
| 137 SkBitmap::Config pref, Mode mode) { | |
| 138 SkASSERT(stream); | |
| 139 SkASSERT(bm); | |
| 140 | |
| 141 bool success = false; | |
| 142 SkImageDecoder* codec = SkImageDecoder::Factory(stream); | |
| 143 | |
| 144 if (NULL != codec) { | |
| 145 success = codec->decode(stream, bm, pref, mode); | |
| 146 delete codec; | |
| 147 } | |
| 148 return success; | |
| 149 } | |
| 150 | |
| 151 /////////////////////////////////////////////////////////////////////////////// | |
| 152 | |
| 153 #ifdef SK_SUPPORT_IMAGE_ENCODE | |
| 154 | |
| 155 SkImageEncoder::~SkImageEncoder() {} | |
| 156 | |
| 157 bool SkImageEncoder::encodeStream(SkWStream* stream, const SkBitmap& bm, | |
| 158 int quality) { | |
| 159 quality = SkMin32(100, SkMax32(0, quality)); | |
| 160 return this->onEncode(stream, bm, quality); | |
| 161 } | |
| 162 | |
| 163 bool SkImageEncoder::encodeFile(const char file[], const SkBitmap& bm, | |
| 164 int quality) { | |
| 165 quality = SkMin32(100, SkMax32(0, quality)); | |
| 166 SkFILEWStream stream(file); | |
| 167 return this->onEncode(&stream, bm, quality); | |
| 168 } | |
| 169 | |
| 170 #endif | |
| 171 | |
| OLD | NEW |