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 "SkOrderedReadBuffer.h" | 11 #include "SkOrderedReadBuffer.h" |
11 #include "SkStream.h" | 12 #include "SkStream.h" |
12 #include "SkTypeface.h" | 13 #include "SkTypeface.h" |
13 | 14 |
14 SkOrderedReadBuffer::SkOrderedReadBuffer() : INHERITED() { | 15 SkOrderedReadBuffer::SkOrderedReadBuffer() : INHERITED() { |
15 fMemoryPtr = NULL; | 16 fMemoryPtr = NULL; |
16 | 17 |
17 fBitmapStorage = NULL; | 18 fBitmapStorage = NULL; |
18 fTFArray = NULL; | 19 fTFArray = NULL; |
19 fTFCount = 0; | 20 fTFCount = 0; |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 const uint32_t byteLength = count * sizeof(SkScalar); | 162 const uint32_t byteLength = count * sizeof(SkScalar); |
162 memcpy(values, fReader.skip(SkAlign4(byteLength)), byteLength); | 163 memcpy(values, fReader.skip(SkAlign4(byteLength)), byteLength); |
163 return count; | 164 return count; |
164 } | 165 } |
165 | 166 |
166 uint32_t SkOrderedReadBuffer::getArrayCount() { | 167 uint32_t SkOrderedReadBuffer::getArrayCount() { |
167 return *(uint32_t*)fReader.peek(); | 168 return *(uint32_t*)fReader.peek(); |
168 } | 169 } |
169 | 170 |
170 void SkOrderedReadBuffer::readBitmap(SkBitmap* bitmap) { | 171 void SkOrderedReadBuffer::readBitmap(SkBitmap* bitmap) { |
| 172 const int width = this->readInt(); |
| 173 const int height = this->readInt(); |
171 const size_t length = this->readUInt(); | 174 const size_t length = this->readUInt(); |
172 if (length > 0) { | 175 if (length > 0) { |
173 // Bitmap was encoded. | 176 // Bitmap was encoded. |
174 const void* data = this->skip(length); | 177 const void* data = this->skip(length); |
175 const int width = this->readInt(); | |
176 const int height = this->readInt(); | |
177 if (fBitmapDecoder != NULL && fBitmapDecoder(data, length, bitmap)) { | 178 if (fBitmapDecoder != NULL && fBitmapDecoder(data, length, bitmap)) { |
178 SkASSERT(bitmap->width() == width && bitmap->height() == height); | 179 SkASSERT(bitmap->width() == width && bitmap->height() == height); |
179 } else { | 180 return; |
180 // This bitmap was encoded when written, but we are unable to decode
, possibly due to | |
181 // not having a decoder. Use a placeholder bitmap. | |
182 SkDebugf("Could not decode bitmap. Resulting bitmap will be red.\n")
; | |
183 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); | |
184 bitmap->allocPixels(); | |
185 bitmap->eraseColor(SK_ColorRED); | |
186 } | 181 } |
| 182 // This bitmap was encoded when written, but we are unable to decode, po
ssibly due to |
| 183 // not having a decoder. |
| 184 SkDebugf("Could not decode bitmap. Resulting bitmap will be red.\n"); |
187 } else { | 185 } else { |
188 if (fBitmapStorage) { | 186 // The writer stored a boolean value to determine whether an SkBitmapHea
p was used during |
| 187 // writing. |
| 188 if (this->readBool()) { |
189 const uint32_t index = fReader.readU32(); | 189 const uint32_t index = fReader.readU32(); |
190 fReader.readU32(); // bitmap generation ID (see SkOrderedWriteBuffer
::writeBitmap) | 190 fReader.readU32(); // bitmap generation ID (see SkOrderedWriteBuffer
::writeBitmap) |
191 *bitmap = *fBitmapStorage->getBitmap(index); | 191 if (fBitmapStorage) { |
192 fBitmapStorage->releaseRef(index); | 192 *bitmap = *fBitmapStorage->getBitmap(index); |
| 193 fBitmapStorage->releaseRef(index); |
| 194 return; |
| 195 } else { |
| 196 // Throw an error. |
| 197 SkErrorInternals::SetError(kParseError_SkError, "SkOrderedWriteB
uffer::writeBitmap " |
| 198 "stored the SkBitmap in an SkBitmapHe
ap, but " |
| 199 "SkOrderedReadBuffer has no SkBitmapH
eapReader to " |
| 200 "retrieve the SkBitmap."); |
| 201 } |
193 } else { | 202 } else { |
| 203 // The SkBitmap was simply flattened. |
194 bitmap->unflatten(*this); | 204 bitmap->unflatten(*this); |
| 205 return; |
195 } | 206 } |
196 } | 207 } |
| 208 // Could not read the SkBitmap. Use a placeholder bitmap. |
| 209 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 210 bitmap->allocPixels(); |
| 211 bitmap->eraseColor(SK_ColorRED); |
197 } | 212 } |
198 | 213 |
199 SkTypeface* SkOrderedReadBuffer::readTypeface() { | 214 SkTypeface* SkOrderedReadBuffer::readTypeface() { |
200 | 215 |
201 uint32_t index = fReader.readU32(); | 216 uint32_t index = fReader.readU32(); |
202 if (0 == index || index > (unsigned)fTFCount) { | 217 if (0 == index || index > (unsigned)fTFCount) { |
203 if (index) { | 218 if (index) { |
204 SkDebugf("====== typeface index %d\n", index); | 219 SkDebugf("====== typeface index %d\n", index); |
205 } | 220 } |
206 return NULL; | 221 return NULL; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 if (sizeRecorded != sizeRead) { | 262 if (sizeRecorded != sizeRead) { |
248 // we could try to fix up the offset... | 263 // we could try to fix up the offset... |
249 sk_throw(); | 264 sk_throw(); |
250 } | 265 } |
251 } else { | 266 } else { |
252 // we must skip the remaining data | 267 // we must skip the remaining data |
253 fReader.skip(sizeRecorded); | 268 fReader.skip(sizeRecorded); |
254 } | 269 } |
255 return obj; | 270 return obj; |
256 } | 271 } |
OLD | NEW |