Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 | |
| 2 /* | |
| 3 * Copyright 2014 Google Inc. | |
| 4 * | |
| 5 * Use of this source code is governed by a BSD-style license that can be | |
| 6 * found in the LICENSE file. | |
| 7 */ | |
| 8 | |
| 9 | |
| 10 #ifndef SkKTXFile_DEFINED | |
| 11 #define SkKTXFile_DEFINED | |
| 12 | |
| 13 #include "SkTypes.h" | |
| 14 #include "SkTDArray.h" | |
| 15 #include "SkString.h" | |
| 16 | |
| 17 class SkStreamRewindable; | |
| 18 | |
| 19 // KTX Image File | |
| 20 // --- | |
| 21 // KTX is a general texture data storage file format ratified by the Khronos Gro up. As an | |
| 22 // overview, a KTX file contains all of the appropriate values needed to fully s pecify a | |
| 23 // texture in an OpenGL application, including the use of compressed data. | |
| 24 // | |
| 25 // A full format specification can be found here: | |
| 26 // http://www.khronos.org/opengles/sdk/tools/KTX/file_format_spec/ | |
| 27 | |
| 28 class SkKTXFile { | |
| 29 public: | |
| 30 // The ownership of the data remains with the caller. This class is intended | |
| 31 // to be used as a logical wrapper around the data in order to properly | |
| 32 // access the pixels. | |
|
hal.canary
2014/06/03 15:41:58
Alternatively, you could take and ref an SkData he
krajcevski
2014/06/03 18:56:29
Hrm, this is a good suggestion. It gets rid of my
| |
| 33 SkKTXFile(const uint8_t* data, size_t dataLen) | |
| 34 : fSwapBytes(false) | |
| 35 { | |
| 36 fValid = this->readKTXFile(data, dataLen); | |
| 37 } | |
| 38 | |
| 39 bool valid() const { return fValid; } | |
| 40 | |
| 41 int width() const { return static_cast<int>(fHeader.fPixelWidth); } | |
| 42 int height() const { return static_cast<int>(fHeader.fPixelHeight); } | |
| 43 | |
| 44 const uint8_t *pixelData(int mipmap = 0) const { | |
| 45 SkASSERT(!this->valid() || mipmap < fPixelData.count()); | |
| 46 return this->valid() ? fPixelData[mipmap].data() : NULL; | |
| 47 } | |
| 48 | |
| 49 int numMipmaps() const { return static_cast<int>(fHeader.fNumberOfMipmapLeve ls); } | |
| 50 | |
| 51 bool isETC1() const; | |
| 52 bool isRGBA8() const; | |
| 53 bool isRGB8() const; | |
| 54 | |
| 55 static bool is_ktx(const uint8_t *data); | |
| 56 static bool is_ktx(SkStreamRewindable* stream); | |
| 57 | |
| 58 private: | |
| 59 | |
| 60 // This header captures all of the data that describes the format | |
| 61 // of the image data in a KTX file. | |
| 62 struct Header { | |
| 63 uint32_t fGLType; | |
| 64 uint32_t fGLTypeSize; | |
| 65 uint32_t fGLFormat; | |
| 66 uint32_t fGLInternalFormat; | |
| 67 uint32_t fGLBaseInternalFormat; | |
| 68 uint32_t fPixelWidth; | |
| 69 uint32_t fPixelHeight; | |
| 70 uint32_t fPixelDepth; | |
| 71 uint32_t fNumberOfArrayElements; | |
| 72 uint32_t fNumberOfFaces; | |
| 73 uint32_t fNumberOfMipmapLevels; | |
| 74 uint32_t fBytesOfKeyValueData; | |
| 75 | |
| 76 Header() { memset(this, 0, sizeof(*this)); } | |
| 77 void swapBytes(); | |
| 78 } fHeader; | |
| 79 | |
| 80 // A Key Value pair stored in the KTX file. There may be | |
| 81 // arbitrarily many of these. | |
| 82 class KeyValue { | |
| 83 public: | |
| 84 KeyValue(size_t size) : fDataSz(size) { } | |
| 85 bool readKeyAndValue(const uint8_t *data); | |
| 86 | |
| 87 private: | |
| 88 const size_t fDataSz; | |
| 89 SkString fKey; | |
| 90 SkString fValue; | |
| 91 }; | |
| 92 | |
| 93 // The pixel data for a single mipmap level in an image. Based on how | |
| 94 // the rest of the data is stored, this may be compressed, a cubemap, etc. | |
| 95 // The header will describe the format of this data. | |
| 96 class PixelData { | |
| 97 public: | |
| 98 PixelData(const uint8_t *ptr, size_t sz) : fDataSz(sz), fDataPtr(ptr) { } | |
| 99 const uint8_t *data() const { return fDataPtr; } | |
| 100 size_t dataSize() const { return fDataSz; } | |
| 101 private: | |
| 102 const size_t fDataSz; | |
| 103 const uint8_t *fDataPtr; | |
| 104 }; | |
| 105 | |
| 106 bool readKTXFile(const uint8_t *data, size_t dataLen); | |
| 107 | |
| 108 SkTDArray<KeyValue> fKeyValuePairs; | |
| 109 SkTDArray<PixelData> fPixelData; | |
| 110 bool fValid; | |
| 111 | |
| 112 // If the endianness of the platform is different than the file, | |
| 113 // then we need to do proper byte swapping. | |
| 114 bool fSwapBytes; | |
| 115 | |
| 116 // Read an integer from a buffer, advance the buffer, and swap | |
| 117 // bytes if fSwapBytes is set | |
| 118 uint32_t readInt(const uint8_t** buf, size_t* bytesLeft) const; | |
| 119 }; | |
| 120 | |
| 121 #endif // SkKTXFile_DEFINED | |
| OLD | NEW |