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  * For SkEncodedImageFormat::kWEBP, if quality is 100, it will use lossless compression. Otherwise
     31  * it will use lossy.
     32  *
     33  * For examples of encoding an image to a file or to a block of memory,
     34  * see tools/sk_tool_utils.h.
     35  */
     36 SK_API bool SkEncodeImage(SkWStream* dst, const SkPixmap& src,
     37                           SkEncodedImageFormat format, int quality);
     38 
     39 /**
     40  * The following helper function wraps SkEncodeImage().
     41  */
     42 inline bool SkEncodeImage(SkWStream* dst, const SkBitmap& src, SkEncodedImageFormat f, int q) {
     43     SkPixmap pixmap;
     44     return src.peekPixels(&pixmap) && SkEncodeImage(dst, pixmap, f, q);
     45 }
     46 
     47 /**
     48  * Encode SkPixmap in the given binary image format.
     49  *
     50  * @param  src     source pixels.
     51  * @param  format  image format, not all formats are supported.
     52  * @param  quality range from 0-100, this is supported by jpeg and webp.
     53  *                 higher values correspond to improved visual quality, but less compression.
     54  *
     55  * @return encoded data or nullptr if input is bad or format is unsupported.
     56  *
     57  * Will always return nullptr if Skia is compiled without image
     58  * encoders.
     59  *
     60  * For SkEncodedImageFormat::kWEBP, if quality is 100, it will use lossless compression. Otherwise
     61  * it will use lossy.
     62  */
     63 SK_API sk_sp<SkData> SkEncodePixmap(const SkPixmap& src, SkEncodedImageFormat format, int quality);
     64 
     65 /**
     66  *  Helper that extracts the pixmap from the bitmap, and then calls SkEncodePixmap()
     67  */
     68 SK_API sk_sp<SkData> SkEncodeBitmap(const SkBitmap& src, SkEncodedImageFormat format, int quality);
     69 
     70 #endif  // SkImageEncoder_DEFINED
     71