Index: src/codec/SkRawCodec.cpp |
diff --git a/src/codec/SkRawCodec.cpp b/src/codec/SkRawCodec.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6f3c8026343b931ee7e95b9d3f6ef55d61d312af |
--- /dev/null |
+++ b/src/codec/SkRawCodec.cpp |
@@ -0,0 +1,195 @@ |
+/* |
+ * 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 SkDngStream : public dng_stream { |
+ public: |
+ explicit SkDngStream(SkStream* stream) |
+ : fData(stream->getMemoryBase()) |
+ , fDataSize(stream->getLength()) {} |
+ ~SkDngStream() override {} |
+ |
+ protected: |
+ uint64 DoGetLength() override { return fDataSize; } |
+ void DoRead(void* data, uint32 count, uint64 offset) override { |
+ if (fDataSize < offset + count) { |
+ ThrowReadFile(); |
+ } |
+ |
+ memcpy(data, reinterpret_cast<const uint8*>(fData) + offset, count); |
+ } |
+ |
+ private: |
+ const void* fData; |
+ size_t fDataSize; |
+}; |
+ |
+bool IsDng(SkStream* data) { |
scroggo
2015/11/24 14:46:05
Is it possible to do less work here? It appears yo
ebrauer
2016/01/07 10:07:36
To identify a valid DNG we would require this code
|
+ try { |
+ SkDngStream 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 (...) { |
+ 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); |
scroggo
2015/11/24 14:46:05
Why didn't you just put the code for IsDng here? I
ebrauer
2016/01/07 10:07:36
We are going to rework this as part of https://cod
|
+} |
+ |
+SkCodec* SkRawCodec::NewFromStream(SkStream* data) { |
+ SkDngStream 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, |
msarett
2015/11/25 15:15:23
We would normally recommend kN32_SkColorType here
ebrauer
2016/01/07 10:07:36
We changed it in https://codereview.chromium.org/1
|
+ 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> finalDngImage; |
+ try { |
+ SkDngStream 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); |
+ |
+ finalDngImage.reset(render.Render()); |
+ } catch (const dng_exception& exception) { |
+ return kInvalidInput; |
+ } catch (...) { |
+ return kInvalidInput; |
+ } |
+ |
+ void* dstRow = dst; |
+ uint8 srcRow[width * 3]; |
+ std::unique_ptr<SkSwizzler> swizzler(SkSwizzler::CreateSwizzler( |
msarett
2015/11/25 15:15:23
Is the swizzler now working as you expect?
ebrauer
2016/01/07 10:07:36
It does what I expect, thanks.
|
+ SkSwizzler::kRGB, nullptr, requestedInfo, options)); |
+ if (!swizzler) { |
+ return kInvalidInput; |
+ } |
+ for (int i = 0; i < height; ++i) { |
+ dng_pixel_buffer buffer; |
+ buffer.fData = &srcRow[0]; |
+ buffer.fArea = dng_rect(i, 0, i + 1, width); |
+ buffer.fPlane = 0; |
+ buffer.fPlanes = 3; |
+ buffer.fColStep = buffer.fPlanes; |
+ buffer.fPlaneStep = 1; |
+ buffer.fPixelType = ttByte; |
+ buffer.fPixelSize = sizeof(uint8); |
+ buffer.fRowStep = sizeof(srcRow); |
+ finalDngImage->Get(buffer, dng_image::edge_zero); |
+ swizzler->swizzle(dstRow, &srcRow[0]); |
+ dstRow = SkTAddOffset<void>(dstRow, dstRowBytes); |
+ } |
+ return kSuccess; |
+} |
+ |
+SkRawCodec::SkRawCodec(const SkImageInfo& srcInfo, SkStream* stream) |
+ : INHERITED(srcInfo, stream) {} |
+ |