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