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

Side by Side Diff: src/codec/SkCodec_libgif.cpp

Issue 1058873006: Get rid of leaks in SkCodec::NewFromStream. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Add comment that ReadHeader does not own SkStream. Created 5 years, 8 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
« no previous file with comments | « src/codec/SkCodec_libbmp.cpp ('k') | src/codec/SkCodec_libpng.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "SkCodec_libgif.h" 8 #include "SkCodec_libgif.h"
9 #include "SkCodecPriv.h" 9 #include "SkCodecPriv.h"
10 #include "SkColorPriv.h" 10 #include "SkColorPriv.h"
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 // index was not found. 129 // index was not found.
130 return SK_MaxU32; 130 return SK_MaxU32;
131 } 131 }
132 132
133 /* 133 /*
134 * Assumes IsGif was called and returned true 134 * Assumes IsGif was called and returned true
135 * Creates a gif decoder 135 * Creates a gif decoder
136 * Reads enough of the stream to determine the image format 136 * Reads enough of the stream to determine the image format
137 */ 137 */
138 SkCodec* SkGifCodec::NewFromStream(SkStream* stream) { 138 SkCodec* SkGifCodec::NewFromStream(SkStream* stream) {
139 SkAutoTDelete<SkStream> streamDeleter(stream);
139 // Read gif header, logical screen descriptor, and global color table 140 // Read gif header, logical screen descriptor, and global color table
140 SkAutoTCallIProc<GifFileType, CloseGif> gif(open_gif(stream)); 141 SkAutoTCallIProc<GifFileType, CloseGif> gif(open_gif(stream));
141 142
142 if (NULL == gif) { 143 if (NULL == gif) {
143 gif_error("DGifOpen failed.\n"); 144 gif_error("DGifOpen failed.\n");
144 return NULL; 145 return NULL;
145 } 146 }
146 147
147 // Get fields from header 148 // Get fields from header
148 const int32_t width = gif->SWidth; 149 const int32_t width = gif->SWidth;
149 const int32_t height = gif->SHeight; 150 const int32_t height = gif->SHeight;
150 if (width <= 0 || height <= 0) { 151 if (width <= 0 || height <= 0) {
151 gif_error("Invalid dimensions.\n"); 152 gif_error("Invalid dimensions.\n");
152 return NULL; 153 return NULL;
153 } 154 }
154 155
155 // Return the codec 156 // Return the codec
156 // kIndex is the most natural color type for gifs, so we set this as 157 // kIndex is the most natural color type for gifs, so we set this as
157 // the default. 158 // the default.
158 // Many gifs specify a color table index for transparent pixels. Every 159 // Many gifs specify a color table index for transparent pixels. Every
159 // other pixel is guaranteed to be opaque. Despite this, because of the 160 // other pixel is guaranteed to be opaque. Despite this, because of the
160 // possiblity of transparent pixels, we cannot assume that the image is 161 // possiblity of transparent pixels, we cannot assume that the image is
161 // opaque. We have the option to set the alpha type as kPremul or 162 // opaque. We have the option to set the alpha type as kPremul or
162 // kUnpremul. Both are valid since the alpha component will always be 163 // kUnpremul. Both are valid since the alpha component will always be
163 // 0xFF or the entire 32-bit pixel will be set to zero. We prefer 164 // 0xFF or the entire 32-bit pixel will be set to zero. We prefer
164 // kPremul because we support kPremul, and it is more efficient to 165 // kPremul because we support kPremul, and it is more efficient to
165 // use kPremul directly even when kUnpremul is supported. 166 // use kPremul directly even when kUnpremul is supported.
166 const SkImageInfo& imageInfo = SkImageInfo::Make(width, height, 167 const SkImageInfo& imageInfo = SkImageInfo::Make(width, height,
167 kIndex_8_SkColorType, kPremul_SkAlphaType); 168 kIndex_8_SkColorType, kPremul_SkAlphaType);
168 return SkNEW_ARGS(SkGifCodec, (imageInfo, stream, gif.detach())); 169 return SkNEW_ARGS(SkGifCodec, (imageInfo, streamDeleter.detach(), gif.detach ()));
169 } 170 }
170 171
171 SkGifCodec::SkGifCodec(const SkImageInfo& srcInfo, SkStream* stream, 172 SkGifCodec::SkGifCodec(const SkImageInfo& srcInfo, SkStream* stream,
172 GifFileType* gif) 173 GifFileType* gif)
173 : INHERITED(srcInfo, stream) 174 : INHERITED(srcInfo, stream)
174 , fGif(gif) 175 , fGif(gif)
175 {} 176 {}
176 177
177 /* 178 /*
178 * Checks if the conversion between the input image and the requested output 179 * Checks if the conversion between the input image and the requested output
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 // giflib returns an error code if the record type is not known. 509 // giflib returns an error code if the record type is not known.
509 // We should catch this error immediately. 510 // We should catch this error immediately.
510 SkASSERT(false); 511 SkASSERT(false);
511 break; 512 break;
512 } 513 }
513 } while (TERMINATE_RECORD_TYPE != recordType); 514 } while (TERMINATE_RECORD_TYPE != recordType);
514 515
515 return gif_error("Could not find any images to decode in gif file.\n", 516 return gif_error("Could not find any images to decode in gif file.\n",
516 kInvalidInput); 517 kInvalidInput);
517 } 518 }
OLDNEW
« no previous file with comments | « src/codec/SkCodec_libbmp.cpp ('k') | src/codec/SkCodec_libpng.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698