Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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 "SkBitmap.h" | 8 #include "SkBitmap.h" |
| 9 #include "SkCanvas.h" | |
| 10 #include "SkColor.h" | |
| 9 #include "SkColorPriv.h" | 11 #include "SkColorPriv.h" |
| 12 #include "SkData.h" | |
| 10 #include "SkForceLinking.h" | 13 #include "SkForceLinking.h" |
| 14 #include "SkGradientShader.h" | |
| 11 #include "SkImageDecoder.h" | 15 #include "SkImageDecoder.h" |
| 16 #include "SkImageEncoder.h" | |
| 12 #include "SkOSFile.h" | 17 #include "SkOSFile.h" |
| 18 #include "SkPoint.h" | |
| 19 #include "SkShader.h" | |
| 13 #include "SkStream.h" | 20 #include "SkStream.h" |
| 14 #include "SkString.h" | 21 #include "SkString.h" |
| 15 #include "Test.h" | 22 #include "Test.h" |
| 16 | 23 |
| 17 __SK_FORCE_IMAGE_DECODER_LINKING; | 24 __SK_FORCE_IMAGE_DECODER_LINKING; |
| 18 | 25 |
| 19 /** | 26 /** |
| 20 * Interprets c as an unpremultiplied color, and returns the | 27 * Interprets c as an unpremultiplied color, and returns the |
| 21 * premultiplied equivalent. | 28 * premultiplied equivalent. |
| 22 */ | 29 */ |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 178 do { | 185 do { |
| 179 SkString filename = SkOSPath::SkPathJoin(resourcePath.c_str(), basen ame.c_str()); | 186 SkString filename = SkOSPath::SkPathJoin(resourcePath.c_str(), basen ame.c_str()); |
| 180 //SkDebugf("about to decode \"%s\"\n", filename.c_str()); | 187 //SkDebugf("about to decode \"%s\"\n", filename.c_str()); |
| 181 compare_unpremul(reporter, filename); | 188 compare_unpremul(reporter, filename); |
| 182 } while (iter.next(&basename)); | 189 } while (iter.next(&basename)); |
| 183 } else { | 190 } else { |
| 184 SkDebugf("Failed to find any files :(\n"); | 191 SkDebugf("Failed to find any files :(\n"); |
| 185 } | 192 } |
| 186 } | 193 } |
| 187 | 194 |
| 195 #ifdef SK_DEBUG | |
| 196 // Create a stream containing a bitmap encoded to Type type. | |
| 197 static SkStream* create_image_stream(SkImageEncoder::Type type) { | |
| 198 SkBitmap bm; | |
| 199 const int size = 50; | |
| 200 bm.setConfig(SkBitmap::kARGB_8888_Config, size, size); | |
| 201 bm.allocPixels(); | |
| 202 SkCanvas canvas(bm); | |
| 203 SkPoint points[2] = { | |
| 204 { SkIntToScalar(0), SkIntToScalar(0) }, | |
| 205 { SkIntToScalar(size), SkIntToScalar(size) } | |
| 206 }; | |
| 207 SkColor colors[2] = { SK_ColorWHITE, SK_ColorBLUE }; | |
| 208 SkShader* shader = SkGradientShader::CreateLinear(points, colors, NULL, | |
| 209 SK_ARRAY_COUNT(colors), | |
| 210 SkShader::kClamp_TileMode) ; | |
| 211 SkPaint paint; | |
| 212 paint.setShader(shader)->unref(); | |
| 213 canvas.drawPaint(paint); | |
| 214 // Now encode it to a stream. | |
| 215 SkAutoTUnref<SkData> data(SkImageEncoder::EncodeData(bm, type, 100)); | |
| 216 if (NULL == data.get()) { | |
| 217 return NULL; | |
| 218 } | |
| 219 return SkNEW_ARGS(SkMemoryStream, (data.get())); | |
| 220 } | |
| 221 | |
| 222 // For every format that supports tile based decoding, ensure that | |
| 223 // calling decodeSubset will not fail if the caller has unreffed the | |
| 224 // stream provided in buildTileIndex. | |
| 225 // Only runs in debug mode since we are testing for a crash. | |
| 226 static void test_stream_life() { | |
| 227 const SkImageEncoder::Type gTypes[] = { | |
| 228 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK | |
| 229 SkImageEncoder::kJPEG_Type, | |
|
djsollen
2013/07/17 19:52:17
not needed for this CL, but we should look into bu
| |
| 230 #endif | |
| 231 #ifdef SK_BUILD_FOR_ANDROID | |
| 232 SkImageEncoder::kPNG_Type, | |
| 233 #endif | |
| 234 SkImageEncoder::kWEBP_Type, | |
| 235 }; | |
| 236 for (size_t i = 0; i < SK_ARRAY_COUNT(gTypes); ++i) { | |
| 237 SkDebugf("encoding to %i\n", i); | |
| 238 SkAutoTUnref<SkStream> stream(create_image_stream(gTypes[i])); | |
| 239 if (NULL == stream.get()) { | |
| 240 SkDebugf("no stream\n"); | |
| 241 continue; | |
| 242 } | |
| 243 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream)); | |
| 244 if (NULL == decoder.get()) { | |
| 245 SkDebugf("no decoder\n"); | |
| 246 continue; | |
| 247 } | |
| 248 int width, height; | |
| 249 if (!decoder->buildTileIndex(stream.get(), &width, &height)) { | |
| 250 SkDebugf("could not build a tile index\n"); | |
| 251 continue; | |
| 252 } | |
| 253 // Now unref the stream to make sure it survives | |
| 254 stream.reset(NULL); | |
| 255 SkBitmap bm; | |
| 256 decoder->decodeSubset(&bm, SkIRect::MakeWH(width, height), | |
| 257 SkBitmap::kARGB_8888_Config); | |
| 258 } | |
| 259 } | |
| 260 #endif | |
| 261 | |
| 188 static void test_imageDecodingTests(skiatest::Reporter* reporter) { | 262 static void test_imageDecodingTests(skiatest::Reporter* reporter) { |
| 189 test_unpremul(reporter); | 263 test_unpremul(reporter); |
| 190 test_pref_config_table(reporter); | 264 test_pref_config_table(reporter); |
| 265 #ifdef SK_DEBUG | |
| 266 test_stream_life(); | |
| 267 #endif | |
| 191 } | 268 } |
| 192 | 269 |
| 193 #include "TestClassDef.h" | 270 #include "TestClassDef.h" |
| 194 DEFINE_TESTCLASS("ImageDecoding", ImageDecodingTestClass, | 271 DEFINE_TESTCLASS("ImageDecoding", ImageDecodingTestClass, |
| 195 test_imageDecodingTests) | 272 test_imageDecodingTests) |
| OLD | NEW |