Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(265)

Side by Side Diff: src/images/SkImageDecoder_ktx.cpp

Issue 302333002: Initial KTX file decoder (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Fix includes and SkTArray Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
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"
10 #include "SkScaledBitmapSampler.h"
11 #include "SkStream.h"
12 #include "SkStreamHelpers.h"
13 #include "SkTypes.h"
14
15 #include "SkKTXFile.h"
16
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 Gro up. As an
27 // overview, a KTX file contains all of the appropriate values needed to fully s pecify 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 ba ckends
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. Othe rwise,
33 // they will decode the data into a format that Skia supports.
34
35 class SkKTXImageDecoder : public SkImageDecoder {
36 public:
37 SkKTXImageDecoder() { }
38
39 virtual Format getFormat() const SK_OVERRIDE {
40 return kKTX_Format;
41 }
42
43 protected:
44 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE;
45
46 private:
47 typedef SkImageDecoder INHERITED;
48 };
49
50 bool SkKTXImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
51 SkAutoMalloc autoMal;
52 const size_t length = CopyStreamToStorage(&autoMal, stream);
53 if (0 == length) {
54 return false;
55 }
56
57 SkKTXFile ktxFile(reinterpret_cast<const uint8_t *>(autoMal.get()), length);
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,
77 ktxFile.isRGBA8()? kUnpremul_SkAlphaType : kOpaque_SkAlphaType );
78 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.pixel Data());
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.pixelD ata());
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
139 // Uncompressed RGBA data
140 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGBA, *this)) {
141 return false;
142 }
143
144 // Just need to read RGBA pixels
145 const int srcRowBytes = width * 4;
146 const int dstHeight = sampler.scaledHeight();
147 const uint8_t *srcRow = reinterpret_cast<const uint8_t *>(ktxFile.pixelD ata());
148 srcRow += sampler.srcY0() * srcRowBytes;
149 for (int y = 0; y < dstHeight; ++y) {
150 sampler.next(srcRow);
151 srcRow += sampler.srcDY() * srcRowBytes;
152 }
153
154 return true;
155 }
156
157 return false;
158 }
159
160 //////////////////////////////////////////////////////////////////////////////// /////////
161 DEFINE_DECODER_CREATOR(KTXImageDecoder);
162 //////////////////////////////////////////////////////////////////////////////// /////////
163
164 static SkImageDecoder* sk_libktx_dfactory(SkStreamRewindable* stream) {
165 if (SkKTXFile::is_ktx(stream)) {
166 return SkNEW(SkKTXImageDecoder);
167 }
168 return NULL;
169 }
170
171 static SkImageDecoder_DecodeReg gReg(sk_libktx_dfactory);
172
173 static SkImageDecoder::Format get_format_ktx(SkStreamRewindable* stream) {
174 if (SkKTXFile::is_ktx(stream)) {
175 return SkImageDecoder::kKTX_Format;
176 }
177 return SkImageDecoder::kUnknown_Format;
178 }
179
180 static SkImageDecoder_FormatReg gFormatReg(get_format_ktx);
OLDNEW
« no previous file with comments | « src/images/SkImageDecoder.cpp ('k') | src/utils/SkKTXFile.h » ('j') | src/utils/SkKTXFile.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698