blob: 5cec2bf297c2db1ad929b629387372d5f515e29e [file] [log] [blame]
krajcevskic250d2e2014-06-06 06:16:28 -07001/*
2 * Copyright 2014 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 "SkBitmap.h"
9#include "SkData.h"
10#include "SkDecodingImageGenerator.h"
11#include "SkForceLinking.h"
12#include "SkImageDecoder.h"
13#include "SkOSFile.h"
14#include "SkRandom.h"
15#include "SkStream.h"
16#include "Test.h"
17
18__SK_FORCE_IMAGE_DECODER_LINKING;
19
20/**
21 * First, make sure that writing an 8-bit RGBA KTX file and then
22 * reading it produces the same bitmap.
23 */
24DEF_TEST(KtxReadWrite, reporter) {
25
26 // Random number generator with explicit seed for reproducibility
27 SkRandom rand(0x1005cbad);
28
29 SkBitmap bm8888;
reed6c225732014-06-09 19:52:07 -070030 bool pixelsAllocated = bm8888.allocN32Pixels(128, 128);
krajcevskic250d2e2014-06-06 06:16:28 -070031 REPORTER_ASSERT(reporter, pixelsAllocated);
32
33 uint8_t *pixels = reinterpret_cast<uint8_t*>(bm8888.getPixels());
34 REPORTER_ASSERT(reporter, NULL != pixels);
35
36 if (NULL == pixels) {
37 return;
38 }
39
40 uint8_t *row = pixels;
41 for (int y = 0; y < bm8888.height(); ++y) {
42 for (int x = 0; x < bm8888.width(); ++x) {
43 uint8_t a = rand.nextRangeU(0, 255);
44 uint8_t r = rand.nextRangeU(0, 255);
45 uint8_t g = rand.nextRangeU(0, 255);
46 uint8_t b = rand.nextRangeU(0, 255);
47
48 SkPMColor &pixel = *(reinterpret_cast<SkPMColor*>(row + x*sizeof(SkPMColor)));
49 pixel = SkPreMultiplyARGB(a, r, g, b);
50 }
51 row += bm8888.rowBytes();
52 }
53 REPORTER_ASSERT(reporter, !(bm8888.empty()));
54
55 SkAutoDataUnref encodedData(SkImageEncoder::EncodeData(bm8888, SkImageEncoder::kKTX_Type, 0));
56 REPORTER_ASSERT(reporter, NULL != encodedData);
57
58 SkAutoTUnref<SkMemoryStream> stream(SkNEW_ARGS(SkMemoryStream, (encodedData)));
59 REPORTER_ASSERT(reporter, NULL != stream);
60
61 SkBitmap decodedBitmap;
62 bool imageDecodeSuccess = SkImageDecoder::DecodeStream(stream, &decodedBitmap);
63 REPORTER_ASSERT(reporter, imageDecodeSuccess);
64
reedc3b32662014-06-17 08:38:31 -070065 REPORTER_ASSERT(reporter, decodedBitmap.colorType() == bm8888.colorType());
krajcevskic250d2e2014-06-06 06:16:28 -070066 REPORTER_ASSERT(reporter, decodedBitmap.alphaType() == bm8888.alphaType());
67 REPORTER_ASSERT(reporter, decodedBitmap.width() == bm8888.width());
68 REPORTER_ASSERT(reporter, decodedBitmap.height() == bm8888.height());
69 REPORTER_ASSERT(reporter, !(decodedBitmap.empty()));
70
71 uint8_t *decodedPixels = reinterpret_cast<uint8_t*>(decodedBitmap.getPixels());
72 REPORTER_ASSERT(reporter, NULL != decodedPixels);
73 REPORTER_ASSERT(reporter, decodedBitmap.getSize() == bm8888.getSize());
74
75 if (NULL == decodedPixels) {
76 return;
77 }
78
79 REPORTER_ASSERT(reporter, memcmp(decodedPixels, pixels, decodedBitmap.getSize()) == 0);
80}
81
82/**
83 * Next test is to see whether or not reading an unpremultiplied KTX file accurately
84 * creates a premultiplied buffer...
85 */
86DEF_TEST(KtxReadUnpremul, reporter) {
87
88 static const uint8_t kHalfWhiteKTX[] = {
89 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, // First twelve bytes is magic
90 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A, // KTX identifier string
91 0x01, 0x02, 0x03, 0x04, // Then magic endian specifier
92 0x01, 0x14, 0x00, 0x00, // uint32_t fGLType;
93 0x01, 0x00, 0x00, 0x00, // uint32_t fGLTypeSize;
94 0x08, 0x19, 0x00, 0x00, // uint32_t fGLFormat;
95 0x58, 0x80, 0x00, 0x00, // uint32_t fGLInternalFormat;
96 0x08, 0x19, 0x00, 0x00, // uint32_t fGLBaseInternalFormat;
97 0x02, 0x00, 0x00, 0x00, // uint32_t fPixelWidth;
98 0x02, 0x00, 0x00, 0x00, // uint32_t fPixelHeight;
99 0x00, 0x00, 0x00, 0x00, // uint32_t fPixelDepth;
100 0x00, 0x00, 0x00, 0x00, // uint32_t fNumberOfArrayElements;
101 0x01, 0x00, 0x00, 0x00, // uint32_t fNumberOfFaces;
102 0x01, 0x00, 0x00, 0x00, // uint32_t fNumberOfMipmapLevels;
103 0x00, 0x00, 0x00, 0x00, // uint32_t fBytesOfKeyValueData;
104 0x10, 0x00, 0x00, 0x00, // image size: 2x2 image of RGBA = 4 * 4 = 16 bytes
105 0xFF, 0xFF, 0xFF, 0x80, // Pixel 1
106 0xFF, 0xFF, 0xFF, 0x80, // Pixel 2
107 0xFF, 0xFF, 0xFF, 0x80, // Pixel 3
108 0xFF, 0xFF, 0xFF, 0x80};// Pixel 4
109
110 SkAutoTUnref<SkMemoryStream> stream(
111 SkNEW_ARGS(SkMemoryStream, (kHalfWhiteKTX, sizeof(kHalfWhiteKTX))));
112 REPORTER_ASSERT(reporter, NULL != stream);
113
114 SkBitmap decodedBitmap;
115 bool imageDecodeSuccess = SkImageDecoder::DecodeStream(stream, &decodedBitmap);
116 REPORTER_ASSERT(reporter, imageDecodeSuccess);
117
reed0689d7b2014-06-14 05:30:20 -0700118 REPORTER_ASSERT(reporter, decodedBitmap.colorType() == kN32_SkColorType);
krajcevskic250d2e2014-06-06 06:16:28 -0700119 REPORTER_ASSERT(reporter, decodedBitmap.alphaType() == kPremul_SkAlphaType);
120 REPORTER_ASSERT(reporter, decodedBitmap.width() == 2);
121 REPORTER_ASSERT(reporter, decodedBitmap.height() == 2);
122 REPORTER_ASSERT(reporter, !(decodedBitmap.empty()));
123
124 uint8_t *decodedPixels = reinterpret_cast<uint8_t*>(decodedBitmap.getPixels());
125 REPORTER_ASSERT(reporter, NULL != decodedPixels);
126
127 uint8_t *row = decodedPixels;
128 for (int j = 0; j < decodedBitmap.height(); ++j) {
129 for (int i = 0; i < decodedBitmap.width(); ++i) {
130 SkPMColor pixel = *(reinterpret_cast<SkPMColor*>(row + i*sizeof(SkPMColor)));
131 REPORTER_ASSERT(reporter, SkPreMultiplyARGB(0x80, 0xFF, 0xFF, 0xFF) == pixel);
132 }
133 row += decodedBitmap.rowBytes();
134 }
135}
136
137/**
138 * Finally, make sure that if we get ETC1 data from a PKM file that we can then
139 * accurately write it out into a KTX file (i.e. transferring the ETC1 data from
140 * the PKM to the KTX should produce an identical KTX to the one we have on file)
141 */
142DEF_TEST(KtxReexportPKM, reporter) {
143 SkString resourcePath = skiatest::Test::GetResourcePath();
144 SkString filename = SkOSPath::SkPathJoin(resourcePath.c_str(), "mandrill_128.pkm");
145
146 // Load PKM file into a bitmap
147 SkBitmap etcBitmap;
148 SkAutoTUnref<SkData> fileData(SkData::NewFromFileName(filename.c_str()));
149 REPORTER_ASSERT(reporter, NULL != fileData);
150
151 bool installDiscardablePixelRefSuccess =
152 SkInstallDiscardablePixelRef(
153 SkDecodingImageGenerator::Create(
154 fileData, SkDecodingImageGenerator::Options()), &etcBitmap);
155 REPORTER_ASSERT(reporter, installDiscardablePixelRefSuccess);
156
157 // Write the bitmap out to a KTX file.
158 SkData *ktxDataPtr = SkImageEncoder::EncodeData(etcBitmap, SkImageEncoder::kKTX_Type, 0);
159 SkAutoDataUnref newKtxData(ktxDataPtr);
160 REPORTER_ASSERT(reporter, NULL != ktxDataPtr);
161
162 // See is this data is identical to data in existing ktx file.
163 SkString ktxFilename = SkOSPath::SkPathJoin(resourcePath.c_str(), "mandrill_128.ktx");
164 SkAutoDataUnref oldKtxData(SkData::NewFromFileName(ktxFilename.c_str()));
165 REPORTER_ASSERT(reporter, oldKtxData->equals(newKtxData));
166}