Index: src/codec/SkRawCodec.cpp~ |
diff --git a/src/codec/SkRawCodec.cpp~ b/src/codec/SkRawCodec.cpp~ |
new file mode 100644 |
index 0000000000000000000000000000000000000000..61e72f170d9673652af8f954922d2377ee97a0e7 |
--- /dev/null |
+++ b/src/codec/SkRawCodec.cpp~ |
@@ -0,0 +1,193 @@ |
+/* |
+ * Copyright 2015 Google Inc. |
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+ |
+#include "SkCodec.h" |
+#include "SkRawCodec.h" |
+#include "SkCodecPriv.h" |
+#include "SkColorPriv.h" |
+#include "SkStream.h" |
+#include "SkTemplates.h" |
+#include "SkTypes.h" |
+#include "SkSwizzler.h" |
+ |
+#include "dng_color_space.h" |
+#include "dng_exceptions.h" |
+#include "dng_host.h" |
+#include "dng_info.h" |
+#include "dng_render.h" |
+ |
+namespace { |
+ |
+class DngStream : public dng_stream { |
+ public: |
+ explicit DngStream(SkStream* stream) : data_(stream->getMemoryBase()), |
+ data_size_(stream->getLength()) {} |
+ ~DngStream() override {} |
+ |
+ protected: |
+ uint64 DoGetLength() override { return data_size_; } |
+ void DoRead(void* data, uint32 count, uint64 offset) override { |
+ if (data_size_ < offset + count) { |
+ ThrowReadFile(); |
+ } |
+ |
+ std::memcpy(data, reinterpret_cast<const uint8*>(data_) + offset, count); |
+ } |
+ |
+ private: |
+ const void* data_; |
+ size_t data_size_; |
+}; |
+ |
+bool IsDng(SkStream* data) { |
+ try { |
+ DngStream stream(data); |
+ dng_host host; |
+ dng_info info; |
+ info.Parse(host, stream); |
+ info.PostParse(host); |
+ return info.IsValidDNG(); |
+ } catch (...) { // Handle all kinds of exceptions as "false". |
+ return false; |
+ } |
+} |
+ |
+bool ReadDng(dng_stream* stream, dng_host* host, dng_info* info, |
+ std::unique_ptr<dng_negative>* negative) { |
+ const uint32 kPreferredSize = 0; |
+ const uint32 kMinimumSize = 0; |
+ const uint32 kMaximumSize = 0; |
+ try { |
+ host->SetPreferredSize(kPreferredSize); |
+ host->SetMinimumSize(kMinimumSize); |
+ host->SetMaximumSize(kMaximumSize); |
+ |
+ host->ValidateSizes(); |
+ |
+ // Read stage 1 image into the negative. |
+ { |
+ info->Parse(*host, *stream); |
+ info->PostParse(*host); |
+ |
+ if (!info->IsValidDNG()) { |
+ return false; |
+ } |
+ |
+ negative->reset(host->Make_dng_negative()); |
+ (*negative)->Parse(*host, *stream, *info); |
+ (*negative)->PostParse(*host, *stream, *info); |
+ } |
+ |
+ (*negative)->SynchronizeMetadata(); |
+ } catch (const dng_exception& exception) { |
+ return false; |
+ } catch (...) { // Following the example from dng_validate.cc we also catch |
+ // at this point. |
+ return false; |
+ } |
+ |
+ return true; |
+} |
+ |
+bool PrepareStage3(dng_stream* stream, dng_host* host, dng_info* info, |
+ dng_negative* negative) { |
+ if (negative->Stage3Image() == nullptr) { |
+ negative->ReadStage1Image(*host, *stream, *info); |
+ |
+ if (info->fMaskIndex != -1) { |
+ negative->ReadTransparencyMask(*host, *stream, *info); |
+ } |
+ |
+ negative->ValidateRawImageDigest(*host); |
+ if (negative->IsDamaged()) { |
+ return false; |
+ } |
+ |
+ const int32 kMosaicPlane = -1; |
+ negative->BuildStage2Image(*host); |
+ negative->BuildStage3Image(*host, kMosaicPlane); |
+ } |
+ |
+ return true; |
+} |
+ |
+} // namespace |
+ |
+bool SkRawCodec::IsRaw(SkStream* stream) { |
+ return IsDng(stream); |
+} |
+ |
+SkCodec* SkRawCodec::NewFromStream(SkStream* data) { |
+ DngStream stream(data); |
+ dng_host host; |
+ dng_info info; |
+ std::unique_ptr<dng_negative> negative; |
+ if (!ReadDng(&stream, &host, &info, &negative) && |
+ !PrepareStage3(&stream, &host, &info, negative.get())) { |
+ return nullptr; |
+ } |
+ const SkImageInfo& imageInfo = SkImageInfo::Make( |
+ negative->DefaultCropSizeH().As_real64(), |
+ negative->DefaultCropSizeV().As_real64(), kRGBA_8888_SkColorType, |
+ kOpaque_SkAlphaType); |
+ return new SkRawCodec(imageInfo, data); |
+} |
+ |
+SkCodec::Result SkRawCodec::onGetPixels(const SkImageInfo& requestedInfo, void* dst, |
+ size_t dstRowBytes, const Options& options, |
+ SkPMColor ctable[], int* ctableCount, |
+ int* rowsDecoded) { |
+ const int width = requestedInfo.width(); |
+ //const int height = requestedInfo.height(); |
+ std::unique_ptr<dng_image> final_dng_image; |
+ try { |
+ DngStream stream(this->stream()); |
+ dng_host host; |
+ dng_info info; |
+ std::unique_ptr<dng_negative> negative; |
+ if (!ReadDng(&stream, &host, &info, &negative) || |
+ !PrepareStage3(&stream, &host, &info, negative.get())) { |
+ return kInvalidInput; |
+ } |
+ |
+ dng_render render(host, *negative); |
+ render.SetFinalSpace(dng_space_sRGB::Get()); |
+ render.SetFinalPixelType(ttByte); |
+ |
+ final_dng_image.reset(render.Render()); |
+ } catch (const dng_exception& exception) { |
+ return kInvalidInput; |
+ } catch (...) { |
+ return kInvalidInput; |
+ } |
+ |
+ void* dst_row = dst; |
+ //uint8 src_row[width * 3]; |
+ //const SkPMColor color = SkPreMultiplyColor(0xFFFFFFFF); |
+ /*std::unique_ptr<SkSwizzler> swizzler(SkSwizzler::CreateSwizzler( |
+ SkSwizzler::kRGB, nullptr, requestedInfo, options));*/ |
+ for (int i = 0; i < height - 1; ++i) { |
+ dng_pixel_buffer buffer; |
+ buffer.fData = dst_row; |
+ buffer.fArea = dng_rect(i, 0, i + 1, width); |
+ buffer.fPlane = 0; |
+ buffer.fPlanes = 4; |
+ buffer.fColStep = buffer.fPlanes; |
+ buffer.fPlaneStep = 1; |
+ buffer.fPixelType = ttByte; |
+ buffer.fPixelSize = sizeof(uint8); |
+ buffer.fRowStep = dstRowBytes; |
+ final_dng_image->Get(buffer, dng_image::edge_zero); |
+ //swizzler->swizzle(dst_row, &src_row[0]); |
+ dst_row = SkTAddOffset<void>(dst_row, dstRowBytes); |
+ } |
+ return kSuccess; |
+} |
+ |
+SkRawCodec::SkRawCodec(const SkImageInfo& srcInfo, SkStream* stream) |
+ : INHERITED(srcInfo, stream) {} |
+ |