OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2015 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 "SkCodec_libbmp.h" |
| 9 #include "SkCodec_libico.h" |
| 10 #include "SkCodec_libpng.h" |
| 11 #include "SkCodecPriv.h" |
| 12 #include "SkColorPriv.h" |
| 13 #include "SkData.h" |
| 14 #include "SkStream.h" |
| 15 #include "SkTDArray.h" |
| 16 #include "SkTSort.h" |
| 17 |
| 18 /* |
| 19 * Checks the start of the stream to see if the image is an Ico or Cur |
| 20 */ |
| 21 bool SkIcoCodec::IsIco(SkStream* stream) { |
| 22 const char icoSig[] = { '\x00', '\x00', '\x01', '\x00' }; |
| 23 const char curSig[] = { '\x00', '\x00', '\x02', '\x00' }; |
| 24 char buffer[sizeof(icoSig)]; |
| 25 return stream->read(buffer, sizeof(icoSig)) == sizeof(icoSig) && |
| 26 (!memcmp(buffer, icoSig, sizeof(icoSig)) || |
| 27 !memcmp(buffer, curSig, sizeof(curSig))); |
| 28 } |
| 29 |
| 30 /* |
| 31 * Assumes IsIco was called and returned true |
| 32 * Creates an Ico decoder |
| 33 * Reads enough of the stream to determine the image format |
| 34 */ |
| 35 SkCodec* SkIcoCodec::NewFromStream(SkStream* stream) { |
| 36 // Header size constants |
| 37 static const uint32_t kIcoDirectoryBytes = 6; |
| 38 static const uint32_t kIcoDirEntryBytes = 16; |
| 39 |
| 40 // Read the directory header |
| 41 SkAutoTDeleteArray<uint8_t> dirBuffer( |
| 42 SkNEW_ARRAY(uint8_t, kIcoDirectoryBytes)); |
| 43 if (stream->read(dirBuffer.get(), kIcoDirectoryBytes) != |
| 44 kIcoDirectoryBytes) { |
| 45 SkDebugf("Error: unable to read ico directory header.\n"); |
| 46 return NULL; |
| 47 } |
| 48 |
| 49 // Process the directory header |
| 50 const uint16_t numImages = get_short(dirBuffer.get(), 4); |
| 51 if (0 == numImages) { |
| 52 SkDebugf("Error: No images embedded in ico.\n"); |
| 53 return NULL; |
| 54 } |
| 55 |
| 56 // Ensure that we can read all of indicated directory entries |
| 57 SkAutoTDeleteArray<uint8_t> entryBuffer( |
| 58 SkNEW_ARRAY(uint8_t, numImages*kIcoDirEntryBytes)); |
| 59 if (stream->read(entryBuffer.get(), numImages*kIcoDirEntryBytes) != |
| 60 numImages*kIcoDirEntryBytes) { |
| 61 SkDebugf("Error: unable to read ico directory entries.\n"); |
| 62 return NULL; |
| 63 } |
| 64 |
| 65 // This structure is used to represent the vital information about entries |
| 66 // in the directory header. We will obtain this information for each |
| 67 // directory entry. |
| 68 struct Entry { |
| 69 uint32_t offset; |
| 70 uint32_t size; |
| 71 }; |
| 72 SkAutoTDeleteArray<Entry> directoryEntries(SkNEW_ARRAY(Entry, numImages)); |
| 73 |
| 74 // Iterate over directory entries |
| 75 for (uint32_t i = 0; i < numImages; i++) { |
| 76 // The directory entry contains information such as width, height, |
| 77 // bits per pixel, and number of colors in the color palette. We will |
| 78 // ignore these fields since they are repeated in the header of the |
| 79 // embedded image. In the event of an inconsistency, we would always |
| 80 // defer to the value in the embedded header anyway. |
| 81 |
| 82 // Specifies the size of the embedded image, including the header |
| 83 uint32_t size = get_int(entryBuffer.get(), 8 + i*kIcoDirEntryBytes); |
| 84 |
| 85 // Specifies the offset of the embedded image from the start of file. |
| 86 // It does not indicate the start of the pixel data, but rather the |
| 87 // start of the embedded image header. |
| 88 uint32_t offset = get_int(entryBuffer.get(), 12 + i*kIcoDirEntryBytes); |
| 89 |
| 90 // Save the vital fields |
| 91 directoryEntries.get()[i].offset = offset; |
| 92 directoryEntries.get()[i].size = size; |
| 93 } |
| 94 |
| 95 // It is "customary" that the embedded images will be stored in order of |
| 96 // increasing offset. However, the specification does not indicate that |
| 97 // they must be stored in this order, so we will not trust that this is the |
| 98 // case. Here we sort the embedded images by increasing offset. |
| 99 struct EntryLessThan { |
| 100 bool operator() (Entry a, Entry b) const { |
| 101 return a.offset < b.offset; |
| 102 } |
| 103 }; |
| 104 EntryLessThan lessThan; |
| 105 SkTQSort(directoryEntries.get(), directoryEntries.get() + numImages - 1, |
| 106 lessThan); |
| 107 |
| 108 // Now will construct a candidate codec for each of the embedded images |
| 109 uint32_t bytesRead = kIcoDirectoryBytes + numImages * kIcoDirEntryBytes; |
| 110 SkAutoTDelete<SkTArray<SkAutoTDelete<SkCodec>, true>> codecs( |
| 111 SkNEW_ARGS((SkTArray<SkAutoTDelete<SkCodec>, true>), (numImages))); |
| 112 for (uint32_t i = 0; i < numImages; i++) { |
| 113 uint32_t offset = directoryEntries.get()[i].offset; |
| 114 uint32_t size = directoryEntries.get()[i].size; |
| 115 |
| 116 // Ensure that the offset is valid |
| 117 if (offset < bytesRead) { |
| 118 SkDebugf("Warning: invalid ico offset.\n"); |
| 119 continue; |
| 120 } |
| 121 |
| 122 // If we cannot skip, assume we have reached the end of the stream and |
| 123 // stop trying to make codecs |
| 124 if (stream->skip(offset - bytesRead) != offset - bytesRead) { |
| 125 SkDebugf("Warning: could not skip to ico offset.\n"); |
| 126 break; |
| 127 } |
| 128 bytesRead = offset; |
| 129 |
| 130 // Create a new stream for the embedded codec |
| 131 SkAutoTUnref<SkData> data(SkData::NewFromStream(stream, size)); |
| 132 if (NULL == data.get()) { |
| 133 SkDebugf("Warning: could not create embedded stream.\n"); |
| 134 break; |
| 135 } |
| 136 SkAutoTDelete<SkMemoryStream> |
| 137 embeddedStream(SkNEW_ARGS(SkMemoryStream, (data.get()))); |
| 138 bytesRead += size; |
| 139 |
| 140 // Check if the embedded codec is bmp or png and create the codec |
| 141 const bool isPng = SkPngCodec::IsPng(embeddedStream); |
| 142 SkAssertResult(embeddedStream->rewind()); |
| 143 SkCodec* codec = NULL; |
| 144 if (isPng) { |
| 145 codec = SkPngCodec::NewFromStream(embeddedStream.detach()); |
| 146 } else { |
| 147 codec = SkBmpCodec::NewFromIco(embeddedStream.detach()); |
| 148 } |
| 149 |
| 150 // Save a valid codec |
| 151 if (NULL != codec) { |
| 152 codecs->push_back().reset(codec); |
| 153 } |
| 154 } |
| 155 |
| 156 // Recognize if there are no valid codecs |
| 157 if (0 == codecs->count()) { |
| 158 SkDebugf("Error: could not find any valid embedded ico codecs.\n"); |
| 159 return NULL; |
| 160 } |
| 161 |
| 162 // Use the largest codec as a "suggestion" for image info |
| 163 uint32_t maxSize = 0; |
| 164 uint32_t maxIndex = 0; |
| 165 for (int32_t i = 0; i < codecs->count(); i++) { |
| 166 SkImageInfo info = codecs->operator[](i)->getInfo(); |
| 167 uint32_t size = info.width() * info.height(); |
| 168 if (size > maxSize) { |
| 169 maxSize = size; |
| 170 maxIndex = i; |
| 171 } |
| 172 } |
| 173 SkImageInfo info = codecs->operator[](maxIndex)->getInfo(); |
| 174 |
| 175 // Note that stream is owned by the embedded codec, the ico does not need |
| 176 // direct access to the stream. |
| 177 return SkNEW_ARGS(SkIcoCodec, (info, codecs.detach())); |
| 178 } |
| 179 |
| 180 /* |
| 181 * Creates an instance of the decoder |
| 182 * Called only by NewFromStream |
| 183 */ |
| 184 SkIcoCodec::SkIcoCodec(const SkImageInfo& info, |
| 185 SkTArray<SkAutoTDelete<SkCodec>, true>* codecs) |
| 186 : INHERITED(info, NULL) |
| 187 , fEmbeddedCodecs(codecs) |
| 188 {} |
| 189 |
| 190 /* |
| 191 * Chooses the best dimensions given the desired scale |
| 192 */ |
| 193 SkISize SkIcoCodec::onGetScaledDimensions(float desiredScale) const { |
| 194 // We set the dimensions to the largest candidate image by default. |
| 195 // Regardless of the scale request, this is the largest image that we |
| 196 // will decode. |
| 197 if (desiredScale >= 1.0) { |
| 198 return this->getInfo().dimensions(); |
| 199 } |
| 200 |
| 201 int origWidth = this->getInfo().width(); |
| 202 int origHeight = this->getInfo().height(); |
| 203 float desiredSize = desiredScale * origWidth * origHeight; |
| 204 // At least one image will have smaller error than this initial value |
| 205 float minError = ((float) (origWidth * origHeight)) - desiredSize + 1.0f; |
| 206 int32_t minIndex = -1; |
| 207 for (int32_t i = 0; i < fEmbeddedCodecs->count(); i++) { |
| 208 int width = fEmbeddedCodecs->operator[](i)->getInfo().width(); |
| 209 int height = fEmbeddedCodecs->operator[](i)->getInfo().height(); |
| 210 float error = SkTAbs(((float) (width * height)) - desiredSize); |
| 211 if (error < minError) { |
| 212 minError = error; |
| 213 minIndex = i; |
| 214 } |
| 215 } |
| 216 SkASSERT(minIndex >= 0); |
| 217 |
| 218 return fEmbeddedCodecs->operator[](minIndex)->getInfo().dimensions(); |
| 219 } |
| 220 |
| 221 /* |
| 222 * Initiates the Ico decode |
| 223 */ |
| 224 SkCodec::Result SkIcoCodec::onGetPixels(const SkImageInfo& dstInfo, |
| 225 void* dst, size_t dstRowBytes, |
| 226 const Options& opts, SkPMColor* ct, |
| 227 int* ptr) { |
| 228 // We return invalid scale if there is no candidate image with matching |
| 229 // dimensions. |
| 230 Result result = kInvalidScale; |
| 231 for (int32_t i = 0; i < fEmbeddedCodecs->count(); i++) { |
| 232 // If the dimensions match, try to decode |
| 233 if (dstInfo.dimensions() == |
| 234 fEmbeddedCodecs->operator[](i)->getInfo().dimensions()) { |
| 235 |
| 236 // Perform the decode |
| 237 result = fEmbeddedCodecs->operator[](i)->getPixels(dstInfo, |
| 238 dst, dstRowBytes, &opts, ct, ptr); |
| 239 |
| 240 // On a fatal error, keep trying to find an image to decode |
| 241 if (kInvalidConversion == result || kInvalidInput == result || |
| 242 kInvalidScale == result) { |
| 243 SkDebugf("Warning: Attempt to decode candidate ico failed.\n"); |
| 244 continue; |
| 245 } |
| 246 |
| 247 // On success or partial success, return the result |
| 248 return result; |
| 249 } |
| 250 } |
| 251 |
| 252 SkDebugf("Error: No matching candidate image in ico.\n"); |
| 253 return result; |
| 254 } |
OLD | NEW |