Commit 9481b0a194dbb2045d84b327a59b1154eebda87d
Committed by
Paul Wisbey
1 parent
b15bfd01
Fixed SVACE issues in ktx-loader.cpp + general maintenance.
- simplified the navigation of the file stream: the ktx metadata is skipped with a single fseek() from SEEK_CUR, by the number of bytes known from the ktx header; - removed the allocation of a single large buffer: image data is read from file, per image, directly into a buffer to be transferred to PixelData; - using std::unique_ptr<> to manage the scope of the file handle and image buffer; - removed local arrays of image sizes and buffer pointers as a total of no more than one of these is used at any given time; Change-Id: If51688568dd90dc1117b3c9a0de10f6c429bd394 Signed-off-by: György Straub <g.straub@partner.samsung.com>
Showing
1 changed file
with
36 additions
and
62 deletions
examples/rendering-basic-pbr/ktx-loader.cpp
| ... | ... | @@ -94,82 +94,53 @@ bool ConvertPixelFormat(const uint32_t ktxPixelFormat, Dali::Pixel::Format& form |
| 94 | 94 | |
| 95 | 95 | bool LoadCubeMapFromKtxFile( const std::string& path, CubeData& cubedata ) |
| 96 | 96 | { |
| 97 | - Dali::FileStream fileStream( path, Dali::FileStream::READ | Dali::FileStream::BINARY ); | |
| 98 | - FILE* fp = fileStream.GetFile(); | |
| 99 | - if( NULL == fp ) | |
| 97 | + std::unique_ptr<FILE, void(*)(FILE*)> fp(fopen(path.c_str(), "rb"), [](FILE* fp) { | |
| 98 | + if (fp) | |
| 99 | + { | |
| 100 | + fclose(fp); | |
| 101 | + } | |
| 102 | + }); | |
| 103 | + if (!fp) | |
| 100 | 104 | { |
| 101 | 105 | return false; |
| 102 | 106 | } |
| 103 | 107 | |
| 104 | 108 | KtxFileHeader header; |
| 105 | - | |
| 106 | - int result = fread(&header,1,sizeof(KtxFileHeader),fp); | |
| 107 | - if( 0 == result ) | |
| 109 | + int result = fread(&header, sizeof(KtxFileHeader), 1u, fp.get()); | |
| 110 | + if (0 == result) | |
| 108 | 111 | { |
| 109 | 112 | return false; |
| 110 | 113 | } |
| 111 | 114 | |
| 112 | - long lSize = 0; | |
| 113 | - | |
| 114 | 115 | // Skip the key-values: |
| 115 | - const long int imageSizeOffset = sizeof(KtxFileHeader) + header.bytesOfKeyValueData; | |
| 116 | - | |
| 117 | - if( fseek(fp, 0, SEEK_END) ) | |
| 118 | - { | |
| 119 | - return false; | |
| 120 | - } | |
| 121 | - | |
| 122 | - lSize = ftell(fp); | |
| 123 | - if( lSize == -1L ) | |
| 124 | - { | |
| 125 | - return false; | |
| 126 | - } | |
| 127 | - | |
| 128 | - lSize -= imageSizeOffset; | |
| 129 | - | |
| 130 | - rewind(fp); | |
| 131 | - | |
| 132 | - if( fseek(fp, imageSizeOffset, SEEK_SET) ) | |
| 116 | + if (fseek(fp.get(), header.bytesOfKeyValueData, SEEK_CUR)) | |
| 133 | 117 | { |
| 134 | 118 | return false; |
| 135 | 119 | } |
| 136 | 120 | |
| 137 | 121 | cubedata.img.resize(header.numberOfFaces); |
| 138 | 122 | |
| 139 | - for(unsigned int face=0; face < header.numberOfFaces; ++face) //array_element must be 0 or 1 | |
| 123 | + for (unsigned int face = 0; face < header.numberOfFaces; ++face) //array_element must be 0 or 1 | |
| 140 | 124 | { |
| 141 | 125 | cubedata.img[face].resize(header.numberOfMipmapLevels); |
| 142 | 126 | } |
| 143 | 127 | |
| 144 | - unsigned char* buffer= reinterpret_cast<unsigned char*>( malloc( lSize ) ); | |
| 145 | - | |
| 146 | - unsigned char* img[6]; | |
| 147 | - unsigned int imgSize[6]; | |
| 148 | - unsigned char* imgPointer = buffer; | |
| 149 | - result = fread(buffer,1,lSize,fp); | |
| 150 | - | |
| 151 | - if( 0 == result ) | |
| 152 | - { | |
| 153 | - free( buffer ); | |
| 154 | - return false; | |
| 155 | - } | |
| 156 | - | |
| 157 | - if( 0 == header.numberOfMipmapLevels ) | |
| 128 | + if (0 == header.numberOfMipmapLevels) | |
| 158 | 129 | { |
| 159 | 130 | header.numberOfMipmapLevels = 1u; |
| 160 | 131 | } |
| 161 | 132 | |
| 162 | - if( 0 == header.numberOfArrayElements ) | |
| 133 | + if (0 == header.numberOfArrayElements) | |
| 163 | 134 | { |
| 164 | 135 | header.numberOfArrayElements = 1u; |
| 165 | 136 | } |
| 166 | 137 | |
| 167 | - if( 0 == header.pixelDepth ) | |
| 138 | + if (0 == header.pixelDepth) | |
| 168 | 139 | { |
| 169 | 140 | header.pixelDepth = 1u; |
| 170 | 141 | } |
| 171 | 142 | |
| 172 | - if( 0 == header.pixelHeight ) | |
| 143 | + if (0 == header.pixelHeight) | |
| 173 | 144 | { |
| 174 | 145 | header.pixelHeight = 1u; |
| 175 | 146 | } |
| ... | ... | @@ -178,33 +149,36 @@ bool LoadCubeMapFromKtxFile( const std::string& path, CubeData& cubedata ) |
| 178 | 149 | |
| 179 | 150 | ConvertPixelFormat(header.glInternalFormat, daliformat); |
| 180 | 151 | |
| 181 | - for( unsigned int mipmapLevel = 0; mipmapLevel < header.numberOfMipmapLevels; ++mipmapLevel ) | |
| 152 | + for (unsigned int mipmapLevel = 0; mipmapLevel < header.numberOfMipmapLevels; ++mipmapLevel) | |
| 182 | 153 | { |
| 183 | - long int byteSize = 0; | |
| 184 | - int imageSize; | |
| 185 | - memcpy(&imageSize,imgPointer,sizeof(unsigned int)); | |
| 186 | - imgPointer += 4u; | |
| 187 | - for(unsigned int arrayElement=0; arrayElement < header.numberOfArrayElements; ++arrayElement) //arrayElement must be 0 or 1 | |
| 154 | + uint32_t byteSize = 0; | |
| 155 | + if (fread(&byteSize, sizeof(byteSize), 1u, fp.get()) != 1) | |
| 156 | + { | |
| 157 | + return false; | |
| 158 | + } | |
| 159 | + | |
| 160 | + if (0 != byteSize % 4u) | |
| 188 | 161 | { |
| 189 | - for(unsigned int face=0; face < header.numberOfFaces; ++face) | |
| 162 | + byteSize += 4u - byteSize % 4u; | |
| 163 | + } | |
| 164 | + | |
| 165 | + for (unsigned int arrayElement = 0; arrayElement < header.numberOfArrayElements; ++arrayElement) // arrayElement must be 0 or 1 | |
| 166 | + { | |
| 167 | + for (unsigned int face = 0; face < header.numberOfFaces; ++face) | |
| 190 | 168 | { |
| 191 | - byteSize = imageSize; | |
| 192 | - if(byteSize % 4u) | |
| 169 | + std::unique_ptr<uint8_t, void(*)(void*)> img(static_cast<unsigned char*>(malloc(byteSize)), free); // resources will be freed when the PixelData is destroyed. | |
| 170 | + if (fread(img.get(), byteSize, 1u, fp.get()) != 1) | |
| 193 | 171 | { |
| 194 | - byteSize += 4u - byteSize % 4u; | |
| 172 | + return false; | |
| 195 | 173 | } |
| 196 | - img[face] = reinterpret_cast<unsigned char*>( malloc( byteSize ) ); // resources will be freed when the PixelData is destroyed. | |
| 197 | - memcpy(img[face],imgPointer,byteSize); | |
| 198 | - imgSize[face] = byteSize; | |
| 199 | - imgPointer += byteSize; | |
| 200 | - cubedata.img[face][mipmapLevel] = PixelData::New( img[face], imgSize[face], header.pixelWidth , header.pixelHeight , daliformat, PixelData::FREE ); | |
| 174 | + cubedata.img[face][mipmapLevel] = PixelData::New(img.release(), byteSize, header.pixelWidth, header.pixelHeight, daliformat, PixelData::FREE); | |
| 201 | 175 | } |
| 202 | 176 | } |
| 203 | - header.pixelHeight/=2u; | |
| 204 | - header.pixelWidth/=2u; | |
| 177 | + | |
| 178 | + header.pixelHeight /= 2u; | |
| 179 | + header.pixelWidth /= 2u; | |
| 205 | 180 | } |
| 206 | 181 | |
| 207 | - free(buffer); | |
| 208 | 182 | return true; |
| 209 | 183 | } |
| 210 | 184 | ... | ... |