Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2011 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 SkImageEncoder_DEFINED
      9 #define SkImageEncoder_DEFINED
     10 
     11 #include "SkBitmap.h"
     12 #include "SkData.h"
     13 #include "SkEncodedImageFormat.h"
     14 #include "SkStream.h"
     15 
     16 /**
     17  * Encode SkPixmap in the given binary image format.
     18  *
     19  * @param  dst     results are written to this stream.
     20  * @param  src     source pixels.
     21  * @param  format  image format, not all formats are supported.
     22  * @param  quality range from 0-100, this is supported by jpeg and webp.
     23  *                 higher values correspond to improved visual quality, but less compression.
     24  *
     25  * @return false iff input is bad or format is unsupported.
     26  *
     27  * Will always return false if Skia is compiled without image
     28  * encoders.
     29  *
     30  * Note that webp encodes will use webp lossy compression.
     31  *
     32  * For examples of encoding an image to a file or to a block of memory,
     33  * see tools/sk_tool_utils.h.
     34  */
     35 SK_API bool SkEncodeImage(SkWStream* dst, const SkPixmap& src,
     36                           SkEncodedImageFormat format, int quality);
     37 
     38 /**
     39  * The following helper function wraps SkEncodeImage().
     40  */
     41 inline bool SkEncodeImage(SkWStream* dst, const SkBitmap& src, SkEncodedImageFormat f, int q) {
     42     SkPixmap pixmap;
     43     return src.peekPixels(&pixmap) && SkEncodeImage(dst, pixmap, f, q);
     44 }
     45 
     46 /**
     47  * Encode SkPixmap in the given binary image format.
     48  *
     49  * @param  src     source pixels.
     50  * @param  format  image format, not all formats are supported.
     51  * @param  quality range from 0-100, this is supported by jpeg and webp.
     52  *                 higher values correspond to improved visual quality, but less compression.
     53  *
     54  * @return encoded data or nullptr if input is bad or format is unsupported.
     55  *
     56  * Will always return nullptr if Skia is compiled without image
     57  * encoders.
     58  *
     59  * Note that webp encodes will use webp lossy compression.
     60  */
     61 SK_API sk_sp<SkData> SkEncodePixmap(const SkPixmap& src, SkEncodedImageFormat format, int quality);
     62 
     63 /**
     64  *  Helper that extracts the pixmap from the bitmap, and then calls SkEncodePixmap()
     65  */
     66 SK_API sk_sp<SkData> SkEncodeBitmap(const SkBitmap& src, SkEncodedImageFormat format, int quality);
     67 
     68 #endif  // SkImageEncoder_DEFINED
     69