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 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
208 if (!safe_add_to_size_t(this->bytesWritten(), size, &newSize) || | 208 if (!safe_add_to_size_t(this->bytesWritten(), size, &newSize) || |
209 newSize > kMaxStreamSize) | 209 newSize > kMaxStreamSize) |
210 { | 210 { |
211 SkCodecPrintf("Error: Stream size exceeds the limit.\n"); | 211 SkCodecPrintf("Error: Stream size exceeds the limit.\n"); |
212 return false; | 212 return false; |
213 } | 213 } |
214 return this->INHERITED::write(buffer, size); | 214 return this->INHERITED::write(buffer, size); |
215 } | 215 } |
216 | 216 |
217 private: | 217 private: |
218 // Most of valid RAW images will not be larger than 100MB. This limit is hel pful to avoid | |
219 // streaming too large data chunk. We can always adjust the limit here if we need. | |
218 const size_t kMaxStreamSize = 100 * 1024 * 1024; // 100MB | 220 const size_t kMaxStreamSize = 100 * 1024 * 1024; // 100MB |
219 | 221 |
220 typedef SkDynamicMemoryWStream INHERITED; | 222 typedef SkDynamicMemoryWStream INHERITED; |
221 }; | 223 }; |
222 | 224 |
223 // Note: the maximum buffer size is 100MB (limited by SkRawLimitedDynamicMemoryW Stream). | 225 // Note: the maximum buffer size is 100MB (limited by SkRawLimitedDynamicMemoryW Stream). |
224 class SkRawBufferedStream : public SkRawStream { | 226 class SkRawBufferedStream : public SkRawStream { |
225 public: | 227 public: |
226 // Will take the ownership of the stream. | 228 // Will take the ownership of the stream. |
227 explicit SkRawBufferedStream(SkStream* stream) | 229 explicit SkRawBufferedStream(SkStream* stream) |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
438 class SkDngImage { | 440 class SkDngImage { |
439 public: | 441 public: |
440 /* | 442 /* |
441 * Initializes the object with the information from Piex in a first attempt. This way it can | 443 * Initializes the object with the information from Piex in a first attempt. This way it can |
442 * save time and storage to obtain the DNG dimensions and color filter array (CFA) pattern | 444 * save time and storage to obtain the DNG dimensions and color filter array (CFA) pattern |
443 * which is essential for the demosaicing of the sensor image. | 445 * which is essential for the demosaicing of the sensor image. |
444 * Note: this will take the ownership of the stream. | 446 * Note: this will take the ownership of the stream. |
445 */ | 447 */ |
446 static SkDngImage* NewFromStream(SkRawStream* stream) { | 448 static SkDngImage* NewFromStream(SkRawStream* stream) { |
447 SkAutoTDelete<SkDngImage> dngImage(new SkDngImage(stream)); | 449 SkAutoTDelete<SkDngImage> dngImage(new SkDngImage(stream)); |
450 if (!dngImage->isTiffHeaderValid()) { | |
msarett
2016/03/10 13:51:56
Just to make sure I understand:
All dngs should h
yujieqin
2016/03/10 13:56:03
Yes, DNG image is basically an extended TIFF. It s
| |
451 return nullptr; | |
452 } | |
453 | |
448 if (!dngImage->initFromPiex()) { | 454 if (!dngImage->initFromPiex()) { |
449 if (!dngImage->readDng()) { | 455 if (!dngImage->readDng()) { |
450 return nullptr; | 456 return nullptr; |
451 } | 457 } |
452 } | 458 } |
453 | 459 |
454 return dngImage.release(); | 460 return dngImage.release(); |
455 } | 461 } |
456 | 462 |
457 /* | 463 /* |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
515 | 521 |
516 bool isScalable() const { | 522 bool isScalable() const { |
517 return fIsScalable; | 523 return fIsScalable; |
518 } | 524 } |
519 | 525 |
520 bool isXtransImage() const { | 526 bool isXtransImage() const { |
521 return fIsXtransImage; | 527 return fIsXtransImage; |
522 } | 528 } |
523 | 529 |
524 private: | 530 private: |
531 // Quick check if the image contains a valid TIFF header as requested by DNG format. | |
532 bool isTiffHeaderValid() const { | |
533 const size_t kHeaderSize = 4; | |
534 SkAutoSTMalloc<kHeaderSize, unsigned char> header(kHeaderSize); | |
535 if (!fStream->read(header.get(), 0 /* offset */, kHeaderSize)) { | |
536 return false; | |
537 } | |
538 | |
539 // Check if the header is valid (endian info and magic number "42"). | |
540 return | |
541 (header[0] == 0x49 && header[1] == 0x49 && header[2] == 0x2A && head er[3] == 0x00) || | |
542 (header[0] == 0x4D && header[1] == 0x4D && header[2] == 0x00 && head er[3] == 0x2A); | |
543 } | |
544 | |
525 void init(const int width, const int height, const dng_point& cfaPatternSize ) { | 545 void init(const int width, const int height, const dng_point& cfaPatternSize ) { |
526 fImageInfo = SkImageInfo::Make(width, height, kN32_SkColorType, kOpaque_ SkAlphaType); | 546 fImageInfo = SkImageInfo::Make(width, height, kN32_SkColorType, kOpaque_ SkAlphaType); |
527 | 547 |
528 // The DNG SDK scales only during demosaicing, so scaling is only possib le when | 548 // The DNG SDK scales only during demosaicing, so scaling is only possib le when |
529 // a mosaic info is available. | 549 // a mosaic info is available. |
530 fIsScalable = cfaPatternSize.v != 0 && cfaPatternSize.h != 0; | 550 fIsScalable = cfaPatternSize.v != 0 && cfaPatternSize.h != 0; |
531 fIsXtransImage = fIsScalable ? (cfaPatternSize.v == 6 && cfaPatternSize. h == 6) : false; | 551 fIsXtransImage = fIsScalable ? (cfaPatternSize.v == 6 && cfaPatternSize. h == 6) : false; |
532 } | 552 } |
533 | 553 |
534 bool initFromPiex() { | 554 bool initFromPiex() { |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
732 SkISize sizeFloor = this->onGetScaledDimensions(1.f / std::floor(fullShortEd ge / shortEdge)); | 752 SkISize sizeFloor = this->onGetScaledDimensions(1.f / std::floor(fullShortEd ge / shortEdge)); |
733 SkISize sizeCeil = this->onGetScaledDimensions(1.f / std::ceil(fullShortEdge / shortEdge)); | 753 SkISize sizeCeil = this->onGetScaledDimensions(1.f / std::ceil(fullShortEdge / shortEdge)); |
734 return sizeFloor == dim || sizeCeil == dim; | 754 return sizeFloor == dim || sizeCeil == dim; |
735 } | 755 } |
736 | 756 |
737 SkRawCodec::~SkRawCodec() {} | 757 SkRawCodec::~SkRawCodec() {} |
738 | 758 |
739 SkRawCodec::SkRawCodec(SkDngImage* dngImage) | 759 SkRawCodec::SkRawCodec(SkDngImage* dngImage) |
740 : INHERITED(dngImage->getImageInfo(), nullptr) | 760 : INHERITED(dngImage->getImageInfo(), nullptr) |
741 , fDngImage(dngImage) {} | 761 , fDngImage(dngImage) {} |
OLD | NEW |