blob: ec597b60434e31dbd0088f30f9281b320a9fc156 [file] [log] [blame]
halcanarya096d7a2015-03-27 12:16:53 -07001/*
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 "Resources.h"
9#include "SkBitmap.h"
10#include "SkCodec.h"
11#include "SkMD5.h"
12#include "Test.h"
13
14static SkStreamAsset* resource(const char path[]) {
15 SkString fullPath = GetResourcePath(path);
16 return SkStream::NewFromFile(fullPath.c_str());
17}
18
19static void md5(const SkBitmap& bm, SkMD5::Digest* digest) {
20 SkAutoLockPixels autoLockPixels(bm);
21 SkASSERT(bm.getPixels());
22 SkMD5 md5;
23 size_t rowLen = bm.info().bytesPerPixel() * bm.width();
24 for (int y = 0; y < bm.height(); ++y) {
25 md5.update(static_cast<uint8_t*>(bm.getAddr(0, y)), rowLen);
26 }
27 md5.finish(*digest);
28}
29
30static void check(skiatest::Reporter* r,
31 const char path[],
32 SkISize size,
33 bool canRewind) {
34 SkAutoTDelete<SkStream> stream(resource(path));
35 if (!stream) {
36 SkDebugf("Missing resource '%s'\n", path);
37 return;
38 }
39 SkAutoTDelete<SkImageGenerator> gen(
40 SkCodec::NewFromStream(stream.detach()));
41 if (!gen) {
42 ERRORF(r, "Unable to decode '%s'", path);
43 return;
44 }
45 SkImageInfo info = gen->getInfo();
46 REPORTER_ASSERT(r, info.dimensions() == size);
47 SkBitmap bm;
48 bm.allocPixels(info);
49 SkAutoLockPixels autoLockPixels(bm);
50 SkImageGenerator::Result result =
51 gen->getPixels(info, bm.getPixels(), bm.rowBytes(), NULL, NULL, NULL);
52 REPORTER_ASSERT(r, result == SkImageGenerator::kSuccess);
53
54 SkMD5::Digest digest1, digest2;
55 md5(bm, &digest1);
56
57 bm.eraseColor(SK_ColorYELLOW);
58
59 result =
60 gen->getPixels(info, bm.getPixels(), bm.rowBytes(), NULL, NULL, NULL);
61
62 // All ImageGenerators should support re-decoding the pixels.
63 // It is a known bug that some can not.
64 if (canRewind) {
65 REPORTER_ASSERT(r, result == SkImageGenerator::kSuccess);
66 // verify that re-decoding gives the same result.
67 md5(bm, &digest2);
68 REPORTER_ASSERT(r, digest1 == digest2);
69 }
70}
71
72DEF_TEST(Codec, r) {
73 // WBMP
74 check(r, "mandrill.wbmp", SkISize::Make(512, 512), true);
75
76 // BMP
77 // TODO (msarett): SkBmpCodec should be able to rewind.
78 check(r, "randPixels.bmp", SkISize::Make(8, 8), false);
79
80 // ICO
81 // TODO (msarett): SkIcoCodec should be able to rewind.
82 check(r, "color_wheel.ico", SkISize::Make(128, 128), false);
83
84 // PNG
85 // TODO (scroggo): SkPngCodec should be able to rewind.
86 check(r, "arrow.png", SkISize::Make(187, 312), false);
87 check(r, "baby_tux.png", SkISize::Make(240, 246), false);
88 check(r, "color_wheel.png", SkISize::Make(128, 128), false);
89 check(r, "half-transparent-white-pixel.png", SkISize::Make(1, 1), false);
90 check(r, "mandrill_128.png", SkISize::Make(128, 128), false);
91 check(r, "mandrill_16.png", SkISize::Make(16, 16), false);
92 check(r, "mandrill_256.png", SkISize::Make(256, 256), false);
93 check(r, "mandrill_32.png", SkISize::Make(32, 32), false);
94 check(r, "mandrill_512.png", SkISize::Make(512, 512), false);
95 check(r, "mandrill_64.png", SkISize::Make(64, 64), false);
96 check(r, "plane.png", SkISize::Make(250, 126), false);
97 check(r, "randPixels.png", SkISize::Make(8, 8), false);
98 check(r, "yellow_rose.png", SkISize::Make(400, 301), false);
99}