msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [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 | */ |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 7 | #ifndef SkBmpCodec_DEFINED |
| 8 | #define SkBmpCodec_DEFINED |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 9 | |
| 10 | #include "SkCodec.h" |
msarett | ad8bcfe | 2016-03-07 07:09:03 -0800 | [diff] [blame] | 11 | #include "SkColorSpace.h" |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 12 | #include "SkColorTable.h" |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 13 | #include "SkImageInfo.h" |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 14 | #include "SkStream.h" |
| 15 | #include "SkSwizzler.h" |
| 16 | #include "SkTypes.h" |
| 17 | |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 18 | /* |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 19 | * This class enables code sharing between its bmp codec subclasses. The |
| 20 | * subclasses actually do the work. |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 21 | */ |
| 22 | class SkBmpCodec : public SkCodec { |
| 23 | public: |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 24 | static bool IsBmp(const void*, size_t); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 25 | |
| 26 | /* |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 27 | * Assumes IsBmp was called and returned true |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 28 | * Creates a bmp decoder |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 29 | * Reads enough of the stream to determine the image format |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 30 | */ |
| 31 | static SkCodec* NewFromStream(SkStream*); |
| 32 | |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 33 | /* |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 34 | * Creates a bmp decoder for a bmp embedded in ico |
| 35 | * Reads enough of the stream to determine the image format |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 36 | */ |
| 37 | static SkCodec* NewFromIco(SkStream*); |
| 38 | |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 39 | protected: |
| 40 | |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 41 | SkBmpCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream, |
| 42 | uint16_t bitsPerPixel, SkCodec::SkScanlineOrder rowOrder); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 43 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 44 | SkEncodedFormat onGetEncodedFormat() const override { return kBMP_SkEncodedFormat; } |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 45 | |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 46 | /* |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 47 | * Read enough of the stream to initialize the SkBmpCodec. Returns a bool |
| 48 | * representing success or failure. If it returned true, and codecOut was |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 49 | * not nullptr, it will be set to a new SkBmpCodec. |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 50 | * Does *not* take ownership of the passed in SkStream. |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 51 | */ |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 52 | static bool ReadHeader(SkStream*, bool inIco, SkCodec** codecOut); |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 53 | |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 54 | bool onRewind() override; |
| 55 | |
scroggo | 79e378d | 2015-04-01 07:39:40 -0700 | [diff] [blame] | 56 | /* |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 57 | * Returns whether this BMP is part of an ICO image. |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 58 | */ |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 59 | bool inIco() const { |
| 60 | return this->onInIco(); |
| 61 | } |
| 62 | |
| 63 | virtual bool onInIco() const { |
| 64 | return false; |
| 65 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 66 | |
| 67 | /* |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 68 | * Get the destination row number corresponding to the encoded row number. |
| 69 | * For kTopDown, we simply return y, but for kBottomUp, the rows will be |
| 70 | * decoded in reverse order. |
| 71 | * |
| 72 | * @param y Iterates from 0 to height, indicating the current row. |
| 73 | * @param height The height of the current subset of the image that we are |
| 74 | * decoding. This is generally equal to the full height |
| 75 | * when we want to decode the full or one when we are |
| 76 | * sampling. |
| 77 | */ |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 78 | int32_t getDstRow(int32_t y, int32_t height) const; |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 79 | |
| 80 | /* |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 81 | * Accessors used by subclasses |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 82 | */ |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 83 | uint16_t bitsPerPixel() const { return fBitsPerPixel; } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 84 | SkScanlineOrder onGetScanlineOrder() const override { return fRowOrder; } |
msarett | 9b9497e | 2016-02-11 13:29:36 -0800 | [diff] [blame] | 85 | size_t srcRowBytes() const { return fSrcRowBytes; } |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * To be overriden by bmp subclasses, which provide unique implementations. |
| 89 | * Performs subclass specific setup. |
| 90 | * |
| 91 | * @param dstInfo Contains output information. Height specifies |
| 92 | * the total number of rows that will be decoded. |
| 93 | * @param options Additonal options to pass to the decoder. |
| 94 | * @param inputColorPtr Client-provided memory for a color table. Must |
| 95 | * be enough for 256 colors. This will be |
| 96 | * populated with colors if the encoded image uses |
| 97 | * a color table. |
| 98 | * @param inputColorCount If the encoded image uses a color table, this |
| 99 | * will be set to the number of colors in the |
| 100 | * color table. |
| 101 | */ |
| 102 | virtual SkCodec::Result prepareToDecode(const SkImageInfo& dstInfo, |
| 103 | const SkCodec::Options& options, SkPMColor inputColorPtr[], |
| 104 | int* inputColorCount) = 0; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 105 | |
| 106 | private: |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 107 | |
| 108 | /* |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 109 | * Creates a bmp decoder |
| 110 | * Reads enough of the stream to determine the image format |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 111 | */ |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 112 | static SkCodec* NewFromStream(SkStream*, bool inIco); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 113 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 114 | /* |
| 115 | * Decodes the next dstInfo.height() lines. |
| 116 | * |
| 117 | * onGetPixels() uses this for full image decodes. |
| 118 | * SkScaledCodec::onGetPixels() uses the scanline decoder to call this with |
| 119 | * dstInfo.height() = 1, in order to implement sampling. |
| 120 | * A potential future use is to allow the caller to decode a subset of the |
| 121 | * lines in the image. |
| 122 | * |
| 123 | * @param dstInfo Contains output information. Height specifies the |
| 124 | * number of rows to decode at this time. |
| 125 | * @param dst Memory location to store output pixels |
| 126 | * @param dstRowBytes Bytes in a row of the destination |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 127 | * @return Number of rows successfully decoded |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 128 | */ |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 129 | virtual int decodeRows(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 130 | const Options& opts) = 0; |
| 131 | |
msarett | 9b9497e | 2016-02-11 13:29:36 -0800 | [diff] [blame] | 132 | virtual bool skipRows(int count); |
| 133 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 134 | Result onStartScanlineDecode(const SkImageInfo& dstInfo, const SkCodec::Options&, |
| 135 | SkPMColor inputColorPtr[], int* inputColorCount) override; |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 136 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 137 | int onGetScanlines(void* dst, int count, size_t rowBytes) override; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 138 | |
msarett | 9b9497e | 2016-02-11 13:29:36 -0800 | [diff] [blame] | 139 | bool onSkipScanlines(int count) override; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 140 | |
| 141 | const uint16_t fBitsPerPixel; |
| 142 | const SkScanlineOrder fRowOrder; |
msarett | 9b9497e | 2016-02-11 13:29:36 -0800 | [diff] [blame] | 143 | const size_t fSrcRowBytes; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 144 | |
| 145 | typedef SkCodec INHERITED; |
| 146 | }; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 147 | |
| 148 | #endif |