Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Unified Diff: src/codec/SkRawCodec.cpp

Issue 1520403003: Prototype of RAW decoding in Skia. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Initial upload of the Prototype. Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« src/codec/SkRawCodec.h ('K') | « src/codec/SkRawCodec.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/codec/SkRawCodec.cpp
diff --git a/src/codec/SkRawCodec.cpp b/src/codec/SkRawCodec.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..954d7351f291e8f26c769720ec6f74d840f686f0
--- /dev/null
+++ b/src/codec/SkRawCodec.cpp
@@ -0,0 +1,283 @@
+/*
+ * 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 "SkJpegCodec.h"
+
+#include "dng_color_space.h"
+#include "dng_exceptions.h"
+#include "dng_host.h"
+#include "dng_info.h"
+#include "dng_render.h"
+
+#include "src/piex.h"
+
+#include <cstring>
+#include <cmath>
+#include <vector>
+
+namespace {
+
+class SkDngStream : public dng_stream {
+ public:
+ explicit SkDngStream(SkStream* stream)
+ : fData(stream->getMemoryBase())
scroggo 2015/12/18 15:56:26 I brought this up in the other code review (see ht
yujieqin 2016/01/06 18:47:19 Done for the DNG case. We still plan to do more
+ , fDataSize(stream->getLength()) {}
scroggo 2015/12/18 15:56:26 ditto.
yujieqin 2016/01/06 18:47:19 Done.
+
+ ~SkDngStream() override {}
+
+ protected:
+ uint64 DoGetLength() override { return fDataSize; }
+ void DoRead(void* data, uint32 count, uint64 offset) override {
+ if (fDataSize < offset + count) {
+ ThrowReadFile();
+ }
+ std::memcpy(data,
scroggo 2015/12/18 15:56:27 Why not just: memcpy(data, reinterpret_cast<cons
yujieqin 2016/01/06 18:47:19 Done.
+ reinterpret_cast<const uint8*>(fData) + offset, count);
+ }
+
+ private:
+ const void* fData;
+ size_t fDataSize;
+};
+
+class SkPiexStream : public ::piex::StreamInterface {
+ public:
+ explicit SkPiexStream(SkStream* stream)
+ : fData(stream->getMemoryBase())
+ , fDataSize(stream->getLength()) {}
+
+ ::piex::Error GetData(const size_t offset, const size_t length,
+ uint8* data) override {
+ if (offset >= fDataSize || fDataSize - offset < length) {
+ return ::piex::Error::kFail;
+ }
+ std::memcpy(data,
+ reinterpret_cast<const uint8*>(fData) + offset, length);
+ return ::piex::Error::kOk;
+ }
+
+ private:
+ const void* fData;
+ size_t fDataSize;
+};
+
+bool IsDng(SkStream* data) {
+ try {
+ SkDngStream stream(data);
scroggo 2015/12/18 15:56:27 Please see comments in https://codereview.chromium
yujieqin 2016/01/06 18:47:19 code removed.
+ dng_host host;
+ dng_info info;
+ info.Parse(host, stream);
+ info.PostParse(host);
+ const bool ok= info.IsValidDNG();
+ return ok;
+ } catch (...) { // Handle all kinds of exceptions as "false".
+ return false;
+ }
+}
+
+bool IsPiexSupportedRaw(SkStream* data) {
+ SkPiexStream stream(data);
+ const bool ok = ::piex::IsRaw(&stream);
+ return ok;
+}
+
+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) {
+ printf("ReadDng faile with dng_exception\n");
+ return false;
+ } catch (...) {
+ printf("ReadDng faile with unknown exception\n");
+ 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()) {
+ printf("PrepareStage3 Fail: negative->IsDamaged\n");
+ return false;
+ }
+
+ const int32 kMosaicPlane = -1;
+ negative->BuildStage2Image(*host);
+ negative->BuildStage3Image(*host, kMosaicPlane);
+ }
+
+ return true;
+}
+
+} // namespace
+
+bool SkRawCodec::IsRaw(SkStream* stream) {
+ const bool ok = IsDng(stream) || IsPiexSupportedRaw(stream);
+ return ok;
+}
+
+SkCodec* SkRawCodec::NewFromStream(SkStream* data) {
+ if (IsDng(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())) {
+ printf("NewFromStream Dng SDK Failed.\n");
+ 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);
+ } else if (IsPiexSupportedRaw(data)) {
+ ::piex::PreviewImageData imageData;
+ SkPiexStream stream(data);
+ ::piex::Error error = ::piex::GetPreviewImageData(&stream, &imageData);
+ if (error != ::piex::Error::kOk) {
+ printf("NewFromStream Piex Failed.\n");
+ return nullptr;
+ }
+
+ if (!data->rewind()) {
+ printf("Failed to rewind the stream.\n");
+ return nullptr;
+ }
+
+ data->skip(imageData.jpeg_offset);
+ std::vector<unsigned char>
+ buffer_jpeg_head(std::min(3u, imageData.jpeg_length));
+ data->read(static_cast<void*>(buffer_jpeg_head.data()),
+ buffer_jpeg_head.size());
+ if (!SkJpegCodec::IsJpeg(static_cast<void*>(buffer_jpeg_head.data()),
+ buffer_jpeg_head.size())) {
+ printf("SkRawCodec Piex -> SkJpegCodec::IsJpeg failed.\n");
+ return nullptr;
+ }
+
+ if (!data->rewind()) {
+ printf("Failed to rewind the stream.\n");
+ return nullptr;
+ }
+ data->skip(imageData.jpeg_offset);
+ SkCodec* newCodec = SkJpegCodec::NewFromStream(data);
+ if (newCodec == nullptr) {
+ printf("SkRawCodec Piex -> SkJpegCodec is nullptr!\n");
+ }
+ return newCodec;
+ }
+
+ return nullptr;
+}
+
+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();
+ if (IsDng(this->stream())) {
+ 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())) {
+ printf("onGetPixels faile on Prepare\n");
msarett 2015/12/18 16:26:13 nit: Spelling
yujieqin 2016/01/06 18:47:19 code for debugging is removed.
+ 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) {
+ printf("onGetPixels faile with dng_exception\n");
msarett 2015/12/18 16:26:13 nit: Spelling
yujieqin 2016/01/06 18:47:19 code for debugging is removed.
+ return kInvalidInput;
+ } catch (...) {
+ printf("onGetPixels faile with unknown exception\n");
msarett 2015/12/18 16:26:12 nit: Spelling
yujieqin 2016/01/06 18:47:19 code for debugging is removed.
+ return kInvalidInput;
+ }
+
+ void* dstRow = dst;
+ uint8 srcRow[width * 3];
+ std::unique_ptr<SkSwizzler> swizzler(
+ SkSwizzler::CreateSwizzler(SkSwizzler::kRGB, nullptr, requestedInfo, options));
+ if (!swizzler) {
+ printf("onGetPixels swizzler failed\n");
+ 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;
+ }
+
+ return kUnimplemented;
+}
+
+SkRawCodec::SkRawCodec(const SkImageInfo& srcInfo, SkStream* stream)
+ : INHERITED(srcInfo, stream) {}
+
« src/codec/SkRawCodec.h ('K') | « src/codec/SkRawCodec.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698