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