OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 /* | 8 /* |
9 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. | 9 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. |
10 * | 10 * |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 return result; | 66 return result; |
67 } | 67 } |
68 | 68 |
69 /* | 69 /* |
70 * Assumes IsGif was called and returned true | 70 * Assumes IsGif was called and returned true |
71 * Creates a gif decoder | 71 * Creates a gif decoder |
72 * Reads enough of the stream to determine the image format | 72 * Reads enough of the stream to determine the image format |
73 */ | 73 */ |
74 SkCodec* SkGifCodec::NewFromStream(SkStream* stream) { | 74 SkCodec* SkGifCodec::NewFromStream(SkStream* stream) { |
75 std::unique_ptr<SkGifImageReader> reader(new SkGifImageReader(stream)); | 75 std::unique_ptr<SkGifImageReader> reader(new SkGifImageReader(stream)); |
76 if (!reader->parse(SkGifImageReader::GIFSizeQuery)) { | 76 if (!reader->parse(SkGifImageReader::SkGIFSizeQuery)) { |
77 // Not enough data to determine the size. | 77 // Not enough data to determine the size. |
78 return nullptr; | 78 return nullptr; |
79 } | 79 } |
80 | 80 |
81 if (0 == reader->screenWidth() || 0 == reader->screenHeight()) { | 81 if (0 == reader->screenWidth() || 0 == reader->screenHeight()) { |
82 return nullptr; | 82 return nullptr; |
83 } | 83 } |
84 | 84 |
85 const auto alpha = reader->firstFrameHasAlpha() ? SkEncodedInfo::kBinary_Alp
ha | 85 const auto alpha = reader->firstFrameHasAlpha() ? SkEncodedInfo::kBinary_Alp
ha |
86 : SkEncodedInfo::kOpaque_Alp
ha; | 86 : SkEncodedInfo::kOpaque_Alp
ha; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 , fFilledBackground(false) | 120 , fFilledBackground(false) |
121 , fFirstCallToIncrementalDecode(false) | 121 , fFirstCallToIncrementalDecode(false) |
122 , fDst(nullptr) | 122 , fDst(nullptr) |
123 , fDstRowBytes(0) | 123 , fDstRowBytes(0) |
124 , fRowsDecoded(0) | 124 , fRowsDecoded(0) |
125 { | 125 { |
126 reader->setClient(this); | 126 reader->setClient(this); |
127 } | 127 } |
128 | 128 |
129 std::vector<SkCodec::FrameInfo> SkGifCodec::onGetFrameInfo() { | 129 std::vector<SkCodec::FrameInfo> SkGifCodec::onGetFrameInfo() { |
130 fReader->parse(SkGifImageReader::GIFFrameCountQuery); | 130 fReader->parse(SkGifImageReader::SkGIFFrameCountQuery); |
131 const size_t size = fReader->imagesCount(); | 131 const size_t size = fReader->imagesCount(); |
132 std::vector<FrameInfo> result(size); | 132 std::vector<FrameInfo> result(size); |
133 for (size_t i = 0; i < size; i++) { | 133 for (size_t i = 0; i < size; i++) { |
134 const GIFFrameContext* frameContext = fReader->frameContext(i); | 134 const SkGIFFrameContext* frameContext = fReader->frameContext(i); |
135 result[i].fDuration = frameContext->delayTime(); | 135 result[i].fDuration = frameContext->delayTime(); |
136 result[i].fRequiredFrame = frameContext->getRequiredFrame(); | 136 result[i].fRequiredFrame = frameContext->getRequiredFrame(); |
137 } | 137 } |
138 return result; | 138 return result; |
139 } | 139 } |
140 | 140 |
141 void SkGifCodec::initializeColorTable(const SkImageInfo& dstInfo, size_t frameIn
dex, | 141 void SkGifCodec::initializeColorTable(const SkImageInfo& dstInfo, size_t frameIn
dex, |
142 SkPMColor* inputColorPtr, int* inputColorCount) { | 142 SkPMColor* inputColorPtr, int* inputColorCount) { |
143 fCurrColorTable = fReader->getColorTable(dstInfo.colorType(), frameIndex); | 143 fCurrColorTable = fReader->getColorTable(dstInfo.colorType(), frameIndex); |
144 fCurrColorTableIsReal = fCurrColorTable; | 144 fCurrColorTableIsReal = fCurrColorTable; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 // FIXME: It is possible that a later frame can be decoded to index8, if
it does one of the | 177 // FIXME: It is possible that a later frame can be decoded to index8, if
it does one of the |
178 // following: | 178 // following: |
179 // - Covers the entire previous frame | 179 // - Covers the entire previous frame |
180 // - Shares a color table (and transparent index) with any prior frames
that are showing. | 180 // - Shares a color table (and transparent index) with any prior frames
that are showing. |
181 // We must support index8 for the first frame to be backwards compatible
on Android, but | 181 // We must support index8 for the first frame to be backwards compatible
on Android, but |
182 // we do not (currently) need to support later frames as index8. | 182 // we do not (currently) need to support later frames as index8. |
183 return gif_error("Cannot decode multiframe gif (except frame 0) as index
8.\n", | 183 return gif_error("Cannot decode multiframe gif (except frame 0) as index
8.\n", |
184 kInvalidConversion); | 184 kInvalidConversion); |
185 } | 185 } |
186 | 186 |
187 fReader->parse((SkGifImageReader::GIFParseQuery) frameIndex); | 187 fReader->parse((SkGifImageReader::SkGIFParseQuery) frameIndex); |
188 | 188 |
189 if (frameIndex >= fReader->imagesCount()) { | 189 if (frameIndex >= fReader->imagesCount()) { |
190 return gif_error("frame index out of range!\n", kIncompleteInput); | 190 return gif_error("frame index out of range!\n", kIncompleteInput); |
191 } | 191 } |
192 | 192 |
193 fTmpBuffer.reset(new uint8_t[dstInfo.minRowBytes()]); | 193 fTmpBuffer.reset(new uint8_t[dstInfo.minRowBytes()]); |
194 | 194 |
195 // Initialize color table and copy to the client if necessary | 195 // Initialize color table and copy to the client if necessary |
196 this->initializeColorTable(dstInfo, frameIndex, inputColorPtr, inputColorCou
nt); | 196 this->initializeColorTable(dstInfo, frameIndex, inputColorPtr, inputColorCou
nt); |
197 this->initializeSwizzler(dstInfo, frameIndex); | 197 this->initializeSwizzler(dstInfo, frameIndex); |
198 return kSuccess; | 198 return kSuccess; |
199 } | 199 } |
200 | 200 |
201 void SkGifCodec::initializeSwizzler(const SkImageInfo& dstInfo, size_t frameInde
x) { | 201 void SkGifCodec::initializeSwizzler(const SkImageInfo& dstInfo, size_t frameInde
x) { |
202 const GIFFrameContext* frame = fReader->frameContext(frameIndex); | 202 const SkGIFFrameContext* frame = fReader->frameContext(frameIndex); |
203 // This is only called by prepareToDecode, which ensures frameIndex is in ra
nge. | 203 // This is only called by prepareToDecode, which ensures frameIndex is in ra
nge. |
204 SkASSERT(frame); | 204 SkASSERT(frame); |
205 | 205 |
206 const int xBegin = frame->xOffset(); | 206 const int xBegin = frame->xOffset(); |
207 const int xEnd = std::min(static_cast<int>(frame->xOffset() + frame->width()
), | 207 const int xEnd = std::min(static_cast<int>(frame->xOffset() + frame->width()
), |
208 static_cast<int>(fReader->screenWidth())); | 208 static_cast<int>(fReader->screenWidth())); |
209 | 209 |
210 // CreateSwizzler only reads left and right of the frame. We cannot use the
frame's raw | 210 // CreateSwizzler only reads left and right of the frame. We cannot use the
frame's raw |
211 // frameRect, since it might extend beyond the edge of the frame. | 211 // frameRect, since it might extend beyond the edge of the frame. |
212 SkIRect swizzleRect = SkIRect::MakeLTRB(xBegin, 0, xEnd, 0); | 212 SkIRect swizzleRect = SkIRect::MakeLTRB(xBegin, 0, xEnd, 0); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 | 262 |
263 fFirstCallToIncrementalDecode = true; | 263 fFirstCallToIncrementalDecode = true; |
264 | 264 |
265 return kSuccess; | 265 return kSuccess; |
266 } | 266 } |
267 | 267 |
268 SkCodec::Result SkGifCodec::onIncrementalDecode(int* rowsDecoded) { | 268 SkCodec::Result SkGifCodec::onIncrementalDecode(int* rowsDecoded) { |
269 // It is possible the client has appended more data. Parse, if needed. | 269 // It is possible the client has appended more data. Parse, if needed. |
270 const auto& options = this->options(); | 270 const auto& options = this->options(); |
271 const size_t frameIndex = options.fFrameIndex; | 271 const size_t frameIndex = options.fFrameIndex; |
272 fReader->parse((SkGifImageReader::GIFParseQuery) frameIndex); | 272 fReader->parse((SkGifImageReader::SkGIFParseQuery) frameIndex); |
273 | 273 |
274 const bool firstCallToIncrementalDecode = fFirstCallToIncrementalDecode; | 274 const bool firstCallToIncrementalDecode = fFirstCallToIncrementalDecode; |
275 fFirstCallToIncrementalDecode = false; | 275 fFirstCallToIncrementalDecode = false; |
276 return this->decodeFrame(firstCallToIncrementalDecode, options, rowsDecoded)
; | 276 return this->decodeFrame(firstCallToIncrementalDecode, options, rowsDecoded)
; |
277 } | 277 } |
278 | 278 |
279 SkCodec::Result SkGifCodec::decodeFrame(bool firstAttempt, const Options& opts,
int* rowsDecoded) { | 279 SkCodec::Result SkGifCodec::decodeFrame(bool firstAttempt, const Options& opts,
int* rowsDecoded) { |
280 const SkImageInfo& dstInfo = this->dstInfo(); | 280 const SkImageInfo& dstInfo = this->dstInfo(); |
281 const size_t frameIndex = opts.fFrameIndex; | 281 const size_t frameIndex = opts.fFrameIndex; |
282 SkASSERT(frameIndex < fReader->imagesCount()); | 282 SkASSERT(frameIndex < fReader->imagesCount()); |
283 const GIFFrameContext* frameContext = fReader->frameContext(frameIndex); | 283 const SkGIFFrameContext* frameContext = fReader->frameContext(frameIndex); |
284 if (firstAttempt) { | 284 if (firstAttempt) { |
285 // rowsDecoded reports how many rows have been initialized, so a layer a
bove | 285 // rowsDecoded reports how many rows have been initialized, so a layer a
bove |
286 // can fill the rest. In some cases, we fill the background before decod
ing | 286 // can fill the rest. In some cases, we fill the background before decod
ing |
287 // (or it is already filled for us), so we report rowsDecoded to be the
full | 287 // (or it is already filled for us), so we report rowsDecoded to be the
full |
288 // height. | 288 // height. |
289 bool filledBackground = false; | 289 bool filledBackground = false; |
290 if (frameContext->getRequiredFrame() == kNone) { | 290 if (frameContext->getRequiredFrame() == kNone) { |
291 // We may need to clear to transparent for one of the following reas
ons: | 291 // We may need to clear to transparent for one of the following reas
ons: |
292 // - The frameRect does not cover the full bounds. haveDecodedRow wi
ll | 292 // - The frameRect does not cover the full bounds. haveDecodedRow wi
ll |
293 // only draw inside the frameRect, so we need to clear the rest. | 293 // only draw inside the frameRect, so we need to clear the rest. |
294 // - There is a valid transparent pixel value. (FIXME: I'm assuming | 294 // - There is a valid transparent pixel value. (FIXME: I'm assuming |
295 // writeTransparentPixels will be false in this case, based on | 295 // writeTransparentPixels will be false in this case, based on |
296 // Chromium's assumption that it would already be zeroed. If we | 296 // Chromium's assumption that it would already be zeroed. If we |
297 // change that behavior, could we skip Filling here?) | 297 // change that behavior, could we skip Filling here?) |
298 // - The frame is interlaced. There is no obvious way to fill | 298 // - The frame is interlaced. There is no obvious way to fill |
299 // afterwards for an incomplete image. (FIXME: Does the first pass | 299 // afterwards for an incomplete image. (FIXME: Does the first pass |
300 // cover all rows? If so, we do not have to fill here.) | 300 // cover all rows? If so, we do not have to fill here.) |
301 if (frameContext->frameRect() != this->getInfo().bounds() | 301 if (frameContext->frameRect() != this->getInfo().bounds() |
302 || frameContext->transparentPixel() < MAX_COLORS | 302 || frameContext->transparentPixel() < SK_MAX_COLORS |
303 || frameContext->interlaced()) { | 303 || frameContext->interlaced()) { |
304 // fill ignores the width (replaces it with the actual, scaled w
idth). | 304 // fill ignores the width (replaces it with the actual, scaled w
idth). |
305 // But we need to scale in Y. | 305 // But we need to scale in Y. |
306 const int scaledHeight = get_scaled_dimension(dstInfo.height(), | 306 const int scaledHeight = get_scaled_dimension(dstInfo.height(), |
307 fSwizzler->sampleY
()); | 307 fSwizzler->sampleY
()); |
308 auto fillInfo = dstInfo.makeWH(0, scaledHeight); | 308 auto fillInfo = dstInfo.makeWH(0, scaledHeight); |
309 fSwizzler->fill(fillInfo, fDst, fDstRowBytes, this->getFillValue
(dstInfo), | 309 fSwizzler->fill(fillInfo, fDst, fDstRowBytes, this->getFillValue
(dstInfo), |
310 opts.fZeroInitialized); | 310 opts.fZeroInitialized); |
311 filledBackground = true; | 311 filledBackground = true; |
312 } | 312 } |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 // which ignores the background color. | 399 // which ignores the background color. |
400 // If the colorType is kIndex_8, and there was no color table (i.e. | 400 // If the colorType is kIndex_8, and there was no color table (i.e. |
401 // fCurrColorTableIsReal is false), this value (zero) corresponds to the | 401 // fCurrColorTableIsReal is false), this value (zero) corresponds to the |
402 // only entry in the dummy color table provided to the client. | 402 // only entry in the dummy color table provided to the client. |
403 return SK_ColorTRANSPARENT; | 403 return SK_ColorTRANSPARENT; |
404 } | 404 } |
405 | 405 |
406 bool SkGifCodec::haveDecodedRow(size_t frameIndex, const unsigned char* rowBegin
, | 406 bool SkGifCodec::haveDecodedRow(size_t frameIndex, const unsigned char* rowBegin
, |
407 size_t rowNumber, unsigned repeatCount, bool wri
teTransparentPixels) | 407 size_t rowNumber, unsigned repeatCount, bool wri
teTransparentPixels) |
408 { | 408 { |
409 const GIFFrameContext* frameContext = fReader->frameContext(frameIndex); | 409 const SkGIFFrameContext* frameContext = fReader->frameContext(frameIndex); |
410 // The pixel data and coordinates supplied to us are relative to the frame's | 410 // The pixel data and coordinates supplied to us are relative to the frame's |
411 // origin within the entire image size, i.e. | 411 // origin within the entire image size, i.e. |
412 // (frameContext->xOffset, frameContext->yOffset). There is no guarantee | 412 // (frameContext->xOffset, frameContext->yOffset). There is no guarantee |
413 // that width == (size().width() - frameContext->xOffset), so | 413 // that width == (size().width() - frameContext->xOffset), so |
414 // we must ensure we don't run off the end of either the source data or the | 414 // we must ensure we don't run off the end of either the source data or the |
415 // row's X-coordinates. | 415 // row's X-coordinates. |
416 const size_t width = frameContext->width(); | 416 const size_t width = frameContext->width(); |
417 const int xBegin = frameContext->xOffset(); | 417 const int xBegin = frameContext->xOffset(); |
418 const int yBegin = frameContext->yOffset() + rowNumber; | 418 const int yBegin = frameContext->yOffset() + rowNumber; |
419 const int xEnd = std::min(static_cast<int>(frameContext->xOffset() + width), | 419 const int xEnd = std::min(static_cast<int>(frameContext->xOffset() + width), |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 void* copiedLine = SkTAddOffset<void>(dstLine, fSwizzler->swizzleOffsetB
ytes()); | 554 void* copiedLine = SkTAddOffset<void>(dstLine, fSwizzler->swizzleOffsetB
ytes()); |
555 void* dst = copiedLine; | 555 void* dst = copiedLine; |
556 for (unsigned i = 1; i < repeatCount; i++) { | 556 for (unsigned i = 1; i < repeatCount; i++) { |
557 dst = SkTAddOffset<void>(dst, fDstRowBytes); | 557 dst = SkTAddOffset<void>(dst, fDstRowBytes); |
558 memcpy(dst, copiedLine, bytesToCopy); | 558 memcpy(dst, copiedLine, bytesToCopy); |
559 } | 559 } |
560 } | 560 } |
561 | 561 |
562 return true; | 562 return true; |
563 } | 563 } |
OLD | NEW |