| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2012 Google Inc. | 3 * Copyright 2012 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
| 10 #include "SkErrorInternals.h" | 10 #include "SkErrorInternals.h" |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 const uint32_t byteLength = count * sizeof(SkScalar); | 162 const uint32_t byteLength = count * sizeof(SkScalar); |
| 163 memcpy(values, fReader.skip(SkAlign4(byteLength)), byteLength); | 163 memcpy(values, fReader.skip(SkAlign4(byteLength)), byteLength); |
| 164 return count; | 164 return count; |
| 165 } | 165 } |
| 166 | 166 |
| 167 uint32_t SkOrderedReadBuffer::getArrayCount() { | 167 uint32_t SkOrderedReadBuffer::getArrayCount() { |
| 168 return *(uint32_t*)fReader.peek(); | 168 return *(uint32_t*)fReader.peek(); |
| 169 } | 169 } |
| 170 | 170 |
| 171 void SkOrderedReadBuffer::readBitmap(SkBitmap* bitmap) { | 171 void SkOrderedReadBuffer::readBitmap(SkBitmap* bitmap) { |
| 172 const size_t length = this->readUInt(); | 172 const int width = this->readInt(); |
| 173 if (length > 0) { | 173 const int height = this->readInt(); |
| 174 // Bitmap was encoded. | 174 // The writer stored a boolean value to determine whether an SkBitmapHeap wa
s used during |
| 175 const void* data = this->skip(length); | 175 // writing. |
| 176 const int width = this->readInt(); | 176 if (this->readBool()) { |
| 177 const int height = this->readInt(); | 177 // An SkBitmapHeap was used for writing. Read the index from the stream
and find the |
| 178 if (fBitmapDecoder != NULL && fBitmapDecoder(data, length, bitmap)) { | 178 // corresponding SkBitmap in fBitmapStorage. |
| 179 SkASSERT(bitmap->width() == width && bitmap->height() == height); | 179 const uint32_t index = fReader.readU32(); |
| 180 fReader.readU32(); // bitmap generation ID (see SkOrderedWriteBuffer::wr
iteBitmap) |
| 181 if (fBitmapStorage) { |
| 182 *bitmap = *fBitmapStorage->getBitmap(index); |
| 183 fBitmapStorage->releaseRef(index); |
| 184 return; |
| 180 } else { | 185 } else { |
| 186 // The bitmap was stored in a heap, but there is no way to access it
. Set an error and |
| 187 // fall through to use a place holder bitmap. |
| 188 SkErrorInternals::SetError(kParseError_SkError, "SkOrderedWriteBuffe
r::writeBitmap " |
| 189 "stored the SkBitmap in an SkBitmapHeap,
but " |
| 190 "SkOrderedReadBuffer has no SkBitmapHeapR
eader to " |
| 191 "retrieve the SkBitmap."); |
| 192 } |
| 193 } else { |
| 194 // The writer stored false, meaning the SkBitmap was not stored in an Sk
BitmapHeap. |
| 195 const size_t length = this->readUInt(); |
| 196 if (length > 0) { |
| 197 // A non-zero size means the SkBitmap was encoded. |
| 198 const void* data = this->skip(length); |
| 199 if (fBitmapDecoder != NULL && fBitmapDecoder(data, length, bitmap))
{ |
| 200 SkASSERT(bitmap->width() == width && bitmap->height() == height)
; |
| 201 return; |
| 202 } |
| 181 // This bitmap was encoded when written, but we are unable to decode
, possibly due to | 203 // This bitmap was encoded when written, but we are unable to decode
, possibly due to |
| 182 // not having a decoder. Use a placeholder bitmap. | 204 // not having a decoder. |
| 183 SkErrorInternals::SetError(kParseError_SkError, | 205 SkErrorInternals::SetError(kParseError_SkError, |
| 184 "Could not decode bitmap. Resulting bitma
p will be red."); | 206 "Could not decode bitmap. Resulting bitma
p will be red."); |
| 185 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); | |
| 186 bitmap->allocPixels(); | |
| 187 bitmap->eraseColor(SK_ColorRED); | |
| 188 } | |
| 189 } else { | |
| 190 if (fBitmapStorage) { | |
| 191 const uint32_t index = fReader.readU32(); | |
| 192 fReader.readU32(); // bitmap generation ID (see SkOrderedWriteBuffer
::writeBitmap) | |
| 193 *bitmap = *fBitmapStorage->getBitmap(index); | |
| 194 fBitmapStorage->releaseRef(index); | |
| 195 } else { | 207 } else { |
| 208 // A size of zero means the SkBitmap was simply flattened. |
| 196 bitmap->unflatten(*this); | 209 bitmap->unflatten(*this); |
| 210 return; |
| 197 } | 211 } |
| 198 } | 212 } |
| 213 // Could not read the SkBitmap. Use a placeholder bitmap. |
| 214 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 215 bitmap->allocPixels(); |
| 216 bitmap->eraseColor(SK_ColorRED); |
| 199 } | 217 } |
| 200 | 218 |
| 201 SkTypeface* SkOrderedReadBuffer::readTypeface() { | 219 SkTypeface* SkOrderedReadBuffer::readTypeface() { |
| 202 | 220 |
| 203 uint32_t index = fReader.readU32(); | 221 uint32_t index = fReader.readU32(); |
| 204 if (0 == index || index > (unsigned)fTFCount) { | 222 if (0 == index || index > (unsigned)fTFCount) { |
| 205 if (index) { | 223 if (index) { |
| 206 SkDebugf("====== typeface index %d\n", index); | 224 SkDebugf("====== typeface index %d\n", index); |
| 207 } | 225 } |
| 208 return NULL; | 226 return NULL; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 if (sizeRecorded != sizeRead) { | 267 if (sizeRecorded != sizeRead) { |
| 250 // we could try to fix up the offset... | 268 // we could try to fix up the offset... |
| 251 sk_throw(); | 269 sk_throw(); |
| 252 } | 270 } |
| 253 } else { | 271 } else { |
| 254 // we must skip the remaining data | 272 // we must skip the remaining data |
| 255 fReader.skip(sizeRecorded); | 273 fReader.skip(sizeRecorded); |
| 256 } | 274 } |
| 257 return obj; | 275 return obj; |
| 258 } | 276 } |
| OLD | NEW |