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

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: * Add DNG scaling. * Adress all comments. Created 4 years, 11 months 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
Index: src/codec/SkRawCodec.cpp
diff --git a/src/codec/SkRawCodec.cpp b/src/codec/SkRawCodec.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..a12c50a9225080ec24dc928b6dd2320af844a553
--- /dev/null
+++ b/src/codec/SkRawCodec.cpp
@@ -0,0 +1,316 @@
+/*
+ * 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 <limits>
+#include <memory>
+#include <vector>
scroggo 2016/01/08 17:12:25 Why do you need to include <vector>? I don't see a
yujieqin 2016/01/11 14:03:07 Done.
+
+namespace {
+
+// T must be unsigned type.
+template <class T>
+bool safe_add_to_size_t(T arg1, T arg2, size_t* result) {
+ SkASSERT(arg1 >= 0);
+ SkASSERT(arg2 >= 0);
+ if (arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) {
+ T sum = arg1 + arg2;
+ if (sum <= std::numeric_limits<size_t>::max()) {
+ *result = static_cast<size_t>(sum);
+ return true;
+ }
+ }
+ return false;
+}
+
+dng_negative* read_dng(dng_stream* stream, dng_host* host, dng_info* info) {
+ try {
+ SkAutoTDelete<dng_negative> negative;
+ host->ValidateSizes();
+
+ 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();
+
+ return negative.release();
+ } catch (...) {
+ return nullptr;
+ }
+}
+
+dng_image* render_dng_for_prefered_size(const int prefered_size, dng_stream* stream,
scroggo 2016/01/11 17:44:02 Can you add comments explaining what this does, ho
yujieqin 2016/01/12 10:47:40 Done.
+ dng_host* host, dng_info* info, dng_negative* negative) {
+ try {
+ host->SetPreferredSize(prefered_size);
+ host->ValidateSizes();
+
+ negative->ReadStage1Image(*host, *stream, *info);
+
+ if (info->fMaskIndex != -1) {
+ negative->ReadTransparencyMask(*host, *stream, *info);
+ }
+
+ negative->ValidateRawImageDigest(*host);
+ if (negative->IsDamaged()) {
+ return nullptr;
+ }
+
+ const int32 kMosaicPlane = -1;
+ negative->BuildStage2Image(*host);
+ negative->BuildStage3Image(*host, kMosaicPlane);
+
+ dng_render render(*host, *negative);
+ render.SetFinalSpace(dng_space_sRGB::Get());
+ render.SetFinalPixelType(ttByte);
+
+ dng_point stage3_size = negative->Stage3Image()->Size();
+ render.SetMaximumSize(SkTMax(stage3_size.h, stage3_size.v));
+
+ return render.Render();
+ } catch (...) {
+ return nullptr;
+ }
+}
+
+} // namespace
+
+// Note: this class could throw exception if it is used as dng_stream.
+class SkRawStream : public dng_stream, public ::piex::StreamInterface {
+public:
+ // Note that this call will take the ownership of stream.
+ explicit SkRawStream(SkStream* stream)
+ : fStream(stream), fWholeStreamRead(false), fStreamBufferSize(0) {}
+
+ ~SkRawStream() override {}
+
+ // Creates a SkMemoryStream from the offset with size.
+ SkMemoryStream* copyBuffer(size_t offset, size_t size) {
+ size_t sum;
+ if (!safe_add_to_size_t(offset, size, &sum) ||
scroggo 2016/01/08 17:12:25 nit: I think these can fit on one line? if (!safe
yujieqin 2016/01/11 14:03:07 Done.
+ !bufferMoreData(sum)) {
+ return nullptr;
+ }
+ return new SkMemoryStream(fStreamBuffer.get() + offset, size, true);
+ }
+
+ // For PIEX
+ ::piex::Error GetData(const size_t offset, const size_t length,
+ uint8* data) override {
+ size_t sum;
+ if (!safe_add_to_size_t(offset, length, &sum) ||
scroggo 2016/01/08 17:12:25 one line?
yujieqin 2016/01/11 14:03:07 Done.
+ !bufferMoreData(sum)) {
scroggo 2016/01/08 17:12:25 Sorry, something else I missed on previous reviews
yujieqin 2016/01/11 14:03:08 Done.
+ return ::piex::Error::kFail;
+ }
+ memcpy(data, fStreamBuffer.get() + offset, length);
+ return ::piex::Error::kOk;
+ }
+
+protected:
+ // For dng_stream
+ uint64 DoGetLength() override {
+ if (!bufferMoreData(0, true)) { // read whole stream
+ ThrowReadFile();
+ }
+ return fStreamBufferSize;
+ }
+
+ // For dng_stream
+ void DoRead(void* data, uint32 count, uint64 offset) override {
+ size_t sum;
+ if (!safe_add_to_size_t(static_cast<uint64>(count), offset, &sum) ||
+ !bufferMoreData(sum)) {
+ ThrowReadFile();
+ }
+ memcpy(data, fStreamBuffer.get() + offset, count);
+ }
+
+private:
+ // Note: if the readToEnd is "true", the newSize will be basically ignored in this function.
+ bool bufferMoreData(size_t newSize, bool readToEnd = false) {
scroggo 2016/01/08 17:12:26 Instead of having two mutually-exclusive parameter
yujieqin 2016/01/11 14:03:08 Done.
+ if (readToEnd) {
+ if (fWholeStreamRead) { // the stream is already read-to-end.
+ return true;
+ }
+
+ SkDynamicMemoryWStream tempStream;
+ if (!SkStreamCopy(&tempStream, fStream.get())) {
+ return false;
+ }
+
+ size_t wholeStreamSize;
+ if (!safe_add_to_size_t(fStreamBufferSize, tempStream.bytesWritten(),
+ &wholeStreamSize)) {
+ return false;
+ }
+
+ fStreamBuffer.realloc(wholeStreamSize);
+ uint8_t* appendDst = fStreamBuffer.get() + fStreamBufferSize;
+ tempStream.copyTo(appendDst);
+ fStreamBufferSize = wholeStreamSize;
scroggo 2016/01/08 17:12:26 I think you can return true; here. Then you do
yujieqin 2016/01/11 14:03:07 Done.
+ } else {
+ if (newSize <= fStreamBufferSize) { // the stream is already buffered to newSize.
+ return true;
+ }
+ if (fWholeStreamRead) { // newSize is larger than the whole stream.
+ return false;
+ }
+
+ fStreamBuffer.realloc(newSize);
+ uint8_t* appendDst = fStreamBuffer.get() + fStreamBufferSize;
+ const size_t sizeToRead = newSize - fStreamBufferSize;
+ const size_t bytesRead = fStream->read(appendDst, sizeToRead);
+ if (bytesRead < sizeToRead) {
+ return false;
+ }
+ fStreamBufferSize = newSize;
+ }
+ return true;
+ }
+
+ SkAutoTDelete<SkStream> fStream;
+ bool fWholeStreamRead;
+
+ SkAutoTMalloc<uint8> fStreamBuffer;
+ size_t fStreamBufferSize;
+};
+
+SkCodec* SkRawCodec::NewFromStream(SkStream* stream) {
+ SkAutoTDelete<SkRawStream> rawStream(new SkRawStream(stream));
+ ::piex::PreviewImageData imageData;
+ if (::piex::IsRaw(rawStream.get()) &&
+ ::piex::GetPreviewImageData(rawStream.get(), &imageData) == ::piex::Error::kOk)
+ {
+ SkMemoryStream* memoryStream =
+ rawStream->copyBuffer(imageData.jpeg_offset, imageData.jpeg_length);
+ return memoryStream ? SkJpegCodec::NewFromStream(memoryStream) : nullptr;
+ }
+
+ SkAutoTDelete<dng_host> host(new dng_host);
+ SkAutoTDelete<dng_info> info(new dng_info);
+ SkAutoTDelete<dng_negative> negative(read_dng(rawStream.get(), host.get(), info.get()));
+ 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, rawStream.detach(), host.release(), info.release(), negative.release());
msarett 2016/01/08 14:38:34 nit: Line is too long
scroggo 2016/01/08 17:12:25 Also: For historical reasons, SkAutoTDelete has t
yujieqin 2016/01/11 14:03:07 Done.
+}
+
+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 (!fDngImage) {
msarett 2016/01/08 14:38:34 I think here, you are assuming that getScaledDimen
yujieqin 2016/01/11 14:03:07 Done.
scroggo 2016/01/11 17:44:02 How was this fixed?
adaubert 2016/01/11 17:57:05 It was fixed in the way that the size of the image
msarett 2016/01/11 19:12:44 Ah I see. This is not quite enough though. After
yujieqin 2016/01/12 10:47:40 We prefer the caller use the size from onGetScaled
msarett 2016/01/12 14:01:29 Ok thanks I understand. Maybe returning kInvalidI
yujieqin 2016/01/12 15:11:09 Added the warnings.
scroggo 2016/01/12 21:54:39 Right, but this is a virtual function, and other S
yujieqin 2016/01/13 15:29:41 We refactor the whole logic of scaling DNG images.
msarett 2016/01/13 17:15:14 Good question. I believe it might. Certainly we
scroggo 2016/01/13 18:24:44 In the current common use case, Android will only
yujieqin 2016/01/14 09:33:11 Optimized the workflow for the that onGetPixels()
scroggo 2016/01/14 15:26:37 Yeah, I think it's fine that it's not efficient. A
+ const int preferedSize = SkTMax(width, height);
+ fDngImage.reset(render_dng_for_prefered_size(preferedSize, fRawStream, fDngHost.get(),
+ fDngInfo.get(), fDngNegative.get()));
+ if (!fDngImage) {
+ return kInvalidInput;
scroggo 2016/01/08 17:12:26 Arguably this could be kInvalidScale, although I d
yujieqin 2016/01/11 14:03:07 We think "kInvalidInput" should be correct for thi
scroggo 2016/01/11 17:44:02 So will render_dng_for_prefered_size succeed even
adaubert 2016/01/11 17:57:05 It will always succeed if the input data is valid.
+ }
+ }
+
+ SkDEBUGCODE(dng_point imageSize = fDngImage->Size();)
+ SkASSERT(imageSize.h == width);
+ SkASSERT(imageSize.v == height);
+
+ 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 {
+ fDngImage->Get(buffer, dng_image::edge_zero);
+ } catch (...) {
+ return kInvalidInput;
+ }
+
+ swizzler->swizzle(dstRow, &srcRow[0]);
+ dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
+ }
+ return kSuccess;
+}
+
+SkISize SkRawCodec::onGetScaledDimensions(float desiredScale) const {
+ SkISize dim = this->getInfo().dimensions();
+ // SkCodec treats zero dimensional images as errors, so the minimum size
+ // that we will recommend is 1x1.
+ dim.fWidth = SkTMax(1, SkScalarRoundToInt(desiredScale * dim.fWidth));
+ dim.fHeight = SkTMax(1, SkScalarRoundToInt(desiredScale * dim.fHeight));
+
+ const int preferedSize = SkTMax(dim.fWidth, dim.fHeight);
msarett 2016/01/08 14:38:34 Just for my curiosity, why does dng only need to k
yujieqin 2016/01/11 14:03:07 The DNG SDK only need the long edge size, because
scroggo 2016/01/11 17:44:02 Good to know. Can you add a comment? Matt and I wi
yujieqin 2016/01/12 10:47:40 Comment added. FYI, for maintaining the RAW relat
+ fDngImage.reset(render_dng_for_prefered_size(preferedSize, fRawStream, fDngHost.get(),
+ fDngInfo.get(), fDngNegative.get()));
+ if (!fDngImage) {
+ return this->getInfo().dimensions();
msarett 2016/01/08 14:38:34 In general, if the exact requested scale is not su
scroggo 2016/01/08 17:12:25 (On another note, this should be indented four spa
yujieqin 2016/01/11 14:03:07 Yes, this is actually an error case.
yujieqin 2016/01/11 14:03:08 Done.
+ }
+
+ dng_point imageSize = fDngImage->Size();
+ dim.fWidth = imageSize.h;
+ dim.fHeight = imageSize.v;
+
+ return dim;
+}
+
+bool SkRawCodec::onDimensionsSupported(const SkISize& dim) {
+ const SkImageInfo& info = this->getInfo();
+ return dim.width() >= 1 && dim.width() <= info.width()
+ && dim.height() >= 1 && dim.height() <= info.height();
+}
+
+SkRawCodec::~SkRawCodec() {
+ delete fRawStream;
+}
+
+SkRawCodec::SkRawCodec(const SkImageInfo& srcInfo, SkRawStream* stream, dng_host* host, dng_info* info,
msarett 2016/01/08 14:38:34 nit: Line greater than 100 chars
yujieqin 2016/01/11 14:03:07 Done.
+ dng_negative* negative)
+ : INHERITED(srcInfo, nullptr), fRawStream(stream), fDngHost(host), fDngInfo(info), fDngNegative(negative) {}
msarett 2016/01/08 14:38:34 nit: Line greater than 100 chars
scroggo 2016/01/08 17:12:25 FWIW, in Skia we often will put each member on its
yujieqin 2016/01/11 14:03:07 Done.
yujieqin 2016/01/11 14:03:08 Done.

Powered by Google App Engine
This is Rietveld 408576698