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..82d653428bb8b542c86c958a545fb475673674e0 |
| --- /dev/null |
| +++ b/src/codec/SkRawCodec.cpp |
| @@ -0,0 +1,233 @@ |
| +/* |
| + * 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 "SkJpegCodec.h" |
| +#include "SkStream.h" |
| +#include "SkStreamPriv.h" |
| +#include "SkSwizzler.h" |
| +#include "SkTemplates.h" |
| +#include "SkTypes.h" |
| + |
| +#include "dng_color_space.h" |
| +#include "dng_exceptions.h" |
| +#include "dng_host.h" |
| +#include "dng_info.h" |
| +#include "dng_render.h" |
| +#include "dng_stream.h" |
| + |
| +#include "src/piex.h" |
| + |
| +#include <cmath> |
| +#include <cstring> |
| +#include <memory> |
| +#include <vector> |
| + |
| +namespace { |
| + |
| +/* |
| + * For the dng sdk stream implementation we need to provide the full |
| + * length of the image file. However, that is not too bad because we |
| + * would need it anyway for the dng rendering pipeline. |
| + */ |
| +class SkDngStream : public dng_stream { |
| +public: |
| + SkDngStream(const void* data, size_t size) |
| + : fData(data) |
| + , fDataSize(size) {} |
| + |
| + ~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: |
| + // The memory is owned by the codec, here is only a pointer. |
| + const void* fData; |
| + const size_t fDataSize; |
| +}; |
| + |
| +class SkPiexStream : public ::piex::StreamInterface { |
| + public: |
| + SkPiexStream(const void* data, size_t size) |
| + : fData(data) |
| + , fDataSize(size) {} |
| + |
| + ::piex::Error GetData(const size_t offset, const size_t length, |
| + uint8* data) override { |
| + if (offset >= fDataSize || fDataSize - offset < length) { |
| + return ::piex::Error::kFail; |
| + } |
| + memcpy(data, reinterpret_cast<const uint8*>(fData) + offset, length); |
| + return ::piex::Error::kOk; |
| + } |
| + |
| + private: |
| + // The memory is owned by the codec, here is only a pointer. |
| + const void* fData; |
| + const size_t fDataSize; |
| +}; |
| + |
| +dng_negative* ReadDng(dng_stream* stream, dng_host* host, dng_info* info) { |
| + const uint32 kPreferredSize = 0; |
| + const uint32 kMinimumSize = 0; |
| + const uint32 kMaximumSize = 0; |
| + SkAutoTDelete<dng_negative> negative; |
| + 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 nullptr; |
| + } |
| + |
| + negative.reset(host->Make_dng_negative()); |
| + negative->Parse(*host, *stream, *info); |
| + negative->PostParse(*host, *stream, *info); |
| + } |
| + |
| + negative->SynchronizeMetadata(); |
| + } catch (...) { |
| + return nullptr; |
| + } |
| + |
| + return negative.release(); |
| +} |
| + |
| +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 |
| + |
| +SkCodec* SkRawCodec::NewFromStream(SkStream* stream) { |
| + SkAutoMalloc data; |
| + const size_t dataSize = SkCopyStreamToStorage(&data, stream); |
| + |
| + SkPiexStream piexStream(data.get(), dataSize); |
| + ::piex::PreviewImageData imageData; |
| + if (::piex::IsRaw(&piexStream) && |
| + ::piex::GetPreviewImageData(&piexStream, &imageData) == ::piex::Error::kOk) |
| + { |
| + return SkJpegCodec::NewFromStream(new SkMemoryStream( |
| + reinterpret_cast<const uint8*>(data.get()) + imageData.jpeg_offset, |
| + imageData.jpeg_length, true)); |
| + } |
| + |
| + SkDngStream dngStream(data.get(), dataSize); |
| + dng_host host; |
| + dng_info info; |
| + SkAutoTDelete<dng_negative> negative(ReadDng(&dngStream, &host, &info)); |
| + if (!negative) { |
| + return nullptr; |
| + } |
| + |
| + const SkImageInfo& imageInfo = SkImageInfo::Make( |
| + negative->DefaultCropSizeH().As_real64(), |
| + negative->DefaultCropSizeV().As_real64(), |
| + kN32_SkColorType, kOpaque_SkAlphaType); |
| + return new SkRawCodec(imageInfo, dataSize, data.detach()); |
| +} |
| + |
| +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(); |
| + SkAutoTDelete<dng_image> finalDngImage; |
| + try { |
| + SkDngStream dngStream(fDngStorage, fDngLength); |
|
scroggo
2016/01/07 13:53:20
Oh, I see the problem - this is a tab, but it shou
yujieqin
2016/01/07 15:52:01
Got it and fixed.
|
| + |
| + dng_host host; |
| + dng_info info; |
| + SkAutoTDelete<dng_negative> negative(ReadDng(&dngStream, &host, &info)); |
| + if (!negative || |
| + !PrepareStage3(&dngStream, &host, &info, negative.get())) |
|
scroggo
2016/01/07 13:53:20
I think this can fit on one line now? Then you can
yujieqin
2016/01/07 15:52:01
Done.
|
| + { |
| + return kInvalidInput; |
| + } |
| + |
| + dng_render render(host, *negative); |
| + render.SetFinalSpace(dng_space_sRGB::Get()); |
| + render.SetFinalPixelType(ttByte); |
| + |
| + finalDngImage.reset(render.Render()); |
| + } catch (...) { |
| + return kInvalidInput; |
| + } |
| + |
| + void* dstRow = dst; |
| + uint8 srcRow[width * 3]; |
| + SkAutoTDelete<SkSwizzler> swizzler(SkSwizzler::CreateSwizzler( |
| + SkSwizzler::kRGB, nullptr, requestedInfo, options)); |
| + SkASSERT(swizzler); |
| + 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); |
| + |
| + try { |
| + finalDngImage->Get(buffer, dng_image::edge_zero); |
| + } catch (...) { |
|
scroggo
2016/01/07 13:53:20
Again, this uses a TAB, but it should be spaces.
yujieqin
2016/01/07 15:52:01
Done.
|
| + return kInvalidInput; |
| + } |
| + |
| + swizzler->swizzle(dstRow, &srcRow[0]); |
| + dstRow = SkTAddOffset<void>(dstRow, dstRowBytes); |
| + } |
| + return kSuccess; |
| +} |
| + |
| +SkRawCodec::~SkRawCodec() { |
| + sk_free(fDngStorage); |
| +} |
| + |
| +SkRawCodec::SkRawCodec(const SkImageInfo& srcInfo, size_t length, void* storage) |
| + : INHERITED(srcInfo, nullptr), fDngLength(length), fDngStorage(storage) {} |