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

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

Issue 1695473002: Fix colorType/alphaType checks in SkCodec (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Rebase Created 4 years, 10 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/SkJpegCodec.cpp ('k') | no next file » | 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.h" 8 #include "SkCodec.h"
9 #include "SkCodecPriv.h" 9 #include "SkCodecPriv.h"
10 #include "SkColorPriv.h" 10 #include "SkColorPriv.h"
(...skipping 12 matching lines...) Expand all
23 23
24 static inline void setup_color_table(SkColorType colorType, 24 static inline void setup_color_table(SkColorType colorType,
25 SkPMColor* colorPtr, int* colorCount) { 25 SkPMColor* colorPtr, int* colorCount) {
26 if (kIndex_8_SkColorType == colorType) { 26 if (kIndex_8_SkColorType == colorType) {
27 colorPtr[0] = SK_ColorBLACK; 27 colorPtr[0] = SK_ColorBLACK;
28 colorPtr[1] = SK_ColorWHITE; 28 colorPtr[1] = SK_ColorWHITE;
29 *colorCount = 2; 29 *colorCount = 2;
30 } 30 }
31 } 31 }
32 32
33 static inline bool valid_color_type(SkColorType colorType, SkAlphaType alphaType ) {
34 switch (colorType) {
35 case kN32_SkColorType:
36 case kIndex_8_SkColorType:
37 return true;
38 case kGray_8_SkColorType:
39 case kRGB_565_SkColorType:
40 return kOpaque_SkAlphaType == alphaType;
41 default:
42 return false;
43 }
44 }
45
33 static bool read_byte(SkStream* stream, uint8_t* data) 46 static bool read_byte(SkStream* stream, uint8_t* data)
34 { 47 {
35 return stream->read(data, 1) == 1; 48 return stream->read(data, 1) == 1;
36 } 49 }
37 50
38 // http://en.wikipedia.org/wiki/Variable-length_quantity 51 // http://en.wikipedia.org/wiki/Variable-length_quantity
39 static bool read_mbf(SkStream* stream, uint64_t* value) { 52 static bool read_mbf(SkStream* stream, uint64_t* value) {
40 uint64_t n = 0; 53 uint64_t n = 0;
41 uint8_t data; 54 uint8_t data;
42 const uint64_t kLimit = 0xFE00000000000000; 55 const uint64_t kLimit = 0xFE00000000000000;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 } 90 }
78 return true; 91 return true;
79 } 92 }
80 93
81 bool SkWbmpCodec::onRewind() { 94 bool SkWbmpCodec::onRewind() {
82 return read_header(this->stream(), nullptr); 95 return read_header(this->stream(), nullptr);
83 } 96 }
84 97
85 SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info, const SkPMC olor* ctable, 98 SkSwizzler* SkWbmpCodec::initializeSwizzler(const SkImageInfo& info, const SkPMC olor* ctable,
86 const Options& opts) { 99 const Options& opts) {
87 // Create the swizzler based on the desired color type
88 switch (info.colorType()) {
89 case kIndex_8_SkColorType:
90 case kN32_SkColorType:
91 case kRGB_565_SkColorType:
92 case kGray_8_SkColorType:
93 break;
94 default:
95 return nullptr;
96 }
97 return SkSwizzler::CreateSwizzler(SkSwizzler::kBit, ctable, info, opts); 100 return SkSwizzler::CreateSwizzler(SkSwizzler::kBit, ctable, info, opts);
98 } 101 }
99 102
100 bool SkWbmpCodec::readRow(uint8_t* row) { 103 bool SkWbmpCodec::readRow(uint8_t* row) {
101 return this->stream()->read(row, fSrcRowBytes) == fSrcRowBytes; 104 return this->stream()->read(row, fSrcRowBytes) == fSrcRowBytes;
102 } 105 }
103 106
104 SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream) 107 SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream)
105 : INHERITED(info, stream) 108 : INHERITED(info, stream)
106 , fSrcRowBytes(get_src_row_bytes(this->getInfo().width())) 109 , fSrcRowBytes(get_src_row_bytes(this->getInfo().width()))
(...skipping 10 matching lines...) Expand all
117 size_t rowBytes, 120 size_t rowBytes,
118 const Options& options, 121 const Options& options,
119 SkPMColor ctable[], 122 SkPMColor ctable[],
120 int* ctableCount, 123 int* ctableCount,
121 int* rowsDecoded) { 124 int* rowsDecoded) {
122 if (options.fSubset) { 125 if (options.fSubset) {
123 // Subsets are not supported. 126 // Subsets are not supported.
124 return kUnimplemented; 127 return kUnimplemented;
125 } 128 }
126 129
127 if (!valid_alpha(info.alphaType(), this->getInfo().alphaType())) { 130 if (!valid_color_type(info.colorType(), info.alphaType()) ||
131 !valid_alpha(info.alphaType(), this->getInfo().alphaType())) {
128 return kInvalidConversion; 132 return kInvalidConversion;
129 } 133 }
130 134
131 // Prepare a color table if necessary 135 // Prepare a color table if necessary
132 setup_color_table(info.colorType(), ctable, ctableCount); 136 setup_color_table(info.colorType(), ctable, ctableCount);
133 137
134 // Initialize the swizzler 138 // Initialize the swizzler
135 SkAutoTDelete<SkSwizzler> swizzler(this->initializeSwizzler(info, ctable, op tions)); 139 SkAutoTDelete<SkSwizzler> swizzler(this->initializeSwizzler(info, ctable, op tions));
136 if (nullptr == swizzler.get()) { 140 SkASSERT(swizzler);
137 return kInvalidConversion;
138 }
139 141
140 // Perform the decode 142 // Perform the decode
141 SkISize size = info.dimensions(); 143 SkISize size = info.dimensions();
142 SkAutoTMalloc<uint8_t> src(fSrcRowBytes); 144 SkAutoTMalloc<uint8_t> src(fSrcRowBytes);
143 void* dstRow = dst; 145 void* dstRow = dst;
144 for (int y = 0; y < size.height(); ++y) { 146 for (int y = 0; y < size.height(); ++y) {
145 if (!this->readRow(src.get())) { 147 if (!this->readRow(src.get())) {
146 *rowsDecoded = y; 148 *rowsDecoded = y;
147 return kIncompleteInput; 149 return kIncompleteInput;
148 } 150 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 return this->stream()->skip(bytesToSkip) == bytesToSkip; 188 return this->stream()->skip(bytesToSkip) == bytesToSkip;
187 } 189 }
188 190
189 SkCodec::Result SkWbmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, 191 SkCodec::Result SkWbmpCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
190 const Options& options, SkPMColor inputColorTable[], int* inputColorCoun t) { 192 const Options& options, SkPMColor inputColorTable[], int* inputColorCoun t) {
191 if (options.fSubset) { 193 if (options.fSubset) {
192 // Subsets are not supported. 194 // Subsets are not supported.
193 return kUnimplemented; 195 return kUnimplemented;
194 } 196 }
195 197
196 if (!valid_alpha(dstInfo.alphaType(), this->getInfo().alphaType())) { 198 if (!valid_color_type(dstInfo.colorType(), dstInfo.alphaType()) ||
199 !valid_alpha(dstInfo.alphaType(), this->getInfo().alphaType())) {
197 return kInvalidConversion; 200 return kInvalidConversion;
198 } 201 }
199 202
200 // Fill in the color table 203 // Fill in the color table
201 setup_color_table(dstInfo.colorType(), inputColorTable, inputColorCount); 204 setup_color_table(dstInfo.colorType(), inputColorTable, inputColorCount);
202 205
203 // Copy the color table to a pointer that can be owned by the scanline decod er 206 // Copy the color table to a pointer that can be owned by the scanline decod er
204 if (kIndex_8_SkColorType == dstInfo.colorType()) { 207 if (kIndex_8_SkColorType == dstInfo.colorType()) {
205 fColorTable.reset(new SkColorTable(inputColorTable, 2)); 208 fColorTable.reset(new SkColorTable(inputColorTable, 2));
206 } 209 }
207 210
208 // Initialize the swizzler 211 // Initialize the swizzler
209 fSwizzler.reset(this->initializeSwizzler(dstInfo, get_color_ptr(fColorTable. get()), options)); 212 fSwizzler.reset(this->initializeSwizzler(dstInfo, get_color_ptr(fColorTable. get()), options));
210 if (nullptr == fSwizzler.get()) { 213 SkASSERT(fSwizzler);
211 return kInvalidConversion;
212 }
213 214
214 fSrcBuffer.reset(fSrcRowBytes); 215 fSrcBuffer.reset(fSrcRowBytes);
215 216
216 return kSuccess; 217 return kSuccess;
217 } 218 }
OLDNEW
« no previous file with comments | « src/codec/SkJpegCodec.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698