1cb93a386Sopenharmony_ci/* 2cb93a386Sopenharmony_ci * Copyright 2015 Google Inc. 3cb93a386Sopenharmony_ci * 4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be 5cb93a386Sopenharmony_ci * found in the LICENSE file. 6cb93a386Sopenharmony_ci */ 7cb93a386Sopenharmony_ci 8cb93a386Sopenharmony_ci#include "include/core/SkData.h" 9cb93a386Sopenharmony_ci#include "include/core/SkStream.h" 10cb93a386Sopenharmony_ci#include "include/private/SkColorData.h" 11cb93a386Sopenharmony_ci#include "include/private/SkTDArray.h" 12cb93a386Sopenharmony_ci#include "src/codec/SkBmpCodec.h" 13cb93a386Sopenharmony_ci#include "src/codec/SkCodecPriv.h" 14cb93a386Sopenharmony_ci#include "src/codec/SkIcoCodec.h" 15cb93a386Sopenharmony_ci#include "src/codec/SkPngCodec.h" 16cb93a386Sopenharmony_ci#include "src/core/SkStreamPriv.h" 17cb93a386Sopenharmony_ci#include "src/core/SkTSort.h" 18cb93a386Sopenharmony_ci 19cb93a386Sopenharmony_ci/* 20cb93a386Sopenharmony_ci * Checks the start of the stream to see if the image is an Ico or Cur 21cb93a386Sopenharmony_ci */ 22cb93a386Sopenharmony_cibool SkIcoCodec::IsIco(const void* buffer, size_t bytesRead) { 23cb93a386Sopenharmony_ci const char icoSig[] = { '\x00', '\x00', '\x01', '\x00' }; 24cb93a386Sopenharmony_ci const char curSig[] = { '\x00', '\x00', '\x02', '\x00' }; 25cb93a386Sopenharmony_ci return bytesRead >= sizeof(icoSig) && 26cb93a386Sopenharmony_ci (!memcmp(buffer, icoSig, sizeof(icoSig)) || 27cb93a386Sopenharmony_ci !memcmp(buffer, curSig, sizeof(curSig))); 28cb93a386Sopenharmony_ci} 29cb93a386Sopenharmony_ci 30cb93a386Sopenharmony_cistd::unique_ptr<SkCodec> SkIcoCodec::MakeFromStream(std::unique_ptr<SkStream> stream, 31cb93a386Sopenharmony_ci Result* result) { 32cb93a386Sopenharmony_ci // It is helpful to have the entire stream in a contiguous buffer. In some cases, 33cb93a386Sopenharmony_ci // this is already the case anyway, so this method is faster. In others, this is 34cb93a386Sopenharmony_ci // safer than the old method, which required allocating a block of memory whose 35cb93a386Sopenharmony_ci // byte size is stored in the stream as a uint32_t, and may result in a large or 36cb93a386Sopenharmony_ci // failed allocation. 37cb93a386Sopenharmony_ci sk_sp<SkData> data = nullptr; 38cb93a386Sopenharmony_ci if (stream->getMemoryBase()) { 39cb93a386Sopenharmony_ci // It is safe to make without copy because we'll hold onto the stream. 40cb93a386Sopenharmony_ci data = SkData::MakeWithoutCopy(stream->getMemoryBase(), stream->getLength()); 41cb93a386Sopenharmony_ci } else { 42cb93a386Sopenharmony_ci data = SkCopyStreamToData(stream.get()); 43cb93a386Sopenharmony_ci 44cb93a386Sopenharmony_ci // If we are forced to copy the stream to a data, we can go ahead and delete the stream. 45cb93a386Sopenharmony_ci stream.reset(nullptr); 46cb93a386Sopenharmony_ci } 47cb93a386Sopenharmony_ci 48cb93a386Sopenharmony_ci // Header size constants 49cb93a386Sopenharmony_ci constexpr uint32_t kIcoDirectoryBytes = 6; 50cb93a386Sopenharmony_ci constexpr uint32_t kIcoDirEntryBytes = 16; 51cb93a386Sopenharmony_ci 52cb93a386Sopenharmony_ci // Read the directory header 53cb93a386Sopenharmony_ci if (data->size() < kIcoDirectoryBytes) { 54cb93a386Sopenharmony_ci SkCodecPrintf("Error: unable to read ico directory header.\n"); 55cb93a386Sopenharmony_ci *result = kIncompleteInput; 56cb93a386Sopenharmony_ci return nullptr; 57cb93a386Sopenharmony_ci } 58cb93a386Sopenharmony_ci 59cb93a386Sopenharmony_ci // Process the directory header 60cb93a386Sopenharmony_ci const uint16_t numImages = get_short(data->bytes(), 4); 61cb93a386Sopenharmony_ci if (0 == numImages) { 62cb93a386Sopenharmony_ci SkCodecPrintf("Error: No images embedded in ico.\n"); 63cb93a386Sopenharmony_ci *result = kInvalidInput; 64cb93a386Sopenharmony_ci return nullptr; 65cb93a386Sopenharmony_ci } 66cb93a386Sopenharmony_ci 67cb93a386Sopenharmony_ci // This structure is used to represent the vital information about entries 68cb93a386Sopenharmony_ci // in the directory header. We will obtain this information for each 69cb93a386Sopenharmony_ci // directory entry. 70cb93a386Sopenharmony_ci struct Entry { 71cb93a386Sopenharmony_ci uint32_t offset; 72cb93a386Sopenharmony_ci uint32_t size; 73cb93a386Sopenharmony_ci#ifdef ICO_CODEC_HW_HIGH_QUALITY_DECODE 74cb93a386Sopenharmony_ci uint16_t bitsPerPixel; 75cb93a386Sopenharmony_ci int width; 76cb93a386Sopenharmony_ci int height; 77cb93a386Sopenharmony_ci#endif 78cb93a386Sopenharmony_ci }; 79cb93a386Sopenharmony_ci SkAutoFree dirEntryBuffer(sk_malloc_canfail(sizeof(Entry) * numImages)); 80cb93a386Sopenharmony_ci if (!dirEntryBuffer) { 81cb93a386Sopenharmony_ci SkCodecPrintf("Error: OOM allocating ICO directory for %i images.\n", 82cb93a386Sopenharmony_ci numImages); 83cb93a386Sopenharmony_ci *result = kInternalError; 84cb93a386Sopenharmony_ci return nullptr; 85cb93a386Sopenharmony_ci } 86cb93a386Sopenharmony_ci auto* directoryEntries = reinterpret_cast<Entry*>(dirEntryBuffer.get()); 87cb93a386Sopenharmony_ci 88cb93a386Sopenharmony_ci // Iterate over directory entries 89cb93a386Sopenharmony_ci for (uint32_t i = 0; i < numImages; i++) { 90cb93a386Sopenharmony_ci const uint8_t* entryBuffer = data->bytes() + kIcoDirectoryBytes + i * kIcoDirEntryBytes; 91cb93a386Sopenharmony_ci if (data->size() < kIcoDirectoryBytes + (i+1) * kIcoDirEntryBytes) { 92cb93a386Sopenharmony_ci SkCodecPrintf("Error: Dir entries truncated in ico.\n"); 93cb93a386Sopenharmony_ci *result = kIncompleteInput; 94cb93a386Sopenharmony_ci return nullptr; 95cb93a386Sopenharmony_ci } 96cb93a386Sopenharmony_ci 97cb93a386Sopenharmony_ci // The directory entry contains information such as width, height, 98cb93a386Sopenharmony_ci // bits per pixel, and number of colors in the color palette. We will 99cb93a386Sopenharmony_ci // ignore these fields since they are repeated in the header of the 100cb93a386Sopenharmony_ci // embedded image. In the event of an inconsistency, we would always 101cb93a386Sopenharmony_ci // defer to the value in the embedded header anyway. 102cb93a386Sopenharmony_ci 103cb93a386Sopenharmony_ci // Specifies the size of the embedded image, including the header 104cb93a386Sopenharmony_ci uint32_t size = get_int(entryBuffer, 8); 105cb93a386Sopenharmony_ci 106cb93a386Sopenharmony_ci // Specifies the offset of the embedded image from the start of file. 107cb93a386Sopenharmony_ci // It does not indicate the start of the pixel data, but rather the 108cb93a386Sopenharmony_ci // start of the embedded image header. 109cb93a386Sopenharmony_ci uint32_t offset = get_int(entryBuffer, 12); 110cb93a386Sopenharmony_ci 111cb93a386Sopenharmony_ci // Save the vital fields 112cb93a386Sopenharmony_ci directoryEntries[i].offset = offset; 113cb93a386Sopenharmony_ci directoryEntries[i].size = size; 114cb93a386Sopenharmony_ci#ifdef ICO_CODEC_HW_HIGH_QUALITY_DECODE 115cb93a386Sopenharmony_ci // store bitsPerPixel, width, height and save the vital fields 116cb93a386Sopenharmony_ci uint16_t bitsPerPixel = get_short(entryBuffer, 6); 117cb93a386Sopenharmony_ci // Storing them in int (instead of matching uint8_t) is so we can record 118cb93a386Sopenharmony_ci // dimensions of size 256 (which is what a zero byte really means) 119cb93a386Sopenharmony_ci static const int maxSize = 256; 120cb93a386Sopenharmony_ci int width = static_cast<int>(get_byte(entryBuffer, 0)); 121cb93a386Sopenharmony_ci int height = static_cast<int>(get_byte(entryBuffer, 1)); 122cb93a386Sopenharmony_ci if (width == 0) { 123cb93a386Sopenharmony_ci width = maxSize; 124cb93a386Sopenharmony_ci } 125cb93a386Sopenharmony_ci if (height == 0) { 126cb93a386Sopenharmony_ci height = maxSize; 127cb93a386Sopenharmony_ci } 128cb93a386Sopenharmony_ci 129cb93a386Sopenharmony_ci directoryEntries[i].bitsPerPixel = bitsPerPixel; 130cb93a386Sopenharmony_ci directoryEntries[i].width = width; 131cb93a386Sopenharmony_ci directoryEntries[i].height = height; 132cb93a386Sopenharmony_ci#endif 133cb93a386Sopenharmony_ci } 134cb93a386Sopenharmony_ci 135cb93a386Sopenharmony_ci // Default Result, if no valid embedded codecs are found. 136cb93a386Sopenharmony_ci *result = kInvalidInput; 137cb93a386Sopenharmony_ci 138cb93a386Sopenharmony_ci // It is "customary" that the embedded images will be stored in order of 139cb93a386Sopenharmony_ci // increasing offset. However, the specification does not indicate that 140cb93a386Sopenharmony_ci // they must be stored in this order, so we will not trust that this is the 141cb93a386Sopenharmony_ci // case. Here we sort the embedded images by increasing offset. 142cb93a386Sopenharmony_ci#ifdef ICO_CODEC_HW_HIGH_QUALITY_DECODE 143cb93a386Sopenharmony_ci struct EntryGreaterThan { 144cb93a386Sopenharmony_ci bool operator()(Entry a, Entry b) const { 145cb93a386Sopenharmony_ci return (a.width * a.height == b.width * b.height) ? (a.bitsPerPixel > b.bitsPerPixel) : 146cb93a386Sopenharmony_ci (a.width * a.height > b.width * b.height); 147cb93a386Sopenharmony_ci } 148cb93a386Sopenharmony_ci }; 149cb93a386Sopenharmony_ci EntryGreaterThan greaterThan; 150cb93a386Sopenharmony_ci SkTQSort(directoryEntries, directoryEntries + numImages, greaterThan); 151cb93a386Sopenharmony_ci#else 152cb93a386Sopenharmony_ci struct EntryLessThan { 153cb93a386Sopenharmony_ci bool operator() (Entry a, Entry b) const { 154cb93a386Sopenharmony_ci return a.offset < b.offset; 155cb93a386Sopenharmony_ci } 156cb93a386Sopenharmony_ci }; 157cb93a386Sopenharmony_ci EntryLessThan lessThan; 158cb93a386Sopenharmony_ci SkTQSort(directoryEntries, directoryEntries + numImages, lessThan); 159cb93a386Sopenharmony_ci#endif 160cb93a386Sopenharmony_ci 161cb93a386Sopenharmony_ci // Now will construct a candidate codec for each of the embedded images 162cb93a386Sopenharmony_ci uint32_t bytesRead = kIcoDirectoryBytes + numImages * kIcoDirEntryBytes; 163cb93a386Sopenharmony_ci std::unique_ptr<SkTArray<std::unique_ptr<SkCodec>, true>> codecs( 164cb93a386Sopenharmony_ci new SkTArray<std::unique_ptr<SkCodec>, true>(numImages)); 165cb93a386Sopenharmony_ci for (uint32_t i = 0; i < numImages; i++) { 166cb93a386Sopenharmony_ci uint32_t offset = directoryEntries[i].offset; 167cb93a386Sopenharmony_ci uint32_t size = directoryEntries[i].size; 168cb93a386Sopenharmony_ci 169cb93a386Sopenharmony_ci // Ensure that the offset is valid 170cb93a386Sopenharmony_ci if (offset < bytesRead) { 171cb93a386Sopenharmony_ci SkCodecPrintf("Warning: invalid ico offset.\n"); 172cb93a386Sopenharmony_ci continue; 173cb93a386Sopenharmony_ci } 174cb93a386Sopenharmony_ci 175cb93a386Sopenharmony_ci // If we cannot skip, assume we have reached the end of the stream and 176cb93a386Sopenharmony_ci // stop trying to make codecs 177cb93a386Sopenharmony_ci if (offset >= data->size()) { 178cb93a386Sopenharmony_ci SkCodecPrintf("Warning: could not skip to ico offset.\n"); 179cb93a386Sopenharmony_ci break; 180cb93a386Sopenharmony_ci } 181cb93a386Sopenharmony_ci bytesRead = offset; 182cb93a386Sopenharmony_ci 183cb93a386Sopenharmony_ci if (offset + size > data->size()) { 184cb93a386Sopenharmony_ci SkCodecPrintf("Warning: could not create embedded stream.\n"); 185cb93a386Sopenharmony_ci *result = kIncompleteInput; 186cb93a386Sopenharmony_ci break; 187cb93a386Sopenharmony_ci } 188cb93a386Sopenharmony_ci 189cb93a386Sopenharmony_ci sk_sp<SkData> embeddedData(SkData::MakeSubset(data.get(), offset, size)); 190cb93a386Sopenharmony_ci auto embeddedStream = SkMemoryStream::Make(embeddedData); 191cb93a386Sopenharmony_ci bytesRead += size; 192cb93a386Sopenharmony_ci 193cb93a386Sopenharmony_ci // Check if the embedded codec is bmp or png and create the codec 194cb93a386Sopenharmony_ci std::unique_ptr<SkCodec> codec; 195cb93a386Sopenharmony_ci Result ignoredResult; 196cb93a386Sopenharmony_ci if (SkPngCodec::IsPng(embeddedData->bytes(), embeddedData->size())) { 197cb93a386Sopenharmony_ci codec = SkPngCodec::MakeFromStream(std::move(embeddedStream), &ignoredResult); 198cb93a386Sopenharmony_ci } else { 199cb93a386Sopenharmony_ci codec = SkBmpCodec::MakeFromIco(std::move(embeddedStream), &ignoredResult); 200cb93a386Sopenharmony_ci } 201cb93a386Sopenharmony_ci 202cb93a386Sopenharmony_ci if (nullptr != codec) { 203cb93a386Sopenharmony_ci codecs->push_back().reset(codec.release()); 204cb93a386Sopenharmony_ci } 205cb93a386Sopenharmony_ci } 206cb93a386Sopenharmony_ci 207cb93a386Sopenharmony_ci if (0 == codecs->count()) { 208cb93a386Sopenharmony_ci SkCodecPrintf("Error: could not find any valid embedded ico codecs.\n"); 209cb93a386Sopenharmony_ci return nullptr; 210cb93a386Sopenharmony_ci } 211cb93a386Sopenharmony_ci 212cb93a386Sopenharmony_ci // Use the largest codec as a "suggestion" for image info 213cb93a386Sopenharmony_ci size_t maxSize = 0; 214cb93a386Sopenharmony_ci int maxIndex = 0; 215cb93a386Sopenharmony_ci for (int i = 0; i < codecs->count(); i++) { 216cb93a386Sopenharmony_ci SkImageInfo info = codecs->operator[](i)->getInfo(); 217cb93a386Sopenharmony_ci size_t size = info.computeMinByteSize(); 218cb93a386Sopenharmony_ci 219cb93a386Sopenharmony_ci if (size > maxSize) { 220cb93a386Sopenharmony_ci maxSize = size; 221cb93a386Sopenharmony_ci maxIndex = i; 222cb93a386Sopenharmony_ci } 223cb93a386Sopenharmony_ci } 224cb93a386Sopenharmony_ci 225cb93a386Sopenharmony_ci auto maxInfo = codecs->operator[](maxIndex)->getEncodedInfo().copy(); 226cb93a386Sopenharmony_ci 227cb93a386Sopenharmony_ci *result = kSuccess; 228cb93a386Sopenharmony_ci return std::unique_ptr<SkCodec>(new SkIcoCodec(std::move(maxInfo), std::move(stream), 229cb93a386Sopenharmony_ci codecs.release())); 230cb93a386Sopenharmony_ci} 231cb93a386Sopenharmony_ci 232cb93a386Sopenharmony_ciSkIcoCodec::SkIcoCodec(SkEncodedInfo&& info, std::unique_ptr<SkStream> stream, 233cb93a386Sopenharmony_ci SkTArray<std::unique_ptr<SkCodec>, true>* codecs) 234cb93a386Sopenharmony_ci // The source skcms_PixelFormat will not be used. The embedded 235cb93a386Sopenharmony_ci // codec's will be used instead. 236cb93a386Sopenharmony_ci : INHERITED(std::move(info), skcms_PixelFormat(), std::move(stream)) 237cb93a386Sopenharmony_ci , fEmbeddedCodecs(codecs) 238cb93a386Sopenharmony_ci , fCurrCodec(nullptr) 239cb93a386Sopenharmony_ci{} 240cb93a386Sopenharmony_ci 241cb93a386Sopenharmony_ci/* 242cb93a386Sopenharmony_ci * Chooses the best dimensions given the desired scale 243cb93a386Sopenharmony_ci */ 244cb93a386Sopenharmony_ciSkISize SkIcoCodec::onGetScaledDimensions(float desiredScale) const { 245cb93a386Sopenharmony_ci // We set the dimensions to the largest candidate image by default. 246cb93a386Sopenharmony_ci // Regardless of the scale request, this is the largest image that we 247cb93a386Sopenharmony_ci // will decode. 248cb93a386Sopenharmony_ci int origWidth = this->dimensions().width(); 249cb93a386Sopenharmony_ci int origHeight = this->dimensions().height(); 250cb93a386Sopenharmony_ci#ifdef ICO_CODEC_HW_HIGH_QUALITY_DECODE 251cb93a386Sopenharmony_ci // desiredScale is max(desireWidth/origWidth, desireHeight/origHeight) 252cb93a386Sopenharmony_ci float desiredSize = desiredScale * origWidth * desiredScale * origHeight; 253cb93a386Sopenharmony_ci#else 254cb93a386Sopenharmony_ci float desiredSize = desiredScale * origWidth * origHeight; 255cb93a386Sopenharmony_ci#endif 256cb93a386Sopenharmony_ci // At least one image will have smaller error than this initial value 257cb93a386Sopenharmony_ci float minError = ((float) (origWidth * origHeight)) - desiredSize + 1.0f; 258cb93a386Sopenharmony_ci int32_t minIndex = -1; 259cb93a386Sopenharmony_ci for (int32_t i = 0; i < fEmbeddedCodecs->count(); i++) { 260cb93a386Sopenharmony_ci auto dimensions = fEmbeddedCodecs->operator[](i)->dimensions(); 261cb93a386Sopenharmony_ci int width = dimensions.width(); 262cb93a386Sopenharmony_ci int height = dimensions.height(); 263cb93a386Sopenharmony_ci float error = SkTAbs(((float) (width * height)) - desiredSize); 264cb93a386Sopenharmony_ci if (error < minError) { 265cb93a386Sopenharmony_ci minError = error; 266cb93a386Sopenharmony_ci minIndex = i; 267cb93a386Sopenharmony_ci } 268cb93a386Sopenharmony_ci } 269cb93a386Sopenharmony_ci SkASSERT(minIndex >= 0); 270cb93a386Sopenharmony_ci 271cb93a386Sopenharmony_ci return fEmbeddedCodecs->operator[](minIndex)->dimensions(); 272cb93a386Sopenharmony_ci} 273cb93a386Sopenharmony_ci 274cb93a386Sopenharmony_ciint SkIcoCodec::chooseCodec(const SkISize& requestedSize, int startIndex) { 275cb93a386Sopenharmony_ci SkASSERT(startIndex >= 0); 276cb93a386Sopenharmony_ci 277cb93a386Sopenharmony_ci // FIXME: Cache the index from onGetScaledDimensions? 278cb93a386Sopenharmony_ci for (int i = startIndex; i < fEmbeddedCodecs->count(); i++) { 279cb93a386Sopenharmony_ci if (fEmbeddedCodecs->operator[](i)->dimensions() == requestedSize) { 280cb93a386Sopenharmony_ci return i; 281cb93a386Sopenharmony_ci } 282cb93a386Sopenharmony_ci } 283cb93a386Sopenharmony_ci 284cb93a386Sopenharmony_ci return -1; 285cb93a386Sopenharmony_ci} 286cb93a386Sopenharmony_ci 287cb93a386Sopenharmony_cibool SkIcoCodec::onDimensionsSupported(const SkISize& dim) { 288cb93a386Sopenharmony_ci return this->chooseCodec(dim, 0) >= 0; 289cb93a386Sopenharmony_ci} 290cb93a386Sopenharmony_ci 291cb93a386Sopenharmony_ci/* 292cb93a386Sopenharmony_ci * Initiates the Ico decode 293cb93a386Sopenharmony_ci */ 294cb93a386Sopenharmony_ciSkCodec::Result SkIcoCodec::onGetPixels(const SkImageInfo& dstInfo, 295cb93a386Sopenharmony_ci void* dst, size_t dstRowBytes, 296cb93a386Sopenharmony_ci const Options& opts, 297cb93a386Sopenharmony_ci int* rowsDecoded) { 298cb93a386Sopenharmony_ci if (opts.fSubset) { 299cb93a386Sopenharmony_ci // Subsets are not supported. 300cb93a386Sopenharmony_ci return kUnimplemented; 301cb93a386Sopenharmony_ci } 302cb93a386Sopenharmony_ci 303cb93a386Sopenharmony_ci int index = 0; 304cb93a386Sopenharmony_ci SkCodec::Result result = kInvalidScale; 305cb93a386Sopenharmony_ci while (true) { 306cb93a386Sopenharmony_ci index = this->chooseCodec(dstInfo.dimensions(), index); 307cb93a386Sopenharmony_ci if (index < 0) { 308cb93a386Sopenharmony_ci break; 309cb93a386Sopenharmony_ci } 310cb93a386Sopenharmony_ci 311cb93a386Sopenharmony_ci SkCodec* embeddedCodec = fEmbeddedCodecs->operator[](index).get(); 312cb93a386Sopenharmony_ci result = embeddedCodec->getPixels(dstInfo, dst, dstRowBytes, &opts); 313cb93a386Sopenharmony_ci switch (result) { 314cb93a386Sopenharmony_ci case kSuccess: 315cb93a386Sopenharmony_ci case kIncompleteInput: 316cb93a386Sopenharmony_ci // The embedded codec will handle filling incomplete images, so we will indicate 317cb93a386Sopenharmony_ci // that all of the rows are initialized. 318cb93a386Sopenharmony_ci *rowsDecoded = dstInfo.height(); 319cb93a386Sopenharmony_ci return result; 320cb93a386Sopenharmony_ci default: 321cb93a386Sopenharmony_ci // Continue trying to find a valid embedded codec on a failed decode. 322cb93a386Sopenharmony_ci break; 323cb93a386Sopenharmony_ci } 324cb93a386Sopenharmony_ci 325cb93a386Sopenharmony_ci index++; 326cb93a386Sopenharmony_ci } 327cb93a386Sopenharmony_ci 328cb93a386Sopenharmony_ci SkCodecPrintf("Error: No matching candidate image in ico.\n"); 329cb93a386Sopenharmony_ci return result; 330cb93a386Sopenharmony_ci} 331cb93a386Sopenharmony_ci 332cb93a386Sopenharmony_ciSkCodec::Result SkIcoCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, 333cb93a386Sopenharmony_ci const SkCodec::Options& options) { 334cb93a386Sopenharmony_ci int index = 0; 335cb93a386Sopenharmony_ci SkCodec::Result result = kInvalidScale; 336cb93a386Sopenharmony_ci while (true) { 337cb93a386Sopenharmony_ci index = this->chooseCodec(dstInfo.dimensions(), index); 338cb93a386Sopenharmony_ci if (index < 0) { 339cb93a386Sopenharmony_ci break; 340cb93a386Sopenharmony_ci } 341cb93a386Sopenharmony_ci 342cb93a386Sopenharmony_ci SkCodec* embeddedCodec = fEmbeddedCodecs->operator[](index).get(); 343cb93a386Sopenharmony_ci result = embeddedCodec->startScanlineDecode(dstInfo, &options); 344cb93a386Sopenharmony_ci if (kSuccess == result) { 345cb93a386Sopenharmony_ci fCurrCodec = embeddedCodec; 346cb93a386Sopenharmony_ci return result; 347cb93a386Sopenharmony_ci } 348cb93a386Sopenharmony_ci 349cb93a386Sopenharmony_ci index++; 350cb93a386Sopenharmony_ci } 351cb93a386Sopenharmony_ci 352cb93a386Sopenharmony_ci SkCodecPrintf("Error: No matching candidate image in ico.\n"); 353cb93a386Sopenharmony_ci return result; 354cb93a386Sopenharmony_ci} 355cb93a386Sopenharmony_ci 356cb93a386Sopenharmony_ciint SkIcoCodec::onGetScanlines(void* dst, int count, size_t rowBytes) { 357cb93a386Sopenharmony_ci SkASSERT(fCurrCodec); 358cb93a386Sopenharmony_ci return fCurrCodec->getScanlines(dst, count, rowBytes); 359cb93a386Sopenharmony_ci} 360cb93a386Sopenharmony_ci 361cb93a386Sopenharmony_cibool SkIcoCodec::onSkipScanlines(int count) { 362cb93a386Sopenharmony_ci SkASSERT(fCurrCodec); 363cb93a386Sopenharmony_ci return fCurrCodec->skipScanlines(count); 364cb93a386Sopenharmony_ci} 365cb93a386Sopenharmony_ci 366cb93a386Sopenharmony_ciSkCodec::Result SkIcoCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo, 367cb93a386Sopenharmony_ci void* pixels, size_t rowBytes, const SkCodec::Options& options) { 368cb93a386Sopenharmony_ci int index = 0; 369cb93a386Sopenharmony_ci while (true) { 370cb93a386Sopenharmony_ci index = this->chooseCodec(dstInfo.dimensions(), index); 371cb93a386Sopenharmony_ci if (index < 0) { 372cb93a386Sopenharmony_ci break; 373cb93a386Sopenharmony_ci } 374cb93a386Sopenharmony_ci 375cb93a386Sopenharmony_ci SkCodec* embeddedCodec = fEmbeddedCodecs->operator[](index).get(); 376cb93a386Sopenharmony_ci switch (embeddedCodec->startIncrementalDecode(dstInfo, 377cb93a386Sopenharmony_ci pixels, rowBytes, &options)) { 378cb93a386Sopenharmony_ci case kSuccess: 379cb93a386Sopenharmony_ci fCurrCodec = embeddedCodec; 380cb93a386Sopenharmony_ci return kSuccess; 381cb93a386Sopenharmony_ci case kUnimplemented: 382cb93a386Sopenharmony_ci // FIXME: embeddedCodec is a BMP. If scanline decoding would work, 383cb93a386Sopenharmony_ci // return kUnimplemented so that SkSampledCodec will fall through 384cb93a386Sopenharmony_ci // to use the scanline decoder. 385cb93a386Sopenharmony_ci // Note that calling startScanlineDecode will require an extra 386cb93a386Sopenharmony_ci // rewind. The embedded codec has an SkMemoryStream, which is 387cb93a386Sopenharmony_ci // cheap to rewind, though it will do extra work re-reading the 388cb93a386Sopenharmony_ci // header. 389cb93a386Sopenharmony_ci // Also note that we pass nullptr for Options. This is because 390cb93a386Sopenharmony_ci // Options that are valid for incremental decoding may not be 391cb93a386Sopenharmony_ci // valid for scanline decoding. 392cb93a386Sopenharmony_ci // Once BMP supports incremental decoding this workaround can go 393cb93a386Sopenharmony_ci // away. 394cb93a386Sopenharmony_ci if (embeddedCodec->startScanlineDecode(dstInfo) == kSuccess) { 395cb93a386Sopenharmony_ci return kUnimplemented; 396cb93a386Sopenharmony_ci } 397cb93a386Sopenharmony_ci // Move on to the next embedded codec. 398cb93a386Sopenharmony_ci break; 399cb93a386Sopenharmony_ci default: 400cb93a386Sopenharmony_ci break; 401cb93a386Sopenharmony_ci } 402cb93a386Sopenharmony_ci 403cb93a386Sopenharmony_ci index++; 404cb93a386Sopenharmony_ci } 405cb93a386Sopenharmony_ci 406cb93a386Sopenharmony_ci SkCodecPrintf("Error: No matching candidate image in ico.\n"); 407cb93a386Sopenharmony_ci return kInvalidScale; 408cb93a386Sopenharmony_ci} 409cb93a386Sopenharmony_ci 410cb93a386Sopenharmony_ciSkCodec::Result SkIcoCodec::onIncrementalDecode(int* rowsDecoded) { 411cb93a386Sopenharmony_ci SkASSERT(fCurrCodec); 412cb93a386Sopenharmony_ci return fCurrCodec->incrementalDecode(rowsDecoded); 413cb93a386Sopenharmony_ci} 414cb93a386Sopenharmony_ci 415cb93a386Sopenharmony_ciSkCodec::SkScanlineOrder SkIcoCodec::onGetScanlineOrder() const { 416cb93a386Sopenharmony_ci // FIXME: This function will possibly return the wrong value if it is called 417cb93a386Sopenharmony_ci // before startScanlineDecode()/startIncrementalDecode(). 418cb93a386Sopenharmony_ci if (fCurrCodec) { 419cb93a386Sopenharmony_ci return fCurrCodec->getScanlineOrder(); 420cb93a386Sopenharmony_ci } 421cb93a386Sopenharmony_ci 422cb93a386Sopenharmony_ci return INHERITED::onGetScanlineOrder(); 423cb93a386Sopenharmony_ci} 424cb93a386Sopenharmony_ci 425cb93a386Sopenharmony_ciSkSampler* SkIcoCodec::getSampler(bool createIfNecessary) { 426cb93a386Sopenharmony_ci if (fCurrCodec) { 427cb93a386Sopenharmony_ci return fCurrCodec->getSampler(createIfNecessary); 428cb93a386Sopenharmony_ci } 429cb93a386Sopenharmony_ci 430cb93a386Sopenharmony_ci return nullptr; 431cb93a386Sopenharmony_ci} 432