blob: fd1db5e9d2e0283df9c9f8818a56cdf16428c5f7 [file] [log] [blame]
krajcevski99ffe242014-06-03 13:04:35 -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 "SkColorPriv.h"
9#include "SkImageDecoder.h"
krajcevskic250d2e2014-06-06 06:16:28 -070010#include "SkPixelRef.h"
krajcevski99ffe242014-06-03 13:04:35 -070011#include "SkScaledBitmapSampler.h"
12#include "SkStream.h"
13#include "SkStreamHelpers.h"
14#include "SkTypes.h"
15
16#include "ktx.h"
17#include "etc1.h"
18
19/////////////////////////////////////////////////////////////////////////////////////////
20
21
22/////////////////////////////////////////////////////////////////////////////////////////
23
24// KTX Image decoder
25// ---
26// KTX is a general texture data storage file format ratified by the Khronos Group. As an
27// overview, a KTX file contains all of the appropriate values needed to fully specify a
28// texture in an OpenGL application, including the use of compressed data.
29//
30// This decoder is meant to be used with an SkDiscardablePixelRef so that GPU backends
31// can sniff the data before creating a texture. If they encounter a compressed format
32// that they understand, they can then upload the data directly to the GPU. Otherwise,
33// they will decode the data into a format that Skia supports.
34
35class SkKTXImageDecoder : public SkImageDecoder {
36public:
37 SkKTXImageDecoder() { }
38
39 virtual Format getFormat() const SK_OVERRIDE {
40 return kKTX_Format;
41 }
42
43protected:
44 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE;
45
46private:
47 typedef SkImageDecoder INHERITED;
48};
49
50bool SkKTXImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
51 // TODO: Implement SkStream::copyToData() that's cheap for memory and file streams
52 SkAutoDataUnref data(CopyStreamToData(stream));
53 if (NULL == data) {
54 return false;
55 }
56
57 SkKTXFile ktxFile(data);
58 if (!ktxFile.valid()) {
59 return false;
60 }
61
62 const unsigned short width = ktxFile.width();
63 const unsigned short height = ktxFile.height();
64
65 // should we allow the Chooser (if present) to pick a config for us???
reed6c225732014-06-09 19:52:07 -070066 if (!this->chooseFromOneChoice(kN32_SkColorType, width, height)) {
krajcevski99ffe242014-06-03 13:04:35 -070067 return false;
68 }
69
krajcevski3b570c62014-06-10 09:17:48 -070070 // Set a flag if our source is premultiplied alpha
71 const SkString premulKey("KTXPremultipliedAlpha");
72 const bool bSrcIsPremul = ktxFile.getValueForKey(premulKey) == SkString("True");
73
krajcevski99ffe242014-06-03 13:04:35 -070074 // Setup the sampler...
75 SkScaledBitmapSampler sampler(width, height, this->getSampleSize());
76
krajcevski3b570c62014-06-10 09:17:48 -070077 // Determine the alpha of the bitmap...
78 SkAlphaType alphaType = kOpaque_SkAlphaType;
79 if (ktxFile.isRGBA8()) {
80 if (this->getRequireUnpremultipliedColors()) {
81 alphaType = kUnpremul_SkAlphaType;
82 // If the client wants unpremul colors and we only have
83 // premul, then we cannot honor their wish.
84 if (bSrcIsPremul) {
85 return false;
86 }
87 } else {
88 alphaType = kPremul_SkAlphaType;
89 }
90 }
91
krajcevski99ffe242014-06-03 13:04:35 -070092 // Set the config...
krajcevski3b570c62014-06-10 09:17:48 -070093 bm->setInfo(SkImageInfo::MakeN32(sampler.scaledWidth(), sampler.scaledHeight(), alphaType));
krajcevski99ffe242014-06-03 13:04:35 -070094 if (SkImageDecoder::kDecodeBounds_Mode == mode) {
95 return true;
96 }
97
98 // If we've made it this far, then we know how to grok the data.
99 if (!this->allocPixelRef(bm, NULL)) {
100 return false;
101 }
102
103 // Lock the pixels, since we're about to write to them...
104 SkAutoLockPixels alp(*bm);
105
106 if (ktxFile.isETC1()) {
107 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, *this)) {
108 return false;
109 }
110
111 // ETC1 Data is encoded as RGB pixels, so we should extract it as such
112 int nPixels = width * height;
113 SkAutoMalloc outRGBData(nPixels * 3);
114 etc1_byte *outRGBDataPtr = reinterpret_cast<etc1_byte *>(outRGBData.get());
115
116 // Decode ETC1
117 const etc1_byte *buf = reinterpret_cast<const etc1_byte *>(ktxFile.pixelData());
118 if (etc1_decode_image(buf, outRGBDataPtr, width, height, 3, width*3)) {
119 return false;
120 }
121
122 // Set each of the pixels...
123 const int srcRowBytes = width * 3;
124 const int dstHeight = sampler.scaledHeight();
125 const uint8_t *srcRow = reinterpret_cast<uint8_t *>(outRGBDataPtr);
126 srcRow += sampler.srcY0() * srcRowBytes;
127 for (int y = 0; y < dstHeight; ++y) {
128 sampler.next(srcRow);
129 srcRow += sampler.srcDY() * srcRowBytes;
130 }
131
132 return true;
133
134 } else if (ktxFile.isRGB8()) {
135
136 // Uncompressed RGB data (without alpha)
137 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, *this)) {
138 return false;
139 }
140
141 // Just need to read RGB pixels
142 const int srcRowBytes = width * 3;
143 const int dstHeight = sampler.scaledHeight();
144 const uint8_t *srcRow = reinterpret_cast<const uint8_t *>(ktxFile.pixelData());
145 srcRow += sampler.srcY0() * srcRowBytes;
146 for (int y = 0; y < dstHeight; ++y) {
147 sampler.next(srcRow);
148 srcRow += sampler.srcDY() * srcRowBytes;
149 }
150
151 return true;
152
153 } else if (ktxFile.isRGBA8()) {
154
155 // Uncompressed RGBA data
krajcevski3b570c62014-06-10 09:17:48 -0700156
157 // If we know that the image contains premultiplied alpha, then
158 // we need to turn off the premultiplier
159 SkScaledBitmapSampler::Options opts (*this);
160 if (bSrcIsPremul) {
161 SkASSERT(bm->alphaType() == kPremul_SkAlphaType);
162 SkASSERT(!this->getRequireUnpremultipliedColors());
163
164 opts.fPremultiplyAlpha = false;
165 }
166
167 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGBA, opts)) {
krajcevski99ffe242014-06-03 13:04:35 -0700168 return false;
169 }
170
171 // Just need to read RGBA pixels
172 const int srcRowBytes = width * 4;
173 const int dstHeight = sampler.scaledHeight();
174 const uint8_t *srcRow = reinterpret_cast<const uint8_t *>(ktxFile.pixelData());
175 srcRow += sampler.srcY0() * srcRowBytes;
176 for (int y = 0; y < dstHeight; ++y) {
177 sampler.next(srcRow);
178 srcRow += sampler.srcDY() * srcRowBytes;
179 }
180
181 return true;
182 }
183
184 return false;
185}
186
krajcevskic250d2e2014-06-06 06:16:28 -0700187///////////////////////////////////////////////////////////////////////////////
188
189// KTX Image Encoder
190//
191// This encoder takes a best guess at how to encode the bitmap passed to it. If
192// there is an installed discardable pixel ref with existing PKM data, then we
193// will repurpose the existing ETC1 data into a KTX file. If the data contains
194// KTX data, then we simply return a copy of the same data. For all other files,
195// the underlying KTX library tries to do its best to encode the appropriate
196// data specified by the bitmap based on the config. (i.e. kAlpha8_Config will
197// be represented as a full resolution 8-bit image dump with the appropriate
198// OpenGL defines in the header).
199
200class SkKTXImageEncoder : public SkImageEncoder {
201protected:
202 virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) SK_OVERRIDE;
203
204private:
205 virtual bool encodePKM(SkWStream* stream, const SkData *data);
206 typedef SkImageEncoder INHERITED;
207};
208
209bool SkKTXImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bitmap, int) {
210 SkAutoDataUnref data(bitmap.pixelRef()->refEncodedData());
211
212 // Is this even encoded data?
213 if (NULL != data) {
214 const uint8_t *bytes = data->bytes();
215 if (etc1_pkm_is_valid(bytes)) {
216 return this->encodePKM(stream, data);
217 }
218
219 // Is it a KTX file??
220 if (SkKTXFile::is_ktx(bytes)) {
221 return stream->write(bytes, data->size());
222 }
223
224 // If it's neither a KTX nor a PKM, then we need to
225 // get at the actual pixels, so fall through and decompress...
226 }
227
228 return SkKTXFile::WriteBitmapToKTX(stream, bitmap);
229}
230
231bool SkKTXImageEncoder::encodePKM(SkWStream* stream, const SkData *data) {
232 const uint8_t* bytes = data->bytes();
233 SkASSERT(etc1_pkm_is_valid(bytes));
234
235 etc1_uint32 width = etc1_pkm_get_width(bytes);
236 etc1_uint32 height = etc1_pkm_get_height(bytes);
237
238 // ETC1 Data is stored as compressed 4x4 pixel blocks, so we must make sure
239 // that our dimensions are valid.
240 if (width == 0 || (width & 3) != 0 || height == 0 || (height & 3) != 0) {
241 return false;
242 }
243
244 // Advance pointer to etc1 data.
245 bytes += ETC_PKM_HEADER_SIZE;
246
247 return SkKTXFile::WriteETC1ToKTX(stream, bytes, width, height);
248}
249
krajcevski99ffe242014-06-03 13:04:35 -0700250/////////////////////////////////////////////////////////////////////////////////////////
251DEFINE_DECODER_CREATOR(KTXImageDecoder);
krajcevskic250d2e2014-06-06 06:16:28 -0700252DEFINE_ENCODER_CREATOR(KTXImageEncoder);
krajcevski99ffe242014-06-03 13:04:35 -0700253/////////////////////////////////////////////////////////////////////////////////////////
254
255static SkImageDecoder* sk_libktx_dfactory(SkStreamRewindable* stream) {
256 if (SkKTXFile::is_ktx(stream)) {
257 return SkNEW(SkKTXImageDecoder);
258 }
259 return NULL;
260}
261
krajcevski99ffe242014-06-03 13:04:35 -0700262static SkImageDecoder::Format get_format_ktx(SkStreamRewindable* stream) {
263 if (SkKTXFile::is_ktx(stream)) {
264 return SkImageDecoder::kKTX_Format;
265 }
266 return SkImageDecoder::kUnknown_Format;
267}
268
krajcevskic250d2e2014-06-06 06:16:28 -0700269SkImageEncoder* sk_libktx_efactory(SkImageEncoder::Type t) {
270 return (SkImageEncoder::kKTX_Type == t) ? SkNEW(SkKTXImageEncoder) : NULL;
271}
272
273static SkImageDecoder_DecodeReg gReg(sk_libktx_dfactory);
krajcevski99ffe242014-06-03 13:04:35 -0700274static SkImageDecoder_FormatReg gFormatReg(get_format_ktx);
krajcevskic250d2e2014-06-06 06:16:28 -0700275static SkImageEncoder_EncodeReg gEReg(sk_libktx_efactory);