| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 #include "SkCodec_libbmp.h" | 8 #include "SkCodec_libbmp.h" |
| 9 #include "SkCodec_libico.h" | 9 #include "SkCodec_libico.h" |
| 10 #include "SkCodec_libpng.h" | 10 #include "SkCodec_libpng.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 | 38 |
| 39 // Header size constants | 39 // Header size constants |
| 40 static const uint32_t kIcoDirectoryBytes = 6; | 40 static const uint32_t kIcoDirectoryBytes = 6; |
| 41 static const uint32_t kIcoDirEntryBytes = 16; | 41 static const uint32_t kIcoDirEntryBytes = 16; |
| 42 | 42 |
| 43 // Read the directory header | 43 // Read the directory header |
| 44 SkAutoTDeleteArray<uint8_t> dirBuffer( | 44 SkAutoTDeleteArray<uint8_t> dirBuffer( |
| 45 SkNEW_ARRAY(uint8_t, kIcoDirectoryBytes)); | 45 SkNEW_ARRAY(uint8_t, kIcoDirectoryBytes)); |
| 46 if (inputStream.get()->read(dirBuffer.get(), kIcoDirectoryBytes) != | 46 if (inputStream.get()->read(dirBuffer.get(), kIcoDirectoryBytes) != |
| 47 kIcoDirectoryBytes) { | 47 kIcoDirectoryBytes) { |
| 48 SkDebugf("Error: unable to read ico directory header.\n"); | 48 SkCodecPrintf("Error: unable to read ico directory header.\n"); |
| 49 return NULL; | 49 return NULL; |
| 50 } | 50 } |
| 51 | 51 |
| 52 // Process the directory header | 52 // Process the directory header |
| 53 const uint16_t numImages = get_short(dirBuffer.get(), 4); | 53 const uint16_t numImages = get_short(dirBuffer.get(), 4); |
| 54 if (0 == numImages) { | 54 if (0 == numImages) { |
| 55 SkDebugf("Error: No images embedded in ico.\n"); | 55 SkCodecPrintf("Error: No images embedded in ico.\n"); |
| 56 return NULL; | 56 return NULL; |
| 57 } | 57 } |
| 58 | 58 |
| 59 // Ensure that we can read all of indicated directory entries | 59 // Ensure that we can read all of indicated directory entries |
| 60 SkAutoTDeleteArray<uint8_t> entryBuffer( | 60 SkAutoTDeleteArray<uint8_t> entryBuffer( |
| 61 SkNEW_ARRAY(uint8_t, numImages*kIcoDirEntryBytes)); | 61 SkNEW_ARRAY(uint8_t, numImages*kIcoDirEntryBytes)); |
| 62 if (inputStream.get()->read(entryBuffer.get(), numImages*kIcoDirEntryBytes)
!= | 62 if (inputStream.get()->read(entryBuffer.get(), numImages*kIcoDirEntryBytes)
!= |
| 63 numImages*kIcoDirEntryBytes) { | 63 numImages*kIcoDirEntryBytes) { |
| 64 SkDebugf("Error: unable to read ico directory entries.\n"); | 64 SkCodecPrintf("Error: unable to read ico directory entries.\n"); |
| 65 return NULL; | 65 return NULL; |
| 66 } | 66 } |
| 67 | 67 |
| 68 // This structure is used to represent the vital information about entries | 68 // This structure is used to represent the vital information about entries |
| 69 // in the directory header. We will obtain this information for each | 69 // in the directory header. We will obtain this information for each |
| 70 // directory entry. | 70 // directory entry. |
| 71 struct Entry { | 71 struct Entry { |
| 72 uint32_t offset; | 72 uint32_t offset; |
| 73 uint32_t size; | 73 uint32_t size; |
| 74 }; | 74 }; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 // Now will construct a candidate codec for each of the embedded images | 111 // Now will construct a candidate codec for each of the embedded images |
| 112 uint32_t bytesRead = kIcoDirectoryBytes + numImages * kIcoDirEntryBytes; | 112 uint32_t bytesRead = kIcoDirectoryBytes + numImages * kIcoDirEntryBytes; |
| 113 SkAutoTDelete<SkTArray<SkAutoTDelete<SkCodec>, true>> codecs( | 113 SkAutoTDelete<SkTArray<SkAutoTDelete<SkCodec>, true>> codecs( |
| 114 SkNEW_ARGS((SkTArray<SkAutoTDelete<SkCodec>, true>), (numImages))); | 114 SkNEW_ARGS((SkTArray<SkAutoTDelete<SkCodec>, true>), (numImages))); |
| 115 for (uint32_t i = 0; i < numImages; i++) { | 115 for (uint32_t i = 0; i < numImages; i++) { |
| 116 uint32_t offset = directoryEntries.get()[i].offset; | 116 uint32_t offset = directoryEntries.get()[i].offset; |
| 117 uint32_t size = directoryEntries.get()[i].size; | 117 uint32_t size = directoryEntries.get()[i].size; |
| 118 | 118 |
| 119 // Ensure that the offset is valid | 119 // Ensure that the offset is valid |
| 120 if (offset < bytesRead) { | 120 if (offset < bytesRead) { |
| 121 SkDebugf("Warning: invalid ico offset.\n"); | 121 SkCodecPrintf("Warning: invalid ico offset.\n"); |
| 122 continue; | 122 continue; |
| 123 } | 123 } |
| 124 | 124 |
| 125 // If we cannot skip, assume we have reached the end of the stream and | 125 // If we cannot skip, assume we have reached the end of the stream and |
| 126 // stop trying to make codecs | 126 // stop trying to make codecs |
| 127 if (inputStream.get()->skip(offset - bytesRead) != offset - bytesRead) { | 127 if (inputStream.get()->skip(offset - bytesRead) != offset - bytesRead) { |
| 128 SkDebugf("Warning: could not skip to ico offset.\n"); | 128 SkCodecPrintf("Warning: could not skip to ico offset.\n"); |
| 129 break; | 129 break; |
| 130 } | 130 } |
| 131 bytesRead = offset; | 131 bytesRead = offset; |
| 132 | 132 |
| 133 // Create a new stream for the embedded codec | 133 // Create a new stream for the embedded codec |
| 134 SkAutoTUnref<SkData> data( | 134 SkAutoTUnref<SkData> data( |
| 135 SkData::NewFromStream(inputStream.get(), size)); | 135 SkData::NewFromStream(inputStream.get(), size)); |
| 136 if (NULL == data.get()) { | 136 if (NULL == data.get()) { |
| 137 SkDebugf("Warning: could not create embedded stream.\n"); | 137 SkCodecPrintf("Warning: could not create embedded stream.\n"); |
| 138 break; | 138 break; |
| 139 } | 139 } |
| 140 SkAutoTDelete<SkMemoryStream> | 140 SkAutoTDelete<SkMemoryStream> |
| 141 embeddedStream(SkNEW_ARGS(SkMemoryStream, (data.get()))); | 141 embeddedStream(SkNEW_ARGS(SkMemoryStream, (data.get()))); |
| 142 bytesRead += size; | 142 bytesRead += size; |
| 143 | 143 |
| 144 // Check if the embedded codec is bmp or png and create the codec | 144 // Check if the embedded codec is bmp or png and create the codec |
| 145 const bool isPng = SkPngCodec::IsPng(embeddedStream); | 145 const bool isPng = SkPngCodec::IsPng(embeddedStream); |
| 146 SkAssertResult(embeddedStream->rewind()); | 146 SkAssertResult(embeddedStream->rewind()); |
| 147 SkCodec* codec = NULL; | 147 SkCodec* codec = NULL; |
| 148 if (isPng) { | 148 if (isPng) { |
| 149 codec = SkPngCodec::NewFromStream(embeddedStream.detach()); | 149 codec = SkPngCodec::NewFromStream(embeddedStream.detach()); |
| 150 } else { | 150 } else { |
| 151 codec = SkBmpCodec::NewFromIco(embeddedStream.detach()); | 151 codec = SkBmpCodec::NewFromIco(embeddedStream.detach()); |
| 152 } | 152 } |
| 153 | 153 |
| 154 // Save a valid codec | 154 // Save a valid codec |
| 155 if (NULL != codec) { | 155 if (NULL != codec) { |
| 156 codecs->push_back().reset(codec); | 156 codecs->push_back().reset(codec); |
| 157 } | 157 } |
| 158 } | 158 } |
| 159 | 159 |
| 160 // Recognize if there are no valid codecs | 160 // Recognize if there are no valid codecs |
| 161 if (0 == codecs->count()) { | 161 if (0 == codecs->count()) { |
| 162 SkDebugf("Error: could not find any valid embedded ico codecs.\n"); | 162 SkCodecPrintf("Error: could not find any valid embedded ico codecs.\n"); |
| 163 return NULL; | 163 return NULL; |
| 164 } | 164 } |
| 165 | 165 |
| 166 // Use the largest codec as a "suggestion" for image info | 166 // Use the largest codec as a "suggestion" for image info |
| 167 uint32_t maxSize = 0; | 167 uint32_t maxSize = 0; |
| 168 uint32_t maxIndex = 0; | 168 uint32_t maxIndex = 0; |
| 169 for (int32_t i = 0; i < codecs->count(); i++) { | 169 for (int32_t i = 0; i < codecs->count(); i++) { |
| 170 SkImageInfo info = codecs->operator[](i)->getInfo(); | 170 SkImageInfo info = codecs->operator[](i)->getInfo(); |
| 171 uint32_t size = info.width() * info.height(); | 171 uint32_t size = info.width() * info.height(); |
| 172 if (size > maxSize) { | 172 if (size > maxSize) { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 if (dstInfo.dimensions() == | 237 if (dstInfo.dimensions() == |
| 238 fEmbeddedCodecs->operator[](i)->getInfo().dimensions()) { | 238 fEmbeddedCodecs->operator[](i)->getInfo().dimensions()) { |
| 239 | 239 |
| 240 // Perform the decode | 240 // Perform the decode |
| 241 result = fEmbeddedCodecs->operator[](i)->getPixels(dstInfo, | 241 result = fEmbeddedCodecs->operator[](i)->getPixels(dstInfo, |
| 242 dst, dstRowBytes, &opts, ct, ptr); | 242 dst, dstRowBytes, &opts, ct, ptr); |
| 243 | 243 |
| 244 // On a fatal error, keep trying to find an image to decode | 244 // On a fatal error, keep trying to find an image to decode |
| 245 if (kInvalidConversion == result || kInvalidInput == result || | 245 if (kInvalidConversion == result || kInvalidInput == result || |
| 246 kInvalidScale == result) { | 246 kInvalidScale == result) { |
| 247 SkDebugf("Warning: Attempt to decode candidate ico failed.\n"); | 247 SkCodecPrintf("Warning: Attempt to decode candidate ico failed.\
n"); |
| 248 continue; | 248 continue; |
| 249 } | 249 } |
| 250 | 250 |
| 251 // On success or partial success, return the result | 251 // On success or partial success, return the result |
| 252 return result; | 252 return result; |
| 253 } | 253 } |
| 254 } | 254 } |
| 255 | 255 |
| 256 SkDebugf("Error: No matching candidate image in ico.\n"); | 256 SkCodecPrintf("Error: No matching candidate image in ico.\n"); |
| 257 return result; | 257 return result; |
| 258 } | 258 } |
| OLD | NEW |