scroggo | e7fc14b | 2015-10-02 13:14:46 -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 | */ |
| 7 | #ifndef SkSampler_DEFINED |
| 8 | #define SkSampler_DEFINED |
| 9 | |
msarett | a4970dc | 2016-01-11 07:23:23 -0800 | [diff] [blame] | 10 | #include "SkCodec.h" |
scroggo | 4f2a88c | 2016-10-17 14:32:41 -0700 | [diff] [blame] | 11 | #include "SkCodecPriv.h" |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 12 | #include "SkTypes.h" |
| 13 | |
| 14 | class SkSampler : public SkNoncopyable { |
| 15 | public: |
| 16 | /** |
| 17 | * Update the sampler to sample every sampleX'th pixel. Returns the |
| 18 | * width after sampling. |
| 19 | */ |
| 20 | int setSampleX(int sampleX) { |
| 21 | return this->onSetSampleX(sampleX); |
| 22 | } |
| 23 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 24 | /** |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 25 | * Update the sampler to sample every sampleY'th row. |
| 26 | */ |
| 27 | void setSampleY(int sampleY) { |
| 28 | fSampleY = sampleY; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Retrieve the value set for sampleY. |
| 33 | */ |
| 34 | int sampleY() const { |
| 35 | return fSampleY; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Based on fSampleY, return whether this row belongs in the output. |
| 40 | * |
scroggo | 4f2a88c | 2016-10-17 14:32:41 -0700 | [diff] [blame] | 41 | * @param row Row of the image, starting with the first row in the subset. |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 42 | */ |
| 43 | bool rowNeeded(int row) const { |
scroggo | aef247a | 2016-10-19 12:56:46 -0700 | [diff] [blame] | 44 | return (row - get_start_coord(fSampleY)) % fSampleY == 0; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /** |
Leon Scroggins III | e643a9e | 2018-08-03 16:15:04 -0400 | [diff] [blame] | 48 | * Fill the remainder of the destination with 0. |
| 49 | * |
| 50 | * 0 has a different meaning depending on the SkColorType. For color types |
| 51 | * with transparency, this means transparent. For k565 and kGray, 0 is |
| 52 | * black. |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 53 | * |
| 54 | * @param info |
| 55 | * Contains the color type of the rows to fill. |
Leon Scroggins III | e643a9e | 2018-08-03 16:15:04 -0400 | [diff] [blame] | 56 | * Contains the pixel width of the destination rows to fill |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 57 | * Contains the number of rows that we need to fill. |
| 58 | * |
| 59 | * @param dst |
Leon Scroggins III | e643a9e | 2018-08-03 16:15:04 -0400 | [diff] [blame] | 60 | * The destination row to fill. |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 61 | * |
| 62 | * @param rowBytes |
| 63 | * Stride in bytes of the destination. |
| 64 | * |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 65 | * @param zeroInit |
| 66 | * Indicates whether memory is already zero initialized. |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 67 | */ |
| 68 | static void Fill(const SkImageInfo& info, void* dst, size_t rowBytes, |
Leon Scroggins III | e643a9e | 2018-08-03 16:15:04 -0400 | [diff] [blame] | 69 | SkCodec::ZeroInitialized zeroInit); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Allow subclasses to implement unique versions of fill(). |
| 73 | */ |
| 74 | virtual void fill(const SkImageInfo& info, void* dst, size_t rowBytes, |
Leon Scroggins III | e643a9e | 2018-08-03 16:15:04 -0400 | [diff] [blame] | 75 | SkCodec::ZeroInitialized zeroInit) {} // FIXME: Can this be abstract? |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 76 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 77 | SkSampler() |
| 78 | : fSampleY(1) |
| 79 | {} |
| 80 | |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 81 | virtual ~SkSampler() {} |
| 82 | private: |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 83 | int fSampleY; |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 84 | |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 85 | virtual int onSetSampleX(int) = 0; |
| 86 | }; |
| 87 | |
| 88 | #endif // SkSampler_DEFINED |