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 "SkStream.h" | |
13 #include "SkTemplates.h" | |
14 #include "SkTypes.h" | |
15 #include "SkSwizzler.h" | |
16 | |
17 #include "dng_color_space.h" | |
18 #include "dng_exceptions.h" | |
19 #include "dng_host.h" | |
20 #include "dng_info.h" | |
21 #include "dng_render.h" | |
22 | |
23 namespace { | |
24 | |
25 class SkDngStream : public dng_stream { | |
26 public: | |
27 explicit SkDngStream(SkStream* stream) | |
28 : fData(stream->getMemoryBase()) | |
29 , fDataSize(stream->getLength()) {} | |
30 ~SkDngStream() override {} | |
31 | |
32 protected: | |
33 uint64 DoGetLength() override { return fDataSize; } | |
34 void DoRead(void* data, uint32 count, uint64 offset) override { | |
35 if (fDataSize < offset + count) { | |
36 ThrowReadFile(); | |
37 } | |
38 | |
39 memcpy(data, reinterpret_cast<const uint8*>(fData) + offset, count); | |
40 } | |
41 | |
42 private: | |
43 const void* fData; | |
44 size_t fDataSize; | |
45 }; | |
46 | |
47 bool IsDng(SkStream* data) { | |
scroggo
2015/11/24 14:46:05
Is it possible to do less work here? It appears yo
ebrauer
2016/01/07 10:07:36
To identify a valid DNG we would require this code
| |
48 try { | |
49 SkDngStream stream(data); | |
50 dng_host host; | |
51 dng_info info; | |
52 info.Parse(host, stream); | |
53 info.PostParse(host); | |
54 return info.IsValidDNG(); | |
55 } catch (...) { // Handle all kinds of exceptions as "false". | |
56 return false; | |
57 } | |
58 } | |
59 | |
60 bool ReadDng(dng_stream* stream, dng_host* host, dng_info* info, | |
61 std::unique_ptr<dng_negative>* negative) { | |
62 const uint32 kPreferredSize = 0; | |
63 const uint32 kMinimumSize = 0; | |
64 const uint32 kMaximumSize = 0; | |
65 try { | |
66 host->SetPreferredSize(kPreferredSize); | |
67 host->SetMinimumSize(kMinimumSize); | |
68 host->SetMaximumSize(kMaximumSize); | |
69 | |
70 host->ValidateSizes(); | |
71 | |
72 // Read stage 1 image into the negative. | |
73 { | |
74 info->Parse(*host, *stream); | |
75 info->PostParse(*host); | |
76 | |
77 if (!info->IsValidDNG()) { | |
78 return false; | |
79 } | |
80 | |
81 negative->reset(host->Make_dng_negative()); | |
82 (*negative)->Parse(*host, *stream, *info); | |
83 (*negative)->PostParse(*host, *stream, *info); | |
84 } | |
85 | |
86 (*negative)->SynchronizeMetadata(); | |
87 } catch (const dng_exception& exception) { | |
88 return false; | |
89 } catch (...) { | |
90 return false; | |
91 } | |
92 | |
93 return true; | |
94 } | |
95 | |
96 bool PrepareStage3(dng_stream* stream, dng_host* host, dng_info* info, | |
97 dng_negative* negative) { | |
98 if (negative->Stage3Image() == nullptr) { | |
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 false; | |
108 } | |
109 | |
110 const int32 kMosaicPlane = -1; | |
111 negative->BuildStage2Image(*host); | |
112 negative->BuildStage3Image(*host, kMosaicPlane); | |
113 } | |
114 | |
115 return true; | |
116 } | |
117 | |
118 } // namespace | |
119 | |
120 bool SkRawCodec::IsRaw(SkStream* stream) { | |
121 return IsDng(stream); | |
scroggo
2015/11/24 14:46:05
Why didn't you just put the code for IsDng here? I
ebrauer
2016/01/07 10:07:36
We are going to rework this as part of https://cod
| |
122 } | |
123 | |
124 SkCodec* SkRawCodec::NewFromStream(SkStream* data) { | |
125 SkDngStream stream(data); | |
126 dng_host host; | |
127 dng_info info; | |
128 std::unique_ptr<dng_negative> negative; | |
129 if (!ReadDng(&stream, &host, &info, &negative) && | |
130 !PrepareStage3(&stream, &host, &info, negative.get())) { | |
131 return nullptr; | |
132 } | |
133 const SkImageInfo& imageInfo = SkImageInfo::Make( | |
134 negative->DefaultCropSizeH().As_real64(), | |
135 negative->DefaultCropSizeV().As_real64(), kRGBA_8888_SkColorType, | |
msarett
2015/11/25 15:15:23
We would normally recommend kN32_SkColorType here
ebrauer
2016/01/07 10:07:36
We changed it in https://codereview.chromium.org/1
| |
136 kOpaque_SkAlphaType); | |
137 return new SkRawCodec(imageInfo, data); | |
138 } | |
139 | |
140 SkCodec::Result SkRawCodec::onGetPixels(const SkImageInfo& requestedInfo, void* dst, | |
141 size_t dstRowBytes, const Options& optio ns, | |
142 SkPMColor ctable[], int* ctableCount, | |
143 int* rowsDecoded) { | |
144 const int width = requestedInfo.width(); | |
145 const int height = requestedInfo.height(); | |
146 std::unique_ptr<dng_image> finalDngImage; | |
147 try { | |
148 SkDngStream stream(this->stream()); | |
149 dng_host host; | |
150 dng_info info; | |
151 std::unique_ptr<dng_negative> negative; | |
152 if (!ReadDng(&stream, &host, &info, &negative) || | |
153 !PrepareStage3(&stream, &host, &info, negative.get())) { | |
154 return kInvalidInput; | |
155 } | |
156 | |
157 dng_render render(host, *negative); | |
158 render.SetFinalSpace(dng_space_sRGB::Get()); | |
159 render.SetFinalPixelType(ttByte); | |
160 | |
161 finalDngImage.reset(render.Render()); | |
162 } catch (const dng_exception& exception) { | |
163 return kInvalidInput; | |
164 } catch (...) { | |
165 return kInvalidInput; | |
166 } | |
167 | |
168 void* dstRow = dst; | |
169 uint8 srcRow[width * 3]; | |
170 std::unique_ptr<SkSwizzler> swizzler(SkSwizzler::CreateSwizzler( | |
msarett
2015/11/25 15:15:23
Is the swizzler now working as you expect?
ebrauer
2016/01/07 10:07:36
It does what I expect, thanks.
| |
171 SkSwizzler::kRGB, nullptr, requestedInfo, options)); | |
172 if (!swizzler) { | |
173 return kInvalidInput; | |
174 } | |
175 for (int i = 0; i < height; ++i) { | |
176 dng_pixel_buffer buffer; | |
177 buffer.fData = &srcRow[0]; | |
178 buffer.fArea = dng_rect(i, 0, i + 1, width); | |
179 buffer.fPlane = 0; | |
180 buffer.fPlanes = 3; | |
181 buffer.fColStep = buffer.fPlanes; | |
182 buffer.fPlaneStep = 1; | |
183 buffer.fPixelType = ttByte; | |
184 buffer.fPixelSize = sizeof(uint8); | |
185 buffer.fRowStep = sizeof(srcRow); | |
186 finalDngImage->Get(buffer, dng_image::edge_zero); | |
187 swizzler->swizzle(dstRow, &srcRow[0]); | |
188 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes); | |
189 } | |
190 return kSuccess; | |
191 } | |
192 | |
193 SkRawCodec::SkRawCodec(const SkImageInfo& srcInfo, SkStream* stream) | |
194 : INHERITED(srcInfo, stream) {} | |
195 | |
OLD | NEW |