michael@0: michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: #ifndef SkScaledBitmapSampler_DEFINED michael@0: #define SkScaledBitmapSampler_DEFINED michael@0: michael@0: #include "SkTypes.h" michael@0: #include "SkColor.h" michael@0: #include "SkImageDecoder.h" michael@0: michael@0: class SkBitmap; michael@0: michael@0: class SkScaledBitmapSampler { michael@0: public: michael@0: SkScaledBitmapSampler(int origWidth, int origHeight, int cellSize); michael@0: michael@0: int scaledWidth() const { return fScaledWidth; } michael@0: int scaledHeight() const { return fScaledHeight; } michael@0: michael@0: int srcY0() const { return fY0; } michael@0: int srcDX() const { return fDX; } michael@0: int srcDY() const { return fDY; } michael@0: michael@0: enum SrcConfig { michael@0: kGray, // 1 byte per pixel michael@0: kIndex, // 1 byte per pixel michael@0: kRGB, // 3 bytes per pixel michael@0: kRGBX, // 4 byes per pixel (ignore 4th) michael@0: kRGBA, // 4 bytes per pixel michael@0: kRGB_565 // 2 bytes per pixel michael@0: }; michael@0: michael@0: // Given a dst bitmap (with pixels already allocated) and a src-config, michael@0: // prepares iterator to process the src colors and write them into dst. michael@0: // Returns false if the request cannot be fulfulled. michael@0: bool begin(SkBitmap* dst, SrcConfig sc, const SkImageDecoder& decoder, michael@0: const SkPMColor* = NULL); michael@0: // call with row of src pixels, for y = 0...scaledHeight-1. michael@0: // returns true if the row had non-opaque alpha in it michael@0: bool next(const uint8_t* SK_RESTRICT src); michael@0: michael@0: // Like next(), but specifies the y value of the source row, so the michael@0: // rows can come in any order. If the row is not part of the output michael@0: // sample, it will be skipped. Only sampleInterlaced OR next should michael@0: // be called for one SkScaledBitmapSampler. michael@0: bool sampleInterlaced(const uint8_t* SK_RESTRICT src, int srcY); michael@0: michael@0: typedef bool (*RowProc)(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, michael@0: int width, int deltaSrc, int y, michael@0: const SkPMColor[]); michael@0: michael@0: private: michael@0: int fScaledWidth; michael@0: int fScaledHeight; michael@0: michael@0: int fX0; // first X coord to sample michael@0: int fY0; // first Y coord (scanline) to sample michael@0: int fDX; // step between X samples michael@0: int fDY; // step between Y samples michael@0: michael@0: #ifdef SK_DEBUG michael@0: // Keep track of whether the caller is using next or sampleInterlaced. michael@0: // Only one can be used per sampler. michael@0: enum SampleMode { michael@0: kUninitialized_SampleMode, michael@0: kConsecutive_SampleMode, michael@0: kInterlaced_SampleMode, michael@0: }; michael@0: michael@0: SampleMode fSampleMode; michael@0: #endif michael@0: michael@0: // setup state michael@0: char* fDstRow; // points into bitmap's pixels michael@0: size_t fDstRowBytes; michael@0: int fCurrY; // used for dithering michael@0: int fSrcPixelSize; // 1, 3, 4 michael@0: RowProc fRowProc; michael@0: michael@0: // optional reference to the src colors if the src is a palette model michael@0: const SkPMColor* fCTable; michael@0: michael@0: #ifdef SK_DEBUG michael@0: // Helper class allowing a test to have access to fRowProc. michael@0: friend class RowProcTester; michael@0: #endif michael@0: }; michael@0: michael@0: #endif