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

Side by Side Diff: third_party/ktx/ktx.h

Issue 1882593002: Make is_ktx safer (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 8 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/SkKTXImageEncoder.cpp ('k') | third_party/ktx/ktx.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2014 Google Inc. 2 * Copyright 2014 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 8
9 #ifndef SkKTXFile_DEFINED 9 #ifndef SkKTXFile_DEFINED
10 #define SkKTXFile_DEFINED 10 #define SkKTXFile_DEFINED
(...skipping 16 matching lines...) Expand all
27 // texture in an OpenGL application, including the use of compressed data. 27 // texture in an OpenGL application, including the use of compressed data.
28 // 28 //
29 // A full format specification can be found here: 29 // A full format specification can be found here:
30 // http://www.khronos.org/opengles/sdk/tools/KTX/file_format_spec/ 30 // http://www.khronos.org/opengles/sdk/tools/KTX/file_format_spec/
31 31
32 class SkKTXFile { 32 class SkKTXFile {
33 public: 33 public:
34 // The ownership of the data remains with the caller. This class is intended 34 // The ownership of the data remains with the caller. This class is intended
35 // to be used as a logical wrapper around the data in order to properly 35 // to be used as a logical wrapper around the data in order to properly
36 // access the pixels. 36 // access the pixels.
37 SkKTXFile(SkData* data) 37 SkKTXFile(SkData* data) : fData(data), fSwapBytes(false) {
38 : fData(data), fSwapBytes(false)
39 {
40 data->ref(); 38 data->ref();
41 fValid = this->readKTXFile(fData->bytes(), fData->size()); 39 fValid = this->readKTXFile(fData->bytes(), fData->size());
42 } 40 }
43 41
44 bool valid() const { return fValid; } 42 bool valid() const { return fValid; }
45 43
46 int width() const { return static_cast<int>(fHeader.fPixelWidth); } 44 int width() const { return static_cast<int>(fHeader.fPixelWidth); }
47 int height() const { return static_cast<int>(fHeader.fPixelHeight); } 45 int height() const { return static_cast<int>(fHeader.fPixelHeight); }
48 46
49 const uint8_t *pixelData(int mipmap = 0) const { 47 const uint8_t *pixelData(int mipmap = 0) const {
50 SkASSERT(!this->valid() || mipmap < fPixelData.count()); 48 SkASSERT(!this->valid() || mipmap < fPixelData.count());
51 return this->valid() ? fPixelData[mipmap].data() : NULL; 49 return this->valid() ? fPixelData[mipmap].data() : NULL;
52 } 50 }
53 51
54 // If the decoded KTX file has the following key, then it will 52 // If the decoded KTX file has the following key, then it will
55 // return the associated value. If not found, the empty string 53 // return the associated value. If not found, the empty string
56 // is returned. 54 // is returned.
57 SkString getValueForKey(const SkString& key) const; 55 SkString getValueForKey(const SkString& key) const;
58 56
59 int numMipmaps() const { return static_cast<int>(fHeader.fNumberOfMipmapLeve ls); } 57 int numMipmaps() const { return static_cast<int>(fHeader.fNumberOfMipmapLeve ls); }
60 58
61 bool isCompressedFormat(SkTextureCompressor::Format fmt) const; 59 bool isCompressedFormat(SkTextureCompressor::Format fmt) const;
62 bool isRGBA8() const; 60 bool isRGBA8() const;
63 bool isRGB8() const; 61 bool isRGB8() const;
64 62
65 static bool is_ktx(const uint8_t *data); 63 static bool is_ktx(const uint8_t data[], size_t size);
66 static bool is_ktx(SkStreamRewindable* stream); 64 static bool is_ktx(SkStreamRewindable* stream);
67 65
68 static bool WriteETC1ToKTX(SkWStream* stream, const uint8_t *etc1Data, 66 static bool WriteETC1ToKTX(SkWStream* stream, const uint8_t *etc1Data,
69 uint32_t width, uint32_t height); 67 uint32_t width, uint32_t height);
70 static bool WriteBitmapToKTX(SkWStream* stream, const SkBitmap& bitmap); 68 static bool WriteBitmapToKTX(SkWStream* stream, const SkBitmap& bitmap);
71 private: 69 private:
72 70
73 // The blob holding the file data. 71 // The blob holding the file data.
74 SkAutoTUnref<SkData> fData; 72 SkAutoTUnref<SkData> fData;
75 73
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 // If the endianness of the platform is different than the file, 133 // If the endianness of the platform is different than the file,
136 // then we need to do proper byte swapping. 134 // then we need to do proper byte swapping.
137 bool fSwapBytes; 135 bool fSwapBytes;
138 136
139 // Read an integer from a buffer, advance the buffer, and swap 137 // Read an integer from a buffer, advance the buffer, and swap
140 // bytes if fSwapBytes is set 138 // bytes if fSwapBytes is set
141 uint32_t readInt(const uint8_t** buf, size_t* bytesLeft) const; 139 uint32_t readInt(const uint8_t** buf, size_t* bytesLeft) const;
142 }; 140 };
143 141
144 #endif // SkKTXFile_DEFINED 142 #endif // SkKTXFile_DEFINED
OLDNEW
« no previous file with comments | « src/images/SkKTXImageEncoder.cpp ('k') | third_party/ktx/ktx.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698