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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkCodec.h"
9 #include "SkRawCodec.h"
10 #include "SkCodecPriv.h"
11 #include "SkColorPriv.h"
12 #include "SkJpegCodec.h"
13 #include "SkStream.h"
14 #include "SkStreamPriv.h"
15 #include "SkSwizzler.h"
16 #include "SkTemplates.h"
17 #include "SkTypes.h"
18
19 #include "dng_color_space.h"
20 #include "dng_exceptions.h"
21 #include "dng_host.h"
22 #include "dng_info.h"
23 #include "dng_render.h"
24 #include "dng_stream.h"
25
26 #include "src/piex.h"
27
28 #include <cmath>
29 #include <cstring>
30 #include <limits>
31 #include <memory>
32 #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.
33
34 namespace {
35
36 // T must be unsigned type.
37 template <class T>
38 bool safe_add_to_size_t(T arg1, T arg2, size_t* result) {
39 SkASSERT(arg1 >= 0);
40 SkASSERT(arg2 >= 0);
41 if (arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) {
42 T sum = arg1 + arg2;
43 if (sum <= std::numeric_limits<size_t>::max()) {
44 *result = static_cast<size_t>(sum);
45 return true;
46 }
47 }
48 return false;
49 }
50
51 dng_negative* read_dng(dng_stream* stream, dng_host* host, dng_info* info) {
52 try {
53 SkAutoTDelete<dng_negative> negative;
54 host->ValidateSizes();
55
56 info->Parse(*host, *stream);
57 info->PostParse(*host);
58
59 if (!info->IsValidDNG()) {
60 return nullptr;
61 }
62
63 negative.reset(host->Make_dng_negative());
64 negative->Parse(*host, *stream, *info);
65 negative->PostParse(*host, *stream, *info);
66 negative->SynchronizeMetadata();
67
68 return negative.release();
69 } catch (...) {
70 return nullptr;
71 }
72 }
73
74 dng_image* render_dng_for_prefered_size(const int prefered_size, dng_stream* str eam,
scroggo 2016/01/11 17:44:02 Can you add comments explaining what this does, ho
yujieqin 2016/01/12 10:47:40 Done.
75 dng_host* host, dng_info* info, dng_nega tive* negative) {
76 try {
77 host->SetPreferredSize(prefered_size);
78 host->ValidateSizes();
79
80 negative->ReadStage1Image(*host, *stream, *info);
81
82 if (info->fMaskIndex != -1) {
83 negative->ReadTransparencyMask(*host, *stream, *info);
84 }
85
86 negative->ValidateRawImageDigest(*host);
87 if (negative->IsDamaged()) {
88 return nullptr;
89 }
90
91 const int32 kMosaicPlane = -1;
92 negative->BuildStage2Image(*host);
93 negative->BuildStage3Image(*host, kMosaicPlane);
94
95 dng_render render(*host, *negative);
96 render.SetFinalSpace(dng_space_sRGB::Get());
97 render.SetFinalPixelType(ttByte);
98
99 dng_point stage3_size = negative->Stage3Image()->Size();
100 render.SetMaximumSize(SkTMax(stage3_size.h, stage3_size.v));
101
102 return render.Render();
103 } catch (...) {
104 return nullptr;
105 }
106 }
107
108 } // namespace
109
110 // Note: this class could throw exception if it is used as dng_stream.
111 class SkRawStream : public dng_stream, public ::piex::StreamInterface {
112 public:
113 // Note that this call will take the ownership of stream.
114 explicit SkRawStream(SkStream* stream)
115 : fStream(stream), fWholeStreamRead(false), fStreamBufferSize(0) {}
116
117 ~SkRawStream() override {}
118
119 // Creates a SkMemoryStream from the offset with size.
120 SkMemoryStream* copyBuffer(size_t offset, size_t size) {
121 size_t sum;
122 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.
123 !bufferMoreData(sum)) {
124 return nullptr;
125 }
126 return new SkMemoryStream(fStreamBuffer.get() + offset, size, true);
127 }
128
129 // For PIEX
130 ::piex::Error GetData(const size_t offset, const size_t length,
131 uint8* data) override {
132 size_t sum;
133 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.
134 !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.
135 return ::piex::Error::kFail;
136 }
137 memcpy(data, fStreamBuffer.get() + offset, length);
138 return ::piex::Error::kOk;
139 }
140
141 protected:
142 // For dng_stream
143 uint64 DoGetLength() override {
144 if (!bufferMoreData(0, true)) { // read whole stream
145 ThrowReadFile();
146 }
147 return fStreamBufferSize;
148 }
149
150 // For dng_stream
151 void DoRead(void* data, uint32 count, uint64 offset) override {
152 size_t sum;
153 if (!safe_add_to_size_t(static_cast<uint64>(count), offset, &sum) ||
154 !bufferMoreData(sum)) {
155 ThrowReadFile();
156 }
157 memcpy(data, fStreamBuffer.get() + offset, count);
158 }
159
160 private:
161 // Note: if the readToEnd is "true", the newSize will be basically ignored i n this function.
162 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.
163 if (readToEnd) {
164 if (fWholeStreamRead) { // the stream is already read-to-end.
165 return true;
166 }
167
168 SkDynamicMemoryWStream tempStream;
169 if (!SkStreamCopy(&tempStream, fStream.get())) {
170 return false;
171 }
172
173 size_t wholeStreamSize;
174 if (!safe_add_to_size_t(fStreamBufferSize, tempStream.bytesWritten() ,
175 &wholeStreamSize)) {
176 return false;
177 }
178
179 fStreamBuffer.realloc(wholeStreamSize);
180 uint8_t* appendDst = fStreamBuffer.get() + fStreamBufferSize;
181 tempStream.copyTo(appendDst);
182 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.
183 } else {
184 if (newSize <= fStreamBufferSize) { // the stream is already buffer ed to newSize.
185 return true;
186 }
187 if (fWholeStreamRead) { // newSize is larger than the whole stream.
188 return false;
189 }
190
191 fStreamBuffer.realloc(newSize);
192 uint8_t* appendDst = fStreamBuffer.get() + fStreamBufferSize;
193 const size_t sizeToRead = newSize - fStreamBufferSize;
194 const size_t bytesRead = fStream->read(appendDst, sizeToRead);
195 if (bytesRead < sizeToRead) {
196 return false;
197 }
198 fStreamBufferSize = newSize;
199 }
200 return true;
201 }
202
203 SkAutoTDelete<SkStream> fStream;
204 bool fWholeStreamRead;
205
206 SkAutoTMalloc<uint8> fStreamBuffer;
207 size_t fStreamBufferSize;
208 };
209
210 SkCodec* SkRawCodec::NewFromStream(SkStream* stream) {
211 SkAutoTDelete<SkRawStream> rawStream(new SkRawStream(stream));
212 ::piex::PreviewImageData imageData;
213 if (::piex::IsRaw(rawStream.get()) &&
214 ::piex::GetPreviewImageData(rawStream.get(), &imageData) == ::piex::Erro r::kOk)
215 {
216 SkMemoryStream* memoryStream =
217 rawStream->copyBuffer(imageData.jpeg_offset, imageData.jpeg_leng th);
218 return memoryStream ? SkJpegCodec::NewFromStream(memoryStream) : nullptr ;
219 }
220
221 SkAutoTDelete<dng_host> host(new dng_host);
222 SkAutoTDelete<dng_info> info(new dng_info);
223 SkAutoTDelete<dng_negative> negative(read_dng(rawStream.get(), host.get(), i nfo.get()));
224 if (!negative) {
225 return nullptr;
226 }
227
228 const SkImageInfo& imageInfo = SkImageInfo::Make(
229 negative->DefaultCropSizeH().As_real64(),
230 negative->DefaultCropSizeV().As_real64(),
231 kN32_SkColorType, kOpaque_SkAlphaType);
232 return new SkRawCodec(imageInfo, rawStream.detach(), host.release(), info.re lease(), 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.
233 }
234
235 SkCodec::Result SkRawCodec::onGetPixels(const SkImageInfo& requestedInfo, void* dst,
236 size_t dstRowBytes, const Options& optio ns,
237 SkPMColor ctable[], int* ctableCount,
238 int* rowsDecoded) {
239 const int width = requestedInfo.width();
240 const int height = requestedInfo.height();
241 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
242 const int preferedSize = SkTMax(width, height);
243 fDngImage.reset(render_dng_for_prefered_size(preferedSize, fRawStream, f DngHost.get(),
244 fDngInfo.get(), fDngNegativ e.get()));
245 if (!fDngImage) {
246 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.
247 }
248 }
249
250 SkDEBUGCODE(dng_point imageSize = fDngImage->Size();)
251 SkASSERT(imageSize.h == width);
252 SkASSERT(imageSize.v == height);
253
254 void* dstRow = dst;
255 uint8 srcRow[width * 3];
256 SkAutoTDelete<SkSwizzler> swizzler(SkSwizzler::CreateSwizzler(
257 SkSwizzler::kRGB, nullptr, requestedInfo, options));
258 SkASSERT(swizzler);
259 for (int i = 0; i < height; ++i) {
260 dng_pixel_buffer buffer;
261 buffer.fData = &srcRow[0];
262 buffer.fArea = dng_rect(i, 0, i + 1, width);
263 buffer.fPlane = 0;
264 buffer.fPlanes = 3;
265 buffer.fColStep = buffer.fPlanes;
266 buffer.fPlaneStep = 1;
267 buffer.fPixelType = ttByte;
268 buffer.fPixelSize = sizeof(uint8);
269 buffer.fRowStep = sizeof(srcRow);
270
271 try {
272 fDngImage->Get(buffer, dng_image::edge_zero);
273 } catch (...) {
274 return kInvalidInput;
275 }
276
277 swizzler->swizzle(dstRow, &srcRow[0]);
278 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
279 }
280 return kSuccess;
281 }
282
283 SkISize SkRawCodec::onGetScaledDimensions(float desiredScale) const {
284 SkISize dim = this->getInfo().dimensions();
285 // SkCodec treats zero dimensional images as errors, so the minimum size
286 // that we will recommend is 1x1.
287 dim.fWidth = SkTMax(1, SkScalarRoundToInt(desiredScale * dim.fWidth));
288 dim.fHeight = SkTMax(1, SkScalarRoundToInt(desiredScale * dim.fHeight));
289
290 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
291 fDngImage.reset(render_dng_for_prefered_size(preferedSize, fRawStream, fDngH ost.get(),
292 fDngInfo.get(), fDngNegative.ge t()));
293 if (!fDngImage) {
294 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.
295 }
296
297 dng_point imageSize = fDngImage->Size();
298 dim.fWidth = imageSize.h;
299 dim.fHeight = imageSize.v;
300
301 return dim;
302 }
303
304 bool SkRawCodec::onDimensionsSupported(const SkISize& dim) {
305 const SkImageInfo& info = this->getInfo();
306 return dim.width() >= 1 && dim.width() <= info.width()
307 && dim.height() >= 1 && dim.height() <= info.height();
308 }
309
310 SkRawCodec::~SkRawCodec() {
311 delete fRawStream;
312 }
313
314 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.
315 dng_negative* negative)
316 : 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.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698