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 "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> | |
33 | |
34 namespace { | |
35 | |
36 // T must be unsigned type. | |
37 template <class T> | |
38 bool SafeAddToSizeT(T arg1, T arg2, size_t* result) { | |
scroggo
2016/01/07 20:57:42
In Skia, we name static functions (or those in ano
yujieqin
2016/01/08 14:00:12
Done.
| |
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* ReadDng(dng_stream* stream, dng_host* host, dng_info* info) { | |
scroggo
2016/01/07 20:57:43
This should be read_dng
yujieqin
2016/01/08 14:00:12
Done.
| |
52 const uint32 kPreferredSize = 0; | |
53 const uint32 kMinimumSize = 0; | |
54 const uint32 kMaximumSize = 0; | |
55 SkAutoTDelete<dng_negative> negative; | |
56 try { | |
57 host->SetPreferredSize(kPreferredSize); | |
58 host->SetMinimumSize(kMinimumSize); | |
59 host->SetMaximumSize(kMaximumSize); | |
60 | |
61 host->ValidateSizes(); | |
62 | |
63 // Read stage 1 image into the negative. | |
64 { | |
65 info->Parse(*host, *stream); | |
66 info->PostParse(*host); | |
67 | |
68 if (!info->IsValidDNG()) { | |
69 return nullptr; | |
70 } | |
71 | |
72 negative.reset(host->Make_dng_negative()); | |
73 negative->Parse(*host, *stream, *info); | |
74 negative->PostParse(*host, *stream, *info); | |
75 } | |
76 | |
77 negative->SynchronizeMetadata(); | |
78 } catch (...) { | |
79 return nullptr; | |
80 } | |
81 | |
82 return negative.release(); | |
83 } | |
84 | |
85 bool PrepareStage3(dng_stream* stream, dng_host* host, dng_info* info, | |
scroggo
2016/01/07 20:57:42
prepare_stage_3
yujieqin
2016/01/08 14:00:12
Done.
| |
86 dng_negative* negative) { | |
87 if (negative->Stage3Image() == nullptr) { | |
88 negative->ReadStage1Image(*host, *stream, *info); | |
89 | |
90 if (info->fMaskIndex != -1) { | |
91 negative->ReadTransparencyMask(*host, *stream, *info); | |
92 } | |
93 | |
94 negative->ValidateRawImageDigest(*host); | |
95 if (negative->IsDamaged()) { | |
96 return false; | |
97 } | |
98 | |
99 const int32 kMosaicPlane = -1; | |
100 negative->BuildStage2Image(*host); | |
101 negative->BuildStage3Image(*host, kMosaicPlane); | |
102 } | |
103 | |
104 return true; | |
105 } | |
106 | |
107 } // namespace | |
108 | |
109 // Note: this class could throw exception if it is used as dng_stream. | |
110 class SkRawStream : public dng_stream, public ::piex::StreamInterface { | |
scroggo
2016/01/07 20:57:42
We usually avoid multiple inheritance in Skia (see
yujieqin
2016/01/08 14:00:12
Both dng_stream and ::piex::StreamInterface are in
| |
111 public: | |
112 // Note that this call will take the ownership of stream. | |
113 explicit SkRawStream(SkStream* stream) | |
114 : fStream(stream), fWholeStreamRead(false), fStreamBufferSize(0) | |
115 // Arbitrary buffer size. | |
116 #if defined(GOOGLE3) | |
117 // Stack frame size is limited in GOOGLE3. | |
118 , fReadBufferSize(8 * 1024) {} // 8KB | |
119 #else | |
120 , fReadBufferSize(256 * 1024) {} // 256KB | |
121 #endif | |
122 | |
123 ~SkRawStream() override {} | |
124 | |
125 // Creates a SkMemoryStream from the offset with size. | |
126 SkMemoryStream* CopyBuffer(size_t offset, size_t size) { | |
scroggo
2016/01/07 20:57:42
style: (non-static) member functions are named lik
yujieqin
2016/01/08 14:00:12
Done.
| |
127 size_t sum; | |
128 if (!SafeAddToSizeT(offset, size, &sum) || | |
129 !IncreaseStreamBufferSize(sum)) { | |
130 return nullptr; | |
131 } | |
132 return new SkMemoryStream( | |
133 reinterpret_cast<const uint8*>(fStreamBuffer.get()) + offset, si ze, true); | |
scroggo
2016/01/07 20:57:42
You can get rid of these casts by making fStreamBu
yujieqin
2016/01/08 14:00:12
Nice, thanks! :)
| |
134 } | |
135 | |
136 // For PIEX | |
137 ::piex::Error GetData(const size_t offset, const size_t length, | |
138 uint8* data) override { | |
139 size_t sum; | |
140 if (!SafeAddToSizeT(offset, length, &sum) || | |
141 !IncreaseStreamBufferSize(sum)) { | |
142 return ::piex::Error::kFail; | |
143 } | |
144 memcpy(data, reinterpret_cast<const uint8*>(fStreamBuffer.get()) + offse t, length); | |
145 return ::piex::Error::kOk; | |
146 } | |
147 | |
148 protected: | |
149 // For dng_stream | |
150 uint64 DoGetLength() override { | |
151 if (!IncreaseStreamBufferSize(0, true)) { // read whole stream | |
152 ThrowReadFile(); | |
153 } | |
154 return fStreamBufferSize; | |
155 } | |
156 | |
157 // For dng_stream | |
158 void DoRead(void* data, uint32 count, uint64 offset) override { | |
159 size_t sum; | |
160 if (!SafeAddToSizeT(static_cast<uint64>(count), offset, &sum) || | |
161 !IncreaseStreamBufferSize(sum)) { | |
162 ThrowReadFile(); | |
163 } | |
164 memcpy(data, reinterpret_cast<const uint8*>(fStreamBuffer.get()) + offse t, count); | |
165 } | |
166 | |
167 private: | |
168 bool IncreaseStreamBufferSize(size_t size, bool readToEnd = false) { | |
scroggo
2016/01/07 20:57:42
style: increaseStreamBufferSize
Also, I find this
yujieqin
2016/01/08 14:00:12
Done.
| |
169 if (!readToEnd) { | |
scroggo
2016/01/07 20:57:43
nit: I find it weird to say:
if (!condition) {
yujieqin
2016/01/08 14:00:12
Done.
| |
170 if (size <= fStreamBufferSize) { | |
171 return true; | |
172 } else if (fWholeStreamRead) { // request more than available. | |
scroggo
2016/01/07 20:57:43
no need for "else" here. We would have returned if
yujieqin
2016/01/08 14:00:12
Done.
| |
173 return false; | |
174 } | |
175 } else { | |
176 if (fWholeStreamRead) { | |
scroggo
2016/01/07 20:57:43
It seems weird to me that this is different depend
yujieqin
2016/01/08 14:00:12
Yes, if readToEnd is "true", the newSize will be b
| |
177 return true; | |
178 } | |
179 } | |
180 | |
181 const size_t sizeToRead = readToEnd ? 0 : size - fStreamBufferSize; | |
182 const size_t readBufferSize = | |
183 readToEnd ? fReadBufferSize : std::min(fReadBufferSize, sizeToRe ad); | |
scroggo
2016/01/07 20:57:42
nit: SkTMin
yujieqin
2016/01/08 14:00:12
Done.
| |
184 | |
185 char buffer[readBufferSize]; | |
186 size_t sizeRead = 0; | |
187 SkDynamicMemoryWStream tempStream; | |
scroggo
2016/01/07 20:57:42
I think this code is made more complex by the fact
yujieqin
2016/01/08 14:00:12
Done.
| |
188 do { | |
189 size_t bytesRead = fStream->read(buffer, readBufferSize); | |
190 tempStream.write(buffer, bytesRead); | |
191 sizeRead = tempStream.bytesWritten(); | |
192 } while (!fStream->isAtEnd() && | |
193 (readToEnd || sizeRead < sizeToRead)); | |
194 if (sizeRead < sizeToRead) { | |
195 return false; | |
196 } | |
197 | |
198 const size_t newStreamBufferSize = fStreamBufferSize + sizeRead; | |
199 SkAutoTDelete<uint8> oldBufferPtr(reinterpret_cast<uint8*>(fStreamBuffer .detach())); | |
200 void* newBufferPtr = fStreamBuffer.reset(newStreamBufferSize); | |
201 | |
202 memcpy(reinterpret_cast<uint8*>(newBufferPtr), oldBufferPtr.get(), fStre amBufferSize); | |
203 tempStream.copyTo(reinterpret_cast<uint8*>(newBufferPtr) + fStreamBuffer Size); | |
204 fStreamBufferSize = newStreamBufferSize; | |
205 fWholeStreamRead = fStream->isAtEnd(); | |
206 return true; | |
207 } | |
208 | |
209 SkAutoTDelete<SkStream> fStream; | |
210 bool fWholeStreamRead; | |
211 | |
212 SkAutoMalloc fStreamBuffer; | |
213 size_t fStreamBufferSize; | |
214 | |
215 const size_t fReadBufferSize; | |
216 }; | |
217 | |
218 SkCodec* SkRawCodec::NewFromStream(SkStream* stream) { | |
219 SkAutoTDelete<SkRawStream> rawStream(new SkRawStream(stream)); | |
220 ::piex::PreviewImageData imageData; | |
221 if (::piex::IsRaw(rawStream.get()) && | |
222 ::piex::GetPreviewImageData(rawStream.get(), &imageData) == ::piex::Erro r::kOk) | |
223 { | |
224 SkMemoryStream* memoryStream = | |
225 rawStream->CopyBuffer(imageData.jpeg_offset, imageData.jpeg_leng th); | |
226 return memoryStream ? SkJpegCodec::NewFromStream(memoryStream) : nullptr ; | |
227 } | |
228 | |
229 dng_host host; | |
230 dng_info info; | |
231 SkAutoTDelete<dng_negative> negative(ReadDng(rawStream.get(), &host, &info)) ; | |
232 if (!negative) { | |
233 return nullptr; | |
234 } | |
235 | |
236 const SkImageInfo& imageInfo = SkImageInfo::Make( | |
237 negative->DefaultCropSizeH().As_real64(), | |
238 negative->DefaultCropSizeV().As_real64(), | |
239 kN32_SkColorType, kOpaque_SkAlphaType); | |
240 return new SkRawCodec(imageInfo, rawStream.detach()); | |
241 } | |
242 | |
243 SkCodec::Result SkRawCodec::onGetPixels(const SkImageInfo& requestedInfo, void* dst, | |
244 size_t dstRowBytes, const Options& optio ns, | |
245 SkPMColor ctable[], int* ctableCount, | |
246 int* rowsDecoded) { | |
247 const int width = requestedInfo.width(); | |
248 const int height = requestedInfo.height(); | |
249 SkAutoTDelete<dng_image> finalDngImage; | |
250 try { | |
251 dng_host host; | |
252 dng_info info; | |
253 SkAutoTDelete<dng_negative> negative(ReadDng(fRawStream, &host, &info)); | |
254 if (!negative || !PrepareStage3(fRawStream, &host, &info, negative.get() )) { | |
255 return kInvalidInput; | |
256 } | |
257 | |
258 dng_render render(host, *negative); | |
259 render.SetFinalSpace(dng_space_sRGB::Get()); | |
260 render.SetFinalPixelType(ttByte); | |
261 | |
262 finalDngImage.reset(render.Render()); | |
263 } catch (...) { | |
264 return kInvalidInput; | |
265 } | |
266 | |
267 void* dstRow = dst; | |
268 uint8 srcRow[width * 3]; | |
269 SkAutoTDelete<SkSwizzler> swizzler(SkSwizzler::CreateSwizzler( | |
270 SkSwizzler::kRGB, nullptr, requestedInfo, options)); | |
271 SkASSERT(swizzler); | |
272 for (int i = 0; i < height; ++i) { | |
273 dng_pixel_buffer buffer; | |
274 buffer.fData = &srcRow[0]; | |
275 buffer.fArea = dng_rect(i, 0, i + 1, width); | |
276 buffer.fPlane = 0; | |
277 buffer.fPlanes = 3; | |
278 buffer.fColStep = buffer.fPlanes; | |
279 buffer.fPlaneStep = 1; | |
280 buffer.fPixelType = ttByte; | |
281 buffer.fPixelSize = sizeof(uint8); | |
282 buffer.fRowStep = sizeof(srcRow); | |
283 | |
284 try { | |
285 finalDngImage->Get(buffer, dng_image::edge_zero); | |
286 } catch (...) { | |
287 return kInvalidInput; | |
288 } | |
289 | |
290 swizzler->swizzle(dstRow, &srcRow[0]); | |
291 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes); | |
292 } | |
293 return kSuccess; | |
294 } | |
295 | |
296 SkRawCodec::~SkRawCodec() { | |
297 if (fRawStream) { | |
scroggo
2016/01/07 20:57:42
I think it's safe to delete fRawStream even if it'
yujieqin
2016/01/08 14:00:12
Done.
| |
298 delete fRawStream; | |
299 } | |
300 } | |
301 | |
302 SkRawCodec::SkRawCodec(const SkImageInfo& srcInfo, SkRawStream* stream) | |
303 : INHERITED(srcInfo, nullptr), fRawStream(stream) {} | |
OLD | NEW |