Home | History | Annotate | Download | only in core
      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  */
      8 #ifndef SkImageEncoder_DEFINED
      9 #define SkImageEncoder_DEFINED
     10 
     11 #include "SkTypes.h"
     12 
     13 class SkBitmap;
     14 class SkData;
     15 class SkWStream;
     16 
     17 class SkImageEncoder {
     18 public:
     19     enum Type {
     20         kUnknown_Type,
     21         kBMP_Type,
     22         kGIF_Type,
     23         kICO_Type,
     24         kJPEG_Type,
     25         kPNG_Type,
     26         kWBMP_Type,
     27         kWEBP_Type,
     28     };
     29     static SkImageEncoder* Create(Type);
     30 
     31     virtual ~SkImageEncoder();
     32 
     33     /*  Quality ranges from 0..100 */
     34     enum {
     35         kDefaultQuality = 80
     36     };
     37 
     38     /**
     39      *  Encode bitmap 'bm', returning the results in an SkData, at quality level
     40      *  'quality' (which can be in range 0-100). If the bitmap cannot be
     41      *  encoded, return null. On success, the caller is responsible for
     42      *  calling unref() on the data when they are finished.
     43      */
     44     SkData* encodeData(const SkBitmap&, int quality);
     45 
     46     /**
     47      * Encode bitmap 'bm' in the desired format, writing results to
     48      * file 'file', at quality level 'quality' (which can be in range
     49      * 0-100). Returns false on failure.
     50      */
     51     bool encodeFile(const char file[], const SkBitmap& bm, int quality);
     52 
     53     /**
     54      * Encode bitmap 'bm' in the desired format, writing results to
     55      * stream 'stream', at quality level 'quality' (which can be in
     56      * range 0-100). Returns false on failure.
     57      */
     58     bool encodeStream(SkWStream* stream, const SkBitmap& bm, int quality);
     59 
     60     static SkData* EncodeData(const SkBitmap&, Type, int quality);
     61     static bool EncodeFile(const char file[], const SkBitmap&, Type,
     62                            int quality);
     63     static bool EncodeStream(SkWStream*, const SkBitmap&, Type,
     64                            int quality);
     65 
     66 protected:
     67     /**
     68      * Encode bitmap 'bm' in the desired format, writing results to
     69      * stream 'stream', at quality level 'quality' (which can be in
     70      * range 0-100).
     71      *
     72      * This must be overridden by each SkImageEncoder implementation.
     73      */
     74     virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) = 0;
     75 };
     76 
     77 // This macro declares a global (i.e., non-class owned) creation entry point
     78 // for each encoder (e.g., CreateJPEGImageEncoder)
     79 #define DECLARE_ENCODER_CREATOR(codec)          \
     80     SkImageEncoder *Create ## codec ();
     81 
     82 // This macro defines the global creation entry point for each encoder. Each
     83 // encoder implementation that registers with the encoder factory must call it.
     84 #define DEFINE_ENCODER_CREATOR(codec)           \
     85     SkImageEncoder *Create ## codec () {        \
     86         return SkNEW( Sk ## codec );            \
     87     }
     88 
     89 // All the encoders known by Skia. Note that, depending on the compiler settings,
     90 // not all of these will be available
     91 /** An ARGBImageEncoder will always write out
     92  *  bitmap.width() * bitmap.height() * 4
     93  *  bytes.
     94  */
     95 DECLARE_ENCODER_CREATOR(ARGBImageEncoder);
     96 DECLARE_ENCODER_CREATOR(JPEGImageEncoder);
     97 DECLARE_ENCODER_CREATOR(PNGImageEncoder);
     98 DECLARE_ENCODER_CREATOR(WEBPImageEncoder);
     99 
    100 #endif
    101