Chromium Code Reviews| 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 "SkColorPriv.h" | 8 #include "SkColorPriv.h" |
| 9 #include "SkImageDecoder.h" | 9 #include "SkImageDecoder.h" |
| 10 #include "SkPixelRef.h" | 10 #include "SkPixelRef.h" |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 84 // If the client wants unpremul colors and we only have | 84 // If the client wants unpremul colors and we only have |
| 85 // premul, then we cannot honor their wish. | 85 // premul, then we cannot honor their wish. |
| 86 if (bSrcIsPremul) { | 86 if (bSrcIsPremul) { |
| 87 return false; | 87 return false; |
| 88 } | 88 } |
| 89 } else { | 89 } else { |
| 90 alphaType = kPremul_SkAlphaType; | 90 alphaType = kPremul_SkAlphaType; |
| 91 } | 91 } |
| 92 } | 92 } |
| 93 | 93 |
| 94 // Set the config... | 94 // Search through the compressed formats to see if the KTX file is holding |
| 95 bm->setInfo(SkImageInfo::MakeN32(sampler.scaledWidth(), sampler.scaledHeight (), alphaType)); | 95 // compressed data |
| 96 bool ktxIsCompressed = false; | |
| 97 SkTextureCompressor::Format ktxCompressedFormat; | |
| 98 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) { | |
| 99 SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Forma t>(i); | |
| 100 if (ktxFile.isCompressedFormat(fmt)) { | |
| 101 ktxIsCompressed = true; | |
| 102 ktxCompressedFormat = fmt; | |
| 103 break; | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 // If the compressed format is a grayscale image, then setup the bitmap prop erly... | |
| 108 bool isCompressedAlpha = ktxIsCompressed && | |
| 109 ((SkTextureCompressor::kLATC_Format == ktxCompressedFormat) || | |
| 110 (SkTextureCompressor::kR11_EAC_Format == ktxCompressedFormat)); | |
| 111 | |
| 112 // Set the image dimensions and underlying pixel type. | |
| 113 if (isCompressedAlpha) { | |
| 114 const int w = sampler.scaledWidth(); | |
| 115 const int h = sampler.scaledHeight(); | |
| 116 bm->setInfo(SkImageInfo::MakeA8(w, h)); | |
| 117 } else { | |
| 118 const int w = sampler.scaledWidth(); | |
| 119 const int h = sampler.scaledHeight(); | |
| 120 bm->setInfo(SkImageInfo::MakeN32(w, h, alphaType)); | |
| 121 } | |
| 122 | |
| 96 if (SkImageDecoder::kDecodeBounds_Mode == mode) { | 123 if (SkImageDecoder::kDecodeBounds_Mode == mode) { |
| 97 return true; | 124 return true; |
| 98 } | 125 } |
| 99 | 126 |
| 100 // If we've made it this far, then we know how to grok the data. | 127 // If we've made it this far, then we know how to grok the data. |
| 101 if (!this->allocPixelRef(bm, NULL)) { | 128 if (!this->allocPixelRef(bm, NULL)) { |
| 102 return false; | 129 return false; |
| 103 } | 130 } |
| 104 | 131 |
| 105 // Lock the pixels, since we're about to write to them... | 132 // Lock the pixels, since we're about to write to them... |
| 106 SkAutoLockPixels alp(*bm); | 133 SkAutoLockPixels alp(*bm); |
| 107 | 134 |
| 108 if (ktxFile.isCompressedFormat(SkTextureCompressor::kETC1_Format)) { | 135 if (isCompressedAlpha) { |
| 136 if (!sampler.begin(bm, SkScaledBitmapSampler::kGray, *this)) { | |
| 137 return false; | |
| 138 } | |
| 139 | |
| 140 // Alpha data is only a single byte per pixel. | |
| 141 int nPixels = width * height; | |
| 142 SkAutoMalloc outRGBData(nPixels); | |
| 143 uint8_t *outRGBDataPtr = reinterpret_cast<uint8_t *>(outRGBData.get()); | |
| 144 | |
| 145 // Decode the compressed format | |
| 146 const uint8_t *buf = reinterpret_cast<const uint8_t *>(ktxFile.pixelData ()); | |
| 147 if (!SkTextureCompressor::DecompressBufferFromFormat( | |
| 148 outRGBDataPtr, width, buf, width, height, ktxCompressedFormat)) { | |
| 149 return false; | |
| 150 } | |
| 151 | |
| 152 // Set each of the pixels... | |
| 153 const int srcRowBytes = width; | |
| 154 const int dstHeight = sampler.scaledHeight(); | |
| 155 const uint8_t *srcRow = reinterpret_cast<uint8_t *>(outRGBDataPtr); | |
| 156 srcRow += sampler.srcY0() * srcRowBytes; | |
| 157 for (int y = 0; y < dstHeight; ++y) { | |
| 158 sampler.next(srcRow); | |
| 159 srcRow += sampler.srcDY() * srcRowBytes; | |
| 160 } | |
| 161 | |
| 162 return true; | |
| 163 | |
|
robertphillips
2014/08/07 16:37:29
Couldn't we say "ktxIsCompressed && SkTextureCompr
| |
| 164 } else if (ktxFile.isCompressedFormat(SkTextureCompressor::kETC1_Format)) { | |
| 109 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, *this)) { | 165 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, *this)) { |
| 110 return false; | 166 return false; |
| 111 } | 167 } |
| 112 | 168 |
| 113 // ETC1 Data is encoded as RGB pixels, so we should extract it as such | 169 // ETC1 Data is encoded as RGB pixels, so we should extract it as such |
| 114 int nPixels = width * height; | 170 int nPixels = width * height; |
| 115 SkAutoMalloc outRGBData(nPixels * 3); | 171 SkAutoMalloc outRGBData(nPixels * 3); |
| 116 uint8_t *outRGBDataPtr = reinterpret_cast<uint8_t *>(outRGBData.get()); | 172 uint8_t *outRGBDataPtr = reinterpret_cast<uint8_t *>(outRGBData.get()); |
| 117 | 173 |
| 118 // Decode ETC1 | 174 // Decode ETC1 |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 272 return SkImageDecoder::kUnknown_Format; | 328 return SkImageDecoder::kUnknown_Format; |
| 273 } | 329 } |
| 274 | 330 |
| 275 SkImageEncoder* sk_libktx_efactory(SkImageEncoder::Type t) { | 331 SkImageEncoder* sk_libktx_efactory(SkImageEncoder::Type t) { |
| 276 return (SkImageEncoder::kKTX_Type == t) ? SkNEW(SkKTXImageEncoder) : NULL; | 332 return (SkImageEncoder::kKTX_Type == t) ? SkNEW(SkKTXImageEncoder) : NULL; |
| 277 } | 333 } |
| 278 | 334 |
| 279 static SkImageDecoder_DecodeReg gReg(sk_libktx_dfactory); | 335 static SkImageDecoder_DecodeReg gReg(sk_libktx_dfactory); |
| 280 static SkImageDecoder_FormatReg gFormatReg(get_format_ktx); | 336 static SkImageDecoder_FormatReg gFormatReg(get_format_ktx); |
| 281 static SkImageEncoder_EncodeReg gEReg(sk_libktx_efactory); | 337 static SkImageEncoder_EncodeReg gEReg(sk_libktx_efactory); |
| OLD | NEW |