OLD | NEW |
---|---|
(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 "SkCodecPriv.h" | |
10 #include "SkColorPriv.h" | |
11 #include "SkData.h" | |
12 #if !defined(GOOGLE3) | |
13 #include "SkJpegCodec.h" | |
14 #endif | |
15 #include "SkRawCodec.h" | |
16 #include "SkRefCnt.h" | |
17 #include "SkStream.h" | |
18 #include "SkStreamPriv.h" | |
19 #include "SkSwizzler.h" | |
20 #include "SkTemplates.h" | |
21 #include "SkTypes.h" | |
22 | |
23 #include "dng_color_space.h" | |
24 #include "dng_exceptions.h" | |
25 #include "dng_host.h" | |
26 #include "dng_info.h" | |
27 #include "dng_render.h" | |
28 #include "dng_stream.h" | |
29 | |
30 #include "src/piex.h" | |
31 | |
32 #include <cmath> // for std::round,floor,ceil | |
33 #include <limits> | |
34 | |
35 namespace { | |
36 | |
37 // T must be unsigned type. | |
38 template <class T> | |
39 bool safe_add_to_size_t(T arg1, T arg2, size_t* result) { | |
40 SkASSERT(arg1 >= 0); | |
41 SkASSERT(arg2 >= 0); | |
42 if (arg1 >= 0 && arg2 <= std::numeric_limits<T>::max() - arg1) { | |
43 T sum = arg1 + arg2; | |
44 if (sum <= std::numeric_limits<size_t>::max()) { | |
45 *result = static_cast<size_t>(sum); | |
46 return true; | |
47 } | |
48 } | |
49 return false; | |
50 } | |
51 | |
52 } // namespace | |
53 | |
54 // Note: this class could throw exception if it is used as dng_stream. | |
55 class SkRawStream : public dng_stream, public ::piex::StreamInterface { | |
56 public: | |
57 // Note that this call will take the ownership of stream. | |
58 explicit SkRawStream(SkStream* stream) | |
59 : fStream(stream), fWholeStreamRead(false) {} | |
60 | |
61 ~SkRawStream() override {} | |
62 | |
63 /* | |
64 * Creates a SkMemoryStream from the offset with size. | |
scroggo
2016/01/14 15:26:38
nit: an SkMemoryStream
yujieqin
2016/01/15 14:49:31
Done.
| |
65 * Note: for performance reason, this function is destructive to the SkRawSt ream. One should | |
66 * abandon current object after the function call. | |
67 */ | |
68 SkMemoryStream* copyBuffer(size_t offset, size_t size) { | |
scroggo
2016/01/14 15:26:38
Maybe this should have a more descriptive name tha
yujieqin
2016/01/15 14:49:30
Let's call it transferBuffer().
| |
69 SkAutoTUnref<SkData> data(SkData::NewUninitialized(size)); | |
70 if (offset > fStreamBuffer.bytesWritten()) { | |
71 // If the offset is not buffered, read from fStream directly and ski p the buffering. | |
72 // This is destructive to currect object. | |
73 const size_t skipLength = offset - fStreamBuffer.bytesWritten(); | |
74 if (fStream->skip(skipLength) != skipLength) { | |
75 return nullptr; | |
76 } | |
77 if (fStream->read(data->writable_data(), size) != size) { | |
78 return nullptr; | |
scroggo
2016/01/14 15:26:38
I suggested this, and it matches the intent of the
yujieqin
2016/01/15 14:49:30
Done.
| |
79 } | |
80 } else { | |
81 const size_t alreadyBuffered = fStreamBuffer.bytesWritten() - offset ; | |
82 if (!fStreamBuffer.read(data->writable_data(), offset, alreadyBuffer ed)) { | |
83 return nullptr; | |
84 } | |
85 | |
86 const size_t remaining = size - alreadyBuffered; | |
87 auto* dst = static_cast<uint8_t*>(data->writable_data()) + alreadyBu ffered; | |
88 if (remaining && fStream->read(dst, remaining) != remaining) { | |
scroggo
2016/01/14 15:26:38
The above comment ("This is destructive to the cur
yujieqin
2016/01/15 14:49:31
true, I just removed the line 72, and we had comme
| |
89 return nullptr; | |
scroggo
2016/01/14 15:26:38
Similar to the above case, we can pass what is ava
yujieqin
2016/01/15 14:49:30
Done.
| |
90 } | |
91 } | |
92 return new SkMemoryStream(data); | |
93 } | |
94 | |
95 // For PIEX | |
96 ::piex::Error GetData(const size_t offset, const size_t length, | |
97 uint8* data) override { | |
98 if (offset == 0 && length == 0) { | |
99 return ::piex::Error::kOk; | |
100 } | |
101 size_t sum; | |
102 if (!safe_add_to_size_t(offset, length, &sum) || !this->bufferMoreData(s um)) { | |
103 return ::piex::Error::kFail; | |
104 } | |
105 if (!fStreamBuffer.read(data, offset, length)) { | |
106 return ::piex::Error::kFail; | |
107 } | |
108 return ::piex::Error::kOk; | |
109 } | |
110 | |
111 protected: | |
112 // For dng_stream | |
113 uint64 DoGetLength() override { | |
114 if (!this->bufferMoreData(kReadToEnd)) { // read whole stream | |
115 ThrowReadFile(); | |
116 } | |
117 return fStreamBuffer.bytesWritten(); | |
118 } | |
119 | |
120 // For dng_stream | |
121 void DoRead(void* data, uint32 count, uint64 offset) override { | |
122 if (count == 0 && offset == 0) { | |
123 return; | |
124 } | |
125 size_t sum; | |
126 if (!safe_add_to_size_t(static_cast<uint64>(count), offset, &sum) || | |
127 !this->bufferMoreData(sum)) { | |
128 ThrowReadFile(); | |
129 } | |
130 | |
131 if (!fStreamBuffer.read(data, offset, count)) { | |
132 ThrowReadFile(); | |
133 } | |
134 } | |
135 | |
136 private: | |
137 // Note: if the newSize == kReadToEnd (0), this function will read to the en d of stream. | |
138 bool bufferMoreData(size_t newSize) { | |
139 if (newSize == kReadToEnd) { | |
140 if (fWholeStreamRead) { // already read-to-end. | |
141 return true; | |
142 } | |
143 | |
144 // TODO: optimize for the special case when the input is SkMemoryStr eam. | |
145 return SkStreamCopy(&fStreamBuffer, fStream.get()); | |
146 } | |
147 | |
148 if (newSize <= fStreamBuffer.bytesWritten()) { // already buffered to n ewSize | |
149 return true; | |
150 } | |
151 if (fWholeStreamRead) { // newSize is larger than the whole stream. | |
152 return false; | |
153 } | |
154 | |
155 const size_t sizeToRead = newSize - fStreamBuffer.bytesWritten(); | |
156 SkAutoTMalloc<uint8> tempBuffer(sizeToRead); | |
157 const size_t bytesRead = fStream->read(tempBuffer.get(), sizeToRead); | |
158 if (bytesRead != sizeToRead) { | |
159 return false; | |
160 } | |
161 return fStreamBuffer.write(tempBuffer.get(), bytesRead); | |
162 } | |
163 | |
164 SkAutoTDelete<SkStream> fStream; | |
165 bool fWholeStreamRead; | |
166 | |
167 SkDynamicMemoryWStream fStreamBuffer; | |
168 | |
169 const size_t kReadToEnd = 0; | |
170 }; | |
171 | |
172 class SkDngImage { | |
173 public: | |
174 static SkDngImage* NewFromStream(SkRawStream* stream) { | |
175 SkAutoTDelete<dng_host> host(new dng_host); | |
176 SkAutoTDelete<dng_info> info(new dng_info); | |
177 SkAutoTDelete<dng_negative> negative(SkDngImage::ReadDng(stream, host.ge t(), info.get())); | |
178 if (!negative) { | |
179 return nullptr; | |
180 } | |
181 return new SkDngImage(stream, host.release(), info.release(), negative.r elease()); | |
182 } | |
183 | |
184 /* | |
185 * Renders the DNG image to the size. | |
186 * The rendered image will be close to the specified size, but there is no g uarantee that any | |
scroggo
2016/01/14 15:26:38
So long as the specified size is greater than 128
yujieqin
2016/01/15 14:49:31
We update the comment, but we did not add a bug fo
| |
187 * of the edges will match the requested size. E.g. | |
188 * 100% size: 4000 x 3000 | |
189 * requested size: 1600 x 1200 | |
190 * returned size could be: 2000 x 1500 | |
191 */ | |
192 dng_image* render(int width, int height) { | |
193 // render() takes the ownership of fHost, fInfo and fNegative when avail able. | |
scroggo
2016/01/14 15:26:38
nit: takes ownership* (no need for "the" here)
yujieqin
2016/01/15 14:49:31
Done.
yujieqin
2016/01/15 14:49:31
Done.
| |
194 SkAutoTDelete<dng_host> host; | |
195 SkAutoTDelete<dng_info> info; | |
196 SkAutoTDelete<dng_negative> negative; | |
197 if (!fHost || !fInfo || !fNegative) { | |
198 SkCodecPrintf("Warning: SkDngImage::render() is called multiple time s."); | |
199 host.reset(new dng_host); | |
200 info.reset(new dng_info); | |
201 negative.reset(SkDngImage::ReadDng(fStream.get(), host.get(), info.g et())); | |
202 if (!negative) { | |
203 return nullptr; | |
204 } | |
205 } else { | |
206 host.reset(fHost.release()); | |
207 info.reset(fInfo.release()); | |
208 negative.reset(fNegative.release()); | |
209 } | |
210 | |
211 // DNG SDK preserves the aspect ratio, so it only needs to know the long er dimension. | |
212 const int preferredSize = SkTMax(width, height); | |
213 try { | |
214 host->SetPreferredSize(preferredSize); | |
215 host->ValidateSizes(); | |
216 | |
217 negative->ReadStage1Image(*host, *fStream, *info); | |
218 | |
219 if (info->fMaskIndex != -1) { | |
220 negative->ReadTransparencyMask(*host, *fStream, *info); | |
221 } | |
222 | |
223 negative->ValidateRawImageDigest(*host); | |
224 if (negative->IsDamaged()) { | |
225 return nullptr; | |
226 } | |
227 | |
228 const int32 kMosaicPlane = -1; | |
229 negative->BuildStage2Image(*host); | |
230 negative->BuildStage3Image(*host, kMosaicPlane); | |
231 | |
232 dng_render render(*host, *negative); | |
233 render.SetFinalSpace(dng_space_sRGB::Get()); | |
234 render.SetFinalPixelType(ttByte); | |
235 | |
236 dng_point stage3_size = negative->Stage3Image()->Size(); | |
237 render.SetMaximumSize(SkTMax(stage3_size.h, stage3_size.v)); | |
238 | |
239 return render.Render(); | |
240 } catch (...) { | |
241 return nullptr; | |
242 } | |
243 } | |
244 | |
245 SkImageInfo getImageInfo() const { | |
scroggo
2016/01/14 15:26:38
nit: You can make this return const SkImageInfo& t
yujieqin
2016/01/15 14:49:31
Done.
| |
246 return fImageInfo; | |
247 } | |
248 | |
249 private: | |
250 static dng_negative* ReadDng(SkRawStream* stream, dng_host* host, dng_info* info) { | |
251 try { | |
252 host->ValidateSizes(); | |
253 info->Parse(*host, *stream); | |
254 info->PostParse(*host); | |
255 if (!info->IsValidDNG()) { | |
256 return nullptr; | |
257 } | |
258 | |
259 SkAutoTDelete<dng_negative> negative; | |
260 negative.reset(host->Make_dng_negative()); | |
261 negative->Parse(*host, *stream, *info); | |
262 negative->PostParse(*host, *stream, *info); | |
263 negative->SynchronizeMetadata(); | |
264 return negative.release(); | |
265 } catch (...) { | |
266 return nullptr; | |
267 } | |
268 } | |
269 | |
270 SkDngImage(SkRawStream* stream, dng_host* host, dng_info* info, dng_negative * negative) | |
271 : fStream(stream) | |
272 , fHost(host) | |
273 , fInfo(info) | |
274 , fNegative(negative) | |
275 , fImageInfo(SkImageInfo::Make(fNegative->DefaultCropSizeH().As_real64() , | |
276 fNegative->DefaultCropSizeV().As_real64() , | |
277 kN32_SkColorType, kOpaque_SkAlphaType)) { } | |
278 | |
279 SkAutoTDelete<SkRawStream> fStream; | |
280 SkAutoTDelete<dng_host> fHost; | |
281 SkAutoTDelete<dng_info> fInfo; | |
282 SkAutoTDelete<dng_negative> fNegative; | |
283 | |
284 const SkImageInfo fImageInfo; | |
285 }; | |
286 | |
287 /* | |
288 * Tries to handle the image with PIEX. If PIEX returns kOk and finds the previe w image, create a | |
289 * SkJpegCodec. If PIEX returns kFail, then the file is invalid, return nullptr. In other cases, | |
290 * fallback to create SkRawCodec for DNG images. | |
291 */ | |
292 SkCodec* SkRawCodec::NewFromStream(SkStream* stream) { | |
293 SkAutoTDelete<SkRawStream> rawStream(new SkRawStream(stream)); | |
294 ::piex::PreviewImageData imageData; | |
295 if (::piex::IsRaw(rawStream.get())) { | |
296 ::piex::Error error = ::piex::GetPreviewImageData(rawStream.get(), &imag eData); | |
297 | |
298 if (error == ::piex::Error::kOk && imageData.preview_length > 0) { | |
299 #if !defined(GOOGLE3) | |
300 // copyBuffer() is destructive to the rawStream. Abandon the rawStre am after this | |
301 // function call. | |
302 SkMemoryStream* memoryStream = | |
303 rawStream->copyBuffer(imageData.preview_offset, imageData.pr eview_length); | |
304 return memoryStream ? SkJpegCodec::NewFromStream(memoryStream) : nul lptr; | |
305 #else | |
306 return nullptr; | |
307 #endif | |
308 } else if (error == ::piex::Error::kFail) { | |
309 return nullptr; | |
310 } | |
311 } | |
312 | |
313 SkAutoTDelete<SkDngImage> dngImage(SkDngImage::NewFromStream(rawStream.relea se())); | |
314 if (!dngImage) { | |
315 return nullptr; | |
316 } | |
317 | |
318 return new SkRawCodec(dngImage.release()); | |
319 } | |
320 | |
321 SkCodec::Result SkRawCodec::onGetPixels(const SkImageInfo& requestedInfo, void* dst, | |
scroggo
2016/01/14 15:26:38
This function needs to check to see that the reque
yujieqin
2016/01/15 14:49:30
The DNG SDK returns RGB 8bit data. Currently we ar
scroggo
2016/01/19 21:05:44
The check is to early exit. Yes, the SkSwizzler do
yujieqin
2016/01/20 12:53:03
I see your point. Added the conversion_possible()
scroggo
2016/01/20 18:13:35
With conversion_possible exiting early, you may no
yujieqin
2016/01/20 21:42:19
From my test with DM, this is still necessary, bec
| |
322 size_t dstRowBytes, const Options& optio ns, | |
323 SkPMColor ctable[], int* ctableCount, | |
324 int* rowsDecoded) { | |
325 int width = requestedInfo.width(); | |
326 int height = requestedInfo.height(); | |
327 SkAutoTDelete<dng_image> image(fDngImage->render(width, height)); | |
328 if (!image) { | |
329 return kInvalidInput; | |
330 } | |
331 | |
332 // Because the DNG SDK can not gaurantee to render to requested size, we all ow a small | |
scroggo
2016/01/14 15:26:38
guarantee*
yujieqin
2016/01/15 14:49:30
Done.
| |
333 // difference. Only the overlapping region will be copied in onGetPixels(). | |
scroggo
2016/01/14 15:26:38
nit: It's not really a copy - it looks like it's a
yujieqin
2016/01/15 14:49:30
It seems like we can not avoid these two step conv
scroggo
2016/01/19 21:05:44
It's fine if we cannot avoid the two step conversi
yujieqin
2016/01/20 12:53:04
AFAIK,
For option 1, it is not possible due to th
| |
334 const float maxDiffRatio = 1.02f; | |
335 const dng_point& imageSize = image->Size(); | |
336 if (SkTMax(width, imageSize.h) / SkTMin(width, imageSize.h) > maxDiffRatio | | | |
msarett
2016/01/14 19:02:14
If width is greater than imageSize.h and/or height
yujieqin
2016/01/15 14:49:30
Changed logic, we don't have this case anymore.
| |
337 SkTMax(height, imageSize.v) / SkTMin(height, imageSize.v) > maxDiffRatio ) { | |
338 return SkCodec::kInvalidScale; | |
339 } | |
340 | |
341 // Only copy the top-left of the overlapping region. | |
scroggo
2016/01/14 15:26:38
Again, I think "copy" is misleading here.
yujieqin
2016/01/15 14:49:30
we don't need this anymore.
| |
342 width = SkTMin(width, imageSize.h); | |
343 height = SkTMin(height, imageSize.v); | |
344 | |
345 void* dstRow = dst; | |
346 uint8_t srcRow[width * 3]; | |
347 SkAutoTDelete<SkSwizzler> swizzler(SkSwizzler::CreateSwizzler( | |
348 SkSwizzler::kRGB, nullptr, requestedInfo, options)); | |
349 SkASSERT(swizzler); | |
350 | |
351 dng_pixel_buffer buffer; | |
352 buffer.fData = &srcRow[0]; | |
353 buffer.fPlane = 0; | |
354 buffer.fPlanes = 3; | |
355 buffer.fColStep = buffer.fPlanes; | |
356 buffer.fPlaneStep = 1; | |
357 buffer.fPixelType = ttByte; | |
358 buffer.fPixelSize = sizeof(uint8_t); | |
359 buffer.fRowStep = sizeof(srcRow); | |
360 | |
361 for (int i = 0; i < height; ++i) { | |
362 buffer.fArea = dng_rect(i, 0, i + 1, width); | |
363 | |
364 try { | |
365 image->Get(buffer, dng_image::edge_zero); | |
366 } catch (...) { | |
367 *rowsDecoded = i; | |
368 return kIncompleteInput; | |
369 } | |
370 | |
371 swizzler->swizzle(dstRow, &srcRow[0]); | |
372 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes); | |
373 } | |
374 return kSuccess; | |
375 } | |
376 | |
377 SkISize SkRawCodec::onGetScaledDimensions(float desiredScale) const { | |
378 SkASSERT(desiredScale <= 1.f); | |
379 SkISize dim = this->getInfo().dimensions(); | |
380 const int longEdge = SkTMax(dim.fWidth, dim.fHeight); | |
381 | |
382 // Limits the minimun size to be 128 on the long edge. | |
383 if (desiredScale < 128.f / static_cast<float>(longEdge)) { | |
384 desiredScale = 128.f / static_cast<float>(longEdge); | |
385 } | |
386 | |
387 const float finalScale = (int)(1.f/ desiredScale); | |
388 | |
389 dim.fWidth = std::round(dim.fWidth / finalScale); | |
390 dim.fHeight = std::round(dim.fHeight / finalScale); | |
391 return dim; | |
392 } | |
393 | |
394 bool SkRawCodec::onDimensionsSupported(const SkISize& dim) { | |
395 const SkISize fullDim = this->getInfo().dimensions(); | |
396 const float fullLongEdge = SkTMax(fullDim.fWidth, fullDim.fHeight); | |
397 const float longEdge = SkTMax(dim.fWidth, dim.fHeight); | |
398 | |
399 SkISize sizeFloor = this->onGetScaledDimensions(1.f / std::floor(fullLongEdg e / longEdge)); | |
400 SkISize sizeCeil = this->onGetScaledDimensions(1.f / std::ceil(fullLongEdge / longEdge)); | |
401 return sizeFloor == dim || sizeCeil == dim; | |
402 } | |
403 | |
404 SkRawCodec::~SkRawCodec() {} | |
405 | |
406 SkRawCodec::SkRawCodec(SkDngImage* dngImage) | |
407 : INHERITED(dngImage->getImageInfo(), nullptr) | |
408 , fDngImage(dngImage) {} | |
OLD | NEW |