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

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: Address 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
« src/codec/SkRawCodec.h ('K') | « src/codec/SkRawCodec.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <limits>
29
30 namespace {
31
32 // T must be unsigned type.
33 template <class T>
34 bool safe_add_to_size_t(T arg1, T arg2, size_t* result) {
35 SkASSERT(arg1 >= 0);
36 SkASSERT(arg2 >= 0);
37 if (arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) {
38 T sum = arg1 + arg2;
39 if (sum <= std::numeric_limits<size_t>::max()) {
scroggo 2016/01/12 21:54:39 How can this be false if the above if statement is
yujieqin 2016/01/13 15:29:42 T could be uint64 in our use case, so we need this
40 *result = static_cast<size_t>(sum);
41 return true;
42 }
43 }
44 return false;
45 }
46
47 dng_negative* read_dng(dng_stream* stream, dng_host* host, dng_info* info) {
48 try {
49 SkAutoTDelete<dng_negative> negative;
50 host->ValidateSizes();
51
52 info->Parse(*host, *stream);
53 info->PostParse(*host);
54
55 if (!info->IsValidDNG()) {
56 return nullptr;
57 }
58
59 negative.reset(host->Make_dng_negative());
60 negative->Parse(*host, *stream, *info);
61 negative->PostParse(*host, *stream, *info);
62 negative->SynchronizeMetadata();
63
64 return negative.release();
65 } catch (...) {
66 return nullptr;
67 }
68 }
69
70 /*
71 * Renders the DNG image, which is loaded by read_dng().
scroggo 2016/01/12 21:54:39 I find this comment confusing. Is it saying that i
yujieqin 2016/01/13 15:29:42 moved into the class SkDngImage, then we don't nee
72 *
73 * @param preferredSize
74 * Determines the preferred size of the long edge. The rendered image will be cl ose to this size,
scroggo 2016/01/12 21:54:39 nit: Specifies the preferred size ... sounds bett
yujieqin 2016/01/13 15:29:42 Done.
75 * but there is no guarantee that any of the edges will match the preferred size . E.g.
76 * 100% size: 4000 x 3000
77 * preferred size: 1600 (1600 x 1200)
78 * returned size could be: 2000 x 1500
79 *
80 * @param stream
81 * Contains the data of the image.
scroggo 2016/01/12 21:54:39 I think this is more clear as: Contains the compre
yujieqin 2016/01/13 15:29:42 Comment removed.
82 *
83 * @param host
84 * Contains the connection to DNG SDK.
85 *
86 * @param info
87 * Contains the infomation of the DNG image.
88 *
89 * @param negative
90 * Contains the parsed information of the DNG image.
91 *
92 */
93 dng_image* render_dng_for_preferred_size(const int preferredSize, dng_stream* st ream,
94 dng_host* host, dng_info* info, dng_neg ative* negative) {
95 try {
96 host->SetPreferredSize(preferredSize);
97 host->ValidateSizes();
98
99 negative->ReadStage1Image(*host, *stream, *info);
100
101 if (info->fMaskIndex != -1) {
102 negative->ReadTransparencyMask(*host, *stream, *info);
103 }
104
105 negative->ValidateRawImageDigest(*host);
106 if (negative->IsDamaged()) {
107 return nullptr;
108 }
109
110 const int32 kMosaicPlane = -1;
111 negative->BuildStage2Image(*host);
112 negative->BuildStage3Image(*host, kMosaicPlane);
113
114 dng_render render(*host, *negative);
115 render.SetFinalSpace(dng_space_sRGB::Get());
116 render.SetFinalPixelType(ttByte);
117
118 dng_point stage3_size = negative->Stage3Image()->Size();
119 render.SetMaximumSize(SkTMax(stage3_size.h, stage3_size.v));
120
121 return render.Render();
122 } catch (...) {
123 return nullptr;
124 }
125 }
126
127 } // namespace
128
129 // Note: this class could throw exception if it is used as dng_stream.
130 class SkRawStream : public dng_stream, public ::piex::StreamInterface {
131 public:
132 // Note that this call will take the ownership of stream.
133 explicit SkRawStream(SkStream* stream)
134 : fStream(stream), fWholeStreamRead(false) {}
135
136 ~SkRawStream() override {}
137
138 // Creates a SkMemoryStream from the offset with size.
139 SkMemoryStream* copyBuffer(size_t offset, size_t size) {
scroggo 2016/01/12 21:54:39 I think this method could be more efficient. Assum
yujieqin 2016/01/13 15:29:42 Agreed, I think it is a good improvement if the of
140 size_t sum;
141 if (!safe_add_to_size_t(offset, size, &sum) || !this->bufferMoreData(sum )) {
142 return nullptr;
143 }
144 SkAutoTDelete<SkMemoryStream> newStream(new SkMemoryStream(size));
145 if (!fStreamBuffer.read(const_cast<void*>(newStream->getMemoryBase()), o ffset, size)) {
146 return nullptr;
147 }
148 return newStream.release();
149 }
150
151 // For PIEX
152 ::piex::Error GetData(const size_t offset, const size_t length,
153 uint8* data) override {
154 if (offset == 0 && length == 0) {
155 return ::piex::Error::kOk;
156 }
157 size_t sum;
158 if (!safe_add_to_size_t(offset, length, &sum) || !this->bufferMoreData(s um)) {
159 return ::piex::Error::kFail;
160 }
161 if (!fStreamBuffer.read(data, offset, length)) {
162 return ::piex::Error::kFail;
163 }
164 return ::piex::Error::kOk;
165 }
166
167 protected:
168 // For dng_stream
169 uint64 DoGetLength() override {
170 if (!this->bufferMoreData(kReadToEnd)) { // read whole stream
171 ThrowReadFile();
172 }
173 return fStreamBuffer.bytesWritten();
174 }
175
176 // For dng_stream
177 void DoRead(void* data, uint32 count, uint64 offset) override {
178 if (count == 0 && offset == 0) {
179 return;
180 }
181 size_t sum;
182 if (!safe_add_to_size_t(static_cast<uint64>(count), offset, &sum) ||
183 !this->bufferMoreData(sum)) {
184 ThrowReadFile();
185 }
186
187 if (!fStreamBuffer.read(data, offset, count)) {
188 ThrowReadFile();
189 }
190 }
191
192 private:
193 // Note: if the newSize == kReadToEnd (0), this function will read to the en d of stream..
194 bool bufferMoreData(size_t newSize) {
195 if (newSize == kReadToEnd) {
196 if (fWholeStreamRead) { // the stream is already read-to-end.
197 return true;
198 }
199
200 // TODO: optimize for the special case when the input is SkMemoryStr eam.
201 return SkStreamCopy(&fStreamBuffer, fStream.get());
202 }
203
204 if (newSize <= fStreamBuffer.bytesWritten()) { // the stream is already buffered to newSize.
scroggo 2016/01/12 21:54:39 nit: 100 chars
yujieqin 2016/01/13 15:29:42 Done.
205 return true;
206 }
207 if (fWholeStreamRead) { // newSize is larger than the whole stream.
208 return false;
209 }
210
211 const size_t sizeToRead = newSize - fStreamBuffer.bytesWritten();
212 SkAutoTMalloc<uint8> tempBuffer;
213 tempBuffer.realloc(sizeToRead);
scroggo 2016/01/12 21:54:39 nit: You can combine these two lines into: SkAuto
yujieqin 2016/01/13 15:29:42 Thanks.
214 const size_t bytesRead = fStream->read(tempBuffer.get(), sizeToRead);
215 if (bytesRead != sizeToRead) {
216 return false;
217 }
218 return fStreamBuffer.write(tempBuffer.get(), bytesRead);
219 }
220
221 SkAutoTDelete<SkStream> fStream;
222 bool fWholeStreamRead;
223
224 SkDynamicMemoryWStream fStreamBuffer;
225
226 const size_t kReadToEnd = 0;
227 };
228
229 SkCodec* SkRawCodec::NewFromStream(SkStream* stream) {
230 SkAutoTDelete<SkRawStream> rawStream(new SkRawStream(stream));
231 ::piex::PreviewImageData imageData;
232 if (::piex::IsRaw(rawStream.get()) &&
233 ::piex::GetPreviewImageData(rawStream.get(), &imageData) == ::piex::Erro r::kOk)
234 {
235 SkMemoryStream* memoryStream =
236 rawStream->copyBuffer(imageData.jpeg_offset, imageData.jpeg_leng th);
237 return memoryStream ? SkJpegCodec::NewFromStream(memoryStream) : nullptr ;
scroggo 2016/01/12 21:54:39 We'll need to skip the JPEG code ing Google3 (i.e.
yujieqin 2016/01/13 15:29:42 Done
238 }
239
240 SkAutoTDelete<dng_host> host(new dng_host);
241 SkAutoTDelete<dng_info> info(new dng_info);
242 SkAutoTDelete<dng_negative> negative(read_dng(rawStream.get(), host.get(), i nfo.get()));
243 if (!negative) {
244 return nullptr;
245 }
246
247 const SkImageInfo& imageInfo = SkImageInfo::Make(
scroggo 2016/01/12 21:54:39 I don't think this is right to make this a referen
yujieqin 2016/01/13 15:29:41 Removed.
248 negative->DefaultCropSizeH().As_real64(),
249 negative->DefaultCropSizeV().As_real64(),
250 kN32_SkColorType, kOpaque_SkAlphaType);
251 return new SkRawCodec(imageInfo, rawStream.release(), host.release(),
252 info.release(), negative.release());
253 }
254
255 SkCodec::Result SkRawCodec::onGetPixels(const SkImageInfo& requestedInfo, void* dst,
256 size_t dstRowBytes, const Options& optio ns,
257 SkPMColor ctable[], int* ctableCount,
258 int* rowsDecoded) {
259 const int width = requestedInfo.width();
260 const int height = requestedInfo.height();
261 if (!fDngImage ||
262 (fDngImage->Size().h != width || fDngImage->Size().v != height))
263 {
264 // Reset the DNG image, so that we can rerender it for different size.
265 if (fDngImage) {
266 SkCodecPrintf("Warning: For RAW images, calling onGetPixels() with a size that \
267 does not match the result of onGetScaledDimensions() forces the decoder to \
268 render the image multiple times.");
269 fDngImage.reset();
270 fDngHost.reset(new dng_host);
scroggo 2016/01/12 21:54:39 Can these fields not be reused? I also notice tha
yujieqin 2016/01/13 15:29:42 Done.
271 fDngInfo.reset(new dng_info);
272 fDngNegative.reset(read_dng(fRawStream, fDngHost.get(), fDngInfo.get ()));
273 if (!fDngNegative) {
274 return kInvalidInput;
275 }
276 }
277
278 const int preferredSize = SkTMax(width, height);
279 fDngImage.reset(render_dng_for_preferred_size(preferredSize, fRawStream, fDngHost.get(),
280 fDngInfo.get(), fDngNegati ve.get()));
281 if (!fDngImage) {
282 return kInvalidInput;
283 }
284
285 const dng_point& imageSize = fDngImage->Size();
286 if (imageSize.h != width || imageSize.v != height) {
287 return kInvalidScale;
288 }
289 }
290
291 void* dstRow = dst;
292 uint8 srcRow[width * 3];
293 SkAutoTDelete<SkSwizzler> swizzler(SkSwizzler::CreateSwizzler(
294 SkSwizzler::kRGB, nullptr, requestedInfo, options));
295 SkASSERT(swizzler);
296 for (int i = 0; i < height; ++i) {
297 dng_pixel_buffer buffer;
298 buffer.fData = &srcRow[0];
299 buffer.fArea = dng_rect(i, 0, i + 1, width);
300 buffer.fPlane = 0;
301 buffer.fPlanes = 3;
302 buffer.fColStep = buffer.fPlanes;
303 buffer.fPlaneStep = 1;
304 buffer.fPixelType = ttByte;
305 buffer.fPixelSize = sizeof(uint8);
306 buffer.fRowStep = sizeof(srcRow);
307
308 try {
309 fDngImage->Get(buffer, dng_image::edge_zero);
310 } catch (...) {
311 return kInvalidInput;
312 }
313
314 swizzler->swizzle(dstRow, &srcRow[0]);
315 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
316 }
317 return kSuccess;
318 }
319
320 SkISize SkRawCodec::onGetScaledDimensions(float desiredScale) const {
321 SkISize dim = this->getInfo().dimensions();
322 if (desiredScale >= 1.0) {
scroggo 2016/01/12 21:54:39 This is redundant. We already call this in getScal
yujieqin 2016/01/13 15:29:42 Done.
323 return dim;
324 }
325
326 if (fDngImage) {
327 SkCodecPrintf("Warning: Multiple calls to onGetScaledDimensions() for RA W image. \
328 Every call requires the image to be re-rendered.");
329 }
330
331 // SkCodec treats zero dimensional images as errors, so the minimum size
332 // that we will recommend is 1x1.
333 dim.fWidth = SkTMax(1, SkScalarRoundToInt(desiredScale * dim.fWidth));
334 dim.fHeight = SkTMax(1, SkScalarRoundToInt(desiredScale * dim.fHeight));
335
336 // DNG SDK preserves the aspect ratio, so it only needs to know the longer d imension.
337 const int preferredSize = SkTMax(dim.fWidth, dim.fHeight);
338 fDngImage.reset(render_dng_for_preferred_size(preferredSize, fRawStream, fDn gHost.get(),
339 fDngInfo.get(), fDngNegative.g et()));
340 if (!fDngImage) {
341 return SkISize::Make(0, 0);
342 }
343
344 dng_point imageSize = fDngImage->Size();
345 dim.fWidth = imageSize.h;
346 dim.fHeight = imageSize.v;
347
348 return dim;
349 }
350
351 bool SkRawCodec::onDimensionsSupported(const SkISize& dim) {
352 const SkImageInfo& info = this->getInfo();
353 return dim.width() >= 1 && dim.width() <= info.width()
scroggo 2016/01/12 21:54:39 This is misleading. It reports that it can support
yujieqin 2016/01/13 15:29:41 We now offer a more reliable check.
354 && dim.height() >= 1 && dim.height() <= info.height();
355 }
356
357 SkRawCodec::~SkRawCodec() {
358 delete fRawStream;
359 }
360
361 SkRawCodec::SkRawCodec(const SkImageInfo& srcInfo, SkRawStream* stream, dng_host * host,
362 dng_info* info, dng_negative* negative)
363 : INHERITED(srcInfo, nullptr)
364 , fRawStream(stream)
365 , fDngHost(host)
366 , fDngInfo(info)
367 , fDngNegative(negative) {}
OLDNEW
« 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