epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #ifndef SkScaledBitmapSampler_DEFINED |
| 9 | #define SkScaledBitmapSampler_DEFINED |
| 10 | |
| 11 | #include "SkTypes.h" |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 12 | #include "SkColor.h" |
scroggo@google.com | 8d23924 | 2013-10-01 17:27:15 +0000 | [diff] [blame] | 13 | #include "SkImageDecoder.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | |
| 15 | class SkBitmap; |
| 16 | |
| 17 | class SkScaledBitmapSampler { |
| 18 | public: |
| 19 | SkScaledBitmapSampler(int origWidth, int origHeight, int cellSize); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 20 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | int scaledWidth() const { return fScaledWidth; } |
| 22 | int scaledHeight() const { return fScaledHeight; } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 23 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | int srcY0() const { return fY0; } |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 25 | int srcDX() const { return fDX; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | int srcDY() const { return fDY; } |
| 27 | |
| 28 | enum SrcConfig { |
| 29 | kGray, // 1 byte per pixel |
| 30 | kIndex, // 1 byte per pixel |
| 31 | kRGB, // 3 bytes per pixel |
| 32 | kRGBX, // 4 byes per pixel (ignore 4th) |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 33 | kRGBA, // 4 bytes per pixel |
| 34 | kRGB_565 // 2 bytes per pixel |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
krajcevski | 407d7c9 | 2014-06-09 14:29:11 -0700 | [diff] [blame] | 37 | struct Options { |
| 38 | bool fDither; |
| 39 | bool fPremultiplyAlpha; |
| 40 | bool fSkipZeros; |
| 41 | explicit Options(const SkImageDecoder &dec) |
| 42 | : fDither(dec.getDitherImage()) |
| 43 | , fPremultiplyAlpha(!dec.getRequireUnpremultipliedColors()) |
| 44 | , fSkipZeros(dec.getSkipWritingZeroes()) |
| 45 | { } |
| 46 | }; |
| 47 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | // Given a dst bitmap (with pixels already allocated) and a src-config, |
| 49 | // prepares iterator to process the src colors and write them into dst. |
| 50 | // Returns false if the request cannot be fulfulled. |
scroggo@google.com | 8d23924 | 2013-10-01 17:27:15 +0000 | [diff] [blame] | 51 | bool begin(SkBitmap* dst, SrcConfig sc, const SkImageDecoder& decoder, |
Tom Hudson | 2880df2 | 2015-10-29 09:55:42 -0400 | [diff] [blame] | 52 | const SkPMColor* = nullptr); |
krajcevski | 407d7c9 | 2014-06-09 14:29:11 -0700 | [diff] [blame] | 53 | bool begin(SkBitmap* dst, SrcConfig sc, const Options& opts, |
Tom Hudson | 2880df2 | 2015-10-29 09:55:42 -0400 | [diff] [blame] | 54 | const SkPMColor* = nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | // call with row of src pixels, for y = 0...scaledHeight-1. |
| 56 | // returns true if the row had non-opaque alpha in it |
| 57 | bool next(const uint8_t* SK_RESTRICT src); |
| 58 | |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 59 | // Like next(), but specifies the y value of the source row, so the |
| 60 | // rows can come in any order. If the row is not part of the output |
| 61 | // sample, it will be skipped. Only sampleInterlaced OR next should |
| 62 | // be called for one SkScaledBitmapSampler. |
| 63 | bool sampleInterlaced(const uint8_t* SK_RESTRICT src, int srcY); |
| 64 | |
scroggo@google.com | 8d23924 | 2013-10-01 17:27:15 +0000 | [diff] [blame] | 65 | typedef bool (*RowProc)(void* SK_RESTRICT dstRow, |
| 66 | const uint8_t* SK_RESTRICT src, |
| 67 | int width, int deltaSrc, int y, |
| 68 | const SkPMColor[]); |
| 69 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | private: |
| 71 | int fScaledWidth; |
| 72 | int fScaledHeight; |
| 73 | |
| 74 | int fX0; // first X coord to sample |
| 75 | int fY0; // first Y coord (scanline) to sample |
| 76 | int fDX; // step between X samples |
| 77 | int fDY; // step between Y samples |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 78 | |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 79 | #ifdef SK_DEBUG |
| 80 | // Keep track of whether the caller is using next or sampleInterlaced. |
| 81 | // Only one can be used per sampler. |
| 82 | enum SampleMode { |
| 83 | kUninitialized_SampleMode, |
| 84 | kConsecutive_SampleMode, |
| 85 | kInterlaced_SampleMode, |
| 86 | }; |
| 87 | |
| 88 | SampleMode fSampleMode; |
| 89 | #endif |
| 90 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 91 | // setup state |
| 92 | char* fDstRow; // points into bitmap's pixels |
scroggo@google.com | e5f4824 | 2013-02-25 21:47:41 +0000 | [diff] [blame] | 93 | size_t fDstRowBytes; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | int fCurrY; // used for dithering |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 95 | int fSrcPixelSize; // 1, 3, 4 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | RowProc fRowProc; |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 97 | |
| 98 | // optional reference to the src colors if the src is a palette model |
| 99 | const SkPMColor* fCTable; |
scroggo@google.com | 8d23924 | 2013-10-01 17:27:15 +0000 | [diff] [blame] | 100 | |
| 101 | #ifdef SK_DEBUG |
| 102 | // Helper class allowing a test to have access to fRowProc. |
| 103 | friend class RowProcTester; |
| 104 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | #endif |