OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 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 "Resources.h" | 8 #include "Resources.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkData.h" | 10 #include "SkData.h" |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 uint8_t *row = decodedPixels; | 136 uint8_t *row = decodedPixels; |
137 for (int j = 0; j < decodedBitmap.height(); ++j) { | 137 for (int j = 0; j < decodedBitmap.height(); ++j) { |
138 for (int i = 0; i < decodedBitmap.width(); ++i) { | 138 for (int i = 0; i < decodedBitmap.width(); ++i) { |
139 SkPMColor pixel = *(reinterpret_cast<SkPMColor*>(row + i*sizeof(SkPM
Color))); | 139 SkPMColor pixel = *(reinterpret_cast<SkPMColor*>(row + i*sizeof(SkPM
Color))); |
140 REPORTER_ASSERT(reporter, SkPreMultiplyARGB(0x80, 0xFF, 0xFF, 0xFF)
== pixel); | 140 REPORTER_ASSERT(reporter, SkPreMultiplyARGB(0x80, 0xFF, 0xFF, 0xFF)
== pixel); |
141 } | 141 } |
142 row += decodedBitmap.rowBytes(); | 142 row += decodedBitmap.rowBytes(); |
143 } | 143 } |
144 } | 144 } |
145 | 145 |
| 146 |
| 147 // For KtxReexportPKM, below |
| 148 class BareMemoryAllocator : public SkBitmap::Allocator { |
| 149 const SkImageInfo fInfo; |
| 150 void* const fMemory; |
| 151 const size_t fRowBytes; |
| 152 |
| 153 public: |
| 154 BareMemoryAllocator(const SkImageInfo& info, void* memory, size_t rowBytes) |
| 155 : fInfo(info), fMemory(memory), fRowBytes(rowBytes) |
| 156 {} |
| 157 |
| 158 protected: |
| 159 bool allocPixelRef(SkBitmap* bm, SkColorTable* ctable) override { |
| 160 const SkImageInfo bmi = bm->info(); |
| 161 if (bmi.width() != fInfo.width() || bmi.height() != fInfo.height() || |
| 162 bmi.colorType() != fInfo.colorType()) |
| 163 { |
| 164 return false; |
| 165 } |
| 166 return bm->installPixels(bmi, fMemory, fRowBytes, ctable, nullptr, nullp
tr); |
| 167 } |
| 168 }; |
| 169 |
| 170 class SkImageDecoderGenerator : public SkImageGenerator { |
| 171 const SkImageInfo fInfo; |
| 172 SkAutoTDelete<SkImageDecoder> fDecoder; |
| 173 SkAutoTUnref<SkData> fData; |
| 174 |
| 175 public: |
| 176 SkImageDecoderGenerator(const SkImageInfo& info, SkImageDecoder* decoder, Sk
Data* data) |
| 177 : INHERITED(info), fInfo(info), fDecoder(decoder), fData(SkRef(data)) |
| 178 {} |
| 179 |
| 180 protected: |
| 181 SkData* onRefEncodedData(SK_REFENCODEDDATA_CTXPARAM) override { |
| 182 return SkRef(fData.get()); |
| 183 } |
| 184 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 185 SkPMColor ctableEntries[], int* ctableCount) override { |
| 186 SkMemoryStream stream(fData->data(), fData->size(), false); |
| 187 SkAutoTUnref<BareMemoryAllocator> allocator( |
| 188 new BareMemoryAllocator(info, pixels, rowBytes)); |
| 189 fDecoder->setAllocator(allocator); |
| 190 fDecoder->setRequireUnpremultipliedColors(kUnpremul_SkAlphaType == info.
alphaType()); |
| 191 |
| 192 SkBitmap bm; |
| 193 const SkImageDecoder::Result result = fDecoder->decode(&stream, &bm, inf
o.colorType(), |
| 194 SkImageDecoder::k
DecodePixels_Mode); |
| 195 if (SkImageDecoder::kFailure == result) { |
| 196 return false; |
| 197 } |
| 198 |
| 199 SkASSERT(info.colorType() == bm.info().colorType()); |
| 200 |
| 201 if (kIndex_8_SkColorType == info.colorType()) { |
| 202 SkASSERT(ctableEntries); |
| 203 |
| 204 SkColorTable* ctable = bm.getColorTable(); |
| 205 if (nullptr == ctable) { |
| 206 return false; |
| 207 } |
| 208 const int count = ctable->count(); |
| 209 memcpy(ctableEntries, ctable->readColors(), count * sizeof(SkPMColor
)); |
| 210 *ctableCount = count; |
| 211 } |
| 212 return true; |
| 213 } |
| 214 |
| 215 bool onGetYUV8Planes(SkISize sizes[3], void* planes[3], size_t rowBytes[3], |
| 216 SkYUVColorSpace* colorSpace) override { |
| 217 SkMemoryStream stream(fData->data(), fData->size(), false); |
| 218 return fDecoder->decodeYUV8Planes(&stream, sizes, planes, rowBytes, colo
rSpace); |
| 219 } |
| 220 |
| 221 private: |
| 222 typedef SkImageGenerator INHERITED; |
| 223 }; |
| 224 |
| 225 static SkImageGenerator* decoder_image_generator(SkData* data) { |
| 226 SkMemoryStream stream(data->data(), data->size(), false); |
| 227 SkImageDecoder* decoder = SkImageDecoder::Factory(&stream); |
| 228 if (nullptr == decoder) { |
| 229 return nullptr; |
| 230 } |
| 231 |
| 232 SkBitmap bm; |
| 233 stream.rewind(); |
| 234 if (!decoder->decode(&stream, &bm, kUnknown_SkColorType, SkImageDecoder::kDe
codeBounds_Mode)) { |
| 235 delete decoder; |
| 236 return nullptr; |
| 237 } |
| 238 |
| 239 return new SkImageDecoderGenerator(bm.info(), decoder, data); |
| 240 } |
| 241 |
146 /** | 242 /** |
147 * Finally, make sure that if we get ETC1 data from a PKM file that we can then | 243 * Finally, make sure that if we get ETC1 data from a PKM file that we can then |
148 * accurately write it out into a KTX file (i.e. transferring the ETC1 data from | 244 * accurately write it out into a KTX file (i.e. transferring the ETC1 data from |
149 * the PKM to the KTX should produce an identical KTX to the one we have on file
) | 245 * the PKM to the KTX should produce an identical KTX to the one we have on file
) |
150 */ | 246 */ |
151 DEF_TEST(KtxReexportPKM, reporter) { | 247 DEF_TEST(KtxReexportPKM, reporter) { |
152 SkString pkmFilename = GetResourcePath("mandrill_128.pkm"); | 248 SkString pkmFilename = GetResourcePath("mandrill_128.pkm"); |
153 | 249 |
154 // Load PKM file into a bitmap | 250 // Load PKM file into a bitmap |
155 SkBitmap etcBitmap; | 251 SkBitmap etcBitmap; |
156 SkAutoTUnref<SkData> fileData(SkData::NewFromFileName(pkmFilename.c_str())); | 252 SkAutoTUnref<SkData> fileData(SkData::NewFromFileName(pkmFilename.c_str())); |
157 if (nullptr == fileData) { | 253 if (nullptr == fileData) { |
158 SkDebugf("KtxReexportPKM: can't load test file %s\n", pkmFilename.c_str(
)); | 254 SkDebugf("KtxReexportPKM: can't load test file %s\n", pkmFilename.c_str(
)); |
159 return; | 255 return; |
160 } | 256 } |
161 | 257 |
162 bool installDiscardablePixelRefSuccess = | 258 bool installDiscardablePixelRefSuccess = |
163 SkDEPRECATED_InstallDiscardablePixelRef(fileData, &etcBitmap); | 259 SkDEPRECATED_InstallDiscardablePixelRef(decoder_image_generator(fileData
), &etcBitmap); |
164 if (!installDiscardablePixelRefSuccess) { | 260 if (!installDiscardablePixelRefSuccess) { |
165 ERRORF(reporter, "failed to create discardable pixelRef from KTX file"); | 261 ERRORF(reporter, "failed to create discardable pixelRef from KTX file"); |
166 return; | 262 return; |
167 } | 263 } |
168 | 264 |
169 // Write the bitmap out to a KTX file. | 265 // Write the bitmap out to a KTX file. |
170 SkData *ktxDataPtr = SkImageEncoder::EncodeData(etcBitmap, SkImageEncoder::k
KTX_Type, 0); | 266 SkData *ktxDataPtr = SkImageEncoder::EncodeData(etcBitmap, SkImageEncoder::k
KTX_Type, 0); |
171 SkAutoDataUnref newKtxData(ktxDataPtr); | 267 SkAutoDataUnref newKtxData(ktxDataPtr); |
172 REPORTER_ASSERT(reporter, ktxDataPtr); | 268 REPORTER_ASSERT(reporter, ktxDataPtr); |
173 | 269 |
174 // See is this data is identical to data in existing ktx file. | 270 // See is this data is identical to data in existing ktx file. |
175 SkString ktxFilename = GetResourcePath("mandrill_128.ktx"); | 271 SkString ktxFilename = GetResourcePath("mandrill_128.ktx"); |
176 SkAutoDataUnref oldKtxData(SkData::NewFromFileName(ktxFilename.c_str())); | 272 SkAutoDataUnref oldKtxData(SkData::NewFromFileName(ktxFilename.c_str())); |
177 REPORTER_ASSERT(reporter, oldKtxData->equals(newKtxData)); | 273 REPORTER_ASSERT(reporter, oldKtxData->equals(newKtxData)); |
178 } | 274 } |
OLD | NEW |