blob: 6ff245954008fed7845d651436157240ddc2870d [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???
66 if (!this->chooseFromOneChoice(SkBitmap::kARGB_8888_Config, width, height)) {
67 return false;
68 }
69
70 // Setup the sampler...
71 SkScaledBitmapSampler sampler(width, height, this->getSampleSize());
72
73 // Set the config...
74 bm->setConfig(SkBitmap::kARGB_8888_Config,
75 sampler.scaledWidth(), sampler.scaledHeight(),
76 0,
krajcevskic250d2e2014-06-06 06:16:28 -070077 ktxFile.isRGBA8()? kPremul_SkAlphaType : kOpaque_SkAlphaType);
krajcevski99ffe242014-06-03 13:04:35 -070078 if (SkImageDecoder::kDecodeBounds_Mode == mode) {
79 return true;
80 }
81
82 // If we've made it this far, then we know how to grok the data.
83 if (!this->allocPixelRef(bm, NULL)) {
84 return false;
85 }
86
87 // Lock the pixels, since we're about to write to them...
88 SkAutoLockPixels alp(*bm);
89
90 if (ktxFile.isETC1()) {
91 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, *this)) {
92 return false;
93 }
94
95 // ETC1 Data is encoded as RGB pixels, so we should extract it as such
96 int nPixels = width * height;
97 SkAutoMalloc outRGBData(nPixels * 3);
98 etc1_byte *outRGBDataPtr = reinterpret_cast<etc1_byte *>(outRGBData.get());
99
100 // Decode ETC1
101 const etc1_byte *buf = reinterpret_cast<const etc1_byte *>(ktxFile.pixelData());
102 if (etc1_decode_image(buf, outRGBDataPtr, width, height, 3, width*3)) {
103 return false;
104 }
105
106 // Set each of the pixels...
107 const int srcRowBytes = width * 3;
108 const int dstHeight = sampler.scaledHeight();
109 const uint8_t *srcRow = reinterpret_cast<uint8_t *>(outRGBDataPtr);
110 srcRow += sampler.srcY0() * srcRowBytes;
111 for (int y = 0; y < dstHeight; ++y) {
112 sampler.next(srcRow);
113 srcRow += sampler.srcDY() * srcRowBytes;
114 }
115
116 return true;
117
118 } else if (ktxFile.isRGB8()) {
119
120 // Uncompressed RGB data (without alpha)
121 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, *this)) {
122 return false;
123 }
124
125 // Just need to read RGB pixels
126 const int srcRowBytes = width * 3;
127 const int dstHeight = sampler.scaledHeight();
128 const uint8_t *srcRow = reinterpret_cast<const uint8_t *>(ktxFile.pixelData());
129 srcRow += sampler.srcY0() * srcRowBytes;
130 for (int y = 0; y < dstHeight; ++y) {
131 sampler.next(srcRow);
132 srcRow += sampler.srcDY() * srcRowBytes;
133 }
134
135 return true;
136
137 } else if (ktxFile.isRGBA8()) {
138
krajcevskic250d2e2014-06-06 06:16:28 -0700139 // If we know that the image contains premultiplied alpha, then
140 // don't premultiply it upon decoding.
141 bool setRequireUnpremul = false;
142 const SkString premulKey("KTXPremultipliedAlpha");
143 if (ktxFile.getValueForKey(premulKey) == SkString("True")) {
144 this->setRequireUnpremultipliedColors(true);
145 setRequireUnpremul = true;
146 }
147
krajcevski99ffe242014-06-03 13:04:35 -0700148 // Uncompressed RGBA data
149 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGBA, *this)) {
150 return false;
151 }
152
153 // Just need to read RGBA pixels
154 const int srcRowBytes = width * 4;
155 const int dstHeight = sampler.scaledHeight();
156 const uint8_t *srcRow = reinterpret_cast<const uint8_t *>(ktxFile.pixelData());
157 srcRow += sampler.srcY0() * srcRowBytes;
158 for (int y = 0; y < dstHeight; ++y) {
159 sampler.next(srcRow);
160 srcRow += sampler.srcDY() * srcRowBytes;
161 }
162
krajcevskic250d2e2014-06-06 06:16:28 -0700163 // Reset this in case the decoder needs to be used again.
164 if (setRequireUnpremul) {
165 this->setRequireUnpremultipliedColors(false);
166 }
167
krajcevski99ffe242014-06-03 13:04:35 -0700168 return true;
169 }
170
171 return false;
172}
173
krajcevskic250d2e2014-06-06 06:16:28 -0700174///////////////////////////////////////////////////////////////////////////////
175
176// KTX Image Encoder
177//
178// This encoder takes a best guess at how to encode the bitmap passed to it. If
179// there is an installed discardable pixel ref with existing PKM data, then we
180// will repurpose the existing ETC1 data into a KTX file. If the data contains
181// KTX data, then we simply return a copy of the same data. For all other files,
182// the underlying KTX library tries to do its best to encode the appropriate
183// data specified by the bitmap based on the config. (i.e. kAlpha8_Config will
184// be represented as a full resolution 8-bit image dump with the appropriate
185// OpenGL defines in the header).
186
187class SkKTXImageEncoder : public SkImageEncoder {
188protected:
189 virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) SK_OVERRIDE;
190
191private:
192 virtual bool encodePKM(SkWStream* stream, const SkData *data);
193 typedef SkImageEncoder INHERITED;
194};
195
196bool SkKTXImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bitmap, int) {
197 SkAutoDataUnref data(bitmap.pixelRef()->refEncodedData());
198
199 // Is this even encoded data?
200 if (NULL != data) {
201 const uint8_t *bytes = data->bytes();
202 if (etc1_pkm_is_valid(bytes)) {
203 return this->encodePKM(stream, data);
204 }
205
206 // Is it a KTX file??
207 if (SkKTXFile::is_ktx(bytes)) {
208 return stream->write(bytes, data->size());
209 }
210
211 // If it's neither a KTX nor a PKM, then we need to
212 // get at the actual pixels, so fall through and decompress...
213 }
214
215 return SkKTXFile::WriteBitmapToKTX(stream, bitmap);
216}
217
218bool SkKTXImageEncoder::encodePKM(SkWStream* stream, const SkData *data) {
219 const uint8_t* bytes = data->bytes();
220 SkASSERT(etc1_pkm_is_valid(bytes));
221
222 etc1_uint32 width = etc1_pkm_get_width(bytes);
223 etc1_uint32 height = etc1_pkm_get_height(bytes);
224
225 // ETC1 Data is stored as compressed 4x4 pixel blocks, so we must make sure
226 // that our dimensions are valid.
227 if (width == 0 || (width & 3) != 0 || height == 0 || (height & 3) != 0) {
228 return false;
229 }
230
231 // Advance pointer to etc1 data.
232 bytes += ETC_PKM_HEADER_SIZE;
233
234 return SkKTXFile::WriteETC1ToKTX(stream, bytes, width, height);
235}
236
krajcevski99ffe242014-06-03 13:04:35 -0700237/////////////////////////////////////////////////////////////////////////////////////////
238DEFINE_DECODER_CREATOR(KTXImageDecoder);
krajcevskic250d2e2014-06-06 06:16:28 -0700239DEFINE_ENCODER_CREATOR(KTXImageEncoder);
krajcevski99ffe242014-06-03 13:04:35 -0700240/////////////////////////////////////////////////////////////////////////////////////////
241
242static SkImageDecoder* sk_libktx_dfactory(SkStreamRewindable* stream) {
243 if (SkKTXFile::is_ktx(stream)) {
244 return SkNEW(SkKTXImageDecoder);
245 }
246 return NULL;
247}
248
krajcevski99ffe242014-06-03 13:04:35 -0700249static SkImageDecoder::Format get_format_ktx(SkStreamRewindable* stream) {
250 if (SkKTXFile::is_ktx(stream)) {
251 return SkImageDecoder::kKTX_Format;
252 }
253 return SkImageDecoder::kUnknown_Format;
254}
255
krajcevskic250d2e2014-06-06 06:16:28 -0700256SkImageEncoder* sk_libktx_efactory(SkImageEncoder::Type t) {
257 return (SkImageEncoder::kKTX_Type == t) ? SkNEW(SkKTXImageEncoder) : NULL;
258}
259
260static SkImageDecoder_DecodeReg gReg(sk_libktx_dfactory);
krajcevski99ffe242014-06-03 13:04:35 -0700261static SkImageDecoder_FormatReg gFormatReg(get_format_ktx);
krajcevskic250d2e2014-06-06 06:16:28 -0700262static SkImageEncoder_EncodeReg gEReg(sk_libktx_efactory);