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

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: More code comments 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
« no previous file with comments | « src/images/SkImageDecoder.cpp ('k') | src/images/SkStreamHelpers.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "ktx.h"
16 #include "etc1.h"
17
18 //////////////////////////////////////////////////////////////////////////////// /////////
19
20
21 //////////////////////////////////////////////////////////////////////////////// /////////
22
23 // KTX Image decoder
24 // ---
25 // KTX is a general texture data storage file format ratified by the Khronos Gro up. As an
26 // overview, a KTX file contains all of the appropriate values needed to fully s pecify a
27 // texture in an OpenGL application, including the use of compressed data.
28 //
29 // This decoder is meant to be used with an SkDiscardablePixelRef so that GPU ba ckends
30 // can sniff the data before creating a texture. If they encounter a compressed format
31 // that they understand, they can then upload the data directly to the GPU. Othe rwise,
32 // they will decode the data into a format that Skia supports.
33
34 class SkKTXImageDecoder : public SkImageDecoder {
35 public:
36 SkKTXImageDecoder() { }
37
38 virtual Format getFormat() const SK_OVERRIDE {
39 return kKTX_Format;
40 }
41
42 protected:
43 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE;
44
45 private:
46 typedef SkImageDecoder INHERITED;
47 };
48
49 bool SkKTXImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
50 // TODO: Implement SkStream::copyToData() that's cheap for memory and file s treams
51 SkAutoDataUnref data(CopyStreamToData(stream));
52 if (NULL == data) {
53 return false;
54 }
55
56 SkKTXFile ktxFile(data);
57 if (!ktxFile.valid()) {
58 return false;
59 }
60
61 const unsigned short width = ktxFile.width();
62 const unsigned short height = ktxFile.height();
63
64 // should we allow the Chooser (if present) to pick a config for us???
65 if (!this->chooseFromOneChoice(SkBitmap::kARGB_8888_Config, width, height)) {
66 return false;
67 }
68
69 // Setup the sampler...
70 SkScaledBitmapSampler sampler(width, height, this->getSampleSize());
71
72 // Set the config...
73 bm->setConfig(SkBitmap::kARGB_8888_Config,
74 sampler.scaledWidth(), sampler.scaledHeight(),
75 0,
76 ktxFile.isRGBA8()? kUnpremul_SkAlphaType : kOpaque_SkAlphaType );
77 if (SkImageDecoder::kDecodeBounds_Mode == mode) {
78 return true;
79 }
80
81 // If we've made it this far, then we know how to grok the data.
82 if (!this->allocPixelRef(bm, NULL)) {
83 return false;
84 }
85
86 // Lock the pixels, since we're about to write to them...
87 SkAutoLockPixels alp(*bm);
88
89 if (ktxFile.isETC1()) {
90 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, *this)) {
91 return false;
92 }
93
94 // ETC1 Data is encoded as RGB pixels, so we should extract it as such
95 int nPixels = width * height;
96 SkAutoMalloc outRGBData(nPixels * 3);
97 etc1_byte *outRGBDataPtr = reinterpret_cast<etc1_byte *>(outRGBData.get( ));
98
99 // Decode ETC1
100 const etc1_byte *buf = reinterpret_cast<const etc1_byte *>(ktxFile.pixel Data());
101 if (etc1_decode_image(buf, outRGBDataPtr, width, height, 3, width*3)) {
102 return false;
103 }
104
105 // Set each of the pixels...
106 const int srcRowBytes = width * 3;
107 const int dstHeight = sampler.scaledHeight();
108 const uint8_t *srcRow = reinterpret_cast<uint8_t *>(outRGBDataPtr);
109 srcRow += sampler.srcY0() * srcRowBytes;
110 for (int y = 0; y < dstHeight; ++y) {
111 sampler.next(srcRow);
112 srcRow += sampler.srcDY() * srcRowBytes;
113 }
114
115 return true;
116
117 } else if (ktxFile.isRGB8()) {
118
119 // Uncompressed RGB data (without alpha)
120 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGB, *this)) {
121 return false;
122 }
123
124 // Just need to read RGB pixels
125 const int srcRowBytes = width * 3;
126 const int dstHeight = sampler.scaledHeight();
127 const uint8_t *srcRow = reinterpret_cast<const uint8_t *>(ktxFile.pixelD ata());
128 srcRow += sampler.srcY0() * srcRowBytes;
129 for (int y = 0; y < dstHeight; ++y) {
130 sampler.next(srcRow);
131 srcRow += sampler.srcDY() * srcRowBytes;
132 }
133
134 return true;
135
136 } else if (ktxFile.isRGBA8()) {
137
138 // Uncompressed RGBA data
139 if (!sampler.begin(bm, SkScaledBitmapSampler::kRGBA, *this)) {
140 return false;
141 }
142
143 // Just need to read RGBA pixels
144 const int srcRowBytes = width * 4;
145 const int dstHeight = sampler.scaledHeight();
146 const uint8_t *srcRow = reinterpret_cast<const uint8_t *>(ktxFile.pixelD ata());
147 srcRow += sampler.srcY0() * srcRowBytes;
148 for (int y = 0; y < dstHeight; ++y) {
149 sampler.next(srcRow);
150 srcRow += sampler.srcDY() * srcRowBytes;
151 }
152
153 return true;
154 }
155
156 return false;
157 }
158
159 //////////////////////////////////////////////////////////////////////////////// /////////
160 DEFINE_DECODER_CREATOR(KTXImageDecoder);
161 //////////////////////////////////////////////////////////////////////////////// /////////
162
163 static SkImageDecoder* sk_libktx_dfactory(SkStreamRewindable* stream) {
164 if (SkKTXFile::is_ktx(stream)) {
165 return SkNEW(SkKTXImageDecoder);
166 }
167 return NULL;
168 }
169
170 static SkImageDecoder_DecodeReg gReg(sk_libktx_dfactory);
171
172 static SkImageDecoder::Format get_format_ktx(SkStreamRewindable* stream) {
173 if (SkKTXFile::is_ktx(stream)) {
174 return SkImageDecoder::kKTX_Format;
175 }
176 return SkImageDecoder::kUnknown_Format;
177 }
178
179 static SkImageDecoder_FormatReg gFormatReg(get_format_ktx);
OLDNEW
« no previous file with comments | « src/images/SkImageDecoder.cpp ('k') | src/images/SkStreamHelpers.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698