OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkCodec.h" | 8 #include "SkCodec.h" |
9 #include "SkCodecPriv.h" | 9 #include "SkCodecPriv.h" |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
157 } | 157 } |
158 }; | 158 }; |
159 | 159 |
160 } // namespace | 160 } // namespace |
161 | 161 |
162 // Note: this class could throw exception if it is used as dng_stream. | 162 // Note: this class could throw exception if it is used as dng_stream. |
163 class SkRawStream : public ::piex::StreamInterface { | 163 class SkRawStream : public ::piex::StreamInterface { |
164 public: | 164 public: |
165 // Note that this call will take the ownership of stream. | 165 // Note that this call will take the ownership of stream. |
166 explicit SkRawStream(SkStream* stream) | 166 explicit SkRawStream(SkStream* stream) |
167 : fStream(stream), fWholeStreamRead(false) {} | 167 : fStream(stream) |
168 , fWholeStreamRead(false) | |
169 , fIsSeekable(fStream->peekData() != nullptr) {} | |
msarett
2016/02/01 16:23:43
I think maybe this is a mix of two suggestions.
I
scroggo
2016/02/01 16:40:18
That is correct.
| |
168 | 170 |
169 ~SkRawStream() override {} | 171 ~SkRawStream() override {} |
170 | 172 |
171 /* | 173 /* |
172 * Creates an SkMemoryStream from the offset with size. | 174 * Creates an SkMemoryStream from the offset with size. |
173 * Note: for performance reason, this function is destructive to the SkRawSt ream. One should | 175 * Note: for performance reason, this function is destructive to the SkRawSt ream. One should |
174 * abandon current object after the function call. | 176 * abandon current object after the function call. |
175 */ | 177 */ |
176 SkMemoryStream* transferBuffer(size_t offset, size_t size) { | 178 SkMemoryStream* transferBuffer(size_t offset, size_t size) { |
179 if (fIsSeekable) { | |
180 size_t sum; | |
181 if (!safe_add_to_size_t(offset, size, &sum)) { | |
182 return nullptr; | |
183 } | |
184 SkAutoTUnref<SkData> data(SkData::NewWithCopy( | |
185 static_cast<const uint8_t*>(fStream->peekData()->data()) + offse t, size)); | |
186 fStream.free(); | |
187 return new SkMemoryStream(data); | |
188 } | |
189 | |
177 SkAutoTUnref<SkData> data(SkData::NewUninitialized(size)); | 190 SkAutoTUnref<SkData> data(SkData::NewUninitialized(size)); |
178 if (offset > fStreamBuffer.bytesWritten()) { | 191 if (offset > fStreamBuffer.bytesWritten()) { |
179 // If the offset is not buffered, read from fStream directly and ski p the buffering. | 192 // If the offset is not buffered, read from fStream directly and ski p the buffering. |
180 const size_t skipLength = offset - fStreamBuffer.bytesWritten(); | 193 const size_t skipLength = offset - fStreamBuffer.bytesWritten(); |
181 if (fStream->skip(skipLength) != skipLength) { | 194 if (fStream->skip(skipLength) != skipLength) { |
182 return nullptr; | 195 return nullptr; |
183 } | 196 } |
184 const size_t bytesRead = fStream->read(data->writable_data(), size); | 197 const size_t bytesRead = fStream->read(data->writable_data(), size); |
185 if (bytesRead < size) { | 198 if (bytesRead < size) { |
186 data.reset(SkData::NewSubset(data.get(), 0, bytesRead)); | 199 data.reset(SkData::NewSubset(data.get(), 0, bytesRead)); |
(...skipping 17 matching lines...) Expand all Loading... | |
204 data.reset(SkData::NewSubset(data.get(), 0, newSize)); | 217 data.reset(SkData::NewSubset(data.get(), 0, newSize)); |
205 } | 218 } |
206 } | 219 } |
207 } | 220 } |
208 return new SkMemoryStream(data); | 221 return new SkMemoryStream(data); |
209 } | 222 } |
210 | 223 |
211 // For PIEX | 224 // For PIEX |
212 ::piex::Error GetData(const size_t offset, const size_t length, | 225 ::piex::Error GetData(const size_t offset, const size_t length, |
213 uint8* data) override { | 226 uint8* data) override { |
214 if (offset == 0 && length == 0) { | 227 return read(offset, length, static_cast<void*>(data)) ? |
215 return ::piex::Error::kOk; | 228 ::piex::Error::kOk : ::piex::Error::kFail; |
216 } | |
217 size_t sum; | |
218 if (!safe_add_to_size_t(offset, length, &sum) || !this->bufferMoreData(s um)) { | |
219 return ::piex::Error::kFail; | |
220 } | |
221 if (!fStreamBuffer.read(data, offset, length)) { | |
222 return ::piex::Error::kFail; | |
223 } | |
224 return ::piex::Error::kOk; | |
225 } | 229 } |
226 | 230 |
227 // For dng_stream | 231 // For dng_stream |
228 uint64 getLength() { | 232 uint64 getLength() { |
229 if (!this->bufferMoreData(kReadToEnd)) { // read whole stream | 233 if (fIsSeekable) { |
230 ThrowReadFile(); | 234 return fStream->getLength(); |
235 } else { | |
236 if (!this->bufferMoreData(kReadToEnd)) { // read whole stream | |
237 ThrowReadFile(); | |
238 } | |
239 return fStreamBuffer.bytesWritten(); | |
231 } | 240 } |
232 return fStreamBuffer.bytesWritten(); | |
233 } | 241 } |
234 | 242 |
235 // For dng_stream | 243 // For dng_stream |
236 void read(void* data, uint32 count, uint64 offset) { | 244 void dngRead(void* data, uint32 count, uint64 offset) { |
237 if (count == 0 && offset == 0) { | |
238 return; | |
239 } | |
240 size_t sum; | 245 size_t sum; |
241 if (!safe_add_to_size_t(static_cast<uint64>(count), offset, &sum) || | 246 if (!safe_add_to_size_t(static_cast<uint64>(count), offset, &sum) || |
242 !this->bufferMoreData(sum)) { | 247 !read(static_cast<size_t>(offset), static_cast<size_t>(count), data) ) { |
243 ThrowReadFile(); | |
244 } | |
245 | |
246 if (!fStreamBuffer.read(data, static_cast<size_t>(offset), count)) { | |
247 ThrowReadFile(); | 248 ThrowReadFile(); |
248 } | 249 } |
249 } | 250 } |
250 | 251 |
251 private: | 252 private: |
253 bool read(size_t offset, size_t length, void* data) { | |
254 if (offset == 0 && length == 0) { | |
255 return true; | |
256 } | |
257 | |
258 size_t sum; | |
259 if (!safe_add_to_size_t(offset, length, &sum)) { | |
260 return false; | |
261 } | |
262 | |
263 if (fIsSeekable) { | |
264 if (sum > fStream->getLength()) { | |
265 return false; | |
266 } | |
267 | |
268 return fStream->seek(offset) && | |
269 (fStream->read(data, length) == length); | |
270 } else { | |
271 return this->bufferMoreData(sum) && fStreamBuffer.read(data, offset, length); | |
272 } | |
273 } | |
274 | |
252 // Note: if the newSize == kReadToEnd (0), this function will read to the en d of stream. | 275 // Note: if the newSize == kReadToEnd (0), this function will read to the en d of stream. |
253 bool bufferMoreData(size_t newSize) { | 276 bool bufferMoreData(size_t newSize) { |
277 SkASSERT(!fIsSeekable); | |
278 | |
254 if (newSize == kReadToEnd) { | 279 if (newSize == kReadToEnd) { |
255 if (fWholeStreamRead) { // already read-to-end. | 280 if (fWholeStreamRead) { // already read-to-end. |
256 return true; | 281 return true; |
257 } | 282 } |
258 | 283 |
259 // TODO: optimize for the special case when the input is SkMemoryStr eam. | 284 // TODO: optimize for the special case when the input is SkMemoryStr eam. |
260 return SkStreamCopy(&fStreamBuffer, fStream.get()); | 285 return SkStreamCopy(&fStreamBuffer, fStream.get()); |
261 } | 286 } |
262 | 287 |
263 if (newSize <= fStreamBuffer.bytesWritten()) { // already buffered to n ewSize | 288 if (newSize <= fStreamBuffer.bytesWritten()) { // already buffered to n ewSize |
264 return true; | 289 return true; |
265 } | 290 } |
266 if (fWholeStreamRead) { // newSize is larger than the whole stream. | 291 if (fWholeStreamRead) { // newSize is larger than the whole stream. |
267 return false; | 292 return false; |
268 } | 293 } |
269 | 294 |
270 const size_t sizeToRead = newSize - fStreamBuffer.bytesWritten(); | 295 const size_t sizeToRead = newSize - fStreamBuffer.bytesWritten(); |
271 SkAutoTMalloc<uint8> tempBuffer(sizeToRead); | 296 SkAutoTMalloc<uint8> tempBuffer(sizeToRead); |
272 const size_t bytesRead = fStream->read(tempBuffer.get(), sizeToRead); | 297 const size_t bytesRead = fStream->read(tempBuffer.get(), sizeToRead); |
273 if (bytesRead != sizeToRead) { | 298 if (bytesRead != sizeToRead) { |
274 return false; | 299 return false; |
275 } | 300 } |
276 return fStreamBuffer.write(tempBuffer.get(), bytesRead); | 301 return fStreamBuffer.write(tempBuffer.get(), bytesRead); |
277 } | 302 } |
278 | 303 |
279 SkAutoTDelete<SkStream> fStream; | 304 SkAutoTDelete<SkStream> fStream; |
280 bool fWholeStreamRead; | 305 bool fWholeStreamRead; |
306 bool fIsSeekable; | |
281 | 307 |
282 SkDynamicMemoryWStream fStreamBuffer; | 308 SkDynamicMemoryWStream fStreamBuffer; |
283 | 309 |
284 const size_t kReadToEnd = 0; | 310 const size_t kReadToEnd = 0; |
285 }; | 311 }; |
286 | 312 |
287 class SkDngStream : public dng_stream { | 313 class SkDngStream : public dng_stream { |
288 public: | 314 public: |
289 SkDngStream(SkRawStream* rawStream) : fRawStream(rawStream) {} | 315 SkDngStream(SkRawStream* rawStream) : fRawStream(rawStream) {} |
290 | 316 |
291 uint64 DoGetLength() override { return fRawStream->getLength(); } | 317 uint64 DoGetLength() override { return fRawStream->getLength(); } |
292 | 318 |
293 void DoRead(void* data, uint32 count, uint64 offset) override { | 319 void DoRead(void* data, uint32 count, uint64 offset) override { |
294 fRawStream->read(data, count, offset); | 320 fRawStream->dngRead(data, count, offset); |
295 } | 321 } |
296 | 322 |
297 private: | 323 private: |
298 SkRawStream* fRawStream; | 324 SkRawStream* fRawStream; |
299 }; | 325 }; |
300 | 326 |
301 class SkDngImage { | 327 class SkDngImage { |
302 public: | 328 public: |
303 static SkDngImage* NewFromStream(SkRawStream* stream) { | 329 static SkDngImage* NewFromStream(SkRawStream* stream) { |
304 SkAutoTDelete<SkDngImage> dngImage(new SkDngImage(stream)); | 330 SkAutoTDelete<SkDngImage> dngImage(new SkDngImage(stream)); |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
559 SkISize sizeFloor = this->onGetScaledDimensions(1.f / std::floor(fullShortEd ge / shortEdge)); | 585 SkISize sizeFloor = this->onGetScaledDimensions(1.f / std::floor(fullShortEd ge / shortEdge)); |
560 SkISize sizeCeil = this->onGetScaledDimensions(1.f / std::ceil(fullShortEdge / shortEdge)); | 586 SkISize sizeCeil = this->onGetScaledDimensions(1.f / std::ceil(fullShortEdge / shortEdge)); |
561 return sizeFloor == dim || sizeCeil == dim; | 587 return sizeFloor == dim || sizeCeil == dim; |
562 } | 588 } |
563 | 589 |
564 SkRawCodec::~SkRawCodec() {} | 590 SkRawCodec::~SkRawCodec() {} |
565 | 591 |
566 SkRawCodec::SkRawCodec(SkDngImage* dngImage) | 592 SkRawCodec::SkRawCodec(SkDngImage* dngImage) |
567 : INHERITED(dngImage->getImageInfo(), nullptr) | 593 : INHERITED(dngImage->getImageInfo(), nullptr) |
568 , fDngImage(dngImage) {} | 594 , fDngImage(dngImage) {} |
OLD | NEW |