blob: e6c4577174c3551afbdf1b228b658d1b668113fe [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com8a1c16f2008-12-17 15:59:43 +00008#ifndef SkScaledBitmapSampler_DEFINED
9#define SkScaledBitmapSampler_DEFINED
10
11#include "SkTypes.h"
reed@android.com11344262009-07-08 20:09:23 +000012#include "SkColor.h"
scroggo@google.com8d239242013-10-01 17:27:15 +000013#include "SkImageDecoder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014
15class SkBitmap;
16
17class SkScaledBitmapSampler {
18public:
19 SkScaledBitmapSampler(int origWidth, int origHeight, int cellSize);
rmistry@google.comd6176b02012-08-23 18:14:13 +000020
reed@android.com8a1c16f2008-12-17 15:59:43 +000021 int scaledWidth() const { return fScaledWidth; }
22 int scaledHeight() const { return fScaledHeight; }
rmistry@google.comd6176b02012-08-23 18:14:13 +000023
reed@android.com8a1c16f2008-12-17 15:59:43 +000024 int srcY0() const { return fY0; }
commit-bot@chromium.orgdac4a1d2013-10-08 19:40:18 +000025 int srcDX() const { return fDX; }
reed@android.com8a1c16f2008-12-17 15:59:43 +000026 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.com57f49692011-02-23 20:46:31 +000033 kRGBA, // 4 bytes per pixel
34 kRGB_565 // 2 bytes per pixel
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 };
36
37 // Given a dst bitmap (with pixels already allocated) and a src-config,
38 // prepares iterator to process the src colors and write them into dst.
39 // Returns false if the request cannot be fulfulled.
scroggo@google.com8d239242013-10-01 17:27:15 +000040 bool begin(SkBitmap* dst, SrcConfig sc, const SkImageDecoder& decoder,
41 const SkPMColor* = NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 // call with row of src pixels, for y = 0...scaledHeight-1.
43 // returns true if the row had non-opaque alpha in it
44 bool next(const uint8_t* SK_RESTRICT src);
45
commit-bot@chromium.orgdac4a1d2013-10-08 19:40:18 +000046 // Like next(), but specifies the y value of the source row, so the
47 // rows can come in any order. If the row is not part of the output
48 // sample, it will be skipped. Only sampleInterlaced OR next should
49 // be called for one SkScaledBitmapSampler.
50 bool sampleInterlaced(const uint8_t* SK_RESTRICT src, int srcY);
51
scroggo@google.com8d239242013-10-01 17:27:15 +000052 typedef bool (*RowProc)(void* SK_RESTRICT dstRow,
53 const uint8_t* SK_RESTRICT src,
54 int width, int deltaSrc, int y,
55 const SkPMColor[]);
56
reed@android.com8a1c16f2008-12-17 15:59:43 +000057private:
58 int fScaledWidth;
59 int fScaledHeight;
60
61 int fX0; // first X coord to sample
62 int fY0; // first Y coord (scanline) to sample
63 int fDX; // step between X samples
64 int fDY; // step between Y samples
reed@android.com11344262009-07-08 20:09:23 +000065
commit-bot@chromium.orgdac4a1d2013-10-08 19:40:18 +000066#ifdef SK_DEBUG
67 // Keep track of whether the caller is using next or sampleInterlaced.
68 // Only one can be used per sampler.
69 enum SampleMode {
70 kUninitialized_SampleMode,
71 kConsecutive_SampleMode,
72 kInterlaced_SampleMode,
73 };
74
75 SampleMode fSampleMode;
76#endif
77
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 // setup state
79 char* fDstRow; // points into bitmap's pixels
scroggo@google.come5f48242013-02-25 21:47:41 +000080 size_t fDstRowBytes;
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 int fCurrY; // used for dithering
rmistry@google.comd6176b02012-08-23 18:14:13 +000082 int fSrcPixelSize; // 1, 3, 4
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 RowProc fRowProc;
reed@android.com11344262009-07-08 20:09:23 +000084
85 // optional reference to the src colors if the src is a palette model
86 const SkPMColor* fCTable;
scroggo@google.com8d239242013-10-01 17:27:15 +000087
88#ifdef SK_DEBUG
89 // Helper class allowing a test to have access to fRowProc.
90 friend class RowProcTester;
91#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000092};
93
94#endif