Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 8 | #ifndef SkImageEncoderPriv_DEFINED |
| 9 | #define SkImageEncoderPriv_DEFINED |
| 10 | |
| 11 | #include "SkImageEncoder.h" |
| 12 | #include "SkTRegistry.h" |
| 13 | |
| 14 | #ifndef SK_SUPPORT_LEGACY_IMAGE_ENCODER_CLASS |
| 15 | |
| 16 | // TODO(halcanary): replace this class and registry system with something simpler. |
| 17 | class SkImageEncoder { |
| 18 | public: |
| 19 | typedef SkEncodedImageFormat Type; |
| 20 | static SkImageEncoder* Create(SkEncodedImageFormat); |
| 21 | |
| 22 | virtual ~SkImageEncoder() {} |
| 23 | |
| 24 | bool encodeStream(SkWStream* dst, const SkBitmap& src, int quality) { |
| 25 | return this->onEncode(dst, src, SkMin32(100, SkMax32(0, quality))); |
| 26 | } |
| 27 | |
| 28 | protected: |
| 29 | /** |
| 30 | * Encode bitmap 'bm' in the desired format, writing results to |
| 31 | * stream 'stream', at quality level 'quality' (which can be in |
| 32 | * range 0-100). |
| 33 | */ |
| 34 | virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) = 0; |
| 35 | }; |
| 36 | |
| 37 | #endif // SK_SUPPORT_LEGACY_IMAGE_ENCODER_CLASS |
| 38 | |
| 39 | // This macro declares a global (i.e., non-class owned) creation entry point |
| 40 | // for each encoder (e.g., CreateJPEGImageEncoder) |
| 41 | #define DECLARE_ENCODER_CREATOR(codec) \ |
| 42 | SK_API SkImageEncoder *Create ## codec (); |
| 43 | |
| 44 | // This macro defines the global creation entry point for each encoder. Each |
| 45 | // encoder implementation that registers with the encoder factory must call it. |
| 46 | #define DEFINE_ENCODER_CREATOR(codec) \ |
| 47 | SkImageEncoder* Create##codec() { return new Sk##codec; } |
| 48 | |
| 49 | // All the encoders known by Skia. Note that, depending on the compiler settings, |
| 50 | // not all of these will be available |
| 51 | DECLARE_ENCODER_CREATOR(JPEGImageEncoder); |
| 52 | DECLARE_ENCODER_CREATOR(PNGImageEncoder); |
| 53 | DECLARE_ENCODER_CREATOR(KTXImageEncoder); |
| 54 | DECLARE_ENCODER_CREATOR(WEBPImageEncoder); |
| 55 | |
| 56 | #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
| 57 | SkImageEncoder* CreateImageEncoder_CG(SkImageEncoder::Type type); |
| 58 | #endif |
| 59 | |
| 60 | #if defined(SK_BUILD_FOR_WIN) |
| 61 | SkImageEncoder* CreateImageEncoder_WIC(SkImageEncoder::Type type); |
| 62 | #endif |
| 63 | |
| 64 | // Typedef to make registering encoder callback easier |
| 65 | // This has to be defined outside SkImageEncoder. :( |
| 66 | typedef SkTRegistry<SkImageEncoder*(*)(SkImageEncoder::Type)> SkImageEncoder_EncodeReg; |
| 67 | |
| 68 | #endif // SkImageEncoderPriv_DEFINED |