scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 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 | |
msarett | b46e5e2 | 2015-07-30 11:36:40 -0700 | [diff] [blame] | 8 | #include "SkBmpCodec.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 9 | #include "SkCodec.h" |
| 10 | #include "SkData.h" |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 11 | #include "SkCodec_libgif.h" |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 12 | #include "SkCodec_libico.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 13 | #include "SkCodec_libpng.h" |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 14 | #include "SkCodec_wbmp.h" |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 15 | #include "SkCodecPriv.h" |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 16 | #ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 17 | #include "SkJpegCodec.h" |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 18 | #endif |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 19 | #include "SkStream.h" |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 20 | #include "SkWebpCodec.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 21 | |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 22 | struct DecoderProc { |
| 23 | bool (*IsFormat)(SkStream*); |
| 24 | SkCodec* (*NewFromStream)(SkStream*); |
| 25 | }; |
| 26 | |
| 27 | static const DecoderProc gDecoderProcs[] = { |
| 28 | { SkPngCodec::IsPng, SkPngCodec::NewFromStream }, |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 29 | #ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 30 | { SkJpegCodec::IsJpeg, SkJpegCodec::NewFromStream }, |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 31 | #endif |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 32 | { SkWebpCodec::IsWebp, SkWebpCodec::NewFromStream }, |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 33 | { SkGifCodec::IsGif, SkGifCodec::NewFromStream }, |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 34 | { SkIcoCodec::IsIco, SkIcoCodec::NewFromStream }, |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 35 | { SkBmpCodec::IsBmp, SkBmpCodec::NewFromStream }, |
| 36 | { SkWbmpCodec::IsWbmp, SkWbmpCodec::NewFromStream } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 39 | SkCodec* SkCodec::NewFromStream(SkStream* stream) { |
| 40 | if (!stream) { |
| 41 | return NULL; |
| 42 | } |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 43 | |
| 44 | SkAutoTDelete<SkStream> streamDeleter(stream); |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 45 | |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 46 | SkAutoTDelete<SkCodec> codec(NULL); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 47 | for (uint32_t i = 0; i < SK_ARRAY_COUNT(gDecoderProcs); i++) { |
| 48 | DecoderProc proc = gDecoderProcs[i]; |
| 49 | const bool correctFormat = proc.IsFormat(stream); |
| 50 | if (!stream->rewind()) { |
| 51 | return NULL; |
| 52 | } |
| 53 | if (correctFormat) { |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 54 | codec.reset(proc.NewFromStream(streamDeleter.detach())); |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 55 | break; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 56 | } |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 57 | } |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 58 | |
| 59 | // Set the max size at 128 megapixels (512 MB for kN32). |
| 60 | // This is about 4x smaller than a test image that takes a few minutes for |
| 61 | // dm to decode and draw. |
| 62 | const int32_t maxSize = 1 << 27; |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 63 | if (codec && codec->getInfo().width() * codec->getInfo().height() > maxSize) { |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 64 | SkCodecPrintf("Error: Image size too large, cannot decode.\n"); |
| 65 | return NULL; |
| 66 | } else { |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 67 | return codec.detach(); |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 68 | } |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | SkCodec* SkCodec::NewFromData(SkData* data) { |
| 72 | if (!data) { |
| 73 | return NULL; |
| 74 | } |
| 75 | return NewFromStream(SkNEW_ARGS(SkMemoryStream, (data))); |
| 76 | } |
| 77 | |
| 78 | SkCodec::SkCodec(const SkImageInfo& info, SkStream* stream) |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 79 | : fInfo(info) |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 80 | , fStream(stream) |
| 81 | , fNeedsRewind(false) |
| 82 | {} |
| 83 | |
scroggo | 9b2cdbf4 | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 84 | SkCodec::~SkCodec() {} |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 85 | |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 86 | bool SkCodec::rewindIfNeeded() { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 87 | // Store the value of fNeedsRewind so we can update it. Next read will |
| 88 | // require a rewind. |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 89 | const bool needsRewind = fNeedsRewind; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 90 | fNeedsRewind = true; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 91 | if (!needsRewind) { |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 92 | return true; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 93 | } |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 94 | |
| 95 | if (!fStream->rewind()) { |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | return this->onRewind(); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 100 | } |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 101 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 102 | SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 103 | const Options* options, SkPMColor ctable[], int* ctableCount) { |
| 104 | if (kUnknown_SkColorType == info.colorType()) { |
| 105 | return kInvalidConversion; |
| 106 | } |
| 107 | if (NULL == pixels) { |
| 108 | return kInvalidParameters; |
| 109 | } |
| 110 | if (rowBytes < info.minRowBytes()) { |
| 111 | return kInvalidParameters; |
| 112 | } |
| 113 | |
| 114 | if (kIndex_8_SkColorType == info.colorType()) { |
| 115 | if (NULL == ctable || NULL == ctableCount) { |
| 116 | return kInvalidParameters; |
| 117 | } |
| 118 | } else { |
| 119 | if (ctableCount) { |
| 120 | *ctableCount = 0; |
| 121 | } |
| 122 | ctableCount = NULL; |
| 123 | ctable = NULL; |
| 124 | } |
| 125 | |
| 126 | // Default options. |
| 127 | Options optsStorage; |
| 128 | if (NULL == options) { |
| 129 | options = &optsStorage; |
| 130 | } |
| 131 | const Result result = this->onGetPixels(info, pixels, rowBytes, *options, ctable, ctableCount); |
| 132 | |
| 133 | if ((kIncompleteInput == result || kSuccess == result) && ctableCount) { |
| 134 | SkASSERT(*ctableCount >= 0 && *ctableCount <= 256); |
| 135 | } |
| 136 | return result; |
| 137 | } |
| 138 | |
| 139 | SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 140 | return this->getPixels(info, pixels, rowBytes, NULL, NULL, NULL); |
| 141 | } |