Chromium Code Reviews| Index: src/codec/SkRawCodec.cpp |
| diff --git a/src/codec/SkRawCodec.cpp b/src/codec/SkRawCodec.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..51db4234fae673b66d9dfb01f9873468089ae527 |
| --- /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: |
|
scroggo
2015/11/19 22:16:54
Skia uses four space indents (I probably should ha
ebrauer
2015/11/20 11:43:44
Done.
|
| + explicit DngStream(SkStream* stream) : data_(stream->getMemoryBase()), |
|
scroggo
2015/11/19 22:16:54
This is in the style guide, but this should look l
ebrauer
2015/11/20 11:43:44
Got it. I suppose the stream is read-forward only
scroggo
2015/11/20 21:22:37
I'm not sure what you mean by "is it possible to h
scroggo
2015/12/18 15:51:05
More to the point - the stream may actually *be* a
|
| + 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(); |
|
scroggo
2015/11/19 22:16:54
The rest of Skia does not use exceptions. Can we a
ebrauer
2015/11/20 11:43:44
The dng sdk uses them quite often. I don't see a g
|
| + } |
| + |
| + std::memcpy(data, reinterpret_cast<const uint8*>(data_) + offset, count); |
|
scroggo
2015/11/19 22:16:54
Are you calling a particular version of memcpy? Ev
ebrauer
2015/11/20 11:43:44
Done.
|
| + } |
| + |
| + 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) { |
|
scroggo
2015/11/19 22:16:54
For historical reasons, Skia does not use unique_p
|
| + 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]); |
|
msarett
2015/11/19 16:45:01
What is your question about the swizzler here? It
ebrauer
2015/11/19 16:58:47
One of my problems here is that the code crashes u
scroggo
2015/11/19 22:16:55
Yes, you can pass nullptr for the SkPMColor* param
ebrauer
2015/11/20 11:43:44
The crash happens in the swizzle call giving me a
ebrauer
2015/11/20 12:57:57
Again my fault. I must not use swizzler when the o
scroggo
2015/11/20 21:22:37
The swizzler was not created? That may mean we nee
|
| + dst_row = SkTAddOffset<void>(dst_row, dstRowBytes); |
| + } |
| + return kSuccess; |
| +} |
| + |
| +SkRawCodec::SkRawCodec(const SkImageInfo& srcInfo, SkStream* stream) |
| + : INHERITED(srcInfo, stream) {} |
| + |